blob: a784e634dd71dbc84a670e2e58fda74111369e78 [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"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020050#include "md_psa.h"
pespacek7599a772022-02-07 14:40:55 +010051#endif /* MBEDTLS_USE_PSA_CRYPTO */
Glenn Strauss6f545ac2022-10-25 15:02:14 -040052#include "x509_invasive.h"
Valerio Setti3f00b842023-05-15 12:57:06 +020053#include "pk_internal.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010059#endif
60
Daniel Axtensf0710242020-05-28 11:43:41 +100061#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010062#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Glenn Straussc26bd762022-10-23 19:48:18 -040063#define WIN32_LEAN_AND_MEAN
64#ifndef _WIN32_WINNT
65#define _WIN32_WINNT 0x0600
66#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100071#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000074#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020075#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#include <sys/types.h>
77#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020078#if defined(__MBED__)
79#include <platform/mbed_retarget.h>
80#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020082#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060083#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000084#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#endif
86
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020087/*
88 * Item in a verification chain: cert and flags for it
89 */
90typedef struct {
91 mbedtls_x509_crt *crt;
92 uint32_t flags;
93} x509_crt_verify_chain_item;
94
95/*
96 * Max size of verification chain: end-entity + intermediates + trusted root
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020099
Gilles Peskineffb92da2021-06-02 00:03:26 +0200100/* Default profile. Do not remove items unless there are serious security
101 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200102const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
103{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200104 /* Hashes from SHA-256 and above. Note that this selection
105 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
107 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
108 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200109 0xFFFFFFF, /* Any PK alg */
Valerio Settid4a5d462023-04-05 18:19:01 +0200110#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200111 /* Curves at or above 128-bit security level. Note that this selection
112 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
114 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
115 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
116 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
117 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
118 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200119 0,
Valerio Settid4a5d462023-04-05 18:19:01 +0200120#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200121 0,
Valerio Settid4a5d462023-04-05 18:19:01 +0200122#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200123 2048,
124};
125
Gilles Peskineffb92da2021-06-02 00:03:26 +0200126/* Next-generation profile. Currently identical to the default, but may
127 * be tightened at any time. */
128const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200129{
130 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
132 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
133 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200134 0xFFFFFFF, /* Any PK alg */
135#if defined(MBEDTLS_ECP_C)
136 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
138 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
139 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
140 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
141 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
142 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
143 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200144#else
145 0,
146#endif
147 2048,
148};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200149
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200150/*
151 * NSA Suite B Profile
152 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200153const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
154{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
157 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200158 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
160 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Settid4a5d462023-04-05 18:19:01 +0200161#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200162 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
164 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Settid4a5d462023-04-05 18:19:01 +0200165#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200166 0,
Valerio Settid4a5d462023-04-05 18:19:01 +0200167#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200168 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200169};
170
171/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200172 * Empty / all-forbidden profile
173 */
174const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
175{
176 0,
177 0,
178 0,
179 (uint32_t) -1,
180};
181
182/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200184 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
187 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200188{
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (md_alg == MBEDTLS_MD_NONE) {
190 return -1;
191 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
194 return 0;
195 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200198}
199
200/*
201 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200202 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200203 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
205 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206{
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (pk_alg == MBEDTLS_PK_NONE) {
208 return -1;
209 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
212 return 0;
213 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200216}
217
218/*
219 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200220 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
223 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200224{
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200226
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200227#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
229 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
230 return 0;
231 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200234 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200235#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200236
Valerio Settid4a5d462023-04-05 18:19:01 +0200237#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200239 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Setti97207782023-05-18 18:59:06 +0200241 const mbedtls_ecp_group_id gid = mbedtls_pk_get_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (gid == MBEDTLS_ECP_DP_NONE) {
244 return -1;
245 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
248 return 0;
249 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200252 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200253#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200256}
257
258/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000259 * Like memcmp, but case-insensitive and always returns -1 if different
260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000262{
263 size_t i;
264 unsigned char diff;
265 const unsigned char *n1 = s1, *n2 = s2;
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000268 diff = n1[i] ^ n2[i];
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000271 continue;
272 }
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (diff == 32 &&
275 ((n1[i] >= 'a' && n1[i] <= 'z') ||
276 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
277 continue;
278 }
279
280 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000281 }
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000284}
285
286/*
287 * Return 0 if name matches wildcard, -1 otherwise
288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000290{
291 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000293
294 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
296 return -1;
297 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 for (i = 0; i < cn_len; ++i) {
300 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000301 cn_idx = i;
302 break;
303 }
304 }
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (cn_idx == 0) {
307 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000308 }
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (cn_len - cn_idx == name->len - 1 &&
311 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
312 return 0;
313 }
314
315 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000316}
317
318/*
319 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
320 * variations (but not all).
321 *
322 * Return 0 if equal, -1 otherwise.
323 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000325{
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000327 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 memcmp(a->p, b->p, b->len) == 0) {
329 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000330 }
331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
333 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000334 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 x509_memcasecmp(a->p, b->p, b->len) == 0) {
336 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000337 }
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000340}
341
342/*
343 * Compare two X.509 Names (aka rdnSequence).
344 *
345 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
346 * we sometimes return unequal when the full algorithm would return equal,
347 * but never the other way. (In particular, we don't do Unicode normalisation
348 * or space folding.)
349 *
350 * Return 0 if equal, -1 otherwise.
351 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000353{
354 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 while (a != NULL || b != NULL) {
356 if (a == NULL || b == NULL) {
357 return -1;
358 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000359
360 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000362 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
364 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000365 }
366
367 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (x509_string_cmp(&a->val, &b->val) != 0) {
369 return -1;
370 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000371
372 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (a->next_merged != b->next_merged) {
374 return -1;
375 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000376
377 a = a->next;
378 b = b->next;
379 }
380
381 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000383}
384
385/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200386 * Reset (init or clear) a verify_chain
387 */
388static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200390{
391 size_t i;
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200394 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000395 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200396 }
397
398 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000399
400#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
401 ver_chain->trust_ca_cb_result = NULL;
402#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200403}
404
405/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408static int x509_get_version(unsigned char **p,
409 const unsigned char *end,
410 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411{
Janos Follath865b3eb2019-12-16 11:46:15 +0000412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 size_t len;
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
416 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
417 0)) != 0) {
418 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421 }
422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 }
425
426 end = *p + len;
427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
429 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
430 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (*p != end) {
433 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
434 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
435 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438}
439
440/*
441 * Validity ::= SEQUENCE {
442 * notBefore Time,
443 * notAfter Time }
444 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445static int x509_get_dates(unsigned char **p,
446 const unsigned char *end,
447 mbedtls_x509_time *from,
448 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449{
Janos Follath865b3eb2019-12-16 11:46:15 +0000450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451 size_t len;
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
454 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
455 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
458 end = *p + len;
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
461 return ret;
462 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
465 return ret;
466 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (*p != end) {
469 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
470 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
471 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474}
475
476/*
477 * X.509 v2/v3 unique identifier (not parsed)
478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479static int x509_get_uid(unsigned char **p,
480 const unsigned char *end,
481 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482{
Janos Follath865b3eb2019-12-16 11:46:15 +0000483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (*p == end) {
486 return 0;
487 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
489 uid->tag = **p;
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
492 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
493 n)) != 0) {
494 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
495 return 0;
496 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499 }
500
501 uid->p = *p;
502 *p += uid->len;
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505}
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507static int x509_get_basic_constraints(unsigned char **p,
508 const unsigned char *end,
509 int *ca_istrue,
510 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511{
Janos Follath865b3eb2019-12-16 11:46:15 +0000512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513 size_t len;
514
515 /*
516 * BasicConstraints ::= SEQUENCE {
517 * cA BOOLEAN DEFAULT FALSE,
518 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
519 */
520 *ca_istrue = 0; /* DEFAULT FALSE */
521 *max_pathlen = 0; /* endless */
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
524 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
525 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (*p == end) {
529 return 0;
530 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
533 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
534 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
535 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (ret != 0) {
538 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
539 }
540
541 if (*ca_istrue != 0) {
542 *ca_istrue = 1;
543 }
544 }
545
546 if (*p == end) {
547 return 0;
548 }
549
550 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
551 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
552 }
553
554 if (*p != end) {
555 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
556 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
557 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
Andrzej Kurek16050742020-04-14 09:49:52 -0400559 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
560 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (*max_pathlen == INT_MAX) {
562 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
563 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
564 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400565
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566 (*max_pathlen)++;
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569}
570
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571/*
572 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
573 *
574 * KeyPurposeId ::= OBJECT IDENTIFIER
575 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100576static int x509_get_ext_key_usage(unsigned char **p,
577 const unsigned char *end,
578 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579{
Janos Follath865b3eb2019-12-16 11:46:15 +0000580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
583 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
584 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
586 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (ext_key_usage->buf.p == NULL) {
588 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
589 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
590 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593}
594
595/*
toth92ga41954d2021-02-12 16:11:17 +0100596 * SubjectKeyIdentifier ::= KeyIdentifier
597 *
598 * KeyIdentifier ::= OCTET STRING
599 */
600static int x509_get_subject_key_id(unsigned char **p,
601 const unsigned char *end,
602 mbedtls_x509_buf *subject_key_id)
603{
604 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
605 size_t len = 0u;
606
607 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
608 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100609 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100610 }
611
Przemek Stekiel61aed062023-05-08 11:14:36 +0200612 subject_key_id->len = len;
613 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
614 subject_key_id->p = *p;
615 *p += len;
616
toth92gd96027a2021-04-27 15:41:25 +0200617 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100618 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
619 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200620 }
621
toth92ga41954d2021-02-12 16:11:17 +0100622 return 0;
623}
624
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100625/*
toth92g8d435a02021-05-10 15:16:33 +0200626 * AuthorityKeyIdentifier ::= SEQUENCE {
627 * keyIdentifier [0] KeyIdentifier OPTIONAL,
628 * authorityCertIssuer [1] GeneralNames OPTIONAL,
629 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
630 *
631 * KeyIdentifier ::= OCTET STRING
632 */
633static int x509_get_authority_key_id(unsigned char **p,
634 unsigned char *end,
635 mbedtls_x509_authority *authority_key_id)
636{
637 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
638 size_t len = 0u;
639
640 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100641 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100642 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200643 }
644
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100645 if (*p + len != end) {
646 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
647 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
648 }
649
Przemek Stekieled9fb782023-05-03 16:27:25 +0200650 ret = mbedtls_asn1_get_tag(p, end, &len,
651 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
652
653 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200654 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200655 authority_key_id->keyIdentifier.len = len;
656 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200657 /* Setting tag of the keyIdentfier intentionally to 0x04.
658 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200659 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200660 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
661
662 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200663 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
664 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200665 }
666
667 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200668 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200669 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
670 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100671 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200672 /* authorityCertIssuer and authorityCertSerialNumber MUST both
673 be present or both be absent. At this point we expect to have both. */
674 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200675 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200676 /* "end" also includes the CertSerialNumber field so "len" shall be used */
677 ret = mbedtls_x509_get_subject_alt_name_ext(p,
678 (*p+len),
679 &authority_key_id->authorityCertIssuer);
680 if (ret != 0) {
681 return ret;
682 }
683
684 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
685 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
686 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
687 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
688 }
689 authority_key_id->authorityCertSerialNumber.len = len;
690 authority_key_id->authorityCertSerialNumber.p = *p;
691 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
692 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200693 }
694
695 if (*p != end) {
696 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
697 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
698 }
699
700 return 0;
701}
702
703/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200704 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
705 *
706 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
707 *
708 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
709 *
710 * PolicyInformation ::= SEQUENCE {
711 * policyIdentifier CertPolicyId,
712 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
713 * PolicyQualifierInfo OPTIONAL }
714 *
715 * CertPolicyId ::= OBJECT IDENTIFIER
716 *
717 * PolicyQualifierInfo ::= SEQUENCE {
718 * policyQualifierId PolicyQualifierId,
719 * qualifier ANY DEFINED BY policyQualifierId }
720 *
721 * -- policyQualifierIds for Internet policy qualifiers
722 *
723 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
724 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
725 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
726 *
727 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
728 *
729 * Qualifier ::= CHOICE {
730 * cPSuri CPSuri,
731 * userNotice UserNotice }
732 *
733 * CPSuri ::= IA5String
734 *
735 * UserNotice ::= SEQUENCE {
736 * noticeRef NoticeReference OPTIONAL,
737 * explicitText DisplayText OPTIONAL }
738 *
739 * NoticeReference ::= SEQUENCE {
740 * organization DisplayText,
741 * noticeNumbers SEQUENCE OF INTEGER }
742 *
743 * DisplayText ::= CHOICE {
744 * ia5String IA5String (SIZE (1..200)),
745 * visibleString VisibleString (SIZE (1..200)),
746 * bmpString BMPString (SIZE (1..200)),
747 * utf8String UTF8String (SIZE (1..200)) }
748 *
749 * NOTE: we only parse and use anyPolicy without qualifiers at this point
750 * as defined in RFC 5280.
751 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752static int x509_get_certificate_policies(unsigned char **p,
753 const unsigned char *end,
754 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200755{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300756 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200757 size_t len;
758 mbedtls_asn1_buf *buf;
759 mbedtls_asn1_sequence *cur = certificate_policies;
760
761 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 ret = mbedtls_asn1_get_tag(p, end, &len,
763 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
764 if (ret != 0) {
765 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
766 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (*p + len != end) {
769 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
770 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
771 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200772
773 /*
774 * Cannot be an empty sequence.
775 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (len == 0) {
777 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
778 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
779 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200782 mbedtls_x509_buf policy_oid;
783 const unsigned char *policy_end;
784
785 /*
786 * Get the policy sequence
787 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
789 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
790 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
791 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200792
793 policy_end = *p + len;
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
796 MBEDTLS_ASN1_OID)) != 0) {
797 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
798 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200799
800 policy_oid.tag = MBEDTLS_ASN1_OID;
801 policy_oid.len = len;
802 policy_oid.p = *p;
803
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300804 /*
805 * Only AnyPolicy is currently supported when enforcing policy.
806 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300808 /*
809 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200810 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300811 */
812 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
813 }
814
Ron Eldor74d9acc2019-03-21 14:00:03 +0200815 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (cur->buf.p != NULL) {
817 if (cur->next != NULL) {
818 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
819 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (cur->next == NULL) {
824 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
825 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
826 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200827
828 cur = cur->next;
829 }
830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200832 buf->tag = policy_oid.tag;
833 buf->p = policy_oid.p;
834 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300835
836 *p += len;
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 /*
839 * If there is an optional qualifier, then *p < policy_end
840 * Check the Qualifier len to verify it doesn't exceed policy_end.
841 */
842 if (*p < policy_end) {
843 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
844 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
845 0) {
846 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
847 }
Ron Eldor08063792019-05-13 16:38:39 +0300848 /*
849 * Skip the optional policy qualifiers.
850 */
851 *p += len;
852 }
853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (*p != policy_end) {
855 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
856 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
857 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200858 }
859
860 /* Set final sequence entry's next pointer to NULL */
861 cur->next = NULL;
862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (*p != end) {
864 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
865 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
866 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200869}
870
871/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872 * X.509 v3 extensions
873 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875static int x509_get_crt_ext(unsigned char **p,
876 const unsigned char *end,
877 mbedtls_x509_crt *crt,
878 mbedtls_x509_crt_ext_cb_t cb,
879 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880{
Janos Follath865b3eb2019-12-16 11:46:15 +0000881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200883 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (*p == end) {
886 return 0;
887 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
890 return ret;
891 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892
Hanno Becker12f62fb2019-02-12 17:22:36 +0000893 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 /*
896 * Extension ::= SEQUENCE {
897 * extnID OBJECT IDENTIFIER,
898 * critical BOOLEAN DEFAULT FALSE,
899 * extnValue OCTET STRING }
900 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 int is_critical = 0; /* DEFAULT FALSE */
903 int ext_type = 0;
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
906 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
907 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
908 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909
910 end_ext_data = *p + len;
911
912 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
914 MBEDTLS_ASN1_OID)) != 0) {
915 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
916 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917
k-stachowiak463928a2018-07-24 12:50:59 +0200918 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919 extn_oid.p = *p;
920 *p += extn_oid.len;
921
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
924 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
925 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
926 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
928 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
930 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
931 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
932 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200934 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 end_ext_octet = *p + len;
936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (end_ext_octet != end_ext_data) {
938 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
939 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
940 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941
942 /*
943 * Detect supported extensions
944 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200948 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (cb != NULL) {
950 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
951 if (ret != 0 && is_critical) {
952 return ret;
953 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200954 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200955 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200956 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200957
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 /* No parser found, skip extension */
959 *p = end_ext_octet;
960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
964 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966 continue;
967 }
968
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100969 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if ((crt->ext_types & ext_type) != 0) {
971 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
972 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100973
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 crt->ext_types |= ext_type;
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 switch (ext_type) {
977 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
978 /* Parse basic constraints */
979 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
980 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
981 return ret;
982 }
983 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 case MBEDTLS_X509_EXT_KEY_USAGE:
986 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100987 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
988 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 return ret;
990 }
991 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
994 /* Parse extended key usage */
995 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
996 &crt->ext_key_usage)) != 0) {
997 return ret;
998 }
999 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000
toth92ga41954d2021-02-12 16:11:17 +01001001 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1002 /* Parse subject key identifier */
1003 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1004 &crt->subject_key_id)) != 0) {
1005 return ret;
1006 }
1007 break;
toth92g8d435a02021-05-10 15:16:33 +02001008
toth92ga41954d2021-02-12 16:11:17 +01001009 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1010 /* Parse authority key identifier */
1011 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1012 &crt->authority_key_id)) != 0) {
1013 return ret;
1014 }
1015 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001017 /* Parse subject alt name
1018 * SubjectAltName ::= GeneralNames
1019 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001020 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1021 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 return ret;
1023 }
1024 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1027 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001028 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1029 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return ret;
1031 }
1032 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1035 /* Parse certificate policies type */
1036 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1037 &crt->certificate_policies)) != 0) {
1038 /* Give the callback (if any) a chance to handle the extension
1039 * if it contains unsupported policies */
1040 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1041 cb(p_ctx, crt, &extn_oid, is_critical,
1042 start_ext_octet, end_ext_octet) == 0) {
1043 break;
1044 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (is_critical) {
1047 return ret;
1048 } else
1049 /*
1050 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1051 * cannot interpret or enforce the policy. However, it is up to
1052 * the user to choose how to enforce the policies,
1053 * unless the extension is critical.
1054 */
1055 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1056 return ret;
1057 }
1058 }
1059 break;
1060
1061 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001062 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 * If this is a non-critical extension, which the oid layer
1064 * supports, but there isn't an x509 parser for it,
1065 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (is_critical) {
1068 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1069 } else {
1070 *p = end_ext_octet;
1071 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 }
1073 }
1074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (*p != end) {
1076 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1077 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1078 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081}
1082
1083/*
1084 * Parse and fill a single X.509 certificate in DER format
1085 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1087 const unsigned char *buf,
1088 size_t buflen,
1089 int make_copy,
1090 mbedtls_x509_crt_ext_cb_t cb,
1091 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092{
Janos Follath865b3eb2019-12-16 11:46:15 +00001093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 size_t len;
1095 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1099 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1100 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
1102 /*
1103 * Check for valid input
1104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (crt == NULL || buf == NULL) {
1106 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1107 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001109 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001111 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 end = p + len;
1113
1114 /*
1115 * Certificate ::= SEQUENCE {
1116 * tbsCertificate TBSCertificate,
1117 * signatureAlgorithm AlgorithmIdentifier,
1118 * signatureValue BIT STRING }
1119 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1121 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1122 mbedtls_x509_crt_free(crt);
1123 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124 }
1125
Janos Follathcc0e49d2016-02-17 14:34:12 +00001126 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001127 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001129 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1131 if (crt->raw.p == NULL) {
1132 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1133 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001136 crt->own_buffer = 1;
1137
1138 p += crt->raw.len - len;
1139 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 } else {
1141 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001142 crt->own_buffer = 0;
1143 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001144
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145 /*
1146 * TBSCertificate ::= SEQUENCE {
1147 */
1148 crt->tbs.p = p;
1149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1151 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1152 mbedtls_x509_crt_free(crt);
1153 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 }
1155
1156 end = p + len;
1157 crt->tbs.len = end - crt->tbs.p;
1158
1159 /*
1160 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1161 *
1162 * CertificateSerialNumber ::= INTEGER
1163 *
1164 * signature AlgorithmIdentifier
1165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1167 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1168 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1169 &sig_params1)) != 0) {
1170 mbedtls_x509_crt_free(crt);
1171 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 }
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (crt->version < 0 || crt->version > 2) {
1175 mbedtls_x509_crt_free(crt);
1176 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177 }
1178
Andres AG7ca4a032017-03-09 16:16:11 +00001179 crt->version++;
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1182 &crt->sig_md, &crt->sig_pk,
1183 &crt->sig_opts)) != 0) {
1184 mbedtls_x509_crt_free(crt);
1185 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 }
1187
1188 /*
1189 * issuer Name
1190 */
1191 crt->issuer_raw.p = p;
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1194 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1195 mbedtls_x509_crt_free(crt);
1196 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001197 }
1198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1200 mbedtls_x509_crt_free(crt);
1201 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001202 }
1203
1204 crt->issuer_raw.len = p - crt->issuer_raw.p;
1205
1206 /*
1207 * Validity ::= SEQUENCE {
1208 * notBefore Time,
1209 * notAfter Time }
1210 *
1211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1213 &crt->valid_to)) != 0) {
1214 mbedtls_x509_crt_free(crt);
1215 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216 }
1217
1218 /*
1219 * subject Name
1220 */
1221 crt->subject_raw.p = p;
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1224 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1225 mbedtls_x509_crt_free(crt);
1226 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 }
1228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1230 mbedtls_x509_crt_free(crt);
1231 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232 }
1233
1234 crt->subject_raw.len = p - crt->subject_raw.p;
1235
1236 /*
1237 * SubjectPublicKeyInfo
1238 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001239 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1241 mbedtls_x509_crt_free(crt);
1242 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001244 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245
1246 /*
1247 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1248 * -- If present, version shall be v2 or v3
1249 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1250 * -- If present, version shall be v2 or v3
1251 * extensions [3] EXPLICIT Extensions OPTIONAL
1252 * -- If present, version shall be v3
1253 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (crt->version == 2 || crt->version == 3) {
1255 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1256 if (ret != 0) {
1257 mbedtls_x509_crt_free(crt);
1258 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259 }
1260 }
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (crt->version == 2 || crt->version == 3) {
1263 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1264 if (ret != 0) {
1265 mbedtls_x509_crt_free(crt);
1266 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 }
1268 }
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (crt->version == 3) {
1271 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1272 if (ret != 0) {
1273 mbedtls_x509_crt_free(crt);
1274 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001275 }
1276 }
1277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 if (p != end) {
1279 mbedtls_x509_crt_free(crt);
1280 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1281 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 }
1283
1284 end = crt_end;
1285
1286 /*
1287 * }
1288 * -- end of TBSCertificate
1289 *
1290 * signatureAlgorithm AlgorithmIdentifier,
1291 * signatureValue BIT STRING
1292 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1294 mbedtls_x509_crt_free(crt);
1295 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296 }
1297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 if (crt->sig_oid.len != sig_oid2.len ||
1299 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001300 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001301 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 (sig_params1.len != 0 &&
1303 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1304 mbedtls_x509_crt_free(crt);
1305 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 }
1307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1309 mbedtls_x509_crt_free(crt);
1310 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 }
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (p != end) {
1314 mbedtls_x509_crt_free(crt);
1315 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1316 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 }
1318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320}
1321
1322/*
1323 * Parse one X.509 certificate in DER format from a buffer and add them to a
1324 * chained list
1325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1327 const unsigned char *buf,
1328 size_t buflen,
1329 int make_copy,
1330 mbedtls_x509_crt_ext_cb_t cb,
1331 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001332{
Janos Follath865b3eb2019-12-16 11:46:15 +00001333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335
1336 /*
1337 * Check for valid input
1338 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 if (crt == NULL || buf == NULL) {
1340 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1341 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344 prev = crt;
1345 crt = crt->next;
1346 }
1347
1348 /*
1349 * Add new certificate on the end of the chain if needed.
1350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (crt->version != 0 && crt->next == NULL) {
1352 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 if (crt->next == NULL) {
1355 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1356 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357
1358 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361 }
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1364 if (ret != 0) {
1365 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (crt != chain) {
1370 mbedtls_free(crt);
1371 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374 }
1375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377}
1378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1380 const unsigned char *buf,
1381 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001382{
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001384}
1385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1387 const unsigned char *buf,
1388 size_t buflen,
1389 int make_copy,
1390 mbedtls_x509_crt_ext_cb_t cb,
1391 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001392{
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001394}
1395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1397 const unsigned char *buf,
1398 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001399{
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001401}
1402
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001404 * Parse one or more PEM certificates from a buffer and add them to the chained
1405 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1408 const unsigned char *buf,
1409 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410{
Janos Follath98e28a72016-05-31 14:03:54 +01001411#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001412 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001414#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415
1416 /*
1417 * Check for valid input
1418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (chain == NULL || buf == NULL) {
1420 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1421 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422
1423 /*
1424 * Determine buffer content. Buffer contains either one DER certificate or
1425 * one or more PEM certificates.
1426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1429 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001431 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1434 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1435 }
Janos Follath98e28a72016-05-31 14:03:54 +01001436#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001438#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001445 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001447 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 ret = mbedtls_pem_read_buffer(&pem,
1452 "-----BEGIN CERTIFICATE-----",
1453 "-----END CERTIFICATE-----",
1454 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457 /*
1458 * Was PEM encoded
1459 */
1460 buflen -= use_len;
1461 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1463 return ret;
1464 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1465 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466
1467 /*
1468 * PEM header and footer were found
1469 */
1470 buflen -= use_len;
1471 buf += use_len;
1472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476
Paul Bakker5a5fa922014-09-26 14:53:04 +02001477 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488 /*
1489 * Quit parsing on a memory error
1490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1492 return ret;
1493 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001496 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001498
1499 total_failed++;
1500 continue;
1501 }
1502
1503 success = 1;
1504 }
1505 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (success) {
1508 return total_failed;
1509 } else if (first_error) {
1510 return first_error;
1511 } else {
1512 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1513 }
Janos Follath98e28a72016-05-31 14:03:54 +01001514#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515}
1516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518/*
1519 * Load one or more certificates and add them to the chained list
1520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522{
Janos Follath865b3eb2019-12-16 11:46:15 +00001523 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524 size_t n;
1525 unsigned char *buf;
1526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1528 return ret;
1529 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 mbedtls_platform_zeroize(buf, n);
1534 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001537}
1538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540{
1541 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001542#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Steve Lhomme369d7c72023-06-16 14:16:03 +02001543#if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_XP */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 int w_ret;
1545 WCHAR szDir[MAX_PATH];
1546 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001547 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549
Paul Bakker9af723c2014-05-01 13:03:14 +02001550 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 HANDLE hFind;
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 if (len > MAX_PATH - 3) {
1554 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1555 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 memset(szDir, 0, sizeof(szDir));
1558 memset(filename, 0, MAX_PATH);
1559 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001560 filename[len++] = '\\';
1561 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562 filename[len++] = '*';
1563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1565 MAX_PATH - 3);
1566 if (w_ret == 0) {
1567 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1568 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 hFind = FindFirstFileW(szDir, &file_data);
1571 if (hFind == INVALID_HANDLE_VALUE) {
1572 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1573 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
1575 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 do {
1577 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001584 -1,
1585 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 NULL, NULL);
1587 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001588 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1589 goto cleanup;
1590 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1593 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 }
1598 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603
Ron Eldor36d90422017-01-09 15:09:16 +02001604cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 FindClose(hFind);
Steve Lhomme369d7c72023-06-16 14:16:03 +02001606#else /* !_WIN32_WINNT_XP */
1607#error mbedtls_x509_crt_parse_path not available before Windows XP
1608#endif /* !_WIN32_WINNT_XP */
Paul Bakkerbe089b02013-10-14 15:51:50 +02001609#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001610 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001611 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001613 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001614 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if (dir == NULL) {
1618 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1619 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620
Ron Eldor63140682017-01-09 19:27:59 +02001621#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1623 closedir(dir);
1624 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001625 }
Ron Eldor63140682017-01-09 19:27:59 +02001626#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001631 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001634 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001635 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1636 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 } else if (stat(entry_name, &sb) == -1) {
1638 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001639 /* Broken symbolic link - ignore this entry.
1640 stat(2) will return this error for either (a) a dangling
1641 symlink or (b) a missing file.
1642 Given that we have just obtained the filename from readdir,
1643 assume that it does exist and therefore treat this as a
1644 dangling symlink. */
1645 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001647 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001648 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1649 goto cleanup;
1650 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651 }
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656
1657 // Ignore parse errors
1658 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1660 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001665 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001666
1667cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001669
Ron Eldor63140682017-01-09 19:27:59 +02001670#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 }
Ron Eldor63140682017-01-09 19:27:59 +02001674#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001675
Paul Bakkerbe089b02013-10-14 15:51:50 +02001676#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681
Peter Kolbus9a969b62018-12-11 13:55:56 -06001682#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001683#define PRINT_ITEM(i) \
1684 do { \
1685 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1686 MBEDTLS_X509_SAFE_SNPRINTF; \
1687 sep = ", "; \
1688 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001689
Przemek Stekielf4194942023-04-24 09:52:17 +02001690#define CERT_TYPE(type, name) \
1691 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001692 if (ns_cert_type & (type)) { \
1693 PRINT_ITEM(name); \
1694 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001695 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001696
Przemek Stekielf4194942023-04-24 09:52:17 +02001697#define KEY_USAGE(code, name) \
1698 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001699 if (key_usage & (code)) { \
1700 PRINT_ITEM(name); \
1701 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001702 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704static int x509_info_ext_key_usage(char **buf, size_t *size,
1705 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001706{
Janos Follath865b3eb2019-12-16 11:46:15 +00001707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001708 const char *desc;
1709 size_t n = *size;
1710 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001712 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 while (cur != NULL) {
1715 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001716 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001720 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001721
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001722 sep = ", ";
1723
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001724 cur = cur->next;
1725 }
1726
1727 *size = n;
1728 *buf = p;
1729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001731}
1732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733static int x509_info_cert_policies(char **buf, size_t *size,
1734 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001735{
Janos Follath865b3eb2019-12-16 11:46:15 +00001736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001737 const char *desc;
1738 size_t n = *size;
1739 char *p = *buf;
1740 const mbedtls_x509_sequence *cur = certificate_policies;
1741 const char *sep = "";
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 while (cur != NULL) {
1744 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001745 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001749 MBEDTLS_X509_SAFE_SNPRINTF;
1750
1751 sep = ", ";
1752
1753 cur = cur->next;
1754 }
1755
1756 *size = n;
1757 *buf = p;
1758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001760}
1761
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001762/*
1763 * Return an informational string about the certificate.
1764 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001765#define BEFORE_COLON 18
1766#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001767int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1768 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001769{
Janos Follath865b3eb2019-12-16 11:46:15 +00001770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001771 size_t n;
1772 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773 char key_size_str[BEFORE_COLON];
1774
1775 p = buf;
1776 n = size;
1777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 if (NULL == crt) {
1779 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001780 MBEDTLS_X509_SAFE_SNPRINTF;
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001783 }
1784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1786 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001787 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1789 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001790 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001793 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001796 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001798 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001801 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001803 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1806 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1807 crt->valid_from.year, crt->valid_from.mon,
1808 crt->valid_from.day, crt->valid_from.hour,
1809 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001810 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1813 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1814 crt->valid_to.year, crt->valid_to.mon,
1815 crt->valid_to.day, crt->valid_to.hour,
1816 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001817 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001820 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1823 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001824 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001826 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1828 mbedtls_pk_get_name(&crt->pk))) != 0) {
1829 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001830 }
1831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1833 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001834 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001835
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001836 /*
1837 * Optional extensions
1838 */
1839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1841 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1842 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001843 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (crt->max_pathlen > 0) {
1846 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001847 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001848 }
1849 }
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1852 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001853 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001854
Przemek Stekiel21c37282023-01-16 08:47:49 +01001855 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1856 &crt->subject_alt_names,
1857 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 return ret;
1859 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001860 }
1861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1863 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001864 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001865
Przemek Stekiel21c37282023-01-16 08:47:49 +01001866 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 return ret;
1868 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001869 }
1870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1872 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001873 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001874
Przemek Stekiel21c37282023-01-16 08:47:49 +01001875 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 return ret;
1877 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001878 }
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1881 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001882 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 if ((ret = x509_info_ext_key_usage(&p, &n,
1885 &crt->ext_key_usage)) != 0) {
1886 return ret;
1887 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001888 }
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1891 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001892 MBEDTLS_X509_SAFE_SNPRINTF;
1893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 if ((ret = x509_info_cert_policies(&p, &n,
1895 &crt->certificate_policies)) != 0) {
1896 return ret;
1897 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001898 }
1899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001901 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001904}
1905
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001906struct x509_crt_verify_string {
1907 int code;
1908 const char *string;
1909};
1910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001912static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001913 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001914 { 0, NULL }
1915};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001916#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1919 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001920{
Janos Follath865b3eb2019-12-16 11:46:15 +00001921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001922 const struct x509_crt_verify_string *cur;
1923 char *p = buf;
1924 size_t n = size;
1925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1927 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001928 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001932 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001933 flags ^= cur->code;
1934 }
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (flags != 0) {
1937 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1938 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001939 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001940 }
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001943}
Hanno Becker612a2f12020-10-09 09:19:39 +01001944#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1947 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001948{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001949 unsigned int usage_must, usage_may;
1950 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1954 return 0;
1955 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001956
1957 usage_must = usage & ~may_mask;
1958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1960 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1961 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001962
1963 usage_may = usage & may_mask;
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1966 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1967 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001970}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1973 const char *usage_oid,
1974 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001975{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001977
1978 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1980 return 0;
1981 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001982
1983 /*
1984 * Look for the requested usage (or wildcard ANY) in our list
1985 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (cur_oid->len == usage_len &&
1990 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1991 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001992 }
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1995 return 0;
1996 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001997 }
1998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002000}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002003/*
2004 * Return 1 if the certificate is revoked, or 0 otherwise.
2005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002006int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002007{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 while (cur != NULL && cur->serial.len != 0) {
2011 if (crt->serial.len == cur->serial.len &&
2012 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2013 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002014 }
2015
2016 cur = cur->next;
2017 }
2018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002020}
2021
2022/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002023 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002024 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002026static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2027 mbedtls_x509_crl *crl_list,
2028 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002029{
2030 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002031 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002032#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002033 psa_algorithm_t psa_algorithm;
2034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002036#endif /* MBEDTLS_USE_PSA_CRYPTO */
2037 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (ca == NULL) {
2040 return flags;
2041 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 while (crl_list != NULL) {
2044 if (crl_list->version == 0 ||
2045 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002046 crl_list = crl_list->next;
2047 continue;
2048 }
2049
2050 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002051 * Check if the CA is configured to sign CRLs
2052 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 if (mbedtls_x509_crt_check_key_usage(ca,
2054 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002056 break;
2057 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002058
2059 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002060 * Check if CRL is correctly signed by the trusted CA
2061 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002063 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002067 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002069
pespacek7599a772022-02-07 14:40:55 +01002070#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002071 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 if (psa_hash_compute(psa_algorithm,
2073 crl_list->tbs.p,
2074 crl_list->tbs.len,
2075 hash,
2076 sizeof(hash),
2077 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002078 /* Note: this can't happen except after an internal error */
2079 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2080 break;
2081 }
pespacek7599a772022-02-07 14:40:55 +01002082#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2084 hash_length = mbedtls_md_get_size(md_info);
2085 if (mbedtls_md(md_info,
2086 crl_list->tbs.p,
2087 crl_list->tbs.len,
2088 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002089 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091 break;
2092 }
pespaceka7a64692022-02-14 15:18:43 +01002093#endif /* MBEDTLS_USE_PSA_CRYPTO */
2094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002096 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2100 crl_list->sig_md, hash, hash_length,
2101 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002103 break;
2104 }
2105
2106 /*
2107 * Check for validity of CRL (Do not drop out)
2108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002114 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002116
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117 /*
2118 * Check if certificate is revoked
2119 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122 break;
2123 }
2124
2125 crl_list = crl_list->next;
2126 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002132/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002133 * Check the signature of a certificate by its parent
2134 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002135static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2136 mbedtls_x509_crt *parent,
2137 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002138{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002139 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002140 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002141#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2142 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 md_info = mbedtls_md_info_from_type(child->sig_md);
2144 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002145
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002146 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2148 return -1;
2149 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002150#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002151 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002152 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 status = psa_hash_compute(hash_alg,
2155 child->tbs.p,
2156 child->tbs.len,
2157 hash,
2158 sizeof(hash),
2159 &hash_len);
2160 if (status != PSA_SUCCESS) {
2161 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002162 }
2163
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002164#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002165 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2167 return -1;
2168 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002169
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002170#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2172 return mbedtls_pk_verify_restartable(&parent->pk,
2173 child->sig_md, hash, hash_len,
2174 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002175 }
2176#else
2177 (void) rs_ctx;
2178#endif
2179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2181 child->sig_md, hash, hash_len,
2182 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002183}
2184
2185/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002186 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2187 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002188 *
2189 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002191static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2192 const mbedtls_x509_crt *parent,
2193 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002194{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002195 int need_ca_bit;
2196
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002197 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2199 return -1;
2200 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002201
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002202 /* Parent must have the basicConstraints CA bit set as a general rule */
2203 need_ca_bit = 1;
2204
2205 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002207 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002208 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (need_ca_bit && !parent->ca_istrue) {
2211 return -1;
2212 }
2213
2214 if (need_ca_bit &&
2215 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2216 return -1;
2217 }
2218
2219 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002220}
2221
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002222/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002223 * Find a suitable parent for child in candidates, or return NULL.
2224 *
2225 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002226 * 1. subject name matches child's issuer
2227 * 2. if necessary, the CA bit is set and key usage allows signing certs
2228 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002229 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002230 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002231 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002232 * If there's a suitable candidate which is also time-valid, return the first
2233 * such. Otherwise, return the first suitable candidate (or NULL if there is
2234 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002235 *
2236 * The rationale for this rule is that someone could have a list of trusted
2237 * roots with two versions on the same root with different validity periods.
2238 * (At least one user reported having such a list and wanted it to just work.)
2239 * The reason we don't just require time-validity is that generally there is
2240 * only one version, and if it's expired we want the flags to state that
2241 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002242 *
2243 * The rationale for rule 3 (signature for trusted roots) is that users might
2244 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002245 * way we select the correct one is by checking the signature (as we don't
2246 * rely on key identifier extensions). (This is one way users might choose to
2247 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002248 *
2249 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002250 * - [in] child: certificate for which we're looking for a parent
2251 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002252 * - [out] r_parent: parent found (or NULL)
2253 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002254 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2255 * of the chain, 0 otherwise
2256 * - [in] path_cnt: number of intermediates seen so far
2257 * - [in] self_cnt: number of self-signed intermediates seen so far
2258 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002259 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002260 *
2261 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002262 * - 0 on success
2263 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002264 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002265static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 mbedtls_x509_crt *child,
2267 mbedtls_x509_crt *candidates,
2268 mbedtls_x509_crt **r_parent,
2269 int *r_signature_is_good,
2270 int top,
2271 unsigned path_cnt,
2272 unsigned self_cnt,
2273 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002274{
Janos Follath865b3eb2019-12-16 11:46:15 +00002275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002276 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002277 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002278
2279#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002280 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002282 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002283 parent = rs_ctx->parent;
2284 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002285 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002286
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002287 /* clear saved state */
2288 rs_ctx->parent = NULL;
2289 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002290 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002291
2292 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002293 goto check_signature;
2294 }
2295#endif
2296
2297 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002298 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002301 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002303 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002305
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002306 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (parent->max_pathlen > 0 &&
2308 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002309 continue;
2310 }
2311
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002312 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002313#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2314check_signature:
2315#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002317
2318#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002320 /* save state */
2321 rs_ctx->parent = parent;
2322 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002323 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002326 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002327#else
2328 (void) ret;
2329#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002330
2331 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002333 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002335
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002336 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2338 mbedtls_x509_time_is_future(&parent->valid_from)) {
2339 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002340 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002341 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002342 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002343
2344 continue;
2345 }
2346
Andy Gross1f627142019-01-30 10:25:53 -06002347 *r_parent = parent;
2348 *r_signature_is_good = signature_is_good;
2349
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002350 break;
2351 }
2352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002354 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002355 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002356 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002359}
2360
2361/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002362 * Find a parent in trusted CAs or the provided chain, or return NULL.
2363 *
2364 * Searches in trusted CAs first, and return the first suitable parent found
2365 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002366 *
2367 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002368 * - [in] child: certificate for which we're looking for a parent, followed
2369 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002370 * - [in] trust_ca: list of locally trusted certificates
2371 * - [out] parent: parent found (or NULL)
2372 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2373 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2374 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2375 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002376 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002377 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002378 *
2379 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002380 * - 0 on success
2381 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002382 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002383static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 mbedtls_x509_crt *child,
2385 mbedtls_x509_crt *trust_ca,
2386 mbedtls_x509_crt **parent,
2387 int *parent_is_trusted,
2388 int *signature_is_good,
2389 unsigned path_cnt,
2390 unsigned self_cnt,
2391 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002392{
Janos Follath865b3eb2019-12-16 11:46:15 +00002393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002394 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002395
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002396 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002397
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002398#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002399 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002401 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002402 rs_ctx->parent_is_trusted = -1;
2403 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002404#endif
2405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002407 search_list = *parent_is_trusted ? trust_ca : child->next;
2408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 ret = x509_crt_find_parent_in(child, search_list,
2410 parent, signature_is_good,
2411 *parent_is_trusted,
2412 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002413
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002414#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002416 /* save state */
2417 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002419 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002420#else
2421 (void) ret;
2422#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002423
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002424 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002426 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002428
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002429 /* prepare second iteration */
2430 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002431 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002432
2433 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002435 *parent_is_trusted = 0;
2436 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002437 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002440}
2441
2442/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002443 * Check if an end-entity certificate is locally trusted
2444 *
2445 * Currently we require such certificates to be self-signed (actually only
2446 * check for self-issued as self-signatures are not checked)
2447 */
2448static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 mbedtls_x509_crt *crt,
2450 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002451{
2452 mbedtls_x509_crt *cur;
2453
2454 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2456 return -1;
2457 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002458
2459 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2461 if (crt->raw.len == cur->raw.len &&
2462 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2463 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002464 }
2465 }
2466
2467 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002469}
2470
2471/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002472 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002473 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002474 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2475 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002476 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002477 * such that every cert in the chain is a child of the next one,
2478 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002479 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002480 * Verify that chain and return it with flags for all issues found.
2481 *
2482 * Special cases:
2483 * - EE == Rj -> return a one-element list containing it
2484 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2485 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002486 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002487 * Tests for (aspects of) this function should include at least:
2488 * - trusted EE
2489 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002490 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002491 * - if relevant: EE untrusted
2492 * - if relevant: EE -> intermediate, untrusted
2493 * with the aspect under test checked at each relevant level (EE, int, root).
2494 * For some aspects longer chains are required, but usually length 2 is
2495 * enough (but length 1 is not in general).
2496 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002497 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002498 * - [in] crt: the cert list EE, C1, ..., Cn
2499 * - [in] trust_ca: the trusted list R1, ..., Rp
2500 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002501 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002502 * Only valid when return value is 0, may contain garbage otherwise!
2503 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002504 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002505 *
2506 * Return value:
2507 * - non-zero if the chain could not be fully built and examined
2508 * - 0 is the chain was successfully built and examined,
2509 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002510 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002511static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 mbedtls_x509_crt *crt,
2513 mbedtls_x509_crt *trust_ca,
2514 mbedtls_x509_crl *ca_crl,
2515 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2516 void *p_ca_cb,
2517 const mbedtls_x509_crt_profile *profile,
2518 mbedtls_x509_crt_verify_chain *ver_chain,
2519 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002520{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002521 /* Don't initialize any of those variables here, so that the compiler can
2522 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002523 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002524 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002525 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002526 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002527 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002528 int parent_is_trusted;
2529 int child_is_trusted;
2530 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002531 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002532 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002533
2534#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2535 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002537 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002538 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002539 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002540
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002541 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002542 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002543 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002544 flags = &cur->flags;
2545
2546 goto find_parent;
2547 }
2548#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002549
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002550 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002551 self_cnt = 0;
2552 parent_is_trusted = 0;
2553 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002556 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002557 cur = &ver_chain->items[ver_chain->len];
2558 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002559 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002560 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002561 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002562
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002563 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002565 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002569 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002571
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002572 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (child_is_trusted) {
2574 return 0;
2575 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002576
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002577 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002579 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002583 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002585
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002586 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 if (ver_chain->len == 1 &&
2588 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2589 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002590 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002591
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002592#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2593find_parent:
2594#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002595
2596 /* Obtain list of potential trusted signers from CA callback,
2597 * or use statically provided list. */
2598#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (f_ca_cb != NULL) {
2600 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2601 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002602 ver_chain->trust_ca_cb_result = NULL;
2603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2605 if (ret != 0) {
2606 return MBEDTLS_ERR_X509_FATAL_ERROR;
2607 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002608
2609 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002611#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2612 {
2613 ((void) f_ca_cb);
2614 ((void) p_ca_cb);
2615 cur_trust_ca = trust_ca;
2616 }
2617
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002618 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2620 &parent_is_trusted, &signature_is_good,
2621 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002622
2623#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002625 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002626 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002627 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002628 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002631 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002632#else
2633 (void) ret;
2634#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002635
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002636 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002638 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002640 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002641
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002642 /* Count intermediate self-issued (not necessarily self-signed) certs.
2643 * These can occur with some strategies for key rollover, see [SIRO],
2644 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (ver_chain->len != 1 &&
2646 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002647 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002648 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002649
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002650 /* path_cnt is 0 for the first intermediate CA,
2651 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if (!parent_is_trusted &&
2653 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002654 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002656 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002657
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002658 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002660 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002662
2663 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002665 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002669 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002671#else
2672 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002673#endif
2674
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002675 /* prepare for next iteration */
2676 child = parent;
2677 parent = NULL;
2678 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002679 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002680 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002681}
2682
Glenn Straussc26bd762022-10-23 19:48:18 -04002683#ifdef _WIN32
2684#ifdef _MSC_VER
2685#pragma comment(lib, "ws2_32.lib")
2686#include <winsock2.h>
2687#include <ws2tcpip.h>
2688#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2689#include <winsock2.h>
2690#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002691#else
2692/* inet_pton() is not supported, fallback to software version */
2693#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002694#endif
2695#elif defined(__sun)
2696/* Solaris requires -lsocket -lnsl for inet_pton() */
2697#elif defined(__has_include)
2698#if __has_include(<sys/socket.h>)
2699#include <sys/socket.h>
2700#endif
2701#if __has_include(<arpa/inet.h>)
2702#include <arpa/inet.h>
2703#endif
2704#endif
2705
2706/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2707 * the platform inet_pton() or a local implementation (below). The local
2708 * implementation may be used even in cases where the platform provides
2709 * inet_pton(), e.g. when there are different includes required and/or the
2710 * platform implementation requires dependencies on additional libraries.
2711 * Specifically, Windows requires custom includes and additional link
2712 * dependencies, and Solaris requires additional link dependencies.
2713 * Also, as a coarse heuristic, use the local implementation if the compiler
2714 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002715 * provided by headers included (or not) via __has_include() above.
2716 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2717 * despite having a platform that has inet_pton. */
2718#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2719/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002720static int x509_inet_pton_ipv4(const char *src, void *dst);
2721
2722#define li_cton(c, n) \
2723 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2724
2725static int x509_inet_pton_ipv6(const char *src, void *dst)
2726{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002727 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002728 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002729 uint16_t addr[8];
2730 do {
2731 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002732 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002733 for (uint8_t digit; num_digits < 4; num_digits++) {
2734 if (li_cton(*p, digit) == 0) {
2735 break;
2736 }
2737 group = (group << 4) | digit;
2738 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002739 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002740 if (num_digits != 0) {
Andrzej Kurek0d578962023-04-13 09:09:56 -04002741 addr[nonzero_groups++] = MBEDTLS_IS_BIG_ENDIAN ? group :
2742 (group << 8) | (group >> 8);
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002743 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002744 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002745 } else if (*p == '.') {
2746 /* Don't accept IPv4 too early or late */
2747 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2748 nonzero_groups >= 7) {
2749 break;
2750 }
2751
2752 /* Walk back to prior ':', then parse as IPv4-mapped */
2753 int steps = 4;
2754 do {
2755 p--;
2756 steps--;
2757 } while (*p != ':' && steps > 0);
2758
2759 if (*p != ':') {
2760 break;
2761 }
2762 p++;
2763 nonzero_groups--;
2764 if (x509_inet_pton_ipv4((const char *) p,
2765 addr + nonzero_groups) != 0) {
2766 break;
2767 }
2768
Andrzej Kurek0d578962023-04-13 09:09:56 -04002769 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002770 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002771 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002772 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002773 return -1;
2774 }
2775 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002776 /* Don't accept a second zero group or an invalid delimiter */
2777 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002778 return -1;
2779 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002780 zero_group_start = nonzero_groups;
2781
2782 /* Accept a zero group at start, but it has to be a double colon */
2783 if (zero_group_start == 0 && *++p != ':') {
2784 return -1;
2785 }
2786
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002787 if (p[1] == '\0') {
2788 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002789 break;
2790 }
2791 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002792 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002793 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002794
2795 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002796 return -1;
2797 }
2798
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002799 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002800 if (nonzero_groups > 6) {
2801 return -1;
2802 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002803 int zero_groups = 8 - nonzero_groups;
2804 int groups_after_zero = nonzero_groups - zero_group_start;
2805
2806 /* Move the non-zero part to after the zeroes */
2807 if (groups_after_zero) {
2808 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002809 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002810 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002811 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002812 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002813 } else {
2814 if (nonzero_groups != 8) {
2815 return -1;
2816 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002817 }
2818 memcpy(dst, addr, sizeof(addr));
2819 return 0;
2820}
2821
2822static int x509_inet_pton_ipv4(const char *src, void *dst)
2823{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002824 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002825 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002826 uint8_t digit, num_digits = 0;
2827 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002828 uint16_t octet;
2829
Glenn Strauss416c2952022-10-24 23:00:02 -04002830 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002831 octet = num_digits = 0;
2832 do {
2833 digit = *p - '0';
2834 if (digit > 9) {
2835 break;
2836 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002837
2838 /* Don't allow leading zeroes. These might mean octal format,
2839 * which this implementation does not support. */
2840 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002841 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002842 }
2843
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002844 octet = octet * 10 + digit;
2845 num_digits++;
2846 p++;
2847 } while (num_digits < 3);
2848
2849 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002850 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002851 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002852 *res++ = (uint8_t) octet;
2853 num_octets++;
2854 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002855 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002856}
Glenn Straussc26bd762022-10-23 19:48:18 -04002857
2858#else
2859
2860static int x509_inet_pton_ipv6(const char *src, void *dst)
2861{
2862 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2863}
2864
2865static int x509_inet_pton_ipv4(const char *src, void *dst)
2866{
2867 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2868}
2869
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002870#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002871
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002872MBEDTLS_STATIC_TESTABLE
2873size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002874{
2875 return strchr(cn, ':') == NULL
2876 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2877 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2878}
2879
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002880/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002881 * Check for CN match
2882 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2884 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002885{
2886 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 if (name->len == cn_len &&
2888 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2889 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002890 }
2891
2892 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 if (x509_check_wildcard(cn, name) == 0) {
2894 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002895 }
2896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002898}
2899
Glenn Straussc26bd762022-10-23 19:48:18 -04002900static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2901 const char *cn, size_t cn_len)
2902{
2903 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002904 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002905 if (cn_len == 0) {
2906 return -1;
2907 }
2908
2909 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2910 const unsigned char san_type = (unsigned char) cur->buf.tag &
2911 MBEDTLS_ASN1_TAG_VALUE_MASK;
2912 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2913 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2914 return 0;
2915 }
2916 }
2917
2918 return -1;
2919}
2920
Andrzej Kurek199eab92023-05-10 09:57:19 -04002921static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2922 const char *cn, size_t cn_len)
2923{
2924 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2925 const unsigned char san_type = (unsigned char) cur->buf.tag &
2926 MBEDTLS_ASN1_TAG_VALUE_MASK;
2927 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2928 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2929 return 0;
2930 }
2931 }
2932
2933 return -1;
2934}
2935
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002936/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002937 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2938 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002939static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002941{
Glenn Straussc26bd762022-10-23 19:48:18 -04002942 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002943 int san_uri = 0;
2944 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002945 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2946 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002947 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002948 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2949 return 0;
2950 }
2951 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002952 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002953 san_ip = 1;
2954 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002955 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2956 san_uri = 1;
2957 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002958 /* (We may handle other types here later.) */
2959 default: /* Unrecognized type */
2960 break;
2961 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002963 if (san_ip) {
2964 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2965 return 0;
2966 }
2967 }
2968 if (san_uri) {
2969 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2970 return 0;
2971 }
2972 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002973
Andrzej Kurek199eab92023-05-10 09:57:19 -04002974 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002975}
2976
2977/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002978 * Verify the requested CN - only call this if cn is not NULL!
2979 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002980static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2981 const char *cn,
2982 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002983{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002984 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002988 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2989 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 }
2991 } else {
2992 for (name = &crt->subject; name != NULL; name = name->next) {
2993 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2994 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002995 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 }
2997 }
2998
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002999 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003000
3001 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003002}
3003
3004/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003005 * Merge the flags for all certs in the chain, after calling callback
3006 */
3007static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 uint32_t *flags,
3009 const mbedtls_x509_crt_verify_chain *ver_chain,
3010 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3011 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003012{
Janos Follath865b3eb2019-12-16 11:46:15 +00003013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003014 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003015 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003016 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003017
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003019 cur = &ver_chain->items[i-1];
3020 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 if (NULL != f_vrfy) {
3023 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3024 return ret;
3025 }
3026 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003027
3028 *flags |= cur_flags;
3029 }
3030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003032}
3033
3034/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003035 * Verify the certificate validity, with profile, restartable version
3036 *
3037 * This function:
3038 * - checks the requested CN (if any)
3039 * - checks the type and size of the EE cert's key,
3040 * as that isn't done as part of chain building/verification currently
3041 * - builds and verifies the chain
3042 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003043 *
3044 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3045 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3046 * verification routine to search for trusted signers, and CRLs will
3047 * be disabled. Otherwise, `trust_ca` will be used as the static list
3048 * of trusted signers, and `ca_crl` will be use as the static list
3049 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003051static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3052 mbedtls_x509_crt *trust_ca,
3053 mbedtls_x509_crl *ca_crl,
3054 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3055 void *p_ca_cb,
3056 const mbedtls_x509_crt_profile *profile,
3057 const char *cn, uint32_t *flags,
3058 int (*f_vrfy)(void *,
3059 mbedtls_x509_crt *,
3060 int,
3061 uint32_t *),
3062 void *p_vrfy,
3063 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003064{
Janos Follath865b3eb2019-12-16 11:46:15 +00003065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003066 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003067 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003068 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003069
3070 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003071 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003075 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3076 goto exit;
3077 }
3078
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003079 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 if (cn != NULL) {
3081 x509_crt_verify_name(crt, cn, &ee_flags);
3082 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003083
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003084 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003088 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003092 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003094
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003095 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3097 f_ca_cb, p_ca_cb, profile,
3098 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003099
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003103
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003104 /* Merge end-entity flags */
3105 ver_chain.items[0].flags |= ee_flags;
3106
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003107 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003109
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003110exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003111
3112#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3114 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003115 ver_chain.trust_ca_cb_result = NULL;
3116#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3117
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003118#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3120 mbedtls_x509_crt_restart_free(rs_ctx);
3121 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003122#endif
3123
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003124 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3125 * the SSL module for authmode optional, but non-zero return from the
3126 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003128 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003129 }
3130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (ret != 0) {
3132 *flags = (uint32_t) -1;
3133 return ret;
3134 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003135
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 if (*flags != 0) {
3137 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3138 }
3139
3140 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003141}
3142
Hanno Becker3116fb32019-03-28 13:34:42 +00003143
3144/*
3145 * Verify the certificate validity (default profile, not restartable)
3146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003147int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3148 mbedtls_x509_crt *trust_ca,
3149 mbedtls_x509_crl *ca_crl,
3150 const char *cn, uint32_t *flags,
3151 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3152 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003153{
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3155 NULL, NULL,
3156 &mbedtls_x509_crt_profile_default,
3157 cn, flags,
3158 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003159}
3160
3161/*
3162 * Verify the certificate validity (user-chosen profile, not restartable)
3163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003164int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3165 mbedtls_x509_crt *trust_ca,
3166 mbedtls_x509_crl *ca_crl,
3167 const mbedtls_x509_crt_profile *profile,
3168 const char *cn, uint32_t *flags,
3169 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3170 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003171{
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3173 NULL, NULL,
3174 profile, cn, flags,
3175 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003176}
3177
3178#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3179/*
3180 * Verify the certificate validity (user-chosen profile, CA callback,
3181 * not restartable).
3182 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003183int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3184 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3185 void *p_ca_cb,
3186 const mbedtls_x509_crt_profile *profile,
3187 const char *cn, uint32_t *flags,
3188 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3189 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003190{
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3192 f_ca_cb, p_ca_cb,
3193 profile, cn, flags,
3194 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003195}
3196#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3199 mbedtls_x509_crt *trust_ca,
3200 mbedtls_x509_crl *ca_crl,
3201 const mbedtls_x509_crt_profile *profile,
3202 const char *cn, uint32_t *flags,
3203 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3204 void *p_vrfy,
3205 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003206{
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3208 NULL, NULL,
3209 profile, cn, flags,
3210 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003211}
3212
3213
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003214/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003215 * Initialize a certificate chain
3216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003217void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003218{
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003220}
3221
3222/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003223 * Unallocate all certificate data
3224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003225void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003226{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 mbedtls_x509_crt *cert_cur = crt;
3228 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 while (cert_cur != NULL) {
3231 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003235#endif
3236
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3238 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3239 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3240 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3241 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003242 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003243
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3245 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3246 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003247 }
3248
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003249 cert_prv = cert_cur;
3250 cert_cur = cert_cur->next;
3251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3253 if (cert_prv != crt) {
3254 mbedtls_free(cert_prv);
3255 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003256 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003257}
3258
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003259#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3260/*
3261 * Initialize a restart context
3262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003263void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003264{
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003266
3267 ctx->parent = NULL;
3268 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003269 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003270
3271 ctx->parent_is_trusted = -1;
3272
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003273 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003274 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003276}
3277
3278/*
3279 * Free the components of a restart context
3280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003282{
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003284 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 mbedtls_pk_restart_free(&ctx->pk);
3288 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003289}
3290#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#endif /* MBEDTLS_X509_CRT_PARSE_C */