blob: 30e9668b24bea2e17f01714d6f18b8c09b6df893 [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>
64#else
65#include <time.h>
66#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100067#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020071#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#include <sys/types.h>
73#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020074#if defined(__MBED__)
75#include <platform/mbed_retarget.h>
76#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020078#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060079#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000080#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081#endif
82
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020083/*
84 * Item in a verification chain: cert and flags for it
85 */
86typedef struct {
87 mbedtls_x509_crt *crt;
88 uint32_t flags;
89} x509_crt_verify_chain_item;
90
91/*
92 * Max size of verification chain: end-entity + intermediates + trusted root
93 */
Gilles Peskine449bd832023-01-11 14:50:10 +010094#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020095
Gilles Peskineffb92da2021-06-02 00:03:26 +020096/* Default profile. Do not remove items unless there are serious security
97 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020098const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
99{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200100 /* Hashes from SHA-256 and above. Note that this selection
101 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
103 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
104 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 0xFFFFFFF, /* Any PK alg */
Valerio Setti8c3404f2023-06-26 15:49:48 +0200106#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200107 /* Curves at or above 128-bit security level. Note that this selection
108 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
110 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
111 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
112 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
113 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
114 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200115 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200116#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200117 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200118#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200119 2048,
120};
121
Gilles Peskineffb92da2021-06-02 00:03:26 +0200122/* Next-generation profile. Currently identical to the default, but may
123 * be tightened at any time. */
124const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200125{
126 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
128 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
129 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200130 0xFFFFFFF, /* Any PK alg */
131#if defined(MBEDTLS_ECP_C)
132 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
134 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
135 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
136 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
138 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
139 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200140#else
141 0,
142#endif
143 2048,
144};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200145
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200146/*
147 * NSA Suite B Profile
148 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200149const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
150{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200151 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
153 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200154 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
156 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200157#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200158 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
160 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200161#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200162 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200163#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200164 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200165};
166
167/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200168 * Empty / all-forbidden profile
169 */
170const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
171{
172 0,
173 0,
174 0,
175 (uint32_t) -1,
176};
177
178/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200179 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200180 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200181 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100182static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
183 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184{
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (md_alg == MBEDTLS_MD_NONE) {
186 return -1;
187 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
190 return 0;
191 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194}
195
196/*
197 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200198 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
201 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202{
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (pk_alg == MBEDTLS_PK_NONE) {
204 return -1;
205 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
208 return 0;
209 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200212}
213
214/*
215 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200216 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
219 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220{
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200222
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200223#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
225 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
226 return 0;
227 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200230 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200231#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200232
Valerio Setti8c3404f2023-06-26 15:49:48 +0200233#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200235 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Setti97207782023-05-18 18:59:06 +0200237 const mbedtls_ecp_group_id gid = mbedtls_pk_get_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (gid == MBEDTLS_ECP_DP_NONE) {
240 return -1;
241 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
244 return 0;
245 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200248 }
Valerio Setti8c3404f2023-06-26 15:49:48 +0200249#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200252}
253
254/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000255 * Like memcmp, but case-insensitive and always returns -1 if different
256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000258{
259 size_t i;
260 unsigned char diff;
261 const unsigned char *n1 = s1, *n2 = s2;
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000264 diff = n1[i] ^ n2[i];
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000267 continue;
268 }
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (diff == 32 &&
271 ((n1[i] >= 'a' && n1[i] <= 'z') ||
272 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
273 continue;
274 }
275
276 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000277 }
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000280}
281
282/*
283 * Return 0 if name matches wildcard, -1 otherwise
284 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000286{
287 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000289
290 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
292 return -1;
293 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 for (i = 0; i < cn_len; ++i) {
296 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000297 cn_idx = i;
298 break;
299 }
300 }
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (cn_idx == 0) {
303 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000304 }
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (cn_len - cn_idx == name->len - 1 &&
307 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
308 return 0;
309 }
310
311 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000312}
313
314/*
315 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
316 * variations (but not all).
317 *
318 * Return 0 if equal, -1 otherwise.
319 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000321{
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000323 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 memcmp(a->p, b->p, b->len) == 0) {
325 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000326 }
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
329 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000330 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 x509_memcasecmp(a->p, b->p, b->len) == 0) {
332 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000333 }
334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000336}
337
338/*
339 * Compare two X.509 Names (aka rdnSequence).
340 *
341 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
342 * we sometimes return unequal when the full algorithm would return equal,
343 * but never the other way. (In particular, we don't do Unicode normalisation
344 * or space folding.)
345 *
346 * Return 0 if equal, -1 otherwise.
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000349{
350 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 while (a != NULL || b != NULL) {
352 if (a == NULL || b == NULL) {
353 return -1;
354 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000355
356 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000358 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
360 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000361 }
362
363 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (x509_string_cmp(&a->val, &b->val) != 0) {
365 return -1;
366 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000367
368 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (a->next_merged != b->next_merged) {
370 return -1;
371 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000372
373 a = a->next;
374 b = b->next;
375 }
376
377 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000379}
380
381/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200382 * Reset (init or clear) a verify_chain
383 */
384static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200386{
387 size_t i;
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200390 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000391 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200392 }
393
394 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000395
396#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
397 ver_chain->trust_ca_cb_result = NULL;
398#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200399}
400
401/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404static int x509_get_version(unsigned char **p,
405 const unsigned char *end,
406 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407{
Janos Follath865b3eb2019-12-16 11:46:15 +0000408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409 size_t len;
410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
412 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
413 0)) != 0) {
414 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 }
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420 }
421
422 end = *p + len;
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
425 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
426 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (*p != end) {
429 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
430 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
431 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434}
435
436/*
437 * Validity ::= SEQUENCE {
438 * notBefore Time,
439 * notAfter Time }
440 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441static int x509_get_dates(unsigned char **p,
442 const unsigned char *end,
443 mbedtls_x509_time *from,
444 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445{
Janos Follath865b3eb2019-12-16 11:46:15 +0000446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447 size_t len;
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
450 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
451 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
452 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
454 end = *p + len;
455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
457 return ret;
458 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
461 return ret;
462 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (*p != end) {
465 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
466 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
467 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470}
471
472/*
473 * X.509 v2/v3 unique identifier (not parsed)
474 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475static int x509_get_uid(unsigned char **p,
476 const unsigned char *end,
477 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478{
Janos Follath865b3eb2019-12-16 11:46:15 +0000479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (*p == end) {
482 return 0;
483 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
485 uid->tag = **p;
486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
488 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
489 n)) != 0) {
490 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
491 return 0;
492 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 }
496
497 uid->p = *p;
498 *p += uid->len;
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501}
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503static int x509_get_basic_constraints(unsigned char **p,
504 const unsigned char *end,
505 int *ca_istrue,
506 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507{
Janos Follath865b3eb2019-12-16 11:46:15 +0000508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509 size_t len;
510
511 /*
512 * BasicConstraints ::= SEQUENCE {
513 * cA BOOLEAN DEFAULT FALSE,
514 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
515 */
516 *ca_istrue = 0; /* DEFAULT FALSE */
517 *max_pathlen = 0; /* endless */
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
520 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
521 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 }
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (*p == end) {
525 return 0;
526 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
529 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
530 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
531 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (ret != 0) {
534 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
535 }
536
537 if (*ca_istrue != 0) {
538 *ca_istrue = 1;
539 }
540 }
541
542 if (*p == end) {
543 return 0;
544 }
545
546 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
547 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
548 }
549
550 if (*p != end) {
551 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
552 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
553 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
Andrzej Kurek16050742020-04-14 09:49:52 -0400555 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
556 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (*max_pathlen == INT_MAX) {
558 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
559 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
560 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400561
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562 (*max_pathlen)++;
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565}
566
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567/*
568 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
569 *
570 * KeyPurposeId ::= OBJECT IDENTIFIER
571 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572static int x509_get_ext_key_usage(unsigned char **p,
573 const unsigned char *end,
574 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575{
Janos Follath865b3eb2019-12-16 11:46:15 +0000576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
579 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
580 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
582 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (ext_key_usage->buf.p == NULL) {
584 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
585 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
586 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589}
590
591/*
toth92ga41954d2021-02-12 16:11:17 +0100592 * SubjectKeyIdentifier ::= KeyIdentifier
593 *
594 * KeyIdentifier ::= OCTET STRING
595 */
596static int x509_get_subject_key_id(unsigned char **p,
597 const unsigned char *end,
598 mbedtls_x509_buf *subject_key_id)
599{
600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
601 size_t len = 0u;
602
603 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
604 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100605 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100606 }
607
Przemek Stekiel61aed062023-05-08 11:14:36 +0200608 subject_key_id->len = len;
609 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
610 subject_key_id->p = *p;
611 *p += len;
612
toth92gd96027a2021-04-27 15:41:25 +0200613 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100614 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
615 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200616 }
617
toth92ga41954d2021-02-12 16:11:17 +0100618 return 0;
619}
620
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100621/*
toth92g8d435a02021-05-10 15:16:33 +0200622 * AuthorityKeyIdentifier ::= SEQUENCE {
623 * keyIdentifier [0] KeyIdentifier OPTIONAL,
624 * authorityCertIssuer [1] GeneralNames OPTIONAL,
625 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
626 *
627 * KeyIdentifier ::= OCTET STRING
628 */
629static int x509_get_authority_key_id(unsigned char **p,
630 unsigned char *end,
631 mbedtls_x509_authority *authority_key_id)
632{
633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
634 size_t len = 0u;
635
636 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100637 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100638 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200639 }
640
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100641 if (*p + len != end) {
642 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
643 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
644 }
645
Przemek Stekieled9fb782023-05-03 16:27:25 +0200646 ret = mbedtls_asn1_get_tag(p, end, &len,
647 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
648
649 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200650 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200651 authority_key_id->keyIdentifier.len = len;
652 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200653 /* Setting tag of the keyIdentfier intentionally to 0x04.
654 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200655 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200656 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
657
658 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200659 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
660 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200661 }
662
663 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200664 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200665 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
666 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100667 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200668 /* authorityCertIssuer and authorityCertSerialNumber MUST both
669 be present or both be absent. At this point we expect to have both. */
670 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200671 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200672 /* "end" also includes the CertSerialNumber field so "len" shall be used */
673 ret = mbedtls_x509_get_subject_alt_name_ext(p,
674 (*p+len),
675 &authority_key_id->authorityCertIssuer);
676 if (ret != 0) {
677 return ret;
678 }
679
680 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
681 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
682 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
683 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
684 }
685 authority_key_id->authorityCertSerialNumber.len = len;
686 authority_key_id->authorityCertSerialNumber.p = *p;
687 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
688 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200689 }
690
691 if (*p != end) {
692 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
693 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
694 }
695
696 return 0;
697}
698
699/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200700 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
701 *
702 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
703 *
704 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
705 *
706 * PolicyInformation ::= SEQUENCE {
707 * policyIdentifier CertPolicyId,
708 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
709 * PolicyQualifierInfo OPTIONAL }
710 *
711 * CertPolicyId ::= OBJECT IDENTIFIER
712 *
713 * PolicyQualifierInfo ::= SEQUENCE {
714 * policyQualifierId PolicyQualifierId,
715 * qualifier ANY DEFINED BY policyQualifierId }
716 *
717 * -- policyQualifierIds for Internet policy qualifiers
718 *
719 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
720 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
721 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
722 *
723 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
724 *
725 * Qualifier ::= CHOICE {
726 * cPSuri CPSuri,
727 * userNotice UserNotice }
728 *
729 * CPSuri ::= IA5String
730 *
731 * UserNotice ::= SEQUENCE {
732 * noticeRef NoticeReference OPTIONAL,
733 * explicitText DisplayText OPTIONAL }
734 *
735 * NoticeReference ::= SEQUENCE {
736 * organization DisplayText,
737 * noticeNumbers SEQUENCE OF INTEGER }
738 *
739 * DisplayText ::= CHOICE {
740 * ia5String IA5String (SIZE (1..200)),
741 * visibleString VisibleString (SIZE (1..200)),
742 * bmpString BMPString (SIZE (1..200)),
743 * utf8String UTF8String (SIZE (1..200)) }
744 *
745 * NOTE: we only parse and use anyPolicy without qualifiers at this point
746 * as defined in RFC 5280.
747 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748static int x509_get_certificate_policies(unsigned char **p,
749 const unsigned char *end,
750 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200751{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300752 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200753 size_t len;
754 mbedtls_asn1_buf *buf;
755 mbedtls_asn1_sequence *cur = certificate_policies;
756
757 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 ret = mbedtls_asn1_get_tag(p, end, &len,
759 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
760 if (ret != 0) {
761 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
762 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if (*p + len != end) {
765 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
766 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
767 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200768
769 /*
770 * Cannot be an empty sequence.
771 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (len == 0) {
773 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
774 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
775 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200778 mbedtls_x509_buf policy_oid;
779 const unsigned char *policy_end;
780
781 /*
782 * Get the policy sequence
783 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
785 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
786 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
787 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200788
789 policy_end = *p + len;
790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
792 MBEDTLS_ASN1_OID)) != 0) {
793 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
794 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200795
796 policy_oid.tag = MBEDTLS_ASN1_OID;
797 policy_oid.len = len;
798 policy_oid.p = *p;
799
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300800 /*
801 * Only AnyPolicy is currently supported when enforcing policy.
802 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300804 /*
805 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200806 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300807 */
808 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
809 }
810
Ron Eldor74d9acc2019-03-21 14:00:03 +0200811 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (cur->buf.p != NULL) {
813 if (cur->next != NULL) {
814 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
815 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (cur->next == NULL) {
820 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
821 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
822 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200823
824 cur = cur->next;
825 }
826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200828 buf->tag = policy_oid.tag;
829 buf->p = policy_oid.p;
830 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300831
832 *p += len;
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 /*
835 * If there is an optional qualifier, then *p < policy_end
836 * Check the Qualifier len to verify it doesn't exceed policy_end.
837 */
838 if (*p < policy_end) {
839 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
840 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
841 0) {
842 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
843 }
Ron Eldor08063792019-05-13 16:38:39 +0300844 /*
845 * Skip the optional policy qualifiers.
846 */
847 *p += len;
848 }
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (*p != policy_end) {
851 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
852 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
853 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200854 }
855
856 /* Set final sequence entry's next pointer to NULL */
857 cur->next = NULL;
858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (*p != end) {
860 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
861 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
862 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200865}
866
867/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 * X.509 v3 extensions
869 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871static int x509_get_crt_ext(unsigned char **p,
872 const unsigned char *end,
873 mbedtls_x509_crt *crt,
874 mbedtls_x509_crt_ext_cb_t cb,
875 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876{
Janos Follath865b3eb2019-12-16 11:46:15 +0000877 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200879 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if (*p == end) {
882 return 0;
883 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
886 return ret;
887 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888
Hanno Becker12f62fb2019-02-12 17:22:36 +0000889 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891 /*
892 * Extension ::= SEQUENCE {
893 * extnID OBJECT IDENTIFIER,
894 * critical BOOLEAN DEFAULT FALSE,
895 * extnValue OCTET STRING }
896 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 int is_critical = 0; /* DEFAULT FALSE */
899 int ext_type = 0;
900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
902 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
903 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
904 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905
906 end_ext_data = *p + len;
907
908 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
910 MBEDTLS_ASN1_OID)) != 0) {
911 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
912 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913
k-stachowiak463928a2018-07-24 12:50:59 +0200914 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 extn_oid.p = *p;
916 *p += extn_oid.len;
917
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
920 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
921 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
922 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923
924 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
926 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
927 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
928 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200930 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931 end_ext_octet = *p + len;
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (end_ext_octet != end_ext_data) {
934 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
935 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
936 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937
938 /*
939 * Detect supported extensions
940 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200944 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (cb != NULL) {
946 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
947 if (ret != 0 && is_critical) {
948 return ret;
949 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200950 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200951 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200952 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200953
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954 /* No parser found, skip extension */
955 *p = end_ext_octet;
956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
960 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 continue;
963 }
964
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100965 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if ((crt->ext_types & ext_type) != 0) {
967 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
968 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100969
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 crt->ext_types |= ext_type;
971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 switch (ext_type) {
973 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
974 /* Parse basic constraints */
975 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
976 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
977 return ret;
978 }
979 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 case MBEDTLS_X509_EXT_KEY_USAGE:
982 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100983 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
984 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return ret;
986 }
987 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
990 /* Parse extended key usage */
991 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
992 &crt->ext_key_usage)) != 0) {
993 return ret;
994 }
995 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
toth92ga41954d2021-02-12 16:11:17 +0100997 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
998 /* Parse subject key identifier */
999 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1000 &crt->subject_key_id)) != 0) {
1001 return ret;
1002 }
1003 break;
toth92g8d435a02021-05-10 15:16:33 +02001004
toth92ga41954d2021-02-12 16:11:17 +01001005 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1006 /* Parse authority key identifier */
1007 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1008 &crt->authority_key_id)) != 0) {
1009 return ret;
1010 }
1011 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001013 /* Parse subject alt name
1014 * SubjectAltName ::= GeneralNames
1015 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001016 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1017 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 return ret;
1019 }
1020 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1023 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001024 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1025 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 return ret;
1027 }
1028 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1031 /* Parse certificate policies type */
1032 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1033 &crt->certificate_policies)) != 0) {
1034 /* Give the callback (if any) a chance to handle the extension
1035 * if it contains unsupported policies */
1036 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1037 cb(p_ctx, crt, &extn_oid, is_critical,
1038 start_ext_octet, end_ext_octet) == 0) {
1039 break;
1040 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (is_critical) {
1043 return ret;
1044 } else
1045 /*
1046 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1047 * cannot interpret or enforce the policy. However, it is up to
1048 * the user to choose how to enforce the policies,
1049 * unless the extension is critical.
1050 */
1051 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1052 return ret;
1053 }
1054 }
1055 break;
1056
1057 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001058 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 * If this is a non-critical extension, which the oid layer
1060 * supports, but there isn't an x509 parser for it,
1061 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001062 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (is_critical) {
1064 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1065 } else {
1066 *p = end_ext_octet;
1067 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 }
1069 }
1070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (*p != end) {
1072 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1073 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1074 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077}
1078
1079/*
1080 * Parse and fill a single X.509 certificate in DER format
1081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1083 const unsigned char *buf,
1084 size_t buflen,
1085 int make_copy,
1086 mbedtls_x509_crt_ext_cb_t cb,
1087 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088{
Janos Follath865b3eb2019-12-16 11:46:15 +00001089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090 size_t len;
1091 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1095 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1096 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097
1098 /*
1099 * Check for valid input
1100 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if (crt == NULL || buf == NULL) {
1102 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1103 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001105 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001107 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108 end = p + len;
1109
1110 /*
1111 * Certificate ::= SEQUENCE {
1112 * tbsCertificate TBSCertificate,
1113 * signatureAlgorithm AlgorithmIdentifier,
1114 * signatureValue BIT STRING }
1115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1117 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1118 mbedtls_x509_crt_free(crt);
1119 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120 }
1121
Janos Follathcc0e49d2016-02-17 14:34:12 +00001122 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001123 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001125 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1127 if (crt->raw.p == NULL) {
1128 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1129 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001132 crt->own_buffer = 1;
1133
1134 p += crt->raw.len - len;
1135 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 } else {
1137 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001138 crt->own_buffer = 0;
1139 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001140
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 /*
1142 * TBSCertificate ::= SEQUENCE {
1143 */
1144 crt->tbs.p = p;
1145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1147 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1148 mbedtls_x509_crt_free(crt);
1149 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 }
1151
1152 end = p + len;
1153 crt->tbs.len = end - crt->tbs.p;
1154
1155 /*
1156 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1157 *
1158 * CertificateSerialNumber ::= INTEGER
1159 *
1160 * signature AlgorithmIdentifier
1161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1163 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1164 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1165 &sig_params1)) != 0) {
1166 mbedtls_x509_crt_free(crt);
1167 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 }
1169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (crt->version < 0 || crt->version > 2) {
1171 mbedtls_x509_crt_free(crt);
1172 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 }
1174
Andres AG7ca4a032017-03-09 16:16:11 +00001175 crt->version++;
1176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1178 &crt->sig_md, &crt->sig_pk,
1179 &crt->sig_opts)) != 0) {
1180 mbedtls_x509_crt_free(crt);
1181 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 }
1183
1184 /*
1185 * issuer Name
1186 */
1187 crt->issuer_raw.p = p;
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1190 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1191 mbedtls_x509_crt_free(crt);
1192 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193 }
1194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1196 mbedtls_x509_crt_free(crt);
1197 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001198 }
1199
1200 crt->issuer_raw.len = p - crt->issuer_raw.p;
1201
1202 /*
1203 * Validity ::= SEQUENCE {
1204 * notBefore Time,
1205 * notAfter Time }
1206 *
1207 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1209 &crt->valid_to)) != 0) {
1210 mbedtls_x509_crt_free(crt);
1211 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 }
1213
1214 /*
1215 * subject Name
1216 */
1217 crt->subject_raw.p = p;
1218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1220 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1221 mbedtls_x509_crt_free(crt);
1222 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001223 }
1224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1226 mbedtls_x509_crt_free(crt);
1227 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228 }
1229
1230 crt->subject_raw.len = p - crt->subject_raw.p;
1231
1232 /*
1233 * SubjectPublicKeyInfo
1234 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001235 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1237 mbedtls_x509_crt_free(crt);
1238 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001240 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241
1242 /*
1243 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1244 * -- If present, version shall be v2 or v3
1245 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1246 * -- If present, version shall be v2 or v3
1247 * extensions [3] EXPLICIT Extensions OPTIONAL
1248 * -- If present, version shall be v3
1249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (crt->version == 2 || crt->version == 3) {
1251 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1252 if (ret != 0) {
1253 mbedtls_x509_crt_free(crt);
1254 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 }
1256 }
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (crt->version == 2 || crt->version == 3) {
1259 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1260 if (ret != 0) {
1261 mbedtls_x509_crt_free(crt);
1262 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263 }
1264 }
1265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (crt->version == 3) {
1267 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1268 if (ret != 0) {
1269 mbedtls_x509_crt_free(crt);
1270 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 }
1272 }
1273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (p != end) {
1275 mbedtls_x509_crt_free(crt);
1276 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1277 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 }
1279
1280 end = crt_end;
1281
1282 /*
1283 * }
1284 * -- end of TBSCertificate
1285 *
1286 * signatureAlgorithm AlgorithmIdentifier,
1287 * signatureValue BIT STRING
1288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1290 mbedtls_x509_crt_free(crt);
1291 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 }
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (crt->sig_oid.len != sig_oid2.len ||
1295 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001296 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001297 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 (sig_params1.len != 0 &&
1299 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1300 mbedtls_x509_crt_free(crt);
1301 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302 }
1303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1305 mbedtls_x509_crt_free(crt);
1306 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 }
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (p != end) {
1310 mbedtls_x509_crt_free(crt);
1311 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1312 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313 }
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316}
1317
1318/*
1319 * Parse one X.509 certificate in DER format from a buffer and add them to a
1320 * chained list
1321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1323 const unsigned char *buf,
1324 size_t buflen,
1325 int make_copy,
1326 mbedtls_x509_crt_ext_cb_t cb,
1327 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328{
Janos Follath865b3eb2019-12-16 11:46:15 +00001329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331
1332 /*
1333 * Check for valid input
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (crt == NULL || buf == NULL) {
1336 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1337 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340 prev = crt;
1341 crt = crt->next;
1342 }
1343
1344 /*
1345 * Add new certificate on the end of the chain if needed.
1346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (crt->version != 0 && crt->next == NULL) {
1348 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if (crt->next == NULL) {
1351 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1352 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353
1354 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357 }
1358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1360 if (ret != 0) {
1361 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if (crt != chain) {
1366 mbedtls_free(crt);
1367 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 }
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373}
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1376 const unsigned char *buf,
1377 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001378{
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001380}
1381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1383 const unsigned char *buf,
1384 size_t buflen,
1385 int make_copy,
1386 mbedtls_x509_crt_ext_cb_t cb,
1387 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001388{
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001390}
1391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1393 const unsigned char *buf,
1394 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001395{
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001397}
1398
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001400 * Parse one or more PEM certificates from a buffer and add them to the chained
1401 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1404 const unsigned char *buf,
1405 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406{
Janos Follath98e28a72016-05-31 14:03:54 +01001407#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001408 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001410#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411
1412 /*
1413 * Check for valid input
1414 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if (chain == NULL || buf == NULL) {
1416 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1417 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001418
1419 /*
1420 * Determine buffer content. Buffer contains either one DER certificate or
1421 * one or more PEM certificates.
1422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1425 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001427 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1430 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1431 }
Janos Follath98e28a72016-05-31 14:03:54 +01001432#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001434#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001441 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001445
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001446 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 ret = mbedtls_pem_read_buffer(&pem,
1448 "-----BEGIN CERTIFICATE-----",
1449 "-----END CERTIFICATE-----",
1450 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 /*
1454 * Was PEM encoded
1455 */
1456 buflen -= use_len;
1457 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1459 return ret;
1460 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1461 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462
1463 /*
1464 * PEM header and footer were found
1465 */
1466 buflen -= use_len;
1467 buf += use_len;
1468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472
Paul Bakker5a5fa922014-09-26 14:53:04 +02001473 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001484 /*
1485 * Quit parsing on a memory error
1486 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1488 return ret;
1489 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494
1495 total_failed++;
1496 continue;
1497 }
1498
1499 success = 1;
1500 }
1501 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (success) {
1504 return total_failed;
1505 } else if (first_error) {
1506 return first_error;
1507 } else {
1508 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1509 }
Janos Follath98e28a72016-05-31 14:03:54 +01001510#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001511}
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514/*
1515 * Load one or more certificates and add them to the chained list
1516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518{
Janos Follath865b3eb2019-12-16 11:46:15 +00001519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520 size_t n;
1521 unsigned char *buf;
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1524 return ret;
1525 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 mbedtls_platform_zeroize(buf, n);
1530 mbedtls_free(buf);
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);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545
Paul Bakker9af723c2014-05-01 13:03:14 +02001546 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547 HANDLE hFind;
1548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 if (len > MAX_PATH - 3) {
1550 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1551 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 memset(szDir, 0, sizeof(szDir));
1554 memset(filename, 0, MAX_PATH);
1555 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001556 filename[len++] = '\\';
1557 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558 filename[len++] = '*';
1559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1561 MAX_PATH - 3);
1562 if (w_ret == 0) {
1563 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1564 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 hFind = FindFirstFileW(szDir, &file_data);
1567 if (hFind == INVALID_HANDLE_VALUE) {
1568 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1569 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570
1571 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 do {
1573 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001580 -1,
1581 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 NULL, NULL);
1583 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001584 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1585 goto cleanup;
1586 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1589 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 }
1594 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599
Ron Eldor36d90422017-01-09 15:09:16 +02001600cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 FindClose(hFind);
Steve Lhomme369d7c72023-06-16 14:16:03 +02001602#else /* !_WIN32_WINNT_XP */
1603#error mbedtls_x509_crt_parse_path not available before Windows XP
1604#endif /* !_WIN32_WINNT_XP */
Paul Bakkerbe089b02013-10-14 15:51:50 +02001605#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001606 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001607 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001608 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001609 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001610 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 if (dir == NULL) {
1614 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1615 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616
Ron Eldor63140682017-01-09 19:27:59 +02001617#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1619 closedir(dir);
1620 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001621 }
Ron Eldor63140682017-01-09 19:27:59 +02001622#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001627 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001630 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001631 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1632 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 } else if (stat(entry_name, &sb) == -1) {
1634 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001635 /* Broken symbolic link - ignore this entry.
1636 stat(2) will return this error for either (a) a dangling
1637 symlink or (b) a missing file.
1638 Given that we have just obtained the filename from readdir,
1639 assume that it does exist and therefore treat this as a
1640 dangling symlink. */
1641 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001643 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001644 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1645 goto cleanup;
1646 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647 }
1648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652
1653 // Ignore parse errors
1654 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1656 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001662
1663cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001665
Ron Eldor63140682017-01-09 19:27:59 +02001666#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 }
Ron Eldor63140682017-01-09 19:27:59 +02001670#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001671
Paul Bakkerbe089b02013-10-14 15:51:50 +02001672#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677
Peter Kolbus9a969b62018-12-11 13:55:56 -06001678#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001679#define PRINT_ITEM(i) \
1680 do { \
1681 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1682 MBEDTLS_X509_SAFE_SNPRINTF; \
1683 sep = ", "; \
1684 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001685
Przemek Stekielf4194942023-04-24 09:52:17 +02001686#define CERT_TYPE(type, name) \
1687 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001688 if (ns_cert_type & (type)) { \
1689 PRINT_ITEM(name); \
1690 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001691 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001692
Przemek Stekielf4194942023-04-24 09:52:17 +02001693#define KEY_USAGE(code, name) \
1694 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001695 if (key_usage & (code)) { \
1696 PRINT_ITEM(name); \
1697 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001698 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700static int x509_info_ext_key_usage(char **buf, size_t *size,
1701 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001702{
Janos Follath865b3eb2019-12-16 11:46:15 +00001703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001704 const char *desc;
1705 size_t n = *size;
1706 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001708 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 while (cur != NULL) {
1711 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001712 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001716 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001717
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001718 sep = ", ";
1719
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001720 cur = cur->next;
1721 }
1722
1723 *size = n;
1724 *buf = p;
1725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001727}
1728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729static int x509_info_cert_policies(char **buf, size_t *size,
1730 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001731{
Janos Follath865b3eb2019-12-16 11:46:15 +00001732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001733 const char *desc;
1734 size_t n = *size;
1735 char *p = *buf;
1736 const mbedtls_x509_sequence *cur = certificate_policies;
1737 const char *sep = "";
1738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 while (cur != NULL) {
1740 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001741 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001745 MBEDTLS_X509_SAFE_SNPRINTF;
1746
1747 sep = ", ";
1748
1749 cur = cur->next;
1750 }
1751
1752 *size = n;
1753 *buf = p;
1754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001756}
1757
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758/*
1759 * Return an informational string about the certificate.
1760 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001761#define BEFORE_COLON 18
1762#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001763int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1764 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001765{
Janos Follath865b3eb2019-12-16 11:46:15 +00001766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001767 size_t n;
1768 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001769 char key_size_str[BEFORE_COLON];
1770
1771 p = buf;
1772 n = size;
1773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (NULL == crt) {
1775 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001776 MBEDTLS_X509_SAFE_SNPRINTF;
1777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001779 }
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1782 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001783 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1785 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001786 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001789 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001792 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001794 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001797 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001799 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1802 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1803 crt->valid_from.year, crt->valid_from.mon,
1804 crt->valid_from.day, crt->valid_from.hour,
1805 crt->valid_from.min, crt->valid_from.sec);
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%sexpires on : " \
1809 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1810 crt->valid_to.year, crt->valid_to.mon,
1811 crt->valid_to.day, crt->valid_to.hour,
1812 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001813 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
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_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1819 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001820 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001821
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001822 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1824 mbedtls_pk_get_name(&crt->pk))) != 0) {
1825 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826 }
1827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1829 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001830 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001831
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001832 /*
1833 * Optional extensions
1834 */
1835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1837 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1838 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001839 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (crt->max_pathlen > 0) {
1842 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001843 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001844 }
1845 }
1846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1848 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001849 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001850
Przemek Stekiel21c37282023-01-16 08:47:49 +01001851 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1852 &crt->subject_alt_names,
1853 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 return ret;
1855 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001856 }
1857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1859 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001860 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001861
Przemek Stekiel21c37282023-01-16 08:47:49 +01001862 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 return ret;
1864 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001865 }
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1868 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001869 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001870
Przemek Stekiel21c37282023-01-16 08:47:49 +01001871 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 return ret;
1873 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001874 }
1875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1877 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001878 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if ((ret = x509_info_ext_key_usage(&p, &n,
1881 &crt->ext_key_usage)) != 0) {
1882 return ret;
1883 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001884 }
1885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1887 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001888 MBEDTLS_X509_SAFE_SNPRINTF;
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if ((ret = x509_info_cert_policies(&p, &n,
1891 &crt->certificate_policies)) != 0) {
1892 return ret;
1893 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001894 }
1895
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001897 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001900}
1901
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001902struct x509_crt_verify_string {
1903 int code;
1904 const char *string;
1905};
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001908static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001909 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001910 { 0, NULL }
1911};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001912#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1915 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001916{
Janos Follath865b3eb2019-12-16 11:46:15 +00001917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001918 const struct x509_crt_verify_string *cur;
1919 char *p = buf;
1920 size_t n = size;
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1923 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001924 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001928 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001929 flags ^= cur->code;
1930 }
1931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if (flags != 0) {
1933 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1934 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001935 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001936 }
1937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001939}
Hanno Becker612a2f12020-10-09 09:19:39 +01001940#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1943 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001944{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001945 unsigned int usage_must, usage_may;
1946 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1950 return 0;
1951 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001952
1953 usage_must = usage & ~may_mask;
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1956 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1957 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001958
1959 usage_may = usage & may_mask;
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1962 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1963 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001966}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1969 const char *usage_oid,
1970 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001971{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001973
1974 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1976 return 0;
1977 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001978
1979 /*
1980 * Look for the requested usage (or wildcard ANY) in our list
1981 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (cur_oid->len == usage_len &&
1986 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1987 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001988 }
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1991 return 0;
1992 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001993 }
1994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001996}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001999/*
2000 * Return 1 if the certificate is revoked, or 0 otherwise.
2001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002002int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 while (cur != NULL && cur->serial.len != 0) {
2007 if (crt->serial.len == cur->serial.len &&
2008 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2009 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002010 }
2011
2012 cur = cur->next;
2013 }
2014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016}
2017
2018/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002019 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002020 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002022static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2023 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002024 const mbedtls_x509_crt_profile *profile,
2025 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002026{
2027 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002028 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002029#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002030 psa_algorithm_t psa_algorithm;
2031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002033#endif /* MBEDTLS_USE_PSA_CRYPTO */
2034 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 if (ca == NULL) {
2037 return flags;
2038 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 while (crl_list != NULL) {
2041 if (crl_list->version == 0 ||
2042 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002043 crl_list = crl_list->next;
2044 continue;
2045 }
2046
2047 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002048 * Check if the CA is configured to sign CRLs
2049 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 if (mbedtls_x509_crt_check_key_usage(ca,
2051 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002053 break;
2054 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002055
2056 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057 * Check if CRL is correctly signed by the trusted CA
2058 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002060 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002064 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002066
pespacek7599a772022-02-07 14:40:55 +01002067#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002068 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (psa_hash_compute(psa_algorithm,
2070 crl_list->tbs.p,
2071 crl_list->tbs.len,
2072 hash,
2073 sizeof(hash),
2074 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002075 /* Note: this can't happen except after an internal error */
2076 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2077 break;
2078 }
pespacek7599a772022-02-07 14:40:55 +01002079#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2081 hash_length = mbedtls_md_get_size(md_info);
2082 if (mbedtls_md(md_info,
2083 crl_list->tbs.p,
2084 crl_list->tbs.len,
2085 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002086 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088 break;
2089 }
pespaceka7a64692022-02-14 15:18:43 +01002090#endif /* MBEDTLS_USE_PSA_CRYPTO */
2091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002093 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2097 crl_list->sig_md, hash, hash_length,
2098 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002100 break;
2101 }
2102
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002103#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104 /*
2105 * Check for validity of CRL (Do not drop out)
2106 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002107 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002111 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002112 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002114#else
2115 ((void) now);
2116#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002117
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118 /*
2119 * Check if certificate is revoked
2120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123 break;
2124 }
2125
2126 crl_list = crl_list->next;
2127 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002133/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002134 * Check the signature of a certificate by its parent
2135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002136static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2137 mbedtls_x509_crt *parent,
2138 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002139{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002140 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002141 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002142#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2143 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 md_info = mbedtls_md_info_from_type(child->sig_md);
2145 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002146
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002147 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2149 return -1;
2150 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002151#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002152 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002153 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 status = psa_hash_compute(hash_alg,
2156 child->tbs.p,
2157 child->tbs.len,
2158 hash,
2159 sizeof(hash),
2160 &hash_len);
2161 if (status != PSA_SUCCESS) {
2162 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002163 }
2164
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002165#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002166 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2168 return -1;
2169 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002170
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002171#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2173 return mbedtls_pk_verify_restartable(&parent->pk,
2174 child->sig_md, hash, hash_len,
2175 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002176 }
2177#else
2178 (void) rs_ctx;
2179#endif
2180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2182 child->sig_md, hash, hash_len,
2183 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002184}
2185
2186/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002187 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2188 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002189 *
2190 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2193 const mbedtls_x509_crt *parent,
2194 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002195{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002196 int need_ca_bit;
2197
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002198 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2200 return -1;
2201 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002202
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002203 /* Parent must have the basicConstraints CA bit set as a general rule */
2204 need_ca_bit = 1;
2205
2206 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002208 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002209 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (need_ca_bit && !parent->ca_istrue) {
2212 return -1;
2213 }
2214
2215 if (need_ca_bit &&
2216 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2217 return -1;
2218 }
2219
2220 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002221}
2222
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002223/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002224 * Find a suitable parent for child in candidates, or return NULL.
2225 *
2226 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002227 * 1. subject name matches child's issuer
2228 * 2. if necessary, the CA bit is set and key usage allows signing certs
2229 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002230 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002231 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002232 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002233 * If there's a suitable candidate which is also time-valid, return the first
2234 * such. Otherwise, return the first suitable candidate (or NULL if there is
2235 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002236 *
2237 * The rationale for this rule is that someone could have a list of trusted
2238 * roots with two versions on the same root with different validity periods.
2239 * (At least one user reported having such a list and wanted it to just work.)
2240 * The reason we don't just require time-validity is that generally there is
2241 * only one version, and if it's expired we want the flags to state that
2242 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002243 *
2244 * The rationale for rule 3 (signature for trusted roots) is that users might
2245 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002246 * way we select the correct one is by checking the signature (as we don't
2247 * rely on key identifier extensions). (This is one way users might choose to
2248 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002249 *
2250 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002251 * - [in] child: certificate for which we're looking for a parent
2252 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002253 * - [out] r_parent: parent found (or NULL)
2254 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002255 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2256 * of the chain, 0 otherwise
2257 * - [in] path_cnt: number of intermediates seen so far
2258 * - [in] self_cnt: number of self-signed intermediates seen so far
2259 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002260 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002261 *
2262 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002263 * - 0 on success
2264 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002265 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002266static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 mbedtls_x509_crt *child,
2268 mbedtls_x509_crt *candidates,
2269 mbedtls_x509_crt **r_parent,
2270 int *r_signature_is_good,
2271 int top,
2272 unsigned path_cnt,
2273 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002274 mbedtls_x509_crt_restart_ctx *rs_ctx,
2275 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002276{
Janos Follath865b3eb2019-12-16 11:46:15 +00002277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002278 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002279 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002280
2281#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002282 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002284 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002285 parent = rs_ctx->parent;
2286 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002287 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002288
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002289 /* clear saved state */
2290 rs_ctx->parent = NULL;
2291 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002292 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002293
2294 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002295 goto check_signature;
2296 }
2297#endif
2298
2299 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002300 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002303 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002305 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002307
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002308 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (parent->max_pathlen > 0 &&
2310 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002311 continue;
2312 }
2313
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002314 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002315#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2316check_signature:
2317#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002319
2320#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002322 /* save state */
2323 rs_ctx->parent = parent;
2324 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002325 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002328 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002329#else
2330 (void) ret;
2331#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002332
2333 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002335 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002337
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002338#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002339 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002340 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2341 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002343 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002344 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002345 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002346
2347 continue;
2348 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002349#else
2350 ((void) now);
2351#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002352
Andy Gross1f627142019-01-30 10:25:53 -06002353 *r_parent = parent;
2354 *r_signature_is_good = signature_is_good;
2355
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002356 break;
2357 }
2358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002360 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002361 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002362 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002365}
2366
2367/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002368 * Find a parent in trusted CAs or the provided chain, or return NULL.
2369 *
2370 * Searches in trusted CAs first, and return the first suitable parent found
2371 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002372 *
2373 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002374 * - [in] child: certificate for which we're looking for a parent, followed
2375 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002376 * - [in] trust_ca: list of locally trusted certificates
2377 * - [out] parent: parent found (or NULL)
2378 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2379 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2380 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2381 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002382 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002383 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002384 *
2385 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002386 * - 0 on success
2387 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002388 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002389static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 mbedtls_x509_crt *child,
2391 mbedtls_x509_crt *trust_ca,
2392 mbedtls_x509_crt **parent,
2393 int *parent_is_trusted,
2394 int *signature_is_good,
2395 unsigned path_cnt,
2396 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002397 mbedtls_x509_crt_restart_ctx *rs_ctx,
2398 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002399{
Janos Follath865b3eb2019-12-16 11:46:15 +00002400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002401 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002402
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002403 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002404
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002405#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002406 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002408 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002409 rs_ctx->parent_is_trusted = -1;
2410 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002411#endif
2412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002414 search_list = *parent_is_trusted ? trust_ca : child->next;
2415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 ret = x509_crt_find_parent_in(child, search_list,
2417 parent, signature_is_good,
2418 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002419 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002420
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002421#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002423 /* save state */
2424 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002426 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002427#else
2428 (void) ret;
2429#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002430
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002431 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002433 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002435
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002436 /* prepare second iteration */
2437 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002438 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002439
2440 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002442 *parent_is_trusted = 0;
2443 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002444 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002447}
2448
2449/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002450 * Check if an end-entity certificate is locally trusted
2451 *
2452 * Currently we require such certificates to be self-signed (actually only
2453 * check for self-issued as self-signatures are not checked)
2454 */
2455static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 mbedtls_x509_crt *crt,
2457 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002458{
2459 mbedtls_x509_crt *cur;
2460
2461 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2463 return -1;
2464 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002465
2466 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2468 if (crt->raw.len == cur->raw.len &&
2469 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2470 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002471 }
2472 }
2473
2474 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002476}
2477
2478/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002479 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002480 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002481 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2482 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002483 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002484 * such that every cert in the chain is a child of the next one,
2485 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002486 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002487 * Verify that chain and return it with flags for all issues found.
2488 *
2489 * Special cases:
2490 * - EE == Rj -> return a one-element list containing it
2491 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2492 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002493 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002494 * Tests for (aspects of) this function should include at least:
2495 * - trusted EE
2496 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002497 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002498 * - if relevant: EE untrusted
2499 * - if relevant: EE -> intermediate, untrusted
2500 * with the aspect under test checked at each relevant level (EE, int, root).
2501 * For some aspects longer chains are required, but usually length 2 is
2502 * enough (but length 1 is not in general).
2503 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002504 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002505 * - [in] crt: the cert list EE, C1, ..., Cn
2506 * - [in] trust_ca: the trusted list R1, ..., Rp
2507 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002508 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002509 * Only valid when return value is 0, may contain garbage otherwise!
2510 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002511 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002512 *
2513 * Return value:
2514 * - non-zero if the chain could not be fully built and examined
2515 * - 0 is the chain was successfully built and examined,
2516 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002517 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002518static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 mbedtls_x509_crt *crt,
2520 mbedtls_x509_crt *trust_ca,
2521 mbedtls_x509_crl *ca_crl,
2522 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2523 void *p_ca_cb,
2524 const mbedtls_x509_crt_profile *profile,
2525 mbedtls_x509_crt_verify_chain *ver_chain,
2526 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002527{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002528 /* Don't initialize any of those variables here, so that the compiler can
2529 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002531 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002532 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002533 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002534 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002535 int parent_is_trusted;
2536 int child_is_trusted;
2537 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002538 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002539 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002540 mbedtls_x509_time now;
2541
2542#if defined(MBEDTLS_HAVE_TIME_DATE)
2543 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2544 return MBEDTLS_ERR_X509_FATAL_ERROR;
2545 }
2546#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002547
2548#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2549 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002551 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002552 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002553 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002554
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002555 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002556 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002557 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002558 flags = &cur->flags;
2559
2560 goto find_parent;
2561 }
2562#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002563
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002564 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002565 self_cnt = 0;
2566 parent_is_trusted = 0;
2567 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002570 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002571 cur = &ver_chain->items[ver_chain->len];
2572 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002573 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002574 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002575 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002576
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002577#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002578 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002579 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002580 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002582
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002583 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002584 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002586#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002587
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002588 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 if (child_is_trusted) {
2590 return 0;
2591 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002592
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002593 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002595 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002599 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002601
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002602 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (ver_chain->len == 1 &&
2604 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2605 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002606 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002607
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002608#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2609find_parent:
2610#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002611
2612 /* Obtain list of potential trusted signers from CA callback,
2613 * or use statically provided list. */
2614#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (f_ca_cb != NULL) {
2616 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2617 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002618 ver_chain->trust_ca_cb_result = NULL;
2619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2621 if (ret != 0) {
2622 return MBEDTLS_ERR_X509_FATAL_ERROR;
2623 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002624
2625 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002627#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2628 {
2629 ((void) f_ca_cb);
2630 ((void) p_ca_cb);
2631 cur_trust_ca = trust_ca;
2632 }
2633
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002634 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2636 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002637 ver_chain->len - 1, self_cnt, rs_ctx,
2638 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002639
2640#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002642 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002643 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002644 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002645 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002648 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002649#else
2650 (void) ret;
2651#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002652
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002653 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002655 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002657 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002658
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002659 /* Count intermediate self-issued (not necessarily self-signed) certs.
2660 * These can occur with some strategies for key rollover, see [SIRO],
2661 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if (ver_chain->len != 1 &&
2663 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002664 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002665 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002666
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002667 /* path_cnt is 0 for the first intermediate CA,
2668 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (!parent_is_trusted &&
2670 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002671 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002673 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002674
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002675 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002677 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002679
2680 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002682 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002686 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002687 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002688#else
2689 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002690#endif
2691
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002692 /* prepare for next iteration */
2693 child = parent;
2694 parent = NULL;
2695 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002696 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002697 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002698}
2699
Glenn Straussc26bd762022-10-23 19:48:18 -04002700#ifdef _WIN32
2701#ifdef _MSC_VER
2702#pragma comment(lib, "ws2_32.lib")
2703#include <winsock2.h>
2704#include <ws2tcpip.h>
2705#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2706#include <winsock2.h>
2707#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002708#else
2709/* inet_pton() is not supported, fallback to software version */
2710#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002711#endif
2712#elif defined(__sun)
2713/* Solaris requires -lsocket -lnsl for inet_pton() */
2714#elif defined(__has_include)
2715#if __has_include(<sys/socket.h>)
2716#include <sys/socket.h>
2717#endif
2718#if __has_include(<arpa/inet.h>)
2719#include <arpa/inet.h>
2720#endif
2721#endif
2722
2723/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2724 * the platform inet_pton() or a local implementation (below). The local
2725 * implementation may be used even in cases where the platform provides
2726 * inet_pton(), e.g. when there are different includes required and/or the
2727 * platform implementation requires dependencies on additional libraries.
2728 * Specifically, Windows requires custom includes and additional link
2729 * dependencies, and Solaris requires additional link dependencies.
2730 * Also, as a coarse heuristic, use the local implementation if the compiler
2731 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002732 * provided by headers included (or not) via __has_include() above.
2733 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2734 * despite having a platform that has inet_pton. */
2735#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2736/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002737static int x509_inet_pton_ipv4(const char *src, void *dst);
2738
2739#define li_cton(c, n) \
2740 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2741
2742static int x509_inet_pton_ipv6(const char *src, void *dst)
2743{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002744 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002745 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002746 uint16_t addr[8];
2747 do {
2748 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002749 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002750 for (uint8_t digit; num_digits < 4; num_digits++) {
2751 if (li_cton(*p, digit) == 0) {
2752 break;
2753 }
2754 group = (group << 4) | digit;
2755 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002756 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002757 if (num_digits != 0) {
Andrzej Kurek0d578962023-04-13 09:09:56 -04002758 addr[nonzero_groups++] = MBEDTLS_IS_BIG_ENDIAN ? group :
2759 (group << 8) | (group >> 8);
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002760 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002761 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002762 } else if (*p == '.') {
2763 /* Don't accept IPv4 too early or late */
2764 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2765 nonzero_groups >= 7) {
2766 break;
2767 }
2768
2769 /* Walk back to prior ':', then parse as IPv4-mapped */
2770 int steps = 4;
2771 do {
2772 p--;
2773 steps--;
2774 } while (*p != ':' && steps > 0);
2775
2776 if (*p != ':') {
2777 break;
2778 }
2779 p++;
2780 nonzero_groups--;
2781 if (x509_inet_pton_ipv4((const char *) p,
2782 addr + nonzero_groups) != 0) {
2783 break;
2784 }
2785
Andrzej Kurek0d578962023-04-13 09:09:56 -04002786 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002787 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002788 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002789 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002790 return -1;
2791 }
2792 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002793 /* Don't accept a second zero group or an invalid delimiter */
2794 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002795 return -1;
2796 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002797 zero_group_start = nonzero_groups;
2798
2799 /* Accept a zero group at start, but it has to be a double colon */
2800 if (zero_group_start == 0 && *++p != ':') {
2801 return -1;
2802 }
2803
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002804 if (p[1] == '\0') {
2805 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002806 break;
2807 }
2808 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002809 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002810 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002811
2812 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002813 return -1;
2814 }
2815
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002816 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002817 if (nonzero_groups > 6) {
2818 return -1;
2819 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002820 int zero_groups = 8 - nonzero_groups;
2821 int groups_after_zero = nonzero_groups - zero_group_start;
2822
2823 /* Move the non-zero part to after the zeroes */
2824 if (groups_after_zero) {
2825 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002826 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002827 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002828 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002829 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002830 } else {
2831 if (nonzero_groups != 8) {
2832 return -1;
2833 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002834 }
2835 memcpy(dst, addr, sizeof(addr));
2836 return 0;
2837}
2838
2839static int x509_inet_pton_ipv4(const char *src, void *dst)
2840{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002841 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002842 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002843 uint8_t digit, num_digits = 0;
2844 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002845 uint16_t octet;
2846
Glenn Strauss416c2952022-10-24 23:00:02 -04002847 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002848 octet = num_digits = 0;
2849 do {
2850 digit = *p - '0';
2851 if (digit > 9) {
2852 break;
2853 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002854
2855 /* Don't allow leading zeroes. These might mean octal format,
2856 * which this implementation does not support. */
2857 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002858 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002859 }
2860
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002861 octet = octet * 10 + digit;
2862 num_digits++;
2863 p++;
2864 } while (num_digits < 3);
2865
2866 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002867 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002868 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002869 *res++ = (uint8_t) octet;
2870 num_octets++;
2871 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002872 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002873}
Glenn Straussc26bd762022-10-23 19:48:18 -04002874
2875#else
2876
2877static int x509_inet_pton_ipv6(const char *src, void *dst)
2878{
2879 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2880}
2881
2882static int x509_inet_pton_ipv4(const char *src, void *dst)
2883{
2884 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2885}
2886
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002887#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002888
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002889size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002890{
2891 return strchr(cn, ':') == NULL
2892 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2893 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2894}
2895
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002896/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002897 * Check for CN match
2898 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002899static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2900 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002901{
2902 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 if (name->len == cn_len &&
2904 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2905 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002906 }
2907
2908 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 if (x509_check_wildcard(cn, name) == 0) {
2910 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002911 }
2912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002914}
2915
Glenn Straussc26bd762022-10-23 19:48:18 -04002916static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2917 const char *cn, size_t cn_len)
2918{
2919 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002920 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002921 if (cn_len == 0) {
2922 return -1;
2923 }
2924
2925 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2926 const unsigned char san_type = (unsigned char) cur->buf.tag &
2927 MBEDTLS_ASN1_TAG_VALUE_MASK;
2928 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2929 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2930 return 0;
2931 }
2932 }
2933
2934 return -1;
2935}
2936
Andrzej Kurek199eab92023-05-10 09:57:19 -04002937static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2938 const char *cn, size_t cn_len)
2939{
2940 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2941 const unsigned char san_type = (unsigned char) cur->buf.tag &
2942 MBEDTLS_ASN1_TAG_VALUE_MASK;
2943 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2944 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2945 return 0;
2946 }
2947 }
2948
2949 return -1;
2950}
2951
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002952/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002953 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2954 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002955static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002957{
Glenn Straussc26bd762022-10-23 19:48:18 -04002958 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002959 int san_uri = 0;
2960 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002961 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2962 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002963 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002964 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2965 return 0;
2966 }
2967 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002968 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002969 san_ip = 1;
2970 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002971 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2972 san_uri = 1;
2973 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002974 /* (We may handle other types here later.) */
2975 default: /* Unrecognized type */
2976 break;
2977 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002979 if (san_ip) {
2980 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2981 return 0;
2982 }
2983 }
2984 if (san_uri) {
2985 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2986 return 0;
2987 }
2988 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002989
Andrzej Kurek199eab92023-05-10 09:57:19 -04002990 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002991}
2992
2993/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002994 * Verify the requested CN - only call this if cn is not NULL!
2995 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002996static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2997 const char *cn,
2998 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002999{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003000 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003004 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
3005 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 }
3007 } else {
3008 for (name = &crt->subject; name != NULL; name = name->next) {
3009 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3010 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003011 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 }
3013 }
3014
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003015 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003016
3017 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003018}
3019
3020/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003021 * Merge the flags for all certs in the chain, after calling callback
3022 */
3023static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 uint32_t *flags,
3025 const mbedtls_x509_crt_verify_chain *ver_chain,
3026 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3027 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003028{
Janos Follath865b3eb2019-12-16 11:46:15 +00003029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003030 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003031 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003032 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003035 cur = &ver_chain->items[i-1];
3036 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 if (NULL != f_vrfy) {
3039 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3040 return ret;
3041 }
3042 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003043
3044 *flags |= cur_flags;
3045 }
3046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003048}
3049
3050/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003051 * Verify the certificate validity, with profile, restartable version
3052 *
3053 * This function:
3054 * - checks the requested CN (if any)
3055 * - checks the type and size of the EE cert's key,
3056 * as that isn't done as part of chain building/verification currently
3057 * - builds and verifies the chain
3058 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003059 *
3060 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3061 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3062 * verification routine to search for trusted signers, and CRLs will
3063 * be disabled. Otherwise, `trust_ca` will be used as the static list
3064 * of trusted signers, and `ca_crl` will be use as the static list
3065 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3068 mbedtls_x509_crt *trust_ca,
3069 mbedtls_x509_crl *ca_crl,
3070 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3071 void *p_ca_cb,
3072 const mbedtls_x509_crt_profile *profile,
3073 const char *cn, uint32_t *flags,
3074 int (*f_vrfy)(void *,
3075 mbedtls_x509_crt *,
3076 int,
3077 uint32_t *),
3078 void *p_vrfy,
3079 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003080{
Janos Follath865b3eb2019-12-16 11:46:15 +00003081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003082 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003083 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003084 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003085
3086 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003087 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003091 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3092 goto exit;
3093 }
3094
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003095 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 if (cn != NULL) {
3097 x509_crt_verify_name(crt, cn, &ee_flags);
3098 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003099
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003100 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003104 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003106
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003108 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003110
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003111 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3113 f_ca_cb, p_ca_cb, profile,
3114 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003119
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003120 /* Merge end-entity flags */
3121 ver_chain.items[0].flags |= ee_flags;
3122
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003123 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003125
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003126exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003127
3128#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3130 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003131 ver_chain.trust_ca_cb_result = NULL;
3132#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3133
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003134#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3136 mbedtls_x509_crt_restart_free(rs_ctx);
3137 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003138#endif
3139
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003140 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3141 * the SSL module for authmode optional, but non-zero return from the
3142 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003144 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003145 }
3146
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 if (ret != 0) {
3148 *flags = (uint32_t) -1;
3149 return ret;
3150 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 if (*flags != 0) {
3153 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3154 }
3155
3156 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003157}
3158
Hanno Becker3116fb32019-03-28 13:34:42 +00003159
3160/*
3161 * Verify the certificate validity (default profile, not restartable)
3162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003163int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3164 mbedtls_x509_crt *trust_ca,
3165 mbedtls_x509_crl *ca_crl,
3166 const char *cn, uint32_t *flags,
3167 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3168 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003169{
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3171 NULL, NULL,
3172 &mbedtls_x509_crt_profile_default,
3173 cn, flags,
3174 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003175}
3176
3177/*
3178 * Verify the certificate validity (user-chosen profile, not restartable)
3179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003180int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3181 mbedtls_x509_crt *trust_ca,
3182 mbedtls_x509_crl *ca_crl,
3183 const mbedtls_x509_crt_profile *profile,
3184 const char *cn, uint32_t *flags,
3185 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3186 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003187{
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3189 NULL, NULL,
3190 profile, cn, flags,
3191 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003192}
3193
3194#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3195/*
3196 * Verify the certificate validity (user-chosen profile, CA callback,
3197 * not restartable).
3198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003199int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3200 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3201 void *p_ca_cb,
3202 const mbedtls_x509_crt_profile *profile,
3203 const char *cn, uint32_t *flags,
3204 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3205 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003206{
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3208 f_ca_cb, p_ca_cb,
3209 profile, cn, flags,
3210 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003211}
3212#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3215 mbedtls_x509_crt *trust_ca,
3216 mbedtls_x509_crl *ca_crl,
3217 const mbedtls_x509_crt_profile *profile,
3218 const char *cn, uint32_t *flags,
3219 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3220 void *p_vrfy,
3221 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003222{
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3224 NULL, NULL,
3225 profile, cn, flags,
3226 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003227}
3228
3229
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003230/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003231 * Initialize a certificate chain
3232 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003233void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003234{
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003236}
3237
3238/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003239 * Unallocate all certificate data
3240 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003241void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243 mbedtls_x509_crt *cert_cur = crt;
3244 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 while (cert_cur != NULL) {
3247 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003251#endif
3252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3254 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3255 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3256 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3257 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003258 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3261 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3262 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003263 }
3264
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003265 cert_prv = cert_cur;
3266 cert_cur = cert_cur->next;
3267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3269 if (cert_prv != crt) {
3270 mbedtls_free(cert_prv);
3271 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003272 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003273}
3274
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003275#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3276/*
3277 * Initialize a restart context
3278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003279void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003280{
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003282
3283 ctx->parent = NULL;
3284 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003285 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003286
3287 ctx->parent_is_trusted = -1;
3288
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003289 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003290 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003292}
3293
3294/*
3295 * Free the components of a restart context
3296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003297void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003298{
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003300 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 mbedtls_pk_restart_free(&ctx->pk);
3304 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003305}
3306#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#endif /* MBEDTLS_X509_CRT_PARSE_C */