blob: 6a27e924165a74e2e4169865f86f917f9a96189a [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"
49#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel40afdd22022-09-06 13:08:28 +020051#include "hash_info.h"
Glenn Strauss6f545ac2022-10-25 15:02:14 -040052#include "x509_invasive.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
63#ifndef _WIN32_WINNT
64#define _WIN32_WINNT 0x0600
65#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#include <windows.h>
67#else
68#include <time.h>
69#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100070#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020077#if defined(__MBED__)
78#include <platform/mbed_retarget.h>
79#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020081#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060082#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000083#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084#endif
85
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020086/*
87 * Item in a verification chain: cert and flags for it
88 */
89typedef struct {
90 mbedtls_x509_crt *crt;
91 uint32_t flags;
92} x509_crt_verify_chain_item;
93
94/*
95 * Max size of verification chain: end-entity + intermediates + trusted root
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020098
Gilles Peskineffb92da2021-06-02 00:03:26 +020099/* Default profile. Do not remove items unless there are serious security
100 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
102{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200103 /* Hashes from SHA-256 and above. Note that this selection
104 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
106 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
107 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 0xFFFFFFF, /* Any PK alg */
Valerio Settid4a5d462023-04-05 18:19:01 +0200109#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200110 /* Curves at or above 128-bit security level. Note that this selection
111 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
113 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
114 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
115 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
116 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
117 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200118 0,
Valerio Settid4a5d462023-04-05 18:19:01 +0200119#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200120 0,
Valerio Settid4a5d462023-04-05 18:19:01 +0200121#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200122 2048,
123};
124
Gilles Peskineffb92da2021-06-02 00:03:26 +0200125/* Next-generation profile. Currently identical to the default, but may
126 * be tightened at any time. */
127const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200128{
129 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
131 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
132 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200133 0xFFFFFFF, /* Any PK alg */
134#if defined(MBEDTLS_ECP_C)
135 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
138 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
139 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
140 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
141 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
142 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200143#else
144 0,
145#endif
146 2048,
147};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200148
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200149/*
150 * NSA Suite B Profile
151 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200152const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
153{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200154 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
156 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200157 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
159 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Settid4a5d462023-04-05 18:19:01 +0200160#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200161 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
163 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Settid4a5d462023-04-05 18:19:01 +0200164#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200165 0,
Valerio Settid4a5d462023-04-05 18:19:01 +0200166#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200167 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200168};
169
170/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200171 * Empty / all-forbidden profile
172 */
173const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
174{
175 0,
176 0,
177 0,
178 (uint32_t) -1,
179};
180
181/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200183 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
186 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187{
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (md_alg == MBEDTLS_MD_NONE) {
189 return -1;
190 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
193 return 0;
194 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197}
198
199/*
200 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200201 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
204 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200205{
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if (pk_alg == MBEDTLS_PK_NONE) {
207 return -1;
208 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
211 return 0;
212 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200215}
216
217/*
218 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200219 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
222 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200223{
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200225
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
228 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
229 return 0;
230 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200233 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200234#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200235
Valerio Settid4a5d462023-04-05 18:19:01 +0200236#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200238 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 pk_alg == MBEDTLS_PK_ECKEY_DH) {
240 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (gid == MBEDTLS_ECP_DP_NONE) {
243 return -1;
244 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
247 return 0;
248 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200251 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200252#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200255}
256
257/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000258 * Like memcmp, but case-insensitive and always returns -1 if different
259 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000261{
262 size_t i;
263 unsigned char diff;
264 const unsigned char *n1 = s1, *n2 = s2;
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000267 diff = n1[i] ^ n2[i];
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000270 continue;
271 }
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (diff == 32 &&
274 ((n1[i] >= 'a' && n1[i] <= 'z') ||
275 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
276 continue;
277 }
278
279 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000280 }
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000283}
284
285/*
286 * Return 0 if name matches wildcard, -1 otherwise
287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000289{
290 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000292
293 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
295 return -1;
296 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 for (i = 0; i < cn_len; ++i) {
299 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000300 cn_idx = i;
301 break;
302 }
303 }
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (cn_idx == 0) {
306 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000307 }
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (cn_len - cn_idx == name->len - 1 &&
310 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
311 return 0;
312 }
313
314 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000315}
316
317/*
318 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
319 * variations (but not all).
320 *
321 * Return 0 if equal, -1 otherwise.
322 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000324{
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000326 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 memcmp(a->p, b->p, b->len) == 0) {
328 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000329 }
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
332 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000333 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 x509_memcasecmp(a->p, b->p, b->len) == 0) {
335 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000336 }
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000339}
340
341/*
342 * Compare two X.509 Names (aka rdnSequence).
343 *
344 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
345 * we sometimes return unequal when the full algorithm would return equal,
346 * but never the other way. (In particular, we don't do Unicode normalisation
347 * or space folding.)
348 *
349 * Return 0 if equal, -1 otherwise.
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000352{
353 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 while (a != NULL || b != NULL) {
355 if (a == NULL || b == NULL) {
356 return -1;
357 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000358
359 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000361 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
363 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000364 }
365
366 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (x509_string_cmp(&a->val, &b->val) != 0) {
368 return -1;
369 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000370
371 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (a->next_merged != b->next_merged) {
373 return -1;
374 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000375
376 a = a->next;
377 b = b->next;
378 }
379
380 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000382}
383
384/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200385 * Reset (init or clear) a verify_chain
386 */
387static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200389{
390 size_t i;
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200393 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000394 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200395 }
396
397 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000398
399#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
400 ver_chain->trust_ca_cb_result = NULL;
401#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200402}
403
404/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
406 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407static int x509_get_version(unsigned char **p,
408 const unsigned char *end,
409 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410{
Janos Follath865b3eb2019-12-16 11:46:15 +0000411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 size_t len;
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
415 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
416 0)) != 0) {
417 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420 }
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 }
424
425 end = *p + len;
426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
428 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
429 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (*p != end) {
432 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
433 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
434 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437}
438
439/*
440 * Validity ::= SEQUENCE {
441 * notBefore Time,
442 * notAfter Time }
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444static int x509_get_dates(unsigned char **p,
445 const unsigned char *end,
446 mbedtls_x509_time *from,
447 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448{
Janos Follath865b3eb2019-12-16 11:46:15 +0000449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450 size_t len;
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
453 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
454 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
455 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
457 end = *p + len;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
460 return ret;
461 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
464 return ret;
465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (*p != end) {
468 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
469 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
470 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473}
474
475/*
476 * X.509 v2/v3 unique identifier (not parsed)
477 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478static int x509_get_uid(unsigned char **p,
479 const unsigned char *end,
480 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481{
Janos Follath865b3eb2019-12-16 11:46:15 +0000482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (*p == end) {
485 return 0;
486 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487
488 uid->tag = **p;
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
491 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
492 n)) != 0) {
493 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
494 return 0;
495 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 }
499
500 uid->p = *p;
501 *p += uid->len;
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504}
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506static int x509_get_basic_constraints(unsigned char **p,
507 const unsigned char *end,
508 int *ca_istrue,
509 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510{
Janos Follath865b3eb2019-12-16 11:46:15 +0000511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 size_t len;
513
514 /*
515 * BasicConstraints ::= SEQUENCE {
516 * cA BOOLEAN DEFAULT FALSE,
517 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
518 */
519 *ca_istrue = 0; /* DEFAULT FALSE */
520 *max_pathlen = 0; /* endless */
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
523 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
524 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 }
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (*p == end) {
528 return 0;
529 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
532 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
533 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
534 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (ret != 0) {
537 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
538 }
539
540 if (*ca_istrue != 0) {
541 *ca_istrue = 1;
542 }
543 }
544
545 if (*p == end) {
546 return 0;
547 }
548
549 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
550 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
551 }
552
553 if (*p != end) {
554 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
555 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
556 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
Andrzej Kurek16050742020-04-14 09:49:52 -0400558 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
559 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (*max_pathlen == INT_MAX) {
561 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
562 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
563 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400564
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565 (*max_pathlen)++;
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568}
569
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570/*
571 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
572 *
573 * KeyPurposeId ::= OBJECT IDENTIFIER
574 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100575static int x509_get_ext_key_usage(unsigned char **p,
576 const unsigned char *end,
577 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578{
Janos Follath865b3eb2019-12-16 11:46:15 +0000579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
582 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
583 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
585 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ext_key_usage->buf.p == NULL) {
587 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
588 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
589 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592}
593
594/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200595 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
596 *
597 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
598 *
599 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
600 *
601 * PolicyInformation ::= SEQUENCE {
602 * policyIdentifier CertPolicyId,
603 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
604 * PolicyQualifierInfo OPTIONAL }
605 *
606 * CertPolicyId ::= OBJECT IDENTIFIER
607 *
608 * PolicyQualifierInfo ::= SEQUENCE {
609 * policyQualifierId PolicyQualifierId,
610 * qualifier ANY DEFINED BY policyQualifierId }
611 *
612 * -- policyQualifierIds for Internet policy qualifiers
613 *
614 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
615 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
616 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
617 *
618 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
619 *
620 * Qualifier ::= CHOICE {
621 * cPSuri CPSuri,
622 * userNotice UserNotice }
623 *
624 * CPSuri ::= IA5String
625 *
626 * UserNotice ::= SEQUENCE {
627 * noticeRef NoticeReference OPTIONAL,
628 * explicitText DisplayText OPTIONAL }
629 *
630 * NoticeReference ::= SEQUENCE {
631 * organization DisplayText,
632 * noticeNumbers SEQUENCE OF INTEGER }
633 *
634 * DisplayText ::= CHOICE {
635 * ia5String IA5String (SIZE (1..200)),
636 * visibleString VisibleString (SIZE (1..200)),
637 * bmpString BMPString (SIZE (1..200)),
638 * utf8String UTF8String (SIZE (1..200)) }
639 *
640 * NOTE: we only parse and use anyPolicy without qualifiers at this point
641 * as defined in RFC 5280.
642 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100643static int x509_get_certificate_policies(unsigned char **p,
644 const unsigned char *end,
645 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200646{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300647 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200648 size_t len;
649 mbedtls_asn1_buf *buf;
650 mbedtls_asn1_sequence *cur = certificate_policies;
651
652 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 ret = mbedtls_asn1_get_tag(p, end, &len,
654 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
655 if (ret != 0) {
656 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
657 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (*p + len != end) {
660 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
661 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
662 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200663
664 /*
665 * Cannot be an empty sequence.
666 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (len == 0) {
668 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
669 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
670 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200673 mbedtls_x509_buf policy_oid;
674 const unsigned char *policy_end;
675
676 /*
677 * Get the policy sequence
678 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
680 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
681 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
682 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200683
684 policy_end = *p + len;
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
687 MBEDTLS_ASN1_OID)) != 0) {
688 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
689 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200690
691 policy_oid.tag = MBEDTLS_ASN1_OID;
692 policy_oid.len = len;
693 policy_oid.p = *p;
694
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300695 /*
696 * Only AnyPolicy is currently supported when enforcing policy.
697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300699 /*
700 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200701 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300702 */
703 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
704 }
705
Ron Eldor74d9acc2019-03-21 14:00:03 +0200706 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (cur->buf.p != NULL) {
708 if (cur->next != NULL) {
709 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
710 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (cur->next == NULL) {
715 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
716 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
717 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200718
719 cur = cur->next;
720 }
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200723 buf->tag = policy_oid.tag;
724 buf->p = policy_oid.p;
725 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300726
727 *p += len;
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 /*
730 * If there is an optional qualifier, then *p < policy_end
731 * Check the Qualifier len to verify it doesn't exceed policy_end.
732 */
733 if (*p < policy_end) {
734 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
735 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
736 0) {
737 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
738 }
Ron Eldor08063792019-05-13 16:38:39 +0300739 /*
740 * Skip the optional policy qualifiers.
741 */
742 *p += len;
743 }
744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (*p != policy_end) {
746 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
747 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
748 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200749 }
750
751 /* Set final sequence entry's next pointer to NULL */
752 cur->next = NULL;
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (*p != end) {
755 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
756 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
757 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200760}
761
762/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200763 * X.509 v3 extensions
764 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766static int x509_get_crt_ext(unsigned char **p,
767 const unsigned char *end,
768 mbedtls_x509_crt *crt,
769 mbedtls_x509_crt_ext_cb_t cb,
770 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771{
Janos Follath865b3eb2019-12-16 11:46:15 +0000772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200774 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (*p == end) {
777 return 0;
778 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
781 return ret;
782 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783
Hanno Becker12f62fb2019-02-12 17:22:36 +0000784 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 /*
787 * Extension ::= SEQUENCE {
788 * extnID OBJECT IDENTIFIER,
789 * critical BOOLEAN DEFAULT FALSE,
790 * extnValue OCTET STRING }
791 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793 int is_critical = 0; /* DEFAULT FALSE */
794 int ext_type = 0;
795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
797 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
798 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
799 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800
801 end_ext_data = *p + len;
802
803 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
805 MBEDTLS_ASN1_OID)) != 0) {
806 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
807 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808
k-stachowiak463928a2018-07-24 12:50:59 +0200809 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810 extn_oid.p = *p;
811 *p += extn_oid.len;
812
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
815 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
816 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
817 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200818
819 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
821 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
822 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
823 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200825 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200826 end_ext_octet = *p + len;
827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (end_ext_octet != end_ext_data) {
829 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
830 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
831 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832
833 /*
834 * Detect supported extensions
835 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200839 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (cb != NULL) {
841 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
842 if (ret != 0 && is_critical) {
843 return ret;
844 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200845 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200846 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200847 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200848
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849 /* No parser found, skip extension */
850 *p = end_ext_octet;
851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
855 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857 continue;
858 }
859
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100860 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if ((crt->ext_types & ext_type) != 0) {
862 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
863 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100864
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200865 crt->ext_types |= ext_type;
866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 switch (ext_type) {
868 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
869 /* Parse basic constraints */
870 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
871 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
872 return ret;
873 }
874 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 case MBEDTLS_X509_EXT_KEY_USAGE:
877 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100878 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
879 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return ret;
881 }
882 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
885 /* Parse extended key usage */
886 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
887 &crt->ext_key_usage)) != 0) {
888 return ret;
889 }
890 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
893 /* Parse subject alt name */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100894 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
895 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 return ret;
897 }
898 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
901 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100902 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
903 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 return ret;
905 }
906 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
909 /* Parse certificate policies type */
910 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
911 &crt->certificate_policies)) != 0) {
912 /* Give the callback (if any) a chance to handle the extension
913 * if it contains unsupported policies */
914 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
915 cb(p_ctx, crt, &extn_oid, is_critical,
916 start_ext_octet, end_ext_octet) == 0) {
917 break;
918 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (is_critical) {
921 return ret;
922 } else
923 /*
924 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
925 * cannot interpret or enforce the policy. However, it is up to
926 * the user to choose how to enforce the policies,
927 * unless the extension is critical.
928 */
929 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
930 return ret;
931 }
932 }
933 break;
934
935 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300936 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 * If this is a non-critical extension, which the oid layer
938 * supports, but there isn't an x509 parser for it,
939 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300940 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (is_critical) {
942 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
943 } else {
944 *p = end_ext_octet;
945 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946 }
947 }
948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (*p != end) {
950 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
951 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
952 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955}
956
957/*
958 * Parse and fill a single X.509 certificate in DER format
959 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
961 const unsigned char *buf,
962 size_t buflen,
963 int make_copy,
964 mbedtls_x509_crt_ext_cb_t cb,
965 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966{
Janos Follath865b3eb2019-12-16 11:46:15 +0000967 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 size_t len;
969 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
973 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
974 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975
976 /*
977 * Check for valid input
978 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (crt == NULL || buf == NULL) {
980 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
981 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000983 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +0000985 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 end = p + len;
987
988 /*
989 * Certificate ::= SEQUENCE {
990 * tbsCertificate TBSCertificate,
991 * signatureAlgorithm AlgorithmIdentifier,
992 * signatureValue BIT STRING }
993 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
995 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
996 mbedtls_x509_crt_free(crt);
997 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 }
999
Janos Follathcc0e49d2016-02-17 14:34:12 +00001000 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001001 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001003 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1005 if (crt->raw.p == NULL) {
1006 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1007 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001010 crt->own_buffer = 1;
1011
1012 p += crt->raw.len - len;
1013 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 } else {
1015 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001016 crt->own_buffer = 0;
1017 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001018
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 /*
1020 * TBSCertificate ::= SEQUENCE {
1021 */
1022 crt->tbs.p = p;
1023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1025 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1026 mbedtls_x509_crt_free(crt);
1027 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 }
1029
1030 end = p + len;
1031 crt->tbs.len = end - crt->tbs.p;
1032
1033 /*
1034 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1035 *
1036 * CertificateSerialNumber ::= INTEGER
1037 *
1038 * signature AlgorithmIdentifier
1039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1041 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1042 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1043 &sig_params1)) != 0) {
1044 mbedtls_x509_crt_free(crt);
1045 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 }
1047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (crt->version < 0 || crt->version > 2) {
1049 mbedtls_x509_crt_free(crt);
1050 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 }
1052
Andres AG7ca4a032017-03-09 16:16:11 +00001053 crt->version++;
1054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1056 &crt->sig_md, &crt->sig_pk,
1057 &crt->sig_opts)) != 0) {
1058 mbedtls_x509_crt_free(crt);
1059 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060 }
1061
1062 /*
1063 * issuer Name
1064 */
1065 crt->issuer_raw.p = p;
1066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1068 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1069 mbedtls_x509_crt_free(crt);
1070 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071 }
1072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1074 mbedtls_x509_crt_free(crt);
1075 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076 }
1077
1078 crt->issuer_raw.len = p - crt->issuer_raw.p;
1079
1080 /*
1081 * Validity ::= SEQUENCE {
1082 * notBefore Time,
1083 * notAfter Time }
1084 *
1085 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1087 &crt->valid_to)) != 0) {
1088 mbedtls_x509_crt_free(crt);
1089 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090 }
1091
1092 /*
1093 * subject Name
1094 */
1095 crt->subject_raw.p = p;
1096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1098 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1099 mbedtls_x509_crt_free(crt);
1100 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101 }
1102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1104 mbedtls_x509_crt_free(crt);
1105 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 }
1107
1108 crt->subject_raw.len = p - crt->subject_raw.p;
1109
1110 /*
1111 * SubjectPublicKeyInfo
1112 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001113 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1115 mbedtls_x509_crt_free(crt);
1116 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001118 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119
1120 /*
1121 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1122 * -- If present, version shall be v2 or v3
1123 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1124 * -- If present, version shall be v2 or v3
1125 * extensions [3] EXPLICIT Extensions OPTIONAL
1126 * -- If present, version shall be v3
1127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (crt->version == 2 || crt->version == 3) {
1129 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1130 if (ret != 0) {
1131 mbedtls_x509_crt_free(crt);
1132 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133 }
1134 }
1135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (crt->version == 2 || crt->version == 3) {
1137 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1138 if (ret != 0) {
1139 mbedtls_x509_crt_free(crt);
1140 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 }
1142 }
1143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (crt->version == 3) {
1145 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1146 if (ret != 0) {
1147 mbedtls_x509_crt_free(crt);
1148 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 }
1150 }
1151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if (p != end) {
1153 mbedtls_x509_crt_free(crt);
1154 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1155 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 }
1157
1158 end = crt_end;
1159
1160 /*
1161 * }
1162 * -- end of TBSCertificate
1163 *
1164 * signatureAlgorithm AlgorithmIdentifier,
1165 * signatureValue BIT STRING
1166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1168 mbedtls_x509_crt_free(crt);
1169 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 }
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (crt->sig_oid.len != sig_oid2.len ||
1173 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001174 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001175 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 (sig_params1.len != 0 &&
1177 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1178 mbedtls_x509_crt_free(crt);
1179 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 }
1181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1183 mbedtls_x509_crt_free(crt);
1184 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 }
1186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (p != end) {
1188 mbedtls_x509_crt_free(crt);
1189 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1190 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 }
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194}
1195
1196/*
1197 * Parse one X.509 certificate in DER format from a buffer and add them to a
1198 * chained list
1199 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1201 const unsigned char *buf,
1202 size_t buflen,
1203 int make_copy,
1204 mbedtls_x509_crt_ext_cb_t cb,
1205 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206{
Janos Follath865b3eb2019-12-16 11:46:15 +00001207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209
1210 /*
1211 * Check for valid input
1212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (crt == NULL || buf == NULL) {
1214 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1215 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001218 prev = crt;
1219 crt = crt->next;
1220 }
1221
1222 /*
1223 * Add new certificate on the end of the chain if needed.
1224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (crt->version != 0 && crt->next == NULL) {
1226 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (crt->next == NULL) {
1229 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1230 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231
1232 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001234 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 }
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1238 if (ret != 0) {
1239 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001240 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (crt != chain) {
1244 mbedtls_free(crt);
1245 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248 }
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251}
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1254 const unsigned char *buf,
1255 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001256{
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001258}
1259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1261 const unsigned char *buf,
1262 size_t buflen,
1263 int make_copy,
1264 mbedtls_x509_crt_ext_cb_t cb,
1265 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001266{
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001268}
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1271 const unsigned char *buf,
1272 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001273{
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001275}
1276
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001277/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001278 * Parse one or more PEM certificates from a buffer and add them to the chained
1279 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001281int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1282 const unsigned char *buf,
1283 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284{
Janos Follath98e28a72016-05-31 14:03:54 +01001285#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001286 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001288#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001289
1290 /*
1291 * Check for valid input
1292 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (chain == NULL || buf == NULL) {
1294 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1295 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296
1297 /*
1298 * Determine buffer content. Buffer contains either one DER certificate or
1299 * one or more PEM certificates.
1300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1303 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001305 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1308 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1309 }
Janos Follath98e28a72016-05-31 14:03:54 +01001310#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001312#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001319 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001321 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001323
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001324 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 ret = mbedtls_pem_read_buffer(&pem,
1326 "-----BEGIN CERTIFICATE-----",
1327 "-----END CERTIFICATE-----",
1328 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331 /*
1332 * Was PEM encoded
1333 */
1334 buflen -= use_len;
1335 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1337 return ret;
1338 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1339 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340
1341 /*
1342 * PEM header and footer were found
1343 */
1344 buflen -= use_len;
1345 buf += use_len;
1346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001348 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350
Paul Bakker5a5fa922014-09-26 14:53:04 +02001351 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362 /*
1363 * Quit parsing on a memory error
1364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1366 return ret;
1367 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372
1373 total_failed++;
1374 continue;
1375 }
1376
1377 success = 1;
1378 }
1379 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (success) {
1382 return total_failed;
1383 } else if (first_error) {
1384 return first_error;
1385 } else {
1386 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1387 }
Janos Follath98e28a72016-05-31 14:03:54 +01001388#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389}
1390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001392/*
1393 * Load one or more certificates and add them to the chained list
1394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396{
Janos Follath865b3eb2019-12-16 11:46:15 +00001397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001398 size_t n;
1399 unsigned char *buf;
1400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1402 return ret;
1403 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 mbedtls_platform_zeroize(buf, n);
1408 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411}
1412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414{
1415 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001416#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417 int w_ret;
1418 WCHAR szDir[MAX_PATH];
1419 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001420 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422
Paul Bakker9af723c2014-05-01 13:03:14 +02001423 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424 HANDLE hFind;
1425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (len > MAX_PATH - 3) {
1427 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1428 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 memset(szDir, 0, sizeof(szDir));
1431 memset(filename, 0, MAX_PATH);
1432 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001433 filename[len++] = '\\';
1434 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435 filename[len++] = '*';
1436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1438 MAX_PATH - 3);
1439 if (w_ret == 0) {
1440 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1441 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 hFind = FindFirstFileW(szDir, &file_data);
1444 if (hFind == INVALID_HANDLE_VALUE) {
1445 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1446 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001447
1448 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 do {
1450 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001457 -1,
1458 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 NULL, NULL);
1460 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001461 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1462 goto cleanup;
1463 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1466 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 }
1471 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476
Ron Eldor36d90422017-01-09 15:09:16 +02001477cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001479#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001480 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001481 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001483 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001484 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (dir == NULL) {
1488 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1489 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490
Ron Eldor63140682017-01-09 19:27:59 +02001491#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1493 closedir(dir);
1494 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001495 }
Ron Eldor63140682017-01-09 19:27:59 +02001496#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001501 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001504 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001505 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1506 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 } else if (stat(entry_name, &sb) == -1) {
1508 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001509 /* Broken symbolic link - ignore this entry.
1510 stat(2) will return this error for either (a) a dangling
1511 symlink or (b) a missing file.
1512 Given that we have just obtained the filename from readdir,
1513 assume that it does exist and therefore treat this as a
1514 dangling symlink. */
1515 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001517 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001518 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1519 goto cleanup;
1520 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521 }
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526
1527 // Ignore parse errors
1528 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1530 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001536
1537cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001539
Ron Eldor63140682017-01-09 19:27:59 +02001540#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 }
Ron Eldor63140682017-01-09 19:27:59 +02001544#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001545
Paul Bakkerbe089b02013-10-14 15:51:50 +02001546#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551
Peter Kolbus9a969b62018-12-11 13:55:56 -06001552#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001553static int x509_info_ext_key_usage(char **buf, size_t *size,
1554 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001555{
Janos Follath865b3eb2019-12-16 11:46:15 +00001556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001557 const char *desc;
1558 size_t n = *size;
1559 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001561 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 while (cur != NULL) {
1564 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001565 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001569 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001570
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001571 sep = ", ";
1572
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001573 cur = cur->next;
1574 }
1575
1576 *size = n;
1577 *buf = p;
1578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001580}
1581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582static int x509_info_cert_policies(char **buf, size_t *size,
1583 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001584{
Janos Follath865b3eb2019-12-16 11:46:15 +00001585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001586 const char *desc;
1587 size_t n = *size;
1588 char *p = *buf;
1589 const mbedtls_x509_sequence *cur = certificate_policies;
1590 const char *sep = "";
1591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 while (cur != NULL) {
1593 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001594 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001598 MBEDTLS_X509_SAFE_SNPRINTF;
1599
1600 sep = ", ";
1601
1602 cur = cur->next;
1603 }
1604
1605 *size = n;
1606 *buf = p;
1607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001609}
1610
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611/*
1612 * Return an informational string about the certificate.
1613 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001614#define BEFORE_COLON 18
1615#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001616int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1617 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618{
Janos Follath865b3eb2019-12-16 11:46:15 +00001619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 size_t n;
1621 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622 char key_size_str[BEFORE_COLON];
1623
1624 p = buf;
1625 n = size;
1626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (NULL == crt) {
1628 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001629 MBEDTLS_X509_SAFE_SNPRINTF;
1630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001632 }
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1635 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001636 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1638 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001639 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001642 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001645 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001647 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001650 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001652 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1655 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1656 crt->valid_from.year, crt->valid_from.mon,
1657 crt->valid_from.day, crt->valid_from.hour,
1658 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001659 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1662 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1663 crt->valid_to.year, crt->valid_to.mon,
1664 crt->valid_to.day, crt->valid_to.hour,
1665 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001666 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001669 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1672 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001673 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001675 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1677 mbedtls_pk_get_name(&crt->pk))) != 0) {
1678 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679 }
1680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1682 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001683 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001685 /*
1686 * Optional extensions
1687 */
1688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1690 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1691 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001692 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if (crt->max_pathlen > 0) {
1695 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001696 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001697 }
1698 }
1699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1701 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001702 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001703
Przemek Stekiel21c37282023-01-16 08:47:49 +01001704 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1705 &crt->subject_alt_names,
1706 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 return ret;
1708 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001709 }
1710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1712 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001713 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001714
Przemek Stekiel21c37282023-01-16 08:47:49 +01001715 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 return ret;
1717 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001718 }
1719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1721 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001722 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001723
Przemek Stekiel21c37282023-01-16 08:47:49 +01001724 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 return ret;
1726 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001727 }
1728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1730 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001731 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 if ((ret = x509_info_ext_key_usage(&p, &n,
1734 &crt->ext_key_usage)) != 0) {
1735 return ret;
1736 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001737 }
1738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1740 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001741 MBEDTLS_X509_SAFE_SNPRINTF;
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 if ((ret = x509_info_cert_policies(&p, &n,
1744 &crt->certificate_policies)) != 0) {
1745 return ret;
1746 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001747 }
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001750 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753}
1754
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001755struct x509_crt_verify_string {
1756 int code;
1757 const char *string;
1758};
1759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001761static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001762 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001763 { 0, NULL }
1764};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001765#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1768 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001769{
Janos Follath865b3eb2019-12-16 11:46:15 +00001770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001771 const struct x509_crt_verify_string *cur;
1772 char *p = buf;
1773 size_t n = size;
1774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1776 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001777 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001781 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001782 flags ^= cur->code;
1783 }
1784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (flags != 0) {
1786 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1787 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001788 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001789 }
1790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001792}
Hanno Becker612a2f12020-10-09 09:19:39 +01001793#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1796 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001797{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001798 unsigned int usage_must, usage_may;
1799 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1803 return 0;
1804 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001805
1806 usage_must = usage & ~may_mask;
1807
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1809 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1810 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001811
1812 usage_may = usage & may_mask;
1813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1815 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1816 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001819}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1822 const char *usage_oid,
1823 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001824{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001826
1827 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1829 return 0;
1830 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001831
1832 /*
1833 * Look for the requested usage (or wildcard ANY) in our list
1834 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 if (cur_oid->len == usage_len &&
1839 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1840 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001841 }
1842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1844 return 0;
1845 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001846 }
1847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001849}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001852/*
1853 * Return 1 if the certificate is revoked, or 0 otherwise.
1854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001855int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001856{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 while (cur != NULL && cur->serial.len != 0) {
1860 if (crt->serial.len == cur->serial.len &&
1861 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1862 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001863 }
1864
1865 cur = cur->next;
1866 }
1867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001869}
1870
1871/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001872 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02001873 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001874 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001875static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
1876 mbedtls_x509_crl *crl_list,
1877 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001878{
1879 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001880 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01001881#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01001882 psa_algorithm_t psa_algorithm;
1883#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01001885#endif /* MBEDTLS_USE_PSA_CRYPTO */
1886 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 if (ca == NULL) {
1889 return flags;
1890 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 while (crl_list != NULL) {
1893 if (crl_list->version == 0 ||
1894 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001895 crl_list = crl_list->next;
1896 continue;
1897 }
1898
1899 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001900 * Check if the CA is configured to sign CRLs
1901 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (mbedtls_x509_crt_check_key_usage(ca,
1903 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001905 break;
1906 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001907
1908 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909 * Check if CRL is correctly signed by the trusted CA
1910 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001912 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001916 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001918
pespacek7599a772022-02-07 14:40:55 +01001919#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
1921 if (psa_hash_compute(psa_algorithm,
1922 crl_list->tbs.p,
1923 crl_list->tbs.len,
1924 hash,
1925 sizeof(hash),
1926 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01001927 /* Note: this can't happen except after an internal error */
1928 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
1929 break;
1930 }
pespacek7599a772022-02-07 14:40:55 +01001931#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
1933 hash_length = mbedtls_md_get_size(md_info);
1934 if (mbedtls_md(md_info,
1935 crl_list->tbs.p,
1936 crl_list->tbs.len,
1937 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001938 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001939 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001940 break;
1941 }
pespaceka7a64692022-02-14 15:18:43 +01001942#endif /* MBEDTLS_USE_PSA_CRYPTO */
1943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001945 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1949 crl_list->sig_md, hash, hash_length,
1950 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001952 break;
1953 }
1954
1955 /*
1956 * Check for validity of CRL (Do not drop out)
1957 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001963 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001965
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001966 /*
1967 * Check if certificate is revoked
1968 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001971 break;
1972 }
1973
1974 crl_list = crl_list->next;
1975 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001980
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001981/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001982 * Check the signature of a certificate by its parent
1983 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001984static int x509_crt_check_signature(const mbedtls_x509_crt *child,
1985 mbedtls_x509_crt *parent,
1986 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001987{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04001988 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02001989 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04001990#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1991 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 md_info = mbedtls_md_info_from_type(child->sig_md);
1993 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05001994
Andrzej Kurekd4a65532018-10-31 06:18:39 -04001995 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
1997 return -1;
1998 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04001999#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002001 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 status = psa_hash_compute(hash_alg,
2004 child->tbs.p,
2005 child->tbs.len,
2006 hash,
2007 sizeof(hash),
2008 &hash_len);
2009 if (status != PSA_SUCCESS) {
2010 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002011 }
2012
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002013#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002014 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2016 return -1;
2017 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002018
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002019#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2021 return mbedtls_pk_verify_restartable(&parent->pk,
2022 child->sig_md, hash, hash_len,
2023 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002024 }
2025#else
2026 (void) rs_ctx;
2027#endif
2028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2030 child->sig_md, hash, hash_len,
2031 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002032}
2033
2034/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002035 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2036 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002037 *
2038 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2041 const mbedtls_x509_crt *parent,
2042 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002043{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002044 int need_ca_bit;
2045
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002046 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2048 return -1;
2049 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002050
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002051 /* Parent must have the basicConstraints CA bit set as a general rule */
2052 need_ca_bit = 1;
2053
2054 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002056 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002057 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 if (need_ca_bit && !parent->ca_istrue) {
2060 return -1;
2061 }
2062
2063 if (need_ca_bit &&
2064 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2065 return -1;
2066 }
2067
2068 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002069}
2070
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002071/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002072 * Find a suitable parent for child in candidates, or return NULL.
2073 *
2074 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002075 * 1. subject name matches child's issuer
2076 * 2. if necessary, the CA bit is set and key usage allows signing certs
2077 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002078 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002079 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002080 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002081 * If there's a suitable candidate which is also time-valid, return the first
2082 * such. Otherwise, return the first suitable candidate (or NULL if there is
2083 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002084 *
2085 * The rationale for this rule is that someone could have a list of trusted
2086 * roots with two versions on the same root with different validity periods.
2087 * (At least one user reported having such a list and wanted it to just work.)
2088 * The reason we don't just require time-validity is that generally there is
2089 * only one version, and if it's expired we want the flags to state that
2090 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002091 *
2092 * The rationale for rule 3 (signature for trusted roots) is that users might
2093 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002094 * way we select the correct one is by checking the signature (as we don't
2095 * rely on key identifier extensions). (This is one way users might choose to
2096 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002097 *
2098 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002099 * - [in] child: certificate for which we're looking for a parent
2100 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002101 * - [out] r_parent: parent found (or NULL)
2102 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002103 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2104 * of the chain, 0 otherwise
2105 * - [in] path_cnt: number of intermediates seen so far
2106 * - [in] self_cnt: number of self-signed intermediates seen so far
2107 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002108 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002109 *
2110 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002111 * - 0 on success
2112 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002113 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002114static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 mbedtls_x509_crt *child,
2116 mbedtls_x509_crt *candidates,
2117 mbedtls_x509_crt **r_parent,
2118 int *r_signature_is_good,
2119 int top,
2120 unsigned path_cnt,
2121 unsigned self_cnt,
2122 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002123{
Janos Follath865b3eb2019-12-16 11:46:15 +00002124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002125 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002126 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002127
2128#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002129 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002131 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002132 parent = rs_ctx->parent;
2133 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002134 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002135
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002136 /* clear saved state */
2137 rs_ctx->parent = NULL;
2138 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002139 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002140
2141 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002142 goto check_signature;
2143 }
2144#endif
2145
2146 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002147 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002150 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002152 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002154
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002155 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (parent->max_pathlen > 0 &&
2157 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002158 continue;
2159 }
2160
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002161 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002162#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2163check_signature:
2164#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002166
2167#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002169 /* save state */
2170 rs_ctx->parent = parent;
2171 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002172 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002175 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002176#else
2177 (void) ret;
2178#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002179
2180 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002182 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002184
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002185 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2187 mbedtls_x509_time_is_future(&parent->valid_from)) {
2188 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002189 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002190 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002191 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002192
2193 continue;
2194 }
2195
Andy Gross1f627142019-01-30 10:25:53 -06002196 *r_parent = parent;
2197 *r_signature_is_good = signature_is_good;
2198
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002199 break;
2200 }
2201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002203 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002204 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002205 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002208}
2209
2210/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002211 * Find a parent in trusted CAs or the provided chain, or return NULL.
2212 *
2213 * Searches in trusted CAs first, and return the first suitable parent found
2214 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002215 *
2216 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002217 * - [in] child: certificate for which we're looking for a parent, followed
2218 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002219 * - [in] trust_ca: list of locally trusted certificates
2220 * - [out] parent: parent found (or NULL)
2221 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2222 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2223 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2224 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002225 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002226 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002227 *
2228 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002229 * - 0 on success
2230 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002231 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002232static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 mbedtls_x509_crt *child,
2234 mbedtls_x509_crt *trust_ca,
2235 mbedtls_x509_crt **parent,
2236 int *parent_is_trusted,
2237 int *signature_is_good,
2238 unsigned path_cnt,
2239 unsigned self_cnt,
2240 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002241{
Janos Follath865b3eb2019-12-16 11:46:15 +00002242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002243 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002244
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002245 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002246
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002247#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002248 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002250 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002251 rs_ctx->parent_is_trusted = -1;
2252 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002253#endif
2254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002256 search_list = *parent_is_trusted ? trust_ca : child->next;
2257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 ret = x509_crt_find_parent_in(child, search_list,
2259 parent, signature_is_good,
2260 *parent_is_trusted,
2261 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002262
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002263#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002265 /* save state */
2266 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002268 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002269#else
2270 (void) ret;
2271#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002272
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002273 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002275 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002277
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002278 /* prepare second iteration */
2279 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002280 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002281
2282 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002284 *parent_is_trusted = 0;
2285 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002286 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002289}
2290
2291/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002292 * Check if an end-entity certificate is locally trusted
2293 *
2294 * Currently we require such certificates to be self-signed (actually only
2295 * check for self-issued as self-signatures are not checked)
2296 */
2297static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 mbedtls_x509_crt *crt,
2299 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002300{
2301 mbedtls_x509_crt *cur;
2302
2303 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2305 return -1;
2306 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002307
2308 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2310 if (crt->raw.len == cur->raw.len &&
2311 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2312 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002313 }
2314 }
2315
2316 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002318}
2319
2320/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002321 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002322 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002323 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2324 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002325 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002326 * such that every cert in the chain is a child of the next one,
2327 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002328 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002329 * Verify that chain and return it with flags for all issues found.
2330 *
2331 * Special cases:
2332 * - EE == Rj -> return a one-element list containing it
2333 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2334 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002335 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002336 * Tests for (aspects of) this function should include at least:
2337 * - trusted EE
2338 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002339 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002340 * - if relevant: EE untrusted
2341 * - if relevant: EE -> intermediate, untrusted
2342 * with the aspect under test checked at each relevant level (EE, int, root).
2343 * For some aspects longer chains are required, but usually length 2 is
2344 * enough (but length 1 is not in general).
2345 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002346 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002347 * - [in] crt: the cert list EE, C1, ..., Cn
2348 * - [in] trust_ca: the trusted list R1, ..., Rp
2349 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002350 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002351 * Only valid when return value is 0, may contain garbage otherwise!
2352 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002353 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002354 *
2355 * Return value:
2356 * - non-zero if the chain could not be fully built and examined
2357 * - 0 is the chain was successfully built and examined,
2358 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002359 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002360static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 mbedtls_x509_crt *crt,
2362 mbedtls_x509_crt *trust_ca,
2363 mbedtls_x509_crl *ca_crl,
2364 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2365 void *p_ca_cb,
2366 const mbedtls_x509_crt_profile *profile,
2367 mbedtls_x509_crt_verify_chain *ver_chain,
2368 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002370 /* Don't initialize any of those variables here, so that the compiler can
2371 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002373 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002374 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002375 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002376 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002377 int parent_is_trusted;
2378 int child_is_trusted;
2379 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002380 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002381 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002382
2383#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2384 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002386 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002387 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002388 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002389
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002390 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002391 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002392 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002393 flags = &cur->flags;
2394
2395 goto find_parent;
2396 }
2397#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002398
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002399 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002400 self_cnt = 0;
2401 parent_is_trusted = 0;
2402 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002405 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002406 cur = &ver_chain->items[ver_chain->len];
2407 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002408 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002409 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002410 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002411
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002412 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002414 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002418 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002420
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002421 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 if (child_is_trusted) {
2423 return 0;
2424 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002425
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002426 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002428 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002432 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002434
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002435 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (ver_chain->len == 1 &&
2437 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2438 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002439 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002440
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002441#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2442find_parent:
2443#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002444
2445 /* Obtain list of potential trusted signers from CA callback,
2446 * or use statically provided list. */
2447#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (f_ca_cb != NULL) {
2449 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2450 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002451 ver_chain->trust_ca_cb_result = NULL;
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2454 if (ret != 0) {
2455 return MBEDTLS_ERR_X509_FATAL_ERROR;
2456 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002457
2458 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002460#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2461 {
2462 ((void) f_ca_cb);
2463 ((void) p_ca_cb);
2464 cur_trust_ca = trust_ca;
2465 }
2466
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002467 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2469 &parent_is_trusted, &signature_is_good,
2470 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002471
2472#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002474 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002475 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002476 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002477 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002480 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002481#else
2482 (void) ret;
2483#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002484
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002485 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002487 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002489 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002490
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002491 /* Count intermediate self-issued (not necessarily self-signed) certs.
2492 * These can occur with some strategies for key rollover, see [SIRO],
2493 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (ver_chain->len != 1 &&
2495 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002496 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002497 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002498
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002499 /* path_cnt is 0 for the first intermediate CA,
2500 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 if (!parent_is_trusted &&
2502 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002503 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002505 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002506
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002507 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002509 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002511
2512 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002514 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002518 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002520#else
2521 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002522#endif
2523
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002524 /* prepare for next iteration */
2525 child = parent;
2526 parent = NULL;
2527 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002528 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002529 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002530}
2531
Glenn Straussc26bd762022-10-23 19:48:18 -04002532#ifdef _WIN32
2533#ifdef _MSC_VER
2534#pragma comment(lib, "ws2_32.lib")
2535#include <winsock2.h>
2536#include <ws2tcpip.h>
2537#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2538#include <winsock2.h>
2539#include <ws2tcpip.h>
2540#endif
2541#elif defined(__sun)
2542/* Solaris requires -lsocket -lnsl for inet_pton() */
2543#elif defined(__has_include)
2544#if __has_include(<sys/socket.h>)
2545#include <sys/socket.h>
2546#endif
2547#if __has_include(<arpa/inet.h>)
2548#include <arpa/inet.h>
2549#endif
2550#endif
2551
2552/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2553 * the platform inet_pton() or a local implementation (below). The local
2554 * implementation may be used even in cases where the platform provides
2555 * inet_pton(), e.g. when there are different includes required and/or the
2556 * platform implementation requires dependencies on additional libraries.
2557 * Specifically, Windows requires custom includes and additional link
2558 * dependencies, and Solaris requires additional link dependencies.
2559 * Also, as a coarse heuristic, use the local implementation if the compiler
2560 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002561 * provided by headers included (or not) via __has_include() above.
2562 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2563 * despite having a platform that has inet_pton. */
2564#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2565/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002566static int x509_inet_pton_ipv4(const char *src, void *dst);
2567
2568#define li_cton(c, n) \
2569 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2570
2571static int x509_inet_pton_ipv6(const char *src, void *dst)
2572{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002573 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002574 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002575 uint16_t addr[8];
2576 do {
2577 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002578 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002579 for (uint8_t digit; num_digits < 4; num_digits++) {
2580 if (li_cton(*p, digit) == 0) {
2581 break;
2582 }
2583 group = (group << 4) | digit;
2584 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002585 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002586 if (num_digits != 0) {
Andrzej Kurek0d578962023-04-13 09:09:56 -04002587 addr[nonzero_groups++] = MBEDTLS_IS_BIG_ENDIAN ? group :
2588 (group << 8) | (group >> 8);
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002589 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002590 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002591 } else if (*p == '.') {
2592 /* Don't accept IPv4 too early or late */
2593 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2594 nonzero_groups >= 7) {
2595 break;
2596 }
2597
2598 /* Walk back to prior ':', then parse as IPv4-mapped */
2599 int steps = 4;
2600 do {
2601 p--;
2602 steps--;
2603 } while (*p != ':' && steps > 0);
2604
2605 if (*p != ':') {
2606 break;
2607 }
2608 p++;
2609 nonzero_groups--;
2610 if (x509_inet_pton_ipv4((const char *) p,
2611 addr + nonzero_groups) != 0) {
2612 break;
2613 }
2614
Andrzej Kurek0d578962023-04-13 09:09:56 -04002615 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002616 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002617 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002618 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002619 return -1;
2620 }
2621 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002622 /* Don't accept a second zero group or an invalid delimiter */
2623 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002624 return -1;
2625 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002626 zero_group_start = nonzero_groups;
2627
2628 /* Accept a zero group at start, but it has to be a double colon */
2629 if (zero_group_start == 0 && *++p != ':') {
2630 return -1;
2631 }
2632
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002633 if (p[1] == '\0') {
2634 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002635 break;
2636 }
2637 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002638 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002639 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002640
2641 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002642 return -1;
2643 }
2644
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002645 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002646 if (nonzero_groups > 6) {
2647 return -1;
2648 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002649 int zero_groups = 8 - nonzero_groups;
2650 int groups_after_zero = nonzero_groups - zero_group_start;
2651
2652 /* Move the non-zero part to after the zeroes */
2653 if (groups_after_zero) {
2654 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002655 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002656 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002657 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002658 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002659 } else {
2660 if (nonzero_groups != 8) {
2661 return -1;
2662 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002663 }
2664 memcpy(dst, addr, sizeof(addr));
2665 return 0;
2666}
2667
2668static int x509_inet_pton_ipv4(const char *src, void *dst)
2669{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002670 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002671 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002672 uint8_t digit, num_digits = 0;
2673 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002674 uint16_t octet;
2675
Glenn Strauss416c2952022-10-24 23:00:02 -04002676 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002677 octet = num_digits = 0;
2678 do {
2679 digit = *p - '0';
2680 if (digit > 9) {
2681 break;
2682 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002683
2684 /* Don't allow leading zeroes. These might mean octal format,
2685 * which this implementation does not support. */
2686 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002687 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002688 }
2689
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002690 octet = octet * 10 + digit;
2691 num_digits++;
2692 p++;
2693 } while (num_digits < 3);
2694
2695 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002696 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002697 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002698 *res++ = (uint8_t) octet;
2699 num_octets++;
2700 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002701 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002702}
Glenn Straussc26bd762022-10-23 19:48:18 -04002703
2704#else
2705
2706static int x509_inet_pton_ipv6(const char *src, void *dst)
2707{
2708 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2709}
2710
2711static int x509_inet_pton_ipv4(const char *src, void *dst)
2712{
2713 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2714}
2715
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002716#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002717
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002718MBEDTLS_STATIC_TESTABLE
2719size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002720{
2721 return strchr(cn, ':') == NULL
2722 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2723 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2724}
2725
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002726/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002727 * Check for CN match
2728 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002729static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2730 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002731{
2732 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if (name->len == cn_len &&
2734 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2735 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002736 }
2737
2738 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 if (x509_check_wildcard(cn, name) == 0) {
2740 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002741 }
2742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002744}
2745
Glenn Straussc26bd762022-10-23 19:48:18 -04002746static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2747 const char *cn, size_t cn_len)
2748{
2749 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002750 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002751 if (cn_len == 0) {
2752 return -1;
2753 }
2754
2755 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2756 const unsigned char san_type = (unsigned char) cur->buf.tag &
2757 MBEDTLS_ASN1_TAG_VALUE_MASK;
2758 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2759 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2760 return 0;
2761 }
2762 }
2763
2764 return -1;
2765}
2766
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002767/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002768 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2769 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002770static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002772{
Glenn Straussc26bd762022-10-23 19:48:18 -04002773 int san_ip = 0;
2774 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2775 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
2776 case MBEDTLS_X509_SAN_DNS_NAME: /* dNSName */
2777 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2778 return 0;
2779 }
2780 break;
2781 case MBEDTLS_X509_SAN_IP_ADDRESS: /* iPAddress */
2782 san_ip = 1;
2783 break;
2784 /* (We may handle other types here later.) */
2785 default: /* Unrecognized type */
2786 break;
2787 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002789
Glenn Straussc26bd762022-10-23 19:48:18 -04002790 return san_ip ? x509_crt_check_san_ip(san, cn, cn_len) : -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002791}
2792
2793/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002794 * Verify the requested CN - only call this if cn is not NULL!
2795 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2797 const char *cn,
2798 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002799{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002800 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002804 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2805 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 }
2807 } else {
2808 for (name = &crt->subject; name != NULL; name = name->next) {
2809 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2810 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002811 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 }
2813 }
2814
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002815 }
Glenn Straussc26bd762022-10-23 19:48:18 -04002816
2817 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002818}
2819
2820/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002821 * Merge the flags for all certs in the chain, after calling callback
2822 */
2823static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 uint32_t *flags,
2825 const mbedtls_x509_crt_verify_chain *ver_chain,
2826 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2827 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002828{
Janos Follath865b3eb2019-12-16 11:46:15 +00002829 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002830 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002831 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002832 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002833
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002835 cur = &ver_chain->items[i-1];
2836 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 if (NULL != f_vrfy) {
2839 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
2840 return ret;
2841 }
2842 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002843
2844 *flags |= cur_flags;
2845 }
2846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002848}
2849
2850/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002851 * Verify the certificate validity, with profile, restartable version
2852 *
2853 * This function:
2854 * - checks the requested CN (if any)
2855 * - checks the type and size of the EE cert's key,
2856 * as that isn't done as part of chain building/verification currently
2857 * - builds and verifies the chain
2858 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00002859 *
2860 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
2861 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
2862 * verification routine to search for trusted signers, and CRLs will
2863 * be disabled. Otherwise, `trust_ca` will be used as the static list
2864 * of trusted signers, and `ca_crl` will be use as the static list
2865 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002867static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
2868 mbedtls_x509_crt *trust_ca,
2869 mbedtls_x509_crl *ca_crl,
2870 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2871 void *p_ca_cb,
2872 const mbedtls_x509_crt_profile *profile,
2873 const char *cn, uint32_t *flags,
2874 int (*f_vrfy)(void *,
2875 mbedtls_x509_crt *,
2876 int,
2877 uint32_t *),
2878 void *p_vrfy,
2879 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002880{
Janos Follath865b3eb2019-12-16 11:46:15 +00002881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002882 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002883 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002884 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002885
2886 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002887 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002891 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2892 goto exit;
2893 }
2894
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002895 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (cn != NULL) {
2897 x509_crt_verify_name(crt, cn, &ee_flags);
2898 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002899
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002900 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002904 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002908 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002910
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002911 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
2913 f_ca_cb, p_ca_cb, profile,
2914 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002917 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002919
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002920 /* Merge end-entity flags */
2921 ver_chain.items[0].flags |= ee_flags;
2922
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002923 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002925
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002926exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00002927
2928#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
2930 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002931 ver_chain.trust_ca_cb_result = NULL;
2932#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2933
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002934#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
2936 mbedtls_x509_crt_restart_free(rs_ctx);
2937 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002938#endif
2939
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002940 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2941 * the SSL module for authmode optional, but non-zero return from the
2942 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002944 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002945 }
2946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 if (ret != 0) {
2948 *flags = (uint32_t) -1;
2949 return ret;
2950 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 if (*flags != 0) {
2953 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
2954 }
2955
2956 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002957}
2958
Hanno Becker3116fb32019-03-28 13:34:42 +00002959
2960/*
2961 * Verify the certificate validity (default profile, not restartable)
2962 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002963int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
2964 mbedtls_x509_crt *trust_ca,
2965 mbedtls_x509_crl *ca_crl,
2966 const char *cn, uint32_t *flags,
2967 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2968 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002969{
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2971 NULL, NULL,
2972 &mbedtls_x509_crt_profile_default,
2973 cn, flags,
2974 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002975}
2976
2977/*
2978 * Verify the certificate validity (user-chosen profile, not restartable)
2979 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002980int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
2981 mbedtls_x509_crt *trust_ca,
2982 mbedtls_x509_crl *ca_crl,
2983 const mbedtls_x509_crt_profile *profile,
2984 const char *cn, uint32_t *flags,
2985 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2986 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002987{
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2989 NULL, NULL,
2990 profile, cn, flags,
2991 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002992}
2993
2994#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2995/*
2996 * Verify the certificate validity (user-chosen profile, CA callback,
2997 * not restartable).
2998 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3000 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3001 void *p_ca_cb,
3002 const mbedtls_x509_crt_profile *profile,
3003 const char *cn, uint32_t *flags,
3004 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3005 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003006{
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3008 f_ca_cb, p_ca_cb,
3009 profile, cn, flags,
3010 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003011}
3012#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3013
Gilles Peskine449bd832023-01-11 14:50:10 +01003014int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3015 mbedtls_x509_crt *trust_ca,
3016 mbedtls_x509_crl *ca_crl,
3017 const mbedtls_x509_crt_profile *profile,
3018 const char *cn, uint32_t *flags,
3019 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3020 void *p_vrfy,
3021 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003022{
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3024 NULL, NULL,
3025 profile, cn, flags,
3026 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003027}
3028
3029
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003030/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003031 * Initialize a certificate chain
3032 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003033void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003034{
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003036}
3037
3038/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003039 * Unallocate all certificate data
3040 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003041void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 mbedtls_x509_crt *cert_cur = crt;
3044 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 while (cert_cur != NULL) {
3047 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003051#endif
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3054 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3055 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3056 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3057 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3060 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3061 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003062 }
3063
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003064 cert_prv = cert_cur;
3065 cert_cur = cert_cur->next;
3066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3068 if (cert_prv != crt) {
3069 mbedtls_free(cert_prv);
3070 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003071 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003072}
3073
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003074#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3075/*
3076 * Initialize a restart context
3077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003078void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003079{
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003081
3082 ctx->parent = NULL;
3083 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003084 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003085
3086 ctx->parent_is_trusted = -1;
3087
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003088 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003089 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003091}
3092
3093/*
3094 * Free the components of a restart context
3095 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003096void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003097{
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003099 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 mbedtls_pk_restart_free(&ctx->pk);
3103 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003104}
3105#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107#endif /* MBEDTLS_X509_CRT_PARSE_C */