blob: 7389a9cc628dbb2aa76bfdcd1f5a7bdba1eb44bc [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel40afdd22022-09-06 13:08:28 +020051#include "hash_info.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010057#endif
58
Daniel Axtensf0710242020-05-28 11:43:41 +100059#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <windows.h>
62#else
63#include <time.h>
64#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100065#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020069#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#include <sys/types.h>
71#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020072#if defined(__MBED__)
73#include <platform/mbed_retarget.h>
74#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020076#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060077#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020081/*
82 * Item in a verification chain: cert and flags for it
83 */
84typedef struct {
85 mbedtls_x509_crt *crt;
86 uint32_t flags;
87} x509_crt_verify_chain_item;
88
89/*
90 * Max size of verification chain: end-entity + intermediates + trusted root
91 */
Gilles Peskine449bd832023-01-11 14:50:10 +010092#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020093
Gilles Peskineffb92da2021-06-02 00:03:26 +020094/* Default profile. Do not remove items unless there are serious security
95 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020096const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
97{
Gilles Peskineae270bf2021-06-02 00:05:29 +020098 /* Hashes from SHA-256 and above. Note that this selection
99 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
101 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
102 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200103 0xFFFFFFF, /* Any PK alg */
104#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200105 /* Curves at or above 128-bit security level. Note that this selection
106 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
108 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
109 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
110 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
111 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
112 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200113 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200114#else
115 0,
116#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200117 2048,
118};
119
Gilles Peskineffb92da2021-06-02 00:03:26 +0200120/* Next-generation profile. Currently identical to the default, but may
121 * be tightened at any time. */
122const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200123{
124 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200128 0xFFFFFFF, /* Any PK alg */
129#if defined(MBEDTLS_ECP_C)
130 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
132 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
133 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
134 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
135 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
136 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200138#else
139 0,
140#endif
141 2048,
142};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200143
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200144/*
145 * NSA Suite B Profile
146 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
148{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
151 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200152 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
154 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155#if defined(MBEDTLS_ECP_C)
156 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
158 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200159#else
160 0,
161#endif
162 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163};
164
165/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200166 * Empty / all-forbidden profile
167 */
168const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
169{
170 0,
171 0,
172 0,
173 (uint32_t) -1,
174};
175
176/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200177 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200178 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
181 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182{
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (md_alg == MBEDTLS_MD_NONE) {
184 return -1;
185 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
188 return 0;
189 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200192}
193
194/*
195 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200196 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
199 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200{
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (pk_alg == MBEDTLS_PK_NONE) {
202 return -1;
203 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
206 return 0;
207 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210}
211
212/*
213 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200214 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
217 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218{
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200220
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
223 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
224 return 0;
225 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200228 }
229#endif
230
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200231#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200233 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 pk_alg == MBEDTLS_PK_ECKEY_DH) {
235 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (gid == MBEDTLS_ECP_DP_NONE) {
238 return -1;
239 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
242 return 0;
243 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200246 }
247#endif
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200250}
251
252/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000253 * Like memcmp, but case-insensitive and always returns -1 if different
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000256{
257 size_t i;
258 unsigned char diff;
259 const unsigned char *n1 = s1, *n2 = s2;
260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000262 diff = n1[i] ^ n2[i];
263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000265 continue;
266 }
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (diff == 32 &&
269 ((n1[i] >= 'a' && n1[i] <= 'z') ||
270 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
271 continue;
272 }
273
274 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000275 }
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000278}
279
280/*
281 * Return 0 if name matches wildcard, -1 otherwise
282 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000284{
285 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000287
288 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
290 return -1;
291 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 for (i = 0; i < cn_len; ++i) {
294 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000295 cn_idx = i;
296 break;
297 }
298 }
299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (cn_idx == 0) {
301 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000302 }
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (cn_len - cn_idx == name->len - 1 &&
305 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
306 return 0;
307 }
308
309 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000310}
311
312/*
313 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
314 * variations (but not all).
315 *
316 * Return 0 if equal, -1 otherwise.
317 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000319{
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000321 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 memcmp(a->p, b->p, b->len) == 0) {
323 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000324 }
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
327 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000328 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 x509_memcasecmp(a->p, b->p, b->len) == 0) {
330 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000331 }
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000334}
335
336/*
337 * Compare two X.509 Names (aka rdnSequence).
338 *
339 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
340 * we sometimes return unequal when the full algorithm would return equal,
341 * but never the other way. (In particular, we don't do Unicode normalisation
342 * or space folding.)
343 *
344 * Return 0 if equal, -1 otherwise.
345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000347{
348 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 while (a != NULL || b != NULL) {
350 if (a == NULL || b == NULL) {
351 return -1;
352 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000353
354 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000356 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
358 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000359 }
360
361 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (x509_string_cmp(&a->val, &b->val) != 0) {
363 return -1;
364 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000365
366 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (a->next_merged != b->next_merged) {
368 return -1;
369 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000370
371 a = a->next;
372 b = b->next;
373 }
374
375 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000377}
378
379/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200380 * Reset (init or clear) a verify_chain
381 */
382static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200384{
385 size_t i;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200388 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000389 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200390 }
391
392 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000393
394#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
395 ver_chain->trust_ca_cb_result = NULL;
396#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200397}
398
399/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
401 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402static int x509_get_version(unsigned char **p,
403 const unsigned char *end,
404 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405{
Janos Follath865b3eb2019-12-16 11:46:15 +0000406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 size_t len;
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
410 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
411 0)) != 0) {
412 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 }
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 }
419
420 end = *p + len;
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
423 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
424 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (*p != end) {
427 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
428 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
429 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432}
433
434/*
435 * Validity ::= SEQUENCE {
436 * notBefore Time,
437 * notAfter Time }
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439static int x509_get_dates(unsigned char **p,
440 const unsigned char *end,
441 mbedtls_x509_time *from,
442 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443{
Janos Follath865b3eb2019-12-16 11:46:15 +0000444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 size_t len;
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
448 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
449 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
450 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 end = *p + len;
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
455 return ret;
456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
459 return ret;
460 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (*p != end) {
463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
464 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468}
469
470/*
471 * X.509 v2/v3 unique identifier (not parsed)
472 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100473static int x509_get_uid(unsigned char **p,
474 const unsigned char *end,
475 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476{
Janos Follath865b3eb2019-12-16 11:46:15 +0000477 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (*p == end) {
480 return 0;
481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
483 uid->tag = **p;
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
486 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
487 n)) != 0) {
488 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
489 return 0;
490 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 }
494
495 uid->p = *p;
496 *p += uid->len;
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499}
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501static int x509_get_basic_constraints(unsigned char **p,
502 const unsigned char *end,
503 int *ca_istrue,
504 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505{
Janos Follath865b3eb2019-12-16 11:46:15 +0000506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 size_t len;
508
509 /*
510 * BasicConstraints ::= SEQUENCE {
511 * cA BOOLEAN DEFAULT FALSE,
512 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
513 */
514 *ca_istrue = 0; /* DEFAULT FALSE */
515 *max_pathlen = 0; /* endless */
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
518 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
519 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (*p == end) {
523 return 0;
524 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
527 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
528 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
529 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (ret != 0) {
532 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
533 }
534
535 if (*ca_istrue != 0) {
536 *ca_istrue = 1;
537 }
538 }
539
540 if (*p == end) {
541 return 0;
542 }
543
544 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
545 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
546 }
547
548 if (*p != end) {
549 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
550 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
551 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
Andrzej Kurek16050742020-04-14 09:49:52 -0400553 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
554 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (*max_pathlen == INT_MAX) {
556 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
557 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
558 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400559
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 (*max_pathlen)++;
561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563}
564
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565/*
566 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
567 *
568 * KeyPurposeId ::= OBJECT IDENTIFIER
569 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100570static int x509_get_ext_key_usage(unsigned char **p,
571 const unsigned char *end,
572 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573{
Janos Follath865b3eb2019-12-16 11:46:15 +0000574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
577 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
578 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
580 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (ext_key_usage->buf.p == NULL) {
582 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
583 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
584 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587}
588
589/*
toth92ga41954d2021-02-12 16:11:17 +0100590 * SubjectKeyIdentifier ::= KeyIdentifier
591 *
592 * KeyIdentifier ::= OCTET STRING
593 */
594static int x509_get_subject_key_id(unsigned char **p,
595 const unsigned char *end,
596 mbedtls_x509_buf *subject_key_id)
597{
598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
599 size_t len = 0u;
600
601 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
602 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
603 return ret;
604 } else {
605 subject_key_id->len = len;
606 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
607 subject_key_id->p = *p;
608 *p += len;
609 }
610
toth92gd96027a2021-04-27 15:41:25 +0200611 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100612 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
613 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200614 }
615
toth92ga41954d2021-02-12 16:11:17 +0100616 return 0;
617}
618
619/*
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100620 * SubjectAltName ::= GeneralNames
toth92ga41954d2021-02-12 16:11:17 +0100621 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
622 *
623 * GeneralName ::= CHOICE {
624 * otherName [0] OtherName,
625 * rfc822Name [1] IA5String,
626 * dNSName [2] IA5String,
627 * x400Address [3] ORAddress,
628 * directoryName [4] Name,
629 * ediPartyName [5] EDIPartyName,
630 * uniformResourceIdentifier [6] IA5String,
631 * iPAddress [7] OCTET STRING,
632 * registeredID [8] OBJECT IDENTIFIER }
633 *
634 * OtherName ::= SEQUENCE {
635 * type-id OBJECT IDENTIFIER,
636 * value [0] EXPLICIT ANY DEFINED BY type-id }
637 *
638 * EDIPartyName ::= SEQUENCE {
639 * nameAssigner [0] DirectoryString OPTIONAL,
640 * partyName [1] DirectoryString }
641 *
642 * NOTE: we list all types, but only use dNSName and otherName
643 * of type HwModuleName, as defined in RFC 4108, at this point.
644 */
toth92g8d435a02021-05-10 15:16:33 +0200645static int x509_get_general_names(unsigned char **p,
646 const unsigned char *end,
647 mbedtls_x509_sequence *subject_alt_name)
toth92ga41954d2021-02-12 16:11:17 +0100648{
649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
650 size_t len, tag_len;
651 mbedtls_asn1_buf *buf;
652 unsigned char tag;
653 mbedtls_asn1_sequence *cur = subject_alt_name;
654
655 /* Get main sequence tag */
656 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
657 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
658 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
659 }
660
661 if (*p + len != end) {
662 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
663 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
664 }
665
666 while (*p < end) {
667 mbedtls_x509_subject_alternative_name dummy_san_buf;
668 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
669
670 tag = **p;
671 (*p)++;
672 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
673 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
674 }
675
toth92g8d435a02021-05-10 15:16:33 +0200676 /* Tag shall be CONTEXT_SPECIFIC or SET */
toth92ga41954d2021-02-12 16:11:17 +0100677 if ((tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
678 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
toth92g8d435a02021-05-10 15:16:33 +0200679 if ((tag & (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
680 (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100681 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
682 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
toth92g8d435a02021-05-10 15:16:33 +0200683 }
toth92ga41954d2021-02-12 16:11:17 +0100684 }
685
686 /*
687 * Check that the SAN is structured correctly.
688 */
Przemek Stekiel9a511c52023-01-03 10:23:13 +0100689 ret = mbedtls_x509_parse_subject_alt_name(&(cur->buf), &dummy_san_buf);
toth92ga41954d2021-02-12 16:11:17 +0100690 /*
691 * In case the extension is malformed, return an error,
692 * and clear the allocated sequences.
693 */
694 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
695 mbedtls_asn1_sequence_free(subject_alt_name->next);
696 subject_alt_name->next = NULL;
697 return ret;
698 }
699
700 /* Allocate and assign next pointer */
701 if (cur->buf.p != NULL) {
702 if (cur->next != NULL) {
703 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
704 }
705
706 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
707
708 if (cur->next == NULL) {
709 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
710 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
711 }
712
713 cur = cur->next;
714 }
715
716 buf = &(cur->buf);
717 buf->tag = tag;
718 buf->p = *p;
719 buf->len = tag_len;
720 *p += buf->len;
721 }
722
723 /* Set final sequence entry's next pointer to NULL */
724 cur->next = NULL;
725
726 if (*p != end) {
727 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
728 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
729 }
730
731 return 0;
732}
733
734/*
toth92g8d435a02021-05-10 15:16:33 +0200735 * AuthorityKeyIdentifier ::= SEQUENCE {
736 * keyIdentifier [0] KeyIdentifier OPTIONAL,
737 * authorityCertIssuer [1] GeneralNames OPTIONAL,
738 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
739 *
740 * KeyIdentifier ::= OCTET STRING
741 */
742static int x509_get_authority_key_id(unsigned char **p,
743 unsigned char *end,
744 mbedtls_x509_authority *authority_key_id)
745{
746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
747 size_t len = 0u;
748
749 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100750 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
toth92g8d435a02021-05-10 15:16:33 +0200751 return ret;
752 }
753
754 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
755 MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0) {
756 /* KeyIdentifier is an OPTIONAL field */
757 } else {
758 authority_key_id->keyIdentifier.len = len;
759 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200760 /* Setting tag of the keyIdentfier intentionally to 0x04.
761 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
762 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200763 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
764
765 *p += len;
766 }
767
768 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200769 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200770 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
771 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100772 MBEDTLS_X509_SAN_RFC822_NAME)) != 0) {
toth92g8d435a02021-05-10 15:16:33 +0200773 /* authorityCertIssuer is an OPTIONAL field */
774 } else {
toth92g9232e0a2021-05-11 12:55:58 +0200775 /* Getting directoryName using the required specific class tag [4] */
toth92g8d435a02021-05-10 15:16:33 +0200776 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
777 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100778 MBEDTLS_ASN1_CONSTRUCTED |
779 MBEDTLS_X509_SAN_DIRECTORY_NAME)) != 0) {
toth92g8d435a02021-05-10 15:16:33 +0200780 return ret;
781 } else {
782 /* "end" also includes the CertSerialNumber field so "len" shall be used */
783 ret = x509_get_general_names(p,
784 (*p+len),
785 &authority_key_id->authorityCertIssuer);
786 }
787 }
788 }
789
790 if (*p < end) {
791 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
792 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_INTEGER)) !=
793 0) {
794 /* authorityCertSerialNumber is an OPTIONAL field, but if there are still data it must be the serial number */
795 return ret;
796 } else {
797 authority_key_id->authorityCertSerialNumber.len = len;
798 authority_key_id->authorityCertSerialNumber.p = *p;
799 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_OCTET_STRING;
800 *p += len;
801 }
802 }
803
804 if (*p != end) {
805 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
806 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
807 }
808
809 return 0;
810}
811
812/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200813 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
814 *
815 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
816 *
817 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
818 *
819 * PolicyInformation ::= SEQUENCE {
820 * policyIdentifier CertPolicyId,
821 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
822 * PolicyQualifierInfo OPTIONAL }
823 *
824 * CertPolicyId ::= OBJECT IDENTIFIER
825 *
826 * PolicyQualifierInfo ::= SEQUENCE {
827 * policyQualifierId PolicyQualifierId,
828 * qualifier ANY DEFINED BY policyQualifierId }
829 *
830 * -- policyQualifierIds for Internet policy qualifiers
831 *
832 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
833 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
834 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
835 *
836 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
837 *
838 * Qualifier ::= CHOICE {
839 * cPSuri CPSuri,
840 * userNotice UserNotice }
841 *
842 * CPSuri ::= IA5String
843 *
844 * UserNotice ::= SEQUENCE {
845 * noticeRef NoticeReference OPTIONAL,
846 * explicitText DisplayText OPTIONAL }
847 *
848 * NoticeReference ::= SEQUENCE {
849 * organization DisplayText,
850 * noticeNumbers SEQUENCE OF INTEGER }
851 *
852 * DisplayText ::= CHOICE {
853 * ia5String IA5String (SIZE (1..200)),
854 * visibleString VisibleString (SIZE (1..200)),
855 * bmpString BMPString (SIZE (1..200)),
856 * utf8String UTF8String (SIZE (1..200)) }
857 *
858 * NOTE: we only parse and use anyPolicy without qualifiers at this point
859 * as defined in RFC 5280.
860 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861static int x509_get_certificate_policies(unsigned char **p,
862 const unsigned char *end,
863 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200864{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300865 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200866 size_t len;
867 mbedtls_asn1_buf *buf;
868 mbedtls_asn1_sequence *cur = certificate_policies;
869
870 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 ret = mbedtls_asn1_get_tag(p, end, &len,
872 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
873 if (ret != 0) {
874 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
875 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if (*p + len != end) {
878 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
879 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
880 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200881
882 /*
883 * Cannot be an empty sequence.
884 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (len == 0) {
886 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
887 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
888 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200891 mbedtls_x509_buf policy_oid;
892 const unsigned char *policy_end;
893
894 /*
895 * Get the policy sequence
896 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
898 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
899 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
900 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200901
902 policy_end = *p + len;
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
905 MBEDTLS_ASN1_OID)) != 0) {
906 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
907 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200908
909 policy_oid.tag = MBEDTLS_ASN1_OID;
910 policy_oid.len = len;
911 policy_oid.p = *p;
912
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300913 /*
914 * Only AnyPolicy is currently supported when enforcing policy.
915 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300917 /*
918 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200919 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300920 */
921 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
922 }
923
Ron Eldor74d9acc2019-03-21 14:00:03 +0200924 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if (cur->buf.p != NULL) {
926 if (cur->next != NULL) {
927 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
928 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (cur->next == NULL) {
933 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
934 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
935 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200936
937 cur = cur->next;
938 }
939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200941 buf->tag = policy_oid.tag;
942 buf->p = policy_oid.p;
943 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300944
945 *p += len;
946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 /*
948 * If there is an optional qualifier, then *p < policy_end
949 * Check the Qualifier len to verify it doesn't exceed policy_end.
950 */
951 if (*p < policy_end) {
952 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
953 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
954 0) {
955 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
956 }
Ron Eldor08063792019-05-13 16:38:39 +0300957 /*
958 * Skip the optional policy qualifiers.
959 */
960 *p += len;
961 }
962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 if (*p != policy_end) {
964 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
965 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
966 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200967 }
968
969 /* Set final sequence entry's next pointer to NULL */
970 cur->next = NULL;
971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (*p != end) {
973 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
974 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
975 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200978}
979
980/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 * X.509 v3 extensions
982 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984static int x509_get_crt_ext(unsigned char **p,
985 const unsigned char *end,
986 mbedtls_x509_crt *crt,
987 mbedtls_x509_crt_ext_cb_t cb,
988 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989{
Janos Follath865b3eb2019-12-16 11:46:15 +0000990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200992 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (*p == end) {
995 return 0;
996 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
999 return ret;
1000 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
Hanno Becker12f62fb2019-02-12 17:22:36 +00001002 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 /*
1005 * Extension ::= SEQUENCE {
1006 * extnID OBJECT IDENTIFIER,
1007 * critical BOOLEAN DEFAULT FALSE,
1008 * extnValue OCTET STRING }
1009 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 int is_critical = 0; /* DEFAULT FALSE */
1012 int ext_type = 0;
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
1015 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1016 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1017 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018
1019 end_ext_data = *p + len;
1020
1021 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
1023 MBEDTLS_ASN1_OID)) != 0) {
1024 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1025 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026
k-stachowiak463928a2018-07-24 12:50:59 +02001027 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 extn_oid.p = *p;
1029 *p += extn_oid.len;
1030
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
1033 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
1034 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1035 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036
1037 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
1039 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1040 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1041 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001043 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 end_ext_octet = *p + len;
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (end_ext_octet != end_ext_data) {
1047 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1048 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1049 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050
1051 /*
1052 * Detect supported extensions
1053 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001057 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (cb != NULL) {
1059 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
1060 if (ret != 0 && is_critical) {
1061 return ret;
1062 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001063 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001064 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001065 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001066
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067 /* No parser found, skip extension */
1068 *p = end_ext_octet;
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1073 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 continue;
1076 }
1077
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001078 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if ((crt->ext_types & ext_type) != 0) {
1080 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1081 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001082
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083 crt->ext_types |= ext_type;
1084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 switch (ext_type) {
1086 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
1087 /* Parse basic constraints */
1088 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
1089 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
1090 return ret;
1091 }
1092 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 case MBEDTLS_X509_EXT_KEY_USAGE:
1095 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001096 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
1097 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return ret;
1099 }
1100 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1103 /* Parse extended key usage */
1104 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1105 &crt->ext_key_usage)) != 0) {
1106 return ret;
1107 }
1108 break;
toth92g8d435a02021-05-10 15:16:33 +02001109
toth92ga41954d2021-02-12 16:11:17 +01001110 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1111 /* Parse subject key identifier */
1112 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1113 &crt->subject_key_id)) != 0) {
1114 return ret;
1115 }
1116 break;
toth92g8d435a02021-05-10 15:16:33 +02001117
toth92ga41954d2021-02-12 16:11:17 +01001118 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1119 /* Parse authority key identifier */
1120 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1121 &crt->authority_key_id)) != 0) {
1122 return ret;
1123 }
1124 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001126 /* Parse subject alt name
1127 * SubjectAltName ::= GeneralNames
1128 */
1129 if ((ret = x509_get_general_names(p, end_ext_octet,
1130 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return ret;
1132 }
1133 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1136 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001137 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1138 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return ret;
1140 }
1141 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1144 /* Parse certificate policies type */
1145 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1146 &crt->certificate_policies)) != 0) {
1147 /* Give the callback (if any) a chance to handle the extension
1148 * if it contains unsupported policies */
1149 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1150 cb(p_ctx, crt, &extn_oid, is_critical,
1151 start_ext_octet, end_ext_octet) == 0) {
1152 break;
1153 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (is_critical) {
1156 return ret;
1157 } else
1158 /*
1159 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1160 * cannot interpret or enforce the policy. However, it is up to
1161 * the user to choose how to enforce the policies,
1162 * unless the extension is critical.
1163 */
1164 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1165 return ret;
1166 }
1167 }
1168 break;
1169
1170 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001171 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 * If this is a non-critical extension, which the oid layer
1173 * supports, but there isn't an x509 parser for it,
1174 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001175 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (is_critical) {
1177 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1178 } else {
1179 *p = end_ext_octet;
1180 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 }
1182 }
1183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (*p != end) {
1185 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1186 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1187 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190}
1191
1192/*
1193 * Parse and fill a single X.509 certificate in DER format
1194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1196 const unsigned char *buf,
1197 size_t buflen,
1198 int make_copy,
1199 mbedtls_x509_crt_ext_cb_t cb,
1200 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201{
Janos Follath865b3eb2019-12-16 11:46:15 +00001202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 size_t len;
1204 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1208 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1209 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210
1211 /*
1212 * Check for valid input
1213 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (crt == NULL || buf == NULL) {
1215 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1216 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001218 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001220 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221 end = p + len;
1222
1223 /*
1224 * Certificate ::= SEQUENCE {
1225 * tbsCertificate TBSCertificate,
1226 * signatureAlgorithm AlgorithmIdentifier,
1227 * signatureValue BIT STRING }
1228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1230 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1231 mbedtls_x509_crt_free(crt);
1232 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 }
1234
Janos Follathcc0e49d2016-02-17 14:34:12 +00001235 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001236 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001238 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1240 if (crt->raw.p == NULL) {
1241 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1242 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001245 crt->own_buffer = 1;
1246
1247 p += crt->raw.len - len;
1248 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 } else {
1250 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001251 crt->own_buffer = 0;
1252 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001253
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254 /*
1255 * TBSCertificate ::= SEQUENCE {
1256 */
1257 crt->tbs.p = p;
1258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1260 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1261 mbedtls_x509_crt_free(crt);
1262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263 }
1264
1265 end = p + len;
1266 crt->tbs.len = end - crt->tbs.p;
1267
1268 /*
1269 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1270 *
1271 * CertificateSerialNumber ::= INTEGER
1272 *
1273 * signature AlgorithmIdentifier
1274 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1276 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1277 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1278 &sig_params1)) != 0) {
1279 mbedtls_x509_crt_free(crt);
1280 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 }
1282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (crt->version < 0 || crt->version > 2) {
1284 mbedtls_x509_crt_free(crt);
1285 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 }
1287
Andres AG7ca4a032017-03-09 16:16:11 +00001288 crt->version++;
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1291 &crt->sig_md, &crt->sig_pk,
1292 &crt->sig_opts)) != 0) {
1293 mbedtls_x509_crt_free(crt);
1294 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 }
1296
1297 /*
1298 * issuer Name
1299 */
1300 crt->issuer_raw.p = p;
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1303 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1304 mbedtls_x509_crt_free(crt);
1305 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 }
1307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1309 mbedtls_x509_crt_free(crt);
1310 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 }
1312
1313 crt->issuer_raw.len = p - crt->issuer_raw.p;
1314
1315 /*
1316 * Validity ::= SEQUENCE {
1317 * notBefore Time,
1318 * notAfter Time }
1319 *
1320 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1322 &crt->valid_to)) != 0) {
1323 mbedtls_x509_crt_free(crt);
1324 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325 }
1326
1327 /*
1328 * subject Name
1329 */
1330 crt->subject_raw.p = p;
1331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1333 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1334 mbedtls_x509_crt_free(crt);
1335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336 }
1337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1339 mbedtls_x509_crt_free(crt);
1340 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341 }
1342
1343 crt->subject_raw.len = p - crt->subject_raw.p;
1344
1345 /*
1346 * SubjectPublicKeyInfo
1347 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001348 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1350 mbedtls_x509_crt_free(crt);
1351 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001353 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354
1355 /*
1356 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1357 * -- If present, version shall be v2 or v3
1358 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1359 * -- If present, version shall be v2 or v3
1360 * extensions [3] EXPLICIT Extensions OPTIONAL
1361 * -- If present, version shall be v3
1362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (crt->version == 2 || crt->version == 3) {
1364 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1365 if (ret != 0) {
1366 mbedtls_x509_crt_free(crt);
1367 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368 }
1369 }
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if (crt->version == 2 || crt->version == 3) {
1372 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1373 if (ret != 0) {
1374 mbedtls_x509_crt_free(crt);
1375 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 }
1377 }
1378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 if (crt->version == 3) {
1380 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1381 if (ret != 0) {
1382 mbedtls_x509_crt_free(crt);
1383 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384 }
1385 }
1386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 if (p != end) {
1388 mbedtls_x509_crt_free(crt);
1389 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1390 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391 }
1392
1393 end = crt_end;
1394
1395 /*
1396 * }
1397 * -- end of TBSCertificate
1398 *
1399 * signatureAlgorithm AlgorithmIdentifier,
1400 * signatureValue BIT STRING
1401 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1403 mbedtls_x509_crt_free(crt);
1404 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405 }
1406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (crt->sig_oid.len != sig_oid2.len ||
1408 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001409 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001410 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 (sig_params1.len != 0 &&
1412 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1413 mbedtls_x509_crt_free(crt);
1414 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415 }
1416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1418 mbedtls_x509_crt_free(crt);
1419 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420 }
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if (p != end) {
1423 mbedtls_x509_crt_free(crt);
1424 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1425 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426 }
1427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429}
1430
1431/*
1432 * Parse one X.509 certificate in DER format from a buffer and add them to a
1433 * chained list
1434 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1436 const unsigned char *buf,
1437 size_t buflen,
1438 int make_copy,
1439 mbedtls_x509_crt_ext_cb_t cb,
1440 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441{
Janos Follath865b3eb2019-12-16 11:46:15 +00001442 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444
1445 /*
1446 * Check for valid input
1447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 if (crt == NULL || buf == NULL) {
1449 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1450 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 prev = crt;
1454 crt = crt->next;
1455 }
1456
1457 /*
1458 * Add new certificate on the end of the chain if needed.
1459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 if (crt->version != 0 && crt->next == NULL) {
1461 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (crt->next == NULL) {
1464 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466
1467 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470 }
1471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1473 if (ret != 0) {
1474 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if (crt != chain) {
1479 mbedtls_free(crt);
1480 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483 }
1484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486}
1487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1489 const unsigned char *buf,
1490 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001491{
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001493}
1494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1496 const unsigned char *buf,
1497 size_t buflen,
1498 int make_copy,
1499 mbedtls_x509_crt_ext_cb_t cb,
1500 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001501{
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001503}
1504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1506 const unsigned char *buf,
1507 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001508{
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001510}
1511
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001513 * Parse one or more PEM certificates from a buffer and add them to the chained
1514 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001516int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1517 const unsigned char *buf,
1518 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519{
Janos Follath98e28a72016-05-31 14:03:54 +01001520#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001521 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001523#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524
1525 /*
1526 * Check for valid input
1527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 if (chain == NULL || buf == NULL) {
1529 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1530 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531
1532 /*
1533 * Determine buffer content. Buffer contains either one DER certificate or
1534 * one or more PEM certificates.
1535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1538 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001540 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1543 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1544 }
Janos Follath98e28a72016-05-31 14:03:54 +01001545#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001547#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001554 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001559 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 ret = mbedtls_pem_read_buffer(&pem,
1561 "-----BEGIN CERTIFICATE-----",
1562 "-----END CERTIFICATE-----",
1563 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566 /*
1567 * Was PEM encoded
1568 */
1569 buflen -= use_len;
1570 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1572 return ret;
1573 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1574 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575
1576 /*
1577 * PEM header and footer were found
1578 */
1579 buflen -= use_len;
1580 buf += use_len;
1581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585
Paul Bakker5a5fa922014-09-26 14:53:04 +02001586 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597 /*
1598 * Quit parsing on a memory error
1599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1601 return ret;
1602 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607
1608 total_failed++;
1609 continue;
1610 }
1611
1612 success = 1;
1613 }
1614 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (success) {
1617 return total_failed;
1618 } else if (first_error) {
1619 return first_error;
1620 } else {
1621 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1622 }
Janos Follath98e28a72016-05-31 14:03:54 +01001623#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624}
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001627/*
1628 * Load one or more certificates and add them to the chained list
1629 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001630int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631{
Janos Follath865b3eb2019-12-16 11:46:15 +00001632 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633 size_t n;
1634 unsigned char *buf;
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1637 return ret;
1638 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 mbedtls_platform_zeroize(buf, n);
1643 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646}
1647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649{
1650 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001651#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652 int w_ret;
1653 WCHAR szDir[MAX_PATH];
1654 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001655 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657
Paul Bakker9af723c2014-05-01 13:03:14 +02001658 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 HANDLE hFind;
1660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (len > MAX_PATH - 3) {
1662 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1663 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 memset(szDir, 0, sizeof(szDir));
1666 memset(filename, 0, MAX_PATH);
1667 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001668 filename[len++] = '\\';
1669 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 filename[len++] = '*';
1671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1673 MAX_PATH - 3);
1674 if (w_ret == 0) {
1675 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1676 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 hFind = FindFirstFileW(szDir, &file_data);
1679 if (hFind == INVALID_HANDLE_VALUE) {
1680 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1681 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682
1683 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 do {
1685 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001692 -1,
1693 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 NULL, NULL);
1695 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001696 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1697 goto cleanup;
1698 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1701 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 }
1706 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711
Ron Eldor36d90422017-01-09 15:09:16 +02001712cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001713 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001714#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001715 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001716 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001717 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001718 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001719 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 if (dir == NULL) {
1723 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1724 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001725
Ron Eldor63140682017-01-09 19:27:59 +02001726#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1728 closedir(dir);
1729 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001730 }
Ron Eldor63140682017-01-09 19:27:59 +02001731#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001736 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001739 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001740 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1741 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 } else if (stat(entry_name, &sb) == -1) {
1743 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001744 /* Broken symbolic link - ignore this entry.
1745 stat(2) will return this error for either (a) a dangling
1746 symlink or (b) a missing file.
1747 Given that we have just obtained the filename from readdir,
1748 assume that it does exist and therefore treat this as a
1749 dangling symlink. */
1750 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001752 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001753 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1754 goto cleanup;
1755 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756 }
1757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761
1762 // Ignore parse errors
1763 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1765 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001766 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001771
1772cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001774
Ron Eldor63140682017-01-09 19:27:59 +02001775#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 }
Ron Eldor63140682017-01-09 19:27:59 +02001779#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001780
Paul Bakkerbe089b02013-10-14 15:51:50 +02001781#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786
Przemek Stekiela2939e82023-01-03 13:35:54 +01001787/*
1788 * OtherName ::= SEQUENCE {
1789 * type-id OBJECT IDENTIFIER,
1790 * value [0] EXPLICIT ANY DEFINED BY type-id }
1791 *
1792 * HardwareModuleName ::= SEQUENCE {
1793 * hwType OBJECT IDENTIFIER,
1794 * hwSerialNum OCTET STRING }
1795 *
1796 * NOTE: we currently only parse and use otherName of type HwModuleName,
1797 * as defined in RFC 4108.
1798 */
1799static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1800 mbedtls_x509_san_other_name *other_name)
1801{
1802 int ret = 0;
1803 size_t len;
1804 unsigned char *p = subject_alt_name->p;
1805 const unsigned char *end = p + subject_alt_name->len;
1806 mbedtls_x509_buf cur_oid;
1807
1808 if ((subject_alt_name->tag &
1809 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1810 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1811 /*
1812 * The given subject alternative name is not of type "othername".
1813 */
1814 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1815 }
1816
1817 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1818 MBEDTLS_ASN1_OID)) != 0) {
1819 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1820 }
1821
1822 cur_oid.tag = MBEDTLS_ASN1_OID;
1823 cur_oid.p = p;
1824 cur_oid.len = len;
1825
1826 /*
1827 * Only HwModuleName is currently supported.
1828 */
1829 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1830 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1831 }
1832
1833 if (p + len >= end) {
1834 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1835 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1836 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1837 }
1838 p += len;
1839 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1840 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1841 0) {
1842 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1843 }
1844
1845 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1846 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1847 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1848 }
1849
1850 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1851 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1852 }
1853
1854 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1855 other_name->value.hardware_module_name.oid.p = p;
1856 other_name->value.hardware_module_name.oid.len = len;
1857
1858 if (p + len >= end) {
1859 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1860 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1861 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1862 }
1863 p += len;
1864 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1865 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1866 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1867 }
1868
1869 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1870 other_name->value.hardware_module_name.val.p = p;
1871 other_name->value.hardware_module_name.val.len = len;
1872 p += len;
1873 if (p != end) {
1874 mbedtls_platform_zeroize(other_name,
1875 sizeof(*other_name));
1876 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1877 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1878 }
1879 return 0;
1880}
1881
1882int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1883 mbedtls_x509_subject_alternative_name *san)
1884{
1885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1886 switch (san_buf->tag &
1887 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1888 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1889 /*
1890 * otherName
1891 */
1892 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1893 {
1894 mbedtls_x509_san_other_name other_name;
1895
1896 ret = x509_get_other_name(san_buf, &other_name);
1897 if (ret != 0) {
1898 return ret;
1899 }
1900
1901 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1902 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1903 memcpy(&san->san.other_name,
1904 &other_name, sizeof(other_name));
1905
1906 }
1907 break;
1908
1909 /*
1910 * dNSName
1911 */
1912 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1913 {
1914 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1915 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1916
1917 memcpy(&san->san.unstructured_name,
1918 san_buf, sizeof(*san_buf));
1919
1920 }
1921 break;
1922
1923 /*
Przemek Stekiela2939e82023-01-03 13:35:54 +01001924 * Type not supported
1925 */
1926 default:
1927 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1928 }
1929 return 0;
1930}
1931
Peter Kolbus9a969b62018-12-11 13:55:56 -06001932#if !defined(MBEDTLS_X509_REMOVE_INFO)
toth92g8d435a02021-05-10 15:16:33 +02001933static int x509_info_subject_alt_name(char **buf, size_t *size,
1934 const mbedtls_x509_sequence
1935 *subject_alt_name,
1936 const char *prefix)
1937{
1938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1939 size_t i;
1940 size_t n = *size;
1941 char *p = *buf;
1942 const mbedtls_x509_sequence *cur = subject_alt_name;
1943 mbedtls_x509_subject_alternative_name san;
1944 int parse_ret;
1945
1946 while (cur != NULL) {
1947 memset(&san, 0, sizeof(san));
Przemek Stekiel9a511c52023-01-03 10:23:13 +01001948 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
toth92g8d435a02021-05-10 15:16:33 +02001949 if (parse_ret != 0) {
1950 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1951 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1952 MBEDTLS_X509_SAFE_SNPRINTF;
1953 } else {
1954 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1955 MBEDTLS_X509_SAFE_SNPRINTF;
1956 }
1957 cur = cur->next;
1958 continue;
1959 }
1960
1961 switch (san.type) {
1962 /*
1963 * otherName
1964 */
1965 case MBEDTLS_X509_SAN_OTHER_NAME:
1966 {
1967 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1968
1969 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1970 MBEDTLS_X509_SAFE_SNPRINTF;
1971
1972 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1973 &other_name->value.hardware_module_name.oid) != 0) {
1974 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1975 MBEDTLS_X509_SAFE_SNPRINTF;
1976 ret =
1977 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1978 MBEDTLS_X509_SAFE_SNPRINTF;
1979
1980 ret = mbedtls_oid_get_numeric_string(p,
1981 n,
1982 &other_name->value.hardware_module_name.oid);
1983 MBEDTLS_X509_SAFE_SNPRINTF;
1984
1985 ret =
1986 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1987 MBEDTLS_X509_SAFE_SNPRINTF;
1988
1989 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1990 ret = mbedtls_snprintf(p,
1991 n,
1992 "%02X",
1993 other_name->value.hardware_module_name.val.p[i]);
1994 MBEDTLS_X509_SAFE_SNPRINTF;
1995 }
1996 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1997 }
1998 break;
1999
2000 /*
2001 * dNSName
2002 */
2003 case MBEDTLS_X509_SAN_DNS_NAME:
2004 {
2005 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
2006 MBEDTLS_X509_SAFE_SNPRINTF;
2007 if (san.san.unstructured_name.len >= n) {
2008 *p = '\0';
2009 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
2010 }
2011
2012 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
2013 p += san.san.unstructured_name.len;
2014 n -= san.san.unstructured_name.len;
2015 }
2016 break;
2017
2018 /*
2019 * Type not supported, skip item.
2020 */
2021 default:
2022 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
2023 MBEDTLS_X509_SAFE_SNPRINTF;
2024 break;
2025 }
2026
2027 cur = cur->next;
2028 }
2029
2030 *p = '\0';
2031
2032 *size = n;
2033 *buf = p;
2034
2035 return 0;
2036}
2037
toth92g8d435a02021-05-10 15:16:33 +02002038#define PRINT_ITEM(i) \
2039 { \
2040 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
2041 MBEDTLS_X509_SAFE_SNPRINTF; \
2042 sep = ", "; \
2043 }
2044
2045#define CERT_TYPE(type, name) \
2046 if (ns_cert_type & (type)) \
2047 PRINT_ITEM(name);
2048
2049static int x509_info_cert_type(char **buf, size_t *size,
2050 unsigned char ns_cert_type)
2051{
2052 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2053 size_t n = *size;
2054 char *p = *buf;
2055 const char *sep = "";
2056
2057 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
2058 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
2059 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
2060 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
2061 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
2062 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
2063 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
2064 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
2065
2066 *size = n;
2067 *buf = p;
2068
2069 return 0;
2070}
2071
2072#define KEY_USAGE(code, name) \
2073 if (key_usage & (code)) \
2074 PRINT_ITEM(name);
2075
2076static int x509_info_key_usage(char **buf, size_t *size,
2077 unsigned int key_usage)
2078{
2079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2080 size_t n = *size;
2081 char *p = *buf;
2082 const char *sep = "";
2083
2084 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
2085 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
2086 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
2087 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
2088 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
2089 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
2090 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
2091 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
2092 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
2093
2094 *size = n;
2095 *buf = p;
2096
2097 return 0;
2098}
2099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100static int x509_info_ext_key_usage(char **buf, size_t *size,
2101 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002102{
Janos Follath865b3eb2019-12-16 11:46:15 +00002103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002104 const char *desc;
2105 size_t n = *size;
2106 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002108 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 while (cur != NULL) {
2111 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002112 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002116 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002117
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002118 sep = ", ";
2119
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002120 cur = cur->next;
2121 }
2122
2123 *size = n;
2124 *buf = p;
2125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002127}
2128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129static int x509_info_cert_policies(char **buf, size_t *size,
2130 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02002131{
Janos Follath865b3eb2019-12-16 11:46:15 +00002132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002133 const char *desc;
2134 size_t n = *size;
2135 char *p = *buf;
2136 const mbedtls_x509_sequence *cur = certificate_policies;
2137 const char *sep = "";
2138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 while (cur != NULL) {
2140 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02002141 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002145 MBEDTLS_X509_SAFE_SNPRINTF;
2146
2147 sep = ", ";
2148
2149 cur = cur->next;
2150 }
2151
2152 *size = n;
2153 *buf = p;
2154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002156}
2157
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002158/*
2159 * Return an informational string about the certificate.
2160 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161#define BEFORE_COLON 18
2162#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01002163int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
2164 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002165{
Janos Follath865b3eb2019-12-16 11:46:15 +00002166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002167 size_t n;
2168 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002169 char key_size_str[BEFORE_COLON];
2170
2171 p = buf;
2172 n = size;
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 if (NULL == crt) {
2175 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01002176 MBEDTLS_X509_SAFE_SNPRINTF;
2177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01002179 }
2180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
2182 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002183 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 ret = mbedtls_snprintf(p, n, "%sserial number : ",
2185 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002186 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002187
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002189 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002192 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002194 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002197 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002199 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
2202 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2203 crt->valid_from.year, crt->valid_from.mon,
2204 crt->valid_from.day, crt->valid_from.hour,
2205 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002206 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
2209 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2210 crt->valid_to.year, crt->valid_to.mon,
2211 crt->valid_to.day, crt->valid_to.hour,
2212 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002213 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002216 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
2219 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002220 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002221
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002222 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
2224 mbedtls_pk_get_name(&crt->pk))) != 0) {
2225 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002226 }
2227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
2229 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002230 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002231
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002232 /*
2233 * Optional extensions
2234 */
2235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
2237 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
2238 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002239 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (crt->max_pathlen > 0) {
2242 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002243 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002244 }
2245 }
2246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2248 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002249 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002250
Przemek Stekiel21c37282023-01-16 08:47:49 +01002251 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
2252 &crt->subject_alt_names,
2253 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 return ret;
2255 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002256 }
2257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
2259 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002260 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002261
Przemek Stekiel21c37282023-01-16 08:47:49 +01002262 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 return ret;
2264 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002265 }
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2268 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002269 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002270
Przemek Stekiel21c37282023-01-16 08:47:49 +01002271 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 return ret;
2273 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002274 }
2275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2277 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002278 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002279
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 if ((ret = x509_info_ext_key_usage(&p, &n,
2281 &crt->ext_key_usage)) != 0) {
2282 return ret;
2283 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002284 }
2285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2287 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002288 MBEDTLS_X509_SAFE_SNPRINTF;
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if ((ret = x509_info_cert_policies(&p, &n,
2291 &crt->certificate_policies)) != 0) {
2292 return ret;
2293 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002294 }
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002297 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002300}
2301
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002302struct x509_crt_verify_string {
2303 int code;
2304 const char *string;
2305};
2306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002308static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002309 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002310 { 0, NULL }
2311};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002312#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
2315 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002316{
Janos Follath865b3eb2019-12-16 11:46:15 +00002317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002318 const struct x509_crt_verify_string *cur;
2319 char *p = buf;
2320 size_t n = size;
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
2323 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002324 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002328 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002329 flags ^= cur->code;
2330 }
2331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (flags != 0) {
2333 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
2334 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002335 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002336 }
2337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002339}
Hanno Becker612a2f12020-10-09 09:19:39 +01002340#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2343 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002344{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002345 unsigned int usage_must, usage_may;
2346 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
2350 return 0;
2351 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002352
2353 usage_must = usage & ~may_mask;
2354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
2356 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2357 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002358
2359 usage_may = usage & may_mask;
2360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
2362 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2363 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002366}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2369 const char *usage_oid,
2370 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002373
2374 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2376 return 0;
2377 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002378
2379 /*
2380 * Look for the requested usage (or wildcard ANY) in our list
2381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if (cur_oid->len == usage_len &&
2386 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2387 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002388 }
2389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2391 return 0;
2392 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002393 }
2394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002396}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002399/*
2400 * Return 1 if the certificate is revoked, or 0 otherwise.
2401 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002402int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002403{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002405
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 while (cur != NULL && cur->serial.len != 0) {
2407 if (crt->serial.len == cur->serial.len &&
2408 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2409 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410 }
2411
2412 cur = cur->next;
2413 }
2414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002416}
2417
2418/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002419 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002420 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002421 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2423 mbedtls_x509_crl *crl_list,
2424 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002425{
2426 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002427 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002428#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002429 psa_algorithm_t psa_algorithm;
2430#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002432#endif /* MBEDTLS_USE_PSA_CRYPTO */
2433 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (ca == NULL) {
2436 return flags;
2437 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 while (crl_list != NULL) {
2440 if (crl_list->version == 0 ||
2441 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002442 crl_list = crl_list->next;
2443 continue;
2444 }
2445
2446 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002447 * Check if the CA is configured to sign CRLs
2448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (mbedtls_x509_crt_check_key_usage(ca,
2450 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002452 break;
2453 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002454
2455 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002456 * Check if CRL is correctly signed by the trusted CA
2457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002459 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002463 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002465
pespacek7599a772022-02-07 14:40:55 +01002466#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
2468 if (psa_hash_compute(psa_algorithm,
2469 crl_list->tbs.p,
2470 crl_list->tbs.len,
2471 hash,
2472 sizeof(hash),
2473 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002474 /* Note: this can't happen except after an internal error */
2475 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2476 break;
2477 }
pespacek7599a772022-02-07 14:40:55 +01002478#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2480 hash_length = mbedtls_md_get_size(md_info);
2481 if (mbedtls_md(md_info,
2482 crl_list->tbs.p,
2483 crl_list->tbs.len,
2484 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002485 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002487 break;
2488 }
pespaceka7a64692022-02-14 15:18:43 +01002489#endif /* MBEDTLS_USE_PSA_CRYPTO */
2490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002492 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2496 crl_list->sig_md, hash, hash_length,
2497 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002499 break;
2500 }
2501
2502 /*
2503 * Check for validity of CRL (Do not drop out)
2504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002510 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002512
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002513 /*
2514 * Check if certificate is revoked
2515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002518 break;
2519 }
2520
2521 crl_list = crl_list->next;
2522 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002525}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002527
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002528/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002529 * Check the signature of a certificate by its parent
2530 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2532 mbedtls_x509_crt *parent,
2533 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002534{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002535 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002536 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002537#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2538 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 md_info = mbedtls_md_info_from_type(child->sig_md);
2540 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002541
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002542 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2544 return -1;
2545 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002546#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002548 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 status = psa_hash_compute(hash_alg,
2551 child->tbs.p,
2552 child->tbs.len,
2553 hash,
2554 sizeof(hash),
2555 &hash_len);
2556 if (status != PSA_SUCCESS) {
2557 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002558 }
2559
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002560#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002561 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2563 return -1;
2564 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002565
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002566#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2568 return mbedtls_pk_verify_restartable(&parent->pk,
2569 child->sig_md, hash, hash_len,
2570 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002571 }
2572#else
2573 (void) rs_ctx;
2574#endif
2575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2577 child->sig_md, hash, hash_len,
2578 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002579}
2580
2581/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002582 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2583 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002584 *
2585 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002587static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2588 const mbedtls_x509_crt *parent,
2589 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002590{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002591 int need_ca_bit;
2592
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002593 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2595 return -1;
2596 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002597
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002598 /* Parent must have the basicConstraints CA bit set as a general rule */
2599 need_ca_bit = 1;
2600
2601 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002603 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002604 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (need_ca_bit && !parent->ca_istrue) {
2607 return -1;
2608 }
2609
2610 if (need_ca_bit &&
2611 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2612 return -1;
2613 }
2614
2615 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002616}
2617
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002618/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002619 * Find a suitable parent for child in candidates, or return NULL.
2620 *
2621 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002622 * 1. subject name matches child's issuer
2623 * 2. if necessary, the CA bit is set and key usage allows signing certs
2624 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002625 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002626 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002627 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002628 * If there's a suitable candidate which is also time-valid, return the first
2629 * such. Otherwise, return the first suitable candidate (or NULL if there is
2630 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002631 *
2632 * The rationale for this rule is that someone could have a list of trusted
2633 * roots with two versions on the same root with different validity periods.
2634 * (At least one user reported having such a list and wanted it to just work.)
2635 * The reason we don't just require time-validity is that generally there is
2636 * only one version, and if it's expired we want the flags to state that
2637 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002638 *
2639 * The rationale for rule 3 (signature for trusted roots) is that users might
2640 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002641 * way we select the correct one is by checking the signature (as we don't
2642 * rely on key identifier extensions). (This is one way users might choose to
2643 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002644 *
2645 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002646 * - [in] child: certificate for which we're looking for a parent
2647 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002648 * - [out] r_parent: parent found (or NULL)
2649 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002650 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2651 * of the chain, 0 otherwise
2652 * - [in] path_cnt: number of intermediates seen so far
2653 * - [in] self_cnt: number of self-signed intermediates seen so far
2654 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002655 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002656 *
2657 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002658 * - 0 on success
2659 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002660 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002661static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 mbedtls_x509_crt *child,
2663 mbedtls_x509_crt *candidates,
2664 mbedtls_x509_crt **r_parent,
2665 int *r_signature_is_good,
2666 int top,
2667 unsigned path_cnt,
2668 unsigned self_cnt,
2669 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002670{
Janos Follath865b3eb2019-12-16 11:46:15 +00002671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002672 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002673 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002674
2675#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002676 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002678 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002679 parent = rs_ctx->parent;
2680 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002681 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002682
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002683 /* clear saved state */
2684 rs_ctx->parent = NULL;
2685 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002686 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002687
2688 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002689 goto check_signature;
2690 }
2691#endif
2692
2693 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002694 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002697 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002699 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002701
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002702 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 if (parent->max_pathlen > 0 &&
2704 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002705 continue;
2706 }
2707
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002708 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002709#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2710check_signature:
2711#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002713
2714#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002716 /* save state */
2717 rs_ctx->parent = parent;
2718 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002719 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002722 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002723#else
2724 (void) ret;
2725#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002726
2727 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002729 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002731
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002732 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2734 mbedtls_x509_time_is_future(&parent->valid_from)) {
2735 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002736 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002737 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002738 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002739
2740 continue;
2741 }
2742
Andy Gross1f627142019-01-30 10:25:53 -06002743 *r_parent = parent;
2744 *r_signature_is_good = signature_is_good;
2745
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002746 break;
2747 }
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002750 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002751 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002752 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002755}
2756
2757/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002758 * Find a parent in trusted CAs or the provided chain, or return NULL.
2759 *
2760 * Searches in trusted CAs first, and return the first suitable parent found
2761 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002762 *
2763 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002764 * - [in] child: certificate for which we're looking for a parent, followed
2765 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002766 * - [in] trust_ca: list of locally trusted certificates
2767 * - [out] parent: parent found (or NULL)
2768 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2769 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2770 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2771 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002772 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002773 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002774 *
2775 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002776 * - 0 on success
2777 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002778 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002779static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 mbedtls_x509_crt *child,
2781 mbedtls_x509_crt *trust_ca,
2782 mbedtls_x509_crt **parent,
2783 int *parent_is_trusted,
2784 int *signature_is_good,
2785 unsigned path_cnt,
2786 unsigned self_cnt,
2787 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002788{
Janos Follath865b3eb2019-12-16 11:46:15 +00002789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002790 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002791
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002792 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002793
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002794#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002795 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002797 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002798 rs_ctx->parent_is_trusted = -1;
2799 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002800#endif
2801
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002803 search_list = *parent_is_trusted ? trust_ca : child->next;
2804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 ret = x509_crt_find_parent_in(child, search_list,
2806 parent, signature_is_good,
2807 *parent_is_trusted,
2808 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002809
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002810#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002812 /* save state */
2813 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002815 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002816#else
2817 (void) ret;
2818#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002819
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002820 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002822 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002824
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002825 /* prepare second iteration */
2826 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002827 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002828
2829 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002831 *parent_is_trusted = 0;
2832 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002833 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002836}
2837
2838/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002839 * Check if an end-entity certificate is locally trusted
2840 *
2841 * Currently we require such certificates to be self-signed (actually only
2842 * check for self-issued as self-signatures are not checked)
2843 */
2844static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 mbedtls_x509_crt *crt,
2846 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002847{
2848 mbedtls_x509_crt *cur;
2849
2850 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2852 return -1;
2853 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002854
2855 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2857 if (crt->raw.len == cur->raw.len &&
2858 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2859 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002860 }
2861 }
2862
2863 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002865}
2866
2867/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002868 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002869 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002870 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2871 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002872 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002873 * such that every cert in the chain is a child of the next one,
2874 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002875 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002876 * Verify that chain and return it with flags for all issues found.
2877 *
2878 * Special cases:
2879 * - EE == Rj -> return a one-element list containing it
2880 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2881 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002882 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002883 * Tests for (aspects of) this function should include at least:
2884 * - trusted EE
2885 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002886 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002887 * - if relevant: EE untrusted
2888 * - if relevant: EE -> intermediate, untrusted
2889 * with the aspect under test checked at each relevant level (EE, int, root).
2890 * For some aspects longer chains are required, but usually length 2 is
2891 * enough (but length 1 is not in general).
2892 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002893 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002894 * - [in] crt: the cert list EE, C1, ..., Cn
2895 * - [in] trust_ca: the trusted list R1, ..., Rp
2896 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002897 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002898 * Only valid when return value is 0, may contain garbage otherwise!
2899 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002900 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002901 *
2902 * Return value:
2903 * - non-zero if the chain could not be fully built and examined
2904 * - 0 is the chain was successfully built and examined,
2905 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002906 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002907static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 mbedtls_x509_crt *crt,
2909 mbedtls_x509_crt *trust_ca,
2910 mbedtls_x509_crl *ca_crl,
2911 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2912 void *p_ca_cb,
2913 const mbedtls_x509_crt_profile *profile,
2914 mbedtls_x509_crt_verify_chain *ver_chain,
2915 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002916{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002917 /* Don't initialize any of those variables here, so that the compiler can
2918 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002920 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002921 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002922 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002923 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002924 int parent_is_trusted;
2925 int child_is_trusted;
2926 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002927 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002928 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002929
2930#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2931 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002933 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002934 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002935 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002936
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002937 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002938 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002939 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002940 flags = &cur->flags;
2941
2942 goto find_parent;
2943 }
2944#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002945
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002946 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002947 self_cnt = 0;
2948 parent_is_trusted = 0;
2949 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002952 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002953 cur = &ver_chain->items[ver_chain->len];
2954 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002955 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002956 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002957 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002958
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002959 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002961 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002967
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002968 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 if (child_is_trusted) {
2970 return 0;
2971 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002972
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002979 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002981
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 if (ver_chain->len == 1 &&
2984 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2985 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002986 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002987
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002988#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2989find_parent:
2990#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002991
2992 /* Obtain list of potential trusted signers from CA callback,
2993 * or use statically provided list. */
2994#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 if (f_ca_cb != NULL) {
2996 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2997 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002998 ver_chain->trust_ca_cb_result = NULL;
2999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
3001 if (ret != 0) {
3002 return MBEDTLS_ERR_X509_FATAL_ERROR;
3003 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00003004
3005 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00003007#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3008 {
3009 ((void) f_ca_cb);
3010 ((void) p_ca_cb);
3011 cur_trust_ca = trust_ca;
3012 }
3013
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003014 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
3016 &parent_is_trusted, &signature_is_good,
3017 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003018
3019#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003021 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003022 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003023 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02003024 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003027 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003028#else
3029 (void) ret;
3030#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003031
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003032 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003034 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003036 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003037
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003038 /* Count intermediate self-issued (not necessarily self-signed) certs.
3039 * These can occur with some strategies for key rollover, see [SIRO],
3040 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 if (ver_chain->len != 1 &&
3042 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003043 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02003044 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01003045
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003046 /* path_cnt is 0 for the first intermediate CA,
3047 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 if (!parent_is_trusted &&
3049 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003050 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003052 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003053
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02003054 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003056 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003058
3059 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003061 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003065 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003067#else
3068 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003069#endif
3070
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003071 /* prepare for next iteration */
3072 child = parent;
3073 parent = NULL;
3074 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003075 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003076 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003077}
3078
3079/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003080 * Check for CN match
3081 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003082static int x509_crt_check_cn(const mbedtls_x509_buf *name,
3083 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003084{
3085 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 if (name->len == cn_len &&
3087 x509_memcasecmp(cn, name->p, cn_len) == 0) {
3088 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003089 }
3090
3091 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 if (x509_check_wildcard(cn, name) == 0) {
3093 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003094 }
3095
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003097}
3098
3099/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003100 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003102static int x509_crt_check_san(const mbedtls_x509_buf *name,
3103 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003104{
3105 const unsigned char san_type = (unsigned char) name->tag &
3106 MBEDTLS_ASN1_TAG_VALUE_MASK;
3107
3108 /* dNSName */
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
3110 return x509_crt_check_cn(name, cn, cn_len);
3111 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003112
3113 /* (We may handle other types here later.) */
3114
3115 /* Unrecognized type */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003117}
3118
3119/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003120 * Verify the requested CN - only call this if cn is not NULL!
3121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003122static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3123 const char *cn,
3124 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003125{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003126 const mbedtls_x509_name *name;
3127 const mbedtls_x509_sequence *cur;
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
3131 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
3132 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003133 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003134 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003135 }
3136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003138 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 }
3140 } else {
3141 for (name = &crt->subject; name != NULL; name = name->next) {
3142 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3143 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
3144 break;
3145 }
3146 }
3147
3148 if (name == NULL) {
3149 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3150 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003151 }
3152}
3153
3154/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003155 * Merge the flags for all certs in the chain, after calling callback
3156 */
3157static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 uint32_t *flags,
3159 const mbedtls_x509_crt_verify_chain *ver_chain,
3160 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3161 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003162{
Janos Follath865b3eb2019-12-16 11:46:15 +00003163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003164 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003165 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003166 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003169 cur = &ver_chain->items[i-1];
3170 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003171
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 if (NULL != f_vrfy) {
3173 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3174 return ret;
3175 }
3176 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003177
3178 *flags |= cur_flags;
3179 }
3180
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003182}
3183
3184/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003185 * Verify the certificate validity, with profile, restartable version
3186 *
3187 * This function:
3188 * - checks the requested CN (if any)
3189 * - checks the type and size of the EE cert's key,
3190 * as that isn't done as part of chain building/verification currently
3191 * - builds and verifies the chain
3192 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003193 *
3194 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3195 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3196 * verification routine to search for trusted signers, and CRLs will
3197 * be disabled. Otherwise, `trust_ca` will be used as the static list
3198 * of trusted signers, and `ca_crl` will be use as the static list
3199 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003201static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3202 mbedtls_x509_crt *trust_ca,
3203 mbedtls_x509_crl *ca_crl,
3204 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3205 void *p_ca_cb,
3206 const mbedtls_x509_crt_profile *profile,
3207 const char *cn, uint32_t *flags,
3208 int (*f_vrfy)(void *,
3209 mbedtls_x509_crt *,
3210 int,
3211 uint32_t *),
3212 void *p_vrfy,
3213 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003214{
Janos Follath865b3eb2019-12-16 11:46:15 +00003215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003216 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003217 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003218 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003219
3220 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003221 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003223
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003225 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3226 goto exit;
3227 }
3228
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003229 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 if (cn != NULL) {
3231 x509_crt_verify_name(crt, cn, &ee_flags);
3232 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003233
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003234 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003236
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003238 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003240
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003242 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003244
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003245 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3247 f_ca_cb, p_ca_cb, profile,
3248 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003253
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003254 /* Merge end-entity flags */
3255 ver_chain.items[0].flags |= ee_flags;
3256
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003257 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003259
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003260exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003261
3262#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3264 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003265 ver_chain.trust_ca_cb_result = NULL;
3266#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3267
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003268#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3270 mbedtls_x509_crt_restart_free(rs_ctx);
3271 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003272#endif
3273
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003274 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3275 * the SSL module for authmode optional, but non-zero return from the
3276 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003278 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003279 }
3280
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 if (ret != 0) {
3282 *flags = (uint32_t) -1;
3283 return ret;
3284 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 if (*flags != 0) {
3287 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3288 }
3289
3290 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003291}
3292
Hanno Becker3116fb32019-03-28 13:34:42 +00003293
3294/*
3295 * Verify the certificate validity (default profile, not restartable)
3296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003297int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3298 mbedtls_x509_crt *trust_ca,
3299 mbedtls_x509_crl *ca_crl,
3300 const char *cn, uint32_t *flags,
3301 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3302 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003303{
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3305 NULL, NULL,
3306 &mbedtls_x509_crt_profile_default,
3307 cn, flags,
3308 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003309}
3310
3311/*
3312 * Verify the certificate validity (user-chosen profile, not restartable)
3313 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003314int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3315 mbedtls_x509_crt *trust_ca,
3316 mbedtls_x509_crl *ca_crl,
3317 const mbedtls_x509_crt_profile *profile,
3318 const char *cn, uint32_t *flags,
3319 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3320 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003321{
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3323 NULL, NULL,
3324 profile, cn, flags,
3325 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003326}
3327
3328#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3329/*
3330 * Verify the certificate validity (user-chosen profile, CA callback,
3331 * not restartable).
3332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003333int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3334 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3335 void *p_ca_cb,
3336 const mbedtls_x509_crt_profile *profile,
3337 const char *cn, uint32_t *flags,
3338 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3339 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003340{
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3342 f_ca_cb, p_ca_cb,
3343 profile, cn, flags,
3344 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003345}
3346#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3347
Gilles Peskine449bd832023-01-11 14:50:10 +01003348int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3349 mbedtls_x509_crt *trust_ca,
3350 mbedtls_x509_crl *ca_crl,
3351 const mbedtls_x509_crt_profile *profile,
3352 const char *cn, uint32_t *flags,
3353 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3354 void *p_vrfy,
3355 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003356{
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3358 NULL, NULL,
3359 profile, cn, flags,
3360 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003361}
3362
3363
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003364/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003365 * Initialize a certificate chain
3366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003368{
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003370}
3371
3372/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003373 * Unallocate all certificate data
3374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003375void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 mbedtls_x509_crt *cert_cur = crt;
3378 mbedtls_x509_crt *cert_prv;
Przemek Stekiel62d8f842023-01-03 09:37:47 +01003379 mbedtls_x509_sequence *seq_cur;
3380 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 while (cert_cur != NULL) {
3383 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003387#endif
3388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3390 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3391 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3392 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3393 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003394
toth92g8d435a02021-05-10 15:16:33 +02003395 seq_cur = cert_cur->authority_key_id.authorityCertIssuer.next;
3396 while (seq_cur != NULL) {
3397 seq_prv = seq_cur;
3398 seq_cur = seq_cur->next;
3399 mbedtls_platform_zeroize(seq_prv,
3400 sizeof(mbedtls_x509_sequence));
3401 mbedtls_free(seq_prv);
toth92ga41954d2021-02-12 16:11:17 +01003402 }
3403
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3405 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3406 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003407 }
3408
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003409 cert_prv = cert_cur;
3410 cert_cur = cert_cur->next;
3411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3413 if (cert_prv != crt) {
3414 mbedtls_free(cert_prv);
3415 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003416 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003417}
3418
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003419#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3420/*
3421 * Initialize a restart context
3422 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003423void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003424{
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003426
3427 ctx->parent = NULL;
3428 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003429 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003430
3431 ctx->parent_is_trusted = -1;
3432
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003433 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003434 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003436}
3437
3438/*
3439 * Free the components of a restart context
3440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003441void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003442{
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003444 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 mbedtls_pk_restart_free(&ctx->pk);
3448 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003449}
3450#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452#endif /* MBEDTLS_X509_CRT_PARSE_C */