blob: f77991eb5c1795e5877006aa7ee531bd02951972 [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
Przemek Stekiel21c37282023-01-16 08:47:49 +0100565int mbedtls_x509_get_ns_cert_type(unsigned char **p,
566 const unsigned char *end,
567 unsigned char *ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568{
Janos Follath865b3eb2019-12-16 11:46:15 +0000569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
573 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
574 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Przemek Stekieldb128f52023-01-16 13:33:19 +0100576 if (bs.len == 0) {
577 *ns_cert_type = 0;
578 return 0;
579 }
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (bs.len != 1) {
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
586 /* Get actual bitstring */
587 *ns_cert_type = *bs.p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589}
590
Przemek Stekiel21c37282023-01-16 08:47:49 +0100591int mbedtls_x509_get_key_usage(unsigned char **p,
592 const unsigned char *end,
593 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594{
Janos Follath865b3eb2019-12-16 11:46:15 +0000595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200596 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
600 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
601 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602
Przemek Stekieldb128f52023-01-16 13:33:19 +0100603 if (bs.len == 0) {
604 *key_usage = 0;
605 return 0;
606 }
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (bs.len < 1) {
609 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
610 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
611 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612
613 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200614 *key_usage = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200616 *key_usage |= (unsigned int) bs.p[i] << (8*i);
617 }
618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620}
621
622/*
623 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
624 *
625 * KeyPurposeId ::= OBJECT IDENTIFIER
626 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100627static int x509_get_ext_key_usage(unsigned char **p,
628 const unsigned char *end,
629 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630{
Janos Follath865b3eb2019-12-16 11:46:15 +0000631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
634 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
635 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636
637 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (ext_key_usage->buf.p == NULL) {
639 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
640 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
641 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644}
645
646/*
647 * SubjectAltName ::= GeneralNames
648 *
649 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
650 *
651 * GeneralName ::= CHOICE {
652 * otherName [0] OtherName,
653 * rfc822Name [1] IA5String,
654 * dNSName [2] IA5String,
655 * x400Address [3] ORAddress,
656 * directoryName [4] Name,
657 * ediPartyName [5] EDIPartyName,
658 * uniformResourceIdentifier [6] IA5String,
659 * iPAddress [7] OCTET STRING,
660 * registeredID [8] OBJECT IDENTIFIER }
661 *
662 * OtherName ::= SEQUENCE {
663 * type-id OBJECT IDENTIFIER,
664 * value [0] EXPLICIT ANY DEFINED BY type-id }
665 *
666 * EDIPartyName ::= SEQUENCE {
667 * nameAssigner [0] DirectoryString OPTIONAL,
668 * partyName [1] DirectoryString }
669 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300670 * NOTE: we list all types, but only use dNSName and otherName
671 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672 */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100673int mbedtls_x509_get_subject_alt_name(unsigned char **p,
674 const unsigned char *end,
675 mbedtls_x509_sequence *subject_alt_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676{
Janos Follath865b3eb2019-12-16 11:46:15 +0000677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200678 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
683 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
685 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
686 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
687 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (*p + len != end) {
690 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
691 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
692 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 while (*p < end) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300695 mbedtls_x509_subject_alternative_name dummy_san_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
Ron Eldordbbd9662019-05-15 15:46:03 +0300697
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698 tag = **p;
699 (*p)++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
701 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
702 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if ((tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
705 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
706 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
707 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100708 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
Ron Eldordbbd9662019-05-15 15:46:03 +0300710 /*
irwir74aee1c2019-09-21 18:21:48 +0300711 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300712 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 ret = mbedtls_x509_parse_subject_alt_name(&(cur->buf), &dummy_san_buf);
Ron Eldordbbd9662019-05-15 15:46:03 +0300714 /*
715 * In case the extension is malformed, return an error,
716 * and clear the allocated sequences.
717 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
719 mbedtls_asn1_sequence_free(subject_alt_name->next);
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300720 subject_alt_name->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return ret;
Ron Eldordbbd9662019-05-15 15:46:03 +0300722 }
723
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (cur->buf.p != NULL) {
726 if (cur->next != NULL) {
727 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
728 }
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (cur->next == NULL) {
733 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
734 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
735 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 cur = cur->next;
738 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200739
740 buf = &(cur->buf);
741 buf->tag = tag;
742 buf->p = *p;
743 buf->len = tag_len;
744 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 }
746
747 /* Set final sequence entry's next pointer to NULL */
748 cur->next = NULL;
749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (*p != end) {
751 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
752 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
753 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756}
757
758/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200759 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
760 *
761 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
762 *
763 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
764 *
765 * PolicyInformation ::= SEQUENCE {
766 * policyIdentifier CertPolicyId,
767 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
768 * PolicyQualifierInfo OPTIONAL }
769 *
770 * CertPolicyId ::= OBJECT IDENTIFIER
771 *
772 * PolicyQualifierInfo ::= SEQUENCE {
773 * policyQualifierId PolicyQualifierId,
774 * qualifier ANY DEFINED BY policyQualifierId }
775 *
776 * -- policyQualifierIds for Internet policy qualifiers
777 *
778 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
779 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
780 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
781 *
782 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
783 *
784 * Qualifier ::= CHOICE {
785 * cPSuri CPSuri,
786 * userNotice UserNotice }
787 *
788 * CPSuri ::= IA5String
789 *
790 * UserNotice ::= SEQUENCE {
791 * noticeRef NoticeReference OPTIONAL,
792 * explicitText DisplayText OPTIONAL }
793 *
794 * NoticeReference ::= SEQUENCE {
795 * organization DisplayText,
796 * noticeNumbers SEQUENCE OF INTEGER }
797 *
798 * DisplayText ::= CHOICE {
799 * ia5String IA5String (SIZE (1..200)),
800 * visibleString VisibleString (SIZE (1..200)),
801 * bmpString BMPString (SIZE (1..200)),
802 * utf8String UTF8String (SIZE (1..200)) }
803 *
804 * NOTE: we only parse and use anyPolicy without qualifiers at this point
805 * as defined in RFC 5280.
806 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807static int x509_get_certificate_policies(unsigned char **p,
808 const unsigned char *end,
809 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200810{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300811 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200812 size_t len;
813 mbedtls_asn1_buf *buf;
814 mbedtls_asn1_sequence *cur = certificate_policies;
815
816 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 ret = mbedtls_asn1_get_tag(p, end, &len,
818 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
819 if (ret != 0) {
820 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
821 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (*p + len != end) {
824 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
825 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
826 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200827
828 /*
829 * Cannot be an empty sequence.
830 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (len == 0) {
832 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
833 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
834 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200837 mbedtls_x509_buf policy_oid;
838 const unsigned char *policy_end;
839
840 /*
841 * Get the policy sequence
842 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
844 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
845 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
846 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200847
848 policy_end = *p + len;
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
851 MBEDTLS_ASN1_OID)) != 0) {
852 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
853 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200854
855 policy_oid.tag = MBEDTLS_ASN1_OID;
856 policy_oid.len = len;
857 policy_oid.p = *p;
858
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300859 /*
860 * Only AnyPolicy is currently supported when enforcing policy.
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300863 /*
864 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200865 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300866 */
867 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
868 }
869
Ron Eldor74d9acc2019-03-21 14:00:03 +0200870 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (cur->buf.p != NULL) {
872 if (cur->next != NULL) {
873 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
874 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (cur->next == NULL) {
879 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
880 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
881 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200882
883 cur = cur->next;
884 }
885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200887 buf->tag = policy_oid.tag;
888 buf->p = policy_oid.p;
889 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300890
891 *p += len;
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 /*
894 * If there is an optional qualifier, then *p < policy_end
895 * Check the Qualifier len to verify it doesn't exceed policy_end.
896 */
897 if (*p < policy_end) {
898 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
899 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
900 0) {
901 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
902 }
Ron Eldor08063792019-05-13 16:38:39 +0300903 /*
904 * Skip the optional policy qualifiers.
905 */
906 *p += len;
907 }
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (*p != policy_end) {
910 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
911 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
912 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200913 }
914
915 /* Set final sequence entry's next pointer to NULL */
916 cur->next = NULL;
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (*p != end) {
919 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
920 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
921 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200924}
925
926/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 * X.509 v3 extensions
928 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930static int x509_get_crt_ext(unsigned char **p,
931 const unsigned char *end,
932 mbedtls_x509_crt *crt,
933 mbedtls_x509_crt_ext_cb_t cb,
934 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935{
Janos Follath865b3eb2019-12-16 11:46:15 +0000936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200938 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (*p == end) {
941 return 0;
942 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
945 return ret;
946 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947
Hanno Becker12f62fb2019-02-12 17:22:36 +0000948 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 /*
951 * Extension ::= SEQUENCE {
952 * extnID OBJECT IDENTIFIER,
953 * critical BOOLEAN DEFAULT FALSE,
954 * extnValue OCTET STRING }
955 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 int is_critical = 0; /* DEFAULT FALSE */
958 int ext_type = 0;
959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
961 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
962 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
963 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964
965 end_ext_data = *p + len;
966
967 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
969 MBEDTLS_ASN1_OID)) != 0) {
970 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
971 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972
k-stachowiak463928a2018-07-24 12:50:59 +0200973 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 extn_oid.p = *p;
975 *p += extn_oid.len;
976
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
979 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
980 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
981 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982
983 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
985 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
986 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
987 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200989 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 end_ext_octet = *p + len;
991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 if (end_ext_octet != end_ext_data) {
993 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
994 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
995 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
997 /*
998 * Detect supported extensions
999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001003 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (cb != NULL) {
1005 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
1006 if (ret != 0 && is_critical) {
1007 return ret;
1008 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001009 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001010 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001011 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001012
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 /* No parser found, skip extension */
1014 *p = end_ext_octet;
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1019 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 continue;
1022 }
1023
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001024 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if ((crt->ext_types & ext_type) != 0) {
1026 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1027 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001028
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 crt->ext_types |= ext_type;
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 switch (ext_type) {
1032 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
1033 /* Parse basic constraints */
1034 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
1035 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
1036 return ret;
1037 }
1038 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 case MBEDTLS_X509_EXT_KEY_USAGE:
1041 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001042 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
1043 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 return ret;
1045 }
1046 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1049 /* Parse extended key usage */
1050 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1051 &crt->ext_key_usage)) != 0) {
1052 return ret;
1053 }
1054 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1057 /* Parse subject alt name */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001058 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1059 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return ret;
1061 }
1062 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1065 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001066 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1067 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 return ret;
1069 }
1070 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1073 /* Parse certificate policies type */
1074 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1075 &crt->certificate_policies)) != 0) {
1076 /* Give the callback (if any) a chance to handle the extension
1077 * if it contains unsupported policies */
1078 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1079 cb(p_ctx, crt, &extn_oid, is_critical,
1080 start_ext_octet, end_ext_octet) == 0) {
1081 break;
1082 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (is_critical) {
1085 return ret;
1086 } else
1087 /*
1088 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1089 * cannot interpret or enforce the policy. However, it is up to
1090 * the user to choose how to enforce the policies,
1091 * unless the extension is critical.
1092 */
1093 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1094 return ret;
1095 }
1096 }
1097 break;
1098
1099 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001100 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 * If this is a non-critical extension, which the oid layer
1102 * supports, but there isn't an x509 parser for it,
1103 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (is_critical) {
1106 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1107 } else {
1108 *p = end_ext_octet;
1109 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110 }
1111 }
1112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if (*p != end) {
1114 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1115 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1116 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119}
1120
1121/*
1122 * Parse and fill a single X.509 certificate in DER format
1123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1125 const unsigned char *buf,
1126 size_t buflen,
1127 int make_copy,
1128 mbedtls_x509_crt_ext_cb_t cb,
1129 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130{
Janos Follath865b3eb2019-12-16 11:46:15 +00001131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 size_t len;
1133 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1137 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1138 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139
1140 /*
1141 * Check for valid input
1142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (crt == NULL || buf == NULL) {
1144 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1145 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001147 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001149 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 end = p + len;
1151
1152 /*
1153 * Certificate ::= SEQUENCE {
1154 * tbsCertificate TBSCertificate,
1155 * signatureAlgorithm AlgorithmIdentifier,
1156 * signatureValue BIT STRING }
1157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1159 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1160 mbedtls_x509_crt_free(crt);
1161 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 }
1163
Janos Follathcc0e49d2016-02-17 14:34:12 +00001164 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001165 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001167 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1169 if (crt->raw.p == NULL) {
1170 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1171 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001174 crt->own_buffer = 1;
1175
1176 p += crt->raw.len - len;
1177 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 } else {
1179 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001180 crt->own_buffer = 0;
1181 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001182
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001183 /*
1184 * TBSCertificate ::= SEQUENCE {
1185 */
1186 crt->tbs.p = p;
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1189 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1190 mbedtls_x509_crt_free(crt);
1191 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 }
1193
1194 end = p + len;
1195 crt->tbs.len = end - crt->tbs.p;
1196
1197 /*
1198 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1199 *
1200 * CertificateSerialNumber ::= INTEGER
1201 *
1202 * signature AlgorithmIdentifier
1203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1205 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1206 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1207 &sig_params1)) != 0) {
1208 mbedtls_x509_crt_free(crt);
1209 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 }
1211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (crt->version < 0 || crt->version > 2) {
1213 mbedtls_x509_crt_free(crt);
1214 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 }
1216
Andres AG7ca4a032017-03-09 16:16:11 +00001217 crt->version++;
1218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1220 &crt->sig_md, &crt->sig_pk,
1221 &crt->sig_opts)) != 0) {
1222 mbedtls_x509_crt_free(crt);
1223 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224 }
1225
1226 /*
1227 * issuer Name
1228 */
1229 crt->issuer_raw.p = p;
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1232 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1233 mbedtls_x509_crt_free(crt);
1234 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 }
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1238 mbedtls_x509_crt_free(crt);
1239 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001240 }
1241
1242 crt->issuer_raw.len = p - crt->issuer_raw.p;
1243
1244 /*
1245 * Validity ::= SEQUENCE {
1246 * notBefore Time,
1247 * notAfter Time }
1248 *
1249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1251 &crt->valid_to)) != 0) {
1252 mbedtls_x509_crt_free(crt);
1253 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254 }
1255
1256 /*
1257 * subject Name
1258 */
1259 crt->subject_raw.p = p;
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1262 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1263 mbedtls_x509_crt_free(crt);
1264 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265 }
1266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1268 mbedtls_x509_crt_free(crt);
1269 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001270 }
1271
1272 crt->subject_raw.len = p - crt->subject_raw.p;
1273
1274 /*
1275 * SubjectPublicKeyInfo
1276 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001277 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1279 mbedtls_x509_crt_free(crt);
1280 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001282 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283
1284 /*
1285 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1286 * -- If present, version shall be v2 or v3
1287 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1288 * -- If present, version shall be v2 or v3
1289 * extensions [3] EXPLICIT Extensions OPTIONAL
1290 * -- If present, version shall be v3
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (crt->version == 2 || crt->version == 3) {
1293 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1294 if (ret != 0) {
1295 mbedtls_x509_crt_free(crt);
1296 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 }
1298 }
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (crt->version == 2 || crt->version == 3) {
1301 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1302 if (ret != 0) {
1303 mbedtls_x509_crt_free(crt);
1304 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305 }
1306 }
1307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (crt->version == 3) {
1309 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1310 if (ret != 0) {
1311 mbedtls_x509_crt_free(crt);
1312 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313 }
1314 }
1315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 if (p != end) {
1317 mbedtls_x509_crt_free(crt);
1318 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1319 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 }
1321
1322 end = crt_end;
1323
1324 /*
1325 * }
1326 * -- end of TBSCertificate
1327 *
1328 * signatureAlgorithm AlgorithmIdentifier,
1329 * signatureValue BIT STRING
1330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1332 mbedtls_x509_crt_free(crt);
1333 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334 }
1335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (crt->sig_oid.len != sig_oid2.len ||
1337 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001338 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001339 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 (sig_params1.len != 0 &&
1341 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1342 mbedtls_x509_crt_free(crt);
1343 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344 }
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1347 mbedtls_x509_crt_free(crt);
1348 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349 }
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if (p != end) {
1352 mbedtls_x509_crt_free(crt);
1353 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1354 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355 }
1356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358}
1359
1360/*
1361 * Parse one X.509 certificate in DER format from a buffer and add them to a
1362 * chained list
1363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1365 const unsigned char *buf,
1366 size_t buflen,
1367 int make_copy,
1368 mbedtls_x509_crt_ext_cb_t cb,
1369 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370{
Janos Follath865b3eb2019-12-16 11:46:15 +00001371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373
1374 /*
1375 * Check for valid input
1376 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if (crt == NULL || buf == NULL) {
1378 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1379 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 prev = crt;
1383 crt = crt->next;
1384 }
1385
1386 /*
1387 * Add new certificate on the end of the chain if needed.
1388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (crt->version != 0 && crt->next == NULL) {
1390 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if (crt->next == NULL) {
1393 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1394 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395
1396 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001398 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399 }
1400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1402 if (ret != 0) {
1403 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001404 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (crt != chain) {
1408 mbedtls_free(crt);
1409 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412 }
1413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415}
1416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1418 const unsigned char *buf,
1419 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001420{
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001422}
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1425 const unsigned char *buf,
1426 size_t buflen,
1427 int make_copy,
1428 mbedtls_x509_crt_ext_cb_t cb,
1429 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001430{
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001432}
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1435 const unsigned char *buf,
1436 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001437{
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001439}
1440
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001442 * Parse one or more PEM certificates from a buffer and add them to the chained
1443 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001445int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1446 const unsigned char *buf,
1447 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448{
Janos Follath98e28a72016-05-31 14:03:54 +01001449#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001450 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001452#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453
1454 /*
1455 * Check for valid input
1456 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (chain == NULL || buf == NULL) {
1458 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1459 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
1461 /*
1462 * Determine buffer content. Buffer contains either one DER certificate or
1463 * one or more PEM certificates.
1464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1467 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001469 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1472 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1473 }
Janos Follath98e28a72016-05-31 14:03:54 +01001474#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001476#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001483 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001487
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001488 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 ret = mbedtls_pem_read_buffer(&pem,
1490 "-----BEGIN CERTIFICATE-----",
1491 "-----END CERTIFICATE-----",
1492 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495 /*
1496 * Was PEM encoded
1497 */
1498 buflen -= use_len;
1499 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1501 return ret;
1502 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1503 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504
1505 /*
1506 * PEM header and footer were found
1507 */
1508 buflen -= use_len;
1509 buf += use_len;
1510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
Paul Bakker5a5fa922014-09-26 14:53:04 +02001515 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 /*
1527 * Quit parsing on a memory error
1528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1530 return ret;
1531 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536
1537 total_failed++;
1538 continue;
1539 }
1540
1541 success = 1;
1542 }
1543 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (success) {
1546 return total_failed;
1547 } else if (first_error) {
1548 return first_error;
1549 } else {
1550 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1551 }
Janos Follath98e28a72016-05-31 14:03:54 +01001552#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553}
1554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556/*
1557 * Load one or more certificates and add them to the chained list
1558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001559int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560{
Janos Follath865b3eb2019-12-16 11:46:15 +00001561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562 size_t n;
1563 unsigned char *buf;
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1566 return ret;
1567 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 mbedtls_platform_zeroize(buf, n);
1572 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575}
1576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578{
1579 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001580#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581 int w_ret;
1582 WCHAR szDir[MAX_PATH];
1583 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001584 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
Paul Bakker9af723c2014-05-01 13:03:14 +02001587 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001588 HANDLE hFind;
1589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (len > MAX_PATH - 3) {
1591 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1592 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 memset(szDir, 0, sizeof(szDir));
1595 memset(filename, 0, MAX_PATH);
1596 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001597 filename[len++] = '\\';
1598 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599 filename[len++] = '*';
1600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1602 MAX_PATH - 3);
1603 if (w_ret == 0) {
1604 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1605 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 hFind = FindFirstFileW(szDir, &file_data);
1608 if (hFind == INVALID_HANDLE_VALUE) {
1609 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1610 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
1612 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 do {
1614 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1621 lstrlenW(file_data.cFileName),
1622 p, (int) len - 1,
1623 NULL, NULL);
1624 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001625 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1626 goto cleanup;
1627 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1630 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 }
1635 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640
Ron Eldor36d90422017-01-09 15:09:16 +02001641cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001643#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001644 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001645 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001647 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001648 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (dir == NULL) {
1652 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1653 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654
Ron Eldor63140682017-01-09 19:27:59 +02001655#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1657 closedir(dir);
1658 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001659 }
Ron Eldor63140682017-01-09 19:27:59 +02001660#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 while ((entry = readdir(dir)) != NULL) {
1665 snp_ret = mbedtls_snprintf(entry_name, sizeof entry_name,
1666 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if (snp_ret < 0 || (size_t) snp_ret >= sizeof entry_name) {
Andres AGf9113192016-09-02 14:06:04 +01001669 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1670 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 } else if (stat(entry_name, &sb) == -1) {
1672 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001673 /* Broken symbolic link - ignore this entry.
1674 stat(2) will return this error for either (a) a dangling
1675 symlink or (b) a missing file.
1676 Given that we have just obtained the filename from readdir,
1677 assume that it does exist and therefore treat this as a
1678 dangling symlink. */
1679 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001681 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001682 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1683 goto cleanup;
1684 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685 }
1686
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 if (!S_ISREG(sb.st_mode)) {
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
1691 // Ignore parse errors
1692 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1694 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001699 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001700
1701cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001703
Ron Eldor63140682017-01-09 19:27:59 +02001704#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 }
Ron Eldor63140682017-01-09 19:27:59 +02001708#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001709
Paul Bakkerbe089b02013-10-14 15:51:50 +02001710#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001715
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001716/*
1717 * OtherName ::= SEQUENCE {
1718 * type-id OBJECT IDENTIFIER,
1719 * value [0] EXPLICIT ANY DEFINED BY type-id }
1720 *
1721 * HardwareModuleName ::= SEQUENCE {
1722 * hwType OBJECT IDENTIFIER,
1723 * hwSerialNum OCTET STRING }
1724 *
1725 * NOTE: we currently only parse and use otherName of type HwModuleName,
1726 * as defined in RFC 4108.
1727 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001728static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1729 mbedtls_x509_san_other_name *other_name)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001730{
Janos Follathab23cd12019-05-09 13:53:57 +01001731 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001732 size_t len;
1733 unsigned char *p = subject_alt_name->p;
1734 const unsigned char *end = p + subject_alt_name->len;
1735 mbedtls_x509_buf cur_oid;
1736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 if ((subject_alt_name->tag &
1738 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1739 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001740 /*
1741 * The given subject alternative name is not of type "othername".
1742 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001744 }
1745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1747 MBEDTLS_ASN1_OID)) != 0) {
1748 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1749 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001750
1751 cur_oid.tag = MBEDTLS_ASN1_OID;
1752 cur_oid.p = p;
1753 cur_oid.len = len;
1754
1755 /*
1756 * Only HwModuleName is currently supported.
1757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1759 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001760 }
1761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (p + len >= end) {
1763 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1764 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1765 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Ron Eldor890819a2019-05-13 19:03:04 +03001766 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001767 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1769 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1770 0) {
1771 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1772 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1775 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1776 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1777 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1780 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1781 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001782
1783 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1784 other_name->value.hardware_module_name.oid.p = p;
1785 other_name->value.hardware_module_name.oid.len = len;
1786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 if (p + len >= end) {
1788 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1789 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1790 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Ron Eldor890819a2019-05-13 19:03:04 +03001791 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001792 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1794 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1796 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001797
1798 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1799 other_name->value.hardware_module_name.val.p = p;
1800 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001801 p += len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (p != end) {
1803 mbedtls_platform_zeroize(other_name,
1804 sizeof(*other_name));
1805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1806 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001807 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001809}
1810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1812 mbedtls_x509_subject_alternative_name *san)
Peter Kolbus9a969b62018-12-11 13:55:56 -06001813{
1814 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 switch (san_buf->tag &
1816 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1817 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
Peter Kolbus9a969b62018-12-11 13:55:56 -06001818 /*
1819 * otherName
1820 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
Peter Kolbus9a969b62018-12-11 13:55:56 -06001822 {
1823 mbedtls_x509_san_other_name other_name;
1824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 ret = x509_get_other_name(san_buf, &other_name);
1826 if (ret != 0) {
1827 return ret;
1828 }
Peter Kolbus9a969b62018-12-11 13:55:56 -06001829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Peter Kolbus9a969b62018-12-11 13:55:56 -06001831 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 memcpy(&san->san.other_name,
1833 &other_name, sizeof(other_name));
Peter Kolbus9a969b62018-12-11 13:55:56 -06001834
1835 }
1836 break;
1837
1838 /*
1839 * dNSName
1840 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
Peter Kolbus9a969b62018-12-11 13:55:56 -06001842 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Peter Kolbus9a969b62018-12-11 13:55:56 -06001844 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 memcpy(&san->san.unstructured_name,
1847 san_buf, sizeof(*san_buf));
Peter Kolbus9a969b62018-12-11 13:55:56 -06001848
1849 }
1850 break;
1851
1852 /*
1853 * Type not supported
1854 */
1855 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001857 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 return 0;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001859}
1860
1861#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekiel21c37282023-01-16 08:47:49 +01001862int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
1863 const mbedtls_x509_sequence
1864 *subject_alt_name,
1865 const char *prefix)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001866{
Janos Follath865b3eb2019-12-16 11:46:15 +00001867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001868 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001869 size_t n = *size;
1870 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001872 mbedtls_x509_subject_alternative_name san;
1873 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 while (cur != NULL) {
1876 memset(&san, 0, sizeof(san));
1877 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1878 if (parse_ret != 0) {
1879 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1880 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001881 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 } else {
1883 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001884 MBEDTLS_X509_SAFE_SNPRINTF;
1885 }
1886 cur = cur->next;
1887 continue;
1888 }
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 switch (san.type) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001891 /*
1892 * otherName
1893 */
Ron Eldora2913912019-05-16 16:17:38 +03001894 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001895 {
1896 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001899 MBEDTLS_X509_SAFE_SNPRINTF;
1900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1902 &other_name->value.hardware_module_name.oid) != 0) {
1903 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001904 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 ret =
1906 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001907 MBEDTLS_X509_SAFE_SNPRINTF;
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 ret = mbedtls_oid_get_numeric_string(p,
1910 n,
1911 &other_name->value.hardware_module_name.oid);
Ron Eldor890819a2019-05-13 19:03:04 +03001912 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 ret =
1915 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001916 MBEDTLS_X509_SAFE_SNPRINTF;
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1919 ret = mbedtls_snprintf(p,
1920 n,
1921 "%02X",
1922 other_name->value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001923 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01001924 }
Ron Eldor890819a2019-05-13 19:03:04 +03001925 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1926 }
1927 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001928
1929 /*
1930 * dNSName
1931 */
Ron Eldora2913912019-05-16 16:17:38 +03001932 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001933 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001935 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if (san.san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001937 *p = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001939 }
Ron Eldora2913912019-05-16 16:17:38 +03001940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
Ron Eldora2913912019-05-16 16:17:38 +03001942 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001943 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001944 }
1945 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001946
1947 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001948 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001949 */
1950 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Janos Follath22f605f2019-05-10 10:37:17 +01001952 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001953 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001954 }
1955
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001956 cur = cur->next;
1957 }
1958
1959 *p = '\0';
1960
1961 *size = n;
1962 *buf = p;
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 return 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001965}
1966
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001967#define PRINT_ITEM(i) \
1968 { \
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001970 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001971 sep = ", "; \
1972 }
1973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974#define CERT_TYPE(type, name) \
1975 if (ns_cert_type & (type)) \
1976 PRINT_ITEM(name);
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001977
Przemek Stekiel21c37282023-01-16 08:47:49 +01001978int mbedtls_x509_info_cert_type(char **buf, size_t *size,
1979 unsigned char ns_cert_type)
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001980{
Janos Follath865b3eb2019-12-16 11:46:15 +00001981 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001982 size_t n = *size;
1983 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001984 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1987 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1988 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1989 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1990 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1991 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1992 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1993 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001994
1995 *size = n;
1996 *buf = p;
1997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 return 0;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001999}
2000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001#define KEY_USAGE(code, name) \
2002 if (key_usage & (code)) \
2003 PRINT_ITEM(name);
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02002004
Przemek Stekiel21c37282023-01-16 08:47:49 +01002005int mbedtls_x509_info_key_usage(char **buf, size_t *size,
2006 unsigned int key_usage)
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002007{
Janos Follath865b3eb2019-12-16 11:46:15 +00002008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002009 size_t n = *size;
2010 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002011 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
2014 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
2015 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
2016 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
2017 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
2018 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
2019 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
2020 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
2021 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002022
2023 *size = n;
2024 *buf = p;
2025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 return 0;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002027}
2028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029static int x509_info_ext_key_usage(char **buf, size_t *size,
2030 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002031{
Janos Follath865b3eb2019-12-16 11:46:15 +00002032 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002033 const char *desc;
2034 size_t n = *size;
2035 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002037 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 while (cur != NULL) {
2040 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002041 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002045 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002046
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002047 sep = ", ";
2048
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002049 cur = cur->next;
2050 }
2051
2052 *size = n;
2053 *buf = p;
2054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002056}
2057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058static int x509_info_cert_policies(char **buf, size_t *size,
2059 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02002060{
Janos Follath865b3eb2019-12-16 11:46:15 +00002061 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002062 const char *desc;
2063 size_t n = *size;
2064 char *p = *buf;
2065 const mbedtls_x509_sequence *cur = certificate_policies;
2066 const char *sep = "";
2067
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 while (cur != NULL) {
2069 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02002070 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002074 MBEDTLS_X509_SAFE_SNPRINTF;
2075
2076 sep = ", ";
2077
2078 cur = cur->next;
2079 }
2080
2081 *size = n;
2082 *buf = p;
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002085}
2086
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002087/*
2088 * Return an informational string about the certificate.
2089 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002090#define BEFORE_COLON 18
2091#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01002092int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
2093 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094{
Janos Follath865b3eb2019-12-16 11:46:15 +00002095 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002096 size_t n;
2097 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098 char key_size_str[BEFORE_COLON];
2099
2100 p = buf;
2101 n = size;
2102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 if (NULL == crt) {
2104 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01002105 MBEDTLS_X509_SAFE_SNPRINTF;
2106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01002108 }
2109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
2111 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002112 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 ret = mbedtls_snprintf(p, n, "%sserial number : ",
2114 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002115 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002118 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002121 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002123 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002126 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002128 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
2131 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2132 crt->valid_from.year, crt->valid_from.mon,
2133 crt->valid_from.day, crt->valid_from.hour,
2134 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002135 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
2138 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2139 crt->valid_to.year, crt->valid_to.mon,
2140 crt->valid_to.day, crt->valid_to.hour,
2141 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002142 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002145 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
2148 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002149 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002150
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002151 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
2153 mbedtls_pk_get_name(&crt->pk))) != 0) {
2154 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002155 }
2156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
2158 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002159 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002160
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 /*
2162 * Optional extensions
2163 */
2164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
2166 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
2167 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002168 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (crt->max_pathlen > 0) {
2171 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002172 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002173 }
2174 }
2175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2177 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002178 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002179
Przemek Stekiel21c37282023-01-16 08:47:49 +01002180 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
2181 &crt->subject_alt_names,
2182 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 return ret;
2184 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002185 }
2186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
2188 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002189 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002190
Przemek Stekiel21c37282023-01-16 08:47:49 +01002191 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 return ret;
2193 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002194 }
2195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2197 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002198 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002199
Przemek Stekiel21c37282023-01-16 08:47:49 +01002200 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 return ret;
2202 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002203 }
2204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2206 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002207 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if ((ret = x509_info_ext_key_usage(&p, &n,
2210 &crt->ext_key_usage)) != 0) {
2211 return ret;
2212 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002213 }
2214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2216 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002217 MBEDTLS_X509_SAFE_SNPRINTF;
2218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if ((ret = x509_info_cert_policies(&p, &n,
2220 &crt->certificate_policies)) != 0) {
2221 return ret;
2222 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002223 }
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002226 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002229}
2230
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002231struct x509_crt_verify_string {
2232 int code;
2233 const char *string;
2234};
2235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002237static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002238 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002239 { 0, NULL }
2240};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002241#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
2244 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002245{
Janos Follath865b3eb2019-12-16 11:46:15 +00002246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002247 const struct x509_crt_verify_string *cur;
2248 char *p = buf;
2249 size_t n = size;
2250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
2252 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002253 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002257 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002258 flags ^= cur->code;
2259 }
2260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if (flags != 0) {
2262 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
2263 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002264 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002265 }
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002268}
Hanno Becker612a2f12020-10-09 09:19:39 +01002269#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2272 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002273{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002274 unsigned int usage_must, usage_may;
2275 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
2279 return 0;
2280 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002281
2282 usage_must = usage & ~may_mask;
2283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
2285 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2286 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002287
2288 usage_may = usage & may_mask;
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
2291 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2292 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002295}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002296
Gilles Peskine449bd832023-01-11 14:50:10 +01002297int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2298 const char *usage_oid,
2299 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002302
2303 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2305 return 0;
2306 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002307
2308 /*
2309 * Look for the requested usage (or wildcard ANY) in our list
2310 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (cur_oid->len == usage_len &&
2315 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2316 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002317 }
2318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2320 return 0;
2321 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002322 }
2323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002325}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002328/*
2329 * Return 1 if the certificate is revoked, or 0 otherwise.
2330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002331int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002332{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 while (cur != NULL && cur->serial.len != 0) {
2336 if (crt->serial.len == cur->serial.len &&
2337 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2338 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339 }
2340
2341 cur = cur->next;
2342 }
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002345}
2346
2347/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002348 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002349 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002351static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2352 mbedtls_x509_crl *crl_list,
2353 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002354{
2355 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002356 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002357#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002358 psa_algorithm_t psa_algorithm;
2359#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002360 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002361#endif /* MBEDTLS_USE_PSA_CRYPTO */
2362 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (ca == NULL) {
2365 return flags;
2366 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 while (crl_list != NULL) {
2369 if (crl_list->version == 0 ||
2370 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371 crl_list = crl_list->next;
2372 continue;
2373 }
2374
2375 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002376 * Check if the CA is configured to sign CRLs
2377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 if (mbedtls_x509_crt_check_key_usage(ca,
2379 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002381 break;
2382 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002383
2384 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002385 * Check if CRL is correctly signed by the trusted CA
2386 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002388 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002390
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002392 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002394
pespacek7599a772022-02-07 14:40:55 +01002395#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
2397 if (psa_hash_compute(psa_algorithm,
2398 crl_list->tbs.p,
2399 crl_list->tbs.len,
2400 hash,
2401 sizeof(hash),
2402 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002403 /* Note: this can't happen except after an internal error */
2404 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2405 break;
2406 }
pespacek7599a772022-02-07 14:40:55 +01002407#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2409 hash_length = mbedtls_md_get_size(md_info);
2410 if (mbedtls_md(md_info,
2411 crl_list->tbs.p,
2412 crl_list->tbs.len,
2413 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002414 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002416 break;
2417 }
pespaceka7a64692022-02-14 15:18:43 +01002418#endif /* MBEDTLS_USE_PSA_CRYPTO */
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002421 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2425 crl_list->sig_md, hash, hash_length,
2426 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002428 break;
2429 }
2430
2431 /*
2432 * Check for validity of CRL (Do not drop out)
2433 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002435 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002439 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002441
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002442 /*
2443 * Check if certificate is revoked
2444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002447 break;
2448 }
2449
2450 crl_list = crl_list->next;
2451 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002456
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002457/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002458 * Check the signature of a certificate by its parent
2459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002460static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2461 mbedtls_x509_crt *parent,
2462 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002463{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002464 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002465 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002466#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2467 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 md_info = mbedtls_md_info_from_type(child->sig_md);
2469 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002470
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002471 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2473 return -1;
2474 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002475#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002477 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 status = psa_hash_compute(hash_alg,
2480 child->tbs.p,
2481 child->tbs.len,
2482 hash,
2483 sizeof(hash),
2484 &hash_len);
2485 if (status != PSA_SUCCESS) {
2486 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002487 }
2488
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002489#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002490 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2492 return -1;
2493 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002494
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002495#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2497 return mbedtls_pk_verify_restartable(&parent->pk,
2498 child->sig_md, hash, hash_len,
2499 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002500 }
2501#else
2502 (void) rs_ctx;
2503#endif
2504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2506 child->sig_md, hash, hash_len,
2507 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002508}
2509
2510/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002511 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2512 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002513 *
2514 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002516static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2517 const mbedtls_x509_crt *parent,
2518 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002519{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002520 int need_ca_bit;
2521
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002522 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2524 return -1;
2525 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002526
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002527 /* Parent must have the basicConstraints CA bit set as a general rule */
2528 need_ca_bit = 1;
2529
2530 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002532 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002533 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (need_ca_bit && !parent->ca_istrue) {
2536 return -1;
2537 }
2538
2539 if (need_ca_bit &&
2540 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2541 return -1;
2542 }
2543
2544 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002545}
2546
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002547/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002548 * Find a suitable parent for child in candidates, or return NULL.
2549 *
2550 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002551 * 1. subject name matches child's issuer
2552 * 2. if necessary, the CA bit is set and key usage allows signing certs
2553 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002554 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002555 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002556 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002557 * If there's a suitable candidate which is also time-valid, return the first
2558 * such. Otherwise, return the first suitable candidate (or NULL if there is
2559 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002560 *
2561 * The rationale for this rule is that someone could have a list of trusted
2562 * roots with two versions on the same root with different validity periods.
2563 * (At least one user reported having such a list and wanted it to just work.)
2564 * The reason we don't just require time-validity is that generally there is
2565 * only one version, and if it's expired we want the flags to state that
2566 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002567 *
2568 * The rationale for rule 3 (signature for trusted roots) is that users might
2569 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002570 * way we select the correct one is by checking the signature (as we don't
2571 * rely on key identifier extensions). (This is one way users might choose to
2572 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002573 *
2574 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002575 * - [in] child: certificate for which we're looking for a parent
2576 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002577 * - [out] r_parent: parent found (or NULL)
2578 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002579 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2580 * of the chain, 0 otherwise
2581 * - [in] path_cnt: number of intermediates seen so far
2582 * - [in] self_cnt: number of self-signed intermediates seen so far
2583 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002584 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002585 *
2586 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002587 * - 0 on success
2588 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002589 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002590static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 mbedtls_x509_crt *child,
2592 mbedtls_x509_crt *candidates,
2593 mbedtls_x509_crt **r_parent,
2594 int *r_signature_is_good,
2595 int top,
2596 unsigned path_cnt,
2597 unsigned self_cnt,
2598 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002599{
Janos Follath865b3eb2019-12-16 11:46:15 +00002600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002601 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002602 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002603
2604#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002605 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002607 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002608 parent = rs_ctx->parent;
2609 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002610 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002611
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002612 /* clear saved state */
2613 rs_ctx->parent = NULL;
2614 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002615 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002616
2617 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002618 goto check_signature;
2619 }
2620#endif
2621
2622 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002623 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002626 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002628 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002630
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002631 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (parent->max_pathlen > 0 &&
2633 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002634 continue;
2635 }
2636
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002637 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002638#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2639check_signature:
2640#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002642
2643#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002645 /* save state */
2646 rs_ctx->parent = parent;
2647 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002648 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002649
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002651 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002652#else
2653 (void) ret;
2654#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002655
2656 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002658 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002660
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002661 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2663 mbedtls_x509_time_is_future(&parent->valid_from)) {
2664 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002665 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002666 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002667 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002668
2669 continue;
2670 }
2671
Andy Gross1f627142019-01-30 10:25:53 -06002672 *r_parent = parent;
2673 *r_signature_is_good = signature_is_good;
2674
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002675 break;
2676 }
2677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002679 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002680 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002681 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002684}
2685
2686/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002687 * Find a parent in trusted CAs or the provided chain, or return NULL.
2688 *
2689 * Searches in trusted CAs first, and return the first suitable parent found
2690 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002691 *
2692 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002693 * - [in] child: certificate for which we're looking for a parent, followed
2694 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002695 * - [in] trust_ca: list of locally trusted certificates
2696 * - [out] parent: parent found (or NULL)
2697 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2698 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2699 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2700 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002701 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002702 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002703 *
2704 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002705 * - 0 on success
2706 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002707 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002708static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 mbedtls_x509_crt *child,
2710 mbedtls_x509_crt *trust_ca,
2711 mbedtls_x509_crt **parent,
2712 int *parent_is_trusted,
2713 int *signature_is_good,
2714 unsigned path_cnt,
2715 unsigned self_cnt,
2716 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002717{
Janos Follath865b3eb2019-12-16 11:46:15 +00002718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002719 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002720
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002721 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002722
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002723#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002724 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002726 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002727 rs_ctx->parent_is_trusted = -1;
2728 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002729#endif
2730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002732 search_list = *parent_is_trusted ? trust_ca : child->next;
2733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 ret = x509_crt_find_parent_in(child, search_list,
2735 parent, signature_is_good,
2736 *parent_is_trusted,
2737 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002738
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002739#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002741 /* save state */
2742 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002744 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002745#else
2746 (void) ret;
2747#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002748
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002749 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002751 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002753
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002754 /* prepare second iteration */
2755 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002756 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002757
2758 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002760 *parent_is_trusted = 0;
2761 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002762 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002763
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002765}
2766
2767/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002768 * Check if an end-entity certificate is locally trusted
2769 *
2770 * Currently we require such certificates to be self-signed (actually only
2771 * check for self-issued as self-signatures are not checked)
2772 */
2773static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 mbedtls_x509_crt *crt,
2775 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002776{
2777 mbedtls_x509_crt *cur;
2778
2779 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2781 return -1;
2782 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002783
2784 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2786 if (crt->raw.len == cur->raw.len &&
2787 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2788 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002789 }
2790 }
2791
2792 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002794}
2795
2796/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002797 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002798 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002799 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2800 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002801 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002802 * such that every cert in the chain is a child of the next one,
2803 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002804 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002805 * Verify that chain and return it with flags for all issues found.
2806 *
2807 * Special cases:
2808 * - EE == Rj -> return a one-element list containing it
2809 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2810 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002811 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002812 * Tests for (aspects of) this function should include at least:
2813 * - trusted EE
2814 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002815 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002816 * - if relevant: EE untrusted
2817 * - if relevant: EE -> intermediate, untrusted
2818 * with the aspect under test checked at each relevant level (EE, int, root).
2819 * For some aspects longer chains are required, but usually length 2 is
2820 * enough (but length 1 is not in general).
2821 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002822 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002823 * - [in] crt: the cert list EE, C1, ..., Cn
2824 * - [in] trust_ca: the trusted list R1, ..., Rp
2825 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002826 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002827 * Only valid when return value is 0, may contain garbage otherwise!
2828 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002829 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002830 *
2831 * Return value:
2832 * - non-zero if the chain could not be fully built and examined
2833 * - 0 is the chain was successfully built and examined,
2834 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002835 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002836static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 mbedtls_x509_crt *crt,
2838 mbedtls_x509_crt *trust_ca,
2839 mbedtls_x509_crl *ca_crl,
2840 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2841 void *p_ca_cb,
2842 const mbedtls_x509_crt_profile *profile,
2843 mbedtls_x509_crt_verify_chain *ver_chain,
2844 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002845{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002846 /* Don't initialize any of those variables here, so that the compiler can
2847 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002849 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002850 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002851 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002852 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002853 int parent_is_trusted;
2854 int child_is_trusted;
2855 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002856 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002857 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002858
2859#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2860 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002862 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002863 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002864 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002865
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002866 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002867 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002868 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002869 flags = &cur->flags;
2870
2871 goto find_parent;
2872 }
2873#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002874
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002875 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002876 self_cnt = 0;
2877 parent_is_trusted = 0;
2878 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002881 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002882 cur = &ver_chain->items[ver_chain->len];
2883 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002884 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002885 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002886 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002887
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002888 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002890 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002894 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002896
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002897 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (child_is_trusted) {
2899 return 0;
2900 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002901
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002902 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002904 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002908 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002910
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002911 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 if (ver_chain->len == 1 &&
2913 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2914 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002915 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002916
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002917#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2918find_parent:
2919#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002920
2921 /* Obtain list of potential trusted signers from CA callback,
2922 * or use statically provided list. */
2923#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 if (f_ca_cb != NULL) {
2925 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2926 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002927 ver_chain->trust_ca_cb_result = NULL;
2928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2930 if (ret != 0) {
2931 return MBEDTLS_ERR_X509_FATAL_ERROR;
2932 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002933
2934 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002936#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2937 {
2938 ((void) f_ca_cb);
2939 ((void) p_ca_cb);
2940 cur_trust_ca = trust_ca;
2941 }
2942
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002943 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2945 &parent_is_trusted, &signature_is_good,
2946 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002947
2948#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002950 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002951 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002952 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002953 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002954
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002956 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002957#else
2958 (void) ret;
2959#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002960
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002961 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002963 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002966
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002967 /* Count intermediate self-issued (not necessarily self-signed) certs.
2968 * These can occur with some strategies for key rollover, see [SIRO],
2969 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 if (ver_chain->len != 1 &&
2971 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002972 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002973 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002974
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 /* path_cnt is 0 for the first intermediate CA,
2976 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 if (!parent_is_trusted &&
2978 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002979 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002981 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002982
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002983 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002985 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002987
2988 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002990 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002994 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002996#else
2997 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002998#endif
2999
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003000 /* prepare for next iteration */
3001 child = parent;
3002 parent = NULL;
3003 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003004 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003005 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003006}
3007
3008/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003009 * Check for CN match
3010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011static int x509_crt_check_cn(const mbedtls_x509_buf *name,
3012 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003013{
3014 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (name->len == cn_len &&
3016 x509_memcasecmp(cn, name->p, cn_len) == 0) {
3017 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003018 }
3019
3020 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 if (x509_check_wildcard(cn, name) == 0) {
3022 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003023 }
3024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003026}
3027
3028/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003029 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003031static int x509_crt_check_san(const mbedtls_x509_buf *name,
3032 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003033{
3034 const unsigned char san_type = (unsigned char) name->tag &
3035 MBEDTLS_ASN1_TAG_VALUE_MASK;
3036
3037 /* dNSName */
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
3039 return x509_crt_check_cn(name, cn, cn_len);
3040 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003041
3042 /* (We may handle other types here later.) */
3043
3044 /* Unrecognized type */
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003046}
3047
3048/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003049 * Verify the requested CN - only call this if cn is not NULL!
3050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003051static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3052 const char *cn,
3053 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003054{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003055 const mbedtls_x509_name *name;
3056 const mbedtls_x509_sequence *cur;
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
3060 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
3061 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003062 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003063 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003064 }
3065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003067 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 }
3069 } else {
3070 for (name = &crt->subject; name != NULL; name = name->next) {
3071 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3072 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
3073 break;
3074 }
3075 }
3076
3077 if (name == NULL) {
3078 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3079 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003080 }
3081}
3082
3083/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003084 * Merge the flags for all certs in the chain, after calling callback
3085 */
3086static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 uint32_t *flags,
3088 const mbedtls_x509_crt_verify_chain *ver_chain,
3089 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3090 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003091{
Janos Follath865b3eb2019-12-16 11:46:15 +00003092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003093 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003094 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003095 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003098 cur = &ver_chain->items[i-1];
3099 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 if (NULL != f_vrfy) {
3102 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3103 return ret;
3104 }
3105 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003106
3107 *flags |= cur_flags;
3108 }
3109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003111}
3112
3113/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003114 * Verify the certificate validity, with profile, restartable version
3115 *
3116 * This function:
3117 * - checks the requested CN (if any)
3118 * - checks the type and size of the EE cert's key,
3119 * as that isn't done as part of chain building/verification currently
3120 * - builds and verifies the chain
3121 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003122 *
3123 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3124 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3125 * verification routine to search for trusted signers, and CRLs will
3126 * be disabled. Otherwise, `trust_ca` will be used as the static list
3127 * of trusted signers, and `ca_crl` will be use as the static list
3128 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3131 mbedtls_x509_crt *trust_ca,
3132 mbedtls_x509_crl *ca_crl,
3133 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3134 void *p_ca_cb,
3135 const mbedtls_x509_crt_profile *profile,
3136 const char *cn, uint32_t *flags,
3137 int (*f_vrfy)(void *,
3138 mbedtls_x509_crt *,
3139 int,
3140 uint32_t *),
3141 void *p_vrfy,
3142 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003143{
Janos Follath865b3eb2019-12-16 11:46:15 +00003144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003145 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003146 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003147 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003148
3149 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003150 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003154 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3155 goto exit;
3156 }
3157
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003158 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 if (cn != NULL) {
3160 x509_crt_verify_name(crt, cn, &ee_flags);
3161 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003162
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003163 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003165
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003167 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003169
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003171 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003173
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003174 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3176 f_ca_cb, p_ca_cb, profile,
3177 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003180 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003182
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003183 /* Merge end-entity flags */
3184 ver_chain.items[0].flags |= ee_flags;
3185
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003186 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003188
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003189exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003190
3191#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3193 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003194 ver_chain.trust_ca_cb_result = NULL;
3195#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3196
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003197#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3199 mbedtls_x509_crt_restart_free(rs_ctx);
3200 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003201#endif
3202
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003203 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3204 * the SSL module for authmode optional, but non-zero return from the
3205 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003207 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003208 }
3209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 if (ret != 0) {
3211 *flags = (uint32_t) -1;
3212 return ret;
3213 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 if (*flags != 0) {
3216 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3217 }
3218
3219 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003220}
3221
Hanno Becker3116fb32019-03-28 13:34:42 +00003222
3223/*
3224 * Verify the certificate validity (default profile, not restartable)
3225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3227 mbedtls_x509_crt *trust_ca,
3228 mbedtls_x509_crl *ca_crl,
3229 const char *cn, uint32_t *flags,
3230 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3231 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003232{
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3234 NULL, NULL,
3235 &mbedtls_x509_crt_profile_default,
3236 cn, flags,
3237 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003238}
3239
3240/*
3241 * Verify the certificate validity (user-chosen profile, not restartable)
3242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003243int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3244 mbedtls_x509_crt *trust_ca,
3245 mbedtls_x509_crl *ca_crl,
3246 const mbedtls_x509_crt_profile *profile,
3247 const char *cn, uint32_t *flags,
3248 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3249 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003250{
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3252 NULL, NULL,
3253 profile, cn, flags,
3254 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003255}
3256
3257#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3258/*
3259 * Verify the certificate validity (user-chosen profile, CA callback,
3260 * not restartable).
3261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003262int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3263 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3264 void *p_ca_cb,
3265 const mbedtls_x509_crt_profile *profile,
3266 const char *cn, uint32_t *flags,
3267 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3268 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003269{
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3271 f_ca_cb, p_ca_cb,
3272 profile, cn, flags,
3273 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003274}
3275#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3278 mbedtls_x509_crt *trust_ca,
3279 mbedtls_x509_crl *ca_crl,
3280 const mbedtls_x509_crt_profile *profile,
3281 const char *cn, uint32_t *flags,
3282 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3283 void *p_vrfy,
3284 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003285{
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3287 NULL, NULL,
3288 profile, cn, flags,
3289 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003290}
3291
3292
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003293/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003294 * Initialize a certificate chain
3295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003296void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003297{
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003299}
3300
3301/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003302 * Unallocate all certificate data
3303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003304void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003305{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 mbedtls_x509_crt *cert_cur = crt;
3307 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003308
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 while (cert_cur != NULL) {
3310 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003314#endif
3315
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3317 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3318 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3319 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3320 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003321
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3323 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3324 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003325 }
3326
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003327 cert_prv = cert_cur;
3328 cert_cur = cert_cur->next;
3329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3331 if (cert_prv != crt) {
3332 mbedtls_free(cert_prv);
3333 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003334 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003335}
3336
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003337#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3338/*
3339 * Initialize a restart context
3340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003342{
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003344
3345 ctx->parent = NULL;
3346 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003347 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003348
3349 ctx->parent_is_trusted = -1;
3350
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003351 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003352 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003354}
3355
3356/*
3357 * Free the components of a restart context
3358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003359void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003360{
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003362 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003364
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 mbedtls_pk_restart_free(&ctx->pk);
3366 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003367}
3368#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370#endif /* MBEDTLS_X509_CRT_PARSE_C */