blob: c6d73588e5c7e80e72072b31bc82a0be82ebecda [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel40afdd22022-09-06 13:08:28 +020051#include "hash_info.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010057#endif
58
Daniel Axtensf0710242020-05-28 11:43:41 +100059#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <windows.h>
62#else
63#include <time.h>
64#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100065#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020069#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#include <sys/types.h>
71#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020072#if defined(__MBED__)
73#include <platform/mbed_retarget.h>
74#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020076#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060077#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020081/*
82 * Item in a verification chain: cert and flags for it
83 */
84typedef struct {
85 mbedtls_x509_crt *crt;
86 uint32_t flags;
87} x509_crt_verify_chain_item;
88
89/*
90 * Max size of verification chain: end-entity + intermediates + trusted root
91 */
Gilles Peskine449bd832023-01-11 14:50:10 +010092#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020093
Gilles Peskineffb92da2021-06-02 00:03:26 +020094/* Default profile. Do not remove items unless there are serious security
95 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020096const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
97{
Gilles Peskineae270bf2021-06-02 00:05:29 +020098 /* Hashes from SHA-256 and above. Note that this selection
99 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
101 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
102 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200103 0xFFFFFFF, /* Any PK alg */
104#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200105 /* Curves at or above 128-bit security level. Note that this selection
106 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
108 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
109 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
110 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
111 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
112 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200113 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200114#else
115 0,
116#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200117 2048,
118};
119
Gilles Peskineffb92da2021-06-02 00:03:26 +0200120/* Next-generation profile. Currently identical to the default, but may
121 * be tightened at any time. */
122const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200123{
124 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200128 0xFFFFFFF, /* Any PK alg */
129#if defined(MBEDTLS_ECP_C)
130 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
132 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
133 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
134 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
135 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
136 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200138#else
139 0,
140#endif
141 2048,
142};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200143
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200144/*
145 * NSA Suite B Profile
146 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
148{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
151 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200152 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
154 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155#if defined(MBEDTLS_ECP_C)
156 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
158 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200159#else
160 0,
161#endif
162 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163};
164
165/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200166 * Empty / all-forbidden profile
167 */
168const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
169{
170 0,
171 0,
172 0,
173 (uint32_t) -1,
174};
175
176/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200177 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200178 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
181 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182{
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (md_alg == MBEDTLS_MD_NONE) {
184 return -1;
185 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
188 return 0;
189 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200192}
193
194/*
195 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200196 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
199 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200{
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (pk_alg == MBEDTLS_PK_NONE) {
202 return -1;
203 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
206 return 0;
207 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210}
211
212/*
213 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200214 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
217 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218{
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200220
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
223 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
224 return 0;
225 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200228 }
229#endif
230
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200231#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200233 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 pk_alg == MBEDTLS_PK_ECKEY_DH) {
235 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (gid == MBEDTLS_ECP_DP_NONE) {
238 return -1;
239 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
242 return 0;
243 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200246 }
247#endif
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200250}
251
252/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000253 * Like memcmp, but case-insensitive and always returns -1 if different
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000256{
257 size_t i;
258 unsigned char diff;
259 const unsigned char *n1 = s1, *n2 = s2;
260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000262 diff = n1[i] ^ n2[i];
263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000265 continue;
266 }
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (diff == 32 &&
269 ((n1[i] >= 'a' && n1[i] <= 'z') ||
270 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
271 continue;
272 }
273
274 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000275 }
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000278}
279
280/*
281 * Return 0 if name matches wildcard, -1 otherwise
282 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000284{
285 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000287
288 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
290 return -1;
291 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 for (i = 0; i < cn_len; ++i) {
294 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000295 cn_idx = i;
296 break;
297 }
298 }
299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (cn_idx == 0) {
301 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000302 }
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (cn_len - cn_idx == name->len - 1 &&
305 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
306 return 0;
307 }
308
309 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000310}
311
312/*
313 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
314 * variations (but not all).
315 *
316 * Return 0 if equal, -1 otherwise.
317 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000319{
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000321 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 memcmp(a->p, b->p, b->len) == 0) {
323 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000324 }
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
327 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000328 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 x509_memcasecmp(a->p, b->p, b->len) == 0) {
330 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000331 }
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000334}
335
336/*
337 * Compare two X.509 Names (aka rdnSequence).
338 *
339 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
340 * we sometimes return unequal when the full algorithm would return equal,
341 * but never the other way. (In particular, we don't do Unicode normalisation
342 * or space folding.)
343 *
344 * Return 0 if equal, -1 otherwise.
345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000347{
348 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 while (a != NULL || b != NULL) {
350 if (a == NULL || b == NULL) {
351 return -1;
352 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000353
354 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000356 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
358 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000359 }
360
361 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (x509_string_cmp(&a->val, &b->val) != 0) {
363 return -1;
364 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000365
366 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (a->next_merged != b->next_merged) {
368 return -1;
369 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000370
371 a = a->next;
372 b = b->next;
373 }
374
375 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000377}
378
379/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200380 * Reset (init or clear) a verify_chain
381 */
382static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200384{
385 size_t i;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200388 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000389 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200390 }
391
392 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000393
394#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
395 ver_chain->trust_ca_cb_result = NULL;
396#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200397}
398
399/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
401 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402static int x509_get_version(unsigned char **p,
403 const unsigned char *end,
404 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405{
Janos Follath865b3eb2019-12-16 11:46:15 +0000406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 size_t len;
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
410 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
411 0)) != 0) {
412 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 }
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 }
419
420 end = *p + len;
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
423 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
424 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (*p != end) {
427 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
428 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
429 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432}
433
434/*
435 * Validity ::= SEQUENCE {
436 * notBefore Time,
437 * notAfter Time }
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439static int x509_get_dates(unsigned char **p,
440 const unsigned char *end,
441 mbedtls_x509_time *from,
442 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443{
Janos Follath865b3eb2019-12-16 11:46:15 +0000444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 size_t len;
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
448 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
449 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
450 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 end = *p + len;
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
455 return ret;
456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
459 return ret;
460 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (*p != end) {
463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
464 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468}
469
470/*
471 * X.509 v2/v3 unique identifier (not parsed)
472 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100473static int x509_get_uid(unsigned char **p,
474 const unsigned char *end,
475 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476{
Janos Follath865b3eb2019-12-16 11:46:15 +0000477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (*p == end) {
480 return 0;
481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
483 uid->tag = **p;
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
486 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
487 n)) != 0) {
488 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
489 return 0;
490 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 }
494
495 uid->p = *p;
496 *p += uid->len;
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499}
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501static int x509_get_basic_constraints(unsigned char **p,
502 const unsigned char *end,
503 int *ca_istrue,
504 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505{
Janos Follath865b3eb2019-12-16 11:46:15 +0000506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 size_t len;
508
509 /*
510 * BasicConstraints ::= SEQUENCE {
511 * cA BOOLEAN DEFAULT FALSE,
512 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
513 */
514 *ca_istrue = 0; /* DEFAULT FALSE */
515 *max_pathlen = 0; /* endless */
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
518 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
519 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (*p == end) {
523 return 0;
524 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
527 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
528 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
529 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (ret != 0) {
532 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
533 }
534
535 if (*ca_istrue != 0) {
536 *ca_istrue = 1;
537 }
538 }
539
540 if (*p == end) {
541 return 0;
542 }
543
544 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
545 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
546 }
547
548 if (*p != end) {
549 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
550 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
551 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
Andrzej Kurek16050742020-04-14 09:49:52 -0400553 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
554 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (*max_pathlen == INT_MAX) {
556 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
557 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
558 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400559
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 (*max_pathlen)++;
561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563}
564
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565/*
566 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
567 *
568 * KeyPurposeId ::= OBJECT IDENTIFIER
569 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570static int x509_get_ext_key_usage(unsigned char **p,
571 const unsigned char *end,
572 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573{
Janos Follath865b3eb2019-12-16 11:46:15 +0000574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
577 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
578 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
580 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (ext_key_usage->buf.p == NULL) {
582 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
583 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
584 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587}
588
589/*
toth92ga41954d2021-02-12 16:11:17 +0100590 * SubjectKeyIdentifier ::= KeyIdentifier
591 *
592 * KeyIdentifier ::= OCTET STRING
593 */
594static int x509_get_subject_key_id(unsigned char **p,
595 const unsigned char *end,
596 mbedtls_x509_buf *subject_key_id)
597{
598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
599 size_t len = 0u;
600
601 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
602 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100603 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100604 } else {
605 subject_key_id->len = len;
606 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
607 subject_key_id->p = *p;
608 *p += len;
609 }
610
toth92gd96027a2021-04-27 15:41:25 +0200611 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100612 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
613 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200614 }
615
toth92ga41954d2021-02-12 16:11:17 +0100616 return 0;
617}
618
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100619/*
toth92g8d435a02021-05-10 15:16:33 +0200620 * AuthorityKeyIdentifier ::= SEQUENCE {
621 * keyIdentifier [0] KeyIdentifier OPTIONAL,
622 * authorityCertIssuer [1] GeneralNames OPTIONAL,
623 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
624 *
625 * KeyIdentifier ::= OCTET STRING
626 */
627static int x509_get_authority_key_id(unsigned char **p,
628 unsigned char *end,
629 mbedtls_x509_authority *authority_key_id)
630{
631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
632 size_t len = 0u;
633
634 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100635 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100636 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200637 }
638
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100639 if (*p + len != end) {
640 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
641 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
642 }
643
Przemek Stekieled9fb782023-05-03 16:27:25 +0200644 ret = mbedtls_asn1_get_tag(p, end, &len,
645 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
646
647 /* KeyIdentifier is an OPTIONAL field */
648 if (ret != 0 && ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
649 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
650 } else if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200651 authority_key_id->keyIdentifier.len = len;
652 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200653 /* Setting tag of the keyIdentfier intentionally to 0x04.
654 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200655 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200656 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
657
658 *p += len;
659 }
660
661 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200662 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200663 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
664 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100665 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200666 /* authorityCertIssuer and authorityCertSerialNumber MUST both
667 be present or both be absent. At this point we expect to have both. */
668 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200669 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200670 /* "end" also includes the CertSerialNumber field so "len" shall be used */
671 ret = mbedtls_x509_get_subject_alt_name_ext(p,
672 (*p+len),
673 &authority_key_id->authorityCertIssuer);
674 if (ret != 0) {
675 return ret;
676 }
677
678 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
679 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
680 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
681 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
682 }
683 authority_key_id->authorityCertSerialNumber.len = len;
684 authority_key_id->authorityCertSerialNumber.p = *p;
685 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
686 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200687 }
688
689 if (*p != end) {
690 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
691 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
692 }
693
694 return 0;
695}
696
697/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200698 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
699 *
700 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
701 *
702 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
703 *
704 * PolicyInformation ::= SEQUENCE {
705 * policyIdentifier CertPolicyId,
706 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
707 * PolicyQualifierInfo OPTIONAL }
708 *
709 * CertPolicyId ::= OBJECT IDENTIFIER
710 *
711 * PolicyQualifierInfo ::= SEQUENCE {
712 * policyQualifierId PolicyQualifierId,
713 * qualifier ANY DEFINED BY policyQualifierId }
714 *
715 * -- policyQualifierIds for Internet policy qualifiers
716 *
717 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
718 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
719 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
720 *
721 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
722 *
723 * Qualifier ::= CHOICE {
724 * cPSuri CPSuri,
725 * userNotice UserNotice }
726 *
727 * CPSuri ::= IA5String
728 *
729 * UserNotice ::= SEQUENCE {
730 * noticeRef NoticeReference OPTIONAL,
731 * explicitText DisplayText OPTIONAL }
732 *
733 * NoticeReference ::= SEQUENCE {
734 * organization DisplayText,
735 * noticeNumbers SEQUENCE OF INTEGER }
736 *
737 * DisplayText ::= CHOICE {
738 * ia5String IA5String (SIZE (1..200)),
739 * visibleString VisibleString (SIZE (1..200)),
740 * bmpString BMPString (SIZE (1..200)),
741 * utf8String UTF8String (SIZE (1..200)) }
742 *
743 * NOTE: we only parse and use anyPolicy without qualifiers at this point
744 * as defined in RFC 5280.
745 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746static int x509_get_certificate_policies(unsigned char **p,
747 const unsigned char *end,
748 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200749{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300750 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200751 size_t len;
752 mbedtls_asn1_buf *buf;
753 mbedtls_asn1_sequence *cur = certificate_policies;
754
755 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 ret = mbedtls_asn1_get_tag(p, end, &len,
757 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
758 if (ret != 0) {
759 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
760 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (*p + len != end) {
763 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
764 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
765 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200766
767 /*
768 * Cannot be an empty sequence.
769 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (len == 0) {
771 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
772 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
773 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200776 mbedtls_x509_buf policy_oid;
777 const unsigned char *policy_end;
778
779 /*
780 * Get the policy sequence
781 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
783 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
784 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
785 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200786
787 policy_end = *p + len;
788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
790 MBEDTLS_ASN1_OID)) != 0) {
791 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
792 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200793
794 policy_oid.tag = MBEDTLS_ASN1_OID;
795 policy_oid.len = len;
796 policy_oid.p = *p;
797
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300798 /*
799 * Only AnyPolicy is currently supported when enforcing policy.
800 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300802 /*
803 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200804 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300805 */
806 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
807 }
808
Ron Eldor74d9acc2019-03-21 14:00:03 +0200809 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (cur->buf.p != NULL) {
811 if (cur->next != NULL) {
812 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
813 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (cur->next == NULL) {
818 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
819 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
820 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200821
822 cur = cur->next;
823 }
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200826 buf->tag = policy_oid.tag;
827 buf->p = policy_oid.p;
828 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300829
830 *p += len;
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 /*
833 * If there is an optional qualifier, then *p < policy_end
834 * Check the Qualifier len to verify it doesn't exceed policy_end.
835 */
836 if (*p < policy_end) {
837 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
838 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
839 0) {
840 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
841 }
Ron Eldor08063792019-05-13 16:38:39 +0300842 /*
843 * Skip the optional policy qualifiers.
844 */
845 *p += len;
846 }
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 if (*p != policy_end) {
849 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
850 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
851 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200852 }
853
854 /* Set final sequence entry's next pointer to NULL */
855 cur->next = NULL;
856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 if (*p != end) {
858 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
859 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
860 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200863}
864
865/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866 * X.509 v3 extensions
867 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869static int x509_get_crt_ext(unsigned char **p,
870 const unsigned char *end,
871 mbedtls_x509_crt *crt,
872 mbedtls_x509_crt_ext_cb_t cb,
873 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874{
Janos Follath865b3eb2019-12-16 11:46:15 +0000875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200877 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (*p == end) {
880 return 0;
881 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
884 return ret;
885 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886
Hanno Becker12f62fb2019-02-12 17:22:36 +0000887 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 /*
890 * Extension ::= SEQUENCE {
891 * extnID OBJECT IDENTIFIER,
892 * critical BOOLEAN DEFAULT FALSE,
893 * extnValue OCTET STRING }
894 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 int is_critical = 0; /* DEFAULT FALSE */
897 int ext_type = 0;
898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
900 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
901 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
902 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903
904 end_ext_data = *p + len;
905
906 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
908 MBEDTLS_ASN1_OID)) != 0) {
909 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
910 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911
k-stachowiak463928a2018-07-24 12:50:59 +0200912 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913 extn_oid.p = *p;
914 *p += extn_oid.len;
915
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
918 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
919 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
920 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921
922 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
924 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
925 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
926 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200928 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 end_ext_octet = *p + len;
930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (end_ext_octet != end_ext_data) {
932 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
933 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
934 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
936 /*
937 * Detect supported extensions
938 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200942 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (cb != NULL) {
944 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
945 if (ret != 0 && is_critical) {
946 return ret;
947 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200948 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200949 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200950 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200951
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952 /* No parser found, skip extension */
953 *p = end_ext_octet;
954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
958 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 continue;
961 }
962
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100963 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if ((crt->ext_types & ext_type) != 0) {
965 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
966 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100967
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 crt->ext_types |= ext_type;
969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 switch (ext_type) {
971 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
972 /* Parse basic constraints */
973 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
974 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
975 return ret;
976 }
977 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 case MBEDTLS_X509_EXT_KEY_USAGE:
980 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100981 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
982 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return ret;
984 }
985 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
988 /* Parse extended key usage */
989 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
990 &crt->ext_key_usage)) != 0) {
991 return ret;
992 }
993 break;
toth92g8d435a02021-05-10 15:16:33 +0200994
toth92ga41954d2021-02-12 16:11:17 +0100995 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
996 /* Parse subject key identifier */
997 if ((ret = x509_get_subject_key_id(p, end_ext_data,
998 &crt->subject_key_id)) != 0) {
999 return ret;
1000 }
1001 break;
toth92g8d435a02021-05-10 15:16:33 +02001002
toth92ga41954d2021-02-12 16:11:17 +01001003 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1004 /* Parse authority key identifier */
1005 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1006 &crt->authority_key_id)) != 0) {
1007 return ret;
1008 }
1009 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001011 /* Parse subject alt name
1012 * SubjectAltName ::= GeneralNames
1013 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001014 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1015 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 return ret;
1017 }
1018 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1021 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001022 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1023 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 return ret;
1025 }
1026 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1029 /* Parse certificate policies type */
1030 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1031 &crt->certificate_policies)) != 0) {
1032 /* Give the callback (if any) a chance to handle the extension
1033 * if it contains unsupported policies */
1034 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1035 cb(p_ctx, crt, &extn_oid, is_critical,
1036 start_ext_octet, end_ext_octet) == 0) {
1037 break;
1038 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (is_critical) {
1041 return ret;
1042 } else
1043 /*
1044 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1045 * cannot interpret or enforce the policy. However, it is up to
1046 * the user to choose how to enforce the policies,
1047 * unless the extension is critical.
1048 */
1049 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1050 return ret;
1051 }
1052 }
1053 break;
1054
1055 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001056 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 * If this is a non-critical extension, which the oid layer
1058 * supports, but there isn't an x509 parser for it,
1059 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001060 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (is_critical) {
1062 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1063 } else {
1064 *p = end_ext_octet;
1065 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066 }
1067 }
1068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (*p != end) {
1070 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1071 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1072 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075}
1076
1077/*
1078 * Parse and fill a single X.509 certificate in DER format
1079 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1081 const unsigned char *buf,
1082 size_t buflen,
1083 int make_copy,
1084 mbedtls_x509_crt_ext_cb_t cb,
1085 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086{
Janos Follath865b3eb2019-12-16 11:46:15 +00001087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088 size_t len;
1089 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1093 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1094 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001095
1096 /*
1097 * Check for valid input
1098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if (crt == NULL || buf == NULL) {
1100 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1101 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001103 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001105 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 end = p + len;
1107
1108 /*
1109 * Certificate ::= SEQUENCE {
1110 * tbsCertificate TBSCertificate,
1111 * signatureAlgorithm AlgorithmIdentifier,
1112 * signatureValue BIT STRING }
1113 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1115 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1116 mbedtls_x509_crt_free(crt);
1117 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 }
1119
Janos Follathcc0e49d2016-02-17 14:34:12 +00001120 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001121 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001123 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1125 if (crt->raw.p == NULL) {
1126 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1127 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001130 crt->own_buffer = 1;
1131
1132 p += crt->raw.len - len;
1133 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 } else {
1135 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001136 crt->own_buffer = 0;
1137 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001138
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139 /*
1140 * TBSCertificate ::= SEQUENCE {
1141 */
1142 crt->tbs.p = p;
1143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1145 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1146 mbedtls_x509_crt_free(crt);
1147 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 }
1149
1150 end = p + len;
1151 crt->tbs.len = end - crt->tbs.p;
1152
1153 /*
1154 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1155 *
1156 * CertificateSerialNumber ::= INTEGER
1157 *
1158 * signature AlgorithmIdentifier
1159 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1161 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1162 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1163 &sig_params1)) != 0) {
1164 mbedtls_x509_crt_free(crt);
1165 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 }
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (crt->version < 0 || crt->version > 2) {
1169 mbedtls_x509_crt_free(crt);
1170 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 }
1172
Andres AG7ca4a032017-03-09 16:16:11 +00001173 crt->version++;
1174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1176 &crt->sig_md, &crt->sig_pk,
1177 &crt->sig_opts)) != 0) {
1178 mbedtls_x509_crt_free(crt);
1179 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 }
1181
1182 /*
1183 * issuer Name
1184 */
1185 crt->issuer_raw.p = p;
1186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1188 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1189 mbedtls_x509_crt_free(crt);
1190 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 }
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1194 mbedtls_x509_crt_free(crt);
1195 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 }
1197
1198 crt->issuer_raw.len = p - crt->issuer_raw.p;
1199
1200 /*
1201 * Validity ::= SEQUENCE {
1202 * notBefore Time,
1203 * notAfter Time }
1204 *
1205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1207 &crt->valid_to)) != 0) {
1208 mbedtls_x509_crt_free(crt);
1209 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 }
1211
1212 /*
1213 * subject Name
1214 */
1215 crt->subject_raw.p = p;
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1218 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1219 mbedtls_x509_crt_free(crt);
1220 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221 }
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1224 mbedtls_x509_crt_free(crt);
1225 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226 }
1227
1228 crt->subject_raw.len = p - crt->subject_raw.p;
1229
1230 /*
1231 * SubjectPublicKeyInfo
1232 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001233 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1235 mbedtls_x509_crt_free(crt);
1236 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001238 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239
1240 /*
1241 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1242 * -- If present, version shall be v2 or v3
1243 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1244 * -- If present, version shall be v2 or v3
1245 * extensions [3] EXPLICIT Extensions OPTIONAL
1246 * -- If present, version shall be v3
1247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (crt->version == 2 || crt->version == 3) {
1249 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1250 if (ret != 0) {
1251 mbedtls_x509_crt_free(crt);
1252 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253 }
1254 }
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (crt->version == 2 || crt->version == 3) {
1257 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1258 if (ret != 0) {
1259 mbedtls_x509_crt_free(crt);
1260 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261 }
1262 }
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if (crt->version == 3) {
1265 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1266 if (ret != 0) {
1267 mbedtls_x509_crt_free(crt);
1268 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 }
1270 }
1271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 if (p != end) {
1273 mbedtls_x509_crt_free(crt);
1274 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1275 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 }
1277
1278 end = crt_end;
1279
1280 /*
1281 * }
1282 * -- end of TBSCertificate
1283 *
1284 * signatureAlgorithm AlgorithmIdentifier,
1285 * signatureValue BIT STRING
1286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1288 mbedtls_x509_crt_free(crt);
1289 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 }
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (crt->sig_oid.len != sig_oid2.len ||
1293 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001294 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001295 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 (sig_params1.len != 0 &&
1297 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1298 mbedtls_x509_crt_free(crt);
1299 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 }
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1303 mbedtls_x509_crt_free(crt);
1304 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305 }
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (p != end) {
1308 mbedtls_x509_crt_free(crt);
1309 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1310 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 }
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314}
1315
1316/*
1317 * Parse one X.509 certificate in DER format from a buffer and add them to a
1318 * chained list
1319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001320static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1321 const unsigned char *buf,
1322 size_t buflen,
1323 int make_copy,
1324 mbedtls_x509_crt_ext_cb_t cb,
1325 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001326{
Janos Follath865b3eb2019-12-16 11:46:15 +00001327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001329
1330 /*
1331 * Check for valid input
1332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (crt == NULL || buf == NULL) {
1334 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1335 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001338 prev = crt;
1339 crt = crt->next;
1340 }
1341
1342 /*
1343 * Add new certificate on the end of the chain if needed.
1344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (crt->version != 0 && crt->next == NULL) {
1346 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 if (crt->next == NULL) {
1349 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1350 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351
1352 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355 }
1356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1358 if (ret != 0) {
1359 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (crt != chain) {
1364 mbedtls_free(crt);
1365 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368 }
1369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371}
1372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1374 const unsigned char *buf,
1375 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001376{
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001378}
1379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1381 const unsigned char *buf,
1382 size_t buflen,
1383 int make_copy,
1384 mbedtls_x509_crt_ext_cb_t cb,
1385 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001386{
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001388}
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1391 const unsigned char *buf,
1392 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001393{
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001395}
1396
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001397/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001398 * Parse one or more PEM certificates from a buffer and add them to the chained
1399 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1402 const unsigned char *buf,
1403 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404{
Janos Follath98e28a72016-05-31 14:03:54 +01001405#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001406 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001408#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409
1410 /*
1411 * Check for valid input
1412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (chain == NULL || buf == NULL) {
1414 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1415 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416
1417 /*
1418 * Determine buffer content. Buffer contains either one DER certificate or
1419 * one or more PEM certificates.
1420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1423 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001425 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1428 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1429 }
Janos Follath98e28a72016-05-31 14:03:54 +01001430#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001432#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001439 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001444 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 ret = mbedtls_pem_read_buffer(&pem,
1446 "-----BEGIN CERTIFICATE-----",
1447 "-----END CERTIFICATE-----",
1448 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 /*
1452 * Was PEM encoded
1453 */
1454 buflen -= use_len;
1455 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1457 return ret;
1458 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1459 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
1461 /*
1462 * PEM header and footer were found
1463 */
1464 buflen -= use_len;
1465 buf += use_len;
1466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470
Paul Bakker5a5fa922014-09-26 14:53:04 +02001471 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482 /*
1483 * Quit parsing on a memory error
1484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1486 return ret;
1487 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492
1493 total_failed++;
1494 continue;
1495 }
1496
1497 success = 1;
1498 }
1499 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (success) {
1502 return total_failed;
1503 } else if (first_error) {
1504 return first_error;
1505 } else {
1506 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1507 }
Janos Follath98e28a72016-05-31 14:03:54 +01001508#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509}
1510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512/*
1513 * Load one or more certificates and add them to the chained list
1514 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001515int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516{
Janos Follath865b3eb2019-12-16 11:46:15 +00001517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518 size_t n;
1519 unsigned char *buf;
1520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1522 return ret;
1523 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 mbedtls_platform_zeroize(buf, n);
1528 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531}
1532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534{
1535 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001536#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001537 int w_ret;
1538 WCHAR szDir[MAX_PATH];
1539 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001540 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542
Paul Bakker9af723c2014-05-01 13:03:14 +02001543 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 HANDLE hFind;
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (len > MAX_PATH - 3) {
1547 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1548 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 memset(szDir, 0, sizeof(szDir));
1551 memset(filename, 0, MAX_PATH);
1552 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001553 filename[len++] = '\\';
1554 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555 filename[len++] = '*';
1556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1558 MAX_PATH - 3);
1559 if (w_ret == 0) {
1560 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1561 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 hFind = FindFirstFileW(szDir, &file_data);
1564 if (hFind == INVALID_HANDLE_VALUE) {
1565 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1566 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567
1568 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 do {
1570 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001577 -1,
1578 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 NULL, NULL);
1580 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001581 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1582 goto cleanup;
1583 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1586 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 }
1591 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596
Ron Eldor36d90422017-01-09 15:09:16 +02001597cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001599#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001600 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001601 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001603 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001604 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (dir == NULL) {
1608 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1609 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610
Ron Eldor63140682017-01-09 19:27:59 +02001611#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1613 closedir(dir);
1614 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001615 }
Ron Eldor63140682017-01-09 19:27:59 +02001616#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001621 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001624 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001625 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1626 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 } else if (stat(entry_name, &sb) == -1) {
1628 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001629 /* Broken symbolic link - ignore this entry.
1630 stat(2) will return this error for either (a) a dangling
1631 symlink or (b) a missing file.
1632 Given that we have just obtained the filename from readdir,
1633 assume that it does exist and therefore treat this as a
1634 dangling symlink. */
1635 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001637 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001638 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1639 goto cleanup;
1640 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641 }
1642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646
1647 // Ignore parse errors
1648 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1650 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001656
1657cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001659
Ron Eldor63140682017-01-09 19:27:59 +02001660#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 }
Ron Eldor63140682017-01-09 19:27:59 +02001664#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001665
Paul Bakkerbe089b02013-10-14 15:51:50 +02001666#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671
Peter Kolbus9a969b62018-12-11 13:55:56 -06001672#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001673#define PRINT_ITEM(i) \
1674 do { \
1675 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1676 MBEDTLS_X509_SAFE_SNPRINTF; \
1677 sep = ", "; \
1678 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001679
Przemek Stekielf4194942023-04-24 09:52:17 +02001680#define CERT_TYPE(type, name) \
1681 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001682 if (ns_cert_type & (type)) { \
1683 PRINT_ITEM(name); \
1684 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001685 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001686
Przemek Stekielf4194942023-04-24 09:52:17 +02001687#define KEY_USAGE(code, name) \
1688 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001689 if (key_usage & (code)) { \
1690 PRINT_ITEM(name); \
1691 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001692 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694static int x509_info_ext_key_usage(char **buf, size_t *size,
1695 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001696{
Janos Follath865b3eb2019-12-16 11:46:15 +00001697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001698 const char *desc;
1699 size_t n = *size;
1700 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001702 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 while (cur != NULL) {
1705 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001706 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001710 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001711
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001712 sep = ", ";
1713
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001714 cur = cur->next;
1715 }
1716
1717 *size = n;
1718 *buf = p;
1719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001721}
1722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723static int x509_info_cert_policies(char **buf, size_t *size,
1724 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001725{
Janos Follath865b3eb2019-12-16 11:46:15 +00001726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001727 const char *desc;
1728 size_t n = *size;
1729 char *p = *buf;
1730 const mbedtls_x509_sequence *cur = certificate_policies;
1731 const char *sep = "";
1732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 while (cur != NULL) {
1734 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001735 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001739 MBEDTLS_X509_SAFE_SNPRINTF;
1740
1741 sep = ", ";
1742
1743 cur = cur->next;
1744 }
1745
1746 *size = n;
1747 *buf = p;
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001750}
1751
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752/*
1753 * Return an informational string about the certificate.
1754 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001755#define BEFORE_COLON 18
1756#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001757int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1758 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759{
Janos Follath865b3eb2019-12-16 11:46:15 +00001760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761 size_t n;
1762 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001763 char key_size_str[BEFORE_COLON];
1764
1765 p = buf;
1766 n = size;
1767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if (NULL == crt) {
1769 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001770 MBEDTLS_X509_SAFE_SNPRINTF;
1771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001773 }
1774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1776 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001777 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1779 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001780 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001783 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001786 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001788 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001791 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
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%sissued on : " \
1796 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1797 crt->valid_from.year, crt->valid_from.mon,
1798 crt->valid_from.day, crt->valid_from.hour,
1799 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001800 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1803 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1804 crt->valid_to.year, crt->valid_to.mon,
1805 crt->valid_to.day, crt->valid_to.hour,
1806 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001807 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
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_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1813 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001814 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001816 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1818 mbedtls_pk_get_name(&crt->pk))) != 0) {
1819 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820 }
1821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1823 (int) mbedtls_pk_get_bitlen(&crt->pk));
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 /*
1827 * Optional extensions
1828 */
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1831 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1832 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001833 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (crt->max_pathlen > 0) {
1836 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001837 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001838 }
1839 }
1840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1842 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001843 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001844
Przemek Stekiel21c37282023-01-16 08:47:49 +01001845 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1846 &crt->subject_alt_names,
1847 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return ret;
1849 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001850 }
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1853 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001854 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001855
Przemek Stekiel21c37282023-01-16 08:47:49 +01001856 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return ret;
1858 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001859 }
1860
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1862 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001863 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001864
Przemek Stekiel21c37282023-01-16 08:47:49 +01001865 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 return ret;
1867 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001868 }
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1871 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001872 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if ((ret = x509_info_ext_key_usage(&p, &n,
1875 &crt->ext_key_usage)) != 0) {
1876 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_OID_X509_EXT_CERTIFICATE_POLICIES) {
1881 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001882 MBEDTLS_X509_SAFE_SNPRINTF;
1883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 if ((ret = x509_info_cert_policies(&p, &n,
1885 &crt->certificate_policies)) != 0) {
1886 return ret;
1887 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001888 }
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001891 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001894}
1895
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001896struct x509_crt_verify_string {
1897 int code;
1898 const char *string;
1899};
1900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001902static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001903 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001904 { 0, NULL }
1905};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001906#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1909 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001910{
Janos Follath865b3eb2019-12-16 11:46:15 +00001911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001912 const struct x509_crt_verify_string *cur;
1913 char *p = buf;
1914 size_t n = size;
1915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1917 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001918 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001922 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001923 flags ^= cur->code;
1924 }
1925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 if (flags != 0) {
1927 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1928 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001929 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001930 }
1931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001933}
Hanno Becker612a2f12020-10-09 09:19:39 +01001934#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1937 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001938{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001939 unsigned int usage_must, usage_may;
1940 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1944 return 0;
1945 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001946
1947 usage_must = usage & ~may_mask;
1948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1950 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1951 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001952
1953 usage_may = usage & may_mask;
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1956 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1957 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001960}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1963 const char *usage_oid,
1964 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001967
1968 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1970 return 0;
1971 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001972
1973 /*
1974 * Look for the requested usage (or wildcard ANY) in our list
1975 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001977 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001978
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (cur_oid->len == usage_len &&
1980 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1981 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001982 }
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1985 return 0;
1986 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001987 }
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001990}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001993/*
1994 * Return 1 if the certificate is revoked, or 0 otherwise.
1995 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001996int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001997{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 while (cur != NULL && cur->serial.len != 0) {
2001 if (crt->serial.len == cur->serial.len &&
2002 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2003 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002004 }
2005
2006 cur = cur->next;
2007 }
2008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002010}
2011
2012/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002013 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002014 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002016static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2017 mbedtls_x509_crl *crl_list,
2018 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002019{
2020 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002021 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002022#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002023 psa_algorithm_t psa_algorithm;
2024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002026#endif /* MBEDTLS_USE_PSA_CRYPTO */
2027 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (ca == NULL) {
2030 return flags;
2031 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 while (crl_list != NULL) {
2034 if (crl_list->version == 0 ||
2035 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002036 crl_list = crl_list->next;
2037 continue;
2038 }
2039
2040 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002041 * Check if the CA is configured to sign CRLs
2042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (mbedtls_x509_crt_check_key_usage(ca,
2044 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002046 break;
2047 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002048
2049 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002050 * Check if CRL is correctly signed by the trusted CA
2051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002053 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002057 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002059
pespacek7599a772022-02-07 14:40:55 +01002060#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
2062 if (psa_hash_compute(psa_algorithm,
2063 crl_list->tbs.p,
2064 crl_list->tbs.len,
2065 hash,
2066 sizeof(hash),
2067 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002068 /* Note: this can't happen except after an internal error */
2069 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2070 break;
2071 }
pespacek7599a772022-02-07 14:40:55 +01002072#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2074 hash_length = mbedtls_md_get_size(md_info);
2075 if (mbedtls_md(md_info,
2076 crl_list->tbs.p,
2077 crl_list->tbs.len,
2078 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002079 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002081 break;
2082 }
pespaceka7a64692022-02-14 15:18:43 +01002083#endif /* MBEDTLS_USE_PSA_CRYPTO */
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002086 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2090 crl_list->sig_md, hash, hash_length,
2091 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093 break;
2094 }
2095
2096 /*
2097 * Check for validity of CRL (Do not drop out)
2098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002104 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002106
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107 /*
2108 * Check if certificate is revoked
2109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112 break;
2113 }
2114
2115 crl_list = crl_list->next;
2116 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002121
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002122/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002123 * Check the signature of a certificate by its parent
2124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002125static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2126 mbedtls_x509_crt *parent,
2127 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002128{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002129 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002130 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002131#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2132 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 md_info = mbedtls_md_info_from_type(child->sig_md);
2134 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002135
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002136 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2138 return -1;
2139 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002140#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 status = psa_hash_compute(hash_alg,
2145 child->tbs.p,
2146 child->tbs.len,
2147 hash,
2148 sizeof(hash),
2149 &hash_len);
2150 if (status != PSA_SUCCESS) {
2151 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002152 }
2153
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002154#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002155 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2157 return -1;
2158 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002159
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002160#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2162 return mbedtls_pk_verify_restartable(&parent->pk,
2163 child->sig_md, hash, hash_len,
2164 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002165 }
2166#else
2167 (void) rs_ctx;
2168#endif
2169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2171 child->sig_md, hash, hash_len,
2172 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002173}
2174
2175/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002176 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2177 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002178 *
2179 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2182 const mbedtls_x509_crt *parent,
2183 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002184{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002185 int need_ca_bit;
2186
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002187 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2189 return -1;
2190 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002191
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002192 /* Parent must have the basicConstraints CA bit set as a general rule */
2193 need_ca_bit = 1;
2194
2195 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002197 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002198 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (need_ca_bit && !parent->ca_istrue) {
2201 return -1;
2202 }
2203
2204 if (need_ca_bit &&
2205 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2206 return -1;
2207 }
2208
2209 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002210}
2211
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002212/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002213 * Find a suitable parent for child in candidates, or return NULL.
2214 *
2215 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002216 * 1. subject name matches child's issuer
2217 * 2. if necessary, the CA bit is set and key usage allows signing certs
2218 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002219 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002220 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002221 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002222 * If there's a suitable candidate which is also time-valid, return the first
2223 * such. Otherwise, return the first suitable candidate (or NULL if there is
2224 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002225 *
2226 * The rationale for this rule is that someone could have a list of trusted
2227 * roots with two versions on the same root with different validity periods.
2228 * (At least one user reported having such a list and wanted it to just work.)
2229 * The reason we don't just require time-validity is that generally there is
2230 * only one version, and if it's expired we want the flags to state that
2231 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002232 *
2233 * The rationale for rule 3 (signature for trusted roots) is that users might
2234 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002235 * way we select the correct one is by checking the signature (as we don't
2236 * rely on key identifier extensions). (This is one way users might choose to
2237 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002238 *
2239 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002240 * - [in] child: certificate for which we're looking for a parent
2241 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002242 * - [out] r_parent: parent found (or NULL)
2243 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002244 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2245 * of the chain, 0 otherwise
2246 * - [in] path_cnt: number of intermediates seen so far
2247 * - [in] self_cnt: number of self-signed intermediates seen so far
2248 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002249 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002250 *
2251 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002252 * - 0 on success
2253 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002254 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002255static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 mbedtls_x509_crt *child,
2257 mbedtls_x509_crt *candidates,
2258 mbedtls_x509_crt **r_parent,
2259 int *r_signature_is_good,
2260 int top,
2261 unsigned path_cnt,
2262 unsigned self_cnt,
2263 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002264{
Janos Follath865b3eb2019-12-16 11:46:15 +00002265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002266 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002267 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002268
2269#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002270 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002272 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002273 parent = rs_ctx->parent;
2274 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002275 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002276
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002277 /* clear saved state */
2278 rs_ctx->parent = NULL;
2279 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002280 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002281
2282 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002283 goto check_signature;
2284 }
2285#endif
2286
2287 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002288 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002291 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002293 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002295
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002296 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (parent->max_pathlen > 0 &&
2298 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002299 continue;
2300 }
2301
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002302 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002303#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2304check_signature:
2305#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002307
2308#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002310 /* save state */
2311 rs_ctx->parent = parent;
2312 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002313 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002316 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002317#else
2318 (void) ret;
2319#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002320
2321 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002323 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002325
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002326 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2328 mbedtls_x509_time_is_future(&parent->valid_from)) {
2329 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002330 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002331 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002332 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002333
2334 continue;
2335 }
2336
Andy Gross1f627142019-01-30 10:25:53 -06002337 *r_parent = parent;
2338 *r_signature_is_good = signature_is_good;
2339
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002340 break;
2341 }
2342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002344 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002345 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002346 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002349}
2350
2351/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002352 * Find a parent in trusted CAs or the provided chain, or return NULL.
2353 *
2354 * Searches in trusted CAs first, and return the first suitable parent found
2355 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002356 *
2357 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002358 * - [in] child: certificate for which we're looking for a parent, followed
2359 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002360 * - [in] trust_ca: list of locally trusted certificates
2361 * - [out] parent: parent found (or NULL)
2362 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2363 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2364 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2365 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002366 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002367 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002368 *
2369 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002370 * - 0 on success
2371 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002372 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002373static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 mbedtls_x509_crt *child,
2375 mbedtls_x509_crt *trust_ca,
2376 mbedtls_x509_crt **parent,
2377 int *parent_is_trusted,
2378 int *signature_is_good,
2379 unsigned path_cnt,
2380 unsigned self_cnt,
2381 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002382{
Janos Follath865b3eb2019-12-16 11:46:15 +00002383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002384 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002385
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002386 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002387
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002388#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002389 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002391 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002392 rs_ctx->parent_is_trusted = -1;
2393 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002394#endif
2395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002397 search_list = *parent_is_trusted ? trust_ca : child->next;
2398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 ret = x509_crt_find_parent_in(child, search_list,
2400 parent, signature_is_good,
2401 *parent_is_trusted,
2402 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002403
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002404#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002406 /* save state */
2407 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002409 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002410#else
2411 (void) ret;
2412#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002413
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002414 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002416 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002418
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002419 /* prepare second iteration */
2420 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002421 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002422
2423 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002425 *parent_is_trusted = 0;
2426 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002427 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002430}
2431
2432/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002433 * Check if an end-entity certificate is locally trusted
2434 *
2435 * Currently we require such certificates to be self-signed (actually only
2436 * check for self-issued as self-signatures are not checked)
2437 */
2438static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 mbedtls_x509_crt *crt,
2440 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002441{
2442 mbedtls_x509_crt *cur;
2443
2444 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2446 return -1;
2447 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002448
2449 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2451 if (crt->raw.len == cur->raw.len &&
2452 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2453 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002454 }
2455 }
2456
2457 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002459}
2460
2461/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002462 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002463 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002464 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2465 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002466 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002467 * such that every cert in the chain is a child of the next one,
2468 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002469 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002470 * Verify that chain and return it with flags for all issues found.
2471 *
2472 * Special cases:
2473 * - EE == Rj -> return a one-element list containing it
2474 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2475 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002476 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002477 * Tests for (aspects of) this function should include at least:
2478 * - trusted EE
2479 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002480 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002481 * - if relevant: EE untrusted
2482 * - if relevant: EE -> intermediate, untrusted
2483 * with the aspect under test checked at each relevant level (EE, int, root).
2484 * For some aspects longer chains are required, but usually length 2 is
2485 * enough (but length 1 is not in general).
2486 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002487 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002488 * - [in] crt: the cert list EE, C1, ..., Cn
2489 * - [in] trust_ca: the trusted list R1, ..., Rp
2490 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002491 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002492 * Only valid when return value is 0, may contain garbage otherwise!
2493 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002494 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002495 *
2496 * Return value:
2497 * - non-zero if the chain could not be fully built and examined
2498 * - 0 is the chain was successfully built and examined,
2499 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002500 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002501static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 mbedtls_x509_crt *crt,
2503 mbedtls_x509_crt *trust_ca,
2504 mbedtls_x509_crl *ca_crl,
2505 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2506 void *p_ca_cb,
2507 const mbedtls_x509_crt_profile *profile,
2508 mbedtls_x509_crt_verify_chain *ver_chain,
2509 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002510{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002511 /* Don't initialize any of those variables here, so that the compiler can
2512 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002514 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002515 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002516 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002517 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002518 int parent_is_trusted;
2519 int child_is_trusted;
2520 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002521 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002522 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002523
2524#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2525 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002527 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002528 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002529 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002530
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002531 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002532 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002533 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002534 flags = &cur->flags;
2535
2536 goto find_parent;
2537 }
2538#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002539
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002540 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002541 self_cnt = 0;
2542 parent_is_trusted = 0;
2543 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002546 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002547 cur = &ver_chain->items[ver_chain->len];
2548 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002549 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002550 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002551 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002552
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002553 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002555 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002559 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002561
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002562 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (child_is_trusted) {
2564 return 0;
2565 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002566
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002567 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002569 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002573 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002575
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002576 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (ver_chain->len == 1 &&
2578 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2579 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002580 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002581
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2583find_parent:
2584#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002585
2586 /* Obtain list of potential trusted signers from CA callback,
2587 * or use statically provided list. */
2588#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 if (f_ca_cb != NULL) {
2590 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2591 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002592 ver_chain->trust_ca_cb_result = NULL;
2593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2595 if (ret != 0) {
2596 return MBEDTLS_ERR_X509_FATAL_ERROR;
2597 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002598
2599 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002601#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2602 {
2603 ((void) f_ca_cb);
2604 ((void) p_ca_cb);
2605 cur_trust_ca = trust_ca;
2606 }
2607
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002608 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2610 &parent_is_trusted, &signature_is_good,
2611 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002612
2613#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002615 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002616 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002617 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002618 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002621 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002622#else
2623 (void) ret;
2624#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002625
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002626 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002628 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002630 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002631
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002632 /* Count intermediate self-issued (not necessarily self-signed) certs.
2633 * These can occur with some strategies for key rollover, see [SIRO],
2634 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (ver_chain->len != 1 &&
2636 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002637 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002638 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002639
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002640 /* path_cnt is 0 for the first intermediate CA,
2641 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (!parent_is_trusted &&
2643 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002644 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002646 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002647
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002648 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002650 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002652
2653 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002655 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002659 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002661#else
2662 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002663#endif
2664
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002665 /* prepare for next iteration */
2666 child = parent;
2667 parent = NULL;
2668 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002669 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002670 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002671}
2672
2673/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002674 * Check for CN match
2675 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002676static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2677 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002678{
2679 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (name->len == cn_len &&
2681 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2682 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002683 }
2684
2685 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (x509_check_wildcard(cn, name) == 0) {
2687 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002688 }
2689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002691}
2692
2693/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002694 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2695 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002696static int x509_crt_check_san(const mbedtls_x509_buf *name,
2697 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002698{
2699 const unsigned char san_type = (unsigned char) name->tag &
2700 MBEDTLS_ASN1_TAG_VALUE_MASK;
2701
2702 /* dNSName */
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
2704 return x509_crt_check_cn(name, cn, cn_len);
2705 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002706
2707 /* (We may handle other types here later.) */
2708
2709 /* Unrecognized type */
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002711}
2712
2713/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002714 * Verify the requested CN - only call this if cn is not NULL!
2715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002716static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2717 const char *cn,
2718 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002719{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002720 const mbedtls_x509_name *name;
2721 const mbedtls_x509_sequence *cur;
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2725 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
2726 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002727 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002728 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002729 }
2730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002732 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 }
2734 } else {
2735 for (name = &crt->subject; name != NULL; name = name->next) {
2736 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2737 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
2738 break;
2739 }
2740 }
2741
2742 if (name == NULL) {
2743 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2744 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002745 }
2746}
2747
2748/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002749 * Merge the flags for all certs in the chain, after calling callback
2750 */
2751static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 uint32_t *flags,
2753 const mbedtls_x509_crt_verify_chain *ver_chain,
2754 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2755 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002756{
Janos Follath865b3eb2019-12-16 11:46:15 +00002757 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002758 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002759 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002760 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002763 cur = &ver_chain->items[i-1];
2764 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 if (NULL != f_vrfy) {
2767 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
2768 return ret;
2769 }
2770 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002771
2772 *flags |= cur_flags;
2773 }
2774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002776}
2777
2778/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002779 * Verify the certificate validity, with profile, restartable version
2780 *
2781 * This function:
2782 * - checks the requested CN (if any)
2783 * - checks the type and size of the EE cert's key,
2784 * as that isn't done as part of chain building/verification currently
2785 * - builds and verifies the chain
2786 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00002787 *
2788 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
2789 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
2790 * verification routine to search for trusted signers, and CRLs will
2791 * be disabled. Otherwise, `trust_ca` will be used as the static list
2792 * of trusted signers, and `ca_crl` will be use as the static list
2793 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002794 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002795static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
2796 mbedtls_x509_crt *trust_ca,
2797 mbedtls_x509_crl *ca_crl,
2798 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2799 void *p_ca_cb,
2800 const mbedtls_x509_crt_profile *profile,
2801 const char *cn, uint32_t *flags,
2802 int (*f_vrfy)(void *,
2803 mbedtls_x509_crt *,
2804 int,
2805 uint32_t *),
2806 void *p_vrfy,
2807 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002808{
Janos Follath865b3eb2019-12-16 11:46:15 +00002809 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002810 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002811 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002812 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002813
2814 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002815 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002819 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2820 goto exit;
2821 }
2822
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002823 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 if (cn != NULL) {
2825 x509_crt_verify_name(crt, cn, &ee_flags);
2826 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002827
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002828 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002832 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002836 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002838
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002839 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
2841 f_ca_cb, p_ca_cb, profile,
2842 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002843
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002845 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002847
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002848 /* Merge end-entity flags */
2849 ver_chain.items[0].flags |= ee_flags;
2850
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002851 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002853
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002854exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00002855
2856#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
2858 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002859 ver_chain.trust_ca_cb_result = NULL;
2860#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2861
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002862#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
2864 mbedtls_x509_crt_restart_free(rs_ctx);
2865 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002866#endif
2867
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002868 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2869 * the SSL module for authmode optional, but non-zero return from the
2870 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002872 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002873 }
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 if (ret != 0) {
2876 *flags = (uint32_t) -1;
2877 return ret;
2878 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 if (*flags != 0) {
2881 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
2882 }
2883
2884 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002885}
2886
Hanno Becker3116fb32019-03-28 13:34:42 +00002887
2888/*
2889 * Verify the certificate validity (default profile, not restartable)
2890 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002891int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
2892 mbedtls_x509_crt *trust_ca,
2893 mbedtls_x509_crl *ca_crl,
2894 const char *cn, uint32_t *flags,
2895 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2896 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002897{
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2899 NULL, NULL,
2900 &mbedtls_x509_crt_profile_default,
2901 cn, flags,
2902 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002903}
2904
2905/*
2906 * Verify the certificate validity (user-chosen profile, not restartable)
2907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002908int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
2909 mbedtls_x509_crt *trust_ca,
2910 mbedtls_x509_crl *ca_crl,
2911 const mbedtls_x509_crt_profile *profile,
2912 const char *cn, uint32_t *flags,
2913 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2914 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002915{
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2917 NULL, NULL,
2918 profile, cn, flags,
2919 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002920}
2921
2922#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2923/*
2924 * Verify the certificate validity (user-chosen profile, CA callback,
2925 * not restartable).
2926 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002927int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
2928 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2929 void *p_ca_cb,
2930 const mbedtls_x509_crt_profile *profile,
2931 const char *cn, uint32_t *flags,
2932 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2933 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002934{
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
2936 f_ca_cb, p_ca_cb,
2937 profile, cn, flags,
2938 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002939}
2940#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
2943 mbedtls_x509_crt *trust_ca,
2944 mbedtls_x509_crl *ca_crl,
2945 const mbedtls_x509_crt_profile *profile,
2946 const char *cn, uint32_t *flags,
2947 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2948 void *p_vrfy,
2949 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00002950{
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2952 NULL, NULL,
2953 profile, cn, flags,
2954 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00002955}
2956
2957
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002958/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002959 * Initialize a certificate chain
2960 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002961void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02002962{
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02002964}
2965
2966/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002967 * Unallocate all certificate data
2968 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002969void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 mbedtls_x509_crt *cert_cur = crt;
2972 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 while (cert_cur != NULL) {
2975 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002979#endif
2980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
2982 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
2983 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
2984 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
2985 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
2988 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
2989 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002990 }
2991
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002992 cert_prv = cert_cur;
2993 cert_cur = cert_cur->next;
2994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
2996 if (cert_prv != crt) {
2997 mbedtls_free(cert_prv);
2998 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002999 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003000}
3001
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003002#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3003/*
3004 * Initialize a restart context
3005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003006void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003007{
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003009
3010 ctx->parent = NULL;
3011 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003012 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003013
3014 ctx->parent_is_trusted = -1;
3015
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003016 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003017 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003019}
3020
3021/*
3022 * Free the components of a restart context
3023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003024void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003025{
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003027 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003029
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 mbedtls_pk_restart_free(&ctx->pk);
3031 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003032}
3033#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#endif /* MBEDTLS_X509_CRT_PARSE_C */