blob: c373913c47e28b234aabf85fc564f773148ffae4 [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
toth92g8d435a02021-05-10 15:16:33 +0200644 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
645 MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0) {
646 /* KeyIdentifier is an OPTIONAL field */
647 } else {
648 authority_key_id->keyIdentifier.len = len;
649 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200650 /* Setting tag of the keyIdentfier intentionally to 0x04.
651 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200652 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200653 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
654
655 *p += len;
656 }
657
658 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200659 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200660 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
661 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100662 1)) != 0) {
toth92g8d435a02021-05-10 15:16:33 +0200663 /* authorityCertIssuer is an OPTIONAL field */
664 } else {
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100665 /* "end" also includes the CertSerialNumber field so "len" shall be used */
Przemek Stekiel725688b2023-04-04 22:49:44 +0200666 ret = mbedtls_x509_get_subject_alt_name_ext(p,
667 (*p+len),
668 &authority_key_id->authorityCertIssuer);
toth92g8d435a02021-05-10 15:16:33 +0200669 }
670 }
671
672 if (*p < end) {
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100673 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
toth92g8d435a02021-05-10 15:16:33 +0200674 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel725688b2023-04-04 22:49:44 +0200675 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_INTEGER |
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200676 2)) != 0) {
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100677 /* authorityCertSerialNumber is an OPTIONAL field */
Przemek Stekiel75653b12023-02-01 11:31:32 +0100678 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200679 } else {
680 authority_key_id->authorityCertSerialNumber.len = len;
681 authority_key_id->authorityCertSerialNumber.p = *p;
682 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_OCTET_STRING;
683 *p += len;
684 }
685 }
686
687 if (*p != end) {
688 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
689 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
690 }
691
692 return 0;
693}
694
695/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200696 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
697 *
698 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
699 *
700 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
701 *
702 * PolicyInformation ::= SEQUENCE {
703 * policyIdentifier CertPolicyId,
704 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
705 * PolicyQualifierInfo OPTIONAL }
706 *
707 * CertPolicyId ::= OBJECT IDENTIFIER
708 *
709 * PolicyQualifierInfo ::= SEQUENCE {
710 * policyQualifierId PolicyQualifierId,
711 * qualifier ANY DEFINED BY policyQualifierId }
712 *
713 * -- policyQualifierIds for Internet policy qualifiers
714 *
715 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
716 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
717 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
718 *
719 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
720 *
721 * Qualifier ::= CHOICE {
722 * cPSuri CPSuri,
723 * userNotice UserNotice }
724 *
725 * CPSuri ::= IA5String
726 *
727 * UserNotice ::= SEQUENCE {
728 * noticeRef NoticeReference OPTIONAL,
729 * explicitText DisplayText OPTIONAL }
730 *
731 * NoticeReference ::= SEQUENCE {
732 * organization DisplayText,
733 * noticeNumbers SEQUENCE OF INTEGER }
734 *
735 * DisplayText ::= CHOICE {
736 * ia5String IA5String (SIZE (1..200)),
737 * visibleString VisibleString (SIZE (1..200)),
738 * bmpString BMPString (SIZE (1..200)),
739 * utf8String UTF8String (SIZE (1..200)) }
740 *
741 * NOTE: we only parse and use anyPolicy without qualifiers at this point
742 * as defined in RFC 5280.
743 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744static int x509_get_certificate_policies(unsigned char **p,
745 const unsigned char *end,
746 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200747{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300748 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200749 size_t len;
750 mbedtls_asn1_buf *buf;
751 mbedtls_asn1_sequence *cur = certificate_policies;
752
753 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 ret = mbedtls_asn1_get_tag(p, end, &len,
755 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
756 if (ret != 0) {
757 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
758 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (*p + len != end) {
761 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
762 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
763 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200764
765 /*
766 * Cannot be an empty sequence.
767 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (len == 0) {
769 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
770 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
771 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200774 mbedtls_x509_buf policy_oid;
775 const unsigned char *policy_end;
776
777 /*
778 * Get the policy sequence
779 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
781 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
782 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
783 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200784
785 policy_end = *p + len;
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
788 MBEDTLS_ASN1_OID)) != 0) {
789 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
790 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200791
792 policy_oid.tag = MBEDTLS_ASN1_OID;
793 policy_oid.len = len;
794 policy_oid.p = *p;
795
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300796 /*
797 * Only AnyPolicy is currently supported when enforcing policy.
798 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300800 /*
801 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200802 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300803 */
804 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
805 }
806
Ron Eldor74d9acc2019-03-21 14:00:03 +0200807 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 if (cur->buf.p != NULL) {
809 if (cur->next != NULL) {
810 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
811 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (cur->next == NULL) {
816 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
817 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
818 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200819
820 cur = cur->next;
821 }
822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200824 buf->tag = policy_oid.tag;
825 buf->p = policy_oid.p;
826 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300827
828 *p += len;
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 /*
831 * If there is an optional qualifier, then *p < policy_end
832 * Check the Qualifier len to verify it doesn't exceed policy_end.
833 */
834 if (*p < policy_end) {
835 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
836 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
837 0) {
838 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
839 }
Ron Eldor08063792019-05-13 16:38:39 +0300840 /*
841 * Skip the optional policy qualifiers.
842 */
843 *p += len;
844 }
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (*p != policy_end) {
847 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
848 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
849 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200850 }
851
852 /* Set final sequence entry's next pointer to NULL */
853 cur->next = NULL;
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (*p != end) {
856 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
857 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
858 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200861}
862
863/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864 * X.509 v3 extensions
865 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867static int x509_get_crt_ext(unsigned char **p,
868 const unsigned char *end,
869 mbedtls_x509_crt *crt,
870 mbedtls_x509_crt_ext_cb_t cb,
871 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872{
Janos Follath865b3eb2019-12-16 11:46:15 +0000873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200875 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if (*p == end) {
878 return 0;
879 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
882 return ret;
883 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884
Hanno Becker12f62fb2019-02-12 17:22:36 +0000885 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 /*
888 * Extension ::= SEQUENCE {
889 * extnID OBJECT IDENTIFIER,
890 * critical BOOLEAN DEFAULT FALSE,
891 * extnValue OCTET STRING }
892 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 int is_critical = 0; /* DEFAULT FALSE */
895 int ext_type = 0;
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
898 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
899 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
900 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901
902 end_ext_data = *p + len;
903
904 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
906 MBEDTLS_ASN1_OID)) != 0) {
907 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
908 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909
k-stachowiak463928a2018-07-24 12:50:59 +0200910 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 extn_oid.p = *p;
912 *p += extn_oid.len;
913
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
916 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
917 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
918 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
920 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
922 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
923 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
924 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200926 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 end_ext_octet = *p + len;
928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if (end_ext_octet != end_ext_data) {
930 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
931 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
932 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
934 /*
935 * Detect supported extensions
936 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200940 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (cb != NULL) {
942 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
943 if (ret != 0 && is_critical) {
944 return ret;
945 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200946 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200947 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200948 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200949
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 /* No parser found, skip extension */
951 *p = end_ext_octet;
952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
956 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 continue;
959 }
960
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100961 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if ((crt->ext_types & ext_type) != 0) {
963 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
964 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100965
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966 crt->ext_types |= ext_type;
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 switch (ext_type) {
969 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
970 /* Parse basic constraints */
971 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
972 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
973 return ret;
974 }
975 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 case MBEDTLS_X509_EXT_KEY_USAGE:
978 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100979 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
980 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return ret;
982 }
983 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
986 /* Parse extended key usage */
987 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
988 &crt->ext_key_usage)) != 0) {
989 return ret;
990 }
991 break;
toth92g8d435a02021-05-10 15:16:33 +0200992
toth92ga41954d2021-02-12 16:11:17 +0100993 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
994 /* Parse subject key identifier */
995 if ((ret = x509_get_subject_key_id(p, end_ext_data,
996 &crt->subject_key_id)) != 0) {
997 return ret;
998 }
999 break;
toth92g8d435a02021-05-10 15:16:33 +02001000
toth92ga41954d2021-02-12 16:11:17 +01001001 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1002 /* Parse authority key identifier */
1003 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1004 &crt->authority_key_id)) != 0) {
1005 return ret;
1006 }
1007 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001009 /* Parse subject alt name
1010 * SubjectAltName ::= GeneralNames
1011 */
Przemek Stekiel21903ec2023-02-21 08:32:37 +01001012 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1013 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 return ret;
1015 }
1016 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1019 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001020 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1021 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 return ret;
1023 }
1024 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1027 /* Parse certificate policies type */
1028 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1029 &crt->certificate_policies)) != 0) {
1030 /* Give the callback (if any) a chance to handle the extension
1031 * if it contains unsupported policies */
1032 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1033 cb(p_ctx, crt, &extn_oid, is_critical,
1034 start_ext_octet, end_ext_octet) == 0) {
1035 break;
1036 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if (is_critical) {
1039 return ret;
1040 } else
1041 /*
1042 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1043 * cannot interpret or enforce the policy. However, it is up to
1044 * the user to choose how to enforce the policies,
1045 * unless the extension is critical.
1046 */
1047 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1048 return ret;
1049 }
1050 }
1051 break;
1052
1053 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001054 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 * If this is a non-critical extension, which the oid layer
1056 * supports, but there isn't an x509 parser for it,
1057 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001058 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (is_critical) {
1060 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1061 } else {
1062 *p = end_ext_octet;
1063 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064 }
1065 }
1066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (*p != end) {
1068 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1069 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1070 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073}
1074
1075/*
1076 * Parse and fill a single X.509 certificate in DER format
1077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1079 const unsigned char *buf,
1080 size_t buflen,
1081 int make_copy,
1082 mbedtls_x509_crt_ext_cb_t cb,
1083 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084{
Janos Follath865b3eb2019-12-16 11:46:15 +00001085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 size_t len;
1087 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1091 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1092 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093
1094 /*
1095 * Check for valid input
1096 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (crt == NULL || buf == NULL) {
1098 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1099 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001101 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001103 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104 end = p + len;
1105
1106 /*
1107 * Certificate ::= SEQUENCE {
1108 * tbsCertificate TBSCertificate,
1109 * signatureAlgorithm AlgorithmIdentifier,
1110 * signatureValue BIT STRING }
1111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1113 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1114 mbedtls_x509_crt_free(crt);
1115 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116 }
1117
Janos Follathcc0e49d2016-02-17 14:34:12 +00001118 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001119 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001121 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1123 if (crt->raw.p == NULL) {
1124 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1125 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001128 crt->own_buffer = 1;
1129
1130 p += crt->raw.len - len;
1131 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 } else {
1133 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001134 crt->own_buffer = 0;
1135 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001136
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137 /*
1138 * TBSCertificate ::= SEQUENCE {
1139 */
1140 crt->tbs.p = p;
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1143 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1144 mbedtls_x509_crt_free(crt);
1145 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146 }
1147
1148 end = p + len;
1149 crt->tbs.len = end - crt->tbs.p;
1150
1151 /*
1152 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1153 *
1154 * CertificateSerialNumber ::= INTEGER
1155 *
1156 * signature AlgorithmIdentifier
1157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1159 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1160 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1161 &sig_params1)) != 0) {
1162 mbedtls_x509_crt_free(crt);
1163 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 }
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (crt->version < 0 || crt->version > 2) {
1167 mbedtls_x509_crt_free(crt);
1168 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 }
1170
Andres AG7ca4a032017-03-09 16:16:11 +00001171 crt->version++;
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1174 &crt->sig_md, &crt->sig_pk,
1175 &crt->sig_opts)) != 0) {
1176 mbedtls_x509_crt_free(crt);
1177 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 }
1179
1180 /*
1181 * issuer Name
1182 */
1183 crt->issuer_raw.p = p;
1184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1186 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1187 mbedtls_x509_crt_free(crt);
1188 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 }
1190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1192 mbedtls_x509_crt_free(crt);
1193 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 }
1195
1196 crt->issuer_raw.len = p - crt->issuer_raw.p;
1197
1198 /*
1199 * Validity ::= SEQUENCE {
1200 * notBefore Time,
1201 * notAfter Time }
1202 *
1203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1205 &crt->valid_to)) != 0) {
1206 mbedtls_x509_crt_free(crt);
1207 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 }
1209
1210 /*
1211 * subject Name
1212 */
1213 crt->subject_raw.p = p;
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1216 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1217 mbedtls_x509_crt_free(crt);
1218 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 }
1220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1222 mbedtls_x509_crt_free(crt);
1223 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224 }
1225
1226 crt->subject_raw.len = p - crt->subject_raw.p;
1227
1228 /*
1229 * SubjectPublicKeyInfo
1230 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001231 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1233 mbedtls_x509_crt_free(crt);
1234 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001236 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237
1238 /*
1239 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1240 * -- If present, version shall be v2 or v3
1241 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1242 * -- If present, version shall be v2 or v3
1243 * extensions [3] EXPLICIT Extensions OPTIONAL
1244 * -- If present, version shall be v3
1245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (crt->version == 2 || crt->version == 3) {
1247 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1248 if (ret != 0) {
1249 mbedtls_x509_crt_free(crt);
1250 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 }
1252 }
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (crt->version == 2 || crt->version == 3) {
1255 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1256 if (ret != 0) {
1257 mbedtls_x509_crt_free(crt);
1258 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259 }
1260 }
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (crt->version == 3) {
1263 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1264 if (ret != 0) {
1265 mbedtls_x509_crt_free(crt);
1266 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 }
1268 }
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (p != end) {
1271 mbedtls_x509_crt_free(crt);
1272 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1273 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 }
1275
1276 end = crt_end;
1277
1278 /*
1279 * }
1280 * -- end of TBSCertificate
1281 *
1282 * signatureAlgorithm AlgorithmIdentifier,
1283 * signatureValue BIT STRING
1284 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1286 mbedtls_x509_crt_free(crt);
1287 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 }
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if (crt->sig_oid.len != sig_oid2.len ||
1291 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001292 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001293 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 (sig_params1.len != 0 &&
1295 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1296 mbedtls_x509_crt_free(crt);
1297 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001298 }
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1301 mbedtls_x509_crt_free(crt);
1302 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 }
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 if (p != end) {
1306 mbedtls_x509_crt_free(crt);
1307 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1308 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 }
1310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312}
1313
1314/*
1315 * Parse one X.509 certificate in DER format from a buffer and add them to a
1316 * chained list
1317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001318static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1319 const unsigned char *buf,
1320 size_t buflen,
1321 int make_copy,
1322 mbedtls_x509_crt_ext_cb_t cb,
1323 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001324{
Janos Follath865b3eb2019-12-16 11:46:15 +00001325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327
1328 /*
1329 * Check for valid input
1330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (crt == NULL || buf == NULL) {
1332 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1333 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336 prev = crt;
1337 crt = crt->next;
1338 }
1339
1340 /*
1341 * Add new certificate on the end of the chain if needed.
1342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (crt->version != 0 && crt->next == NULL) {
1344 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (crt->next == NULL) {
1347 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1348 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349
1350 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353 }
1354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1356 if (ret != 0) {
1357 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if (crt != chain) {
1362 mbedtls_free(crt);
1363 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366 }
1367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369}
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1372 const unsigned char *buf,
1373 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001374{
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001376}
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1379 const unsigned char *buf,
1380 size_t buflen,
1381 int make_copy,
1382 mbedtls_x509_crt_ext_cb_t cb,
1383 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001384{
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001386}
1387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1389 const unsigned char *buf,
1390 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001391{
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001393}
1394
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001396 * Parse one or more PEM certificates from a buffer and add them to the chained
1397 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1400 const unsigned char *buf,
1401 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402{
Janos Follath98e28a72016-05-31 14:03:54 +01001403#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001404 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001406#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001407
1408 /*
1409 * Check for valid input
1410 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (chain == NULL || buf == NULL) {
1412 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1413 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414
1415 /*
1416 * Determine buffer content. Buffer contains either one DER certificate or
1417 * one or more PEM certificates.
1418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1421 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001423 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1426 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1427 }
Janos Follath98e28a72016-05-31 14:03:54 +01001428#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001430#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001437 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001442 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 ret = mbedtls_pem_read_buffer(&pem,
1444 "-----BEGIN CERTIFICATE-----",
1445 "-----END CERTIFICATE-----",
1446 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449 /*
1450 * Was PEM encoded
1451 */
1452 buflen -= use_len;
1453 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1455 return ret;
1456 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1457 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458
1459 /*
1460 * PEM header and footer were found
1461 */
1462 buflen -= use_len;
1463 buf += use_len;
1464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468
Paul Bakker5a5fa922014-09-26 14:53:04 +02001469 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480 /*
1481 * Quit parsing on a memory error
1482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1484 return ret;
1485 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490
1491 total_failed++;
1492 continue;
1493 }
1494
1495 success = 1;
1496 }
1497 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (success) {
1500 return total_failed;
1501 } else if (first_error) {
1502 return first_error;
1503 } else {
1504 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1505 }
Janos Follath98e28a72016-05-31 14:03:54 +01001506#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507}
1508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510/*
1511 * Load one or more certificates and add them to the chained list
1512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514{
Janos Follath865b3eb2019-12-16 11:46:15 +00001515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 size_t n;
1517 unsigned char *buf;
1518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1520 return ret;
1521 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 mbedtls_platform_zeroize(buf, n);
1526 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529}
1530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532{
1533 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001534#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535 int w_ret;
1536 WCHAR szDir[MAX_PATH];
1537 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001538 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540
Paul Bakker9af723c2014-05-01 13:03:14 +02001541 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542 HANDLE hFind;
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (len > MAX_PATH - 3) {
1545 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1546 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 memset(szDir, 0, sizeof(szDir));
1549 memset(filename, 0, MAX_PATH);
1550 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001551 filename[len++] = '\\';
1552 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553 filename[len++] = '*';
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1556 MAX_PATH - 3);
1557 if (w_ret == 0) {
1558 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1559 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 hFind = FindFirstFileW(szDir, &file_data);
1562 if (hFind == INVALID_HANDLE_VALUE) {
1563 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1564 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
1566 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 do {
1568 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001575 -1,
1576 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 NULL, NULL);
1578 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001579 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1580 goto cleanup;
1581 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1584 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 }
1589 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
Ron Eldor36d90422017-01-09 15:09:16 +02001595cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001597#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001598 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001599 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001601 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001602 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 if (dir == NULL) {
1606 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1607 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001608
Ron Eldor63140682017-01-09 19:27:59 +02001609#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1611 closedir(dir);
1612 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001613 }
Ron Eldor63140682017-01-09 19:27:59 +02001614#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001619 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001622 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001623 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1624 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 } else if (stat(entry_name, &sb) == -1) {
1626 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001627 /* Broken symbolic link - ignore this entry.
1628 stat(2) will return this error for either (a) a dangling
1629 symlink or (b) a missing file.
1630 Given that we have just obtained the filename from readdir,
1631 assume that it does exist and therefore treat this as a
1632 dangling symlink. */
1633 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001635 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001636 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1637 goto cleanup;
1638 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639 }
1640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644
1645 // Ignore parse errors
1646 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1648 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001654
1655cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001657
Ron Eldor63140682017-01-09 19:27:59 +02001658#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 }
Ron Eldor63140682017-01-09 19:27:59 +02001662#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001663
Paul Bakkerbe089b02013-10-14 15:51:50 +02001664#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669
Peter Kolbus9a969b62018-12-11 13:55:56 -06001670#if !defined(MBEDTLS_X509_REMOVE_INFO)
toth92g8d435a02021-05-10 15:16:33 +02001671#define PRINT_ITEM(i) \
1672 { \
1673 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1674 MBEDTLS_X509_SAFE_SNPRINTF; \
1675 sep = ", "; \
1676 }
1677
1678#define CERT_TYPE(type, name) \
1679 if (ns_cert_type & (type)) \
1680 PRINT_ITEM(name);
1681
toth92g8d435a02021-05-10 15:16:33 +02001682#define KEY_USAGE(code, name) \
1683 if (key_usage & (code)) \
1684 PRINT_ITEM(name);
1685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686static int x509_info_ext_key_usage(char **buf, size_t *size,
1687 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001688{
Janos Follath865b3eb2019-12-16 11:46:15 +00001689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001690 const char *desc;
1691 size_t n = *size;
1692 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001694 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 while (cur != NULL) {
1697 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001698 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001702 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001703
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001704 sep = ", ";
1705
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001706 cur = cur->next;
1707 }
1708
1709 *size = n;
1710 *buf = p;
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001713}
1714
Gilles Peskine449bd832023-01-11 14:50:10 +01001715static int x509_info_cert_policies(char **buf, size_t *size,
1716 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001717{
Janos Follath865b3eb2019-12-16 11:46:15 +00001718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001719 const char *desc;
1720 size_t n = *size;
1721 char *p = *buf;
1722 const mbedtls_x509_sequence *cur = certificate_policies;
1723 const char *sep = "";
1724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 while (cur != NULL) {
1726 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001727 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001731 MBEDTLS_X509_SAFE_SNPRINTF;
1732
1733 sep = ", ";
1734
1735 cur = cur->next;
1736 }
1737
1738 *size = n;
1739 *buf = p;
1740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001742}
1743
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001744/*
1745 * Return an informational string about the certificate.
1746 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001747#define BEFORE_COLON 18
1748#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001749int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1750 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751{
Janos Follath865b3eb2019-12-16 11:46:15 +00001752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753 size_t n;
1754 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755 char key_size_str[BEFORE_COLON];
1756
1757 p = buf;
1758 n = size;
1759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if (NULL == crt) {
1761 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001762 MBEDTLS_X509_SAFE_SNPRINTF;
1763
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001765 }
1766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1768 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001769 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1771 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001772 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001775 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001778 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
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_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001783 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001785 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1788 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1789 crt->valid_from.year, crt->valid_from.mon,
1790 crt->valid_from.day, crt->valid_from.hour,
1791 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001792 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1795 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1796 crt->valid_to.year, crt->valid_to.mon,
1797 crt->valid_to.day, crt->valid_to.hour,
1798 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001799 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001802 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1805 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001806 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001807
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001808 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1810 mbedtls_pk_get_name(&crt->pk))) != 0) {
1811 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001812 }
1813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1815 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001816 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001818 /*
1819 * Optional extensions
1820 */
1821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1823 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1824 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001825 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (crt->max_pathlen > 0) {
1828 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001829 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001830 }
1831 }
1832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1834 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001835 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001836
Przemek Stekiel21c37282023-01-16 08:47:49 +01001837 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1838 &crt->subject_alt_names,
1839 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 return ret;
1841 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001842 }
1843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1845 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001846 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001847
Przemek Stekiel21c37282023-01-16 08:47:49 +01001848 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 return ret;
1850 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001851 }
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1854 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001855 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001856
Przemek Stekiel21c37282023-01-16 08:47:49 +01001857 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 return ret;
1859 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001860 }
1861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1863 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001864 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 if ((ret = x509_info_ext_key_usage(&p, &n,
1867 &crt->ext_key_usage)) != 0) {
1868 return ret;
1869 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001870 }
1871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1873 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001874 MBEDTLS_X509_SAFE_SNPRINTF;
1875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if ((ret = x509_info_cert_policies(&p, &n,
1877 &crt->certificate_policies)) != 0) {
1878 return ret;
1879 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001880 }
1881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001883 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001886}
1887
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001888struct x509_crt_verify_string {
1889 int code;
1890 const char *string;
1891};
1892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001894static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001895 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001896 { 0, NULL }
1897};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001898#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1901 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001902{
Janos Follath865b3eb2019-12-16 11:46:15 +00001903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001904 const struct x509_crt_verify_string *cur;
1905 char *p = buf;
1906 size_t n = size;
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1909 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001910 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001914 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001915 flags ^= cur->code;
1916 }
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (flags != 0) {
1919 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1920 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001921 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001922 }
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001925}
Hanno Becker612a2f12020-10-09 09:19:39 +01001926#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1929 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001930{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001931 unsigned int usage_must, usage_may;
1932 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1936 return 0;
1937 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001938
1939 usage_must = usage & ~may_mask;
1940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1942 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1943 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001944
1945 usage_may = usage & may_mask;
1946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1948 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1949 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001952}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1955 const char *usage_oid,
1956 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001959
1960 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1962 return 0;
1963 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001964
1965 /*
1966 * Look for the requested usage (or wildcard ANY) in our list
1967 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (cur_oid->len == usage_len &&
1972 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1973 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001974 }
1975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1977 return 0;
1978 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001979 }
1980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001982}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001985/*
1986 * Return 1 if the certificate is revoked, or 0 otherwise.
1987 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001988int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 while (cur != NULL && cur->serial.len != 0) {
1993 if (crt->serial.len == cur->serial.len &&
1994 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1995 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001996 }
1997
1998 cur = cur->next;
1999 }
2000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002002}
2003
2004/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002005 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002006 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002008static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2009 mbedtls_x509_crl *crl_list,
2010 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002011{
2012 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002013 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002014#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002015 psa_algorithm_t psa_algorithm;
2016#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002018#endif /* MBEDTLS_USE_PSA_CRYPTO */
2019 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (ca == NULL) {
2022 return flags;
2023 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 while (crl_list != NULL) {
2026 if (crl_list->version == 0 ||
2027 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028 crl_list = crl_list->next;
2029 continue;
2030 }
2031
2032 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002033 * Check if the CA is configured to sign CRLs
2034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (mbedtls_x509_crt_check_key_usage(ca,
2036 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002038 break;
2039 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002040
2041 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002042 * Check if CRL is correctly signed by the trusted CA
2043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002045 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002049 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002051
pespacek7599a772022-02-07 14:40:55 +01002052#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
2054 if (psa_hash_compute(psa_algorithm,
2055 crl_list->tbs.p,
2056 crl_list->tbs.len,
2057 hash,
2058 sizeof(hash),
2059 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002060 /* Note: this can't happen except after an internal error */
2061 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2062 break;
2063 }
pespacek7599a772022-02-07 14:40:55 +01002064#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2066 hash_length = mbedtls_md_get_size(md_info);
2067 if (mbedtls_md(md_info,
2068 crl_list->tbs.p,
2069 crl_list->tbs.len,
2070 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002071 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002073 break;
2074 }
pespaceka7a64692022-02-14 15:18:43 +01002075#endif /* MBEDTLS_USE_PSA_CRYPTO */
2076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002078 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2082 crl_list->sig_md, hash, hash_length,
2083 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085 break;
2086 }
2087
2088 /*
2089 * Check for validity of CRL (Do not drop out)
2090 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002096 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002098
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002099 /*
2100 * Check if certificate is revoked
2101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104 break;
2105 }
2106
2107 crl_list = crl_list->next;
2108 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002113
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002114/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002115 * Check the signature of a certificate by its parent
2116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2118 mbedtls_x509_crt *parent,
2119 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002120{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002121 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002122 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002123#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2124 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 md_info = mbedtls_md_info_from_type(child->sig_md);
2126 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002127
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002128 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2130 return -1;
2131 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002132#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002134 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 status = psa_hash_compute(hash_alg,
2137 child->tbs.p,
2138 child->tbs.len,
2139 hash,
2140 sizeof(hash),
2141 &hash_len);
2142 if (status != PSA_SUCCESS) {
2143 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002144 }
2145
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002146#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002147 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2149 return -1;
2150 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002151
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002152#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2154 return mbedtls_pk_verify_restartable(&parent->pk,
2155 child->sig_md, hash, hash_len,
2156 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002157 }
2158#else
2159 (void) rs_ctx;
2160#endif
2161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2163 child->sig_md, hash, hash_len,
2164 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002165}
2166
2167/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002168 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2169 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002170 *
2171 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002172 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002173static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2174 const mbedtls_x509_crt *parent,
2175 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002176{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002177 int need_ca_bit;
2178
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002179 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2181 return -1;
2182 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002183
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002184 /* Parent must have the basicConstraints CA bit set as a general rule */
2185 need_ca_bit = 1;
2186
2187 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002189 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002190 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (need_ca_bit && !parent->ca_istrue) {
2193 return -1;
2194 }
2195
2196 if (need_ca_bit &&
2197 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2198 return -1;
2199 }
2200
2201 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002202}
2203
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002204/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002205 * Find a suitable parent for child in candidates, or return NULL.
2206 *
2207 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002208 * 1. subject name matches child's issuer
2209 * 2. if necessary, the CA bit is set and key usage allows signing certs
2210 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002211 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002212 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002213 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002214 * If there's a suitable candidate which is also time-valid, return the first
2215 * such. Otherwise, return the first suitable candidate (or NULL if there is
2216 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002217 *
2218 * The rationale for this rule is that someone could have a list of trusted
2219 * roots with two versions on the same root with different validity periods.
2220 * (At least one user reported having such a list and wanted it to just work.)
2221 * The reason we don't just require time-validity is that generally there is
2222 * only one version, and if it's expired we want the flags to state that
2223 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002224 *
2225 * The rationale for rule 3 (signature for trusted roots) is that users might
2226 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002227 * way we select the correct one is by checking the signature (as we don't
2228 * rely on key identifier extensions). (This is one way users might choose to
2229 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002230 *
2231 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002232 * - [in] child: certificate for which we're looking for a parent
2233 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002234 * - [out] r_parent: parent found (or NULL)
2235 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002236 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2237 * of the chain, 0 otherwise
2238 * - [in] path_cnt: number of intermediates seen so far
2239 * - [in] self_cnt: number of self-signed intermediates seen so far
2240 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002241 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002242 *
2243 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002244 * - 0 on success
2245 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002246 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002247static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 mbedtls_x509_crt *child,
2249 mbedtls_x509_crt *candidates,
2250 mbedtls_x509_crt **r_parent,
2251 int *r_signature_is_good,
2252 int top,
2253 unsigned path_cnt,
2254 unsigned self_cnt,
2255 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002256{
Janos Follath865b3eb2019-12-16 11:46:15 +00002257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002258 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002259 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002260
2261#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002262 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002264 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002265 parent = rs_ctx->parent;
2266 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002267 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002268
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002269 /* clear saved state */
2270 rs_ctx->parent = NULL;
2271 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002272 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002273
2274 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002275 goto check_signature;
2276 }
2277#endif
2278
2279 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002280 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002283 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002285 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002287
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002288 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if (parent->max_pathlen > 0 &&
2290 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002291 continue;
2292 }
2293
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002294 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002295#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2296check_signature:
2297#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002299
2300#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002302 /* save state */
2303 rs_ctx->parent = parent;
2304 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002305 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002308 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002309#else
2310 (void) ret;
2311#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002312
2313 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002315 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002317
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002318 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2320 mbedtls_x509_time_is_future(&parent->valid_from)) {
2321 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002322 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002323 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002324 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002325
2326 continue;
2327 }
2328
Andy Gross1f627142019-01-30 10:25:53 -06002329 *r_parent = parent;
2330 *r_signature_is_good = signature_is_good;
2331
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002332 break;
2333 }
2334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002336 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002337 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002338 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002341}
2342
2343/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002344 * Find a parent in trusted CAs or the provided chain, or return NULL.
2345 *
2346 * Searches in trusted CAs first, and return the first suitable parent found
2347 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002348 *
2349 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002350 * - [in] child: certificate for which we're looking for a parent, followed
2351 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002352 * - [in] trust_ca: list of locally trusted certificates
2353 * - [out] parent: parent found (or NULL)
2354 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2355 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2356 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2357 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002358 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002359 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002360 *
2361 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002362 * - 0 on success
2363 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002364 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002365static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 mbedtls_x509_crt *child,
2367 mbedtls_x509_crt *trust_ca,
2368 mbedtls_x509_crt **parent,
2369 int *parent_is_trusted,
2370 int *signature_is_good,
2371 unsigned path_cnt,
2372 unsigned self_cnt,
2373 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002374{
Janos Follath865b3eb2019-12-16 11:46:15 +00002375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002376 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002377
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002378 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002379
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002380#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002381 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002383 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002384 rs_ctx->parent_is_trusted = -1;
2385 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002386#endif
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002389 search_list = *parent_is_trusted ? trust_ca : child->next;
2390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 ret = x509_crt_find_parent_in(child, search_list,
2392 parent, signature_is_good,
2393 *parent_is_trusted,
2394 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002395
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002396#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002398 /* save state */
2399 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002401 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002402#else
2403 (void) ret;
2404#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002405
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002406 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002408 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002410
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002411 /* prepare second iteration */
2412 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002413 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002414
2415 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002417 *parent_is_trusted = 0;
2418 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002419 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002422}
2423
2424/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002425 * Check if an end-entity certificate is locally trusted
2426 *
2427 * Currently we require such certificates to be self-signed (actually only
2428 * check for self-issued as self-signatures are not checked)
2429 */
2430static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 mbedtls_x509_crt *crt,
2432 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002433{
2434 mbedtls_x509_crt *cur;
2435
2436 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2438 return -1;
2439 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002440
2441 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2443 if (crt->raw.len == cur->raw.len &&
2444 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2445 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002446 }
2447 }
2448
2449 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002451}
2452
2453/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002454 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002455 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002456 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2457 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002458 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002459 * such that every cert in the chain is a child of the next one,
2460 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002461 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002462 * Verify that chain and return it with flags for all issues found.
2463 *
2464 * Special cases:
2465 * - EE == Rj -> return a one-element list containing it
2466 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2467 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002468 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002469 * Tests for (aspects of) this function should include at least:
2470 * - trusted EE
2471 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002472 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002473 * - if relevant: EE untrusted
2474 * - if relevant: EE -> intermediate, untrusted
2475 * with the aspect under test checked at each relevant level (EE, int, root).
2476 * For some aspects longer chains are required, but usually length 2 is
2477 * enough (but length 1 is not in general).
2478 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002479 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002480 * - [in] crt: the cert list EE, C1, ..., Cn
2481 * - [in] trust_ca: the trusted list R1, ..., Rp
2482 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002483 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002484 * Only valid when return value is 0, may contain garbage otherwise!
2485 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002486 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002487 *
2488 * Return value:
2489 * - non-zero if the chain could not be fully built and examined
2490 * - 0 is the chain was successfully built and examined,
2491 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002492 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002493static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 mbedtls_x509_crt *crt,
2495 mbedtls_x509_crt *trust_ca,
2496 mbedtls_x509_crl *ca_crl,
2497 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2498 void *p_ca_cb,
2499 const mbedtls_x509_crt_profile *profile,
2500 mbedtls_x509_crt_verify_chain *ver_chain,
2501 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002502{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002503 /* Don't initialize any of those variables here, so that the compiler can
2504 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002506 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002507 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002508 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002509 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002510 int parent_is_trusted;
2511 int child_is_trusted;
2512 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002513 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002514 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002515
2516#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2517 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002519 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002520 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002521 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002522
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002523 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002524 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002525 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002526 flags = &cur->flags;
2527
2528 goto find_parent;
2529 }
2530#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002531
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002532 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002533 self_cnt = 0;
2534 parent_is_trusted = 0;
2535 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002538 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002539 cur = &ver_chain->items[ver_chain->len];
2540 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002541 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002542 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002543 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002544
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002545 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002547 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002551 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002553
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002554 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (child_is_trusted) {
2556 return 0;
2557 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002558
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002559 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002561 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002565 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002567
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002568 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (ver_chain->len == 1 &&
2570 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2571 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002572 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002573
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002574#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2575find_parent:
2576#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002577
2578 /* Obtain list of potential trusted signers from CA callback,
2579 * or use statically provided list. */
2580#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (f_ca_cb != NULL) {
2582 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2583 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002584 ver_chain->trust_ca_cb_result = NULL;
2585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2587 if (ret != 0) {
2588 return MBEDTLS_ERR_X509_FATAL_ERROR;
2589 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002590
2591 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002593#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2594 {
2595 ((void) f_ca_cb);
2596 ((void) p_ca_cb);
2597 cur_trust_ca = trust_ca;
2598 }
2599
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002600 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2602 &parent_is_trusted, &signature_is_good,
2603 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002604
2605#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002607 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002608 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002609 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002610 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002613 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002614#else
2615 (void) ret;
2616#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002617
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002618 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002620 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002622 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002623
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002624 /* Count intermediate self-issued (not necessarily self-signed) certs.
2625 * These can occur with some strategies for key rollover, see [SIRO],
2626 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (ver_chain->len != 1 &&
2628 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002629 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002630 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002631
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002632 /* path_cnt is 0 for the first intermediate CA,
2633 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (!parent_is_trusted &&
2635 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002636 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002638 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002639
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002640 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002642 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002644
2645 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002647 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002651 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002653#else
2654 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002655#endif
2656
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002657 /* prepare for next iteration */
2658 child = parent;
2659 parent = NULL;
2660 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002661 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002662 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002663}
2664
2665/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002666 * Check for CN match
2667 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002668static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2669 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002670{
2671 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (name->len == cn_len &&
2673 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2674 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002675 }
2676
2677 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (x509_check_wildcard(cn, name) == 0) {
2679 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002680 }
2681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002683}
2684
2685/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002686 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2687 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002688static int x509_crt_check_san(const mbedtls_x509_buf *name,
2689 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002690{
2691 const unsigned char san_type = (unsigned char) name->tag &
2692 MBEDTLS_ASN1_TAG_VALUE_MASK;
2693
2694 /* dNSName */
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
2696 return x509_crt_check_cn(name, cn, cn_len);
2697 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002698
2699 /* (We may handle other types here later.) */
2700
2701 /* Unrecognized type */
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002703}
2704
2705/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002706 * Verify the requested CN - only call this if cn is not NULL!
2707 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002708static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2709 const char *cn,
2710 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002711{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002712 const mbedtls_x509_name *name;
2713 const mbedtls_x509_sequence *cur;
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2717 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
2718 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002719 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002720 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002721 }
2722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002724 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 }
2726 } else {
2727 for (name = &crt->subject; name != NULL; name = name->next) {
2728 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2729 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
2730 break;
2731 }
2732 }
2733
2734 if (name == NULL) {
2735 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2736 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002737 }
2738}
2739
2740/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002741 * Merge the flags for all certs in the chain, after calling callback
2742 */
2743static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 uint32_t *flags,
2745 const mbedtls_x509_crt_verify_chain *ver_chain,
2746 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2747 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002748{
Janos Follath865b3eb2019-12-16 11:46:15 +00002749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002750 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002751 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002752 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002755 cur = &ver_chain->items[i-1];
2756 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 if (NULL != f_vrfy) {
2759 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
2760 return ret;
2761 }
2762 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002763
2764 *flags |= cur_flags;
2765 }
2766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002768}
2769
2770/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002771 * Verify the certificate validity, with profile, restartable version
2772 *
2773 * This function:
2774 * - checks the requested CN (if any)
2775 * - checks the type and size of the EE cert's key,
2776 * as that isn't done as part of chain building/verification currently
2777 * - builds and verifies the chain
2778 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00002779 *
2780 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
2781 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
2782 * verification routine to search for trusted signers, and CRLs will
2783 * be disabled. Otherwise, `trust_ca` will be used as the static list
2784 * of trusted signers, and `ca_crl` will be use as the static list
2785 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002786 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002787static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
2788 mbedtls_x509_crt *trust_ca,
2789 mbedtls_x509_crl *ca_crl,
2790 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2791 void *p_ca_cb,
2792 const mbedtls_x509_crt_profile *profile,
2793 const char *cn, uint32_t *flags,
2794 int (*f_vrfy)(void *,
2795 mbedtls_x509_crt *,
2796 int,
2797 uint32_t *),
2798 void *p_vrfy,
2799 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002800{
Janos Follath865b3eb2019-12-16 11:46:15 +00002801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002802 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002803 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002804 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002805
2806 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002807 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002811 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2812 goto exit;
2813 }
2814
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002815 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 if (cn != NULL) {
2817 x509_crt_verify_name(crt, cn, &ee_flags);
2818 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002819
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002820 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002822
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002824 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002828 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002830
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002831 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
2833 f_ca_cb, p_ca_cb, profile,
2834 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002837 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002839
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002840 /* Merge end-entity flags */
2841 ver_chain.items[0].flags |= ee_flags;
2842
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002843 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002845
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002846exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00002847
2848#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
2850 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002851 ver_chain.trust_ca_cb_result = NULL;
2852#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2853
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002854#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
2856 mbedtls_x509_crt_restart_free(rs_ctx);
2857 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002858#endif
2859
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002860 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2861 * the SSL module for authmode optional, but non-zero return from the
2862 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002864 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002865 }
2866
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (ret != 0) {
2868 *flags = (uint32_t) -1;
2869 return ret;
2870 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (*flags != 0) {
2873 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
2874 }
2875
2876 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002877}
2878
Hanno Becker3116fb32019-03-28 13:34:42 +00002879
2880/*
2881 * Verify the certificate validity (default profile, not restartable)
2882 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
2884 mbedtls_x509_crt *trust_ca,
2885 mbedtls_x509_crl *ca_crl,
2886 const char *cn, uint32_t *flags,
2887 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2888 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002889{
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2891 NULL, NULL,
2892 &mbedtls_x509_crt_profile_default,
2893 cn, flags,
2894 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002895}
2896
2897/*
2898 * Verify the certificate validity (user-chosen profile, not restartable)
2899 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002900int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
2901 mbedtls_x509_crt *trust_ca,
2902 mbedtls_x509_crl *ca_crl,
2903 const mbedtls_x509_crt_profile *profile,
2904 const char *cn, uint32_t *flags,
2905 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2906 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002907{
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2909 NULL, NULL,
2910 profile, cn, flags,
2911 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002912}
2913
2914#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2915/*
2916 * Verify the certificate validity (user-chosen profile, CA callback,
2917 * not restartable).
2918 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
2920 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2921 void *p_ca_cb,
2922 const mbedtls_x509_crt_profile *profile,
2923 const char *cn, uint32_t *flags,
2924 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2925 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002926{
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
2928 f_ca_cb, p_ca_cb,
2929 profile, cn, flags,
2930 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002931}
2932#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
2935 mbedtls_x509_crt *trust_ca,
2936 mbedtls_x509_crl *ca_crl,
2937 const mbedtls_x509_crt_profile *profile,
2938 const char *cn, uint32_t *flags,
2939 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2940 void *p_vrfy,
2941 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00002942{
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2944 NULL, NULL,
2945 profile, cn, flags,
2946 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00002947}
2948
2949
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002950/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002951 * Initialize a certificate chain
2952 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002953void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02002954{
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02002956}
2957
2958/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002959 * Unallocate all certificate data
2960 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002961void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002962{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 mbedtls_x509_crt *cert_cur = crt;
2964 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 while (cert_cur != NULL) {
2967 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002971#endif
2972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
2974 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
2975 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
2976 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
2977 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
2980 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
2981 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002982 }
2983
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002984 cert_prv = cert_cur;
2985 cert_cur = cert_cur->next;
2986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
2988 if (cert_prv != crt) {
2989 mbedtls_free(cert_prv);
2990 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002991 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002992}
2993
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002994#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2995/*
2996 * Initialize a restart context
2997 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002998void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002999{
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003001
3002 ctx->parent = NULL;
3003 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003004 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003005
3006 ctx->parent_is_trusted = -1;
3007
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003008 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003009 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003011}
3012
3013/*
3014 * Free the components of a restart context
3015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003016void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003017{
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003019 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 mbedtls_pk_restart_free(&ctx->pk);
3023 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003024}
3025#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003027#endif /* MBEDTLS_X509_CRT_PARSE_C */