blob: 4beda54cbdb385dc718c7e0c9c95f778ad8a86aa [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
Minos Galanakis12b493f2023-08-11 15:22:45 +010065#define MBEDTLS_POP_TARGET_PRAGMA
66#endif
67#if defined(MBEDTLS_POP_TARGET_PRAGMA)
Simon Butcherdef90f42018-03-14 17:02:16 +000068/* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
69 * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
70 * These constants are guaranteed to be the same, though, so we suppress the
71 * warning when including intsafe.h.
Kevin Kane0ec1e682016-12-15 09:27:16 -080072 */
Minos Galanakisa277b212023-08-09 16:32:22 +010073#pragma warning(push )
74#pragma warning(disable : 4005)
Kevin Kane0ec1e682016-12-15 09:27:16 -080075#endif
76#include <intsafe.h>
Minos Galanakis12b493f2023-08-11 15:22:45 +010077#if defined(MBEDTLS_POP_TARGET_PRAGMA)
Minos Galanakisa277b212023-08-09 16:32:22 +010078#pragma warning(pop)
Kevin Kane0ec1e682016-12-15 09:27:16 -080079#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#else
81#include <time.h>
82#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100083#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000086#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020087#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088#include <sys/types.h>
89#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020090#if defined(__MBED__)
91#include <platform/mbed_retarget.h>
92#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020094#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060095#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000096#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020097#endif
98
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020099/*
100 * Item in a verification chain: cert and flags for it
101 */
102typedef struct {
103 mbedtls_x509_crt *crt;
104 uint32_t flags;
105} x509_crt_verify_chain_item;
106
107/*
108 * Max size of verification chain: end-entity + intermediates + trusted root
109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +0200111
Gilles Peskineffb92da2021-06-02 00:03:26 +0200112/* Default profile. Do not remove items unless there are serious security
113 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200114const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
115{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200116 /* Hashes from SHA-256 and above. Note that this selection
117 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
119 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
120 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200121 0xFFFFFFF, /* Any PK alg */
Valerio Setti8c3404f2023-06-26 15:49:48 +0200122#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200123 /* Curves at or above 128-bit security level. Note that this selection
124 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
128 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
129 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
130 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200131 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200132#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200133 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200134#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200135 2048,
136};
137
Gilles Peskineffb92da2021-06-02 00:03:26 +0200138/* Next-generation profile. Currently identical to the default, but may
139 * be tightened at any time. */
140const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200141{
142 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
144 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
145 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200146 0xFFFFFFF, /* Any PK alg */
147#if defined(MBEDTLS_ECP_C)
148 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
150 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
151 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
152 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
153 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
154 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
155 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200156#else
157 0,
158#endif
159 2048,
160};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200161
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200162/*
163 * NSA Suite B Profile
164 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200165const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
166{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200167 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
169 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200170 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
172 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200173#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200174 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
176 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200177#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200178 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200179#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200180 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200181};
182
183/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200184 * Empty / all-forbidden profile
185 */
186const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
187{
188 0,
189 0,
190 0,
191 (uint32_t) -1,
192};
193
194/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200196 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
199 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200{
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (md_alg == MBEDTLS_MD_NONE) {
202 return -1;
203 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
206 return 0;
207 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210}
211
212/*
213 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200214 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
217 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218{
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (pk_alg == MBEDTLS_PK_NONE) {
220 return -1;
221 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
224 return 0;
225 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200228}
229
230/*
231 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200232 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200233 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
235 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200236{
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200238
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200239#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
241 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
242 return 0;
243 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200246 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200247#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200248
Valerio Setti8c3404f2023-06-26 15:49:48 +0200249#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200251 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Setti97207782023-05-18 18:59:06 +0200253 const mbedtls_ecp_group_id gid = mbedtls_pk_get_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (gid == MBEDTLS_ECP_DP_NONE) {
256 return -1;
257 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
260 return 0;
261 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200264 }
Valerio Setti8c3404f2023-06-26 15:49:48 +0200265#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200268}
269
270/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000271 * Like memcmp, but case-insensitive and always returns -1 if different
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000274{
275 size_t i;
276 unsigned char diff;
277 const unsigned char *n1 = s1, *n2 = s2;
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000280 diff = n1[i] ^ n2[i];
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000283 continue;
284 }
285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (diff == 32 &&
287 ((n1[i] >= 'a' && n1[i] <= 'z') ||
288 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
289 continue;
290 }
291
292 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000293 }
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000296}
297
298/*
299 * Return 0 if name matches wildcard, -1 otherwise
300 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000302{
303 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000305
306 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
308 return -1;
309 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 for (i = 0; i < cn_len; ++i) {
312 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000313 cn_idx = i;
314 break;
315 }
316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (cn_idx == 0) {
319 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000320 }
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (cn_len - cn_idx == name->len - 1 &&
323 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
324 return 0;
325 }
326
327 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000328}
329
330/*
331 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
332 * variations (but not all).
333 *
334 * Return 0 if equal, -1 otherwise.
335 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000337{
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000339 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 memcmp(a->p, b->p, b->len) == 0) {
341 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000342 }
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
345 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000346 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 x509_memcasecmp(a->p, b->p, b->len) == 0) {
348 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000349 }
350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000352}
353
354/*
355 * Compare two X.509 Names (aka rdnSequence).
356 *
357 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
358 * we sometimes return unequal when the full algorithm would return equal,
359 * but never the other way. (In particular, we don't do Unicode normalisation
360 * or space folding.)
361 *
362 * Return 0 if equal, -1 otherwise.
363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000365{
366 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 while (a != NULL || b != NULL) {
368 if (a == NULL || b == NULL) {
369 return -1;
370 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000371
372 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000374 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
376 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000377 }
378
379 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (x509_string_cmp(&a->val, &b->val) != 0) {
381 return -1;
382 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000383
384 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (a->next_merged != b->next_merged) {
386 return -1;
387 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000388
389 a = a->next;
390 b = b->next;
391 }
392
393 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000395}
396
397/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200398 * Reset (init or clear) a verify_chain
399 */
400static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200402{
403 size_t i;
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200406 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000407 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200408 }
409
410 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000411
412#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
413 ver_chain->trust_ca_cb_result = NULL;
414#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200415}
416
417/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
419 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100420static int x509_get_version(unsigned char **p,
421 const unsigned char *end,
422 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423{
Janos Follath865b3eb2019-12-16 11:46:15 +0000424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425 size_t len;
426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
428 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
429 0)) != 0) {
430 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 }
434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 }
437
438 end = *p + len;
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (*p != end) {
445 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
446 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
447 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450}
451
452/*
453 * Validity ::= SEQUENCE {
454 * notBefore Time,
455 * notAfter Time }
456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457static int x509_get_dates(unsigned char **p,
458 const unsigned char *end,
459 mbedtls_x509_time *from,
460 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461{
Janos Follath865b3eb2019-12-16 11:46:15 +0000462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463 size_t len;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
466 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
467 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
468 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
470 end = *p + len;
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
473 return ret;
474 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
477 return ret;
478 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (*p != end) {
481 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
482 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
483 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486}
487
488/*
489 * X.509 v2/v3 unique identifier (not parsed)
490 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491static int x509_get_uid(unsigned char **p,
492 const unsigned char *end,
493 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494{
Janos Follath865b3eb2019-12-16 11:46:15 +0000495 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (*p == end) {
498 return 0;
499 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500
501 uid->tag = **p;
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
504 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
505 n)) != 0) {
506 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
507 return 0;
508 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 }
512
513 uid->p = *p;
514 *p += uid->len;
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517}
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519static int x509_get_basic_constraints(unsigned char **p,
520 const unsigned char *end,
521 int *ca_istrue,
522 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523{
Janos Follath865b3eb2019-12-16 11:46:15 +0000524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 size_t len;
526
527 /*
528 * BasicConstraints ::= SEQUENCE {
529 * cA BOOLEAN DEFAULT FALSE,
530 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
531 */
532 *ca_istrue = 0; /* DEFAULT FALSE */
533 *max_pathlen = 0; /* endless */
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
536 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
537 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (*p == end) {
541 return 0;
542 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
545 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
546 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
547 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (ret != 0) {
550 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
551 }
552
553 if (*ca_istrue != 0) {
554 *ca_istrue = 1;
555 }
556 }
557
558 if (*p == end) {
559 return 0;
560 }
561
562 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
563 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
564 }
565
566 if (*p != end) {
567 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
568 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
569 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570
Andrzej Kurek16050742020-04-14 09:49:52 -0400571 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
572 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (*max_pathlen == INT_MAX) {
574 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
575 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
576 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400577
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578 (*max_pathlen)++;
579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581}
582
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583/*
584 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
585 *
586 * KeyPurposeId ::= OBJECT IDENTIFIER
587 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100588static int x509_get_ext_key_usage(unsigned char **p,
589 const unsigned char *end,
590 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591{
Janos Follath865b3eb2019-12-16 11:46:15 +0000592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
595 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
596 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597
598 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (ext_key_usage->buf.p == NULL) {
600 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
601 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
602 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605}
606
607/*
toth92ga41954d2021-02-12 16:11:17 +0100608 * SubjectKeyIdentifier ::= KeyIdentifier
609 *
610 * KeyIdentifier ::= OCTET STRING
611 */
612static int x509_get_subject_key_id(unsigned char **p,
613 const unsigned char *end,
614 mbedtls_x509_buf *subject_key_id)
615{
616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
617 size_t len = 0u;
618
619 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
620 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100621 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100622 }
623
Przemek Stekiel61aed062023-05-08 11:14:36 +0200624 subject_key_id->len = len;
625 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
626 subject_key_id->p = *p;
627 *p += len;
628
toth92gd96027a2021-04-27 15:41:25 +0200629 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100630 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
631 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200632 }
633
toth92ga41954d2021-02-12 16:11:17 +0100634 return 0;
635}
636
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100637/*
toth92g8d435a02021-05-10 15:16:33 +0200638 * AuthorityKeyIdentifier ::= SEQUENCE {
639 * keyIdentifier [0] KeyIdentifier OPTIONAL,
640 * authorityCertIssuer [1] GeneralNames OPTIONAL,
641 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
642 *
643 * KeyIdentifier ::= OCTET STRING
644 */
645static int x509_get_authority_key_id(unsigned char **p,
646 unsigned char *end,
647 mbedtls_x509_authority *authority_key_id)
648{
649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
650 size_t len = 0u;
651
652 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100653 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100654 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200655 }
656
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100657 if (*p + len != end) {
658 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
659 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
660 }
661
Przemek Stekieled9fb782023-05-03 16:27:25 +0200662 ret = mbedtls_asn1_get_tag(p, end, &len,
663 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
664
665 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200666 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200667 authority_key_id->keyIdentifier.len = len;
668 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200669 /* Setting tag of the keyIdentfier intentionally to 0x04.
670 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200671 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200672 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
673
674 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200675 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
676 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200677 }
678
679 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200680 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200681 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
682 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100683 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200684 /* authorityCertIssuer and authorityCertSerialNumber MUST both
685 be present or both be absent. At this point we expect to have both. */
686 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200687 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200688 /* "end" also includes the CertSerialNumber field so "len" shall be used */
689 ret = mbedtls_x509_get_subject_alt_name_ext(p,
690 (*p+len),
691 &authority_key_id->authorityCertIssuer);
692 if (ret != 0) {
693 return ret;
694 }
695
696 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
697 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
698 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
699 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
700 }
701 authority_key_id->authorityCertSerialNumber.len = len;
702 authority_key_id->authorityCertSerialNumber.p = *p;
703 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
704 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200705 }
706
707 if (*p != end) {
708 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
709 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
710 }
711
712 return 0;
713}
714
715/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200716 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
717 *
718 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
719 *
720 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
721 *
722 * PolicyInformation ::= SEQUENCE {
723 * policyIdentifier CertPolicyId,
724 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
725 * PolicyQualifierInfo OPTIONAL }
726 *
727 * CertPolicyId ::= OBJECT IDENTIFIER
728 *
729 * PolicyQualifierInfo ::= SEQUENCE {
730 * policyQualifierId PolicyQualifierId,
731 * qualifier ANY DEFINED BY policyQualifierId }
732 *
733 * -- policyQualifierIds for Internet policy qualifiers
734 *
735 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
736 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
737 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
738 *
739 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
740 *
741 * Qualifier ::= CHOICE {
742 * cPSuri CPSuri,
743 * userNotice UserNotice }
744 *
745 * CPSuri ::= IA5String
746 *
747 * UserNotice ::= SEQUENCE {
748 * noticeRef NoticeReference OPTIONAL,
749 * explicitText DisplayText OPTIONAL }
750 *
751 * NoticeReference ::= SEQUENCE {
752 * organization DisplayText,
753 * noticeNumbers SEQUENCE OF INTEGER }
754 *
755 * DisplayText ::= CHOICE {
756 * ia5String IA5String (SIZE (1..200)),
757 * visibleString VisibleString (SIZE (1..200)),
758 * bmpString BMPString (SIZE (1..200)),
759 * utf8String UTF8String (SIZE (1..200)) }
760 *
761 * NOTE: we only parse and use anyPolicy without qualifiers at this point
762 * as defined in RFC 5280.
763 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100764static int x509_get_certificate_policies(unsigned char **p,
765 const unsigned char *end,
766 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200767{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300768 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200769 size_t len;
770 mbedtls_asn1_buf *buf;
771 mbedtls_asn1_sequence *cur = certificate_policies;
772
773 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 ret = mbedtls_asn1_get_tag(p, end, &len,
775 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
776 if (ret != 0) {
777 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
778 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (*p + len != end) {
781 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
782 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
783 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200784
785 /*
786 * Cannot be an empty sequence.
787 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (len == 0) {
789 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
790 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
791 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200794 mbedtls_x509_buf policy_oid;
795 const unsigned char *policy_end;
796
797 /*
798 * Get the policy sequence
799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
801 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
802 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
803 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200804
805 policy_end = *p + len;
806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
808 MBEDTLS_ASN1_OID)) != 0) {
809 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
810 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200811
812 policy_oid.tag = MBEDTLS_ASN1_OID;
813 policy_oid.len = len;
814 policy_oid.p = *p;
815
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300816 /*
817 * Only AnyPolicy is currently supported when enforcing policy.
818 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300820 /*
821 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200822 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300823 */
824 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
825 }
826
Ron Eldor74d9acc2019-03-21 14:00:03 +0200827 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (cur->buf.p != NULL) {
829 if (cur->next != NULL) {
830 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
831 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (cur->next == NULL) {
836 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
837 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
838 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200839
840 cur = cur->next;
841 }
842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200844 buf->tag = policy_oid.tag;
845 buf->p = policy_oid.p;
846 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300847
848 *p += len;
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 /*
851 * If there is an optional qualifier, then *p < policy_end
852 * Check the Qualifier len to verify it doesn't exceed policy_end.
853 */
854 if (*p < policy_end) {
855 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
856 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
857 0) {
858 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
859 }
Ron Eldor08063792019-05-13 16:38:39 +0300860 /*
861 * Skip the optional policy qualifiers.
862 */
863 *p += len;
864 }
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (*p != policy_end) {
867 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
868 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
869 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200870 }
871
872 /* Set final sequence entry's next pointer to NULL */
873 cur->next = NULL;
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (*p != end) {
876 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
877 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
878 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200881}
882
883/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884 * X.509 v3 extensions
885 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887static int x509_get_crt_ext(unsigned char **p,
888 const unsigned char *end,
889 mbedtls_x509_crt *crt,
890 mbedtls_x509_crt_ext_cb_t cb,
891 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892{
Janos Follath865b3eb2019-12-16 11:46:15 +0000893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200895 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (*p == end) {
898 return 0;
899 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
902 return ret;
903 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904
Hanno Becker12f62fb2019-02-12 17:22:36 +0000905 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907 /*
908 * Extension ::= SEQUENCE {
909 * extnID OBJECT IDENTIFIER,
910 * critical BOOLEAN DEFAULT FALSE,
911 * extnValue OCTET STRING }
912 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 int is_critical = 0; /* DEFAULT FALSE */
915 int ext_type = 0;
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
918 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
919 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
920 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921
922 end_ext_data = *p + len;
923
924 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
926 MBEDTLS_ASN1_OID)) != 0) {
927 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
928 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929
k-stachowiak463928a2018-07-24 12:50:59 +0200930 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 extn_oid.p = *p;
932 *p += extn_oid.len;
933
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
936 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
937 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
938 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
940 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
942 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
943 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
944 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200946 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 end_ext_octet = *p + len;
948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (end_ext_octet != end_ext_data) {
950 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
951 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
952 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
954 /*
955 * Detect supported extensions
956 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200960 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (cb != NULL) {
962 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
963 if (ret != 0 && is_critical) {
964 return ret;
965 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200966 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200967 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200968 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200969
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 /* No parser found, skip extension */
971 *p = end_ext_octet;
972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
976 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 continue;
979 }
980
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100981 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if ((crt->ext_types & ext_type) != 0) {
983 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
984 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100985
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 crt->ext_types |= ext_type;
987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 switch (ext_type) {
989 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
990 /* Parse basic constraints */
991 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
992 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
993 return ret;
994 }
995 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 case MBEDTLS_X509_EXT_KEY_USAGE:
998 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100999 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
1000 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 return ret;
1002 }
1003 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1006 /* Parse extended key usage */
1007 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1008 &crt->ext_key_usage)) != 0) {
1009 return ret;
1010 }
1011 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012
toth92ga41954d2021-02-12 16:11:17 +01001013 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1014 /* Parse subject key identifier */
1015 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1016 &crt->subject_key_id)) != 0) {
1017 return ret;
1018 }
1019 break;
toth92g8d435a02021-05-10 15:16:33 +02001020
toth92ga41954d2021-02-12 16:11:17 +01001021 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1022 /* Parse authority key identifier */
1023 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1024 &crt->authority_key_id)) != 0) {
1025 return ret;
1026 }
1027 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001029 /* Parse subject alt name
1030 * SubjectAltName ::= GeneralNames
1031 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001032 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1033 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 return ret;
1035 }
1036 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1039 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001040 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1041 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return ret;
1043 }
1044 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1047 /* Parse certificate policies type */
1048 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1049 &crt->certificate_policies)) != 0) {
1050 /* Give the callback (if any) a chance to handle the extension
1051 * if it contains unsupported policies */
1052 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1053 cb(p_ctx, crt, &extn_oid, is_critical,
1054 start_ext_octet, end_ext_octet) == 0) {
1055 break;
1056 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (is_critical) {
1059 return ret;
1060 } else
1061 /*
1062 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1063 * cannot interpret or enforce the policy. However, it is up to
1064 * the user to choose how to enforce the policies,
1065 * unless the extension is critical.
1066 */
1067 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1068 return ret;
1069 }
1070 }
1071 break;
1072
1073 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001074 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 * If this is a non-critical extension, which the oid layer
1076 * supports, but there isn't an x509 parser for it,
1077 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if (is_critical) {
1080 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1081 } else {
1082 *p = end_ext_octet;
1083 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084 }
1085 }
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (*p != end) {
1088 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1089 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1090 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093}
1094
1095/*
1096 * Parse and fill a single X.509 certificate in DER format
1097 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1099 const unsigned char *buf,
1100 size_t buflen,
1101 int make_copy,
1102 mbedtls_x509_crt_ext_cb_t cb,
1103 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104{
Janos Follath865b3eb2019-12-16 11:46:15 +00001105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 size_t len;
1107 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1111 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1112 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113
1114 /*
1115 * Check for valid input
1116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if (crt == NULL || buf == NULL) {
1118 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1119 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001121 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001123 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 end = p + len;
1125
1126 /*
1127 * Certificate ::= SEQUENCE {
1128 * tbsCertificate TBSCertificate,
1129 * signatureAlgorithm AlgorithmIdentifier,
1130 * signatureValue BIT STRING }
1131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1133 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1134 mbedtls_x509_crt_free(crt);
1135 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136 }
1137
Janos Follathcc0e49d2016-02-17 14:34:12 +00001138 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001139 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001141 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1143 if (crt->raw.p == NULL) {
1144 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1145 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001148 crt->own_buffer = 1;
1149
1150 p += crt->raw.len - len;
1151 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 } else {
1153 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001154 crt->own_buffer = 0;
1155 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001156
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 /*
1158 * TBSCertificate ::= SEQUENCE {
1159 */
1160 crt->tbs.p = p;
1161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1163 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1164 mbedtls_x509_crt_free(crt);
1165 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 }
1167
1168 end = p + len;
1169 crt->tbs.len = end - crt->tbs.p;
1170
1171 /*
1172 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1173 *
1174 * CertificateSerialNumber ::= INTEGER
1175 *
1176 * signature AlgorithmIdentifier
1177 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1179 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1180 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1181 &sig_params1)) != 0) {
1182 mbedtls_x509_crt_free(crt);
1183 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 }
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (crt->version < 0 || crt->version > 2) {
1187 mbedtls_x509_crt_free(crt);
1188 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 }
1190
Andres AG7ca4a032017-03-09 16:16:11 +00001191 crt->version++;
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1194 &crt->sig_md, &crt->sig_pk,
1195 &crt->sig_opts)) != 0) {
1196 mbedtls_x509_crt_free(crt);
1197 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001198 }
1199
1200 /*
1201 * issuer Name
1202 */
1203 crt->issuer_raw.p = p;
1204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1206 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1207 mbedtls_x509_crt_free(crt);
1208 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 }
1210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1212 mbedtls_x509_crt_free(crt);
1213 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214 }
1215
1216 crt->issuer_raw.len = p - crt->issuer_raw.p;
1217
1218 /*
1219 * Validity ::= SEQUENCE {
1220 * notBefore Time,
1221 * notAfter Time }
1222 *
1223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1225 &crt->valid_to)) != 0) {
1226 mbedtls_x509_crt_free(crt);
1227 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228 }
1229
1230 /*
1231 * subject Name
1232 */
1233 crt->subject_raw.p = p;
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1236 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1237 mbedtls_x509_crt_free(crt);
1238 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 }
1240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1242 mbedtls_x509_crt_free(crt);
1243 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001244 }
1245
1246 crt->subject_raw.len = p - crt->subject_raw.p;
1247
1248 /*
1249 * SubjectPublicKeyInfo
1250 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001251 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1253 mbedtls_x509_crt_free(crt);
1254 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001256 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001257
1258 /*
1259 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1260 * -- If present, version shall be v2 or v3
1261 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1262 * -- If present, version shall be v2 or v3
1263 * extensions [3] EXPLICIT Extensions OPTIONAL
1264 * -- If present, version shall be v3
1265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (crt->version == 2 || crt->version == 3) {
1267 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1268 if (ret != 0) {
1269 mbedtls_x509_crt_free(crt);
1270 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 }
1272 }
1273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (crt->version == 2 || crt->version == 3) {
1275 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1276 if (ret != 0) {
1277 mbedtls_x509_crt_free(crt);
1278 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 }
1280 }
1281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (crt->version == 3) {
1283 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1284 if (ret != 0) {
1285 mbedtls_x509_crt_free(crt);
1286 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001287 }
1288 }
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (p != end) {
1291 mbedtls_x509_crt_free(crt);
1292 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1293 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 }
1295
1296 end = crt_end;
1297
1298 /*
1299 * }
1300 * -- end of TBSCertificate
1301 *
1302 * signatureAlgorithm AlgorithmIdentifier,
1303 * signatureValue BIT STRING
1304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1306 mbedtls_x509_crt_free(crt);
1307 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001308 }
1309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if (crt->sig_oid.len != sig_oid2.len ||
1311 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001312 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001313 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 (sig_params1.len != 0 &&
1315 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1316 mbedtls_x509_crt_free(crt);
1317 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318 }
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1321 mbedtls_x509_crt_free(crt);
1322 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001323 }
1324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (p != end) {
1326 mbedtls_x509_crt_free(crt);
1327 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1328 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329 }
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001332}
1333
1334/*
1335 * Parse one X.509 certificate in DER format from a buffer and add them to a
1336 * chained list
1337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1339 const unsigned char *buf,
1340 size_t buflen,
1341 int make_copy,
1342 mbedtls_x509_crt_ext_cb_t cb,
1343 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344{
Janos Follath865b3eb2019-12-16 11:46:15 +00001345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347
1348 /*
1349 * Check for valid input
1350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (crt == NULL || buf == NULL) {
1352 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1353 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356 prev = crt;
1357 crt = crt->next;
1358 }
1359
1360 /*
1361 * Add new certificate on the end of the chain if needed.
1362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (crt->version != 0 && crt->next == NULL) {
1364 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if (crt->next == NULL) {
1367 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1368 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369
1370 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373 }
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1376 if (ret != 0) {
1377 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (crt != chain) {
1382 mbedtls_free(crt);
1383 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386 }
1387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389}
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1392 const unsigned char *buf,
1393 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001394{
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001396}
1397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1399 const unsigned char *buf,
1400 size_t buflen,
1401 int make_copy,
1402 mbedtls_x509_crt_ext_cb_t cb,
1403 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001404{
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001406}
1407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1409 const unsigned char *buf,
1410 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001411{
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001413}
1414
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001416 * Parse one or more PEM certificates from a buffer and add them to the chained
1417 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1420 const unsigned char *buf,
1421 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422{
Janos Follath98e28a72016-05-31 14:03:54 +01001423#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001424 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001426#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427
1428 /*
1429 * Check for valid input
1430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if (chain == NULL || buf == NULL) {
1432 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1433 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434
1435 /*
1436 * Determine buffer content. Buffer contains either one DER certificate or
1437 * one or more PEM certificates.
1438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1441 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001443 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1446 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1447 }
Janos Follath98e28a72016-05-31 14:03:54 +01001448#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001450#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001457 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001462 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 ret = mbedtls_pem_read_buffer(&pem,
1464 "-----BEGIN CERTIFICATE-----",
1465 "-----END CERTIFICATE-----",
1466 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469 /*
1470 * Was PEM encoded
1471 */
1472 buflen -= use_len;
1473 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1475 return ret;
1476 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1477 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478
1479 /*
1480 * PEM header and footer were found
1481 */
1482 buflen -= use_len;
1483 buf += use_len;
1484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488
Paul Bakker5a5fa922014-09-26 14:53:04 +02001489 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001500 /*
1501 * Quit parsing on a memory error
1502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1504 return ret;
1505 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510
1511 total_failed++;
1512 continue;
1513 }
1514
1515 success = 1;
1516 }
1517 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (success) {
1520 return total_failed;
1521 } else if (first_error) {
1522 return first_error;
1523 } else {
1524 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1525 }
Janos Follath98e28a72016-05-31 14:03:54 +01001526#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527}
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530/*
1531 * Load one or more certificates and add them to the chained list
1532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534{
Janos Follath865b3eb2019-12-16 11:46:15 +00001535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536 size_t n;
1537 unsigned char *buf;
1538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1540 return ret;
1541 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001545 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548}
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551{
1552 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001553#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Steve Lhomme369d7c72023-06-16 14:16:03 +02001554#if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_XP */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555 int w_ret;
1556 WCHAR szDir[MAX_PATH];
1557 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001558 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 size_t len = strlen(path);
Simon Butcherde573f52018-07-05 09:11:30 +01001560 int length_as_int = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
Paul Bakker9af723c2014-05-01 13:03:14 +02001562 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563 HANDLE hFind;
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (len > MAX_PATH - 3) {
1566 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1567 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 memset(szDir, 0, sizeof(szDir));
1570 memset(filename, 0, MAX_PATH);
1571 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001572 filename[len++] = '\\';
1573 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574 filename[len++] = '*';
1575
Minos Galanakisa277b212023-08-09 16:32:22 +01001576 if (FAILED(SizeTToInt(len, &length_as_int))) {
1577 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1578 }
Kevin Kane0ec1e682016-12-15 09:27:16 -08001579
Simon Butcher35e5dad2018-03-15 15:00:03 +00001580 /*
1581 * Note this function uses the code page CP_ACP, and assumes the incoming
1582 * string is encoded in ANSI, before translating it into Unicode. If the
1583 * incoming string were changed to be UTF-8, then the length check needs to
1584 * change to check the number of characters, not the number of bytes, in the
1585 * incoming string are less than MAX_PATH to avoid a buffer overrun with
1586 * MultiByteToWideChar().
1587 */
Simon Butcherde573f52018-07-05 09:11:30 +01001588 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, length_as_int, szDir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 MAX_PATH - 3);
1590 if (w_ret == 0) {
1591 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1592 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 hFind = FindFirstFileW(szDir, &file_data);
1595 if (hFind == INVALID_HANDLE_VALUE) {
1596 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1597 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598
1599 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 do {
1601 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001604 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606
Minos Galanakisa277b212023-08-09 16:32:22 +01001607 if (FAILED(SizeTToInt(wcslen(file_data.cFileName), &length_as_int))) {
1608 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1609 }
Kevin Kane0ec1e682016-12-15 09:27:16 -08001610
Simon Butcherde573f52018-07-05 09:11:30 +01001611 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1612 length_as_int,
1613 p, (int) len - 1,
1614 NULL, NULL);
Minos Galanakisa277b212023-08-09 16:32:22 +01001615 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001616 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1617 goto cleanup;
1618 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1621 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 }
1626 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631
Ron Eldor36d90422017-01-09 15:09:16 +02001632cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 FindClose(hFind);
Steve Lhomme369d7c72023-06-16 14:16:03 +02001634#else /* !_WIN32_WINNT_XP */
Antonio de Angelis1ee4d122023-08-16 12:26:37 +01001635#error "mbedtls_x509_crt_parse_path not available before Windows XP"
Steve Lhomme369d7c72023-06-16 14:16:03 +02001636#endif /* !_WIN32_WINNT_XP */
Paul Bakkerbe089b02013-10-14 15:51:50 +02001637#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001638 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001639 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001641 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001642 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 if (dir == NULL) {
1646 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1647 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648
Ron Eldor63140682017-01-09 19:27:59 +02001649#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1651 closedir(dir);
1652 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001653 }
Ron Eldor63140682017-01-09 19:27:59 +02001654#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001659 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001662 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001663 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1664 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 } else if (stat(entry_name, &sb) == -1) {
1666 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001667 /* Broken symbolic link - ignore this entry.
1668 stat(2) will return this error for either (a) a dangling
1669 symlink or (b) a missing file.
1670 Given that we have just obtained the filename from readdir,
1671 assume that it does exist and therefore treat this as a
1672 dangling symlink. */
1673 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001675 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001676 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1677 goto cleanup;
1678 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679 }
1680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684
1685 // Ignore parse errors
1686 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1688 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001691 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001693 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001694
1695cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001697
Ron Eldor63140682017-01-09 19:27:59 +02001698#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 }
Ron Eldor63140682017-01-09 19:27:59 +02001702#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001703
Paul Bakkerbe089b02013-10-14 15:51:50 +02001704#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001709
Peter Kolbus9a969b62018-12-11 13:55:56 -06001710#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001711#define PRINT_ITEM(i) \
1712 do { \
1713 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1714 MBEDTLS_X509_SAFE_SNPRINTF; \
1715 sep = ", "; \
1716 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001717
Przemek Stekielf4194942023-04-24 09:52:17 +02001718#define CERT_TYPE(type, name) \
1719 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001720 if (ns_cert_type & (type)) { \
1721 PRINT_ITEM(name); \
1722 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001723 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001724
Przemek Stekielf4194942023-04-24 09:52:17 +02001725#define KEY_USAGE(code, name) \
1726 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001727 if (key_usage & (code)) { \
1728 PRINT_ITEM(name); \
1729 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001730 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732static int x509_info_ext_key_usage(char **buf, size_t *size,
1733 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001734{
Janos Follath865b3eb2019-12-16 11:46:15 +00001735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001736 const char *desc;
1737 size_t n = *size;
1738 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001740 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 while (cur != NULL) {
1743 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001744 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001748 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001749
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001750 sep = ", ";
1751
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001752 cur = cur->next;
1753 }
1754
1755 *size = n;
1756 *buf = p;
1757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001759}
1760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761static int x509_info_cert_policies(char **buf, size_t *size,
1762 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001763{
Janos Follath865b3eb2019-12-16 11:46:15 +00001764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001765 const char *desc;
1766 size_t n = *size;
1767 char *p = *buf;
1768 const mbedtls_x509_sequence *cur = certificate_policies;
1769 const char *sep = "";
1770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 while (cur != NULL) {
1772 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001773 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001777 MBEDTLS_X509_SAFE_SNPRINTF;
1778
1779 sep = ", ";
1780
1781 cur = cur->next;
1782 }
1783
1784 *size = n;
1785 *buf = p;
1786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001788}
1789
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001790/*
1791 * Return an informational string about the certificate.
1792 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001793#define BEFORE_COLON 18
1794#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001795int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1796 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001797{
Janos Follath865b3eb2019-12-16 11:46:15 +00001798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799 size_t n;
1800 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801 char key_size_str[BEFORE_COLON];
1802
1803 p = buf;
1804 n = size;
1805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 if (NULL == crt) {
1807 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001808 MBEDTLS_X509_SAFE_SNPRINTF;
1809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001811 }
1812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1814 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001815 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1817 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001818 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
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%sissuer 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->issuer);
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%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001829 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001831 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1834 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1835 crt->valid_from.year, crt->valid_from.mon,
1836 crt->valid_from.day, crt->valid_from.hour,
1837 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001838 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1841 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1842 crt->valid_to.year, crt->valid_to.mon,
1843 crt->valid_to.day, crt->valid_to.hour,
1844 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001845 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001848 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1851 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001852 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001853
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001854 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1856 mbedtls_pk_get_name(&crt->pk))) != 0) {
1857 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001858 }
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1861 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001862 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001863
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001864 /*
1865 * Optional extensions
1866 */
1867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1869 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1870 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001871 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 if (crt->max_pathlen > 0) {
1874 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001875 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001876 }
1877 }
1878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1880 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001881 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001882
Przemek Stekiel21c37282023-01-16 08:47:49 +01001883 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1884 &crt->subject_alt_names,
1885 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 return ret;
1887 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001888 }
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1891 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001892 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001893
Przemek Stekiel21c37282023-01-16 08:47:49 +01001894 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 return ret;
1896 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001897 }
1898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1900 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001901 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001902
Przemek Stekiel21c37282023-01-16 08:47:49 +01001903 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 return ret;
1905 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001906 }
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1909 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001910 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 if ((ret = x509_info_ext_key_usage(&p, &n,
1913 &crt->ext_key_usage)) != 0) {
1914 return ret;
1915 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001916 }
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1919 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001920 MBEDTLS_X509_SAFE_SNPRINTF;
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 if ((ret = x509_info_cert_policies(&p, &n,
1923 &crt->certificate_policies)) != 0) {
1924 return ret;
1925 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001926 }
1927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001929 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001932}
1933
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001934struct x509_crt_verify_string {
1935 int code;
1936 const char *string;
1937};
1938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001940static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001941 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001942 { 0, NULL }
1943};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001944#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1947 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001948{
Janos Follath865b3eb2019-12-16 11:46:15 +00001949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001950 const struct x509_crt_verify_string *cur;
1951 char *p = buf;
1952 size_t n = size;
1953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1955 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001956 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001960 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001961 flags ^= cur->code;
1962 }
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 if (flags != 0) {
1965 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1966 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001967 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001968 }
1969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001971}
Hanno Becker612a2f12020-10-09 09:19:39 +01001972#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1975 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001976{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001977 unsigned int usage_must, usage_may;
1978 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1982 return 0;
1983 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001984
1985 usage_must = usage & ~may_mask;
1986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1988 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1989 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001990
1991 usage_may = usage & may_mask;
1992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1994 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1995 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001998}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2001 const char *usage_oid,
2002 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002005
2006 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2008 return 0;
2009 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002010
2011 /*
2012 * Look for the requested usage (or wildcard ANY) in our list
2013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 if (cur_oid->len == usage_len &&
2018 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2019 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002020 }
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2023 return 0;
2024 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002025 }
2026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002028}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002031/*
2032 * Return 1 if the certificate is revoked, or 0 otherwise.
2033 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002035{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 while (cur != NULL && cur->serial.len != 0) {
2039 if (crt->serial.len == cur->serial.len &&
2040 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2041 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002042 }
2043
2044 cur = cur->next;
2045 }
2046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002048}
2049
2050/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002051 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002052 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002054static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2055 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002056 const mbedtls_x509_crt_profile *profile,
2057 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002058{
2059 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002060 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002061#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002062 psa_algorithm_t psa_algorithm;
2063#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002065#endif /* MBEDTLS_USE_PSA_CRYPTO */
2066 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 if (ca == NULL) {
2069 return flags;
2070 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 while (crl_list != NULL) {
2073 if (crl_list->version == 0 ||
2074 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075 crl_list = crl_list->next;
2076 continue;
2077 }
2078
2079 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002080 * Check if the CA is configured to sign CRLs
2081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (mbedtls_x509_crt_check_key_usage(ca,
2083 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002085 break;
2086 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002087
2088 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089 * Check if CRL is correctly signed by the trusted CA
2090 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002092 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002096 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002098
pespacek7599a772022-02-07 14:40:55 +01002099#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002100 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (psa_hash_compute(psa_algorithm,
2102 crl_list->tbs.p,
2103 crl_list->tbs.len,
2104 hash,
2105 sizeof(hash),
2106 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002107 /* Note: this can't happen except after an internal error */
2108 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2109 break;
2110 }
pespacek7599a772022-02-07 14:40:55 +01002111#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2113 hash_length = mbedtls_md_get_size(md_info);
2114 if (mbedtls_md(md_info,
2115 crl_list->tbs.p,
2116 crl_list->tbs.len,
2117 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002118 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002120 break;
2121 }
pespaceka7a64692022-02-14 15:18:43 +01002122#endif /* MBEDTLS_USE_PSA_CRYPTO */
2123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002125 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2129 crl_list->sig_md, hash, hash_length,
2130 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132 break;
2133 }
2134
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002135#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136 /*
2137 * Check for validity of CRL (Do not drop out)
2138 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002139 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002142
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002143 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002144 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002146#else
2147 ((void) now);
2148#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002149
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002150 /*
2151 * Check if certificate is revoked
2152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002155 break;
2156 }
2157
2158 crl_list = crl_list->next;
2159 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002164
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002165/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002166 * Check the signature of a certificate by its parent
2167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2169 mbedtls_x509_crt *parent,
2170 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002171{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002172 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002173 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002174#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2175 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 md_info = mbedtls_md_info_from_type(child->sig_md);
2177 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002178
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002179 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2181 return -1;
2182 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002183#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002184 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002185 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 status = psa_hash_compute(hash_alg,
2188 child->tbs.p,
2189 child->tbs.len,
2190 hash,
2191 sizeof(hash),
2192 &hash_len);
2193 if (status != PSA_SUCCESS) {
2194 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002195 }
2196
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002197#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002198 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2200 return -1;
2201 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002202
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002203#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2205 return mbedtls_pk_verify_restartable(&parent->pk,
2206 child->sig_md, hash, hash_len,
2207 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002208 }
2209#else
2210 (void) rs_ctx;
2211#endif
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2214 child->sig_md, hash, hash_len,
2215 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002216}
2217
2218/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002219 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2220 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002221 *
2222 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002224static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2225 const mbedtls_x509_crt *parent,
2226 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002227{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002228 int need_ca_bit;
2229
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002230 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2232 return -1;
2233 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002234
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002235 /* Parent must have the basicConstraints CA bit set as a general rule */
2236 need_ca_bit = 1;
2237
2238 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002240 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002241 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 if (need_ca_bit && !parent->ca_istrue) {
2244 return -1;
2245 }
2246
2247 if (need_ca_bit &&
2248 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2249 return -1;
2250 }
2251
2252 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002253}
2254
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002255/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002256 * Find a suitable parent for child in candidates, or return NULL.
2257 *
2258 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002259 * 1. subject name matches child's issuer
2260 * 2. if necessary, the CA bit is set and key usage allows signing certs
2261 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002262 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002263 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002264 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002265 * If there's a suitable candidate which is also time-valid, return the first
2266 * such. Otherwise, return the first suitable candidate (or NULL if there is
2267 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002268 *
2269 * The rationale for this rule is that someone could have a list of trusted
2270 * roots with two versions on the same root with different validity periods.
2271 * (At least one user reported having such a list and wanted it to just work.)
2272 * The reason we don't just require time-validity is that generally there is
2273 * only one version, and if it's expired we want the flags to state that
2274 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002275 *
2276 * The rationale for rule 3 (signature for trusted roots) is that users might
2277 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002278 * way we select the correct one is by checking the signature (as we don't
2279 * rely on key identifier extensions). (This is one way users might choose to
2280 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002281 *
2282 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002283 * - [in] child: certificate for which we're looking for a parent
2284 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002285 * - [out] r_parent: parent found (or NULL)
2286 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002287 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2288 * of the chain, 0 otherwise
2289 * - [in] path_cnt: number of intermediates seen so far
2290 * - [in] self_cnt: number of self-signed intermediates seen so far
2291 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002292 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002293 *
2294 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002295 * - 0 on success
2296 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002297 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002298static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 mbedtls_x509_crt *child,
2300 mbedtls_x509_crt *candidates,
2301 mbedtls_x509_crt **r_parent,
2302 int *r_signature_is_good,
2303 int top,
2304 unsigned path_cnt,
2305 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002306 mbedtls_x509_crt_restart_ctx *rs_ctx,
2307 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002308{
Janos Follath865b3eb2019-12-16 11:46:15 +00002309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002310 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002311 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002312
2313#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002314 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002316 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002317 parent = rs_ctx->parent;
2318 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002319 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002320
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002321 /* clear saved state */
2322 rs_ctx->parent = NULL;
2323 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002324 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002325
2326 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002327 goto check_signature;
2328 }
2329#endif
2330
2331 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002332 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002335 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002337 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002339
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002340 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (parent->max_pathlen > 0 &&
2342 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002343 continue;
2344 }
2345
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002346 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002347#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2348check_signature:
2349#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002351
2352#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002354 /* save state */
2355 rs_ctx->parent = parent;
2356 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002357 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002360 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002361#else
2362 (void) ret;
2363#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002364
2365 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002367 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002369
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002370#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002371 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002372 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2373 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002375 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002376 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002377 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002378
2379 continue;
2380 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002381#else
2382 ((void) now);
2383#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002384
Andy Gross1f627142019-01-30 10:25:53 -06002385 *r_parent = parent;
2386 *r_signature_is_good = signature_is_good;
2387
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002388 break;
2389 }
2390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002392 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002393 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002394 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002397}
2398
2399/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002400 * Find a parent in trusted CAs or the provided chain, or return NULL.
2401 *
2402 * Searches in trusted CAs first, and return the first suitable parent found
2403 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002404 *
2405 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002406 * - [in] child: certificate for which we're looking for a parent, followed
2407 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002408 * - [in] trust_ca: list of locally trusted certificates
2409 * - [out] parent: parent found (or NULL)
2410 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2411 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2412 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2413 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002414 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002415 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002416 *
2417 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002418 * - 0 on success
2419 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002420 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002421static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 mbedtls_x509_crt *child,
2423 mbedtls_x509_crt *trust_ca,
2424 mbedtls_x509_crt **parent,
2425 int *parent_is_trusted,
2426 int *signature_is_good,
2427 unsigned path_cnt,
2428 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002429 mbedtls_x509_crt_restart_ctx *rs_ctx,
2430 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002431{
Janos Follath865b3eb2019-12-16 11:46:15 +00002432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002433 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002434
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002435 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002436
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002437#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002438 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002440 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002441 rs_ctx->parent_is_trusted = -1;
2442 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002443#endif
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002446 search_list = *parent_is_trusted ? trust_ca : child->next;
2447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 ret = x509_crt_find_parent_in(child, search_list,
2449 parent, signature_is_good,
2450 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002451 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002452
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002453#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002455 /* save state */
2456 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002458 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002459#else
2460 (void) ret;
2461#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002462
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002463 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002465 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002467
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002468 /* prepare second iteration */
2469 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002470 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002471
2472 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002474 *parent_is_trusted = 0;
2475 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002476 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002479}
2480
2481/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002482 * Check if an end-entity certificate is locally trusted
2483 *
2484 * Currently we require such certificates to be self-signed (actually only
2485 * check for self-issued as self-signatures are not checked)
2486 */
2487static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 mbedtls_x509_crt *crt,
2489 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002490{
2491 mbedtls_x509_crt *cur;
2492
2493 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2495 return -1;
2496 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002497
2498 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2500 if (crt->raw.len == cur->raw.len &&
2501 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2502 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002503 }
2504 }
2505
2506 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002508}
2509
2510/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002511 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002512 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002513 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2514 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002515 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002516 * such that every cert in the chain is a child of the next one,
2517 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002518 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002519 * Verify that chain and return it with flags for all issues found.
2520 *
2521 * Special cases:
2522 * - EE == Rj -> return a one-element list containing it
2523 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2524 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002525 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002526 * Tests for (aspects of) this function should include at least:
2527 * - trusted EE
2528 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002529 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002530 * - if relevant: EE untrusted
2531 * - if relevant: EE -> intermediate, untrusted
2532 * with the aspect under test checked at each relevant level (EE, int, root).
2533 * For some aspects longer chains are required, but usually length 2 is
2534 * enough (but length 1 is not in general).
2535 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002536 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002537 * - [in] crt: the cert list EE, C1, ..., Cn
2538 * - [in] trust_ca: the trusted list R1, ..., Rp
2539 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002540 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002541 * Only valid when return value is 0, may contain garbage otherwise!
2542 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002543 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002544 *
2545 * Return value:
2546 * - non-zero if the chain could not be fully built and examined
2547 * - 0 is the chain was successfully built and examined,
2548 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002549 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002550static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 mbedtls_x509_crt *crt,
2552 mbedtls_x509_crt *trust_ca,
2553 mbedtls_x509_crl *ca_crl,
2554 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2555 void *p_ca_cb,
2556 const mbedtls_x509_crt_profile *profile,
2557 mbedtls_x509_crt_verify_chain *ver_chain,
2558 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002559{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002560 /* Don't initialize any of those variables here, so that the compiler can
2561 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002562 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002563 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002564 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002565 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002566 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002567 int parent_is_trusted;
2568 int child_is_trusted;
2569 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002570 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002571 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002572 mbedtls_x509_time now;
2573
2574#if defined(MBEDTLS_HAVE_TIME_DATE)
2575 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2576 return MBEDTLS_ERR_X509_FATAL_ERROR;
2577 }
2578#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002579
2580#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2581 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002583 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002584 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002585 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002586
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002587 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002588 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002589 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002590 flags = &cur->flags;
2591
2592 goto find_parent;
2593 }
2594#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002595
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002596 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002597 self_cnt = 0;
2598 parent_is_trusted = 0;
2599 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002602 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002603 cur = &ver_chain->items[ver_chain->len];
2604 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002605 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002606 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002607 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002608
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002609#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002610 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002611 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002612 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002614
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002615 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002616 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002618#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002619
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002620 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (child_is_trusted) {
2622 return 0;
2623 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002624
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002625 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002627 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002631 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002633
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002634 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (ver_chain->len == 1 &&
2636 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2637 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002638 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002639
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002640#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2641find_parent:
2642#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002643
2644 /* Obtain list of potential trusted signers from CA callback,
2645 * or use statically provided list. */
2646#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (f_ca_cb != NULL) {
2648 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2649 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002650 ver_chain->trust_ca_cb_result = NULL;
2651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2653 if (ret != 0) {
2654 return MBEDTLS_ERR_X509_FATAL_ERROR;
2655 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002656
2657 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002659#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2660 {
2661 ((void) f_ca_cb);
2662 ((void) p_ca_cb);
2663 cur_trust_ca = trust_ca;
2664 }
2665
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002666 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2668 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002669 ver_chain->len - 1, self_cnt, rs_ctx,
2670 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002671
2672#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002674 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002675 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002676 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002677 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002680 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002681#else
2682 (void) ret;
2683#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002684
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002685 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002687 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002689 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002690
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002691 /* Count intermediate self-issued (not necessarily self-signed) certs.
2692 * These can occur with some strategies for key rollover, see [SIRO],
2693 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (ver_chain->len != 1 &&
2695 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002696 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002697 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002698
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002699 /* path_cnt is 0 for the first intermediate CA,
2700 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 if (!parent_is_trusted &&
2702 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002703 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002705 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002706
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002707 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002709 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002711
2712 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002714 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002718 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002719 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002720#else
2721 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002722#endif
2723
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002724 /* prepare for next iteration */
2725 child = parent;
2726 parent = NULL;
2727 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002728 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002729 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002730}
2731
Glenn Straussc26bd762022-10-23 19:48:18 -04002732#ifdef _WIN32
2733#ifdef _MSC_VER
2734#pragma comment(lib, "ws2_32.lib")
2735#include <winsock2.h>
2736#include <ws2tcpip.h>
2737#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2738#include <winsock2.h>
2739#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002740#else
2741/* inet_pton() is not supported, fallback to software version */
2742#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002743#endif
2744#elif defined(__sun)
2745/* Solaris requires -lsocket -lnsl for inet_pton() */
2746#elif defined(__has_include)
2747#if __has_include(<sys/socket.h>)
2748#include <sys/socket.h>
2749#endif
2750#if __has_include(<arpa/inet.h>)
2751#include <arpa/inet.h>
2752#endif
2753#endif
2754
2755/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2756 * the platform inet_pton() or a local implementation (below). The local
2757 * implementation may be used even in cases where the platform provides
2758 * inet_pton(), e.g. when there are different includes required and/or the
2759 * platform implementation requires dependencies on additional libraries.
2760 * Specifically, Windows requires custom includes and additional link
2761 * dependencies, and Solaris requires additional link dependencies.
2762 * Also, as a coarse heuristic, use the local implementation if the compiler
2763 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002764 * provided by headers included (or not) via __has_include() above.
2765 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2766 * despite having a platform that has inet_pton. */
2767#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2768/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002769static int x509_inet_pton_ipv4(const char *src, void *dst);
2770
2771#define li_cton(c, n) \
2772 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2773
2774static int x509_inet_pton_ipv6(const char *src, void *dst)
2775{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002776 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002777 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002778 uint16_t addr[8];
2779 do {
2780 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002781 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002782 for (uint8_t digit; num_digits < 4; num_digits++) {
2783 if (li_cton(*p, digit) == 0) {
2784 break;
2785 }
2786 group = (group << 4) | digit;
2787 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002788 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002789 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002790 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2791 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002792 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002793 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002794 } else if (*p == '.') {
2795 /* Don't accept IPv4 too early or late */
2796 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2797 nonzero_groups >= 7) {
2798 break;
2799 }
2800
2801 /* Walk back to prior ':', then parse as IPv4-mapped */
2802 int steps = 4;
2803 do {
2804 p--;
2805 steps--;
2806 } while (*p != ':' && steps > 0);
2807
2808 if (*p != ':') {
2809 break;
2810 }
2811 p++;
2812 nonzero_groups--;
2813 if (x509_inet_pton_ipv4((const char *) p,
2814 addr + nonzero_groups) != 0) {
2815 break;
2816 }
2817
Andrzej Kurek0d578962023-04-13 09:09:56 -04002818 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002819 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002820 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002821 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002822 return -1;
2823 }
2824 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002825 /* Don't accept a second zero group or an invalid delimiter */
2826 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002827 return -1;
2828 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002829 zero_group_start = nonzero_groups;
2830
2831 /* Accept a zero group at start, but it has to be a double colon */
2832 if (zero_group_start == 0 && *++p != ':') {
2833 return -1;
2834 }
2835
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002836 if (p[1] == '\0') {
2837 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002838 break;
2839 }
2840 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002841 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002842 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002843
2844 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002845 return -1;
2846 }
2847
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002848 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002849 if (nonzero_groups > 6) {
2850 return -1;
2851 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002852 int zero_groups = 8 - nonzero_groups;
2853 int groups_after_zero = nonzero_groups - zero_group_start;
2854
2855 /* Move the non-zero part to after the zeroes */
2856 if (groups_after_zero) {
2857 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002858 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002859 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002860 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002861 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002862 } else {
2863 if (nonzero_groups != 8) {
2864 return -1;
2865 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002866 }
2867 memcpy(dst, addr, sizeof(addr));
2868 return 0;
2869}
2870
2871static int x509_inet_pton_ipv4(const char *src, void *dst)
2872{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002873 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002874 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002875 uint8_t digit, num_digits = 0;
2876 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002877 uint16_t octet;
2878
Glenn Strauss416c2952022-10-24 23:00:02 -04002879 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002880 octet = num_digits = 0;
2881 do {
2882 digit = *p - '0';
2883 if (digit > 9) {
2884 break;
2885 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002886
2887 /* Don't allow leading zeroes. These might mean octal format,
2888 * which this implementation does not support. */
2889 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002890 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002891 }
2892
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002893 octet = octet * 10 + digit;
2894 num_digits++;
2895 p++;
2896 } while (num_digits < 3);
2897
2898 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002899 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002900 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002901 *res++ = (uint8_t) octet;
2902 num_octets++;
2903 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002904 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002905}
Glenn Straussc26bd762022-10-23 19:48:18 -04002906
2907#else
2908
2909static int x509_inet_pton_ipv6(const char *src, void *dst)
2910{
2911 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2912}
2913
2914static int x509_inet_pton_ipv4(const char *src, void *dst)
2915{
2916 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2917}
2918
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002919#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002920
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002921size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002922{
2923 return strchr(cn, ':') == NULL
2924 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2925 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2926}
2927
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002928/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002929 * Check for CN match
2930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002931static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2932 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002933{
2934 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (name->len == cn_len &&
2936 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2937 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002938 }
2939
2940 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 if (x509_check_wildcard(cn, name) == 0) {
2942 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002943 }
2944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002946}
2947
Glenn Straussc26bd762022-10-23 19:48:18 -04002948static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2949 const char *cn, size_t cn_len)
2950{
2951 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002952 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002953 if (cn_len == 0) {
2954 return -1;
2955 }
2956
2957 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2958 const unsigned char san_type = (unsigned char) cur->buf.tag &
2959 MBEDTLS_ASN1_TAG_VALUE_MASK;
2960 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2961 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2962 return 0;
2963 }
2964 }
2965
2966 return -1;
2967}
2968
Andrzej Kurek199eab92023-05-10 09:57:19 -04002969static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2970 const char *cn, size_t cn_len)
2971{
2972 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2973 const unsigned char san_type = (unsigned char) cur->buf.tag &
2974 MBEDTLS_ASN1_TAG_VALUE_MASK;
2975 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2976 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2977 return 0;
2978 }
2979 }
2980
2981 return -1;
2982}
2983
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002984/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002985 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2986 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002987static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002989{
Glenn Straussc26bd762022-10-23 19:48:18 -04002990 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002991 int san_uri = 0;
2992 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002993 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2994 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002995 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002996 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2997 return 0;
2998 }
2999 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04003000 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04003001 san_ip = 1;
3002 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04003003 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
3004 san_uri = 1;
3005 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04003006 /* (We may handle other types here later.) */
3007 default: /* Unrecognized type */
3008 break;
3009 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04003011 if (san_ip) {
3012 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
3013 return 0;
3014 }
3015 }
3016 if (san_uri) {
3017 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
3018 return 0;
3019 }
3020 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003021
Andrzej Kurek199eab92023-05-10 09:57:19 -04003022 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003023}
3024
3025/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003026 * Verify the requested CN - only call this if cn is not NULL!
3027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003028static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3029 const char *cn,
3030 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003031{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003032 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003036 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
3037 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 }
3039 } else {
3040 for (name = &crt->subject; name != NULL; name = name->next) {
3041 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3042 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003043 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 }
3045 }
3046
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003047 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003048
3049 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050}
3051
3052/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003053 * Merge the flags for all certs in the chain, after calling callback
3054 */
3055static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 uint32_t *flags,
3057 const mbedtls_x509_crt_verify_chain *ver_chain,
3058 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3059 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003060{
Janos Follath865b3eb2019-12-16 11:46:15 +00003061 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003062 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003063 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003064 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003067 cur = &ver_chain->items[i-1];
3068 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 if (NULL != f_vrfy) {
3071 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3072 return ret;
3073 }
3074 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003075
3076 *flags |= cur_flags;
3077 }
3078
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003080}
3081
3082/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003083 * Verify the certificate validity, with profile, restartable version
3084 *
3085 * This function:
3086 * - checks the requested CN (if any)
3087 * - checks the type and size of the EE cert's key,
3088 * as that isn't done as part of chain building/verification currently
3089 * - builds and verifies the chain
3090 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003091 *
3092 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3093 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3094 * verification routine to search for trusted signers, and CRLs will
3095 * be disabled. Otherwise, `trust_ca` will be used as the static list
3096 * of trusted signers, and `ca_crl` will be use as the static list
3097 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3100 mbedtls_x509_crt *trust_ca,
3101 mbedtls_x509_crl *ca_crl,
3102 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3103 void *p_ca_cb,
3104 const mbedtls_x509_crt_profile *profile,
3105 const char *cn, uint32_t *flags,
3106 int (*f_vrfy)(void *,
3107 mbedtls_x509_crt *,
3108 int,
3109 uint32_t *),
3110 void *p_vrfy,
3111 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003112{
Janos Follath865b3eb2019-12-16 11:46:15 +00003113 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003114 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003115 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003116 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003117
3118 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003119 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003123 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3124 goto exit;
3125 }
3126
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003127 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 if (cn != NULL) {
3129 x509_crt_verify_name(crt, cn, &ee_flags);
3130 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003131
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003132 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003136 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003140 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003142
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003143 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3145 f_ca_cb, p_ca_cb, profile,
3146 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003151
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003152 /* Merge end-entity flags */
3153 ver_chain.items[0].flags |= ee_flags;
3154
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003155 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003157
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003158exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003159
3160#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3162 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003163 ver_chain.trust_ca_cb_result = NULL;
3164#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3165
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003166#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3168 mbedtls_x509_crt_restart_free(rs_ctx);
3169 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003170#endif
3171
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003172 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3173 * the SSL module for authmode optional, but non-zero return from the
3174 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003176 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003177 }
3178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (ret != 0) {
3180 *flags = (uint32_t) -1;
3181 return ret;
3182 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 if (*flags != 0) {
3185 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3186 }
3187
3188 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003189}
3190
Hanno Becker3116fb32019-03-28 13:34:42 +00003191
3192/*
3193 * Verify the certificate validity (default profile, not restartable)
3194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003195int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3196 mbedtls_x509_crt *trust_ca,
3197 mbedtls_x509_crl *ca_crl,
3198 const char *cn, uint32_t *flags,
3199 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3200 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003201{
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3203 NULL, NULL,
3204 &mbedtls_x509_crt_profile_default,
3205 cn, flags,
3206 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003207}
3208
3209/*
3210 * Verify the certificate validity (user-chosen profile, not restartable)
3211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003212int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3213 mbedtls_x509_crt *trust_ca,
3214 mbedtls_x509_crl *ca_crl,
3215 const mbedtls_x509_crt_profile *profile,
3216 const char *cn, uint32_t *flags,
3217 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3218 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003219{
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3221 NULL, NULL,
3222 profile, cn, flags,
3223 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003224}
3225
3226#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3227/*
3228 * Verify the certificate validity (user-chosen profile, CA callback,
3229 * not restartable).
3230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003231int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3232 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3233 void *p_ca_cb,
3234 const mbedtls_x509_crt_profile *profile,
3235 const char *cn, uint32_t *flags,
3236 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3237 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003238{
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3240 f_ca_cb, p_ca_cb,
3241 profile, cn, flags,
3242 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003243}
3244#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3247 mbedtls_x509_crt *trust_ca,
3248 mbedtls_x509_crl *ca_crl,
3249 const mbedtls_x509_crt_profile *profile,
3250 const char *cn, uint32_t *flags,
3251 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3252 void *p_vrfy,
3253 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003254{
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3256 NULL, NULL,
3257 profile, cn, flags,
3258 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003259}
3260
3261
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003262/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003263 * Initialize a certificate chain
3264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003265void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003266{
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003268}
3269
3270/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003271 * Unallocate all certificate data
3272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003273void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 mbedtls_x509_crt *cert_cur = crt;
3276 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 while (cert_cur != NULL) {
3279 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003281#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003283#endif
3284
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3286 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3287 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3288 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3289 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003290 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003293 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003294 }
3295
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003296 cert_prv = cert_cur;
3297 cert_cur = cert_cur->next;
3298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3300 if (cert_prv != crt) {
3301 mbedtls_free(cert_prv);
3302 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003303 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003304}
3305
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003306#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3307/*
3308 * Initialize a restart context
3309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003310void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003311{
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003313
3314 ctx->parent = NULL;
3315 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003316 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003317
3318 ctx->parent_is_trusted = -1;
3319
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003320 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003321 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003323}
3324
3325/*
3326 * Free the components of a restart context
3327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003328void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003329{
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003331 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 mbedtls_pk_restart_free(&ctx->pk);
3335 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003336}
3337#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3338
Minos Galanakis12b493f2023-08-11 15:22:45 +01003339#if defined(MBEDTLS_POP_TARGET_PRAGMA)
3340#undef MBEDTLS_POP_TARGET_PRAGMA
3341#endif
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343#endif /* MBEDTLS_X509_CRT_PARSE_C */