blob: 269ccbc4a4029db5ee243bc3409205f56cb2237c [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) {
612 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
613 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
614 }
615
toth92ga41954d2021-02-12 16:11:17 +0100616 return 0;
617}
618
619/*
toth92ga41954d2021-02-12 16:11:17 +0100620 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
621 *
622 * GeneralName ::= CHOICE {
623 * otherName [0] OtherName,
624 * rfc822Name [1] IA5String,
625 * dNSName [2] IA5String,
626 * x400Address [3] ORAddress,
627 * directoryName [4] Name,
628 * ediPartyName [5] EDIPartyName,
629 * uniformResourceIdentifier [6] IA5String,
630 * iPAddress [7] OCTET STRING,
631 * registeredID [8] OBJECT IDENTIFIER }
632 *
633 * OtherName ::= SEQUENCE {
634 * type-id OBJECT IDENTIFIER,
635 * value [0] EXPLICIT ANY DEFINED BY type-id }
636 *
637 * EDIPartyName ::= SEQUENCE {
638 * nameAssigner [0] DirectoryString OPTIONAL,
639 * partyName [1] DirectoryString }
640 *
641 * NOTE: we list all types, but only use dNSName and otherName
642 * of type HwModuleName, as defined in RFC 4108, at this point.
643 */
toth92g8d435a02021-05-10 15:16:33 +0200644static int x509_get_general_names(unsigned char **p,
645 const unsigned char *end,
646 mbedtls_x509_sequence *subject_alt_name)
toth92ga41954d2021-02-12 16:11:17 +0100647{
648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
649 size_t len, tag_len;
650 mbedtls_asn1_buf *buf;
651 unsigned char tag;
652 mbedtls_asn1_sequence *cur = subject_alt_name;
653
654 /* Get main sequence tag */
655 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
656 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
657 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
658 }
659
660 if (*p + len != end) {
661 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
662 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
663 }
664
665 while (*p < end) {
666 mbedtls_x509_subject_alternative_name dummy_san_buf;
667 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
668
669 tag = **p;
670 (*p)++;
671 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
672 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
673 }
674
toth92g8d435a02021-05-10 15:16:33 +0200675 /* Tag shall be CONTEXT_SPECIFIC or SET */
toth92ga41954d2021-02-12 16:11:17 +0100676 if ((tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
677 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
toth92g8d435a02021-05-10 15:16:33 +0200678 if ((tag & (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
679 (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
680 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
681 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
682 }
toth92ga41954d2021-02-12 16:11:17 +0100683 }
684
685 /*
686 * Check that the SAN is structured correctly.
687 */
Przemek Stekiel9a511c52023-01-03 10:23:13 +0100688 ret = mbedtls_x509_parse_subject_alt_name(&(cur->buf), &dummy_san_buf);
toth92ga41954d2021-02-12 16:11:17 +0100689 /*
690 * In case the extension is malformed, return an error,
691 * and clear the allocated sequences.
692 */
693 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
694 mbedtls_asn1_sequence_free(subject_alt_name->next);
695 subject_alt_name->next = NULL;
696 return ret;
697 }
698
699 /* Allocate and assign next pointer */
700 if (cur->buf.p != NULL) {
701 if (cur->next != NULL) {
702 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
703 }
704
705 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
706
707 if (cur->next == NULL) {
708 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
709 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
710 }
711
712 cur = cur->next;
713 }
714
715 buf = &(cur->buf);
716 buf->tag = tag;
717 buf->p = *p;
718 buf->len = tag_len;
719 *p += buf->len;
720 }
721
722 /* Set final sequence entry's next pointer to NULL */
723 cur->next = NULL;
724
725 if (*p != end) {
726 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
727 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
728 }
729
730 return 0;
731}
732
733/*
toth92g8d435a02021-05-10 15:16:33 +0200734 * AuthorityKeyIdentifier ::= SEQUENCE {
735 * keyIdentifier [0] KeyIdentifier OPTIONAL,
736 * authorityCertIssuer [1] GeneralNames OPTIONAL,
737 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
738 *
739 * KeyIdentifier ::= OCTET STRING
740 */
741static int x509_get_authority_key_id(unsigned char **p,
742 unsigned char *end,
743 mbedtls_x509_authority *authority_key_id)
744{
745 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
746 size_t len = 0u;
747
748 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
749 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE | 0)) != 0) {
750 return ret;
751 }
752
753 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
754 MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0) {
755 /* KeyIdentifier is an OPTIONAL field */
756 } else {
757 authority_key_id->keyIdentifier.len = len;
758 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200759 /* Setting tag of the keyIdentfier intentionally to 0x04.
760 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
761 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200762 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
763
764 *p += len;
765 }
766
767 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200768 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200769 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
770 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
771 1)) != 0) {
772 /* authorityCertIssuer is an OPTIONAL field */
773 } else {
toth92g9232e0a2021-05-11 12:55:58 +0200774 /* Getting directoryName using the required specific class tag [4] */
toth92g8d435a02021-05-10 15:16:33 +0200775 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
776 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
777 MBEDTLS_ASN1_CONSTRUCTED | 4)) != 0) {
778 return ret;
779 } else {
780 /* "end" also includes the CertSerialNumber field so "len" shall be used */
781 ret = x509_get_general_names(p,
782 (*p+len),
783 &authority_key_id->authorityCertIssuer);
784 }
785 }
786 }
787
788 if (*p < end) {
789 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
790 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_INTEGER)) !=
791 0) {
792 /* authorityCertSerialNumber is an OPTIONAL field, but if there are still data it must be the serial number */
793 return ret;
794 } else {
795 authority_key_id->authorityCertSerialNumber.len = len;
796 authority_key_id->authorityCertSerialNumber.p = *p;
797 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_OCTET_STRING;
798 *p += len;
799 }
800 }
801
802 if (*p != end) {
803 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
804 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
805 }
806
807 return 0;
808}
809
810/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200811 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
812 *
813 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
814 *
815 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
816 *
817 * PolicyInformation ::= SEQUENCE {
818 * policyIdentifier CertPolicyId,
819 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
820 * PolicyQualifierInfo OPTIONAL }
821 *
822 * CertPolicyId ::= OBJECT IDENTIFIER
823 *
824 * PolicyQualifierInfo ::= SEQUENCE {
825 * policyQualifierId PolicyQualifierId,
826 * qualifier ANY DEFINED BY policyQualifierId }
827 *
828 * -- policyQualifierIds for Internet policy qualifiers
829 *
830 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
831 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
832 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
833 *
834 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
835 *
836 * Qualifier ::= CHOICE {
837 * cPSuri CPSuri,
838 * userNotice UserNotice }
839 *
840 * CPSuri ::= IA5String
841 *
842 * UserNotice ::= SEQUENCE {
843 * noticeRef NoticeReference OPTIONAL,
844 * explicitText DisplayText OPTIONAL }
845 *
846 * NoticeReference ::= SEQUENCE {
847 * organization DisplayText,
848 * noticeNumbers SEQUENCE OF INTEGER }
849 *
850 * DisplayText ::= CHOICE {
851 * ia5String IA5String (SIZE (1..200)),
852 * visibleString VisibleString (SIZE (1..200)),
853 * bmpString BMPString (SIZE (1..200)),
854 * utf8String UTF8String (SIZE (1..200)) }
855 *
856 * NOTE: we only parse and use anyPolicy without qualifiers at this point
857 * as defined in RFC 5280.
858 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859static int x509_get_certificate_policies(unsigned char **p,
860 const unsigned char *end,
861 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200862{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300863 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200864 size_t len;
865 mbedtls_asn1_buf *buf;
866 mbedtls_asn1_sequence *cur = certificate_policies;
867
868 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 ret = mbedtls_asn1_get_tag(p, end, &len,
870 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
871 if (ret != 0) {
872 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
873 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (*p + len != end) {
876 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
877 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
878 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200879
880 /*
881 * Cannot be an empty sequence.
882 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (len == 0) {
884 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
885 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
886 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200889 mbedtls_x509_buf policy_oid;
890 const unsigned char *policy_end;
891
892 /*
893 * Get the policy sequence
894 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
896 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
897 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
898 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200899
900 policy_end = *p + len;
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
903 MBEDTLS_ASN1_OID)) != 0) {
904 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
905 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200906
907 policy_oid.tag = MBEDTLS_ASN1_OID;
908 policy_oid.len = len;
909 policy_oid.p = *p;
910
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300911 /*
912 * Only AnyPolicy is currently supported when enforcing policy.
913 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300915 /*
916 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200917 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300918 */
919 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
920 }
921
Ron Eldor74d9acc2019-03-21 14:00:03 +0200922 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (cur->buf.p != NULL) {
924 if (cur->next != NULL) {
925 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
926 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (cur->next == NULL) {
931 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
932 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
933 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200934
935 cur = cur->next;
936 }
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200939 buf->tag = policy_oid.tag;
940 buf->p = policy_oid.p;
941 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300942
943 *p += len;
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 /*
946 * If there is an optional qualifier, then *p < policy_end
947 * Check the Qualifier len to verify it doesn't exceed policy_end.
948 */
949 if (*p < policy_end) {
950 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
951 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
952 0) {
953 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
954 }
Ron Eldor08063792019-05-13 16:38:39 +0300955 /*
956 * Skip the optional policy qualifiers.
957 */
958 *p += len;
959 }
960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (*p != policy_end) {
962 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
963 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
964 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200965 }
966
967 /* Set final sequence entry's next pointer to NULL */
968 cur->next = NULL;
969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (*p != end) {
971 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
972 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
973 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200976}
977
978/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 * X.509 v3 extensions
980 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982static int x509_get_crt_ext(unsigned char **p,
983 const unsigned char *end,
984 mbedtls_x509_crt *crt,
985 mbedtls_x509_crt_ext_cb_t cb,
986 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987{
Janos Follath865b3eb2019-12-16 11:46:15 +0000988 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200990 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 if (*p == end) {
993 return 0;
994 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
997 return ret;
998 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
Hanno Becker12f62fb2019-02-12 17:22:36 +00001000 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 /*
1003 * Extension ::= SEQUENCE {
1004 * extnID OBJECT IDENTIFIER,
1005 * critical BOOLEAN DEFAULT FALSE,
1006 * extnValue OCTET STRING }
1007 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 int is_critical = 0; /* DEFAULT FALSE */
1010 int ext_type = 0;
1011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1013 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1014 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1015 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016
1017 end_ext_data = *p + len;
1018
1019 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
1021 MBEDTLS_ASN1_OID)) != 0) {
1022 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1023 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024
k-stachowiak463928a2018-07-24 12:50:59 +02001025 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 extn_oid.p = *p;
1027 *p += extn_oid.len;
1028
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
1031 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
1032 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1033 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034
1035 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
1037 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1038 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1039 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001041 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 end_ext_octet = *p + len;
1043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if (end_ext_octet != end_ext_data) {
1045 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1046 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1047 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048
1049 /*
1050 * Detect supported extensions
1051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001055 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (cb != NULL) {
1057 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
1058 if (ret != 0 && is_critical) {
1059 return ret;
1060 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001061 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001062 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001063 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001064
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065 /* No parser found, skip extension */
1066 *p = end_ext_octet;
1067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1071 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073 continue;
1074 }
1075
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001076 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if ((crt->ext_types & ext_type) != 0) {
1078 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1079 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001080
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081 crt->ext_types |= ext_type;
1082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 switch (ext_type) {
1084 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
1085 /* Parse basic constraints */
1086 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
1087 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
1088 return ret;
1089 }
1090 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 case MBEDTLS_X509_EXT_KEY_USAGE:
1093 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001094 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
1095 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return ret;
1097 }
1098 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1101 /* Parse extended key usage */
1102 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1103 &crt->ext_key_usage)) != 0) {
1104 return ret;
1105 }
1106 break;
toth92g8d435a02021-05-10 15:16:33 +02001107
toth92ga41954d2021-02-12 16:11:17 +01001108 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1109 /* Parse subject key identifier */
1110 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1111 &crt->subject_key_id)) != 0) {
1112 return ret;
1113 }
1114 break;
toth92g8d435a02021-05-10 15:16:33 +02001115
toth92ga41954d2021-02-12 16:11:17 +01001116 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1117 /* Parse authority key identifier */
1118 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1119 &crt->authority_key_id)) != 0) {
1120 return ret;
1121 }
1122 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001124 /* Parse subject alt name
1125 * SubjectAltName ::= GeneralNames
1126 */
1127 if ((ret = x509_get_general_names(p, end_ext_octet,
1128 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return ret;
1130 }
1131 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1134 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001135 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1136 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return ret;
1138 }
1139 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1142 /* Parse certificate policies type */
1143 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1144 &crt->certificate_policies)) != 0) {
1145 /* Give the callback (if any) a chance to handle the extension
1146 * if it contains unsupported policies */
1147 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1148 cb(p_ctx, crt, &extn_oid, is_critical,
1149 start_ext_octet, end_ext_octet) == 0) {
1150 break;
1151 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (is_critical) {
1154 return ret;
1155 } else
1156 /*
1157 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1158 * cannot interpret or enforce the policy. However, it is up to
1159 * the user to choose how to enforce the policies,
1160 * unless the extension is critical.
1161 */
1162 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1163 return ret;
1164 }
1165 }
1166 break;
1167
1168 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001169 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 * If this is a non-critical extension, which the oid layer
1171 * supports, but there isn't an x509 parser for it,
1172 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001173 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (is_critical) {
1175 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1176 } else {
1177 *p = end_ext_octet;
1178 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 }
1180 }
1181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (*p != end) {
1183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1184 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1185 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188}
1189
1190/*
1191 * Parse and fill a single X.509 certificate in DER format
1192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1194 const unsigned char *buf,
1195 size_t buflen,
1196 int make_copy,
1197 mbedtls_x509_crt_ext_cb_t cb,
1198 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199{
Janos Follath865b3eb2019-12-16 11:46:15 +00001200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 size_t len;
1202 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1206 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1207 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208
1209 /*
1210 * Check for valid input
1211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (crt == NULL || buf == NULL) {
1213 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1214 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001216 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001218 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 end = p + len;
1220
1221 /*
1222 * Certificate ::= SEQUENCE {
1223 * tbsCertificate TBSCertificate,
1224 * signatureAlgorithm AlgorithmIdentifier,
1225 * signatureValue BIT STRING }
1226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1228 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1229 mbedtls_x509_crt_free(crt);
1230 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231 }
1232
Janos Follathcc0e49d2016-02-17 14:34:12 +00001233 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001234 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001236 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1238 if (crt->raw.p == NULL) {
1239 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1240 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001243 crt->own_buffer = 1;
1244
1245 p += crt->raw.len - len;
1246 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 } else {
1248 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001249 crt->own_buffer = 0;
1250 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001251
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252 /*
1253 * TBSCertificate ::= SEQUENCE {
1254 */
1255 crt->tbs.p = p;
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1258 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1259 mbedtls_x509_crt_free(crt);
1260 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261 }
1262
1263 end = p + len;
1264 crt->tbs.len = end - crt->tbs.p;
1265
1266 /*
1267 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1268 *
1269 * CertificateSerialNumber ::= INTEGER
1270 *
1271 * signature AlgorithmIdentifier
1272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1274 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1275 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1276 &sig_params1)) != 0) {
1277 mbedtls_x509_crt_free(crt);
1278 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 }
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (crt->version < 0 || crt->version > 2) {
1282 mbedtls_x509_crt_free(crt);
1283 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001284 }
1285
Andres AG7ca4a032017-03-09 16:16:11 +00001286 crt->version++;
1287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1289 &crt->sig_md, &crt->sig_pk,
1290 &crt->sig_opts)) != 0) {
1291 mbedtls_x509_crt_free(crt);
1292 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 }
1294
1295 /*
1296 * issuer Name
1297 */
1298 crt->issuer_raw.p = p;
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1301 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1302 mbedtls_x509_crt_free(crt);
1303 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 }
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1307 mbedtls_x509_crt_free(crt);
1308 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 }
1310
1311 crt->issuer_raw.len = p - crt->issuer_raw.p;
1312
1313 /*
1314 * Validity ::= SEQUENCE {
1315 * notBefore Time,
1316 * notAfter Time }
1317 *
1318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1320 &crt->valid_to)) != 0) {
1321 mbedtls_x509_crt_free(crt);
1322 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001323 }
1324
1325 /*
1326 * subject Name
1327 */
1328 crt->subject_raw.p = p;
1329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1331 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1332 mbedtls_x509_crt_free(crt);
1333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334 }
1335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1337 mbedtls_x509_crt_free(crt);
1338 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001339 }
1340
1341 crt->subject_raw.len = p - crt->subject_raw.p;
1342
1343 /*
1344 * SubjectPublicKeyInfo
1345 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001346 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1348 mbedtls_x509_crt_free(crt);
1349 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001351 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
1353 /*
1354 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1355 * -- If present, version shall be v2 or v3
1356 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1357 * -- If present, version shall be v2 or v3
1358 * extensions [3] EXPLICIT Extensions OPTIONAL
1359 * -- If present, version shall be v3
1360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 if (crt->version == 2 || crt->version == 3) {
1362 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1363 if (ret != 0) {
1364 mbedtls_x509_crt_free(crt);
1365 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366 }
1367 }
1368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 if (crt->version == 2 || crt->version == 3) {
1370 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1371 if (ret != 0) {
1372 mbedtls_x509_crt_free(crt);
1373 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374 }
1375 }
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if (crt->version == 3) {
1378 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1379 if (ret != 0) {
1380 mbedtls_x509_crt_free(crt);
1381 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 }
1383 }
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 if (p != end) {
1386 mbedtls_x509_crt_free(crt);
1387 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1388 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389 }
1390
1391 end = crt_end;
1392
1393 /*
1394 * }
1395 * -- end of TBSCertificate
1396 *
1397 * signatureAlgorithm AlgorithmIdentifier,
1398 * signatureValue BIT STRING
1399 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1401 mbedtls_x509_crt_free(crt);
1402 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403 }
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 if (crt->sig_oid.len != sig_oid2.len ||
1406 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001407 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001408 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 (sig_params1.len != 0 &&
1410 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1411 mbedtls_x509_crt_free(crt);
1412 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001413 }
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1416 mbedtls_x509_crt_free(crt);
1417 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001418 }
1419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 if (p != end) {
1421 mbedtls_x509_crt_free(crt);
1422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1423 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424 }
1425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427}
1428
1429/*
1430 * Parse one X.509 certificate in DER format from a buffer and add them to a
1431 * chained list
1432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1434 const unsigned char *buf,
1435 size_t buflen,
1436 int make_copy,
1437 mbedtls_x509_crt_ext_cb_t cb,
1438 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439{
Janos Follath865b3eb2019-12-16 11:46:15 +00001440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
1443 /*
1444 * Check for valid input
1445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 if (crt == NULL || buf == NULL) {
1447 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1448 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 prev = crt;
1452 crt = crt->next;
1453 }
1454
1455 /*
1456 * Add new certificate on the end of the chain if needed.
1457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (crt->version != 0 && crt->next == NULL) {
1459 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 if (crt->next == NULL) {
1462 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1463 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464
1465 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 }
1469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1471 if (ret != 0) {
1472 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001473 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (crt != chain) {
1477 mbedtls_free(crt);
1478 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481 }
1482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001484}
1485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1487 const unsigned char *buf,
1488 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001489{
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001491}
1492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1494 const unsigned char *buf,
1495 size_t buflen,
1496 int make_copy,
1497 mbedtls_x509_crt_ext_cb_t cb,
1498 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001499{
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001501}
1502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1504 const unsigned char *buf,
1505 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001506{
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001508}
1509
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001511 * Parse one or more PEM certificates from a buffer and add them to the chained
1512 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001513 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001514int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1515 const unsigned char *buf,
1516 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517{
Janos Follath98e28a72016-05-31 14:03:54 +01001518#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001519 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001521#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522
1523 /*
1524 * Check for valid input
1525 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (chain == NULL || buf == NULL) {
1527 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1528 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529
1530 /*
1531 * Determine buffer content. Buffer contains either one DER certificate or
1532 * one or more PEM certificates.
1533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1536 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001538 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1541 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1542 }
Janos Follath98e28a72016-05-31 14:03:54 +01001543#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001545#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001552 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001557 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 ret = mbedtls_pem_read_buffer(&pem,
1559 "-----BEGIN CERTIFICATE-----",
1560 "-----END CERTIFICATE-----",
1561 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564 /*
1565 * Was PEM encoded
1566 */
1567 buflen -= use_len;
1568 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1570 return ret;
1571 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1572 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573
1574 /*
1575 * PEM header and footer were found
1576 */
1577 buflen -= use_len;
1578 buf += use_len;
1579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583
Paul Bakker5a5fa922014-09-26 14:53:04 +02001584 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595 /*
1596 * Quit parsing on a memory error
1597 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1599 return ret;
1600 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605
1606 total_failed++;
1607 continue;
1608 }
1609
1610 success = 1;
1611 }
1612 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 if (success) {
1615 return total_failed;
1616 } else if (first_error) {
1617 return first_error;
1618 } else {
1619 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1620 }
Janos Follath98e28a72016-05-31 14:03:54 +01001621#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622}
1623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625/*
1626 * Load one or more certificates and add them to the chained list
1627 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001628int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629{
Janos Follath865b3eb2019-12-16 11:46:15 +00001630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 size_t n;
1632 unsigned char *buf;
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1635 return ret;
1636 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 mbedtls_platform_zeroize(buf, n);
1641 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644}
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647{
1648 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001649#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650 int w_ret;
1651 WCHAR szDir[MAX_PATH];
1652 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001653 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655
Paul Bakker9af723c2014-05-01 13:03:14 +02001656 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657 HANDLE hFind;
1658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (len > MAX_PATH - 3) {
1660 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1661 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 memset(szDir, 0, sizeof(szDir));
1664 memset(filename, 0, MAX_PATH);
1665 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001666 filename[len++] = '\\';
1667 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668 filename[len++] = '*';
1669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1671 MAX_PATH - 3);
1672 if (w_ret == 0) {
1673 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1674 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 hFind = FindFirstFileW(szDir, &file_data);
1677 if (hFind == INVALID_HANDLE_VALUE) {
1678 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1679 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680
1681 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 do {
1683 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001690 -1,
1691 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 NULL, NULL);
1693 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001694 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1695 goto cleanup;
1696 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1699 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 }
1704 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001709
Ron Eldor36d90422017-01-09 15:09:16 +02001710cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001712#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001713 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001714 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001715 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001716 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001717 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (dir == NULL) {
1721 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1722 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723
Ron Eldor63140682017-01-09 19:27:59 +02001724#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1726 closedir(dir);
1727 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001728 }
Ron Eldor63140682017-01-09 19:27:59 +02001729#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001734 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001737 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001738 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1739 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 } else if (stat(entry_name, &sb) == -1) {
1741 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001742 /* Broken symbolic link - ignore this entry.
1743 stat(2) will return this error for either (a) a dangling
1744 symlink or (b) a missing file.
1745 Given that we have just obtained the filename from readdir,
1746 assume that it does exist and therefore treat this as a
1747 dangling symlink. */
1748 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001750 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001751 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1752 goto cleanup;
1753 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754 }
1755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759
1760 // Ignore parse errors
1761 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1763 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001766 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001769
1770cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001772
Ron Eldor63140682017-01-09 19:27:59 +02001773#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 }
Ron Eldor63140682017-01-09 19:27:59 +02001777#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001778
Paul Bakkerbe089b02013-10-14 15:51:50 +02001779#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784
Przemek Stekiela2939e82023-01-03 13:35:54 +01001785/*
1786 * OtherName ::= SEQUENCE {
1787 * type-id OBJECT IDENTIFIER,
1788 * value [0] EXPLICIT ANY DEFINED BY type-id }
1789 *
1790 * HardwareModuleName ::= SEQUENCE {
1791 * hwType OBJECT IDENTIFIER,
1792 * hwSerialNum OCTET STRING }
1793 *
1794 * NOTE: we currently only parse and use otherName of type HwModuleName,
1795 * as defined in RFC 4108.
1796 */
1797static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1798 mbedtls_x509_san_other_name *other_name)
1799{
1800 int ret = 0;
1801 size_t len;
1802 unsigned char *p = subject_alt_name->p;
1803 const unsigned char *end = p + subject_alt_name->len;
1804 mbedtls_x509_buf cur_oid;
1805
1806 if ((subject_alt_name->tag &
1807 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1808 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1809 /*
1810 * The given subject alternative name is not of type "othername".
1811 */
1812 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1813 }
1814
1815 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1816 MBEDTLS_ASN1_OID)) != 0) {
1817 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1818 }
1819
1820 cur_oid.tag = MBEDTLS_ASN1_OID;
1821 cur_oid.p = p;
1822 cur_oid.len = len;
1823
1824 /*
1825 * Only HwModuleName is currently supported.
1826 */
1827 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1828 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1829 }
1830
1831 if (p + len >= end) {
1832 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1833 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1834 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1835 }
1836 p += len;
1837 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1838 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1839 0) {
1840 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1841 }
1842
1843 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1844 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1845 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1846 }
1847
1848 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1849 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1850 }
1851
1852 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1853 other_name->value.hardware_module_name.oid.p = p;
1854 other_name->value.hardware_module_name.oid.len = len;
1855
1856 if (p + len >= end) {
1857 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1858 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1859 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1860 }
1861 p += len;
1862 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1863 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1864 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1865 }
1866
1867 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1868 other_name->value.hardware_module_name.val.p = p;
1869 other_name->value.hardware_module_name.val.len = len;
1870 p += len;
1871 if (p != end) {
1872 mbedtls_platform_zeroize(other_name,
1873 sizeof(*other_name));
1874 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1875 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1876 }
1877 return 0;
1878}
1879
1880int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1881 mbedtls_x509_subject_alternative_name *san)
1882{
1883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1884 switch (san_buf->tag &
1885 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1886 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1887 /*
1888 * otherName
1889 */
1890 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1891 {
1892 mbedtls_x509_san_other_name other_name;
1893
1894 ret = x509_get_other_name(san_buf, &other_name);
1895 if (ret != 0) {
1896 return ret;
1897 }
1898
1899 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1900 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1901 memcpy(&san->san.other_name,
1902 &other_name, sizeof(other_name));
1903
1904 }
1905 break;
1906
1907 /*
1908 * dNSName
1909 */
1910 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1911 {
1912 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1913 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1914
1915 memcpy(&san->san.unstructured_name,
1916 san_buf, sizeof(*san_buf));
1917
1918 }
1919 break;
1920
1921 /*
1922 * RFC822 Name
1923 */
1924 case (MBEDTLS_ASN1_SEQUENCE | MBEDTLS_X509_SAN_RFC822_NAME):
1925 {
1926 mbedtls_x509_name rfc822Name;
1927 unsigned char *bufferPointer = san_buf->p;
1928 unsigned char **p = &bufferPointer;
1929 const unsigned char *end = san_buf->p + san_buf->len;
1930
1931 /* The leading ASN1 tag and length has been processed. Stepping back with 2 bytes, because mbedtls_x509_get_name expects the beginning of the SET tag */
1932 *p = *p - 2;
1933
1934 ret = mbedtls_x509_get_name(p, end, &rfc822Name);
1935 if (ret != 0) {
1936 return ret;
1937 }
1938
1939 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1940 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1941 memcpy(&san->san.unstructured_name,
1942 &rfc822Name, sizeof(rfc822Name));
1943 }
1944 break;
1945
1946 /*
1947 * Type not supported
1948 */
1949 default:
1950 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1951 }
1952 return 0;
1953}
1954
Peter Kolbus9a969b62018-12-11 13:55:56 -06001955#if !defined(MBEDTLS_X509_REMOVE_INFO)
toth92g8d435a02021-05-10 15:16:33 +02001956static int x509_info_subject_alt_name(char **buf, size_t *size,
1957 const mbedtls_x509_sequence
1958 *subject_alt_name,
1959 const char *prefix)
1960{
1961 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1962 size_t i;
1963 size_t n = *size;
1964 char *p = *buf;
1965 const mbedtls_x509_sequence *cur = subject_alt_name;
1966 mbedtls_x509_subject_alternative_name san;
1967 int parse_ret;
1968
1969 while (cur != NULL) {
1970 memset(&san, 0, sizeof(san));
Przemek Stekiel9a511c52023-01-03 10:23:13 +01001971 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
toth92g8d435a02021-05-10 15:16:33 +02001972 if (parse_ret != 0) {
1973 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1974 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1975 MBEDTLS_X509_SAFE_SNPRINTF;
1976 } else {
1977 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1978 MBEDTLS_X509_SAFE_SNPRINTF;
1979 }
1980 cur = cur->next;
1981 continue;
1982 }
1983
1984 switch (san.type) {
1985 /*
1986 * otherName
1987 */
1988 case MBEDTLS_X509_SAN_OTHER_NAME:
1989 {
1990 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1991
1992 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1993 MBEDTLS_X509_SAFE_SNPRINTF;
1994
1995 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1996 &other_name->value.hardware_module_name.oid) != 0) {
1997 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1998 MBEDTLS_X509_SAFE_SNPRINTF;
1999 ret =
2000 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
2001 MBEDTLS_X509_SAFE_SNPRINTF;
2002
2003 ret = mbedtls_oid_get_numeric_string(p,
2004 n,
2005 &other_name->value.hardware_module_name.oid);
2006 MBEDTLS_X509_SAFE_SNPRINTF;
2007
2008 ret =
2009 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
2010 MBEDTLS_X509_SAFE_SNPRINTF;
2011
2012 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
2013 ret = mbedtls_snprintf(p,
2014 n,
2015 "%02X",
2016 other_name->value.hardware_module_name.val.p[i]);
2017 MBEDTLS_X509_SAFE_SNPRINTF;
2018 }
2019 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
2020 }
2021 break;
2022
2023 /*
2024 * dNSName
2025 */
2026 case MBEDTLS_X509_SAN_DNS_NAME:
2027 {
2028 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
2029 MBEDTLS_X509_SAFE_SNPRINTF;
2030 if (san.san.unstructured_name.len >= n) {
2031 *p = '\0';
2032 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
2033 }
2034
2035 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
2036 p += san.san.unstructured_name.len;
2037 n -= san.san.unstructured_name.len;
2038 }
2039 break;
2040
2041 /*
2042 * Type not supported, skip item.
2043 */
2044 default:
2045 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
2046 MBEDTLS_X509_SAFE_SNPRINTF;
2047 break;
2048 }
2049
2050 cur = cur->next;
2051 }
2052
2053 *p = '\0';
2054
2055 *size = n;
2056 *buf = p;
2057
2058 return 0;
2059}
2060
toth92g8d435a02021-05-10 15:16:33 +02002061#define PRINT_ITEM(i) \
2062 { \
2063 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
2064 MBEDTLS_X509_SAFE_SNPRINTF; \
2065 sep = ", "; \
2066 }
2067
2068#define CERT_TYPE(type, name) \
2069 if (ns_cert_type & (type)) \
2070 PRINT_ITEM(name);
2071
2072static int x509_info_cert_type(char **buf, size_t *size,
2073 unsigned char ns_cert_type)
2074{
2075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2076 size_t n = *size;
2077 char *p = *buf;
2078 const char *sep = "";
2079
2080 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
2081 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
2082 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
2083 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
2084 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
2085 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
2086 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
2087 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
2088
2089 *size = n;
2090 *buf = p;
2091
2092 return 0;
2093}
2094
2095#define KEY_USAGE(code, name) \
2096 if (key_usage & (code)) \
2097 PRINT_ITEM(name);
2098
2099static int x509_info_key_usage(char **buf, size_t *size,
2100 unsigned int key_usage)
2101{
2102 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2103 size_t n = *size;
2104 char *p = *buf;
2105 const char *sep = "";
2106
2107 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
2108 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
2109 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
2110 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
2111 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
2112 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
2113 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
2114 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
2115 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
2116
2117 *size = n;
2118 *buf = p;
2119
2120 return 0;
2121}
2122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123static int x509_info_ext_key_usage(char **buf, size_t *size,
2124 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002125{
Janos Follath865b3eb2019-12-16 11:46:15 +00002126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002127 const char *desc;
2128 size_t n = *size;
2129 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002131 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 while (cur != NULL) {
2134 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002135 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002139 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002140
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002141 sep = ", ";
2142
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002143 cur = cur->next;
2144 }
2145
2146 *size = n;
2147 *buf = p;
2148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002150}
2151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152static int x509_info_cert_policies(char **buf, size_t *size,
2153 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02002154{
Janos Follath865b3eb2019-12-16 11:46:15 +00002155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002156 const char *desc;
2157 size_t n = *size;
2158 char *p = *buf;
2159 const mbedtls_x509_sequence *cur = certificate_policies;
2160 const char *sep = "";
2161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 while (cur != NULL) {
2163 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02002164 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002168 MBEDTLS_X509_SAFE_SNPRINTF;
2169
2170 sep = ", ";
2171
2172 cur = cur->next;
2173 }
2174
2175 *size = n;
2176 *buf = p;
2177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002179}
2180
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002181/*
2182 * Return an informational string about the certificate.
2183 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002184#define BEFORE_COLON 18
2185#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01002186int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
2187 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002188{
Janos Follath865b3eb2019-12-16 11:46:15 +00002189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002190 size_t n;
2191 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002192 char key_size_str[BEFORE_COLON];
2193
2194 p = buf;
2195 n = size;
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (NULL == crt) {
2198 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01002199 MBEDTLS_X509_SAFE_SNPRINTF;
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01002202 }
2203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
2205 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002206 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 ret = mbedtls_snprintf(p, n, "%sserial number : ",
2208 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002209 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002212 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002215 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002217 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002220 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002222 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
2225 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2226 crt->valid_from.year, crt->valid_from.mon,
2227 crt->valid_from.day, crt->valid_from.hour,
2228 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002229 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
2232 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2233 crt->valid_to.year, crt->valid_to.mon,
2234 crt->valid_to.day, crt->valid_to.hour,
2235 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002236 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002239 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
2242 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002243 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002244
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002245 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
2247 mbedtls_pk_get_name(&crt->pk))) != 0) {
2248 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002249 }
2250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
2252 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002253 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002254
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002255 /*
2256 * Optional extensions
2257 */
2258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
2260 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
2261 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002262 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002263
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (crt->max_pathlen > 0) {
2265 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002266 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002267 }
2268 }
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2271 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002272 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002273
Przemek Stekiel21c37282023-01-16 08:47:49 +01002274 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
2275 &crt->subject_alt_names,
2276 prefix)) != 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_NS_CERT_TYPE) {
2282 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002283 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002284
Przemek Stekiel21c37282023-01-16 08:47:49 +01002285 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 return ret;
2287 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002288 }
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2291 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002292 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002293
Przemek Stekiel21c37282023-01-16 08:47:49 +01002294 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 return ret;
2296 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002297 }
2298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2300 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002301 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if ((ret = x509_info_ext_key_usage(&p, &n,
2304 &crt->ext_key_usage)) != 0) {
2305 return ret;
2306 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002307 }
2308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2310 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002311 MBEDTLS_X509_SAFE_SNPRINTF;
2312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if ((ret = x509_info_cert_policies(&p, &n,
2314 &crt->certificate_policies)) != 0) {
2315 return ret;
2316 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002317 }
2318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002320 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323}
2324
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002325struct x509_crt_verify_string {
2326 int code;
2327 const char *string;
2328};
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002331static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002332 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002333 { 0, NULL }
2334};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002335#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
2338 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002339{
Janos Follath865b3eb2019-12-16 11:46:15 +00002340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002341 const struct x509_crt_verify_string *cur;
2342 char *p = buf;
2343 size_t n = size;
2344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
2346 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002347 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002351 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002352 flags ^= cur->code;
2353 }
2354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (flags != 0) {
2356 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
2357 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002358 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002359 }
2360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002362}
Hanno Becker612a2f12020-10-09 09:19:39 +01002363#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2366 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002367{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002368 unsigned int usage_must, usage_may;
2369 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
2373 return 0;
2374 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002375
2376 usage_must = usage & ~may_mask;
2377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
2379 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2380 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002381
2382 usage_may = usage & may_mask;
2383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
2385 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2386 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002389}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2392 const char *usage_oid,
2393 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002396
2397 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2399 return 0;
2400 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002401
2402 /*
2403 * Look for the requested usage (or wildcard ANY) in our list
2404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 if (cur_oid->len == usage_len &&
2409 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2410 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002411 }
2412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2414 return 0;
2415 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002416 }
2417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002419}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002422/*
2423 * Return 1 if the certificate is revoked, or 0 otherwise.
2424 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002426{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 while (cur != NULL && cur->serial.len != 0) {
2430 if (crt->serial.len == cur->serial.len &&
2431 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2432 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002433 }
2434
2435 cur = cur->next;
2436 }
2437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002439}
2440
2441/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002442 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002443 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2446 mbedtls_x509_crl *crl_list,
2447 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002448{
2449 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002450 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002451#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002452 psa_algorithm_t psa_algorithm;
2453#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002455#endif /* MBEDTLS_USE_PSA_CRYPTO */
2456 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 if (ca == NULL) {
2459 return flags;
2460 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 while (crl_list != NULL) {
2463 if (crl_list->version == 0 ||
2464 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002465 crl_list = crl_list->next;
2466 continue;
2467 }
2468
2469 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002470 * Check if the CA is configured to sign CRLs
2471 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (mbedtls_x509_crt_check_key_usage(ca,
2473 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002475 break;
2476 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002477
2478 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002479 * Check if CRL is correctly signed by the trusted CA
2480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002482 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002486 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002488
pespacek7599a772022-02-07 14:40:55 +01002489#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
2491 if (psa_hash_compute(psa_algorithm,
2492 crl_list->tbs.p,
2493 crl_list->tbs.len,
2494 hash,
2495 sizeof(hash),
2496 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002497 /* Note: this can't happen except after an internal error */
2498 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2499 break;
2500 }
pespacek7599a772022-02-07 14:40:55 +01002501#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2503 hash_length = mbedtls_md_get_size(md_info);
2504 if (mbedtls_md(md_info,
2505 crl_list->tbs.p,
2506 crl_list->tbs.len,
2507 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002508 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002510 break;
2511 }
pespaceka7a64692022-02-14 15:18:43 +01002512#endif /* MBEDTLS_USE_PSA_CRYPTO */
2513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002515 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2519 crl_list->sig_md, hash, hash_length,
2520 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002522 break;
2523 }
2524
2525 /*
2526 * Check for validity of CRL (Do not drop out)
2527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002533 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002535
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002536 /*
2537 * Check if certificate is revoked
2538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002541 break;
2542 }
2543
2544 crl_list = crl_list->next;
2545 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002548}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002550
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002551/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002552 * Check the signature of a certificate by its parent
2553 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002554static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2555 mbedtls_x509_crt *parent,
2556 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002557{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002558 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002559 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002560#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2561 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 md_info = mbedtls_md_info_from_type(child->sig_md);
2563 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002564
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002565 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2567 return -1;
2568 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002569#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002571 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 status = psa_hash_compute(hash_alg,
2574 child->tbs.p,
2575 child->tbs.len,
2576 hash,
2577 sizeof(hash),
2578 &hash_len);
2579 if (status != PSA_SUCCESS) {
2580 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002581 }
2582
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002583#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002584 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2586 return -1;
2587 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002588
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002589#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2591 return mbedtls_pk_verify_restartable(&parent->pk,
2592 child->sig_md, hash, hash_len,
2593 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002594 }
2595#else
2596 (void) rs_ctx;
2597#endif
2598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2600 child->sig_md, hash, hash_len,
2601 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002602}
2603
2604/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002605 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2606 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002607 *
2608 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002610static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2611 const mbedtls_x509_crt *parent,
2612 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002613{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002614 int need_ca_bit;
2615
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002616 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2618 return -1;
2619 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002620
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002621 /* Parent must have the basicConstraints CA bit set as a general rule */
2622 need_ca_bit = 1;
2623
2624 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002626 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002627 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (need_ca_bit && !parent->ca_istrue) {
2630 return -1;
2631 }
2632
2633 if (need_ca_bit &&
2634 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2635 return -1;
2636 }
2637
2638 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002639}
2640
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002641/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002642 * Find a suitable parent for child in candidates, or return NULL.
2643 *
2644 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002645 * 1. subject name matches child's issuer
2646 * 2. if necessary, the CA bit is set and key usage allows signing certs
2647 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002648 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002649 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002650 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002651 * If there's a suitable candidate which is also time-valid, return the first
2652 * such. Otherwise, return the first suitable candidate (or NULL if there is
2653 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002654 *
2655 * The rationale for this rule is that someone could have a list of trusted
2656 * roots with two versions on the same root with different validity periods.
2657 * (At least one user reported having such a list and wanted it to just work.)
2658 * The reason we don't just require time-validity is that generally there is
2659 * only one version, and if it's expired we want the flags to state that
2660 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002661 *
2662 * The rationale for rule 3 (signature for trusted roots) is that users might
2663 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002664 * way we select the correct one is by checking the signature (as we don't
2665 * rely on key identifier extensions). (This is one way users might choose to
2666 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002667 *
2668 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002669 * - [in] child: certificate for which we're looking for a parent
2670 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002671 * - [out] r_parent: parent found (or NULL)
2672 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002673 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2674 * of the chain, 0 otherwise
2675 * - [in] path_cnt: number of intermediates seen so far
2676 * - [in] self_cnt: number of self-signed intermediates seen so far
2677 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002678 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002679 *
2680 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002681 * - 0 on success
2682 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002683 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002684static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 mbedtls_x509_crt *child,
2686 mbedtls_x509_crt *candidates,
2687 mbedtls_x509_crt **r_parent,
2688 int *r_signature_is_good,
2689 int top,
2690 unsigned path_cnt,
2691 unsigned self_cnt,
2692 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002693{
Janos Follath865b3eb2019-12-16 11:46:15 +00002694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002695 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002696 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002697
2698#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002699 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002701 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002702 parent = rs_ctx->parent;
2703 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002704 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002705
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002706 /* clear saved state */
2707 rs_ctx->parent = NULL;
2708 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002709 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002710
2711 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002712 goto check_signature;
2713 }
2714#endif
2715
2716 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002717 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002720 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002722 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002724
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002725 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if (parent->max_pathlen > 0 &&
2727 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002728 continue;
2729 }
2730
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002731 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002732#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2733check_signature:
2734#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002736
2737#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002739 /* save state */
2740 rs_ctx->parent = parent;
2741 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002742 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002745 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002746#else
2747 (void) ret;
2748#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002749
2750 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002752 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002754
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002755 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2757 mbedtls_x509_time_is_future(&parent->valid_from)) {
2758 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002759 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002760 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002761 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002762
2763 continue;
2764 }
2765
Andy Gross1f627142019-01-30 10:25:53 -06002766 *r_parent = parent;
2767 *r_signature_is_good = signature_is_good;
2768
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002769 break;
2770 }
2771
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002773 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002774 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002775 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002778}
2779
2780/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002781 * Find a parent in trusted CAs or the provided chain, or return NULL.
2782 *
2783 * Searches in trusted CAs first, and return the first suitable parent found
2784 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002785 *
2786 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002787 * - [in] child: certificate for which we're looking for a parent, followed
2788 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002789 * - [in] trust_ca: list of locally trusted certificates
2790 * - [out] parent: parent found (or NULL)
2791 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2792 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2793 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2794 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002795 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002796 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002797 *
2798 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002799 * - 0 on success
2800 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002801 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002802static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 mbedtls_x509_crt *child,
2804 mbedtls_x509_crt *trust_ca,
2805 mbedtls_x509_crt **parent,
2806 int *parent_is_trusted,
2807 int *signature_is_good,
2808 unsigned path_cnt,
2809 unsigned self_cnt,
2810 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002811{
Janos Follath865b3eb2019-12-16 11:46:15 +00002812 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002813 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002814
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002815 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002816
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002817#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002818 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002820 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002821 rs_ctx->parent_is_trusted = -1;
2822 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002823#endif
2824
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002826 search_list = *parent_is_trusted ? trust_ca : child->next;
2827
Gilles Peskine449bd832023-01-11 14:50:10 +01002828 ret = x509_crt_find_parent_in(child, search_list,
2829 parent, signature_is_good,
2830 *parent_is_trusted,
2831 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002832
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002833#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002835 /* save state */
2836 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002838 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002839#else
2840 (void) ret;
2841#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002842
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002843 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002845 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002847
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002848 /* prepare second iteration */
2849 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002850 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002851
2852 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002854 *parent_is_trusted = 0;
2855 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002856 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002859}
2860
2861/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002862 * Check if an end-entity certificate is locally trusted
2863 *
2864 * Currently we require such certificates to be self-signed (actually only
2865 * check for self-issued as self-signatures are not checked)
2866 */
2867static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 mbedtls_x509_crt *crt,
2869 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002870{
2871 mbedtls_x509_crt *cur;
2872
2873 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2875 return -1;
2876 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002877
2878 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2880 if (crt->raw.len == cur->raw.len &&
2881 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2882 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002883 }
2884 }
2885
2886 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002888}
2889
2890/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002891 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002892 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002893 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2894 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002895 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002896 * such that every cert in the chain is a child of the next one,
2897 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002898 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002899 * Verify that chain and return it with flags for all issues found.
2900 *
2901 * Special cases:
2902 * - EE == Rj -> return a one-element list containing it
2903 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2904 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002905 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002906 * Tests for (aspects of) this function should include at least:
2907 * - trusted EE
2908 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002909 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002910 * - if relevant: EE untrusted
2911 * - if relevant: EE -> intermediate, untrusted
2912 * with the aspect under test checked at each relevant level (EE, int, root).
2913 * For some aspects longer chains are required, but usually length 2 is
2914 * enough (but length 1 is not in general).
2915 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002916 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002917 * - [in] crt: the cert list EE, C1, ..., Cn
2918 * - [in] trust_ca: the trusted list R1, ..., Rp
2919 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002920 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002921 * Only valid when return value is 0, may contain garbage otherwise!
2922 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002923 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002924 *
2925 * Return value:
2926 * - non-zero if the chain could not be fully built and examined
2927 * - 0 is the chain was successfully built and examined,
2928 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002929 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002930static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 mbedtls_x509_crt *crt,
2932 mbedtls_x509_crt *trust_ca,
2933 mbedtls_x509_crl *ca_crl,
2934 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2935 void *p_ca_cb,
2936 const mbedtls_x509_crt_profile *profile,
2937 mbedtls_x509_crt_verify_chain *ver_chain,
2938 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002939{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002940 /* Don't initialize any of those variables here, so that the compiler can
2941 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002943 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002944 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002945 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002946 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002947 int parent_is_trusted;
2948 int child_is_trusted;
2949 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002950 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002951 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002952
2953#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2954 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002956 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002957 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002958 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002959
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002960 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002961 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002962 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002963 flags = &cur->flags;
2964
2965 goto find_parent;
2966 }
2967#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002968
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002969 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002970 self_cnt = 0;
2971 parent_is_trusted = 0;
2972 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002976 cur = &ver_chain->items[ver_chain->len];
2977 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002978 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002979 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002980 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002981
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002984 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002988 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002990
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002991 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 if (child_is_trusted) {
2993 return 0;
2994 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002995
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002996 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002998 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02003000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003002 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02003004
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003005 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 if (ver_chain->len == 1 &&
3007 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
3008 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003009 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02003010
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003011#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3012find_parent:
3013#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00003014
3015 /* Obtain list of potential trusted signers from CA callback,
3016 * or use statically provided list. */
3017#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 if (f_ca_cb != NULL) {
3019 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
3020 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003021 ver_chain->trust_ca_cb_result = NULL;
3022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
3024 if (ret != 0) {
3025 return MBEDTLS_ERR_X509_FATAL_ERROR;
3026 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00003027
3028 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00003030#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3031 {
3032 ((void) f_ca_cb);
3033 ((void) p_ca_cb);
3034 cur_trust_ca = trust_ca;
3035 }
3036
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003037 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
3039 &parent_is_trusted, &signature_is_good,
3040 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003041
3042#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003044 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003045 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003046 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003047 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003050 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003051#else
3052 (void) ret;
3053#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003054
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003055 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003057 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003059 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003060
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003061 /* Count intermediate self-issued (not necessarily self-signed) certs.
3062 * These can occur with some strategies for key rollover, see [SIRO],
3063 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 if (ver_chain->len != 1 &&
3065 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003066 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003067 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003068
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003069 /* path_cnt is 0 for the first intermediate CA,
3070 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (!parent_is_trusted &&
3072 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003073 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003075 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003076
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003077 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003079 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003081
3082 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003084 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003088 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003090#else
3091 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003092#endif
3093
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003094 /* prepare for next iteration */
3095 child = parent;
3096 parent = NULL;
3097 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003098 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003099 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003100}
3101
3102/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003103 * Check for CN match
3104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003105static int x509_crt_check_cn(const mbedtls_x509_buf *name,
3106 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003107{
3108 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 if (name->len == cn_len &&
3110 x509_memcasecmp(cn, name->p, cn_len) == 0) {
3111 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003112 }
3113
3114 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 if (x509_check_wildcard(cn, name) == 0) {
3116 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003117 }
3118
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003120}
3121
3122/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003123 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003125static int x509_crt_check_san(const mbedtls_x509_buf *name,
3126 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003127{
3128 const unsigned char san_type = (unsigned char) name->tag &
3129 MBEDTLS_ASN1_TAG_VALUE_MASK;
3130
3131 /* dNSName */
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
3133 return x509_crt_check_cn(name, cn, cn_len);
3134 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003135
3136 /* (We may handle other types here later.) */
3137
3138 /* Unrecognized type */
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003140}
3141
3142/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003143 * Verify the requested CN - only call this if cn is not NULL!
3144 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003145static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3146 const char *cn,
3147 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003148{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003149 const mbedtls_x509_name *name;
3150 const mbedtls_x509_sequence *cur;
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
3154 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
3155 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003156 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003157 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003158 }
3159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003161 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 }
3163 } else {
3164 for (name = &crt->subject; name != NULL; name = name->next) {
3165 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3166 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
3167 break;
3168 }
3169 }
3170
3171 if (name == NULL) {
3172 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3173 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003174 }
3175}
3176
3177/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003178 * Merge the flags for all certs in the chain, after calling callback
3179 */
3180static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 uint32_t *flags,
3182 const mbedtls_x509_crt_verify_chain *ver_chain,
3183 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3184 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003185{
Janos Follath865b3eb2019-12-16 11:46:15 +00003186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003187 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003188 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003189 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003190
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003192 cur = &ver_chain->items[i-1];
3193 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003194
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 if (NULL != f_vrfy) {
3196 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3197 return ret;
3198 }
3199 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003200
3201 *flags |= cur_flags;
3202 }
3203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003205}
3206
3207/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003208 * Verify the certificate validity, with profile, restartable version
3209 *
3210 * This function:
3211 * - checks the requested CN (if any)
3212 * - checks the type and size of the EE cert's key,
3213 * as that isn't done as part of chain building/verification currently
3214 * - builds and verifies the chain
3215 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003216 *
3217 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3218 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3219 * verification routine to search for trusted signers, and CRLs will
3220 * be disabled. Otherwise, `trust_ca` will be used as the static list
3221 * of trusted signers, and `ca_crl` will be use as the static list
3222 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003224static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3225 mbedtls_x509_crt *trust_ca,
3226 mbedtls_x509_crl *ca_crl,
3227 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3228 void *p_ca_cb,
3229 const mbedtls_x509_crt_profile *profile,
3230 const char *cn, uint32_t *flags,
3231 int (*f_vrfy)(void *,
3232 mbedtls_x509_crt *,
3233 int,
3234 uint32_t *),
3235 void *p_vrfy,
3236 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003237{
Janos Follath865b3eb2019-12-16 11:46:15 +00003238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003239 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003240 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003241 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003242
3243 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003244 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003248 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3249 goto exit;
3250 }
3251
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003252 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 if (cn != NULL) {
3254 x509_crt_verify_name(crt, cn, &ee_flags);
3255 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003256
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003257 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003261 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003263
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003265 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003267
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003268 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3270 f_ca_cb, p_ca_cb, profile,
3271 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003272
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003274 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003276
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003277 /* Merge end-entity flags */
3278 ver_chain.items[0].flags |= ee_flags;
3279
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003280 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003282
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003283exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003284
3285#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3287 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003288 ver_chain.trust_ca_cb_result = NULL;
3289#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3290
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003291#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3293 mbedtls_x509_crt_restart_free(rs_ctx);
3294 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003295#endif
3296
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003297 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3298 * the SSL module for authmode optional, but non-zero return from the
3299 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003301 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003302 }
3303
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 if (ret != 0) {
3305 *flags = (uint32_t) -1;
3306 return ret;
3307 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003308
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 if (*flags != 0) {
3310 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3311 }
3312
3313 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003314}
3315
Hanno Becker3116fb32019-03-28 13:34:42 +00003316
3317/*
3318 * Verify the certificate validity (default profile, not restartable)
3319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003320int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3321 mbedtls_x509_crt *trust_ca,
3322 mbedtls_x509_crl *ca_crl,
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 &mbedtls_x509_crt_profile_default,
3330 cn, flags,
3331 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003332}
3333
3334/*
3335 * Verify the certificate validity (user-chosen profile, not restartable)
3336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003337int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3338 mbedtls_x509_crt *trust_ca,
3339 mbedtls_x509_crl *ca_crl,
3340 const mbedtls_x509_crt_profile *profile,
3341 const char *cn, uint32_t *flags,
3342 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3343 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003344{
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3346 NULL, NULL,
3347 profile, cn, flags,
3348 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003349}
3350
3351#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3352/*
3353 * Verify the certificate validity (user-chosen profile, CA callback,
3354 * not restartable).
3355 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003356int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3357 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3358 void *p_ca_cb,
3359 const mbedtls_x509_crt_profile *profile,
3360 const char *cn, uint32_t *flags,
3361 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3362 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003363{
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3365 f_ca_cb, p_ca_cb,
3366 profile, cn, flags,
3367 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003368}
3369#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3372 mbedtls_x509_crt *trust_ca,
3373 mbedtls_x509_crl *ca_crl,
3374 const mbedtls_x509_crt_profile *profile,
3375 const char *cn, uint32_t *flags,
3376 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3377 void *p_vrfy,
3378 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003379{
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3381 NULL, NULL,
3382 profile, cn, flags,
3383 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003384}
3385
3386
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003387/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003388 * Initialize a certificate chain
3389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003390void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003391{
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003393}
3394
3395/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003396 * Unallocate all certificate data
3397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003398void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 mbedtls_x509_crt *cert_cur = crt;
3401 mbedtls_x509_crt *cert_prv;
Przemek Stekiel62d8f842023-01-03 09:37:47 +01003402 mbedtls_x509_sequence *seq_cur;
3403 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 while (cert_cur != NULL) {
3406 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003410#endif
3411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3413 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3414 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3415 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3416 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003417
toth92g8d435a02021-05-10 15:16:33 +02003418 seq_cur = cert_cur->authority_key_id.authorityCertIssuer.next;
3419 while (seq_cur != NULL) {
3420 seq_prv = seq_cur;
3421 seq_cur = seq_cur->next;
3422 mbedtls_platform_zeroize(seq_prv,
3423 sizeof(mbedtls_x509_sequence));
3424 mbedtls_free(seq_prv);
toth92ga41954d2021-02-12 16:11:17 +01003425 }
3426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3428 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3429 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003430 }
3431
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003432 cert_prv = cert_cur;
3433 cert_cur = cert_cur->next;
3434
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3436 if (cert_prv != crt) {
3437 mbedtls_free(cert_prv);
3438 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003439 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003440}
3441
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003442#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3443/*
3444 * Initialize a restart context
3445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003446void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003447{
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003449
3450 ctx->parent = NULL;
3451 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003452 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003453
3454 ctx->parent_is_trusted = -1;
3455
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003456 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003457 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003459}
3460
3461/*
3462 * Free the components of a restart context
3463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003464void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003465{
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003467 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003468 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003469
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 mbedtls_pk_restart_free(&ctx->pk);
3471 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003472}
3473#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475#endif /* MBEDTLS_X509_CRT_PARSE_C */