blob: bc7818a01285b087b8e3049082395d172a10027b [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 */
David Horstmann8b6068b2023-01-05 15:42:32 +000092#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. */
David Horstmann8b6068b2023-01-05 15:42:32 +0000100 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. */
David Horstmann8b6068b2023-01-05 15:42:32 +0000107 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. */
David Horstmann8b6068b2023-01-05 15:42:32 +0000125 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. */
David Horstmann8b6068b2023-01-05 15:42:32 +0000131 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000150 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000153 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000157 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000180static 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{
David Horstmann8b6068b2023-01-05 15:42:32 +0000183 if (md_alg == MBEDTLS_MD_NONE) {
184 return -1;
185 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200186
David Horstmann8b6068b2023-01-05 15:42:32 +0000187 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000191 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000198static 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{
David Horstmann8b6068b2023-01-05 15:42:32 +0000201 if (pk_alg == MBEDTLS_PK_NONE) {
202 return -1;
203 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200204
David Horstmann8b6068b2023-01-05 15:42:32 +0000205 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000209 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000216static 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{
David Horstmann8b6068b2023-01-05 15:42:32 +0000219 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)
David Horstmann8b6068b2023-01-05 15:42:32 +0000222 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000227 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)
David Horstmann8b6068b2023-01-05 15:42:32 +0000232 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200233 pk_alg == MBEDTLS_PK_ECKEY ||
David Horstmann8b6068b2023-01-05 15:42:32 +0000234 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000237 if (gid == MBEDTLS_ECP_DP_NONE) {
238 return -1;
239 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200240
David Horstmann8b6068b2023-01-05 15:42:32 +0000241 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
242 return 0;
243 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200244
David Horstmann8b6068b2023-01-05 15:42:32 +0000245 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200246 }
247#endif
248
David Horstmann8b6068b2023-01-05 15:42:32 +0000249 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000255static 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000261 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000262 diff = n1[i] ^ n2[i];
263
David Horstmann8b6068b2023-01-05 15:42:32 +0000264 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000265 continue;
266 }
267
David Horstmann8b6068b2023-01-05 15:42:32 +0000268 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000277 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000278}
279
280/*
281 * Return 0 if name matches wildcard, -1 otherwise
282 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000283static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000284{
285 size_t i;
David Horstmann8b6068b2023-01-05 15:42:32 +0000286 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000289 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
290 return -1;
291 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000292
David Horstmann8b6068b2023-01-05 15:42:32 +0000293 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000300 if (cn_idx == 0) {
301 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000302 }
303
David Horstmann8b6068b2023-01-05 15:42:32 +0000304 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000318static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000319{
David Horstmann8b6068b2023-01-05 15:42:32 +0000320 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000321 a->len == b->len &&
David Horstmann8b6068b2023-01-05 15:42:32 +0000322 memcmp(a->p, b->p, b->len) == 0) {
323 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000324 }
325
David Horstmann8b6068b2023-01-05 15:42:32 +0000326 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 &&
David Horstmann8b6068b2023-01-05 15:42:32 +0000329 x509_memcasecmp(a->p, b->p, b->len) == 0) {
330 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000331 }
332
David Horstmann8b6068b2023-01-05 15:42:32 +0000333 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000346static 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000349 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000355 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000356 a->oid.len != b->oid.len ||
David Horstmann8b6068b2023-01-05 15:42:32 +0000357 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000362 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000367 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000376 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(
David Horstmann8b6068b2023-01-05 15:42:32 +0000383 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200384{
385 size_t i;
386
David Horstmann8b6068b2023-01-05 15:42:32 +0000387 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 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000402static 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000409 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;
David Horstmann8b6068b2023-01-05 15:42:32 +0000414 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415 }
416
David Horstmann8b6068b2023-01-05 15:42:32 +0000417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 }
419
420 end = *p + len;
421
David Horstmann8b6068b2023-01-05 15:42:32 +0000422 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000426 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000431 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432}
433
434/*
435 * Validity ::= SEQUENCE {
436 * notBefore Time,
437 * notAfter Time }
438 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000439static 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000447 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000454 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
455 return ret;
456 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
David Horstmann8b6068b2023-01-05 15:42:32 +0000458 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
459 return ret;
460 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
David Horstmann8b6068b2023-01-05 15:42:32 +0000462 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000467 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468}
469
470/*
471 * X.509 v2/v3 unique identifier (not parsed)
472 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000473static 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000479 if (*p == end) {
480 return 0;
481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
483 uid->tag = **p;
484
David Horstmann8b6068b2023-01-05 15:42:32 +0000485 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000492 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000498 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499}
500
David Horstmann8b6068b2023-01-05 15:42:32 +0000501static 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000517 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000522 if (*p == end) {
523 return 0;
524 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525
David Horstmann8b6068b2023-01-05 15:42:32 +0000526 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000531 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. */
David Horstmann8b6068b2023-01-05 15:42:32 +0000555 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000562 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563}
564
David Horstmann8b6068b2023-01-05 15:42:32 +0000565static int 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000572 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
David Horstmann8b6068b2023-01-05 15:42:32 +0000576 if (bs.len != 1) {
577 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
578 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
579 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
581 /* Get actual bitstring */
582 *ns_cert_type = *bs.p;
David Horstmann8b6068b2023-01-05 15:42:32 +0000583 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584}
585
David Horstmann8b6068b2023-01-05 15:42:32 +0000586static int x509_get_key_usage(unsigned char **p,
587 const unsigned char *end,
588 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589{
Janos Follath865b3eb2019-12-16 11:46:15 +0000590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200591 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
David Horstmann8b6068b2023-01-05 15:42:32 +0000594 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
595 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
596 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597
David Horstmann8b6068b2023-01-05 15:42:32 +0000598 if (bs.len < 1) {
599 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
600 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
601 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602
603 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200604 *key_usage = 0;
David Horstmann8b6068b2023-01-05 15:42:32 +0000605 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200606 *key_usage |= (unsigned int) bs.p[i] << (8*i);
607 }
608
David Horstmann8b6068b2023-01-05 15:42:32 +0000609 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610}
611
612/*
613 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
614 *
615 * KeyPurposeId ::= OBJECT IDENTIFIER
616 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000617static int x509_get_ext_key_usage(unsigned char **p,
618 const unsigned char *end,
619 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620{
Janos Follath865b3eb2019-12-16 11:46:15 +0000621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622
David Horstmann8b6068b2023-01-05 15:42:32 +0000623 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
624 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
625 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626
627 /* Sequence length must be >= 1 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000628 if (ext_key_usage->buf.p == NULL) {
629 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
630 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
631 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632
David Horstmann8b6068b2023-01-05 15:42:32 +0000633 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634}
635
636/*
637 * SubjectAltName ::= GeneralNames
638 *
639 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
640 *
641 * GeneralName ::= CHOICE {
642 * otherName [0] OtherName,
643 * rfc822Name [1] IA5String,
644 * dNSName [2] IA5String,
645 * x400Address [3] ORAddress,
646 * directoryName [4] Name,
647 * ediPartyName [5] EDIPartyName,
648 * uniformResourceIdentifier [6] IA5String,
649 * iPAddress [7] OCTET STRING,
650 * registeredID [8] OBJECT IDENTIFIER }
651 *
652 * OtherName ::= SEQUENCE {
653 * type-id OBJECT IDENTIFIER,
654 * value [0] EXPLICIT ANY DEFINED BY type-id }
655 *
656 * EDIPartyName ::= SEQUENCE {
657 * nameAssigner [0] DirectoryString OPTIONAL,
658 * partyName [1] DirectoryString }
659 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300660 * NOTE: we list all types, but only use dNSName and otherName
661 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000663static int x509_get_subject_alt_name(unsigned char **p,
664 const unsigned char *end,
665 mbedtls_x509_sequence *subject_alt_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666{
Janos Follath865b3eb2019-12-16 11:46:15 +0000667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
671 /* Get main sequence tag */
David Horstmann8b6068b2023-01-05 15:42:32 +0000672 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
673 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
674 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
675 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676
David Horstmann8b6068b2023-01-05 15:42:32 +0000677 if (*p + len != end) {
678 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
679 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
680 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
David Horstmann8b6068b2023-01-05 15:42:32 +0000682 while (*p < end) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300683 mbedtls_x509_subject_alternative_name dummy_san_buf;
Hanno Becker13a35242019-09-13 12:24:56 +0100684 mbedtls_x509_buf tmp_san_buf;
David Horstmann8b6068b2023-01-05 15:42:32 +0000685 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
Ron Eldordbbd9662019-05-15 15:46:03 +0300686
Hanno Becker13a35242019-09-13 12:24:56 +0100687 tmp_san_buf.tag = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688 (*p)++;
Hanno Becker13a35242019-09-13 12:24:56 +0100689
David Horstmann8b6068b2023-01-05 15:42:32 +0000690 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
691 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
692 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693
Hanno Becker13a35242019-09-13 12:24:56 +0100694 tmp_san_buf.p = *p;
695 tmp_san_buf.len = tag_len;
696
697 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
David Horstmann8b6068b2023-01-05 15:42:32 +0000698 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
699 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
700 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100701 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
Ron Eldordbbd9662019-05-15 15:46:03 +0300703 /*
irwir74aee1c2019-09-21 18:21:48 +0300704 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300705 */
Hanno Becker13a35242019-09-13 12:24:56 +0100706 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &dummy_san_buf);
Ron Eldordbbd9662019-05-15 15:46:03 +0300707 /*
708 * In case the extension is malformed, return an error,
709 * and clear the allocated sequences.
710 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000711 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
712 mbedtls_asn1_sequence_free(subject_alt_name->next);
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300713 subject_alt_name->next = NULL;
David Horstmann8b6068b2023-01-05 15:42:32 +0000714 return ret;
Ron Eldordbbd9662019-05-15 15:46:03 +0300715 }
716
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717 /* Allocate and assign next pointer */
David Horstmann8b6068b2023-01-05 15:42:32 +0000718 if (cur->buf.p != NULL) {
719 if (cur->next != NULL) {
720 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
721 }
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100722
David Horstmann8b6068b2023-01-05 15:42:32 +0000723 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724
David Horstmann8b6068b2023-01-05 15:42:32 +0000725 if (cur->next == NULL) {
726 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
727 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
728 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730 cur = cur->next;
731 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200732
Hanno Becker13a35242019-09-13 12:24:56 +0100733 cur->buf = tmp_san_buf;
734 *p += tmp_san_buf.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 }
736
737 /* Set final sequence entry's next pointer to NULL */
738 cur->next = NULL;
739
David Horstmann8b6068b2023-01-05 15:42:32 +0000740 if (*p != end) {
741 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
742 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
743 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744
David Horstmann8b6068b2023-01-05 15:42:32 +0000745 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746}
747
748/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200749 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
750 *
751 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
752 *
753 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
754 *
755 * PolicyInformation ::= SEQUENCE {
756 * policyIdentifier CertPolicyId,
757 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
758 * PolicyQualifierInfo OPTIONAL }
759 *
760 * CertPolicyId ::= OBJECT IDENTIFIER
761 *
762 * PolicyQualifierInfo ::= SEQUENCE {
763 * policyQualifierId PolicyQualifierId,
764 * qualifier ANY DEFINED BY policyQualifierId }
765 *
766 * -- policyQualifierIds for Internet policy qualifiers
767 *
768 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
769 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
770 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
771 *
772 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
773 *
774 * Qualifier ::= CHOICE {
775 * cPSuri CPSuri,
776 * userNotice UserNotice }
777 *
778 * CPSuri ::= IA5String
779 *
780 * UserNotice ::= SEQUENCE {
781 * noticeRef NoticeReference OPTIONAL,
782 * explicitText DisplayText OPTIONAL }
783 *
784 * NoticeReference ::= SEQUENCE {
785 * organization DisplayText,
786 * noticeNumbers SEQUENCE OF INTEGER }
787 *
788 * DisplayText ::= CHOICE {
789 * ia5String IA5String (SIZE (1..200)),
790 * visibleString VisibleString (SIZE (1..200)),
791 * bmpString BMPString (SIZE (1..200)),
792 * utf8String UTF8String (SIZE (1..200)) }
793 *
794 * NOTE: we only parse and use anyPolicy without qualifiers at this point
795 * as defined in RFC 5280.
796 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000797static int x509_get_certificate_policies(unsigned char **p,
798 const unsigned char *end,
799 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200800{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300801 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200802 size_t len;
803 mbedtls_asn1_buf *buf;
804 mbedtls_asn1_sequence *cur = certificate_policies;
805
806 /* Get main sequence tag */
David Horstmann8b6068b2023-01-05 15:42:32 +0000807 ret = mbedtls_asn1_get_tag(p, end, &len,
808 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
809 if (ret != 0) {
810 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
811 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200812
David Horstmann8b6068b2023-01-05 15:42:32 +0000813 if (*p + len != end) {
814 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
815 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
816 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200817
818 /*
819 * Cannot be an empty sequence.
820 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000821 if (len == 0) {
822 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
823 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
824 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200825
David Horstmann8b6068b2023-01-05 15:42:32 +0000826 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200827 mbedtls_x509_buf policy_oid;
828 const unsigned char *policy_end;
829
830 /*
831 * Get the policy sequence
832 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000833 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
834 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
835 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
836 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200837
838 policy_end = *p + len;
839
David Horstmann8b6068b2023-01-05 15:42:32 +0000840 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
841 MBEDTLS_ASN1_OID)) != 0) {
842 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
843 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200844
845 policy_oid.tag = MBEDTLS_ASN1_OID;
846 policy_oid.len = len;
847 policy_oid.p = *p;
848
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300849 /*
850 * Only AnyPolicy is currently supported when enforcing policy.
851 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000852 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300853 /*
854 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200855 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300856 */
857 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
858 }
859
Ron Eldor74d9acc2019-03-21 14:00:03 +0200860 /* Allocate and assign next pointer */
David Horstmann8b6068b2023-01-05 15:42:32 +0000861 if (cur->buf.p != NULL) {
862 if (cur->next != NULL) {
863 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
864 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200865
David Horstmann8b6068b2023-01-05 15:42:32 +0000866 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200867
David Horstmann8b6068b2023-01-05 15:42:32 +0000868 if (cur->next == NULL) {
869 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
870 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
871 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200872
873 cur = cur->next;
874 }
875
David Horstmann8b6068b2023-01-05 15:42:32 +0000876 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200877 buf->tag = policy_oid.tag;
878 buf->p = policy_oid.p;
879 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300880
881 *p += len;
882
David Horstmann8b6068b2023-01-05 15:42:32 +0000883 /*
884 * If there is an optional qualifier, then *p < policy_end
885 * Check the Qualifier len to verify it doesn't exceed policy_end.
886 */
887 if (*p < policy_end) {
888 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
889 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
890 0) {
891 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
892 }
Ron Eldor08063792019-05-13 16:38:39 +0300893 /*
894 * Skip the optional policy qualifiers.
895 */
896 *p += len;
897 }
898
David Horstmann8b6068b2023-01-05 15:42:32 +0000899 if (*p != policy_end) {
900 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
901 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
902 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200903 }
904
905 /* Set final sequence entry's next pointer to NULL */
906 cur->next = NULL;
907
David Horstmann8b6068b2023-01-05 15:42:32 +0000908 if (*p != end) {
909 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
910 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
911 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200912
David Horstmann8b6068b2023-01-05 15:42:32 +0000913 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200914}
915
916/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917 * X.509 v3 extensions
918 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000920static int x509_get_crt_ext(unsigned char **p,
921 const unsigned char *end,
922 mbedtls_x509_crt *crt,
923 mbedtls_x509_crt_ext_cb_t cb,
924 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925{
Janos Follath865b3eb2019-12-16 11:46:15 +0000926 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200928 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929
David Horstmann8b6068b2023-01-05 15:42:32 +0000930 if (*p == end) {
931 return 0;
932 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000933
David Horstmann8b6068b2023-01-05 15:42:32 +0000934 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
935 return ret;
936 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937
Hanno Becker12f62fb2019-02-12 17:22:36 +0000938 end = crt->v3_ext.p + crt->v3_ext.len;
David Horstmann8b6068b2023-01-05 15:42:32 +0000939 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940 /*
941 * Extension ::= SEQUENCE {
942 * extnID OBJECT IDENTIFIER,
943 * critical BOOLEAN DEFAULT FALSE,
944 * extnValue OCTET STRING }
945 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000946 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200947 int is_critical = 0; /* DEFAULT FALSE */
948 int ext_type = 0;
949
David Horstmann8b6068b2023-01-05 15:42:32 +0000950 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
951 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
952 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
953 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955 end_ext_data = *p + len;
956
957 /* Get extension ID */
David Horstmann8b6068b2023-01-05 15:42:32 +0000958 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
959 MBEDTLS_ASN1_OID)) != 0) {
960 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
961 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962
k-stachowiak463928a2018-07-24 12:50:59 +0200963 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 extn_oid.p = *p;
965 *p += extn_oid.len;
966
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 /* Get optional critical */
David Horstmann8b6068b2023-01-05 15:42:32 +0000968 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
969 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
970 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
971 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972
973 /* Data should be octet string type */
David Horstmann8b6068b2023-01-05 15:42:32 +0000974 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
975 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
976 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
977 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200979 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 end_ext_octet = *p + len;
981
David Horstmann8b6068b2023-01-05 15:42:32 +0000982 if (end_ext_octet != end_ext_data) {
983 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
984 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
985 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986
987 /*
988 * Detect supported extensions
989 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000990 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
David Horstmann8b6068b2023-01-05 15:42:32 +0000992 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200993 /* Give the callback (if any) a chance to handle the extension */
David Horstmann8b6068b2023-01-05 15:42:32 +0000994 if (cb != NULL) {
995 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
996 if (ret != 0 && is_critical) {
997 return ret;
998 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200999 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001000 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +02001001 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001002
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 /* No parser found, skip extension */
1004 *p = end_ext_octet;
1005
David Horstmann8b6068b2023-01-05 15:42:32 +00001006 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 /* Data is marked as critical: fail */
David Horstmann8b6068b2023-01-05 15:42:32 +00001008 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1009 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 continue;
1012 }
1013
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001014 /* Forbid repeated extensions */
David Horstmann8b6068b2023-01-05 15:42:32 +00001015 if ((crt->ext_types & ext_type) != 0) {
1016 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1017 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001018
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001019 crt->ext_types |= ext_type;
1020
David Horstmann8b6068b2023-01-05 15:42:32 +00001021 switch (ext_type) {
1022 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
1023 /* Parse basic constraints */
1024 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
1025 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
1026 return ret;
1027 }
1028 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029
David Horstmann8b6068b2023-01-05 15:42:32 +00001030 case MBEDTLS_X509_EXT_KEY_USAGE:
1031 /* Parse key usage */
1032 if ((ret = x509_get_key_usage(p, end_ext_octet,
1033 &crt->key_usage)) != 0) {
1034 return ret;
1035 }
1036 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
David Horstmann8b6068b2023-01-05 15:42:32 +00001038 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1039 /* Parse extended key usage */
1040 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1041 &crt->ext_key_usage)) != 0) {
1042 return ret;
1043 }
1044 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045
David Horstmann8b6068b2023-01-05 15:42:32 +00001046 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1047 /* Parse subject alt name */
1048 if ((ret = x509_get_subject_alt_name(p, end_ext_octet,
1049 &crt->subject_alt_names)) != 0) {
1050 return ret;
1051 }
1052 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053
David Horstmann8b6068b2023-01-05 15:42:32 +00001054 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1055 /* Parse netscape certificate type */
1056 if ((ret = x509_get_ns_cert_type(p, end_ext_octet,
1057 &crt->ns_cert_type)) != 0) {
1058 return ret;
1059 }
1060 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001061
David Horstmann8b6068b2023-01-05 15:42:32 +00001062 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1063 /* Parse certificate policies type */
1064 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1065 &crt->certificate_policies)) != 0) {
1066 /* Give the callback (if any) a chance to handle the extension
1067 * if it contains unsupported policies */
1068 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1069 cb(p_ctx, crt, &extn_oid, is_critical,
1070 start_ext_octet, end_ext_octet) == 0) {
1071 break;
1072 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001073
David Horstmann8b6068b2023-01-05 15:42:32 +00001074 if (is_critical) {
1075 return ret;
1076 } else
1077 /*
1078 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1079 * cannot interpret or enforce the policy. However, it is up to
1080 * the user to choose how to enforce the policies,
1081 * unless the extension is critical.
1082 */
1083 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1084 return ret;
1085 }
1086 }
1087 break;
1088
1089 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001090 /*
David Horstmann8b6068b2023-01-05 15:42:32 +00001091 * If this is a non-critical extension, which the oid layer
1092 * supports, but there isn't an x509 parser for it,
1093 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001094 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001095 if (is_critical) {
1096 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1097 } else {
1098 *p = end_ext_octet;
1099 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100 }
1101 }
1102
David Horstmann8b6068b2023-01-05 15:42:32 +00001103 if (*p != end) {
1104 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1105 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107
David Horstmann8b6068b2023-01-05 15:42:32 +00001108 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109}
1110
1111/*
1112 * Parse and fill a single X.509 certificate in DER format
1113 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001114static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1115 const unsigned char *buf,
1116 size_t buflen,
1117 int make_copy,
1118 mbedtls_x509_crt_ext_cb_t cb,
1119 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120{
Janos Follath865b3eb2019-12-16 11:46:15 +00001121 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001122 size_t len;
1123 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001125
David Horstmann8b6068b2023-01-05 15:42:32 +00001126 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1127 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1128 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129
1130 /*
1131 * Check for valid input
1132 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001133 if (crt == NULL || buf == NULL) {
1134 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1135 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001136
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001137 /* Use the original buffer until we figure out actual length. */
David Horstmann8b6068b2023-01-05 15:42:32 +00001138 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001139 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 end = p + len;
1141
1142 /*
1143 * Certificate ::= SEQUENCE {
1144 * tbsCertificate TBSCertificate,
1145 * signatureAlgorithm AlgorithmIdentifier,
1146 * signatureValue BIT STRING }
1147 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001148 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1149 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1150 mbedtls_x509_crt_free(crt);
1151 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001152 }
1153
Janos Follathcc0e49d2016-02-17 14:34:12 +00001154 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001155 crt->raw.len = crt_end - buf;
David Horstmann8b6068b2023-01-05 15:42:32 +00001156 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001157 /* Create and populate a new buffer for the raw field. */
David Horstmann8b6068b2023-01-05 15:42:32 +00001158 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1159 if (crt->raw.p == NULL) {
1160 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1161 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001162
David Horstmann8b6068b2023-01-05 15:42:32 +00001163 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001164 crt->own_buffer = 1;
1165
1166 p += crt->raw.len - len;
1167 end = crt_end = p + len;
David Horstmann8b6068b2023-01-05 15:42:32 +00001168 } else {
1169 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001170 crt->own_buffer = 0;
1171 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001172
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 /*
1174 * TBSCertificate ::= SEQUENCE {
1175 */
1176 crt->tbs.p = p;
1177
David Horstmann8b6068b2023-01-05 15:42:32 +00001178 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1179 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1180 mbedtls_x509_crt_free(crt);
1181 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 }
1183
1184 end = p + len;
1185 crt->tbs.len = end - crt->tbs.p;
1186
1187 /*
1188 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1189 *
1190 * CertificateSerialNumber ::= INTEGER
1191 *
1192 * signature AlgorithmIdentifier
1193 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001194 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1195 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1196 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1197 &sig_params1)) != 0) {
1198 mbedtls_x509_crt_free(crt);
1199 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 }
1201
David Horstmann8b6068b2023-01-05 15:42:32 +00001202 if (crt->version < 0 || crt->version > 2) {
1203 mbedtls_x509_crt_free(crt);
1204 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205 }
1206
Andres AG7ca4a032017-03-09 16:16:11 +00001207 crt->version++;
1208
David Horstmann8b6068b2023-01-05 15:42:32 +00001209 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1210 &crt->sig_md, &crt->sig_pk,
1211 &crt->sig_opts)) != 0) {
1212 mbedtls_x509_crt_free(crt);
1213 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214 }
1215
1216 /*
1217 * issuer Name
1218 */
1219 crt->issuer_raw.p = p;
1220
David Horstmann8b6068b2023-01-05 15:42:32 +00001221 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1222 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1223 mbedtls_x509_crt_free(crt);
1224 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 }
1226
David Horstmann8b6068b2023-01-05 15:42:32 +00001227 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1228 mbedtls_x509_crt_free(crt);
1229 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001230 }
1231
1232 crt->issuer_raw.len = p - crt->issuer_raw.p;
1233
1234 /*
1235 * Validity ::= SEQUENCE {
1236 * notBefore Time,
1237 * notAfter Time }
1238 *
1239 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001240 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1241 &crt->valid_to)) != 0) {
1242 mbedtls_x509_crt_free(crt);
1243 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001244 }
1245
1246 /*
1247 * subject Name
1248 */
1249 crt->subject_raw.p = p;
1250
David Horstmann8b6068b2023-01-05 15:42:32 +00001251 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1252 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1253 mbedtls_x509_crt_free(crt);
1254 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 }
1256
David Horstmann8b6068b2023-01-05 15:42:32 +00001257 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1258 mbedtls_x509_crt_free(crt);
1259 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260 }
1261
1262 crt->subject_raw.len = p - crt->subject_raw.p;
1263
1264 /*
1265 * SubjectPublicKeyInfo
1266 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001267 crt->pk_raw.p = p;
David Horstmann8b6068b2023-01-05 15:42:32 +00001268 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1269 mbedtls_x509_crt_free(crt);
1270 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001272 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273
1274 /*
1275 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1276 * -- If present, version shall be v2 or v3
1277 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1278 * -- If present, version shall be v2 or v3
1279 * extensions [3] EXPLICIT Extensions OPTIONAL
1280 * -- If present, version shall be v3
1281 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001282 if (crt->version == 2 || crt->version == 3) {
1283 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1284 if (ret != 0) {
1285 mbedtls_x509_crt_free(crt);
1286 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001287 }
1288 }
1289
David Horstmann8b6068b2023-01-05 15:42:32 +00001290 if (crt->version == 2 || crt->version == 3) {
1291 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1292 if (ret != 0) {
1293 mbedtls_x509_crt_free(crt);
1294 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 }
1296 }
1297
David Horstmann8b6068b2023-01-05 15:42:32 +00001298 if (crt->version == 3) {
1299 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1300 if (ret != 0) {
1301 mbedtls_x509_crt_free(crt);
1302 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 }
1304 }
1305
David Horstmann8b6068b2023-01-05 15:42:32 +00001306 if (p != end) {
1307 mbedtls_x509_crt_free(crt);
1308 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1309 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 }
1311
1312 end = crt_end;
1313
1314 /*
1315 * }
1316 * -- end of TBSCertificate
1317 *
1318 * signatureAlgorithm AlgorithmIdentifier,
1319 * signatureValue BIT STRING
1320 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001321 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1322 mbedtls_x509_crt_free(crt);
1323 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001324 }
1325
David Horstmann8b6068b2023-01-05 15:42:32 +00001326 if (crt->sig_oid.len != sig_oid2.len ||
1327 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001328 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001329 sig_params1.len != sig_params2.len ||
David Horstmann8b6068b2023-01-05 15:42:32 +00001330 (sig_params1.len != 0 &&
1331 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1332 mbedtls_x509_crt_free(crt);
1333 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334 }
1335
David Horstmann8b6068b2023-01-05 15:42:32 +00001336 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1337 mbedtls_x509_crt_free(crt);
1338 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001339 }
1340
David Horstmann8b6068b2023-01-05 15:42:32 +00001341 if (p != end) {
1342 mbedtls_x509_crt_free(crt);
1343 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1344 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001345 }
1346
David Horstmann8b6068b2023-01-05 15:42:32 +00001347 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001348}
1349
1350/*
1351 * Parse one X.509 certificate in DER format from a buffer and add them to a
1352 * chained list
1353 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001354static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1355 const unsigned char *buf,
1356 size_t buflen,
1357 int make_copy,
1358 mbedtls_x509_crt_ext_cb_t cb,
1359 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360{
Janos Follath865b3eb2019-12-16 11:46:15 +00001361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363
1364 /*
1365 * Check for valid input
1366 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001367 if (crt == NULL || buf == NULL) {
1368 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1369 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370
David Horstmann8b6068b2023-01-05 15:42:32 +00001371 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 prev = crt;
1373 crt = crt->next;
1374 }
1375
1376 /*
1377 * Add new certificate on the end of the chain if needed.
1378 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001379 if (crt->version != 0 && crt->next == NULL) {
1380 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381
David Horstmann8b6068b2023-01-05 15:42:32 +00001382 if (crt->next == NULL) {
1383 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1384 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385
1386 prev = crt;
David Horstmann8b6068b2023-01-05 15:42:32 +00001387 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001388 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389 }
1390
David Horstmann8b6068b2023-01-05 15:42:32 +00001391 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1392 if (ret != 0) {
1393 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394 prev->next = NULL;
David Horstmann8b6068b2023-01-05 15:42:32 +00001395 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396
David Horstmann8b6068b2023-01-05 15:42:32 +00001397 if (crt != chain) {
1398 mbedtls_free(crt);
1399 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001400
David Horstmann8b6068b2023-01-05 15:42:32 +00001401 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402 }
1403
David Horstmann8b6068b2023-01-05 15:42:32 +00001404 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405}
1406
David Horstmann8b6068b2023-01-05 15:42:32 +00001407int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1408 const unsigned char *buf,
1409 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001410{
David Horstmann8b6068b2023-01-05 15:42:32 +00001411 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001412}
1413
David Horstmann8b6068b2023-01-05 15:42:32 +00001414int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1415 const unsigned char *buf,
1416 size_t buflen,
1417 int make_copy,
1418 mbedtls_x509_crt_ext_cb_t cb,
1419 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001420{
David Horstmann8b6068b2023-01-05 15:42:32 +00001421 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001422}
1423
David Horstmann8b6068b2023-01-05 15:42:32 +00001424int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1425 const unsigned char *buf,
1426 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001427{
David Horstmann8b6068b2023-01-05 15:42:32 +00001428 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001429}
1430
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001432 * Parse one or more PEM certificates from a buffer and add them to the chained
1433 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001435int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1436 const unsigned char *buf,
1437 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438{
Janos Follath98e28a72016-05-31 14:03:54 +01001439#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001440 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001442#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443
1444 /*
1445 * Check for valid input
1446 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001447 if (chain == NULL || buf == NULL) {
1448 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1449 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001450
1451 /*
1452 * Determine buffer content. Buffer contains either one DER certificate or
1453 * one or more PEM certificates.
1454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#if defined(MBEDTLS_PEM_PARSE_C)
David Horstmann8b6068b2023-01-05 15:42:32 +00001456 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1457 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001459 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
David Horstmann8b6068b2023-01-05 15:42:32 +00001461 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1462 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1463 }
Janos Follath98e28a72016-05-31 14:03:54 +01001464#else
David Horstmann8b6068b2023-01-05 15:42:32 +00001465 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001466#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#if defined(MBEDTLS_PEM_PARSE_C)
David Horstmann8b6068b2023-01-05 15:42:32 +00001469 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001473 /* 1 rather than 0 since the terminating NULL byte is counted in */
David Horstmann8b6068b2023-01-05 15:42:32 +00001474 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 size_t use_len;
David Horstmann8b6068b2023-01-05 15:42:32 +00001476 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001478 /* If we get there, we know the string is null-terminated */
David Horstmann8b6068b2023-01-05 15:42:32 +00001479 ret = mbedtls_pem_read_buffer(&pem,
1480 "-----BEGIN CERTIFICATE-----",
1481 "-----END CERTIFICATE-----",
1482 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483
David Horstmann8b6068b2023-01-05 15:42:32 +00001484 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485 /*
1486 * Was PEM encoded
1487 */
1488 buflen -= use_len;
1489 buf += use_len;
David Horstmann8b6068b2023-01-05 15:42:32 +00001490 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1491 return ret;
1492 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1493 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494
1495 /*
1496 * PEM header and footer were found
1497 */
1498 buflen -= use_len;
1499 buf += use_len;
1500
David Horstmann8b6068b2023-01-05 15:42:32 +00001501 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502 first_error = ret;
David Horstmann8b6068b2023-01-05 15:42:32 +00001503 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504
Paul Bakker5a5fa922014-09-26 14:53:04 +02001505 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506 continue;
David Horstmann8b6068b2023-01-05 15:42:32 +00001507 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 break;
David Horstmann8b6068b2023-01-05 15:42:32 +00001509 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510
David Horstmann8b6068b2023-01-05 15:42:32 +00001511 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512
David Horstmann8b6068b2023-01-05 15:42:32 +00001513 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
David Horstmann8b6068b2023-01-05 15:42:32 +00001515 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 /*
1517 * Quit parsing on a memory error
1518 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001519 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1520 return ret;
1521 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522
David Horstmann8b6068b2023-01-05 15:42:32 +00001523 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524 first_error = ret;
David Horstmann8b6068b2023-01-05 15:42:32 +00001525 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526
1527 total_failed++;
1528 continue;
1529 }
1530
1531 success = 1;
1532 }
1533 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534
David Horstmann8b6068b2023-01-05 15:42:32 +00001535 if (success) {
1536 return total_failed;
1537 } else if (first_error) {
1538 return first_error;
1539 } else {
1540 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1541 }
Janos Follath98e28a72016-05-31 14:03:54 +01001542#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543}
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546/*
1547 * Load one or more certificates and add them to the chained list
1548 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001549int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550{
Janos Follath865b3eb2019-12-16 11:46:15 +00001551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 size_t n;
1553 unsigned char *buf;
1554
David Horstmann8b6068b2023-01-05 15:42:32 +00001555 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1556 return ret;
1557 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
David Horstmann8b6068b2023-01-05 15:42:32 +00001559 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
David Horstmann8b6068b2023-01-05 15:42:32 +00001561 mbedtls_platform_zeroize(buf, n);
1562 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563
David Horstmann8b6068b2023-01-05 15:42:32 +00001564 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565}
1566
David Horstmann8b6068b2023-01-05 15:42:32 +00001567int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001568{
1569 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001570#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571 int w_ret;
1572 WCHAR szDir[MAX_PATH];
1573 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001574 char *p;
David Horstmann8b6068b2023-01-05 15:42:32 +00001575 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
Paul Bakker9af723c2014-05-01 13:03:14 +02001577 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578 HANDLE hFind;
1579
David Horstmann8b6068b2023-01-05 15:42:32 +00001580 if (len > MAX_PATH - 3) {
1581 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1582 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583
David Horstmann8b6068b2023-01-05 15:42:32 +00001584 memset(szDir, 0, sizeof(szDir));
1585 memset(filename, 0, MAX_PATH);
1586 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001587 filename[len++] = '\\';
1588 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589 filename[len++] = '*';
1590
David Horstmann8b6068b2023-01-05 15:42:32 +00001591 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1592 MAX_PATH - 3);
1593 if (w_ret == 0) {
1594 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1595 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596
David Horstmann8b6068b2023-01-05 15:42:32 +00001597 hFind = FindFirstFileW(szDir, &file_data);
1598 if (hFind == INVALID_HANDLE_VALUE) {
1599 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1600 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601
1602 len = MAX_PATH - len;
David Horstmann8b6068b2023-01-05 15:42:32 +00001603 do {
1604 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605
David Horstmann8b6068b2023-01-05 15:42:32 +00001606 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607 continue;
David Horstmann8b6068b2023-01-05 15:42:32 +00001608 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001609
David Horstmann8b6068b2023-01-05 15:42:32 +00001610 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1611 lstrlenW(file_data.cFileName),
1612 p, (int) len - 1,
1613 NULL, NULL);
1614 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001615 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1616 goto cleanup;
1617 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618
David Horstmann8b6068b2023-01-05 15:42:32 +00001619 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1620 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621 ret++;
David Horstmann8b6068b2023-01-05 15:42:32 +00001622 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623 ret += w_ret;
David Horstmann8b6068b2023-01-05 15:42:32 +00001624 }
1625 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626
David Horstmann8b6068b2023-01-05 15:42:32 +00001627 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
David Horstmann8b6068b2023-01-05 15:42:32 +00001629 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630
Ron Eldor36d90422017-01-09 15:09:16 +02001631cleanup:
David Horstmann8b6068b2023-01-05 15:42:32 +00001632 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001633#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001634 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001635 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001637 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001638 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
David Horstmann8b6068b2023-01-05 15:42:32 +00001639 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640
David Horstmann8b6068b2023-01-05 15:42:32 +00001641 if (dir == NULL) {
1642 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1643 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644
Ron Eldor63140682017-01-09 19:27:59 +02001645#if defined(MBEDTLS_THREADING_C)
David Horstmann8b6068b2023-01-05 15:42:32 +00001646 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1647 closedir(dir);
1648 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001649 }
Ron Eldor63140682017-01-09 19:27:59 +02001650#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001651
David Horstmann8b6068b2023-01-05 15:42:32 +00001652 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001653
David Horstmann8b6068b2023-01-05 15:42:32 +00001654 while ((entry = readdir(dir)) != NULL) {
1655 snp_ret = mbedtls_snprintf(entry_name, sizeof entry_name,
1656 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657
David Horstmann8b6068b2023-01-05 15:42:32 +00001658 if (snp_ret < 0 || (size_t) snp_ret >= sizeof entry_name) {
Andres AGf9113192016-09-02 14:06:04 +01001659 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1660 goto cleanup;
David Horstmann8b6068b2023-01-05 15:42:32 +00001661 } else if (stat(entry_name, &sb) == -1) {
1662 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001663 /* Broken symbolic link - ignore this entry.
1664 stat(2) will return this error for either (a) a dangling
1665 symlink or (b) a missing file.
1666 Given that we have just obtained the filename from readdir,
1667 assume that it does exist and therefore treat this as a
1668 dangling symlink. */
1669 continue;
David Horstmann8b6068b2023-01-05 15:42:32 +00001670 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001671 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001672 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1673 goto cleanup;
1674 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675 }
1676
David Horstmann8b6068b2023-01-05 15:42:32 +00001677 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 continue;
David Horstmann8b6068b2023-01-05 15:42:32 +00001679 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680
1681 // Ignore parse errors
1682 //
David Horstmann8b6068b2023-01-05 15:42:32 +00001683 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1684 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685 ret++;
David Horstmann8b6068b2023-01-05 15:42:32 +00001686 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687 ret += t_ret;
David Horstmann8b6068b2023-01-05 15:42:32 +00001688 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001690
1691cleanup:
David Horstmann8b6068b2023-01-05 15:42:32 +00001692 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001693
Ron Eldor63140682017-01-09 19:27:59 +02001694#if defined(MBEDTLS_THREADING_C)
David Horstmann8b6068b2023-01-05 15:42:32 +00001695 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
David Horstmann8b6068b2023-01-05 15:42:32 +00001697 }
Ron Eldor63140682017-01-09 19:27:59 +02001698#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001699
Paul Bakkerbe089b02013-10-14 15:51:50 +02001700#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701
David Horstmann8b6068b2023-01-05 15:42:32 +00001702 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001706/*
1707 * OtherName ::= SEQUENCE {
1708 * type-id OBJECT IDENTIFIER,
1709 * value [0] EXPLICIT ANY DEFINED BY type-id }
1710 *
1711 * HardwareModuleName ::= SEQUENCE {
1712 * hwType OBJECT IDENTIFIER,
1713 * hwSerialNum OCTET STRING }
1714 *
1715 * NOTE: we currently only parse and use otherName of type HwModuleName,
1716 * as defined in RFC 4108.
1717 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001718static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1719 mbedtls_x509_san_other_name *other_name)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001720{
Janos Follathab23cd12019-05-09 13:53:57 +01001721 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001722 size_t len;
1723 unsigned char *p = subject_alt_name->p;
1724 const unsigned char *end = p + subject_alt_name->len;
1725 mbedtls_x509_buf cur_oid;
1726
David Horstmann8b6068b2023-01-05 15:42:32 +00001727 if ((subject_alt_name->tag &
1728 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1729 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001730 /*
1731 * The given subject alternative name is not of type "othername".
1732 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001733 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001734 }
1735
David Horstmann8b6068b2023-01-05 15:42:32 +00001736 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1737 MBEDTLS_ASN1_OID)) != 0) {
1738 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1739 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001740
1741 cur_oid.tag = MBEDTLS_ASN1_OID;
1742 cur_oid.p = p;
1743 cur_oid.len = len;
1744
1745 /*
1746 * Only HwModuleName is currently supported.
1747 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001748 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1749 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001750 }
1751
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001752 p += len;
David Horstmann8b6068b2023-01-05 15:42:32 +00001753 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1754 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1755 0) {
1756 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1757 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001758
Hanno Beckere7ff7972019-09-13 14:21:13 +01001759 if (end != p + len) {
1760 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1761 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1762 }
1763
David Horstmann8b6068b2023-01-05 15:42:32 +00001764 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1765 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1766 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1767 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001768
Hanno Beckere7ff7972019-09-13 14:21:13 +01001769 if (end != p + len) {
1770 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1771 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1772 }
1773
David Horstmann8b6068b2023-01-05 15:42:32 +00001774 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1775 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1776 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001777
1778 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1779 other_name->value.hardware_module_name.oid.p = p;
1780 other_name->value.hardware_module_name.oid.len = len;
1781
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001782 p += len;
David Horstmann8b6068b2023-01-05 15:42:32 +00001783 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1784 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1785 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1786 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001787
1788 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1789 other_name->value.hardware_module_name.val.p = p;
1790 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001791 p += len;
David Horstmann8b6068b2023-01-05 15:42:32 +00001792 if (p != end) {
David Horstmann8b6068b2023-01-05 15:42:32 +00001793 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1794 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001795 }
David Horstmann8b6068b2023-01-05 15:42:32 +00001796 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001797}
1798
David Horstmann8b6068b2023-01-05 15:42:32 +00001799int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1800 mbedtls_x509_subject_alternative_name *san)
Peter Kolbus9a969b62018-12-11 13:55:56 -06001801{
1802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
David Horstmann8b6068b2023-01-05 15:42:32 +00001803 switch (san_buf->tag &
1804 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1805 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
Peter Kolbus9a969b62018-12-11 13:55:56 -06001806 /*
1807 * otherName
1808 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001809 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
Peter Kolbus9a969b62018-12-11 13:55:56 -06001810 {
1811 mbedtls_x509_san_other_name other_name;
1812
David Horstmann8b6068b2023-01-05 15:42:32 +00001813 ret = x509_get_other_name(san_buf, &other_name);
1814 if (ret != 0) {
1815 return ret;
1816 }
Peter Kolbus9a969b62018-12-11 13:55:56 -06001817
David Horstmann8b6068b2023-01-05 15:42:32 +00001818 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Peter Kolbus9a969b62018-12-11 13:55:56 -06001819 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
David Horstmann8b6068b2023-01-05 15:42:32 +00001820 memcpy(&san->san.other_name,
1821 &other_name, sizeof(other_name));
Peter Kolbus9a969b62018-12-11 13:55:56 -06001822
1823 }
1824 break;
1825
1826 /*
1827 * dNSName
1828 */
David Horstmann8b6068b2023-01-05 15:42:32 +00001829 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
Peter Kolbus9a969b62018-12-11 13:55:56 -06001830 {
David Horstmann8b6068b2023-01-05 15:42:32 +00001831 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Peter Kolbus9a969b62018-12-11 13:55:56 -06001832 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1833
David Horstmann8b6068b2023-01-05 15:42:32 +00001834 memcpy(&san->san.unstructured_name,
1835 san_buf, sizeof(*san_buf));
Peter Kolbus9a969b62018-12-11 13:55:56 -06001836
1837 }
1838 break;
1839
1840 /*
1841 * Type not supported
1842 */
1843 default:
David Horstmann8b6068b2023-01-05 15:42:32 +00001844 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001845 }
David Horstmann8b6068b2023-01-05 15:42:32 +00001846 return 0;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001847}
1848
1849#if !defined(MBEDTLS_X509_REMOVE_INFO)
David Horstmann8b6068b2023-01-05 15:42:32 +00001850static int x509_info_subject_alt_name(char **buf, size_t *size,
1851 const mbedtls_x509_sequence
1852 *subject_alt_name,
1853 const char *prefix)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001854{
Janos Follath865b3eb2019-12-16 11:46:15 +00001855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001856 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001857 size_t n = *size;
1858 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001860 mbedtls_x509_subject_alternative_name san;
1861 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001862
David Horstmann8b6068b2023-01-05 15:42:32 +00001863 while (cur != NULL) {
1864 memset(&san, 0, sizeof(san));
1865 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1866 if (parse_ret != 0) {
1867 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1868 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001869 MBEDTLS_X509_SAFE_SNPRINTF;
David Horstmann8b6068b2023-01-05 15:42:32 +00001870 } else {
1871 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001872 MBEDTLS_X509_SAFE_SNPRINTF;
1873 }
1874 cur = cur->next;
1875 continue;
1876 }
1877
David Horstmann8b6068b2023-01-05 15:42:32 +00001878 switch (san.type) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001879 /*
1880 * otherName
1881 */
Ron Eldora2913912019-05-16 16:17:38 +03001882 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001883 {
1884 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1885
David Horstmann8b6068b2023-01-05 15:42:32 +00001886 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001887 MBEDTLS_X509_SAFE_SNPRINTF;
1888
David Horstmann8b6068b2023-01-05 15:42:32 +00001889 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1890 &other_name->value.hardware_module_name.oid) != 0) {
1891 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001892 MBEDTLS_X509_SAFE_SNPRINTF;
David Horstmann8b6068b2023-01-05 15:42:32 +00001893 ret =
1894 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001895 MBEDTLS_X509_SAFE_SNPRINTF;
1896
David Horstmann8b6068b2023-01-05 15:42:32 +00001897 ret = mbedtls_oid_get_numeric_string(p,
1898 n,
1899 &other_name->value.hardware_module_name.oid);
Ron Eldor890819a2019-05-13 19:03:04 +03001900 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001901
David Horstmann8b6068b2023-01-05 15:42:32 +00001902 ret =
1903 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001904 MBEDTLS_X509_SAFE_SNPRINTF;
1905
David Horstmann8b6068b2023-01-05 15:42:32 +00001906 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1907 ret = mbedtls_snprintf(p,
1908 n,
1909 "%02X",
1910 other_name->value.hardware_module_name.val.p[i]);
Victor Barpp Gomes47c7a732022-09-29 11:34:23 -03001911 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01001912 }
Ron Eldor890819a2019-05-13 19:03:04 +03001913 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1914 }
1915 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001916
1917 /*
1918 * dNSName
1919 */
Ron Eldora2913912019-05-16 16:17:38 +03001920 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001921 {
David Horstmann8b6068b2023-01-05 15:42:32 +00001922 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001923 MBEDTLS_X509_SAFE_SNPRINTF;
David Horstmann8b6068b2023-01-05 15:42:32 +00001924 if (san.san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001925 *p = '\0';
David Horstmann8b6068b2023-01-05 15:42:32 +00001926 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001927 }
Ron Eldora2913912019-05-16 16:17:38 +03001928
David Horstmann8b6068b2023-01-05 15:42:32 +00001929 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
Ron Eldora2913912019-05-16 16:17:38 +03001930 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001931 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001932 }
1933 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001934
1935 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001936 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001937 */
1938 default:
David Horstmann8b6068b2023-01-05 15:42:32 +00001939 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Janos Follath22f605f2019-05-10 10:37:17 +01001940 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001941 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001942 }
1943
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001944 cur = cur->next;
1945 }
1946
1947 *p = '\0';
1948
1949 *size = n;
1950 *buf = p;
1951
David Horstmann8b6068b2023-01-05 15:42:32 +00001952 return 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001953}
1954
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001955#define PRINT_ITEM(i) \
1956 { \
David Horstmann8b6068b2023-01-05 15:42:32 +00001957 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001958 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001959 sep = ", "; \
1960 }
1961
David Horstmann8b6068b2023-01-05 15:42:32 +00001962#define CERT_TYPE(type, name) \
1963 if (ns_cert_type & (type)) \
1964 PRINT_ITEM(name);
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001965
David Horstmann8b6068b2023-01-05 15:42:32 +00001966static int x509_info_cert_type(char **buf, size_t *size,
1967 unsigned char ns_cert_type)
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001968{
Janos Follath865b3eb2019-12-16 11:46:15 +00001969 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001970 size_t n = *size;
1971 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001972 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001973
David Horstmann8b6068b2023-01-05 15:42:32 +00001974 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1975 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1976 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1977 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1978 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1979 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1980 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1981 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001982
1983 *size = n;
1984 *buf = p;
1985
David Horstmann8b6068b2023-01-05 15:42:32 +00001986 return 0;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001987}
1988
David Horstmann8b6068b2023-01-05 15:42:32 +00001989#define KEY_USAGE(code, name) \
1990 if (key_usage & (code)) \
1991 PRINT_ITEM(name);
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001992
David Horstmann8b6068b2023-01-05 15:42:32 +00001993static int x509_info_key_usage(char **buf, size_t *size,
1994 unsigned int key_usage)
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001995{
Janos Follath865b3eb2019-12-16 11:46:15 +00001996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001997 size_t n = *size;
1998 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001999 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002000
David Horstmann8b6068b2023-01-05 15:42:32 +00002001 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
2002 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
2003 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
2004 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
2005 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
2006 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
2007 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
2008 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
2009 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002010
2011 *size = n;
2012 *buf = p;
2013
David Horstmann8b6068b2023-01-05 15:42:32 +00002014 return 0;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002015}
2016
David Horstmann8b6068b2023-01-05 15:42:32 +00002017static int x509_info_ext_key_usage(char **buf, size_t *size,
2018 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002019{
Janos Follath865b3eb2019-12-16 11:46:15 +00002020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002021 const char *desc;
2022 size_t n = *size;
2023 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002025 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002026
David Horstmann8b6068b2023-01-05 15:42:32 +00002027 while (cur != NULL) {
2028 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002029 desc = "???";
David Horstmann8b6068b2023-01-05 15:42:32 +00002030 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002031
David Horstmann8b6068b2023-01-05 15:42:32 +00002032 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002033 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002034
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002035 sep = ", ";
2036
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002037 cur = cur->next;
2038 }
2039
2040 *size = n;
2041 *buf = p;
2042
David Horstmann8b6068b2023-01-05 15:42:32 +00002043 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002044}
2045
David Horstmann8b6068b2023-01-05 15:42:32 +00002046static int x509_info_cert_policies(char **buf, size_t *size,
2047 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02002048{
Janos Follath865b3eb2019-12-16 11:46:15 +00002049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002050 const char *desc;
2051 size_t n = *size;
2052 char *p = *buf;
2053 const mbedtls_x509_sequence *cur = certificate_policies;
2054 const char *sep = "";
2055
David Horstmann8b6068b2023-01-05 15:42:32 +00002056 while (cur != NULL) {
2057 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02002058 desc = "???";
David Horstmann8b6068b2023-01-05 15:42:32 +00002059 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002060
David Horstmann8b6068b2023-01-05 15:42:32 +00002061 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002062 MBEDTLS_X509_SAFE_SNPRINTF;
2063
2064 sep = ", ";
2065
2066 cur = cur->next;
2067 }
2068
2069 *size = n;
2070 *buf = p;
2071
David Horstmann8b6068b2023-01-05 15:42:32 +00002072 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002073}
2074
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075/*
2076 * Return an informational string about the certificate.
2077 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002078#define BEFORE_COLON 18
2079#define BC "18"
David Horstmann8b6068b2023-01-05 15:42:32 +00002080int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
2081 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002082{
Janos Follath865b3eb2019-12-16 11:46:15 +00002083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084 size_t n;
2085 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086 char key_size_str[BEFORE_COLON];
2087
2088 p = buf;
2089 n = size;
2090
David Horstmann8b6068b2023-01-05 15:42:32 +00002091 if (NULL == crt) {
2092 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01002093 MBEDTLS_X509_SAFE_SNPRINTF;
2094
David Horstmann8b6068b2023-01-05 15:42:32 +00002095 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01002096 }
2097
David Horstmann8b6068b2023-01-05 15:42:32 +00002098 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
2099 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002100 MBEDTLS_X509_SAFE_SNPRINTF;
David Horstmann8b6068b2023-01-05 15:42:32 +00002101 ret = mbedtls_snprintf(p, n, "%sserial number : ",
2102 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002103 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104
David Horstmann8b6068b2023-01-05 15:42:32 +00002105 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002106 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107
David Horstmann8b6068b2023-01-05 15:42:32 +00002108 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002109 MBEDTLS_X509_SAFE_SNPRINTF;
David Horstmann8b6068b2023-01-05 15:42:32 +00002110 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002111 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112
David Horstmann8b6068b2023-01-05 15:42:32 +00002113 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002114 MBEDTLS_X509_SAFE_SNPRINTF;
David Horstmann8b6068b2023-01-05 15:42:32 +00002115 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002116 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117
David Horstmann8b6068b2023-01-05 15:42:32 +00002118 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
2119 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2120 crt->valid_from.year, crt->valid_from.mon,
2121 crt->valid_from.day, crt->valid_from.hour,
2122 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002123 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124
David Horstmann8b6068b2023-01-05 15:42:32 +00002125 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
2126 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2127 crt->valid_to.year, crt->valid_to.mon,
2128 crt->valid_to.day, crt->valid_to.hour,
2129 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002130 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131
David Horstmann8b6068b2023-01-05 15:42:32 +00002132 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002133 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002134
David Horstmann8b6068b2023-01-05 15:42:32 +00002135 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
2136 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002137 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002138
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002139 /* Key size */
David Horstmann8b6068b2023-01-05 15:42:32 +00002140 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
2141 mbedtls_pk_get_name(&crt->pk))) != 0) {
2142 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143 }
2144
David Horstmann8b6068b2023-01-05 15:42:32 +00002145 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
2146 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002147 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002148
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002149 /*
2150 * Optional extensions
2151 */
2152
David Horstmann8b6068b2023-01-05 15:42:32 +00002153 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
2154 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
2155 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002156 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002157
David Horstmann8b6068b2023-01-05 15:42:32 +00002158 if (crt->max_pathlen > 0) {
2159 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002160 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 }
2162 }
2163
David Horstmann8b6068b2023-01-05 15:42:32 +00002164 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2165 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002166 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002167
David Horstmann8b6068b2023-01-05 15:42:32 +00002168 if ((ret = x509_info_subject_alt_name(&p, &n,
2169 &crt->subject_alt_names,
2170 prefix)) != 0) {
2171 return ret;
2172 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002173 }
2174
David Horstmann8b6068b2023-01-05 15:42:32 +00002175 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
2176 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002177 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002178
David Horstmann8b6068b2023-01-05 15:42:32 +00002179 if ((ret = x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
2180 return ret;
2181 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002182 }
2183
David Horstmann8b6068b2023-01-05 15:42:32 +00002184 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2185 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002186 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002187
David Horstmann8b6068b2023-01-05 15:42:32 +00002188 if ((ret = x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
2189 return ret;
2190 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002191 }
2192
David Horstmann8b6068b2023-01-05 15:42:32 +00002193 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2194 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002195 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002196
David Horstmann8b6068b2023-01-05 15:42:32 +00002197 if ((ret = x509_info_ext_key_usage(&p, &n,
2198 &crt->ext_key_usage)) != 0) {
2199 return ret;
2200 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002201 }
2202
David Horstmann8b6068b2023-01-05 15:42:32 +00002203 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2204 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002205 MBEDTLS_X509_SAFE_SNPRINTF;
2206
David Horstmann8b6068b2023-01-05 15:42:32 +00002207 if ((ret = x509_info_cert_policies(&p, &n,
2208 &crt->certificate_policies)) != 0) {
2209 return ret;
2210 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002211 }
2212
David Horstmann8b6068b2023-01-05 15:42:32 +00002213 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002214 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002215
David Horstmann8b6068b2023-01-05 15:42:32 +00002216 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002217}
2218
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002219struct x509_crt_verify_string {
2220 int code;
2221 const char *string;
2222};
2223
David Horstmann8b6068b2023-01-05 15:42:32 +00002224#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002225static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002226 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002227 { 0, NULL }
2228};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002229#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002230
David Horstmann8b6068b2023-01-05 15:42:32 +00002231int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
2232 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002233{
Janos Follath865b3eb2019-12-16 11:46:15 +00002234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002235 const struct x509_crt_verify_string *cur;
2236 char *p = buf;
2237 size_t n = size;
2238
David Horstmann8b6068b2023-01-05 15:42:32 +00002239 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
2240 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002241 continue;
David Horstmann8b6068b2023-01-05 15:42:32 +00002242 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002243
David Horstmann8b6068b2023-01-05 15:42:32 +00002244 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002245 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002246 flags ^= cur->code;
2247 }
2248
David Horstmann8b6068b2023-01-05 15:42:32 +00002249 if (flags != 0) {
2250 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
2251 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002252 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002253 }
2254
David Horstmann8b6068b2023-01-05 15:42:32 +00002255 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002256}
Hanno Becker612a2f12020-10-09 09:19:39 +01002257#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002258
David Horstmann8b6068b2023-01-05 15:42:32 +00002259int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2260 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002261{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002262 unsigned int usage_must, usage_may;
2263 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
David Horstmann8b6068b2023-01-05 15:42:32 +00002264 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002265
David Horstmann8b6068b2023-01-05 15:42:32 +00002266 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
2267 return 0;
2268 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002269
2270 usage_must = usage & ~may_mask;
2271
David Horstmann8b6068b2023-01-05 15:42:32 +00002272 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
2273 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2274 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002275
2276 usage_may = usage & may_mask;
2277
David Horstmann8b6068b2023-01-05 15:42:32 +00002278 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
2279 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2280 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002281
David Horstmann8b6068b2023-01-05 15:42:32 +00002282 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002283}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002284
David Horstmann8b6068b2023-01-05 15:42:32 +00002285int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2286 const char *usage_oid,
2287 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002290
2291 /* Extension is not mandatory, absent means no restriction */
David Horstmann8b6068b2023-01-05 15:42:32 +00002292 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2293 return 0;
2294 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002295
2296 /*
2297 * Look for the requested usage (or wildcard ANY) in our list
2298 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002299 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002301
David Horstmann8b6068b2023-01-05 15:42:32 +00002302 if (cur_oid->len == usage_len &&
2303 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2304 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002305 }
2306
David Horstmann8b6068b2023-01-05 15:42:32 +00002307 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2308 return 0;
2309 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002310 }
2311
David Horstmann8b6068b2023-01-05 15:42:32 +00002312 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002313}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002316/*
2317 * Return 1 if the certificate is revoked, or 0 otherwise.
2318 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002319int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002320{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002322
David Horstmann8b6068b2023-01-05 15:42:32 +00002323 while (cur != NULL && cur->serial.len != 0) {
2324 if (crt->serial.len == cur->serial.len &&
2325 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2326 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002327 }
2328
2329 cur = cur->next;
2330 }
2331
David Horstmann8b6068b2023-01-05 15:42:32 +00002332 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333}
2334
2335/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002336 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002337 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002338 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002339static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2340 mbedtls_x509_crl *crl_list,
2341 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002342{
2343 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002344 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002345#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002346 psa_algorithm_t psa_algorithm;
2347#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002349#endif /* MBEDTLS_USE_PSA_CRYPTO */
2350 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351
David Horstmann8b6068b2023-01-05 15:42:32 +00002352 if (ca == NULL) {
2353 return flags;
2354 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355
David Horstmann8b6068b2023-01-05 15:42:32 +00002356 while (crl_list != NULL) {
2357 if (crl_list->version == 0 ||
2358 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002359 crl_list = crl_list->next;
2360 continue;
2361 }
2362
2363 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002364 * Check if the CA is configured to sign CRLs
2365 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002366 if (mbedtls_x509_crt_check_key_usage(ca,
2367 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002369 break;
2370 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002371
2372 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002373 * Check if CRL is correctly signed by the trusted CA
2374 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002375 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002376 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
David Horstmann8b6068b2023-01-05 15:42:32 +00002377 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002378
David Horstmann8b6068b2023-01-05 15:42:32 +00002379 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002380 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
David Horstmann8b6068b2023-01-05 15:42:32 +00002381 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002382
pespacek7599a772022-02-07 14:40:55 +01002383#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann8b6068b2023-01-05 15:42:32 +00002384 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
2385 if (psa_hash_compute(psa_algorithm,
2386 crl_list->tbs.p,
2387 crl_list->tbs.len,
2388 hash,
2389 sizeof(hash),
2390 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002391 /* Note: this can't happen except after an internal error */
2392 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2393 break;
2394 }
pespacek7599a772022-02-07 14:40:55 +01002395#else
David Horstmann8b6068b2023-01-05 15:42:32 +00002396 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2397 hash_length = mbedtls_md_get_size(md_info);
2398 if (mbedtls_md(md_info,
2399 crl_list->tbs.p,
2400 crl_list->tbs.len,
2401 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002402 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002403 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002404 break;
2405 }
pespaceka7a64692022-02-14 15:18:43 +01002406#endif /* MBEDTLS_USE_PSA_CRYPTO */
2407
David Horstmann8b6068b2023-01-05 15:42:32 +00002408 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002409 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
David Horstmann8b6068b2023-01-05 15:42:32 +00002410 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002411
David Horstmann8b6068b2023-01-05 15:42:32 +00002412 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2413 crl_list->sig_md, hash, hash_length,
2414 crl_list->sig.p, crl_list->sig.len) != 0) {
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 }
2418
2419 /*
2420 * Check for validity of CRL (Do not drop out)
2421 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002422 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
David Horstmann8b6068b2023-01-05 15:42:32 +00002424 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002425
David Horstmann8b6068b2023-01-05 15:42:32 +00002426 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002427 flags |= MBEDTLS_X509_BADCRL_FUTURE;
David Horstmann8b6068b2023-01-05 15:42:32 +00002428 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002429
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002430 /*
2431 * Check if certificate is revoked
2432 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002433 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002434 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002435 break;
2436 }
2437
2438 crl_list = crl_list->next;
2439 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002440
David Horstmann8b6068b2023-01-05 15:42:32 +00002441 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002444
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002445/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002446 * Check the signature of a certificate by its parent
2447 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002448static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2449 mbedtls_x509_crt *parent,
2450 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002451{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002452 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002453 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002454#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2455 const mbedtls_md_info_t *md_info;
David Horstmann8b6068b2023-01-05 15:42:32 +00002456 md_info = mbedtls_md_info_from_type(child->sig_md);
2457 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002458
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002459 /* Note: hash errors can happen only after an internal error */
David Horstmann8b6068b2023-01-05 15:42:32 +00002460 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2461 return -1;
2462 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002463#else
David Horstmann8b6068b2023-01-05 15:42:32 +00002464 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002466
David Horstmann8b6068b2023-01-05 15:42:32 +00002467 status = psa_hash_compute(hash_alg,
2468 child->tbs.p,
2469 child->tbs.len,
2470 hash,
2471 sizeof(hash),
2472 &hash_len);
2473 if (status != PSA_SUCCESS) {
2474 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002475 }
2476
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002477#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002478 /* Skip expensive computation on obvious mismatch */
David Horstmann8b6068b2023-01-05 15:42:32 +00002479 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2480 return -1;
2481 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002482
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002483#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmann8b6068b2023-01-05 15:42:32 +00002484 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2485 return mbedtls_pk_verify_restartable(&parent->pk,
2486 child->sig_md, hash, hash_len,
2487 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002488 }
2489#else
2490 (void) rs_ctx;
2491#endif
2492
David Horstmann8b6068b2023-01-05 15:42:32 +00002493 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2494 child->sig_md, hash, hash_len,
2495 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002496}
2497
2498/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002499 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2500 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002501 *
2502 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002503 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002504static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2505 const mbedtls_x509_crt *parent,
2506 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002507{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002508 int need_ca_bit;
2509
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002510 /* Parent must be the issuer */
David Horstmann8b6068b2023-01-05 15:42:32 +00002511 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2512 return -1;
2513 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002514
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002515 /* Parent must have the basicConstraints CA bit set as a general rule */
2516 need_ca_bit = 1;
2517
2518 /* Exception: v1/v2 certificates that are locally trusted. */
David Horstmann8b6068b2023-01-05 15:42:32 +00002519 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002520 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002521 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002522
David Horstmann8b6068b2023-01-05 15:42:32 +00002523 if (need_ca_bit && !parent->ca_istrue) {
2524 return -1;
2525 }
2526
2527 if (need_ca_bit &&
2528 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2529 return -1;
2530 }
2531
2532 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002533}
2534
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002535/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002536 * Find a suitable parent for child in candidates, or return NULL.
2537 *
2538 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002539 * 1. subject name matches child's issuer
2540 * 2. if necessary, the CA bit is set and key usage allows signing certs
2541 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002542 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002543 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002544 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002545 * If there's a suitable candidate which is also time-valid, return the first
2546 * such. Otherwise, return the first suitable candidate (or NULL if there is
2547 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002548 *
2549 * The rationale for this rule is that someone could have a list of trusted
2550 * roots with two versions on the same root with different validity periods.
2551 * (At least one user reported having such a list and wanted it to just work.)
2552 * The reason we don't just require time-validity is that generally there is
2553 * only one version, and if it's expired we want the flags to state that
2554 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002555 *
2556 * The rationale for rule 3 (signature for trusted roots) is that users might
2557 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002558 * way we select the correct one is by checking the signature (as we don't
2559 * rely on key identifier extensions). (This is one way users might choose to
2560 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002561 *
2562 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002563 * - [in] child: certificate for which we're looking for a parent
2564 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002565 * - [out] r_parent: parent found (or NULL)
2566 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002567 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2568 * of the chain, 0 otherwise
2569 * - [in] path_cnt: number of intermediates seen so far
2570 * - [in] self_cnt: number of self-signed intermediates seen so far
2571 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002572 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002573 *
2574 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002575 * - 0 on success
2576 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002577 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002578static int x509_crt_find_parent_in(
David Horstmann8b6068b2023-01-05 15:42:32 +00002579 mbedtls_x509_crt *child,
2580 mbedtls_x509_crt *candidates,
2581 mbedtls_x509_crt **r_parent,
2582 int *r_signature_is_good,
2583 int top,
2584 unsigned path_cnt,
2585 unsigned self_cnt,
2586 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002587{
Janos Follath865b3eb2019-12-16 11:46:15 +00002588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002589 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002590 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002591
2592#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002593 /* did we have something in progress? */
David Horstmann8b6068b2023-01-05 15:42:32 +00002594 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002595 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002596 parent = rs_ctx->parent;
2597 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002598 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002599
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002600 /* clear saved state */
2601 rs_ctx->parent = NULL;
2602 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002603 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002604
2605 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002606 goto check_signature;
2607 }
2608#endif
2609
2610 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002611 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002612
David Horstmann8b6068b2023-01-05 15:42:32 +00002613 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002614 /* basic parenting skills (name, CA bit, key usage) */
David Horstmann8b6068b2023-01-05 15:42:32 +00002615 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002616 continue;
David Horstmann8b6068b2023-01-05 15:42:32 +00002617 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002618
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002619 /* +1 because stored max_pathlen is 1 higher that the actual value */
David Horstmann8b6068b2023-01-05 15:42:32 +00002620 if (parent->max_pathlen > 0 &&
2621 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002622 continue;
2623 }
2624
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002625 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002626#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2627check_signature:
2628#endif
David Horstmann8b6068b2023-01-05 15:42:32 +00002629 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002630
2631#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmann8b6068b2023-01-05 15:42:32 +00002632 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002633 /* save state */
2634 rs_ctx->parent = parent;
2635 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002636 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002637
David Horstmann8b6068b2023-01-05 15:42:32 +00002638 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002639 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002640#else
2641 (void) ret;
2642#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002643
2644 signature_is_good = ret == 0;
David Horstmann8b6068b2023-01-05 15:42:32 +00002645 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002646 continue;
David Horstmann8b6068b2023-01-05 15:42:32 +00002647 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002648
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002649 /* optional time check */
David Horstmann8b6068b2023-01-05 15:42:32 +00002650 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2651 mbedtls_x509_time_is_future(&parent->valid_from)) {
2652 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002653 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002654 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002655 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002656
2657 continue;
2658 }
2659
Andy Gross1f627142019-01-30 10:25:53 -06002660 *r_parent = parent;
2661 *r_signature_is_good = signature_is_good;
2662
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002663 break;
2664 }
2665
David Horstmann8b6068b2023-01-05 15:42:32 +00002666 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002667 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002668 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002669 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002670
David Horstmann8b6068b2023-01-05 15:42:32 +00002671 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002672}
2673
2674/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002675 * Find a parent in trusted CAs or the provided chain, or return NULL.
2676 *
2677 * Searches in trusted CAs first, and return the first suitable parent found
2678 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002679 *
2680 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002681 * - [in] child: certificate for which we're looking for a parent, followed
2682 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002683 * - [in] trust_ca: list of locally trusted certificates
2684 * - [out] parent: parent found (or NULL)
2685 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2686 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2687 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2688 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002689 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002690 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002691 *
2692 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002693 * - 0 on success
2694 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002695 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002696static int x509_crt_find_parent(
David Horstmann8b6068b2023-01-05 15:42:32 +00002697 mbedtls_x509_crt *child,
2698 mbedtls_x509_crt *trust_ca,
2699 mbedtls_x509_crt **parent,
2700 int *parent_is_trusted,
2701 int *signature_is_good,
2702 unsigned path_cnt,
2703 unsigned self_cnt,
2704 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002705{
Janos Follath865b3eb2019-12-16 11:46:15 +00002706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002707 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002708
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002709 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002710
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002711#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002712 /* restore then clear saved state if we have some stored */
David Horstmann8b6068b2023-01-05 15:42:32 +00002713 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002714 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002715 rs_ctx->parent_is_trusted = -1;
2716 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002717#endif
2718
David Horstmann8b6068b2023-01-05 15:42:32 +00002719 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002720 search_list = *parent_is_trusted ? trust_ca : child->next;
2721
David Horstmann8b6068b2023-01-05 15:42:32 +00002722 ret = x509_crt_find_parent_in(child, search_list,
2723 parent, signature_is_good,
2724 *parent_is_trusted,
2725 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002726
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002727#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmann8b6068b2023-01-05 15:42:32 +00002728 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002729 /* save state */
2730 rs_ctx->parent_is_trusted = *parent_is_trusted;
David Horstmann8b6068b2023-01-05 15:42:32 +00002731 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002732 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002733#else
2734 (void) ret;
2735#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002736
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002737 /* stop here if found or already in second iteration */
David Horstmann8b6068b2023-01-05 15:42:32 +00002738 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002739 break;
David Horstmann8b6068b2023-01-05 15:42:32 +00002740 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002741
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002742 /* prepare second iteration */
2743 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002744 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002745
2746 /* extra precaution against mistakes in the caller */
David Horstmann8b6068b2023-01-05 15:42:32 +00002747 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002748 *parent_is_trusted = 0;
2749 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002750 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002751
David Horstmann8b6068b2023-01-05 15:42:32 +00002752 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002753}
2754
2755/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002756 * Check if an end-entity certificate is locally trusted
2757 *
2758 * Currently we require such certificates to be self-signed (actually only
2759 * check for self-issued as self-signatures are not checked)
2760 */
2761static int x509_crt_check_ee_locally_trusted(
David Horstmann8b6068b2023-01-05 15:42:32 +00002762 mbedtls_x509_crt *crt,
2763 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002764{
2765 mbedtls_x509_crt *cur;
2766
2767 /* must be self-issued */
David Horstmann8b6068b2023-01-05 15:42:32 +00002768 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2769 return -1;
2770 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002771
2772 /* look for an exact match with trusted cert */
David Horstmann8b6068b2023-01-05 15:42:32 +00002773 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2774 if (crt->raw.len == cur->raw.len &&
2775 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2776 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002777 }
2778 }
2779
2780 /* too bad */
David Horstmann8b6068b2023-01-05 15:42:32 +00002781 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002782}
2783
2784/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002785 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002786 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002787 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2788 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002789 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002790 * such that every cert in the chain is a child of the next one,
2791 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002792 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002793 * Verify that chain and return it with flags for all issues found.
2794 *
2795 * Special cases:
2796 * - EE == Rj -> return a one-element list containing it
2797 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2798 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002799 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002800 * Tests for (aspects of) this function should include at least:
2801 * - trusted EE
2802 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002803 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002804 * - if relevant: EE untrusted
2805 * - if relevant: EE -> intermediate, untrusted
2806 * with the aspect under test checked at each relevant level (EE, int, root).
2807 * For some aspects longer chains are required, but usually length 2 is
2808 * enough (but length 1 is not in general).
2809 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002810 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002811 * - [in] crt: the cert list EE, C1, ..., Cn
2812 * - [in] trust_ca: the trusted list R1, ..., Rp
2813 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002814 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002815 * Only valid when return value is 0, may contain garbage otherwise!
2816 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002817 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002818 *
2819 * Return value:
2820 * - non-zero if the chain could not be fully built and examined
2821 * - 0 is the chain was successfully built and examined,
2822 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002823 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002824static int x509_crt_verify_chain(
David Horstmann8b6068b2023-01-05 15:42:32 +00002825 mbedtls_x509_crt *crt,
2826 mbedtls_x509_crt *trust_ca,
2827 mbedtls_x509_crl *ca_crl,
2828 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2829 void *p_ca_cb,
2830 const mbedtls_x509_crt_profile *profile,
2831 mbedtls_x509_crt_verify_chain *ver_chain,
2832 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002833{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002834 /* Don't initialize any of those variables here, so that the compiler can
2835 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002837 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002838 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002839 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002840 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002841 int parent_is_trusted;
2842 int child_is_trusted;
2843 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002844 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002845 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002846
2847#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2848 /* resume if we had an operation in progress */
David Horstmann8b6068b2023-01-05 15:42:32 +00002849 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002850 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002851 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002852 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002853
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002854 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002855 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002856 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002857 flags = &cur->flags;
2858
2859 goto find_parent;
2860 }
2861#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002862
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002863 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002864 self_cnt = 0;
2865 parent_is_trusted = 0;
2866 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002867
David Horstmann8b6068b2023-01-05 15:42:32 +00002868 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002869 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002870 cur = &ver_chain->items[ver_chain->len];
2871 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002872 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002873 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002874 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002875
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002876 /* Check time-validity (all certificates) */
David Horstmann8b6068b2023-01-05 15:42:32 +00002877 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002878 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
David Horstmann8b6068b2023-01-05 15:42:32 +00002879 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002880
David Horstmann8b6068b2023-01-05 15:42:32 +00002881 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002882 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
David Horstmann8b6068b2023-01-05 15:42:32 +00002883 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002884
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002885 /* Stop here for trusted roots (but not for trusted EE certs) */
David Horstmann8b6068b2023-01-05 15:42:32 +00002886 if (child_is_trusted) {
2887 return 0;
2888 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002889
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002890 /* Check signature algorithm: MD & PK algs */
David Horstmann8b6068b2023-01-05 15:42:32 +00002891 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002892 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
David Horstmann8b6068b2023-01-05 15:42:32 +00002893 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002894
David Horstmann8b6068b2023-01-05 15:42:32 +00002895 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002896 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
David Horstmann8b6068b2023-01-05 15:42:32 +00002897 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002898
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002899 /* Special case: EE certs that are locally trusted */
David Horstmann8b6068b2023-01-05 15:42:32 +00002900 if (ver_chain->len == 1 &&
2901 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2902 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002903 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002904
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002905#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2906find_parent:
2907#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002908
2909 /* Obtain list of potential trusted signers from CA callback,
2910 * or use statically provided list. */
2911#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
David Horstmann8b6068b2023-01-05 15:42:32 +00002912 if (f_ca_cb != NULL) {
2913 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2914 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002915 ver_chain->trust_ca_cb_result = NULL;
2916
David Horstmann8b6068b2023-01-05 15:42:32 +00002917 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2918 if (ret != 0) {
2919 return MBEDTLS_ERR_X509_FATAL_ERROR;
2920 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002921
2922 cur_trust_ca = ver_chain->trust_ca_cb_result;
David Horstmann8b6068b2023-01-05 15:42:32 +00002923 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002924#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2925 {
2926 ((void) f_ca_cb);
2927 ((void) p_ca_cb);
2928 cur_trust_ca = trust_ca;
2929 }
2930
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002931 /* Look for a parent in trusted CAs or up the chain */
David Horstmann8b6068b2023-01-05 15:42:32 +00002932 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2933 &parent_is_trusted, &signature_is_good,
2934 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002935
2936#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmann8b6068b2023-01-05 15:42:32 +00002937 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002938 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002939 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002940 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002941 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002942
David Horstmann8b6068b2023-01-05 15:42:32 +00002943 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002944 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002945#else
2946 (void) ret;
2947#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002948
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002949 /* No parent? We're done here */
David Horstmann8b6068b2023-01-05 15:42:32 +00002950 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002951 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
David Horstmann8b6068b2023-01-05 15:42:32 +00002952 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002953 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002954
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002955 /* Count intermediate self-issued (not necessarily self-signed) certs.
2956 * These can occur with some strategies for key rollover, see [SIRO],
2957 * and should be excluded from max_pathlen checks. */
David Horstmann8b6068b2023-01-05 15:42:32 +00002958 if (ver_chain->len != 1 &&
2959 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002961 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002962
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002963 /* path_cnt is 0 for the first intermediate CA,
2964 * and if parent is trusted it's not an intermediate CA */
David Horstmann8b6068b2023-01-05 15:42:32 +00002965 if (!parent_is_trusted &&
2966 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002967 /* return immediately to avoid overflow the chain array */
David Horstmann8b6068b2023-01-05 15:42:32 +00002968 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002969 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002970
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002971 /* signature was checked while searching parent */
David Horstmann8b6068b2023-01-05 15:42:32 +00002972 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
David Horstmann8b6068b2023-01-05 15:42:32 +00002974 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975
2976 /* check size of signing key */
David Horstmann8b6068b2023-01-05 15:42:32 +00002977 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002978 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
David Horstmann8b6068b2023-01-05 15:42:32 +00002979 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 /* Check trusted CA's CRL for the given crt */
David Horstmann8b6068b2023-01-05 15:42:32 +00002983 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002984#else
2985 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002986#endif
2987
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002988 /* prepare for next iteration */
2989 child = parent;
2990 parent = NULL;
2991 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002992 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002993 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002994}
2995
2996/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002997 * Check for CN match
2998 */
David Horstmann8b6068b2023-01-05 15:42:32 +00002999static int x509_crt_check_cn(const mbedtls_x509_buf *name,
3000 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003001{
3002 /* try exact match */
David Horstmann8b6068b2023-01-05 15:42:32 +00003003 if (name->len == cn_len &&
3004 x509_memcasecmp(cn, name->p, cn_len) == 0) {
3005 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003006 }
3007
3008 /* try wildcard match */
David Horstmann8b6068b2023-01-05 15:42:32 +00003009 if (x509_check_wildcard(cn, name) == 0) {
3010 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003011 }
3012
David Horstmann8b6068b2023-01-05 15:42:32 +00003013 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003014}
3015
3016/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003017 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3018 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003019static int x509_crt_check_san(const mbedtls_x509_buf *name,
3020 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003021{
3022 const unsigned char san_type = (unsigned char) name->tag &
3023 MBEDTLS_ASN1_TAG_VALUE_MASK;
3024
3025 /* dNSName */
David Horstmann8b6068b2023-01-05 15:42:32 +00003026 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
3027 return x509_crt_check_cn(name, cn, cn_len);
3028 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003029
3030 /* (We may handle other types here later.) */
3031
3032 /* Unrecognized type */
David Horstmann8b6068b2023-01-05 15:42:32 +00003033 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003034}
3035
3036/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003037 * Verify the requested CN - only call this if cn is not NULL!
3038 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003039static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3040 const char *cn,
3041 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003042{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003043 const mbedtls_x509_name *name;
3044 const mbedtls_x509_sequence *cur;
David Horstmann8b6068b2023-01-05 15:42:32 +00003045 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003046
David Horstmann8b6068b2023-01-05 15:42:32 +00003047 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
3048 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
3049 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003050 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003051 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003052 }
3053
David Horstmann8b6068b2023-01-05 15:42:32 +00003054 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003055 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
David Horstmann8b6068b2023-01-05 15:42:32 +00003056 }
3057 } else {
3058 for (name = &crt->subject; name != NULL; name = name->next) {
3059 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3060 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
3061 break;
3062 }
3063 }
3064
3065 if (name == NULL) {
3066 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3067 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003068 }
3069}
3070
3071/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003072 * Merge the flags for all certs in the chain, after calling callback
3073 */
3074static int x509_crt_merge_flags_with_cb(
David Horstmann8b6068b2023-01-05 15:42:32 +00003075 uint32_t *flags,
3076 const mbedtls_x509_crt_verify_chain *ver_chain,
3077 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3078 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003079{
Janos Follath865b3eb2019-12-16 11:46:15 +00003080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003081 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003082 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003083 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003084
David Horstmann8b6068b2023-01-05 15:42:32 +00003085 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003086 cur = &ver_chain->items[i-1];
3087 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003088
David Horstmann8b6068b2023-01-05 15:42:32 +00003089 if (NULL != f_vrfy) {
3090 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3091 return ret;
3092 }
3093 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003094
3095 *flags |= cur_flags;
3096 }
3097
David Horstmann8b6068b2023-01-05 15:42:32 +00003098 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003099}
3100
3101/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003102 * Verify the certificate validity, with profile, restartable version
3103 *
3104 * This function:
3105 * - checks the requested CN (if any)
3106 * - checks the type and size of the EE cert's key,
3107 * as that isn't done as part of chain building/verification currently
3108 * - builds and verifies the chain
3109 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003110 *
3111 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3112 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3113 * verification routine to search for trusted signers, and CRLs will
3114 * be disabled. Otherwise, `trust_ca` will be used as the static list
3115 * of trusted signers, and `ca_crl` will be use as the static list
3116 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003117 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003118static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3119 mbedtls_x509_crt *trust_ca,
3120 mbedtls_x509_crl *ca_crl,
3121 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3122 void *p_ca_cb,
3123 const mbedtls_x509_crt_profile *profile,
3124 const char *cn, uint32_t *flags,
3125 int (*f_vrfy)(void *,
3126 mbedtls_x509_crt *,
3127 int,
3128 uint32_t *),
3129 void *p_vrfy,
3130 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003131{
Janos Follath865b3eb2019-12-16 11:46:15 +00003132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003133 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003134 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003135 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003136
3137 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003138 ee_flags = 0;
David Horstmann8b6068b2023-01-05 15:42:32 +00003139 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003140
David Horstmann8b6068b2023-01-05 15:42:32 +00003141 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003142 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3143 goto exit;
3144 }
3145
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003146 /* check name if requested */
David Horstmann8b6068b2023-01-05 15:42:32 +00003147 if (cn != NULL) {
3148 x509_crt_verify_name(crt, cn, &ee_flags);
3149 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003150
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003151 /* Check the type and size of the key */
David Horstmann8b6068b2023-01-05 15:42:32 +00003152 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003153
David Horstmann8b6068b2023-01-05 15:42:32 +00003154 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003155 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
David Horstmann8b6068b2023-01-05 15:42:32 +00003156 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003157
David Horstmann8b6068b2023-01-05 15:42:32 +00003158 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003159 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
David Horstmann8b6068b2023-01-05 15:42:32 +00003160 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003161
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003162 /* Check the chain */
David Horstmann8b6068b2023-01-05 15:42:32 +00003163 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3164 f_ca_cb, p_ca_cb, profile,
3165 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003166
David Horstmann8b6068b2023-01-05 15:42:32 +00003167 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003168 goto exit;
David Horstmann8b6068b2023-01-05 15:42:32 +00003169 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003170
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003171 /* Merge end-entity flags */
3172 ver_chain.items[0].flags |= ee_flags;
3173
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003174 /* Build final flags, calling callback on the way if any */
David Horstmann8b6068b2023-01-05 15:42:32 +00003175 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003176
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003177exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003178
3179#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
David Horstmann8b6068b2023-01-05 15:42:32 +00003180 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3181 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003182 ver_chain.trust_ca_cb_result = NULL;
3183#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3184
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003185#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
David Horstmann8b6068b2023-01-05 15:42:32 +00003186 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3187 mbedtls_x509_crt_restart_free(rs_ctx);
3188 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003189#endif
3190
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003191 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3192 * the SSL module for authmode optional, but non-zero return from the
3193 * callback means a fatal error so it shouldn't be ignored */
David Horstmann8b6068b2023-01-05 15:42:32 +00003194 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003195 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003196 }
3197
David Horstmann8b6068b2023-01-05 15:42:32 +00003198 if (ret != 0) {
3199 *flags = (uint32_t) -1;
3200 return ret;
3201 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003202
David Horstmann8b6068b2023-01-05 15:42:32 +00003203 if (*flags != 0) {
3204 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3205 }
3206
3207 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003208}
3209
Hanno Becker3116fb32019-03-28 13:34:42 +00003210
3211/*
3212 * Verify the certificate validity (default profile, not restartable)
3213 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003214int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3215 mbedtls_x509_crt *trust_ca,
3216 mbedtls_x509_crl *ca_crl,
3217 const char *cn, uint32_t *flags,
3218 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3219 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003220{
David Horstmann8b6068b2023-01-05 15:42:32 +00003221 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3222 NULL, NULL,
3223 &mbedtls_x509_crt_profile_default,
3224 cn, flags,
3225 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003226}
3227
3228/*
3229 * Verify the certificate validity (user-chosen profile, not restartable)
3230 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003231int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3232 mbedtls_x509_crt *trust_ca,
3233 mbedtls_x509_crl *ca_crl,
3234 const mbedtls_x509_crt_profile *profile,
3235 const char *cn, uint32_t *flags,
3236 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3237 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003238{
David Horstmann8b6068b2023-01-05 15:42:32 +00003239 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3240 NULL, NULL,
3241 profile, cn, flags,
3242 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003243}
3244
3245#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3246/*
3247 * Verify the certificate validity (user-chosen profile, CA callback,
3248 * not restartable).
3249 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003250int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3251 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3252 void *p_ca_cb,
3253 const mbedtls_x509_crt_profile *profile,
3254 const char *cn, uint32_t *flags,
3255 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3256 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003257{
David Horstmann8b6068b2023-01-05 15:42:32 +00003258 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3259 f_ca_cb, p_ca_cb,
3260 profile, cn, flags,
3261 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003262}
3263#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3264
David Horstmann8b6068b2023-01-05 15:42:32 +00003265int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3266 mbedtls_x509_crt *trust_ca,
3267 mbedtls_x509_crl *ca_crl,
3268 const mbedtls_x509_crt_profile *profile,
3269 const char *cn, uint32_t *flags,
3270 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3271 void *p_vrfy,
3272 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003273{
David Horstmann8b6068b2023-01-05 15:42:32 +00003274 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3275 NULL, NULL,
3276 profile, cn, flags,
3277 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003278}
3279
3280
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003281/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003282 * Initialize a certificate chain
3283 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003284void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003285{
David Horstmann8b6068b2023-01-05 15:42:32 +00003286 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003287}
3288
3289/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003290 * Unallocate all certificate data
3291 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003292void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003293{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 mbedtls_x509_crt *cert_cur = crt;
3295 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003296
David Horstmann8b6068b2023-01-05 15:42:32 +00003297 while (cert_cur != NULL) {
3298 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
David Horstmann8b6068b2023-01-05 15:42:32 +00003301 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003302#endif
3303
David Horstmann8b6068b2023-01-05 15:42:32 +00003304 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3305 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3306 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3307 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3308 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003309
David Horstmann8b6068b2023-01-05 15:42:32 +00003310 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3311 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3312 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003313 }
3314
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003315 cert_prv = cert_cur;
3316 cert_cur = cert_cur->next;
3317
David Horstmann8b6068b2023-01-05 15:42:32 +00003318 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3319 if (cert_prv != crt) {
3320 mbedtls_free(cert_prv);
3321 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003322 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003323}
3324
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003325#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3326/*
3327 * Initialize a restart context
3328 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003329void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003330{
David Horstmann8b6068b2023-01-05 15:42:32 +00003331 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003332
3333 ctx->parent = NULL;
3334 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003335 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003336
3337 ctx->parent_is_trusted = -1;
3338
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003339 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003340 ctx->self_cnt = 0;
David Horstmann8b6068b2023-01-05 15:42:32 +00003341 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003342}
3343
3344/*
3345 * Free the components of a restart context
3346 */
David Horstmann8b6068b2023-01-05 15:42:32 +00003347void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003348{
David Horstmann8b6068b2023-01-05 15:42:32 +00003349 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003350 return;
David Horstmann8b6068b2023-01-05 15:42:32 +00003351 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003352
David Horstmann8b6068b2023-01-05 15:42:32 +00003353 mbedtls_pk_restart_free(&ctx->pk);
3354 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003355}
3356#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#endif /* MBEDTLS_X509_CRT_PARSE_C */