blob: 8c955cb260fc58b8725dbe162d67fea2b15acab4 [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>
Kevin Kane0ec1e682016-12-15 09:27:16 -080064#include <intsafe.h>
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#else
66#include <time.h>
67#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100068#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000071#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020072#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#include <sys/types.h>
74#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020075#if defined(__MBED__)
76#include <platform/mbed_retarget.h>
77#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020079#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060080#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000081#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082#endif
83
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020084/*
85 * Item in a verification chain: cert and flags for it
86 */
87typedef struct {
88 mbedtls_x509_crt *crt;
89 uint32_t flags;
90} x509_crt_verify_chain_item;
91
92/*
93 * Max size of verification chain: end-entity + intermediates + trusted root
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020096
Gilles Peskineffb92da2021-06-02 00:03:26 +020097/* Default profile. Do not remove items unless there are serious security
98 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
100{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200101 /* Hashes from SHA-256 and above. Note that this selection
102 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
104 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
105 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200106 0xFFFFFFF, /* Any PK alg */
Valerio Setti8c3404f2023-06-26 15:49:48 +0200107#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200108 /* Curves at or above 128-bit security level. Note that this selection
109 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
111 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
112 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
113 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
114 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
115 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200116 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200117#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200118 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200119#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200120 2048,
121};
122
Gilles Peskineffb92da2021-06-02 00:03:26 +0200123/* Next-generation profile. Currently identical to the default, but may
124 * be tightened at any time. */
125const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200126{
127 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
129 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
130 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200131 0xFFFFFFF, /* Any PK alg */
132#if defined(MBEDTLS_ECP_C)
133 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
135 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
136 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
138 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
139 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
140 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200141#else
142 0,
143#endif
144 2048,
145};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200146
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147/*
148 * NSA Suite B Profile
149 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200150const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
151{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200152 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
154 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
157 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200158#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200159 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
161 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200162#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200163 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200164#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200165 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200166};
167
168/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200169 * Empty / all-forbidden profile
170 */
171const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
172{
173 0,
174 0,
175 0,
176 (uint32_t) -1,
177};
178
179/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200180 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200181 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
184 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185{
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (md_alg == MBEDTLS_MD_NONE) {
187 return -1;
188 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
191 return 0;
192 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195}
196
197/*
198 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200199 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
202 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200203{
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (pk_alg == MBEDTLS_PK_NONE) {
205 return -1;
206 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
209 return 0;
210 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200213}
214
215/*
216 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200217 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
220 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221{
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200223
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200224#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
226 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
227 return 0;
228 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200231 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200232#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200233
Valerio Setti8c3404f2023-06-26 15:49:48 +0200234#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200236 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Setti97207782023-05-18 18:59:06 +0200238 const mbedtls_ecp_group_id gid = mbedtls_pk_get_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if (gid == MBEDTLS_ECP_DP_NONE) {
241 return -1;
242 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
245 return 0;
246 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200249 }
Valerio Setti8c3404f2023-06-26 15:49:48 +0200250#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200253}
254
255/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000256 * Like memcmp, but case-insensitive and always returns -1 if different
257 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000259{
260 size_t i;
261 unsigned char diff;
262 const unsigned char *n1 = s1, *n2 = s2;
263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000265 diff = n1[i] ^ n2[i];
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000268 continue;
269 }
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (diff == 32 &&
272 ((n1[i] >= 'a' && n1[i] <= 'z') ||
273 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
274 continue;
275 }
276
277 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000278 }
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000281}
282
283/*
284 * Return 0 if name matches wildcard, -1 otherwise
285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000287{
288 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000290
291 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
293 return -1;
294 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 for (i = 0; i < cn_len; ++i) {
297 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000298 cn_idx = i;
299 break;
300 }
301 }
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (cn_idx == 0) {
304 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000305 }
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (cn_len - cn_idx == name->len - 1 &&
308 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
309 return 0;
310 }
311
312 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000313}
314
315/*
316 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
317 * variations (but not all).
318 *
319 * Return 0 if equal, -1 otherwise.
320 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000322{
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000324 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 memcmp(a->p, b->p, b->len) == 0) {
326 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000327 }
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
330 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000331 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 x509_memcasecmp(a->p, b->p, b->len) == 0) {
333 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000334 }
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000337}
338
339/*
340 * Compare two X.509 Names (aka rdnSequence).
341 *
342 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
343 * we sometimes return unequal when the full algorithm would return equal,
344 * but never the other way. (In particular, we don't do Unicode normalisation
345 * or space folding.)
346 *
347 * Return 0 if equal, -1 otherwise.
348 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100349static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000350{
351 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 while (a != NULL || b != NULL) {
353 if (a == NULL || b == NULL) {
354 return -1;
355 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000356
357 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000359 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
361 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000362 }
363
364 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (x509_string_cmp(&a->val, &b->val) != 0) {
366 return -1;
367 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000368
369 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (a->next_merged != b->next_merged) {
371 return -1;
372 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000373
374 a = a->next;
375 b = b->next;
376 }
377
378 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000380}
381
382/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200383 * Reset (init or clear) a verify_chain
384 */
385static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200387{
388 size_t i;
389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200391 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000392 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200393 }
394
395 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000396
397#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
398 ver_chain->trust_ca_cb_result = NULL;
399#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200400}
401
402/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405static int x509_get_version(unsigned char **p,
406 const unsigned char *end,
407 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408{
Janos Follath865b3eb2019-12-16 11:46:15 +0000409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410 size_t len;
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
413 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
414 0)) != 0) {
415 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 }
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421 }
422
423 end = *p + len;
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
426 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
427 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (*p != end) {
430 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
431 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
432 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435}
436
437/*
438 * Validity ::= SEQUENCE {
439 * notBefore Time,
440 * notAfter Time }
441 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442static int x509_get_dates(unsigned char **p,
443 const unsigned char *end,
444 mbedtls_x509_time *from,
445 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446{
Janos Follath865b3eb2019-12-16 11:46:15 +0000447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448 size_t len;
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
451 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
452 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
453 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455 end = *p + len;
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
458 return ret;
459 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
462 return ret;
463 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (*p != end) {
466 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
467 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
468 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471}
472
473/*
474 * X.509 v2/v3 unique identifier (not parsed)
475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476static int x509_get_uid(unsigned char **p,
477 const unsigned char *end,
478 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479{
Janos Follath865b3eb2019-12-16 11:46:15 +0000480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (*p == end) {
483 return 0;
484 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485
486 uid->tag = **p;
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
489 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
490 n)) != 0) {
491 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
492 return 0;
493 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 }
497
498 uid->p = *p;
499 *p += uid->len;
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502}
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504static int x509_get_basic_constraints(unsigned char **p,
505 const unsigned char *end,
506 int *ca_istrue,
507 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508{
Janos Follath865b3eb2019-12-16 11:46:15 +0000509 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 size_t len;
511
512 /*
513 * BasicConstraints ::= SEQUENCE {
514 * cA BOOLEAN DEFAULT FALSE,
515 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
516 */
517 *ca_istrue = 0; /* DEFAULT FALSE */
518 *max_pathlen = 0; /* endless */
519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
521 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
522 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 }
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (*p == end) {
526 return 0;
527 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
530 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
531 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
532 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (ret != 0) {
535 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
536 }
537
538 if (*ca_istrue != 0) {
539 *ca_istrue = 1;
540 }
541 }
542
543 if (*p == end) {
544 return 0;
545 }
546
547 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
548 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
549 }
550
551 if (*p != end) {
552 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
553 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
554 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Andrzej Kurek16050742020-04-14 09:49:52 -0400556 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
557 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (*max_pathlen == INT_MAX) {
559 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
560 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
561 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400562
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563 (*max_pathlen)++;
564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566}
567
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568/*
569 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
570 *
571 * KeyPurposeId ::= OBJECT IDENTIFIER
572 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100573static int x509_get_ext_key_usage(unsigned char **p,
574 const unsigned char *end,
575 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576{
Janos Follath865b3eb2019-12-16 11:46:15 +0000577 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
580 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
581 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582
583 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (ext_key_usage->buf.p == NULL) {
585 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
586 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
587 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590}
591
592/*
toth92ga41954d2021-02-12 16:11:17 +0100593 * SubjectKeyIdentifier ::= KeyIdentifier
594 *
595 * KeyIdentifier ::= OCTET STRING
596 */
597static int x509_get_subject_key_id(unsigned char **p,
598 const unsigned char *end,
599 mbedtls_x509_buf *subject_key_id)
600{
601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
602 size_t len = 0u;
603
604 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
605 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100606 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100607 }
608
Przemek Stekiel61aed062023-05-08 11:14:36 +0200609 subject_key_id->len = len;
610 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
611 subject_key_id->p = *p;
612 *p += len;
613
toth92gd96027a2021-04-27 15:41:25 +0200614 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100615 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
616 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200617 }
618
toth92ga41954d2021-02-12 16:11:17 +0100619 return 0;
620}
621
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100622/*
toth92g8d435a02021-05-10 15:16:33 +0200623 * AuthorityKeyIdentifier ::= SEQUENCE {
624 * keyIdentifier [0] KeyIdentifier OPTIONAL,
625 * authorityCertIssuer [1] GeneralNames OPTIONAL,
626 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
627 *
628 * KeyIdentifier ::= OCTET STRING
629 */
630static int x509_get_authority_key_id(unsigned char **p,
631 unsigned char *end,
632 mbedtls_x509_authority *authority_key_id)
633{
634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
635 size_t len = 0u;
636
637 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100638 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100639 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200640 }
641
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100642 if (*p + len != end) {
643 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
644 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
645 }
646
Przemek Stekieled9fb782023-05-03 16:27:25 +0200647 ret = mbedtls_asn1_get_tag(p, end, &len,
648 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
649
650 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200651 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200652 authority_key_id->keyIdentifier.len = len;
653 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200654 /* Setting tag of the keyIdentfier intentionally to 0x04.
655 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200656 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200657 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
658
659 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200660 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
661 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200662 }
663
664 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200665 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200666 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
667 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100668 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200669 /* authorityCertIssuer and authorityCertSerialNumber MUST both
670 be present or both be absent. At this point we expect to have both. */
671 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200672 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200673 /* "end" also includes the CertSerialNumber field so "len" shall be used */
674 ret = mbedtls_x509_get_subject_alt_name_ext(p,
675 (*p+len),
676 &authority_key_id->authorityCertIssuer);
677 if (ret != 0) {
678 return ret;
679 }
680
681 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
682 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
683 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
684 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
685 }
686 authority_key_id->authorityCertSerialNumber.len = len;
687 authority_key_id->authorityCertSerialNumber.p = *p;
688 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
689 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200690 }
691
692 if (*p != end) {
693 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
694 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
695 }
696
697 return 0;
698}
699
700/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200701 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
702 *
703 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
704 *
705 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
706 *
707 * PolicyInformation ::= SEQUENCE {
708 * policyIdentifier CertPolicyId,
709 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
710 * PolicyQualifierInfo OPTIONAL }
711 *
712 * CertPolicyId ::= OBJECT IDENTIFIER
713 *
714 * PolicyQualifierInfo ::= SEQUENCE {
715 * policyQualifierId PolicyQualifierId,
716 * qualifier ANY DEFINED BY policyQualifierId }
717 *
718 * -- policyQualifierIds for Internet policy qualifiers
719 *
720 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
721 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
722 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
723 *
724 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
725 *
726 * Qualifier ::= CHOICE {
727 * cPSuri CPSuri,
728 * userNotice UserNotice }
729 *
730 * CPSuri ::= IA5String
731 *
732 * UserNotice ::= SEQUENCE {
733 * noticeRef NoticeReference OPTIONAL,
734 * explicitText DisplayText OPTIONAL }
735 *
736 * NoticeReference ::= SEQUENCE {
737 * organization DisplayText,
738 * noticeNumbers SEQUENCE OF INTEGER }
739 *
740 * DisplayText ::= CHOICE {
741 * ia5String IA5String (SIZE (1..200)),
742 * visibleString VisibleString (SIZE (1..200)),
743 * bmpString BMPString (SIZE (1..200)),
744 * utf8String UTF8String (SIZE (1..200)) }
745 *
746 * NOTE: we only parse and use anyPolicy without qualifiers at this point
747 * as defined in RFC 5280.
748 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749static int x509_get_certificate_policies(unsigned char **p,
750 const unsigned char *end,
751 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200752{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300753 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200754 size_t len;
755 mbedtls_asn1_buf *buf;
756 mbedtls_asn1_sequence *cur = certificate_policies;
757
758 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 ret = mbedtls_asn1_get_tag(p, end, &len,
760 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
761 if (ret != 0) {
762 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
763 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (*p + len != end) {
766 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
767 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
768 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200769
770 /*
771 * Cannot be an empty sequence.
772 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (len == 0) {
774 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
775 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
776 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200779 mbedtls_x509_buf policy_oid;
780 const unsigned char *policy_end;
781
782 /*
783 * Get the policy sequence
784 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
786 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
787 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
788 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200789
790 policy_end = *p + len;
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
793 MBEDTLS_ASN1_OID)) != 0) {
794 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
795 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200796
797 policy_oid.tag = MBEDTLS_ASN1_OID;
798 policy_oid.len = len;
799 policy_oid.p = *p;
800
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300801 /*
802 * Only AnyPolicy is currently supported when enforcing policy.
803 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300805 /*
806 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200807 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300808 */
809 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
810 }
811
Ron Eldor74d9acc2019-03-21 14:00:03 +0200812 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if (cur->buf.p != NULL) {
814 if (cur->next != NULL) {
815 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
816 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (cur->next == NULL) {
821 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
822 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
823 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200824
825 cur = cur->next;
826 }
827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200829 buf->tag = policy_oid.tag;
830 buf->p = policy_oid.p;
831 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300832
833 *p += len;
834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 /*
836 * If there is an optional qualifier, then *p < policy_end
837 * Check the Qualifier len to verify it doesn't exceed policy_end.
838 */
839 if (*p < policy_end) {
840 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
841 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
842 0) {
843 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
844 }
Ron Eldor08063792019-05-13 16:38:39 +0300845 /*
846 * Skip the optional policy qualifiers.
847 */
848 *p += len;
849 }
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (*p != policy_end) {
852 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
853 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
854 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200855 }
856
857 /* Set final sequence entry's next pointer to NULL */
858 cur->next = NULL;
859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (*p != end) {
861 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
862 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
863 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200866}
867
868/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869 * X.509 v3 extensions
870 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872static int x509_get_crt_ext(unsigned char **p,
873 const unsigned char *end,
874 mbedtls_x509_crt *crt,
875 mbedtls_x509_crt_ext_cb_t cb,
876 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877{
Janos Follath865b3eb2019-12-16 11:46:15 +0000878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200880 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (*p == end) {
883 return 0;
884 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
887 return ret;
888 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889
Hanno Becker12f62fb2019-02-12 17:22:36 +0000890 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 /*
893 * Extension ::= SEQUENCE {
894 * extnID OBJECT IDENTIFIER,
895 * critical BOOLEAN DEFAULT FALSE,
896 * extnValue OCTET STRING }
897 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 int is_critical = 0; /* DEFAULT FALSE */
900 int ext_type = 0;
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
903 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
904 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
905 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906
907 end_ext_data = *p + len;
908
909 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
911 MBEDTLS_ASN1_OID)) != 0) {
912 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
913 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914
k-stachowiak463928a2018-07-24 12:50:59 +0200915 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 extn_oid.p = *p;
917 *p += extn_oid.len;
918
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
921 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
922 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
923 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
925 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
927 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
928 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
929 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200931 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932 end_ext_octet = *p + len;
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (end_ext_octet != end_ext_data) {
935 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
936 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
937 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939 /*
940 * Detect supported extensions
941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200945 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (cb != NULL) {
947 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
948 if (ret != 0 && is_critical) {
949 return ret;
950 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200951 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200952 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200953 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200954
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 /* No parser found, skip extension */
956 *p = end_ext_octet;
957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
961 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 continue;
964 }
965
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100966 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if ((crt->ext_types & ext_type) != 0) {
968 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
969 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100970
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 crt->ext_types |= ext_type;
972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 switch (ext_type) {
974 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
975 /* Parse basic constraints */
976 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
977 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
978 return ret;
979 }
980 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 case MBEDTLS_X509_EXT_KEY_USAGE:
983 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100984 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
985 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 return ret;
987 }
988 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
991 /* Parse extended key usage */
992 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
993 &crt->ext_key_usage)) != 0) {
994 return ret;
995 }
996 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997
toth92ga41954d2021-02-12 16:11:17 +0100998 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
999 /* Parse subject key identifier */
1000 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1001 &crt->subject_key_id)) != 0) {
1002 return ret;
1003 }
1004 break;
toth92g8d435a02021-05-10 15:16:33 +02001005
toth92ga41954d2021-02-12 16:11:17 +01001006 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1007 /* Parse authority key identifier */
1008 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1009 &crt->authority_key_id)) != 0) {
1010 return ret;
1011 }
1012 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001014 /* Parse subject alt name
1015 * SubjectAltName ::= GeneralNames
1016 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001017 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1018 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 return ret;
1020 }
1021 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1024 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001025 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1026 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 return ret;
1028 }
1029 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1032 /* Parse certificate policies type */
1033 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1034 &crt->certificate_policies)) != 0) {
1035 /* Give the callback (if any) a chance to handle the extension
1036 * if it contains unsupported policies */
1037 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1038 cb(p_ctx, crt, &extn_oid, is_critical,
1039 start_ext_octet, end_ext_octet) == 0) {
1040 break;
1041 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if (is_critical) {
1044 return ret;
1045 } else
1046 /*
1047 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1048 * cannot interpret or enforce the policy. However, it is up to
1049 * the user to choose how to enforce the policies,
1050 * unless the extension is critical.
1051 */
1052 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1053 return ret;
1054 }
1055 }
1056 break;
1057
1058 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001059 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 * If this is a non-critical extension, which the oid layer
1061 * supports, but there isn't an x509 parser for it,
1062 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001063 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (is_critical) {
1065 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1066 } else {
1067 *p = end_ext_octet;
1068 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069 }
1070 }
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (*p != end) {
1073 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1074 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1075 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078}
1079
1080/*
1081 * Parse and fill a single X.509 certificate in DER format
1082 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001083static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1084 const unsigned char *buf,
1085 size_t buflen,
1086 int make_copy,
1087 mbedtls_x509_crt_ext_cb_t cb,
1088 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089{
Janos Follath865b3eb2019-12-16 11:46:15 +00001090 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091 size_t len;
1092 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1096 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1097 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098
1099 /*
1100 * Check for valid input
1101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (crt == NULL || buf == NULL) {
1103 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1104 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001106 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001108 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109 end = p + len;
1110
1111 /*
1112 * Certificate ::= SEQUENCE {
1113 * tbsCertificate TBSCertificate,
1114 * signatureAlgorithm AlgorithmIdentifier,
1115 * signatureValue BIT STRING }
1116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1118 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1119 mbedtls_x509_crt_free(crt);
1120 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001121 }
1122
Janos Follathcc0e49d2016-02-17 14:34:12 +00001123 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001124 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001126 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1128 if (crt->raw.p == NULL) {
1129 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1130 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001133 crt->own_buffer = 1;
1134
1135 p += crt->raw.len - len;
1136 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 } else {
1138 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001139 crt->own_buffer = 0;
1140 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001141
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 /*
1143 * TBSCertificate ::= SEQUENCE {
1144 */
1145 crt->tbs.p = p;
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1148 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1149 mbedtls_x509_crt_free(crt);
1150 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001151 }
1152
1153 end = p + len;
1154 crt->tbs.len = end - crt->tbs.p;
1155
1156 /*
1157 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1158 *
1159 * CertificateSerialNumber ::= INTEGER
1160 *
1161 * signature AlgorithmIdentifier
1162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1164 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1165 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1166 &sig_params1)) != 0) {
1167 mbedtls_x509_crt_free(crt);
1168 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 }
1170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (crt->version < 0 || crt->version > 2) {
1172 mbedtls_x509_crt_free(crt);
1173 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 }
1175
Andres AG7ca4a032017-03-09 16:16:11 +00001176 crt->version++;
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1179 &crt->sig_md, &crt->sig_pk,
1180 &crt->sig_opts)) != 0) {
1181 mbedtls_x509_crt_free(crt);
1182 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183 }
1184
1185 /*
1186 * issuer Name
1187 */
1188 crt->issuer_raw.p = p;
1189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1191 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1192 mbedtls_x509_crt_free(crt);
1193 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 }
1195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1197 mbedtls_x509_crt_free(crt);
1198 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199 }
1200
1201 crt->issuer_raw.len = p - crt->issuer_raw.p;
1202
1203 /*
1204 * Validity ::= SEQUENCE {
1205 * notBefore Time,
1206 * notAfter Time }
1207 *
1208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1210 &crt->valid_to)) != 0) {
1211 mbedtls_x509_crt_free(crt);
1212 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001213 }
1214
1215 /*
1216 * subject Name
1217 */
1218 crt->subject_raw.p = p;
1219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1221 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1222 mbedtls_x509_crt_free(crt);
1223 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224 }
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1227 mbedtls_x509_crt_free(crt);
1228 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 }
1230
1231 crt->subject_raw.len = p - crt->subject_raw.p;
1232
1233 /*
1234 * SubjectPublicKeyInfo
1235 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001236 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1238 mbedtls_x509_crt_free(crt);
1239 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001240 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001241 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242
1243 /*
1244 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1245 * -- If present, version shall be v2 or v3
1246 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1247 * -- If present, version shall be v2 or v3
1248 * extensions [3] EXPLICIT Extensions OPTIONAL
1249 * -- If present, version shall be v3
1250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (crt->version == 2 || crt->version == 3) {
1252 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1253 if (ret != 0) {
1254 mbedtls_x509_crt_free(crt);
1255 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256 }
1257 }
1258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if (crt->version == 2 || crt->version == 3) {
1260 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1261 if (ret != 0) {
1262 mbedtls_x509_crt_free(crt);
1263 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001264 }
1265 }
1266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if (crt->version == 3) {
1268 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1269 if (ret != 0) {
1270 mbedtls_x509_crt_free(crt);
1271 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 }
1273 }
1274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (p != end) {
1276 mbedtls_x509_crt_free(crt);
1277 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1278 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 }
1280
1281 end = crt_end;
1282
1283 /*
1284 * }
1285 * -- end of TBSCertificate
1286 *
1287 * signatureAlgorithm AlgorithmIdentifier,
1288 * signatureValue BIT STRING
1289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1291 mbedtls_x509_crt_free(crt);
1292 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 }
1294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (crt->sig_oid.len != sig_oid2.len ||
1296 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001297 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001298 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 (sig_params1.len != 0 &&
1300 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1301 mbedtls_x509_crt_free(crt);
1302 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 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 (p != end) {
1311 mbedtls_x509_crt_free(crt);
1312 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1313 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 }
1315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317}
1318
1319/*
1320 * Parse one X.509 certificate in DER format from a buffer and add them to a
1321 * chained list
1322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1324 const unsigned char *buf,
1325 size_t buflen,
1326 int make_copy,
1327 mbedtls_x509_crt_ext_cb_t cb,
1328 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329{
Janos Follath865b3eb2019-12-16 11:46:15 +00001330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001332
1333 /*
1334 * Check for valid input
1335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (crt == NULL || buf == NULL) {
1337 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1338 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341 prev = crt;
1342 crt = crt->next;
1343 }
1344
1345 /*
1346 * Add new certificate on the end of the chain if needed.
1347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 if (crt->version != 0 && crt->next == NULL) {
1349 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (crt->next == NULL) {
1352 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1353 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354
1355 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358 }
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1361 if (ret != 0) {
1362 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if (crt != chain) {
1367 mbedtls_free(crt);
1368 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371 }
1372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374}
1375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1377 const unsigned char *buf,
1378 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001379{
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001381}
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1384 const unsigned char *buf,
1385 size_t buflen,
1386 int make_copy,
1387 mbedtls_x509_crt_ext_cb_t cb,
1388 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001389{
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001391}
1392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1394 const unsigned char *buf,
1395 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001396{
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001398}
1399
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001400/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001401 * Parse one or more PEM certificates from a buffer and add them to the chained
1402 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001404int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1405 const unsigned char *buf,
1406 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001407{
Janos Follath98e28a72016-05-31 14:03:54 +01001408#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001409 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001411#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412
1413 /*
1414 * Check for valid input
1415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 if (chain == NULL || buf == NULL) {
1417 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1418 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001419
1420 /*
1421 * Determine buffer content. Buffer contains either one DER certificate or
1422 * one or more PEM certificates.
1423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1426 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001428 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1431 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1432 }
Janos Follath98e28a72016-05-31 14:03:54 +01001433#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001435#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001442 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001447 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 ret = mbedtls_pem_read_buffer(&pem,
1449 "-----BEGIN CERTIFICATE-----",
1450 "-----END CERTIFICATE-----",
1451 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454 /*
1455 * Was PEM encoded
1456 */
1457 buflen -= use_len;
1458 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1460 return ret;
1461 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1462 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463
1464 /*
1465 * PEM header and footer were found
1466 */
1467 buflen -= use_len;
1468 buf += use_len;
1469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001473
Paul Bakker5a5fa922014-09-26 14:53:04 +02001474 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485 /*
1486 * Quit parsing on a memory error
1487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1489 return ret;
1490 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495
1496 total_failed++;
1497 continue;
1498 }
1499
1500 success = 1;
1501 }
1502 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if (success) {
1505 return total_failed;
1506 } else if (first_error) {
1507 return first_error;
1508 } else {
1509 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1510 }
Janos Follath98e28a72016-05-31 14:03:54 +01001511#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512}
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515/*
1516 * Load one or more certificates and add them to the chained list
1517 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001518int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519{
Janos Follath865b3eb2019-12-16 11:46:15 +00001520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521 size_t n;
1522 unsigned char *buf;
1523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1525 return ret;
1526 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001530 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533}
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536{
1537 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001538#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Steve Lhomme369d7c72023-06-16 14:16:03 +02001539#if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_XP */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540 int w_ret;
1541 WCHAR szDir[MAX_PATH];
1542 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001543 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 size_t len = strlen(path);
Simon Butcherde573f52018-07-05 09:11:30 +01001545 int length_as_int = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546
Paul Bakker9af723c2014-05-01 13:03:14 +02001547 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548 HANDLE hFind;
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (len > MAX_PATH - 3) {
1551 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1552 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 memset(szDir, 0, sizeof(szDir));
1555 memset(filename, 0, MAX_PATH);
1556 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001557 filename[len++] = '\\';
1558 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559 filename[len++] = '*';
1560
Minos Galanakisa277b212023-08-09 16:32:22 +01001561 if (FAILED(SizeTToInt(len, &length_as_int))) {
1562 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1563 }
Kevin Kane0ec1e682016-12-15 09:27:16 -08001564
Simon Butcher35e5dad2018-03-15 15:00:03 +00001565 /*
1566 * Note this function uses the code page CP_ACP, and assumes the incoming
1567 * string is encoded in ANSI, before translating it into Unicode. If the
1568 * incoming string were changed to be UTF-8, then the length check needs to
1569 * change to check the number of characters, not the number of bytes, in the
1570 * incoming string are less than MAX_PATH to avoid a buffer overrun with
1571 * MultiByteToWideChar().
1572 */
Simon Butcherde573f52018-07-05 09:11:30 +01001573 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, length_as_int, szDir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 MAX_PATH - 3);
1575 if (w_ret == 0) {
1576 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1577 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 hFind = FindFirstFileW(szDir, &file_data);
1580 if (hFind == INVALID_HANDLE_VALUE) {
1581 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1582 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583
1584 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 do {
1586 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591
Minos Galanakisa277b212023-08-09 16:32:22 +01001592 if (FAILED(SizeTToInt(wcslen(file_data.cFileName), &length_as_int))) {
1593 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1594 }
Kevin Kane0ec1e682016-12-15 09:27:16 -08001595
Simon Butcherde573f52018-07-05 09:11:30 +01001596 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1597 length_as_int,
1598 p, (int) len - 1,
1599 NULL, NULL);
Minos Galanakisa277b212023-08-09 16:32:22 +01001600 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001601 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1602 goto cleanup;
1603 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1606 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 }
1611 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616
Ron Eldor36d90422017-01-09 15:09:16 +02001617cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 FindClose(hFind);
Steve Lhomme369d7c72023-06-16 14:16:03 +02001619#else /* !_WIN32_WINNT_XP */
Antonio de Angelis1ee4d122023-08-16 12:26:37 +01001620#error "mbedtls_x509_crt_parse_path not available before Windows XP"
Steve Lhomme369d7c72023-06-16 14:16:03 +02001621#endif /* !_WIN32_WINNT_XP */
Paul Bakkerbe089b02013-10-14 15:51:50 +02001622#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001623 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001624 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001626 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001627 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 if (dir == NULL) {
1631 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1632 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633
Ron Eldor63140682017-01-09 19:27:59 +02001634#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1636 closedir(dir);
1637 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001638 }
Ron Eldor63140682017-01-09 19:27:59 +02001639#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001644 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001647 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001648 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1649 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 } else if (stat(entry_name, &sb) == -1) {
1651 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001652 /* Broken symbolic link - ignore this entry.
1653 stat(2) will return this error for either (a) a dangling
1654 symlink or (b) a missing file.
1655 Given that we have just obtained the filename from readdir,
1656 assume that it does exist and therefore treat this as a
1657 dangling symlink. */
1658 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001660 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001661 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1662 goto cleanup;
1663 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664 }
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669
1670 // Ignore parse errors
1671 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1673 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001679
1680cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001682
Ron Eldor63140682017-01-09 19:27:59 +02001683#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 }
Ron Eldor63140682017-01-09 19:27:59 +02001687#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001688
Paul Bakkerbe089b02013-10-14 15:51:50 +02001689#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001692}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694
Peter Kolbus9a969b62018-12-11 13:55:56 -06001695#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001696#define PRINT_ITEM(i) \
1697 do { \
1698 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1699 MBEDTLS_X509_SAFE_SNPRINTF; \
1700 sep = ", "; \
1701 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001702
Przemek Stekielf4194942023-04-24 09:52:17 +02001703#define CERT_TYPE(type, name) \
1704 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001705 if (ns_cert_type & (type)) { \
1706 PRINT_ITEM(name); \
1707 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001708 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001709
Przemek Stekielf4194942023-04-24 09:52:17 +02001710#define KEY_USAGE(code, name) \
1711 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001712 if (key_usage & (code)) { \
1713 PRINT_ITEM(name); \
1714 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001715 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001716
Gilles Peskine449bd832023-01-11 14:50:10 +01001717static int x509_info_ext_key_usage(char **buf, size_t *size,
1718 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001719{
Janos Follath865b3eb2019-12-16 11:46:15 +00001720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001721 const char *desc;
1722 size_t n = *size;
1723 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001725 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 while (cur != NULL) {
1728 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001729 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001733 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001734
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001735 sep = ", ";
1736
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001737 cur = cur->next;
1738 }
1739
1740 *size = n;
1741 *buf = p;
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001744}
1745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746static int x509_info_cert_policies(char **buf, size_t *size,
1747 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001748{
Janos Follath865b3eb2019-12-16 11:46:15 +00001749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001750 const char *desc;
1751 size_t n = *size;
1752 char *p = *buf;
1753 const mbedtls_x509_sequence *cur = certificate_policies;
1754 const char *sep = "";
1755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 while (cur != NULL) {
1757 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001758 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001762 MBEDTLS_X509_SAFE_SNPRINTF;
1763
1764 sep = ", ";
1765
1766 cur = cur->next;
1767 }
1768
1769 *size = n;
1770 *buf = p;
1771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001773}
1774
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775/*
1776 * Return an informational string about the certificate.
1777 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001778#define BEFORE_COLON 18
1779#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001780int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1781 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782{
Janos Follath865b3eb2019-12-16 11:46:15 +00001783 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784 size_t n;
1785 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786 char key_size_str[BEFORE_COLON];
1787
1788 p = buf;
1789 n = size;
1790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (NULL == crt) {
1792 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001793 MBEDTLS_X509_SAFE_SNPRINTF;
1794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001796 }
1797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1799 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001800 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1802 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001803 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001806 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001809 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001811 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001814 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001816 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1819 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1820 crt->valid_from.year, crt->valid_from.mon,
1821 crt->valid_from.day, crt->valid_from.hour,
1822 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001823 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1826 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1827 crt->valid_to.year, crt->valid_to.mon,
1828 crt->valid_to.day, crt->valid_to.hour,
1829 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001830 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001833 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1836 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001837 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001838
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001839 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1841 mbedtls_pk_get_name(&crt->pk))) != 0) {
1842 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001843 }
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1846 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001847 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001848
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001849 /*
1850 * Optional extensions
1851 */
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1854 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1855 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001856 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 if (crt->max_pathlen > 0) {
1859 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001860 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001861 }
1862 }
1863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1865 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001866 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001867
Przemek Stekiel21c37282023-01-16 08:47:49 +01001868 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1869 &crt->subject_alt_names,
1870 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 return ret;
1872 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001873 }
1874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1876 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001877 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001878
Przemek Stekiel21c37282023-01-16 08:47:49 +01001879 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 return ret;
1881 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001882 }
1883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1885 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001886 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001887
Przemek Stekiel21c37282023-01-16 08:47:49 +01001888 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 return ret;
1890 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001891 }
1892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1894 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001895 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 if ((ret = x509_info_ext_key_usage(&p, &n,
1898 &crt->ext_key_usage)) != 0) {
1899 return ret;
1900 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001901 }
1902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1904 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001905 MBEDTLS_X509_SAFE_SNPRINTF;
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 if ((ret = x509_info_cert_policies(&p, &n,
1908 &crt->certificate_policies)) != 0) {
1909 return ret;
1910 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001911 }
1912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001914 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001917}
1918
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001919struct x509_crt_verify_string {
1920 int code;
1921 const char *string;
1922};
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001925static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001926 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001927 { 0, NULL }
1928};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001929#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1932 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001933{
Janos Follath865b3eb2019-12-16 11:46:15 +00001934 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001935 const struct x509_crt_verify_string *cur;
1936 char *p = buf;
1937 size_t n = size;
1938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1940 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001941 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001945 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001946 flags ^= cur->code;
1947 }
1948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (flags != 0) {
1950 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1951 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001952 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001953 }
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001956}
Hanno Becker612a2f12020-10-09 09:19:39 +01001957#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1960 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001961{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001962 unsigned int usage_must, usage_may;
1963 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1967 return 0;
1968 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001969
1970 usage_must = usage & ~may_mask;
1971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1973 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1974 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001975
1976 usage_may = usage & may_mask;
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1979 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1980 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001983}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1986 const char *usage_oid,
1987 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001988{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001990
1991 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1993 return 0;
1994 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001995
1996 /*
1997 * Look for the requested usage (or wildcard ANY) in our list
1998 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 if (cur_oid->len == usage_len &&
2003 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2004 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002005 }
2006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2008 return 0;
2009 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002010 }
2011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002013}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016/*
2017 * Return 1 if the certificate is revoked, or 0 otherwise.
2018 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002019int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 while (cur != NULL && cur->serial.len != 0) {
2024 if (crt->serial.len == cur->serial.len &&
2025 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2026 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002027 }
2028
2029 cur = cur->next;
2030 }
2031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002033}
2034
2035/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002036 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002037 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2040 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002041 const mbedtls_x509_crt_profile *profile,
2042 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043{
2044 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002045 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002046#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002047 psa_algorithm_t psa_algorithm;
2048#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002050#endif /* MBEDTLS_USE_PSA_CRYPTO */
2051 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 if (ca == NULL) {
2054 return flags;
2055 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 while (crl_list != NULL) {
2058 if (crl_list->version == 0 ||
2059 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002060 crl_list = crl_list->next;
2061 continue;
2062 }
2063
2064 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002065 * Check if the CA is configured to sign CRLs
2066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (mbedtls_x509_crt_check_key_usage(ca,
2068 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002070 break;
2071 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002072
2073 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002074 * Check if CRL is correctly signed by the trusted CA
2075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002077 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002081 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002083
pespacek7599a772022-02-07 14:40:55 +01002084#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002085 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 if (psa_hash_compute(psa_algorithm,
2087 crl_list->tbs.p,
2088 crl_list->tbs.len,
2089 hash,
2090 sizeof(hash),
2091 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002092 /* Note: this can't happen except after an internal error */
2093 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2094 break;
2095 }
pespacek7599a772022-02-07 14:40:55 +01002096#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2098 hash_length = mbedtls_md_get_size(md_info);
2099 if (mbedtls_md(md_info,
2100 crl_list->tbs.p,
2101 crl_list->tbs.len,
2102 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002103 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105 break;
2106 }
pespaceka7a64692022-02-14 15:18:43 +01002107#endif /* MBEDTLS_USE_PSA_CRYPTO */
2108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002110 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2114 crl_list->sig_md, hash, hash_length,
2115 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117 break;
2118 }
2119
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002120#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002121 /*
2122 * Check for validity of CRL (Do not drop out)
2123 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002124 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002127
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002128 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002129 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002131#else
2132 ((void) now);
2133#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002134
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002135 /*
2136 * Check if certificate is revoked
2137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002140 break;
2141 }
2142
2143 crl_list = crl_list->next;
2144 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002149
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002150/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002151 * Check the signature of a certificate by its parent
2152 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002153static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2154 mbedtls_x509_crt *parent,
2155 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002156{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002157 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002158 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002159#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2160 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 md_info = mbedtls_md_info_from_type(child->sig_md);
2162 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002163
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002164 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2166 return -1;
2167 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002168#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002169 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 status = psa_hash_compute(hash_alg,
2173 child->tbs.p,
2174 child->tbs.len,
2175 hash,
2176 sizeof(hash),
2177 &hash_len);
2178 if (status != PSA_SUCCESS) {
2179 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002180 }
2181
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002182#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002183 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2185 return -1;
2186 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002187
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002188#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2190 return mbedtls_pk_verify_restartable(&parent->pk,
2191 child->sig_md, hash, hash_len,
2192 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002193 }
2194#else
2195 (void) rs_ctx;
2196#endif
2197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2199 child->sig_md, hash, hash_len,
2200 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002201}
2202
2203/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002204 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2205 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002206 *
2207 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002209static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2210 const mbedtls_x509_crt *parent,
2211 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002212{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002213 int need_ca_bit;
2214
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002215 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2217 return -1;
2218 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002219
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002220 /* Parent must have the basicConstraints CA bit set as a general rule */
2221 need_ca_bit = 1;
2222
2223 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002225 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002226 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if (need_ca_bit && !parent->ca_istrue) {
2229 return -1;
2230 }
2231
2232 if (need_ca_bit &&
2233 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2234 return -1;
2235 }
2236
2237 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002238}
2239
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002240/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002241 * Find a suitable parent for child in candidates, or return NULL.
2242 *
2243 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002244 * 1. subject name matches child's issuer
2245 * 2. if necessary, the CA bit is set and key usage allows signing certs
2246 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002247 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002248 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002249 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002250 * If there's a suitable candidate which is also time-valid, return the first
2251 * such. Otherwise, return the first suitable candidate (or NULL if there is
2252 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002253 *
2254 * The rationale for this rule is that someone could have a list of trusted
2255 * roots with two versions on the same root with different validity periods.
2256 * (At least one user reported having such a list and wanted it to just work.)
2257 * The reason we don't just require time-validity is that generally there is
2258 * only one version, and if it's expired we want the flags to state that
2259 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002260 *
2261 * The rationale for rule 3 (signature for trusted roots) is that users might
2262 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002263 * way we select the correct one is by checking the signature (as we don't
2264 * rely on key identifier extensions). (This is one way users might choose to
2265 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002266 *
2267 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002268 * - [in] child: certificate for which we're looking for a parent
2269 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002270 * - [out] r_parent: parent found (or NULL)
2271 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002272 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2273 * of the chain, 0 otherwise
2274 * - [in] path_cnt: number of intermediates seen so far
2275 * - [in] self_cnt: number of self-signed intermediates seen so far
2276 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002277 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002278 *
2279 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002280 * - 0 on success
2281 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002282 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002283static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 mbedtls_x509_crt *child,
2285 mbedtls_x509_crt *candidates,
2286 mbedtls_x509_crt **r_parent,
2287 int *r_signature_is_good,
2288 int top,
2289 unsigned path_cnt,
2290 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002291 mbedtls_x509_crt_restart_ctx *rs_ctx,
2292 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002293{
Janos Follath865b3eb2019-12-16 11:46:15 +00002294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002295 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002296 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002297
2298#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002299 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002301 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002302 parent = rs_ctx->parent;
2303 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002304 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002305
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002306 /* clear saved state */
2307 rs_ctx->parent = NULL;
2308 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002309 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002310
2311 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002312 goto check_signature;
2313 }
2314#endif
2315
2316 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002317 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002320 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002322 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002324
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002325 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (parent->max_pathlen > 0 &&
2327 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002328 continue;
2329 }
2330
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002331 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002332#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2333check_signature:
2334#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002336
2337#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002339 /* save state */
2340 rs_ctx->parent = parent;
2341 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002342 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002345 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002346#else
2347 (void) ret;
2348#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002349
2350 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002352 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002354
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002355#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002356 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002357 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2358 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002360 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002361 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002362 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002363
2364 continue;
2365 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002366#else
2367 ((void) now);
2368#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002369
Andy Gross1f627142019-01-30 10:25:53 -06002370 *r_parent = parent;
2371 *r_signature_is_good = signature_is_good;
2372
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002373 break;
2374 }
2375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002377 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002378 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002379 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002382}
2383
2384/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002385 * Find a parent in trusted CAs or the provided chain, or return NULL.
2386 *
2387 * Searches in trusted CAs first, and return the first suitable parent found
2388 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002389 *
2390 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002391 * - [in] child: certificate for which we're looking for a parent, followed
2392 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002393 * - [in] trust_ca: list of locally trusted certificates
2394 * - [out] parent: parent found (or NULL)
2395 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2396 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2397 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2398 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002399 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002400 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002401 *
2402 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002403 * - 0 on success
2404 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002405 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002406static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 mbedtls_x509_crt *child,
2408 mbedtls_x509_crt *trust_ca,
2409 mbedtls_x509_crt **parent,
2410 int *parent_is_trusted,
2411 int *signature_is_good,
2412 unsigned path_cnt,
2413 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002414 mbedtls_x509_crt_restart_ctx *rs_ctx,
2415 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002416{
Janos Follath865b3eb2019-12-16 11:46:15 +00002417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002418 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002419
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002420 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002421
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002422#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002423 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002425 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002426 rs_ctx->parent_is_trusted = -1;
2427 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002428#endif
2429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002431 search_list = *parent_is_trusted ? trust_ca : child->next;
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 ret = x509_crt_find_parent_in(child, search_list,
2434 parent, signature_is_good,
2435 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002436 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002437
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002438#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002440 /* save state */
2441 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002443 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002444#else
2445 (void) ret;
2446#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002447
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002448 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002450 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002452
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002453 /* prepare second iteration */
2454 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002455 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002456
2457 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002459 *parent_is_trusted = 0;
2460 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002461 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002464}
2465
2466/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002467 * Check if an end-entity certificate is locally trusted
2468 *
2469 * Currently we require such certificates to be self-signed (actually only
2470 * check for self-issued as self-signatures are not checked)
2471 */
2472static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 mbedtls_x509_crt *crt,
2474 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002475{
2476 mbedtls_x509_crt *cur;
2477
2478 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2480 return -1;
2481 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002482
2483 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2485 if (crt->raw.len == cur->raw.len &&
2486 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2487 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002488 }
2489 }
2490
2491 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002493}
2494
2495/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002496 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002497 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002498 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2499 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002500 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002501 * such that every cert in the chain is a child of the next one,
2502 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002503 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002504 * Verify that chain and return it with flags for all issues found.
2505 *
2506 * Special cases:
2507 * - EE == Rj -> return a one-element list containing it
2508 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2509 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002510 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002511 * Tests for (aspects of) this function should include at least:
2512 * - trusted EE
2513 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002514 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002515 * - if relevant: EE untrusted
2516 * - if relevant: EE -> intermediate, untrusted
2517 * with the aspect under test checked at each relevant level (EE, int, root).
2518 * For some aspects longer chains are required, but usually length 2 is
2519 * enough (but length 1 is not in general).
2520 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002521 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002522 * - [in] crt: the cert list EE, C1, ..., Cn
2523 * - [in] trust_ca: the trusted list R1, ..., Rp
2524 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002525 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002526 * Only valid when return value is 0, may contain garbage otherwise!
2527 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002528 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002529 *
2530 * Return value:
2531 * - non-zero if the chain could not be fully built and examined
2532 * - 0 is the chain was successfully built and examined,
2533 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002534 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002535static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 mbedtls_x509_crt *crt,
2537 mbedtls_x509_crt *trust_ca,
2538 mbedtls_x509_crl *ca_crl,
2539 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2540 void *p_ca_cb,
2541 const mbedtls_x509_crt_profile *profile,
2542 mbedtls_x509_crt_verify_chain *ver_chain,
2543 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002544{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002545 /* Don't initialize any of those variables here, so that the compiler can
2546 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002548 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002549 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002550 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002551 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002552 int parent_is_trusted;
2553 int child_is_trusted;
2554 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002555 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002556 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002557 mbedtls_x509_time now;
2558
2559#if defined(MBEDTLS_HAVE_TIME_DATE)
2560 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2561 return MBEDTLS_ERR_X509_FATAL_ERROR;
2562 }
2563#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002564
2565#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2566 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002568 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002569 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002570 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002571
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002572 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002573 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002574 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002575 flags = &cur->flags;
2576
2577 goto find_parent;
2578 }
2579#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002580
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002581 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582 self_cnt = 0;
2583 parent_is_trusted = 0;
2584 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002587 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002588 cur = &ver_chain->items[ver_chain->len];
2589 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002590 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002591 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002592 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002593
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002594#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002595 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002596 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002597 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002599
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002600 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002601 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002603#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002604
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002605 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (child_is_trusted) {
2607 return 0;
2608 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002609
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002610 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002612 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002616 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002618
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002619 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (ver_chain->len == 1 &&
2621 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2622 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002623 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002624
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002625#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2626find_parent:
2627#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002628
2629 /* Obtain list of potential trusted signers from CA callback,
2630 * or use statically provided list. */
2631#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (f_ca_cb != NULL) {
2633 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2634 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002635 ver_chain->trust_ca_cb_result = NULL;
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2638 if (ret != 0) {
2639 return MBEDTLS_ERR_X509_FATAL_ERROR;
2640 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002641
2642 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002644#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2645 {
2646 ((void) f_ca_cb);
2647 ((void) p_ca_cb);
2648 cur_trust_ca = trust_ca;
2649 }
2650
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002651 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2653 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002654 ver_chain->len - 1, self_cnt, rs_ctx,
2655 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002656
2657#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002659 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002660 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002661 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002662 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002665 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002666#else
2667 (void) ret;
2668#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002669
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002670 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002672 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002674 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002675
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002676 /* Count intermediate self-issued (not necessarily self-signed) certs.
2677 * These can occur with some strategies for key rollover, see [SIRO],
2678 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (ver_chain->len != 1 &&
2680 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002681 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002682 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002683
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002684 /* path_cnt is 0 for the first intermediate CA,
2685 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (!parent_is_trusted &&
2687 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002688 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002690 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002691
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002692 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002694 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002696
2697 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002699 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002703 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002704 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002705#else
2706 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002707#endif
2708
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002709 /* prepare for next iteration */
2710 child = parent;
2711 parent = NULL;
2712 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002713 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002714 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002715}
2716
Glenn Straussc26bd762022-10-23 19:48:18 -04002717#ifdef _WIN32
2718#ifdef _MSC_VER
2719#pragma comment(lib, "ws2_32.lib")
2720#include <winsock2.h>
2721#include <ws2tcpip.h>
2722#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2723#include <winsock2.h>
2724#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002725#else
2726/* inet_pton() is not supported, fallback to software version */
2727#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002728#endif
2729#elif defined(__sun)
2730/* Solaris requires -lsocket -lnsl for inet_pton() */
2731#elif defined(__has_include)
2732#if __has_include(<sys/socket.h>)
2733#include <sys/socket.h>
2734#endif
2735#if __has_include(<arpa/inet.h>)
2736#include <arpa/inet.h>
2737#endif
2738#endif
2739
2740/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2741 * the platform inet_pton() or a local implementation (below). The local
2742 * implementation may be used even in cases where the platform provides
2743 * inet_pton(), e.g. when there are different includes required and/or the
2744 * platform implementation requires dependencies on additional libraries.
2745 * Specifically, Windows requires custom includes and additional link
2746 * dependencies, and Solaris requires additional link dependencies.
2747 * Also, as a coarse heuristic, use the local implementation if the compiler
2748 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002749 * provided by headers included (or not) via __has_include() above.
2750 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2751 * despite having a platform that has inet_pton. */
2752#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2753/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002754static int x509_inet_pton_ipv4(const char *src, void *dst);
2755
2756#define li_cton(c, n) \
2757 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2758
2759static int x509_inet_pton_ipv6(const char *src, void *dst)
2760{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002761 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002762 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002763 uint16_t addr[8];
2764 do {
2765 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002766 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002767 for (uint8_t digit; num_digits < 4; num_digits++) {
2768 if (li_cton(*p, digit) == 0) {
2769 break;
2770 }
2771 group = (group << 4) | digit;
2772 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002773 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002774 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002775 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2776 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002777 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002778 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002779 } else if (*p == '.') {
2780 /* Don't accept IPv4 too early or late */
2781 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2782 nonzero_groups >= 7) {
2783 break;
2784 }
2785
2786 /* Walk back to prior ':', then parse as IPv4-mapped */
2787 int steps = 4;
2788 do {
2789 p--;
2790 steps--;
2791 } while (*p != ':' && steps > 0);
2792
2793 if (*p != ':') {
2794 break;
2795 }
2796 p++;
2797 nonzero_groups--;
2798 if (x509_inet_pton_ipv4((const char *) p,
2799 addr + nonzero_groups) != 0) {
2800 break;
2801 }
2802
Andrzej Kurek0d578962023-04-13 09:09:56 -04002803 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002804 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002805 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002806 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002807 return -1;
2808 }
2809 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002810 /* Don't accept a second zero group or an invalid delimiter */
2811 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002812 return -1;
2813 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002814 zero_group_start = nonzero_groups;
2815
2816 /* Accept a zero group at start, but it has to be a double colon */
2817 if (zero_group_start == 0 && *++p != ':') {
2818 return -1;
2819 }
2820
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002821 if (p[1] == '\0') {
2822 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002823 break;
2824 }
2825 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002826 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002827 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002828
2829 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002830 return -1;
2831 }
2832
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002833 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002834 if (nonzero_groups > 6) {
2835 return -1;
2836 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002837 int zero_groups = 8 - nonzero_groups;
2838 int groups_after_zero = nonzero_groups - zero_group_start;
2839
2840 /* Move the non-zero part to after the zeroes */
2841 if (groups_after_zero) {
2842 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002843 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002844 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002845 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002846 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002847 } else {
2848 if (nonzero_groups != 8) {
2849 return -1;
2850 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002851 }
2852 memcpy(dst, addr, sizeof(addr));
2853 return 0;
2854}
2855
2856static int x509_inet_pton_ipv4(const char *src, void *dst)
2857{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002858 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002859 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002860 uint8_t digit, num_digits = 0;
2861 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002862 uint16_t octet;
2863
Glenn Strauss416c2952022-10-24 23:00:02 -04002864 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002865 octet = num_digits = 0;
2866 do {
2867 digit = *p - '0';
2868 if (digit > 9) {
2869 break;
2870 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002871
2872 /* Don't allow leading zeroes. These might mean octal format,
2873 * which this implementation does not support. */
2874 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002875 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002876 }
2877
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002878 octet = octet * 10 + digit;
2879 num_digits++;
2880 p++;
2881 } while (num_digits < 3);
2882
2883 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002884 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002885 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002886 *res++ = (uint8_t) octet;
2887 num_octets++;
2888 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002889 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002890}
Glenn Straussc26bd762022-10-23 19:48:18 -04002891
2892#else
2893
2894static int x509_inet_pton_ipv6(const char *src, void *dst)
2895{
2896 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2897}
2898
2899static int x509_inet_pton_ipv4(const char *src, void *dst)
2900{
2901 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2902}
2903
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002904#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002905
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002906size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002907{
2908 return strchr(cn, ':') == NULL
2909 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2910 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2911}
2912
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002913/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002914 * Check for CN match
2915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002916static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2917 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002918{
2919 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 if (name->len == cn_len &&
2921 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2922 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002923 }
2924
2925 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 if (x509_check_wildcard(cn, name) == 0) {
2927 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002928 }
2929
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002931}
2932
Glenn Straussc26bd762022-10-23 19:48:18 -04002933static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2934 const char *cn, size_t cn_len)
2935{
2936 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002937 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002938 if (cn_len == 0) {
2939 return -1;
2940 }
2941
2942 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2943 const unsigned char san_type = (unsigned char) cur->buf.tag &
2944 MBEDTLS_ASN1_TAG_VALUE_MASK;
2945 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2946 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2947 return 0;
2948 }
2949 }
2950
2951 return -1;
2952}
2953
Andrzej Kurek199eab92023-05-10 09:57:19 -04002954static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2955 const char *cn, size_t cn_len)
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_UNIFORM_RESOURCE_IDENTIFIER &&
2961 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2962 return 0;
2963 }
2964 }
2965
2966 return -1;
2967}
2968
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002969/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002970 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2971 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002972static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002974{
Glenn Straussc26bd762022-10-23 19:48:18 -04002975 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002976 int san_uri = 0;
2977 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002978 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2979 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002980 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002981 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2982 return 0;
2983 }
2984 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002985 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002986 san_ip = 1;
2987 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002988 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2989 san_uri = 1;
2990 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002991 /* (We may handle other types here later.) */
2992 default: /* Unrecognized type */
2993 break;
2994 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002996 if (san_ip) {
2997 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2998 return 0;
2999 }
3000 }
3001 if (san_uri) {
3002 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
3003 return 0;
3004 }
3005 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003006
Andrzej Kurek199eab92023-05-10 09:57:19 -04003007 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003008}
3009
3010/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003011 * Verify the requested CN - only call this if cn is not NULL!
3012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003013static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3014 const char *cn,
3015 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003016{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003017 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003021 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
3022 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 }
3024 } else {
3025 for (name = &crt->subject; name != NULL; name = name->next) {
3026 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3027 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003028 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 }
3030 }
3031
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003032 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003033
3034 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003035}
3036
3037/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003038 * Merge the flags for all certs in the chain, after calling callback
3039 */
3040static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 uint32_t *flags,
3042 const mbedtls_x509_crt_verify_chain *ver_chain,
3043 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3044 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003045{
Janos Follath865b3eb2019-12-16 11:46:15 +00003046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003047 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003048 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003049 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003052 cur = &ver_chain->items[i-1];
3053 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003054
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 if (NULL != f_vrfy) {
3056 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3057 return ret;
3058 }
3059 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003060
3061 *flags |= cur_flags;
3062 }
3063
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003065}
3066
3067/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003068 * Verify the certificate validity, with profile, restartable version
3069 *
3070 * This function:
3071 * - checks the requested CN (if any)
3072 * - checks the type and size of the EE cert's key,
3073 * as that isn't done as part of chain building/verification currently
3074 * - builds and verifies the chain
3075 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003076 *
3077 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3078 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3079 * verification routine to search for trusted signers, and CRLs will
3080 * be disabled. Otherwise, `trust_ca` will be used as the static list
3081 * of trusted signers, and `ca_crl` will be use as the static list
3082 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003084static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3085 mbedtls_x509_crt *trust_ca,
3086 mbedtls_x509_crl *ca_crl,
3087 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3088 void *p_ca_cb,
3089 const mbedtls_x509_crt_profile *profile,
3090 const char *cn, uint32_t *flags,
3091 int (*f_vrfy)(void *,
3092 mbedtls_x509_crt *,
3093 int,
3094 uint32_t *),
3095 void *p_vrfy,
3096 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003097{
Janos Follath865b3eb2019-12-16 11:46:15 +00003098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003099 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003100 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003101 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003102
3103 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003104 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003106
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003108 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3109 goto exit;
3110 }
3111
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003112 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 if (cn != NULL) {
3114 x509_crt_verify_name(crt, cn, &ee_flags);
3115 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003116
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003117 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003119
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003121 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003125 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003127
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003128 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3130 f_ca_cb, p_ca_cb, profile,
3131 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003136
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003137 /* Merge end-entity flags */
3138 ver_chain.items[0].flags |= ee_flags;
3139
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003140 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003142
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003143exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003144
3145#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3147 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003148 ver_chain.trust_ca_cb_result = NULL;
3149#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3150
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003151#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3153 mbedtls_x509_crt_restart_free(rs_ctx);
3154 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003155#endif
3156
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003157 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3158 * the SSL module for authmode optional, but non-zero return from the
3159 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003161 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003162 }
3163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 if (ret != 0) {
3165 *flags = (uint32_t) -1;
3166 return ret;
3167 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 if (*flags != 0) {
3170 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3171 }
3172
3173 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003174}
3175
Hanno Becker3116fb32019-03-28 13:34:42 +00003176
3177/*
3178 * Verify the certificate validity (default profile, not restartable)
3179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003180int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3181 mbedtls_x509_crt *trust_ca,
3182 mbedtls_x509_crl *ca_crl,
3183 const char *cn, uint32_t *flags,
3184 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3185 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003186{
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3188 NULL, NULL,
3189 &mbedtls_x509_crt_profile_default,
3190 cn, flags,
3191 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003192}
3193
3194/*
3195 * Verify the certificate validity (user-chosen profile, not restartable)
3196 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003197int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3198 mbedtls_x509_crt *trust_ca,
3199 mbedtls_x509_crl *ca_crl,
3200 const mbedtls_x509_crt_profile *profile,
3201 const char *cn, uint32_t *flags,
3202 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3203 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003204{
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3206 NULL, NULL,
3207 profile, cn, flags,
3208 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003209}
3210
3211#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3212/*
3213 * Verify the certificate validity (user-chosen profile, CA callback,
3214 * not restartable).
3215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003216int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3217 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3218 void *p_ca_cb,
3219 const mbedtls_x509_crt_profile *profile,
3220 const char *cn, uint32_t *flags,
3221 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3222 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003223{
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3225 f_ca_cb, p_ca_cb,
3226 profile, cn, flags,
3227 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003228}
3229#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3230
Gilles Peskine449bd832023-01-11 14:50:10 +01003231int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3232 mbedtls_x509_crt *trust_ca,
3233 mbedtls_x509_crl *ca_crl,
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,
3238 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003239{
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3241 NULL, NULL,
3242 profile, cn, flags,
3243 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003244}
3245
3246
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003247/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003248 * Initialize a certificate chain
3249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003250void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003251{
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003253}
3254
3255/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003256 * Unallocate all certificate data
3257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003258void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 mbedtls_x509_crt *cert_cur = crt;
3261 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003262
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 while (cert_cur != NULL) {
3264 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003268#endif
3269
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3271 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3272 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3273 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3274 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003275 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003278 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003279 }
3280
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003281 cert_prv = cert_cur;
3282 cert_cur = cert_cur->next;
3283
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3285 if (cert_prv != crt) {
3286 mbedtls_free(cert_prv);
3287 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003288 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003289}
3290
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003291#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3292/*
3293 * Initialize a restart context
3294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003295void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003296{
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003298
3299 ctx->parent = NULL;
3300 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003301 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003302
3303 ctx->parent_is_trusted = -1;
3304
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003305 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003306 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003308}
3309
3310/*
3311 * Free the components of a restart context
3312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003313void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003314{
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003316 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003318
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 mbedtls_pk_restart_free(&ctx->pk);
3320 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003321}
3322#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324#endif /* MBEDTLS_X509_CRT_PARSE_C */