blob: 1ebe96a06dcd6b030f7c8b1e002e80de73f99d0b [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) {
603 return ret;
604 } 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
619/*
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100620 * SubjectAltName ::= GeneralNames
toth92ga41954d2021-02-12 16:11:17 +0100621 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
622 *
623 * GeneralName ::= CHOICE {
624 * otherName [0] OtherName,
625 * rfc822Name [1] IA5String,
626 * dNSName [2] IA5String,
627 * x400Address [3] ORAddress,
628 * directoryName [4] Name,
629 * ediPartyName [5] EDIPartyName,
630 * uniformResourceIdentifier [6] IA5String,
631 * iPAddress [7] OCTET STRING,
632 * registeredID [8] OBJECT IDENTIFIER }
633 *
634 * OtherName ::= SEQUENCE {
635 * type-id OBJECT IDENTIFIER,
636 * value [0] EXPLICIT ANY DEFINED BY type-id }
637 *
638 * EDIPartyName ::= SEQUENCE {
639 * nameAssigner [0] DirectoryString OPTIONAL,
640 * partyName [1] DirectoryString }
641 *
642 * NOTE: we list all types, but only use dNSName and otherName
643 * of type HwModuleName, as defined in RFC 4108, at this point.
644 */
toth92g8d435a02021-05-10 15:16:33 +0200645static int x509_get_general_names(unsigned char **p,
646 const unsigned char *end,
647 mbedtls_x509_sequence *subject_alt_name)
toth92ga41954d2021-02-12 16:11:17 +0100648{
649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
650 size_t len, tag_len;
651 mbedtls_asn1_buf *buf;
652 unsigned char tag;
653 mbedtls_asn1_sequence *cur = subject_alt_name;
654
655 /* Get main sequence tag */
656 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
657 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
658 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
659 }
660
661 if (*p + len != end) {
662 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
663 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
664 }
665
666 while (*p < end) {
667 mbedtls_x509_subject_alternative_name dummy_san_buf;
668 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
669
670 tag = **p;
671 (*p)++;
672 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
673 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
674 }
675
toth92g8d435a02021-05-10 15:16:33 +0200676 /* Tag shall be CONTEXT_SPECIFIC or SET */
toth92ga41954d2021-02-12 16:11:17 +0100677 if ((tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
678 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
toth92g8d435a02021-05-10 15:16:33 +0200679 if ((tag & (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
680 (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100681 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
682 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
toth92g8d435a02021-05-10 15:16:33 +0200683 }
toth92ga41954d2021-02-12 16:11:17 +0100684 }
685
686 /*
687 * Check that the SAN is structured correctly.
688 */
Przemek Stekiel9a511c52023-01-03 10:23:13 +0100689 ret = mbedtls_x509_parse_subject_alt_name(&(cur->buf), &dummy_san_buf);
toth92ga41954d2021-02-12 16:11:17 +0100690 /*
691 * In case the extension is malformed, return an error,
692 * and clear the allocated sequences.
693 */
694 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
695 mbedtls_asn1_sequence_free(subject_alt_name->next);
696 subject_alt_name->next = NULL;
697 return ret;
698 }
699
700 /* Allocate and assign next pointer */
701 if (cur->buf.p != NULL) {
702 if (cur->next != NULL) {
703 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
704 }
705
706 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
707
708 if (cur->next == NULL) {
709 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
710 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
711 }
712
713 cur = cur->next;
714 }
715
716 buf = &(cur->buf);
717 buf->tag = tag;
718 buf->p = *p;
719 buf->len = tag_len;
720 *p += buf->len;
721 }
722
723 /* Set final sequence entry's next pointer to NULL */
724 cur->next = NULL;
725
726 if (*p != end) {
727 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
728 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
729 }
730
731 return 0;
732}
733
734/*
toth92g8d435a02021-05-10 15:16:33 +0200735 * AuthorityKeyIdentifier ::= SEQUENCE {
736 * keyIdentifier [0] KeyIdentifier OPTIONAL,
737 * authorityCertIssuer [1] GeneralNames OPTIONAL,
738 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
739 *
740 * KeyIdentifier ::= OCTET STRING
741 */
742static int x509_get_authority_key_id(unsigned char **p,
743 unsigned char *end,
744 mbedtls_x509_authority *authority_key_id)
745{
746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
747 size_t len = 0u;
748
749 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100750 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
toth92g8d435a02021-05-10 15:16:33 +0200751 return ret;
752 }
753
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100754 if (*p + len != end) {
755 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
756 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
757 }
758
toth92g8d435a02021-05-10 15:16:33 +0200759 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
760 MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0) {
761 /* KeyIdentifier is an OPTIONAL field */
762 } else {
763 authority_key_id->keyIdentifier.len = len;
764 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200765 /* Setting tag of the keyIdentfier intentionally to 0x04.
766 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
767 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200768 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
769
770 *p += len;
771 }
772
773 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200774 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200775 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
776 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100777 MBEDTLS_X509_SAN_RFC822_NAME)) != 0) {
toth92g8d435a02021-05-10 15:16:33 +0200778 /* authorityCertIssuer is an OPTIONAL field */
779 } else {
toth92g9232e0a2021-05-11 12:55:58 +0200780 /* Getting directoryName using the required specific class tag [4] */
toth92g8d435a02021-05-10 15:16:33 +0200781 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
782 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100783 MBEDTLS_ASN1_CONSTRUCTED |
784 MBEDTLS_X509_SAN_DIRECTORY_NAME)) != 0) {
toth92g8d435a02021-05-10 15:16:33 +0200785 return ret;
786 } else {
787 /* "end" also includes the CertSerialNumber field so "len" shall be used */
788 ret = x509_get_general_names(p,
789 (*p+len),
790 &authority_key_id->authorityCertIssuer);
791 }
792 }
793 }
794
795 if (*p < end) {
796 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
797 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_INTEGER)) !=
798 0) {
799 /* authorityCertSerialNumber is an OPTIONAL field, but if there are still data it must be the serial number */
800 return ret;
801 } else {
802 authority_key_id->authorityCertSerialNumber.len = len;
803 authority_key_id->authorityCertSerialNumber.p = *p;
804 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_OCTET_STRING;
805 *p += len;
806 }
807 }
808
809 if (*p != end) {
810 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
811 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
812 }
813
814 return 0;
815}
816
817/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200818 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
819 *
820 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
821 *
822 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
823 *
824 * PolicyInformation ::= SEQUENCE {
825 * policyIdentifier CertPolicyId,
826 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
827 * PolicyQualifierInfo OPTIONAL }
828 *
829 * CertPolicyId ::= OBJECT IDENTIFIER
830 *
831 * PolicyQualifierInfo ::= SEQUENCE {
832 * policyQualifierId PolicyQualifierId,
833 * qualifier ANY DEFINED BY policyQualifierId }
834 *
835 * -- policyQualifierIds for Internet policy qualifiers
836 *
837 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
838 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
839 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
840 *
841 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
842 *
843 * Qualifier ::= CHOICE {
844 * cPSuri CPSuri,
845 * userNotice UserNotice }
846 *
847 * CPSuri ::= IA5String
848 *
849 * UserNotice ::= SEQUENCE {
850 * noticeRef NoticeReference OPTIONAL,
851 * explicitText DisplayText OPTIONAL }
852 *
853 * NoticeReference ::= SEQUENCE {
854 * organization DisplayText,
855 * noticeNumbers SEQUENCE OF INTEGER }
856 *
857 * DisplayText ::= CHOICE {
858 * ia5String IA5String (SIZE (1..200)),
859 * visibleString VisibleString (SIZE (1..200)),
860 * bmpString BMPString (SIZE (1..200)),
861 * utf8String UTF8String (SIZE (1..200)) }
862 *
863 * NOTE: we only parse and use anyPolicy without qualifiers at this point
864 * as defined in RFC 5280.
865 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866static int x509_get_certificate_policies(unsigned char **p,
867 const unsigned char *end,
868 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200869{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300870 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200871 size_t len;
872 mbedtls_asn1_buf *buf;
873 mbedtls_asn1_sequence *cur = certificate_policies;
874
875 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 ret = mbedtls_asn1_get_tag(p, end, &len,
877 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
878 if (ret != 0) {
879 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
880 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 if (*p + len != end) {
883 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
884 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
885 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200886
887 /*
888 * Cannot be an empty sequence.
889 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (len == 0) {
891 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
892 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
893 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200896 mbedtls_x509_buf policy_oid;
897 const unsigned char *policy_end;
898
899 /*
900 * Get the policy sequence
901 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
903 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
904 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
905 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200906
907 policy_end = *p + len;
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
910 MBEDTLS_ASN1_OID)) != 0) {
911 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
912 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200913
914 policy_oid.tag = MBEDTLS_ASN1_OID;
915 policy_oid.len = len;
916 policy_oid.p = *p;
917
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300918 /*
919 * Only AnyPolicy is currently supported when enforcing policy.
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300922 /*
923 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200924 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300925 */
926 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
927 }
928
Ron Eldor74d9acc2019-03-21 14:00:03 +0200929 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (cur->buf.p != NULL) {
931 if (cur->next != NULL) {
932 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
933 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (cur->next == NULL) {
938 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
939 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
940 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200941
942 cur = cur->next;
943 }
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200946 buf->tag = policy_oid.tag;
947 buf->p = policy_oid.p;
948 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300949
950 *p += len;
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 /*
953 * If there is an optional qualifier, then *p < policy_end
954 * Check the Qualifier len to verify it doesn't exceed policy_end.
955 */
956 if (*p < policy_end) {
957 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
958 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
959 0) {
960 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
961 }
Ron Eldor08063792019-05-13 16:38:39 +0300962 /*
963 * Skip the optional policy qualifiers.
964 */
965 *p += len;
966 }
967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (*p != policy_end) {
969 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
970 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
971 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200972 }
973
974 /* Set final sequence entry's next pointer to NULL */
975 cur->next = NULL;
976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (*p != end) {
978 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
979 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
980 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200983}
984
985/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 * X.509 v3 extensions
987 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100989static int x509_get_crt_ext(unsigned char **p,
990 const unsigned char *end,
991 mbedtls_x509_crt *crt,
992 mbedtls_x509_crt_ext_cb_t cb,
993 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994{
Janos Follath865b3eb2019-12-16 11:46:15 +0000995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200997 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (*p == end) {
1000 return 0;
1001 }
Hanno Becker12f62fb2019-02-12 17:22:36 +00001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
1004 return ret;
1005 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006
Hanno Becker12f62fb2019-02-12 17:22:36 +00001007 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 /*
1010 * Extension ::= SEQUENCE {
1011 * extnID OBJECT IDENTIFIER,
1012 * critical BOOLEAN DEFAULT FALSE,
1013 * extnValue OCTET STRING }
1014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 int is_critical = 0; /* DEFAULT FALSE */
1017 int ext_type = 0;
1018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1020 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1021 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1022 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023
1024 end_ext_data = *p + len;
1025
1026 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
1028 MBEDTLS_ASN1_OID)) != 0) {
1029 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1030 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031
k-stachowiak463928a2018-07-24 12:50:59 +02001032 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033 extn_oid.p = *p;
1034 *p += extn_oid.len;
1035
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
1038 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
1039 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1040 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041
1042 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
1044 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1045 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1046 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001047
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001048 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001049 end_ext_octet = *p + len;
1050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (end_ext_octet != end_ext_data) {
1052 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1053 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1054 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055
1056 /*
1057 * Detect supported extensions
1058 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001062 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 if (cb != NULL) {
1064 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
1065 if (ret != 0 && is_critical) {
1066 return ret;
1067 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001068 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001069 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001070 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001071
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 /* No parser found, skip extension */
1073 *p = end_ext_octet;
1074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1078 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080 continue;
1081 }
1082
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001083 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if ((crt->ext_types & ext_type) != 0) {
1085 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1086 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001087
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088 crt->ext_types |= ext_type;
1089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 switch (ext_type) {
1091 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
1092 /* Parse basic constraints */
1093 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
1094 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
1095 return ret;
1096 }
1097 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 case MBEDTLS_X509_EXT_KEY_USAGE:
1100 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001101 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
1102 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 return ret;
1104 }
1105 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1108 /* Parse extended key usage */
1109 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1110 &crt->ext_key_usage)) != 0) {
1111 return ret;
1112 }
1113 break;
toth92g8d435a02021-05-10 15:16:33 +02001114
toth92ga41954d2021-02-12 16:11:17 +01001115 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1116 /* Parse subject key identifier */
1117 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1118 &crt->subject_key_id)) != 0) {
1119 return ret;
1120 }
1121 break;
toth92g8d435a02021-05-10 15:16:33 +02001122
toth92ga41954d2021-02-12 16:11:17 +01001123 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1124 /* Parse authority key identifier */
1125 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1126 &crt->authority_key_id)) != 0) {
1127 return ret;
1128 }
1129 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001131 /* Parse subject alt name
1132 * SubjectAltName ::= GeneralNames
1133 */
1134 if ((ret = x509_get_general_names(p, end_ext_octet,
1135 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return ret;
1137 }
1138 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1141 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001142 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1143 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 return ret;
1145 }
1146 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1149 /* Parse certificate policies type */
1150 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1151 &crt->certificate_policies)) != 0) {
1152 /* Give the callback (if any) a chance to handle the extension
1153 * if it contains unsupported policies */
1154 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1155 cb(p_ctx, crt, &extn_oid, is_critical,
1156 start_ext_octet, end_ext_octet) == 0) {
1157 break;
1158 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (is_critical) {
1161 return ret;
1162 } else
1163 /*
1164 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1165 * cannot interpret or enforce the policy. However, it is up to
1166 * the user to choose how to enforce the policies,
1167 * unless the extension is critical.
1168 */
1169 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1170 return ret;
1171 }
1172 }
1173 break;
1174
1175 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001176 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 * If this is a non-critical extension, which the oid layer
1178 * supports, but there isn't an x509 parser for it,
1179 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if (is_critical) {
1182 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1183 } else {
1184 *p = end_ext_octet;
1185 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 }
1187 }
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (*p != end) {
1190 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1191 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1192 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195}
1196
1197/*
1198 * Parse and fill a single X.509 certificate in DER format
1199 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1201 const unsigned char *buf,
1202 size_t buflen,
1203 int make_copy,
1204 mbedtls_x509_crt_ext_cb_t cb,
1205 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206{
Janos Follath865b3eb2019-12-16 11:46:15 +00001207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 size_t len;
1209 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1213 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1214 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215
1216 /*
1217 * Check for valid input
1218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if (crt == NULL || buf == NULL) {
1220 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1221 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001223 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001225 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226 end = p + len;
1227
1228 /*
1229 * Certificate ::= SEQUENCE {
1230 * tbsCertificate TBSCertificate,
1231 * signatureAlgorithm AlgorithmIdentifier,
1232 * signatureValue BIT STRING }
1233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1235 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1236 mbedtls_x509_crt_free(crt);
1237 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001238 }
1239
Janos Follathcc0e49d2016-02-17 14:34:12 +00001240 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001241 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001243 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1245 if (crt->raw.p == NULL) {
1246 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1247 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001250 crt->own_buffer = 1;
1251
1252 p += crt->raw.len - len;
1253 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 } else {
1255 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001256 crt->own_buffer = 0;
1257 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001258
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259 /*
1260 * TBSCertificate ::= SEQUENCE {
1261 */
1262 crt->tbs.p = p;
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1265 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1266 mbedtls_x509_crt_free(crt);
1267 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 }
1269
1270 end = p + len;
1271 crt->tbs.len = end - crt->tbs.p;
1272
1273 /*
1274 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1275 *
1276 * CertificateSerialNumber ::= INTEGER
1277 *
1278 * signature AlgorithmIdentifier
1279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1281 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1282 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1283 &sig_params1)) != 0) {
1284 mbedtls_x509_crt_free(crt);
1285 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 }
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (crt->version < 0 || crt->version > 2) {
1289 mbedtls_x509_crt_free(crt);
1290 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 }
1292
Andres AG7ca4a032017-03-09 16:16:11 +00001293 crt->version++;
1294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1296 &crt->sig_md, &crt->sig_pk,
1297 &crt->sig_opts)) != 0) {
1298 mbedtls_x509_crt_free(crt);
1299 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 }
1301
1302 /*
1303 * issuer Name
1304 */
1305 crt->issuer_raw.p = p;
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1308 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1309 mbedtls_x509_crt_free(crt);
1310 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 }
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1314 mbedtls_x509_crt_free(crt);
1315 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 }
1317
1318 crt->issuer_raw.len = p - crt->issuer_raw.p;
1319
1320 /*
1321 * Validity ::= SEQUENCE {
1322 * notBefore Time,
1323 * notAfter Time }
1324 *
1325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1327 &crt->valid_to)) != 0) {
1328 mbedtls_x509_crt_free(crt);
1329 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330 }
1331
1332 /*
1333 * subject Name
1334 */
1335 crt->subject_raw.p = p;
1336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1338 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1339 mbedtls_x509_crt_free(crt);
1340 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341 }
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1344 mbedtls_x509_crt_free(crt);
1345 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346 }
1347
1348 crt->subject_raw.len = p - crt->subject_raw.p;
1349
1350 /*
1351 * SubjectPublicKeyInfo
1352 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001353 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1355 mbedtls_x509_crt_free(crt);
1356 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001358 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359
1360 /*
1361 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1362 * -- If present, version shall be v2 or v3
1363 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1364 * -- If present, version shall be v2 or v3
1365 * extensions [3] EXPLICIT Extensions OPTIONAL
1366 * -- If present, version shall be v3
1367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (crt->version == 2 || crt->version == 3) {
1369 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1370 if (ret != 0) {
1371 mbedtls_x509_crt_free(crt);
1372 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373 }
1374 }
1375
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 if (crt->version == 2 || crt->version == 3) {
1377 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1378 if (ret != 0) {
1379 mbedtls_x509_crt_free(crt);
1380 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381 }
1382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (crt->version == 3) {
1385 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1386 if (ret != 0) {
1387 mbedtls_x509_crt_free(crt);
1388 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389 }
1390 }
1391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if (p != end) {
1393 mbedtls_x509_crt_free(crt);
1394 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1395 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396 }
1397
1398 end = crt_end;
1399
1400 /*
1401 * }
1402 * -- end of TBSCertificate
1403 *
1404 * signatureAlgorithm AlgorithmIdentifier,
1405 * signatureValue BIT STRING
1406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1408 mbedtls_x509_crt_free(crt);
1409 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410 }
1411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if (crt->sig_oid.len != sig_oid2.len ||
1413 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001414 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001415 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 (sig_params1.len != 0 &&
1417 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1418 mbedtls_x509_crt_free(crt);
1419 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420 }
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1423 mbedtls_x509_crt_free(crt);
1424 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425 }
1426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (p != end) {
1428 mbedtls_x509_crt_free(crt);
1429 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1430 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431 }
1432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434}
1435
1436/*
1437 * Parse one X.509 certificate in DER format from a buffer and add them to a
1438 * chained list
1439 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1441 const unsigned char *buf,
1442 size_t buflen,
1443 int make_copy,
1444 mbedtls_x509_crt_ext_cb_t cb,
1445 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446{
Janos Follath865b3eb2019-12-16 11:46:15 +00001447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
1450 /*
1451 * Check for valid input
1452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 if (crt == NULL || buf == NULL) {
1454 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1455 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458 prev = crt;
1459 crt = crt->next;
1460 }
1461
1462 /*
1463 * Add new certificate on the end of the chain if needed.
1464 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 if (crt->version != 0 && crt->next == NULL) {
1466 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (crt->next == NULL) {
1469 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1470 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471
1472 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 }
1476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1478 if (ret != 0) {
1479 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (crt != chain) {
1484 mbedtls_free(crt);
1485 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488 }
1489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491}
1492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1494 const unsigned char *buf,
1495 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001496{
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001498}
1499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1501 const unsigned char *buf,
1502 size_t buflen,
1503 int make_copy,
1504 mbedtls_x509_crt_ext_cb_t cb,
1505 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001506{
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001508}
1509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1511 const unsigned char *buf,
1512 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001513{
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001515}
1516
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001518 * Parse one or more PEM certificates from a buffer and add them to the chained
1519 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1522 const unsigned char *buf,
1523 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524{
Janos Follath98e28a72016-05-31 14:03:54 +01001525#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001526 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001528#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529
1530 /*
1531 * Check for valid input
1532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if (chain == NULL || buf == NULL) {
1534 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1535 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536
1537 /*
1538 * Determine buffer content. Buffer contains either one DER certificate or
1539 * one or more PEM certificates.
1540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1543 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001545 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1548 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1549 }
Janos Follath98e28a72016-05-31 14:03:54 +01001550#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001552#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001559 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001564 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 ret = mbedtls_pem_read_buffer(&pem,
1566 "-----BEGIN CERTIFICATE-----",
1567 "-----END CERTIFICATE-----",
1568 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571 /*
1572 * Was PEM encoded
1573 */
1574 buflen -= use_len;
1575 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1577 return ret;
1578 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1579 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580
1581 /*
1582 * PEM header and footer were found
1583 */
1584 buflen -= use_len;
1585 buf += use_len;
1586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001588 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001590
Paul Bakker5a5fa922014-09-26 14:53:04 +02001591 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 /*
1603 * Quit parsing on a memory error
1604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1606 return ret;
1607 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
1613 total_failed++;
1614 continue;
1615 }
1616
1617 success = 1;
1618 }
1619 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if (success) {
1622 return total_failed;
1623 } else if (first_error) {
1624 return first_error;
1625 } else {
1626 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1627 }
Janos Follath98e28a72016-05-31 14:03:54 +01001628#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629}
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632/*
1633 * Load one or more certificates and add them to the chained list
1634 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636{
Janos Follath865b3eb2019-12-16 11:46:15 +00001637 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638 size_t n;
1639 unsigned char *buf;
1640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1642 return ret;
1643 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 mbedtls_platform_zeroize(buf, n);
1648 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651}
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654{
1655 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001656#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657 int w_ret;
1658 WCHAR szDir[MAX_PATH];
1659 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001660 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662
Paul Bakker9af723c2014-05-01 13:03:14 +02001663 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664 HANDLE hFind;
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if (len > MAX_PATH - 3) {
1667 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1668 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 memset(szDir, 0, sizeof(szDir));
1671 memset(filename, 0, MAX_PATH);
1672 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001673 filename[len++] = '\\';
1674 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675 filename[len++] = '*';
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1678 MAX_PATH - 3);
1679 if (w_ret == 0) {
1680 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1681 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 hFind = FindFirstFileW(szDir, &file_data);
1684 if (hFind == INVALID_HANDLE_VALUE) {
1685 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1686 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687
1688 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 do {
1690 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001693 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001697 -1,
1698 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 NULL, NULL);
1700 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001701 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1702 goto cleanup;
1703 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1706 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001709 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 }
1711 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001716
Ron Eldor36d90422017-01-09 15:09:16 +02001717cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001719#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001720 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001721 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001722 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001723 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001724 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (dir == NULL) {
1728 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1729 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001730
Ron Eldor63140682017-01-09 19:27:59 +02001731#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1733 closedir(dir);
1734 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001735 }
Ron Eldor63140682017-01-09 19:27:59 +02001736#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001741 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001744 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001745 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1746 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 } else if (stat(entry_name, &sb) == -1) {
1748 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001749 /* Broken symbolic link - ignore this entry.
1750 stat(2) will return this error for either (a) a dangling
1751 symlink or (b) a missing file.
1752 Given that we have just obtained the filename from readdir,
1753 assume that it does exist and therefore treat this as a
1754 dangling symlink. */
1755 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001757 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001758 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1759 goto cleanup;
1760 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761 }
1762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001766
1767 // Ignore parse errors
1768 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1770 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001771 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001773 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001776
1777cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001779
Ron Eldor63140682017-01-09 19:27:59 +02001780#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 }
Ron Eldor63140682017-01-09 19:27:59 +02001784#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001785
Paul Bakkerbe089b02013-10-14 15:51:50 +02001786#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001791
Przemek Stekiela2939e82023-01-03 13:35:54 +01001792/*
1793 * OtherName ::= SEQUENCE {
1794 * type-id OBJECT IDENTIFIER,
1795 * value [0] EXPLICIT ANY DEFINED BY type-id }
1796 *
1797 * HardwareModuleName ::= SEQUENCE {
1798 * hwType OBJECT IDENTIFIER,
1799 * hwSerialNum OCTET STRING }
1800 *
1801 * NOTE: we currently only parse and use otherName of type HwModuleName,
1802 * as defined in RFC 4108.
1803 */
1804static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1805 mbedtls_x509_san_other_name *other_name)
1806{
1807 int ret = 0;
1808 size_t len;
1809 unsigned char *p = subject_alt_name->p;
1810 const unsigned char *end = p + subject_alt_name->len;
1811 mbedtls_x509_buf cur_oid;
1812
1813 if ((subject_alt_name->tag &
1814 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1815 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1816 /*
1817 * The given subject alternative name is not of type "othername".
1818 */
1819 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1820 }
1821
1822 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1823 MBEDTLS_ASN1_OID)) != 0) {
1824 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1825 }
1826
1827 cur_oid.tag = MBEDTLS_ASN1_OID;
1828 cur_oid.p = p;
1829 cur_oid.len = len;
1830
1831 /*
1832 * Only HwModuleName is currently supported.
1833 */
1834 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1835 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1836 }
1837
1838 if (p + len >= end) {
1839 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1840 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1841 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1842 }
1843 p += len;
1844 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1845 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1846 0) {
1847 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1848 }
1849
1850 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1851 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1852 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1853 }
1854
1855 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1856 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1857 }
1858
1859 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1860 other_name->value.hardware_module_name.oid.p = p;
1861 other_name->value.hardware_module_name.oid.len = len;
1862
1863 if (p + len >= end) {
1864 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1865 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1866 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1867 }
1868 p += len;
1869 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1870 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1871 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1872 }
1873
1874 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1875 other_name->value.hardware_module_name.val.p = p;
1876 other_name->value.hardware_module_name.val.len = len;
1877 p += len;
1878 if (p != end) {
1879 mbedtls_platform_zeroize(other_name,
1880 sizeof(*other_name));
1881 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1882 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1883 }
1884 return 0;
1885}
1886
1887int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1888 mbedtls_x509_subject_alternative_name *san)
1889{
1890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1891 switch (san_buf->tag &
1892 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1893 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1894 /*
1895 * otherName
1896 */
1897 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1898 {
1899 mbedtls_x509_san_other_name other_name;
1900
1901 ret = x509_get_other_name(san_buf, &other_name);
1902 if (ret != 0) {
1903 return ret;
1904 }
1905
1906 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1907 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1908 memcpy(&san->san.other_name,
1909 &other_name, sizeof(other_name));
1910
1911 }
1912 break;
1913
1914 /*
1915 * dNSName
1916 */
1917 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1918 {
1919 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1920 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1921
1922 memcpy(&san->san.unstructured_name,
1923 san_buf, sizeof(*san_buf));
1924
1925 }
1926 break;
1927
1928 /*
Przemek Stekiela2939e82023-01-03 13:35:54 +01001929 * Type not supported
1930 */
1931 default:
1932 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1933 }
1934 return 0;
1935}
1936
Peter Kolbus9a969b62018-12-11 13:55:56 -06001937#if !defined(MBEDTLS_X509_REMOVE_INFO)
toth92g8d435a02021-05-10 15:16:33 +02001938static int x509_info_subject_alt_name(char **buf, size_t *size,
1939 const mbedtls_x509_sequence
1940 *subject_alt_name,
1941 const char *prefix)
1942{
1943 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1944 size_t i;
1945 size_t n = *size;
1946 char *p = *buf;
1947 const mbedtls_x509_sequence *cur = subject_alt_name;
1948 mbedtls_x509_subject_alternative_name san;
1949 int parse_ret;
1950
1951 while (cur != NULL) {
1952 memset(&san, 0, sizeof(san));
Przemek Stekiel9a511c52023-01-03 10:23:13 +01001953 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
toth92g8d435a02021-05-10 15:16:33 +02001954 if (parse_ret != 0) {
1955 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1956 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1957 MBEDTLS_X509_SAFE_SNPRINTF;
1958 } else {
1959 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1960 MBEDTLS_X509_SAFE_SNPRINTF;
1961 }
1962 cur = cur->next;
1963 continue;
1964 }
1965
1966 switch (san.type) {
1967 /*
1968 * otherName
1969 */
1970 case MBEDTLS_X509_SAN_OTHER_NAME:
1971 {
1972 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1973
1974 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1975 MBEDTLS_X509_SAFE_SNPRINTF;
1976
1977 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1978 &other_name->value.hardware_module_name.oid) != 0) {
1979 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1980 MBEDTLS_X509_SAFE_SNPRINTF;
1981 ret =
1982 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1983 MBEDTLS_X509_SAFE_SNPRINTF;
1984
1985 ret = mbedtls_oid_get_numeric_string(p,
1986 n,
1987 &other_name->value.hardware_module_name.oid);
1988 MBEDTLS_X509_SAFE_SNPRINTF;
1989
1990 ret =
1991 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1992 MBEDTLS_X509_SAFE_SNPRINTF;
1993
1994 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1995 ret = mbedtls_snprintf(p,
1996 n,
1997 "%02X",
1998 other_name->value.hardware_module_name.val.p[i]);
1999 MBEDTLS_X509_SAFE_SNPRINTF;
2000 }
2001 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
2002 }
2003 break;
2004
2005 /*
2006 * dNSName
2007 */
2008 case MBEDTLS_X509_SAN_DNS_NAME:
2009 {
2010 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
2011 MBEDTLS_X509_SAFE_SNPRINTF;
2012 if (san.san.unstructured_name.len >= n) {
2013 *p = '\0';
2014 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
2015 }
2016
2017 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
2018 p += san.san.unstructured_name.len;
2019 n -= san.san.unstructured_name.len;
2020 }
2021 break;
2022
2023 /*
2024 * Type not supported, skip item.
2025 */
2026 default:
2027 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
2028 MBEDTLS_X509_SAFE_SNPRINTF;
2029 break;
2030 }
2031
2032 cur = cur->next;
2033 }
2034
2035 *p = '\0';
2036
2037 *size = n;
2038 *buf = p;
2039
2040 return 0;
2041}
2042
toth92g8d435a02021-05-10 15:16:33 +02002043#define PRINT_ITEM(i) \
2044 { \
2045 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
2046 MBEDTLS_X509_SAFE_SNPRINTF; \
2047 sep = ", "; \
2048 }
2049
2050#define CERT_TYPE(type, name) \
2051 if (ns_cert_type & (type)) \
2052 PRINT_ITEM(name);
2053
2054static int x509_info_cert_type(char **buf, size_t *size,
2055 unsigned char ns_cert_type)
2056{
2057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2058 size_t n = *size;
2059 char *p = *buf;
2060 const char *sep = "";
2061
2062 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
2063 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
2064 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
2065 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
2066 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
2067 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
2068 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
2069 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
2070
2071 *size = n;
2072 *buf = p;
2073
2074 return 0;
2075}
2076
2077#define KEY_USAGE(code, name) \
2078 if (key_usage & (code)) \
2079 PRINT_ITEM(name);
2080
2081static int x509_info_key_usage(char **buf, size_t *size,
2082 unsigned int key_usage)
2083{
2084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2085 size_t n = *size;
2086 char *p = *buf;
2087 const char *sep = "";
2088
2089 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
2090 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
2091 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
2092 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
2093 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
2094 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
2095 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
2096 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
2097 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
2098
2099 *size = n;
2100 *buf = p;
2101
2102 return 0;
2103}
2104
Gilles Peskine449bd832023-01-11 14:50:10 +01002105static int x509_info_ext_key_usage(char **buf, size_t *size,
2106 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002107{
Janos Follath865b3eb2019-12-16 11:46:15 +00002108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002109 const char *desc;
2110 size_t n = *size;
2111 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002113 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 while (cur != NULL) {
2116 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002117 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002121 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002122
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002123 sep = ", ";
2124
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002125 cur = cur->next;
2126 }
2127
2128 *size = n;
2129 *buf = p;
2130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002132}
2133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134static int x509_info_cert_policies(char **buf, size_t *size,
2135 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02002136{
Janos Follath865b3eb2019-12-16 11:46:15 +00002137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002138 const char *desc;
2139 size_t n = *size;
2140 char *p = *buf;
2141 const mbedtls_x509_sequence *cur = certificate_policies;
2142 const char *sep = "";
2143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 while (cur != NULL) {
2145 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02002146 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002150 MBEDTLS_X509_SAFE_SNPRINTF;
2151
2152 sep = ", ";
2153
2154 cur = cur->next;
2155 }
2156
2157 *size = n;
2158 *buf = p;
2159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002161}
2162
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002163/*
2164 * Return an informational string about the certificate.
2165 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002166#define BEFORE_COLON 18
2167#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01002168int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
2169 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002170{
Janos Follath865b3eb2019-12-16 11:46:15 +00002171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002172 size_t n;
2173 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002174 char key_size_str[BEFORE_COLON];
2175
2176 p = buf;
2177 n = size;
2178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (NULL == crt) {
2180 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01002181 MBEDTLS_X509_SAFE_SNPRINTF;
2182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01002184 }
2185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
2187 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002188 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 ret = mbedtls_snprintf(p, n, "%sserial number : ",
2190 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002191 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002194 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002197 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002199 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002202 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002204 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
2207 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2208 crt->valid_from.year, crt->valid_from.mon,
2209 crt->valid_from.day, crt->valid_from.hour,
2210 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002211 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
2214 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2215 crt->valid_to.year, crt->valid_to.mon,
2216 crt->valid_to.day, crt->valid_to.hour,
2217 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002218 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002221 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
2224 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002225 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002226
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002227 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
2229 mbedtls_pk_get_name(&crt->pk))) != 0) {
2230 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002231 }
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
2234 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002235 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002236
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002237 /*
2238 * Optional extensions
2239 */
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
2242 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
2243 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002244 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if (crt->max_pathlen > 0) {
2247 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002248 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002249 }
2250 }
2251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2253 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002254 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002255
Przemek Stekiel21c37282023-01-16 08:47:49 +01002256 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
2257 &crt->subject_alt_names,
2258 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 return ret;
2260 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002261 }
2262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
2264 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002265 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002266
Przemek Stekiel21c37282023-01-16 08:47:49 +01002267 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 return ret;
2269 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002270 }
2271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2273 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002274 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002275
Przemek Stekiel21c37282023-01-16 08:47:49 +01002276 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 return ret;
2278 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002279 }
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2282 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002283 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 if ((ret = x509_info_ext_key_usage(&p, &n,
2286 &crt->ext_key_usage)) != 0) {
2287 return ret;
2288 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002289 }
2290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2292 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002293 MBEDTLS_X509_SAFE_SNPRINTF;
2294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 if ((ret = x509_info_cert_policies(&p, &n,
2296 &crt->certificate_policies)) != 0) {
2297 return ret;
2298 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002299 }
2300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002302 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305}
2306
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002307struct x509_crt_verify_string {
2308 int code;
2309 const char *string;
2310};
2311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002313static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002314 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002315 { 0, NULL }
2316};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002317#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
2320 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002321{
Janos Follath865b3eb2019-12-16 11:46:15 +00002322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002323 const struct x509_crt_verify_string *cur;
2324 char *p = buf;
2325 size_t n = size;
2326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
2328 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002329 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002333 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002334 flags ^= cur->code;
2335 }
2336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (flags != 0) {
2338 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
2339 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002340 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002341 }
2342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002344}
Hanno Becker612a2f12020-10-09 09:19:39 +01002345#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2348 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002349{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002350 unsigned int usage_must, usage_may;
2351 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
2355 return 0;
2356 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002357
2358 usage_must = usage & ~may_mask;
2359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
2361 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2362 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002363
2364 usage_may = usage & may_mask;
2365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
2367 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2368 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002371}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2374 const char *usage_oid,
2375 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002378
2379 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2381 return 0;
2382 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002383
2384 /*
2385 * Look for the requested usage (or wildcard ANY) in our list
2386 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (cur_oid->len == usage_len &&
2391 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2392 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002393 }
2394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2396 return 0;
2397 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002398 }
2399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002401}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002404/*
2405 * Return 1 if the certificate is revoked, or 0 otherwise.
2406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002407int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 while (cur != NULL && cur->serial.len != 0) {
2412 if (crt->serial.len == cur->serial.len &&
2413 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2414 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002415 }
2416
2417 cur = cur->next;
2418 }
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002421}
2422
2423/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002424 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002425 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002426 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2428 mbedtls_x509_crl *crl_list,
2429 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002430{
2431 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002432 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002433#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002434 psa_algorithm_t psa_algorithm;
2435#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002437#endif /* MBEDTLS_USE_PSA_CRYPTO */
2438 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 if (ca == NULL) {
2441 return flags;
2442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 while (crl_list != NULL) {
2445 if (crl_list->version == 0 ||
2446 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002447 crl_list = crl_list->next;
2448 continue;
2449 }
2450
2451 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002452 * Check if the CA is configured to sign CRLs
2453 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 if (mbedtls_x509_crt_check_key_usage(ca,
2455 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002457 break;
2458 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002459
2460 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002461 * Check if CRL is correctly signed by the trusted CA
2462 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002464 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002468 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002470
pespacek7599a772022-02-07 14:40:55 +01002471#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
2473 if (psa_hash_compute(psa_algorithm,
2474 crl_list->tbs.p,
2475 crl_list->tbs.len,
2476 hash,
2477 sizeof(hash),
2478 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002479 /* Note: this can't happen except after an internal error */
2480 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2481 break;
2482 }
pespacek7599a772022-02-07 14:40:55 +01002483#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2485 hash_length = mbedtls_md_get_size(md_info);
2486 if (mbedtls_md(md_info,
2487 crl_list->tbs.p,
2488 crl_list->tbs.len,
2489 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002490 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002492 break;
2493 }
pespaceka7a64692022-02-14 15:18:43 +01002494#endif /* MBEDTLS_USE_PSA_CRYPTO */
2495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002497 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2501 crl_list->sig_md, hash, hash_length,
2502 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002504 break;
2505 }
2506
2507 /*
2508 * Check for validity of CRL (Do not drop out)
2509 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002511 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002515 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002517
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002518 /*
2519 * Check if certificate is revoked
2520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002523 break;
2524 }
2525
2526 crl_list = crl_list->next;
2527 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002530}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002532
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002533/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002534 * Check the signature of a certificate by its parent
2535 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2537 mbedtls_x509_crt *parent,
2538 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002539{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002540 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002541 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002542#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2543 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 md_info = mbedtls_md_info_from_type(child->sig_md);
2545 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002546
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002547 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2549 return -1;
2550 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002551#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 status = psa_hash_compute(hash_alg,
2556 child->tbs.p,
2557 child->tbs.len,
2558 hash,
2559 sizeof(hash),
2560 &hash_len);
2561 if (status != PSA_SUCCESS) {
2562 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002563 }
2564
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002565#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002566 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2568 return -1;
2569 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002570
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002571#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2573 return mbedtls_pk_verify_restartable(&parent->pk,
2574 child->sig_md, hash, hash_len,
2575 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002576 }
2577#else
2578 (void) rs_ctx;
2579#endif
2580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2582 child->sig_md, hash, hash_len,
2583 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002584}
2585
2586/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002587 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2588 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002589 *
2590 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002592static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2593 const mbedtls_x509_crt *parent,
2594 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002595{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002596 int need_ca_bit;
2597
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002598 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2600 return -1;
2601 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002602
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002603 /* Parent must have the basicConstraints CA bit set as a general rule */
2604 need_ca_bit = 1;
2605
2606 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002608 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002609 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 if (need_ca_bit && !parent->ca_istrue) {
2612 return -1;
2613 }
2614
2615 if (need_ca_bit &&
2616 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2617 return -1;
2618 }
2619
2620 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002621}
2622
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002623/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002624 * Find a suitable parent for child in candidates, or return NULL.
2625 *
2626 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002627 * 1. subject name matches child's issuer
2628 * 2. if necessary, the CA bit is set and key usage allows signing certs
2629 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002630 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002631 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002632 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002633 * If there's a suitable candidate which is also time-valid, return the first
2634 * such. Otherwise, return the first suitable candidate (or NULL if there is
2635 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002636 *
2637 * The rationale for this rule is that someone could have a list of trusted
2638 * roots with two versions on the same root with different validity periods.
2639 * (At least one user reported having such a list and wanted it to just work.)
2640 * The reason we don't just require time-validity is that generally there is
2641 * only one version, and if it's expired we want the flags to state that
2642 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002643 *
2644 * The rationale for rule 3 (signature for trusted roots) is that users might
2645 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002646 * way we select the correct one is by checking the signature (as we don't
2647 * rely on key identifier extensions). (This is one way users might choose to
2648 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002649 *
2650 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002651 * - [in] child: certificate for which we're looking for a parent
2652 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002653 * - [out] r_parent: parent found (or NULL)
2654 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002655 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2656 * of the chain, 0 otherwise
2657 * - [in] path_cnt: number of intermediates seen so far
2658 * - [in] self_cnt: number of self-signed intermediates seen so far
2659 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002660 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002661 *
2662 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002663 * - 0 on success
2664 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002665 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002666static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 mbedtls_x509_crt *child,
2668 mbedtls_x509_crt *candidates,
2669 mbedtls_x509_crt **r_parent,
2670 int *r_signature_is_good,
2671 int top,
2672 unsigned path_cnt,
2673 unsigned self_cnt,
2674 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002675{
Janos Follath865b3eb2019-12-16 11:46:15 +00002676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002677 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002678 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002679
2680#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002681 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002683 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002684 parent = rs_ctx->parent;
2685 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002686 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002687
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002688 /* clear saved state */
2689 rs_ctx->parent = NULL;
2690 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002691 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002692
2693 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002694 goto check_signature;
2695 }
2696#endif
2697
2698 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002699 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002702 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002704 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002706
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002707 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 if (parent->max_pathlen > 0 &&
2709 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002710 continue;
2711 }
2712
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002713 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002714#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2715check_signature:
2716#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002718
2719#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002721 /* save state */
2722 rs_ctx->parent = parent;
2723 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002724 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002727 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002728#else
2729 (void) ret;
2730#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002731
2732 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002734 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002736
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002737 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2739 mbedtls_x509_time_is_future(&parent->valid_from)) {
2740 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002741 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002742 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002743 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002744
2745 continue;
2746 }
2747
Andy Gross1f627142019-01-30 10:25:53 -06002748 *r_parent = parent;
2749 *r_signature_is_good = signature_is_good;
2750
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002751 break;
2752 }
2753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002755 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002756 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002757 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002760}
2761
2762/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002763 * Find a parent in trusted CAs or the provided chain, or return NULL.
2764 *
2765 * Searches in trusted CAs first, and return the first suitable parent found
2766 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002767 *
2768 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002769 * - [in] child: certificate for which we're looking for a parent, followed
2770 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002771 * - [in] trust_ca: list of locally trusted certificates
2772 * - [out] parent: parent found (or NULL)
2773 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2774 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2775 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2776 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002777 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002778 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002779 *
2780 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002781 * - 0 on success
2782 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002783 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002784static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 mbedtls_x509_crt *child,
2786 mbedtls_x509_crt *trust_ca,
2787 mbedtls_x509_crt **parent,
2788 int *parent_is_trusted,
2789 int *signature_is_good,
2790 unsigned path_cnt,
2791 unsigned self_cnt,
2792 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002793{
Janos Follath865b3eb2019-12-16 11:46:15 +00002794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002795 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002796
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002797 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002798
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002799#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002800 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002802 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002803 rs_ctx->parent_is_trusted = -1;
2804 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002805#endif
2806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002808 search_list = *parent_is_trusted ? trust_ca : child->next;
2809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 ret = x509_crt_find_parent_in(child, search_list,
2811 parent, signature_is_good,
2812 *parent_is_trusted,
2813 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002814
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002815#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002817 /* save state */
2818 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002820 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002821#else
2822 (void) ret;
2823#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002824
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002825 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002827 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002829
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002830 /* prepare second iteration */
2831 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002832 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002833
2834 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002836 *parent_is_trusted = 0;
2837 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002838 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002841}
2842
2843/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002844 * Check if an end-entity certificate is locally trusted
2845 *
2846 * Currently we require such certificates to be self-signed (actually only
2847 * check for self-issued as self-signatures are not checked)
2848 */
2849static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 mbedtls_x509_crt *crt,
2851 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002852{
2853 mbedtls_x509_crt *cur;
2854
2855 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2857 return -1;
2858 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002859
2860 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2862 if (crt->raw.len == cur->raw.len &&
2863 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2864 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002865 }
2866 }
2867
2868 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002870}
2871
2872/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002873 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002874 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002875 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2876 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002877 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002878 * such that every cert in the chain is a child of the next one,
2879 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002880 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002881 * Verify that chain and return it with flags for all issues found.
2882 *
2883 * Special cases:
2884 * - EE == Rj -> return a one-element list containing it
2885 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2886 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002887 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002888 * Tests for (aspects of) this function should include at least:
2889 * - trusted EE
2890 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002891 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002892 * - if relevant: EE untrusted
2893 * - if relevant: EE -> intermediate, untrusted
2894 * with the aspect under test checked at each relevant level (EE, int, root).
2895 * For some aspects longer chains are required, but usually length 2 is
2896 * enough (but length 1 is not in general).
2897 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002898 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002899 * - [in] crt: the cert list EE, C1, ..., Cn
2900 * - [in] trust_ca: the trusted list R1, ..., Rp
2901 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002902 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002903 * Only valid when return value is 0, may contain garbage otherwise!
2904 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002905 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002906 *
2907 * Return value:
2908 * - non-zero if the chain could not be fully built and examined
2909 * - 0 is the chain was successfully built and examined,
2910 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002911 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002912static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 mbedtls_x509_crt *crt,
2914 mbedtls_x509_crt *trust_ca,
2915 mbedtls_x509_crl *ca_crl,
2916 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2917 void *p_ca_cb,
2918 const mbedtls_x509_crt_profile *profile,
2919 mbedtls_x509_crt_verify_chain *ver_chain,
2920 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002921{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002922 /* Don't initialize any of those variables here, so that the compiler can
2923 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002925 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002926 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002927 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002928 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002929 int parent_is_trusted;
2930 int child_is_trusted;
2931 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002932 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002933 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002934
2935#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2936 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002938 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002939 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002940 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002941
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002942 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002943 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002944 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002945 flags = &cur->flags;
2946
2947 goto find_parent;
2948 }
2949#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002950
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002951 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002952 self_cnt = 0;
2953 parent_is_trusted = 0;
2954 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002957 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002958 cur = &ver_chain->items[ver_chain->len];
2959 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002960 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002961 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002962 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002963
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002964 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002966 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002968
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002970 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002972
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 if (child_is_trusted) {
2975 return 0;
2976 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002977
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002978 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002980 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002984 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002986
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002987 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (ver_chain->len == 1 &&
2989 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2990 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002991 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002992
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002993#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2994find_parent:
2995#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002996
2997 /* Obtain list of potential trusted signers from CA callback,
2998 * or use statically provided list. */
2999#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 if (f_ca_cb != NULL) {
3001 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
3002 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003003 ver_chain->trust_ca_cb_result = NULL;
3004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
3006 if (ret != 0) {
3007 return MBEDTLS_ERR_X509_FATAL_ERROR;
3008 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00003009
3010 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00003012#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3013 {
3014 ((void) f_ca_cb);
3015 ((void) p_ca_cb);
3016 cur_trust_ca = trust_ca;
3017 }
3018
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003019 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
3021 &parent_is_trusted, &signature_is_good,
3022 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003023
3024#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003026 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003027 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003028 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003029 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003032 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003033#else
3034 (void) ret;
3035#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003036
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003037 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003039 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003041 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003042
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003043 /* Count intermediate self-issued (not necessarily self-signed) certs.
3044 * These can occur with some strategies for key rollover, see [SIRO],
3045 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 if (ver_chain->len != 1 &&
3047 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003048 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003049 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003050
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003051 /* path_cnt is 0 for the first intermediate CA,
3052 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 if (!parent_is_trusted &&
3054 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003055 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003057 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003058
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003059 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003061 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003063
3064 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003066 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003069#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003070 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003072#else
3073 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003074#endif
3075
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003076 /* prepare for next iteration */
3077 child = parent;
3078 parent = NULL;
3079 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003080 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003081 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003082}
3083
3084/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003085 * Check for CN match
3086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003087static int x509_crt_check_cn(const mbedtls_x509_buf *name,
3088 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003089{
3090 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 if (name->len == cn_len &&
3092 x509_memcasecmp(cn, name->p, cn_len) == 0) {
3093 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003094 }
3095
3096 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 if (x509_check_wildcard(cn, name) == 0) {
3098 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003099 }
3100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003102}
3103
3104/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003105 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003107static int x509_crt_check_san(const mbedtls_x509_buf *name,
3108 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003109{
3110 const unsigned char san_type = (unsigned char) name->tag &
3111 MBEDTLS_ASN1_TAG_VALUE_MASK;
3112
3113 /* dNSName */
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
3115 return x509_crt_check_cn(name, cn, cn_len);
3116 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003117
3118 /* (We may handle other types here later.) */
3119
3120 /* Unrecognized type */
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003122}
3123
3124/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003125 * Verify the requested CN - only call this if cn is not NULL!
3126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003127static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3128 const char *cn,
3129 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003130{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003131 const mbedtls_x509_name *name;
3132 const mbedtls_x509_sequence *cur;
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
3136 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
3137 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003138 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003139 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003140 }
3141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003143 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 }
3145 } else {
3146 for (name = &crt->subject; name != NULL; name = name->next) {
3147 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3148 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
3149 break;
3150 }
3151 }
3152
3153 if (name == NULL) {
3154 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3155 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003156 }
3157}
3158
3159/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003160 * Merge the flags for all certs in the chain, after calling callback
3161 */
3162static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 uint32_t *flags,
3164 const mbedtls_x509_crt_verify_chain *ver_chain,
3165 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3166 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003167{
Janos Follath865b3eb2019-12-16 11:46:15 +00003168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003169 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003170 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003171 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003172
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003174 cur = &ver_chain->items[i-1];
3175 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003176
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 if (NULL != f_vrfy) {
3178 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3179 return ret;
3180 }
3181 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003182
3183 *flags |= cur_flags;
3184 }
3185
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003187}
3188
3189/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003190 * Verify the certificate validity, with profile, restartable version
3191 *
3192 * This function:
3193 * - checks the requested CN (if any)
3194 * - checks the type and size of the EE cert's key,
3195 * as that isn't done as part of chain building/verification currently
3196 * - builds and verifies the chain
3197 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003198 *
3199 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3200 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3201 * verification routine to search for trusted signers, and CRLs will
3202 * be disabled. Otherwise, `trust_ca` will be used as the static list
3203 * of trusted signers, and `ca_crl` will be use as the static list
3204 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003205 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003206static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3207 mbedtls_x509_crt *trust_ca,
3208 mbedtls_x509_crl *ca_crl,
3209 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3210 void *p_ca_cb,
3211 const mbedtls_x509_crt_profile *profile,
3212 const char *cn, uint32_t *flags,
3213 int (*f_vrfy)(void *,
3214 mbedtls_x509_crt *,
3215 int,
3216 uint32_t *),
3217 void *p_vrfy,
3218 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003219{
Janos Follath865b3eb2019-12-16 11:46:15 +00003220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003221 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003222 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003223 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003224
3225 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003226 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003230 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3231 goto exit;
3232 }
3233
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003234 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 if (cn != NULL) {
3236 x509_crt_verify_name(crt, cn, &ee_flags);
3237 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003238
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003239 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003243 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003244 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003247 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003249
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003250 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3252 f_ca_cb, p_ca_cb, profile,
3253 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003254
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003256 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003258
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003259 /* Merge end-entity flags */
3260 ver_chain.items[0].flags |= ee_flags;
3261
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003262 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003264
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003265exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003266
3267#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3269 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003270 ver_chain.trust_ca_cb_result = NULL;
3271#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3272
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003273#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3275 mbedtls_x509_crt_restart_free(rs_ctx);
3276 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003277#endif
3278
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003279 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3280 * the SSL module for authmode optional, but non-zero return from the
3281 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003283 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003284 }
3285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 if (ret != 0) {
3287 *flags = (uint32_t) -1;
3288 return ret;
3289 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003290
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 if (*flags != 0) {
3292 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3293 }
3294
3295 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003296}
3297
Hanno Becker3116fb32019-03-28 13:34:42 +00003298
3299/*
3300 * Verify the certificate validity (default profile, not restartable)
3301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003302int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3303 mbedtls_x509_crt *trust_ca,
3304 mbedtls_x509_crl *ca_crl,
3305 const char *cn, uint32_t *flags,
3306 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3307 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003308{
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3310 NULL, NULL,
3311 &mbedtls_x509_crt_profile_default,
3312 cn, flags,
3313 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003314}
3315
3316/*
3317 * Verify the certificate validity (user-chosen profile, not restartable)
3318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003319int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3320 mbedtls_x509_crt *trust_ca,
3321 mbedtls_x509_crl *ca_crl,
3322 const mbedtls_x509_crt_profile *profile,
3323 const char *cn, uint32_t *flags,
3324 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3325 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003326{
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3328 NULL, NULL,
3329 profile, cn, flags,
3330 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003331}
3332
3333#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3334/*
3335 * Verify the certificate validity (user-chosen profile, CA callback,
3336 * not restartable).
3337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003338int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3339 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3340 void *p_ca_cb,
3341 const mbedtls_x509_crt_profile *profile,
3342 const char *cn, uint32_t *flags,
3343 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3344 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003345{
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3347 f_ca_cb, p_ca_cb,
3348 profile, cn, flags,
3349 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003350}
3351#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3354 mbedtls_x509_crt *trust_ca,
3355 mbedtls_x509_crl *ca_crl,
3356 const mbedtls_x509_crt_profile *profile,
3357 const char *cn, uint32_t *flags,
3358 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3359 void *p_vrfy,
3360 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003361{
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3363 NULL, NULL,
3364 profile, cn, flags,
3365 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003366}
3367
3368
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003369/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003370 * Initialize a certificate chain
3371 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003372void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003373{
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003375}
3376
3377/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003378 * Unallocate all certificate data
3379 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003380void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 mbedtls_x509_crt *cert_cur = crt;
3383 mbedtls_x509_crt *cert_prv;
Przemek Stekiel62d8f842023-01-03 09:37:47 +01003384 mbedtls_x509_sequence *seq_cur;
3385 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 while (cert_cur != NULL) {
3388 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003392#endif
3393
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3395 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3396 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3397 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3398 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003399
toth92g8d435a02021-05-10 15:16:33 +02003400 seq_cur = cert_cur->authority_key_id.authorityCertIssuer.next;
3401 while (seq_cur != NULL) {
3402 seq_prv = seq_cur;
3403 seq_cur = seq_cur->next;
3404 mbedtls_platform_zeroize(seq_prv,
3405 sizeof(mbedtls_x509_sequence));
3406 mbedtls_free(seq_prv);
toth92ga41954d2021-02-12 16:11:17 +01003407 }
3408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3410 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3411 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003412 }
3413
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003414 cert_prv = cert_cur;
3415 cert_cur = cert_cur->next;
3416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3418 if (cert_prv != crt) {
3419 mbedtls_free(cert_prv);
3420 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003421 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003422}
3423
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003424#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3425/*
3426 * Initialize a restart context
3427 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003428void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003429{
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003431
3432 ctx->parent = NULL;
3433 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003434 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003435
3436 ctx->parent_is_trusted = -1;
3437
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003438 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003439 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003441}
3442
3443/*
3444 * Free the components of a restart context
3445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003446void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003447{
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003449 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003451
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 mbedtls_pk_restart_free(&ctx->pk);
3453 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003454}
3455#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457#endif /* MBEDTLS_X509_CRT_PARSE_C */