blob: aaeb515ba28d440dcb4833a731af6262693dede7 [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"
50#endif
51
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010056#endif
57
Daniel Axtens301db662020-05-28 11:43:41 +100058#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010059#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#include <windows.h>
61#else
62#include <time.h>
63#endif
Daniel Axtens301db662020-05-28 11:43:41 +100064#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000067#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020068#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#include <sys/types.h>
70#include <sys/stat.h>
71#include <dirent.h>
Eduardo Silva32ffb2b2019-04-25 10:43:26 -060072#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000073#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif
75
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020076/*
77 * Item in a verification chain: cert and flags for it
78 */
79typedef struct {
80 mbedtls_x509_crt *crt;
81 uint32_t flags;
82} x509_crt_verify_chain_item;
83
84/*
85 * Max size of verification chain: end-entity + intermediates + trusted root
86 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020088
Gilles Peskine0ecd7192021-06-07 21:24:26 +020089/* Default profile. Do not remove items unless there are serious security
90 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020091const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
92{
Gilles Peskine5e79cb32017-05-04 16:17:21 +020093 /* Only SHA-2 hashes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
95 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
96 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
97 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020098 0xFFFFFFF, /* Any PK alg */
99 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200100 2048,
101};
102
103/*
104 * Next-default profile
105 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200106const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
107{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 /* Hashes from SHA-256 and above */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
110 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
111 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200112 0xFFFFFFF, /* Any PK alg */
113#if defined(MBEDTLS_ECP_C)
114 /* Curves at or above 128-bit security level */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
116 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
117 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
118 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
119 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
120 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
121 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200122#else
123 0,
124#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200125 2048,
126};
127
128/*
129 * NSA Suite B Profile
130 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200131const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
132{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200133 /* Only SHA-256 and 384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
135 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200136 /* Only ECDSA */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
138 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200139#if defined(MBEDTLS_ECP_C)
140 /* Only NIST P-256 and P-384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
142 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200143#else
144 0,
145#endif
146 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147};
148
149/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200150 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200151 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200152 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
154 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200155{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if (md_alg == MBEDTLS_MD_NONE) {
157 return -1;
158 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
161 return 0;
162 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200165}
166
167/*
168 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200169 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200170 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
172 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200173{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 if (pk_alg == MBEDTLS_PK_NONE) {
175 return -1;
176 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
179 return 0;
180 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183}
184
185/*
186 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200187 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200188 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
190 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200193
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
196 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
197 return 0;
198 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200201 }
202#endif
203
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200204#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200206 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 pk_alg == MBEDTLS_PK_ECKEY_DH) {
208 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 if (gid == MBEDTLS_ECP_DP_NONE) {
211 return -1;
212 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
215 return 0;
216 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200219 }
220#endif
221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200223}
224
225/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000226 * Like memcmp, but case-insensitive and always returns -1 if different
227 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000229{
230 size_t i;
231 unsigned char diff;
232 const unsigned char *n1 = s1, *n2 = s2;
233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000235 diff = n1[i] ^ n2[i];
236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000238 continue;
239 }
240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 if (diff == 32 &&
242 ((n1[i] >= 'a' && n1[i] <= 'z') ||
243 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
244 continue;
245 }
246
247 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000248 }
249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000251}
252
253/*
254 * Return 0 if name matches wildcard, -1 otherwise
255 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000257{
258 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000260
261 /* We can't have a match if there is no wildcard to match */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
263 return -1;
264 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 for (i = 0; i < cn_len; ++i) {
267 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000268 cn_idx = i;
269 break;
270 }
271 }
272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 if (cn_idx == 0) {
274 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000275 }
276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 if (cn_len - cn_idx == name->len - 1 &&
278 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
279 return 0;
280 }
281
282 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000283}
284
285/*
286 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
287 * variations (but not all).
288 *
289 * Return 0 if equal, -1 otherwise.
290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000292{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000294 a->len == b->len &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 memcmp(a->p, b->p, b->len) == 0) {
296 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000297 }
298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
300 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000301 a->len == b->len &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 x509_memcasecmp(a->p, b->p, b->len) == 0) {
303 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000304 }
305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000307}
308
309/*
310 * Compare two X.509 Names (aka rdnSequence).
311 *
312 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
313 * we sometimes return unequal when the full algorithm would return equal,
314 * but never the other way. (In particular, we don't do Unicode normalisation
315 * or space folding.)
316 *
317 * Return 0 if equal, -1 otherwise.
318 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000320{
321 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 while (a != NULL || b != NULL) {
323 if (a == NULL || b == NULL) {
324 return -1;
325 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000326
327 /* type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000329 a->oid.len != b->oid.len ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
331 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000332 }
333
334 /* value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 if (x509_string_cmp(&a->val, &b->val) != 0) {
336 return -1;
337 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000338
339 /* structure of the list of sets */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 if (a->next_merged != b->next_merged) {
341 return -1;
342 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000343
344 a = a->next;
345 b = b->next;
346 }
347
348 /* a == NULL == b */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000350}
351
352/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200353 * Reset (init or clear) a verify_chain
354 */
355static void x509_crt_verify_chain_reset(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200357{
358 size_t i;
359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200361 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000362 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200363 }
364
365 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000366
367#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
368 ver_chain->trust_ca_cb_result = NULL;
369#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200370}
371
372/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
374 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375static int x509_get_version(unsigned char **p,
376 const unsigned char *end,
377 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378{
Janos Follath865b3eb2019-12-16 11:46:15 +0000379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 size_t len;
381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
383 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
384 0)) != 0) {
385 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 *ver = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 }
389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 }
392
393 end = *p + len;
394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
396 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
397 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 if (*p != end) {
400 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
401 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
402 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405}
406
407/*
408 * Validity ::= SEQUENCE {
409 * notBefore Time,
410 * notAfter Time }
411 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412static int x509_get_dates(unsigned char **p,
413 const unsigned char *end,
414 mbedtls_x509_time *from,
415 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416{
Janos Follath865b3eb2019-12-16 11:46:15 +0000417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 size_t len;
419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
421 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
423 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
425 end = *p + len;
426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100427 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
428 return ret;
429 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
432 return ret;
433 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 if (*p != end) {
436 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
437 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
438 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441}
442
443/*
444 * X.509 v2/v3 unique identifier (not parsed)
445 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446static int x509_get_uid(unsigned char **p,
447 const unsigned char *end,
448 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449{
Janos Follath865b3eb2019-12-16 11:46:15 +0000450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 if (*p == end) {
453 return 0;
454 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 uid->tag = **p;
457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
459 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
460 n)) != 0) {
461 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
462 return 0;
463 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 }
467
468 uid->p = *p;
469 *p += uid->len;
470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472}
473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474static int x509_get_basic_constraints(unsigned char **p,
475 const unsigned char *end,
476 int *ca_istrue,
477 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478{
Janos Follath865b3eb2019-12-16 11:46:15 +0000479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480 size_t len;
481
482 /*
483 * BasicConstraints ::= SEQUENCE {
484 * cA BOOLEAN DEFAULT FALSE,
485 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
486 */
487 *ca_istrue = 0; /* DEFAULT FALSE */
488 *max_pathlen = 0; /* endless */
489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
491 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
492 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 }
494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 if (*p == end) {
496 return 0;
497 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100499 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
500 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
501 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
502 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 if (ret != 0) {
505 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
506 }
507
508 if (*ca_istrue != 0) {
509 *ca_istrue = 1;
510 }
511 }
512
513 if (*p == end) {
514 return 0;
515 }
516
517 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
518 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
519 }
520
521 if (*p != end) {
522 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
523 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
524 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525
Andrzej Kurek16050742020-04-14 09:49:52 -0400526 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
527 * overflow, which is an undefined behavior. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 if (*max_pathlen == INT_MAX) {
529 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
530 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
531 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400532
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 (*max_pathlen)++;
534
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536}
537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538static int x509_get_ns_cert_type(unsigned char **p,
539 const unsigned char *end,
540 unsigned char *ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541{
Janos Follath865b3eb2019-12-16 11:46:15 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
546 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
547 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549 if (bs.len != 1) {
550 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
551 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
552 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553
554 /* Get actual bitstring */
555 *ns_cert_type = *bs.p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557}
558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559static int x509_get_key_usage(unsigned char **p,
560 const unsigned char *end,
561 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562{
Janos Follath865b3eb2019-12-16 11:46:15 +0000563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200564 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
568 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
569 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 if (bs.len < 1) {
572 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
573 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
574 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
576 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200577 *key_usage = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200579 *key_usage |= (unsigned int) bs.p[i] << (8*i);
580 }
581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583}
584
585/*
586 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
587 *
588 * KeyPurposeId ::= OBJECT IDENTIFIER
589 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590static int x509_get_ext_key_usage(unsigned char **p,
591 const unsigned char *end,
592 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593{
Janos Follath865b3eb2019-12-16 11:46:15 +0000594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
597 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
598 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599
600 /* Sequence length must be >= 1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100601 if (ext_key_usage->buf.p == NULL) {
602 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
603 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
604 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100606 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607}
608
609/*
610 * SubjectAltName ::= GeneralNames
611 *
612 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
613 *
614 * GeneralName ::= CHOICE {
615 * otherName [0] OtherName,
616 * rfc822Name [1] IA5String,
617 * dNSName [2] IA5String,
618 * x400Address [3] ORAddress,
619 * directoryName [4] Name,
620 * ediPartyName [5] EDIPartyName,
621 * uniformResourceIdentifier [6] IA5String,
622 * iPAddress [7] OCTET STRING,
623 * registeredID [8] OBJECT IDENTIFIER }
624 *
625 * OtherName ::= SEQUENCE {
626 * type-id OBJECT IDENTIFIER,
627 * value [0] EXPLICIT ANY DEFINED BY type-id }
628 *
629 * EDIPartyName ::= SEQUENCE {
630 * nameAssigner [0] DirectoryString OPTIONAL,
631 * partyName [1] DirectoryString }
632 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300633 * NOTE: we list all types, but only use dNSName and otherName
634 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636static int x509_get_subject_alt_name(unsigned char **p,
637 const unsigned char *end,
638 mbedtls_x509_sequence *subject_alt_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639{
Janos Follath865b3eb2019-12-16 11:46:15 +0000640 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645
646 /* Get main sequence tag */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
648 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
649 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
650 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100652 if (*p + len != end) {
653 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
654 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
655 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100657 while (*p < end) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300658 mbedtls_x509_subject_alternative_name dummy_san_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
Ron Eldordbbd9662019-05-15 15:46:03 +0300660
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 tag = **p;
662 (*p)++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
664 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
665 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100667 if ((tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
668 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
669 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
670 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100671 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672
Ron Eldordbbd9662019-05-15 15:46:03 +0300673 /*
irwir74aee1c2019-09-21 18:21:48 +0300674 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300675 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100676 ret = mbedtls_x509_parse_subject_alt_name(&(cur->buf), &dummy_san_buf);
Ron Eldordbbd9662019-05-15 15:46:03 +0300677 /*
678 * In case the extension is malformed, return an error,
679 * and clear the allocated sequences.
680 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300682 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
683 mbedtls_x509_sequence *seq_prv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 while (seq_cur != NULL) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300685 seq_prv = seq_cur;
686 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100687 mbedtls_platform_zeroize(seq_prv,
688 sizeof(mbedtls_x509_sequence));
689 mbedtls_free(seq_prv);
Ron Eldordbbd9662019-05-15 15:46:03 +0300690 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300691 subject_alt_name->next = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100692 return ret;
Ron Eldordbbd9662019-05-15 15:46:03 +0300693 }
694
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 /* Allocate and assign next pointer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100696 if (cur->buf.p != NULL) {
697 if (cur->next != NULL) {
698 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
699 }
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 if (cur->next == NULL) {
704 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
705 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
706 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 cur = cur->next;
709 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200710
711 buf = &(cur->buf);
712 buf->tag = tag;
713 buf->p = *p;
714 buf->len = tag_len;
715 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716 }
717
718 /* Set final sequence entry's next pointer to NULL */
719 cur->next = NULL;
720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100721 if (*p != end) {
722 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
723 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
724 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100726 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727}
728
729/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200730 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
731 *
732 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
733 *
734 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
735 *
736 * PolicyInformation ::= SEQUENCE {
737 * policyIdentifier CertPolicyId,
738 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
739 * PolicyQualifierInfo OPTIONAL }
740 *
741 * CertPolicyId ::= OBJECT IDENTIFIER
742 *
743 * PolicyQualifierInfo ::= SEQUENCE {
744 * policyQualifierId PolicyQualifierId,
745 * qualifier ANY DEFINED BY policyQualifierId }
746 *
747 * -- policyQualifierIds for Internet policy qualifiers
748 *
749 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
750 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
751 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
752 *
753 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
754 *
755 * Qualifier ::= CHOICE {
756 * cPSuri CPSuri,
757 * userNotice UserNotice }
758 *
759 * CPSuri ::= IA5String
760 *
761 * UserNotice ::= SEQUENCE {
762 * noticeRef NoticeReference OPTIONAL,
763 * explicitText DisplayText OPTIONAL }
764 *
765 * NoticeReference ::= SEQUENCE {
766 * organization DisplayText,
767 * noticeNumbers SEQUENCE OF INTEGER }
768 *
769 * DisplayText ::= CHOICE {
770 * ia5String IA5String (SIZE (1..200)),
771 * visibleString VisibleString (SIZE (1..200)),
772 * bmpString BMPString (SIZE (1..200)),
773 * utf8String UTF8String (SIZE (1..200)) }
774 *
775 * NOTE: we only parse and use anyPolicy without qualifiers at this point
776 * as defined in RFC 5280.
777 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100778static int x509_get_certificate_policies(unsigned char **p,
779 const unsigned char *end,
780 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200781{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300782 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200783 size_t len;
784 mbedtls_asn1_buf *buf;
785 mbedtls_asn1_sequence *cur = certificate_policies;
786
787 /* Get main sequence tag */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100788 ret = mbedtls_asn1_get_tag(p, end, &len,
789 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
790 if (ret != 0) {
791 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
792 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 if (*p + len != end) {
795 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
796 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
797 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200798
799 /*
800 * Cannot be an empty sequence.
801 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100802 if (len == 0) {
803 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
804 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
805 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200808 mbedtls_x509_buf policy_oid;
809 const unsigned char *policy_end;
810
811 /*
812 * Get the policy sequence
813 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100814 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
815 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
816 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
817 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200818
819 policy_end = *p + len;
820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100821 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
822 MBEDTLS_ASN1_OID)) != 0) {
823 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
824 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200825
826 policy_oid.tag = MBEDTLS_ASN1_OID;
827 policy_oid.len = len;
828 policy_oid.p = *p;
829
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300830 /*
831 * Only AnyPolicy is currently supported when enforcing policy.
832 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100833 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300834 /*
835 * Set the parsing return code but continue parsing, in case this
836 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
837 * is configured.
838 */
839 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
840 }
841
Ron Eldor74d9acc2019-03-21 14:00:03 +0200842 /* Allocate and assign next pointer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843 if (cur->buf.p != NULL) {
844 if (cur->next != NULL) {
845 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
846 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100850 if (cur->next == NULL) {
851 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
852 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
853 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200854
855 cur = cur->next;
856 }
857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100858 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200859 buf->tag = policy_oid.tag;
860 buf->p = policy_oid.p;
861 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300862
863 *p += len;
864
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100865 /*
866 * If there is an optional qualifier, then *p < policy_end
867 * Check the Qualifier len to verify it doesn't exceed policy_end.
868 */
869 if (*p < policy_end) {
870 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
871 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
872 0) {
873 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
874 }
Ron Eldor08063792019-05-13 16:38:39 +0300875 /*
876 * Skip the optional policy qualifiers.
877 */
878 *p += len;
879 }
880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100881 if (*p != policy_end) {
882 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
883 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
884 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200885 }
886
887 /* Set final sequence entry's next pointer to NULL */
888 cur->next = NULL;
889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100890 if (*p != end) {
891 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
892 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
893 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100895 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200896}
897
898/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 * X.509 v3 extensions
900 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100902static int x509_get_crt_ext(unsigned char **p,
903 const unsigned char *end,
904 mbedtls_x509_crt *crt,
905 mbedtls_x509_crt_ext_cb_t cb,
906 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907{
Janos Follath865b3eb2019-12-16 11:46:15 +0000908 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200910 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100912 if (*p == end) {
913 return 0;
914 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000915
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100916 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
917 return ret;
918 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
Hanno Becker12f62fb2019-02-12 17:22:36 +0000920 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100921 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922 /*
923 * Extension ::= SEQUENCE {
924 * extnID OBJECT IDENTIFIER,
925 * critical BOOLEAN DEFAULT FALSE,
926 * extnValue OCTET STRING }
927 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100928 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 int is_critical = 0; /* DEFAULT FALSE */
930 int ext_type = 0;
931
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100932 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
933 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
934 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
935 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936
937 end_ext_data = *p + len;
938
939 /* Get extension ID */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100940 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
941 MBEDTLS_ASN1_OID)) != 0) {
942 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
943 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944
k-stachowiak463928a2018-07-24 12:50:59 +0200945 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946 extn_oid.p = *p;
947 *p += extn_oid.len;
948
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 /* Get optional critical */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
951 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
952 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
953 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955 /* Data should be octet string type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100956 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
957 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
958 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
959 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200961 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 end_ext_octet = *p + len;
963
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100964 if (end_ext_octet != end_ext_data) {
965 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
966 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
967 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968
969 /*
970 * Detect supported extensions
971 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100972 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100974 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200975 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100976 if (cb != NULL) {
977 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
978 if (ret != 0 && is_critical) {
979 return ret;
980 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200981 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200982 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200983 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200984
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 /* No parser found, skip extension */
986 *p = end_ext_octet;
987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100989 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 /* Data is marked as critical: fail */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
992 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 }
994#endif
995 continue;
996 }
997
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100998 /* Forbid repeated extensions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100999 if ((crt->ext_types & ext_type) != 0) {
1000 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1001 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +01001002
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 crt->ext_types |= ext_type;
1004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001005 switch (ext_type) {
1006 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
1007 /* Parse basic constraints */
1008 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
1009 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
1010 return ret;
1011 }
1012 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001014 case MBEDTLS_X509_EXT_KEY_USAGE:
1015 /* Parse key usage */
1016 if ((ret = x509_get_key_usage(p, end_ext_octet,
1017 &crt->key_usage)) != 0) {
1018 return ret;
1019 }
1020 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1023 /* Parse extended key usage */
1024 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1025 &crt->ext_key_usage)) != 0) {
1026 return ret;
1027 }
1028 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001030 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1031 /* Parse subject alt name */
1032 if ((ret = x509_get_subject_alt_name(p, end_ext_octet,
1033 &crt->subject_alt_names)) != 0) {
1034 return ret;
1035 }
1036 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001038 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1039 /* Parse netscape certificate type */
1040 if ((ret = x509_get_ns_cert_type(p, end_ext_octet,
1041 &crt->ns_cert_type)) != 0) {
1042 return ret;
1043 }
1044 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001046 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1047 /* Parse certificate policies type */
1048 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1049 &crt->certificate_policies)) != 0) {
1050 /* Give the callback (if any) a chance to handle the extension
1051 * if it contains unsupported policies */
1052 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1053 cb(p_ctx, crt, &extn_oid, is_critical,
1054 start_ext_octet, end_ext_octet) == 0) {
1055 break;
1056 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001057
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001058#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 if (is_critical) {
1060 return ret;
1061 } else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001062#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001063 /*
1064 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1065 * cannot interpret or enforce the policy. However, it is up to
1066 * the user to choose how to enforce the policies,
1067 * unless the extension is critical.
1068 */
1069 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1070 return ret;
1071 }
1072 }
1073 break;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 default:
1076 /*
1077 * If this is a non-critical extension, which the oid layer
1078 * supports, but there isn't an x509 parser for it,
1079 * skip the extension.
1080 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001081#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082 if (is_critical) {
1083 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1084 } else
Ron Eldordf48efa2019-04-08 13:28:24 +03001085#endif
1086 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087 }
1088 }
1089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001090 if (*p != end) {
1091 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1092 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1093 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001095 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096}
1097
1098/*
1099 * Parse and fill a single X.509 certificate in DER format
1100 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001101static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1102 const unsigned char *buf,
1103 size_t buflen,
1104 int make_copy,
1105 mbedtls_x509_crt_ext_cb_t cb,
1106 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107{
Janos Follath865b3eb2019-12-16 11:46:15 +00001108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109 size_t len;
1110 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001113 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1114 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1115 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116
1117 /*
1118 * Check for valid input
1119 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001120 if (crt == NULL || buf == NULL) {
1121 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1122 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001124 /* Use the original buffer until we figure out actual length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001125 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001126 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 end = p + len;
1128
1129 /*
1130 * Certificate ::= SEQUENCE {
1131 * tbsCertificate TBSCertificate,
1132 * signatureAlgorithm AlgorithmIdentifier,
1133 * signatureValue BIT STRING }
1134 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001135 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1137 mbedtls_x509_crt_free(crt);
1138 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139 }
1140
Janos Follathcc0e49d2016-02-17 14:34:12 +00001141 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001142 crt->raw.len = crt_end - buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001143 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001144 /* Create and populate a new buffer for the raw field. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001145 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1146 if (crt->raw.p == NULL) {
1147 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1148 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001150 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001151 crt->own_buffer = 1;
1152
1153 p += crt->raw.len - len;
1154 end = crt_end = p + len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001155 } else {
1156 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001157 crt->own_buffer = 0;
1158 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001159
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 /*
1161 * TBSCertificate ::= SEQUENCE {
1162 */
1163 crt->tbs.p = p;
1164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001165 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1166 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1167 mbedtls_x509_crt_free(crt);
1168 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 }
1170
1171 end = p + len;
1172 crt->tbs.len = end - crt->tbs.p;
1173
1174 /*
1175 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1176 *
1177 * CertificateSerialNumber ::= INTEGER
1178 *
1179 * signature AlgorithmIdentifier
1180 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001181 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1182 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1183 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1184 &sig_params1)) != 0) {
1185 mbedtls_x509_crt_free(crt);
1186 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 }
1188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001189 if (crt->version < 0 || crt->version > 2) {
1190 mbedtls_x509_crt_free(crt);
1191 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 }
1193
Andres AG7ca4a032017-03-09 16:16:11 +00001194 crt->version++;
1195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1197 &crt->sig_md, &crt->sig_pk,
1198 &crt->sig_opts)) != 0) {
1199 mbedtls_x509_crt_free(crt);
1200 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 }
1202
1203 /*
1204 * issuer Name
1205 */
1206 crt->issuer_raw.p = p;
1207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1209 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1210 mbedtls_x509_crt_free(crt);
1211 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001212 }
1213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001214 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1215 mbedtls_x509_crt_free(crt);
1216 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217 }
1218
1219 crt->issuer_raw.len = p - crt->issuer_raw.p;
1220
1221 /*
1222 * Validity ::= SEQUENCE {
1223 * notBefore Time,
1224 * notAfter Time }
1225 *
1226 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001227 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1228 &crt->valid_to)) != 0) {
1229 mbedtls_x509_crt_free(crt);
1230 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231 }
1232
1233 /*
1234 * subject Name
1235 */
1236 crt->subject_raw.p = p;
1237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1239 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1240 mbedtls_x509_crt_free(crt);
1241 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242 }
1243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1245 mbedtls_x509_crt_free(crt);
1246 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 }
1248
1249 crt->subject_raw.len = p - crt->subject_raw.p;
1250
1251 /*
1252 * SubjectPublicKeyInfo
1253 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001254 crt->pk_raw.p = p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001255 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1256 mbedtls_x509_crt_free(crt);
1257 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001259 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260
1261 /*
1262 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1263 * -- If present, version shall be v2 or v3
1264 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1265 * -- If present, version shall be v2 or v3
1266 * extensions [3] EXPLICIT Extensions OPTIONAL
1267 * -- If present, version shall be v3
1268 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001269 if (crt->version == 2 || crt->version == 3) {
1270 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1271 if (ret != 0) {
1272 mbedtls_x509_crt_free(crt);
1273 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 }
1275 }
1276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001277 if (crt->version == 2 || crt->version == 3) {
1278 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1279 if (ret != 0) {
1280 mbedtls_x509_crt_free(crt);
1281 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 }
1283 }
1284
David Horstmannab617512022-10-27 11:45:01 +01001285 int extensions_allowed = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001287 if (crt->version != 3) {
David Horstmannab617512022-10-27 11:45:01 +01001288 extensions_allowed = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001289 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001290#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001291 if (extensions_allowed) {
1292 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1293 if (ret != 0) {
1294 mbedtls_x509_crt_free(crt);
1295 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296 }
1297 }
1298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001299 if (p != end) {
1300 mbedtls_x509_crt_free(crt);
1301 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1302 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 }
1304
1305 end = crt_end;
1306
1307 /*
1308 * }
1309 * -- end of TBSCertificate
1310 *
1311 * signatureAlgorithm AlgorithmIdentifier,
1312 * signatureValue BIT STRING
1313 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001314 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1315 mbedtls_x509_crt_free(crt);
1316 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 }
1318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001319 if (crt->sig_oid.len != sig_oid2.len ||
1320 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001321 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001322 sig_params1.len != sig_params2.len ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001323 (sig_params1.len != 0 &&
1324 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1325 mbedtls_x509_crt_free(crt);
1326 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327 }
1328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1330 mbedtls_x509_crt_free(crt);
1331 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001332 }
1333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001334 if (p != end) {
1335 mbedtls_x509_crt_free(crt);
1336 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1337 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001338 }
1339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001340 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341}
1342
1343/*
1344 * Parse one X.509 certificate in DER format from a buffer and add them to a
1345 * chained list
1346 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001347static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1348 const unsigned char *buf,
1349 size_t buflen,
1350 int make_copy,
1351 mbedtls_x509_crt_ext_cb_t cb,
1352 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353{
Janos Follath865b3eb2019-12-16 11:46:15 +00001354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356
1357 /*
1358 * Check for valid input
1359 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001360 if (crt == NULL || buf == NULL) {
1361 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1362 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365 prev = crt;
1366 crt = crt->next;
1367 }
1368
1369 /*
1370 * Add new certificate on the end of the chain if needed.
1371 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001372 if (crt->version != 0 && crt->next == NULL) {
1373 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001375 if (crt->next == NULL) {
1376 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1377 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378
1379 prev = crt;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001380 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 }
1383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001384 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1385 if (ret != 0) {
1386 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387 prev->next = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001388 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001390 if (crt != chain) {
1391 mbedtls_free(crt);
1392 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001394 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395 }
1396
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001397 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001398}
1399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001400int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1401 const unsigned char *buf,
1402 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001403{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001404 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001405}
1406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001407int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1408 const unsigned char *buf,
1409 size_t buflen,
1410 int make_copy,
1411 mbedtls_x509_crt_ext_cb_t cb,
1412 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001413{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001414 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001415}
1416
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001417int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1418 const unsigned char *buf,
1419 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001420{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001421 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001422}
1423
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001425 * Parse one or more PEM certificates from a buffer and add them to the chained
1426 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001428int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1429 const unsigned char *buf,
1430 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431{
Janos Follath98e28a72016-05-31 14:03:54 +01001432#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001433 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001435#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436
1437 /*
1438 * Check for valid input
1439 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001440 if (chain == NULL || buf == NULL) {
1441 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443
1444 /*
1445 * Determine buffer content. Buffer contains either one DER certificate or
1446 * one or more PEM certificates.
1447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001449 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1450 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001454 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1455 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1456 }
Janos Follath98e28a72016-05-31 14:03:54 +01001457#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001458 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001459#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001462 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001466 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001467 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 size_t use_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001469 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001471 /* If we get there, we know the string is null-terminated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001472 ret = mbedtls_pem_read_buffer(&pem,
1473 "-----BEGIN CERTIFICATE-----",
1474 "-----END CERTIFICATE-----",
1475 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001477 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478 /*
1479 * Was PEM encoded
1480 */
1481 buflen -= use_len;
1482 buf += use_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001483 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1484 return ret;
1485 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1486 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001487
1488 /*
1489 * PEM header and footer were found
1490 */
1491 buflen -= use_len;
1492 buf += use_len;
1493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001494 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495 first_error = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001496 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497
Paul Bakker5a5fa922014-09-26 14:53:04 +02001498 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001502 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001506 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001508 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509 /*
1510 * Quit parsing on a memory error
1511 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1513 return ret;
1514 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001516 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517 first_error = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001518 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519
1520 total_failed++;
1521 continue;
1522 }
1523
1524 success = 1;
1525 }
1526 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001528 if (success) {
1529 return total_failed;
1530 } else if (first_error) {
1531 return first_error;
1532 } else {
1533 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1534 }
Janos Follath98e28a72016-05-31 14:03:54 +01001535#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536}
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539/*
1540 * Load one or more certificates and add them to the chained list
1541 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001542int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543{
Janos Follath865b3eb2019-12-16 11:46:15 +00001544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545 size_t n;
1546 unsigned char *buf;
1547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001548 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1549 return ret;
1550 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001552 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554 mbedtls_platform_zeroize(buf, n);
1555 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001557 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558}
1559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001560int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561{
1562 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001563#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564 int w_ret;
1565 WCHAR szDir[MAX_PATH];
1566 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001567 char *p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569
Paul Bakker9af723c2014-05-01 13:03:14 +02001570 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571 HANDLE hFind;
1572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001573 if (len > MAX_PATH - 3) {
1574 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1575 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001577 memset(szDir, 0, sizeof(szDir));
1578 memset(filename, 0, MAX_PATH);
1579 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001580 filename[len++] = '\\';
1581 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582 filename[len++] = '*';
1583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001584 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1585 MAX_PATH - 3);
1586 if (w_ret == 0) {
1587 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1588 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590 hFind = FindFirstFileW(szDir, &file_data);
1591 if (hFind == INVALID_HANDLE_VALUE) {
1592 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1593 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
1595 len = MAX_PATH - len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001596 do {
1597 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001599 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001601 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001603 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1604 lstrlenW(file_data.cFileName),
1605 p, (int) len - 1,
1606 NULL, NULL);
1607 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001608 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1609 goto cleanup;
1610 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001612 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1613 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614 ret++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001615 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616 ret += w_ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001617 }
1618 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623
Ron Eldor36d90422017-01-09 15:09:16 +02001624cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001625 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001626#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001627 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001628 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001630 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001631 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001632 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001634 if (dir == NULL) {
1635 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1636 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637
Ron Eldor63140682017-01-09 19:27:59 +02001638#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001639 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1640 closedir(dir);
1641 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001642 }
Ron Eldor63140682017-01-09 19:27:59 +02001643#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001645 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001647 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman18688702023-02-02 12:40:50 +00001648 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001649 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650
Dave Rodgman18688702023-02-02 12:40:50 +00001651 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001652 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1653 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001654 } else if (stat(entry_name, &sb) == -1) {
1655 if (errno == ENOENT) {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001656 /* Broken symbolic link - ignore this entry.
1657 stat(2) will return this error for either (a) a dangling
1658 symlink or (b) a missing file.
1659 Given that we have just obtained the filename from readdir,
1660 assume that it does exist and therefore treat this as a
1661 dangling symlink. */
1662 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001663 } else {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001664 /* Some other file error; report the error. */
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001665 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1666 goto cleanup;
1667 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668 }
1669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001672 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673
1674 // Ignore parse errors
1675 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001676 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1677 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 ret++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680 ret += t_ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001681 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001683
1684cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001685 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001686
Ron Eldor63140682017-01-09 19:27:59 +02001687#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001688 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001690 }
Ron Eldor63140682017-01-09 19:27:59 +02001691#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001692
Paul Bakkerbe089b02013-10-14 15:51:50 +02001693#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001695 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001699/*
1700 * OtherName ::= SEQUENCE {
1701 * type-id OBJECT IDENTIFIER,
1702 * value [0] EXPLICIT ANY DEFINED BY type-id }
1703 *
1704 * HardwareModuleName ::= SEQUENCE {
1705 * hwType OBJECT IDENTIFIER,
1706 * hwSerialNum OCTET STRING }
1707 *
1708 * NOTE: we currently only parse and use otherName of type HwModuleName,
1709 * as defined in RFC 4108.
1710 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001711static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1712 mbedtls_x509_san_other_name *other_name)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001713{
Janos Follathab23cd12019-05-09 13:53:57 +01001714 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001715 size_t len;
1716 unsigned char *p = subject_alt_name->p;
1717 const unsigned char *end = p + subject_alt_name->len;
1718 mbedtls_x509_buf cur_oid;
1719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001720 if ((subject_alt_name->tag &
1721 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1722 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001723 /*
1724 * The given subject alternative name is not of type "othername".
1725 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001726 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001727 }
1728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001729 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1730 MBEDTLS_ASN1_OID)) != 0) {
1731 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1732 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001733
1734 cur_oid.tag = MBEDTLS_ASN1_OID;
1735 cur_oid.p = p;
1736 cur_oid.len = len;
1737
1738 /*
1739 * Only HwModuleName is currently supported.
1740 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001741 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1742 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001743 }
1744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001745 if (p + len >= end) {
1746 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1747 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1748 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Ron Eldor890819a2019-05-13 19:03:04 +03001749 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001750 p += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001751 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1752 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1753 0) {
1754 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1755 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001757 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1758 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1759 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1760 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001762 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1763 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1764 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001765
1766 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1767 other_name->value.hardware_module_name.oid.p = p;
1768 other_name->value.hardware_module_name.oid.len = len;
1769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001770 if (p + len >= end) {
1771 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1772 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1773 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Ron Eldor890819a2019-05-13 19:03:04 +03001774 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001775 p += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001776 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1777 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1778 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1779 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001780
1781 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1782 other_name->value.hardware_module_name.val.p = p;
1783 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001784 p += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001785 if (p != end) {
1786 mbedtls_platform_zeroize(other_name,
1787 sizeof(*other_name));
1788 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1789 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001790 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001791 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001792}
1793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001794static int x509_info_subject_alt_name(char **buf, size_t *size,
1795 const mbedtls_x509_sequence
1796 *subject_alt_name,
1797 const char *prefix)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001798{
Janos Follath865b3eb2019-12-16 11:46:15 +00001799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -03001800 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001801 size_t n = *size;
1802 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001804 mbedtls_x509_subject_alternative_name san;
1805 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001807 while (cur != NULL) {
1808 memset(&san, 0, sizeof(san));
1809 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1810 if (parse_ret != 0) {
1811 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1812 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001813 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001814 } else {
1815 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001816 MBEDTLS_X509_SAFE_SNPRINTF;
1817 }
1818 cur = cur->next;
1819 continue;
1820 }
1821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001822 switch (san.type) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001823 /*
1824 * otherName
1825 */
Ron Eldora2913912019-05-16 16:17:38 +03001826 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001827 {
1828 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001830 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001831 MBEDTLS_X509_SAFE_SNPRINTF;
1832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001833 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1834 &other_name->value.hardware_module_name.oid) != 0) {
1835 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001836 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001837 ret =
1838 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001839 MBEDTLS_X509_SAFE_SNPRINTF;
1840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001841 ret = mbedtls_oid_get_numeric_string(p,
1842 n,
1843 &other_name->value.hardware_module_name.oid);
Ron Eldor890819a2019-05-13 19:03:04 +03001844 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846 ret =
1847 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001848 MBEDTLS_X509_SAFE_SNPRINTF;
1849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001850 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1851 ret = mbedtls_snprintf(p,
1852 n,
1853 "%02X",
1854 other_name->value.hardware_module_name.val.p[i]);
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -03001855 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01001856 }
Ron Eldor890819a2019-05-13 19:03:04 +03001857 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1858 }
1859 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001860
1861 /*
1862 * dNSName
1863 */
Ron Eldora2913912019-05-16 16:17:38 +03001864 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001865 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001866 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001867 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001868 if (san.san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001869 *p = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001870 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001871 }
Ron Eldora2913912019-05-16 16:17:38 +03001872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001873 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
Ron Eldora2913912019-05-16 16:17:38 +03001874 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001875 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001876 }
1877 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001878
1879 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001880 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001881 */
1882 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001883 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Janos Follath22f605f2019-05-10 10:37:17 +01001884 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001885 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001886 }
1887
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001888 cur = cur->next;
1889 }
1890
1891 *p = '\0';
1892
1893 *size = n;
1894 *buf = p;
1895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001896 return 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001897}
1898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1900 mbedtls_x509_subject_alternative_name *san)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001901{
Janos Follath865b3eb2019-12-16 11:46:15 +00001902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001903 switch (san_buf->tag &
1904 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1905 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
Ron Eldor890819a2019-05-13 19:03:04 +03001906 /*
1907 * otherName
1908 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001909 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001910 {
Ron Eldor890819a2019-05-13 19:03:04 +03001911 mbedtls_x509_san_other_name other_name;
1912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001913 ret = x509_get_other_name(san_buf, &other_name);
1914 if (ret != 0) {
1915 return ret;
1916 }
Ron Eldor890819a2019-05-13 19:03:04 +03001917
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001918 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Ron Eldor890819a2019-05-13 19:03:04 +03001919 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001920 memcpy(&san->san.other_name,
1921 &other_name, sizeof(other_name));
Ron Eldor890819a2019-05-13 19:03:04 +03001922
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001923 }
Ron Eldor890819a2019-05-13 19:03:04 +03001924 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001925
Ron Eldor890819a2019-05-13 19:03:04 +03001926 /*
1927 * dNSName
1928 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001929 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001930 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001931 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Ron Eldor890819a2019-05-13 19:03:04 +03001932 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 memcpy(&san->san.unstructured_name,
1935 san_buf, sizeof(*san_buf));
Janos Follath6c379b42019-05-10 14:17:16 +01001936
Ron Eldor890819a2019-05-13 19:03:04 +03001937 }
1938 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001939
Ron Eldor890819a2019-05-13 19:03:04 +03001940 /*
1941 * Type not supported
1942 */
1943 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001944 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Ron Eldor890819a2019-05-13 19:03:04 +03001945 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001946 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001947}
1948
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001949#define PRINT_ITEM(i) \
1950 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001952 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001953 sep = ", "; \
1954 }
1955
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001956#define CERT_TYPE(type, name) \
1957 if (ns_cert_type & (type)) \
1958 PRINT_ITEM(name);
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001959
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001960static int x509_info_cert_type(char **buf, size_t *size,
1961 unsigned char ns_cert_type)
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001962{
Janos Follath865b3eb2019-12-16 11:46:15 +00001963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001964 size_t n = *size;
1965 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001966 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001967
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001968 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1969 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1970 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1971 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1972 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1973 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1974 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1975 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001976
1977 *size = n;
1978 *buf = p;
1979
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001980 return 0;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001981}
1982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001983#define KEY_USAGE(code, name) \
1984 if (key_usage & (code)) \
1985 PRINT_ITEM(name);
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001986
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001987static int x509_info_key_usage(char **buf, size_t *size,
1988 unsigned int key_usage)
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001989{
Janos Follath865b3eb2019-12-16 11:46:15 +00001990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001991 size_t n = *size;
1992 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001993 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1996 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1997 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1998 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1999 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
2000 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
2001 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
2002 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
2003 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002004
2005 *size = n;
2006 *buf = p;
2007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002008 return 0;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002009}
2010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002011static int x509_info_ext_key_usage(char **buf, size_t *size,
2012 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002013{
Janos Follath865b3eb2019-12-16 11:46:15 +00002014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002015 const char *desc;
2016 size_t n = *size;
2017 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002019 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002021 while (cur != NULL) {
2022 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002023 desc = "???";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002024 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002026 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002027 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002028
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002029 sep = ", ";
2030
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002031 cur = cur->next;
2032 }
2033
2034 *size = n;
2035 *buf = p;
2036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002037 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002038}
2039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040static int x509_info_cert_policies(char **buf, size_t *size,
2041 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02002042{
Janos Follath865b3eb2019-12-16 11:46:15 +00002043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002044 const char *desc;
2045 size_t n = *size;
2046 char *p = *buf;
2047 const mbedtls_x509_sequence *cur = certificate_policies;
2048 const char *sep = "";
2049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002050 while (cur != NULL) {
2051 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02002052 desc = "???";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002053 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002056 MBEDTLS_X509_SAFE_SNPRINTF;
2057
2058 sep = ", ";
2059
2060 cur = cur->next;
2061 }
2062
2063 *size = n;
2064 *buf = p;
2065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002066 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002067}
2068
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002069/*
2070 * Return an informational string about the certificate.
2071 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002072#define BEFORE_COLON 18
2073#define BC "18"
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002074int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
2075 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076{
Janos Follath865b3eb2019-12-16 11:46:15 +00002077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002078 size_t n;
2079 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 char key_size_str[BEFORE_COLON];
2081
2082 p = buf;
2083 n = size;
2084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002085 if (NULL == crt) {
2086 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01002087 MBEDTLS_X509_SAFE_SNPRINTF;
2088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002089 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01002090 }
2091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002092 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
2093 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002094 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002095 ret = mbedtls_snprintf(p, n, "%sserial number : ",
2096 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002097 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002099 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002100 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002102 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002103 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002104 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002105 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002107 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002108 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002109 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002110 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002112 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
2113 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2114 crt->valid_from.year, crt->valid_from.mon,
2115 crt->valid_from.day, crt->valid_from.hour,
2116 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002117 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
2120 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2121 crt->valid_to.year, crt->valid_to.mon,
2122 crt->valid_to.day, crt->valid_to.hour,
2123 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002124 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002127 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002129 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
2130 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002131 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002133 /* Key size */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002134 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
2135 mbedtls_pk_get_name(&crt->pk))) != 0) {
2136 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002137 }
2138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
2140 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002141 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002142
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002143 /*
2144 * Optional extensions
2145 */
2146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002147 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
2148 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
2149 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002150 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 if (crt->max_pathlen > 0) {
2153 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002154 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002155 }
2156 }
2157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2159 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002160 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 if ((ret = x509_info_subject_alt_name(&p, &n,
2163 &crt->subject_alt_names,
2164 prefix)) != 0) {
2165 return ret;
2166 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002167 }
2168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002169 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
2170 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002171 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002172
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002173 if ((ret = x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
2174 return ret;
2175 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002176 }
2177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002178 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2179 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002180 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002182 if ((ret = x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
2183 return ret;
2184 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002185 }
2186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002187 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2188 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002189 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002191 if ((ret = x509_info_ext_key_usage(&p, &n,
2192 &crt->ext_key_usage)) != 0) {
2193 return ret;
2194 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002195 }
2196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002197 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2198 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002199 MBEDTLS_X509_SAFE_SNPRINTF;
2200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 if ((ret = x509_info_cert_policies(&p, &n,
2202 &crt->certificate_policies)) != 0) {
2203 return ret;
2204 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002205 }
2206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002207 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002208 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002210 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002211}
2212
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002213struct x509_crt_verify_string {
2214 int code;
2215 const char *string;
2216};
2217
2218static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002219 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002220 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002221 { MBEDTLS_X509_BADCERT_CN_MISMATCH,
2222 "The certificate Common Name (CN) does not match with the expected CN" },
2223 { MBEDTLS_X509_BADCERT_NOT_TRUSTED,
2224 "The certificate is not correctly signed by the trusted CA" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002225 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2226 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002227 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2228 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2229 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002230 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002231 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2232 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2233 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2234 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002235 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002236 { MBEDTLS_X509_BADCERT_BAD_PK,
2237 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2238 { MBEDTLS_X509_BADCERT_BAD_KEY,
2239 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002240 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241 { MBEDTLS_X509_BADCRL_BAD_PK,
2242 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2243 { MBEDTLS_X509_BADCRL_BAD_KEY,
2244 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002245 { 0, NULL }
2246};
2247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
2249 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002250{
Janos Follath865b3eb2019-12-16 11:46:15 +00002251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002252 const struct x509_crt_verify_string *cur;
2253 char *p = buf;
2254 size_t n = size;
2255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002256 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
2257 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002258 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002259 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002261 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002262 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002263 flags ^= cur->code;
2264 }
2265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002266 if (flags != 0) {
2267 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
2268 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002269 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002270 }
2271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002272 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002273}
2274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002275#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002276int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2277 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002278{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002279 unsigned int usage_must, usage_may;
2280 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002283 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
2284 return 0;
2285 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002286
2287 usage_must = usage & ~may_mask;
2288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002289 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
2290 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2291 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002292
2293 usage_may = usage & may_mask;
2294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002295 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
2296 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2297 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002299 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002300}
2301#endif
2302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2305 const char *usage_oid,
2306 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002307{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002309
2310 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002311 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2312 return 0;
2313 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002314
2315 /*
2316 * Look for the requested usage (or wildcard ANY) in our list
2317 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002321 if (cur_oid->len == usage_len &&
2322 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2323 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002324 }
2325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002326 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2327 return 0;
2328 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002329 }
2330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002331 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336/*
2337 * Return 1 if the certificate is revoked, or 0 otherwise.
2338 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002339int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002340{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002343 while (cur != NULL && cur->serial.len != 0) {
2344 if (crt->serial.len == cur->serial.len &&
2345 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2346 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347 }
2348
2349 cur = cur->next;
2350 }
2351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002352 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002353}
2354
2355/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002356 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002357 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002358 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2360 mbedtls_x509_crl *crl_list,
2361 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002362{
2363 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2365 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002367 if (ca == NULL) {
2368 return flags;
2369 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002371 while (crl_list != NULL) {
2372 if (crl_list->version == 0 ||
2373 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002374 crl_list = crl_list->next;
2375 continue;
2376 }
2377
2378 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002379 * Check if the CA is configured to sign CRLs
2380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002382 if (mbedtls_x509_crt_check_key_usage(ca,
2383 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002385 break;
2386 }
2387#endif
2388
2389 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002390 * Check if CRL is correctly signed by the trusted CA
2391 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002393 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002394 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002396 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002397 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002400 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2401 if (mbedtls_md(md_info, crl_list->tbs.p, crl_list->tbs.len, 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 }
2406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002408 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002411 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2412 crl_list->sig_md, hash, mbedtls_md_get_size(md_info),
2413 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002415 break;
2416 }
2417
2418 /*
2419 * Check for validity of CRL (Do not drop out)
2420 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002421 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002423 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002425 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002426 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002427 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002428
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002429 /*
2430 * Check if certificate is revoked
2431 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002432 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002434 break;
2435 }
2436
2437 crl_list = crl_list->next;
2438 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002440 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002443
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002444/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002445 * Check the signature of a certificate by its parent
2446 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002447static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2448 mbedtls_x509_crt *parent,
2449 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002450{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002451 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002452 size_t hash_len;
2453#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2454 const mbedtls_md_info_t *md_info;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002455 md_info = mbedtls_md_info_from_type(child->sig_md);
2456 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002457
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002458 /* Note: hash errors can happen only after an internal error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2460 return -1;
2461 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002462#else
Jaeden Amero34973232019-02-20 10:32:28 +00002463 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(child->sig_md);
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002466 if (psa_hash_setup(&hash_operation, hash_alg) != PSA_SUCCESS) {
2467 return -1;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002468 }
2469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002470 if (psa_hash_update(&hash_operation, child->tbs.p, child->tbs.len)
2471 != PSA_SUCCESS) {
2472 return -1;
2473 }
2474
2475 if (psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_len)
2476 != PSA_SUCCESS) {
2477 return -1;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002478 }
2479#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002480 /* Skip expensive computation on obvious mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002481 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2482 return -1;
2483 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002484
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002485#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002486 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2487 return mbedtls_pk_verify_restartable(&parent->pk,
2488 child->sig_md, hash, hash_len,
2489 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002490 }
2491#else
2492 (void) rs_ctx;
2493#endif
2494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002495 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2496 child->sig_md, hash, hash_len,
2497 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002498}
2499
2500/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002501 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2502 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002503 *
2504 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002505 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002506static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2507 const mbedtls_x509_crt *parent,
2508 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002509{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002510 int need_ca_bit;
2511
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002512 /* Parent must be the issuer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002513 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2514 return -1;
2515 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002516
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002517 /* Parent must have the basicConstraints CA bit set as a general rule */
2518 need_ca_bit = 1;
2519
2520 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002521 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002522 need_ca_bit = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 }
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002524
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002525 if (need_ca_bit && !parent->ca_istrue) {
2526 return -1;
2527 }
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002529#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002530 if (need_ca_bit &&
2531 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2532 return -1;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002533 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002534#endif
2535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002536 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002537}
2538
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002539/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002540 * Find a suitable parent for child in candidates, or return NULL.
2541 *
2542 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002543 * 1. subject name matches child's issuer
2544 * 2. if necessary, the CA bit is set and key usage allows signing certs
2545 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002546 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002547 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002548 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002549 * If there's a suitable candidate which is also time-valid, return the first
2550 * such. Otherwise, return the first suitable candidate (or NULL if there is
2551 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002552 *
2553 * The rationale for this rule is that someone could have a list of trusted
2554 * roots with two versions on the same root with different validity periods.
2555 * (At least one user reported having such a list and wanted it to just work.)
2556 * The reason we don't just require time-validity is that generally there is
2557 * only one version, and if it's expired we want the flags to state that
2558 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002559 *
2560 * The rationale for rule 3 (signature for trusted roots) is that users might
2561 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002562 * way we select the correct one is by checking the signature (as we don't
2563 * rely on key identifier extensions). (This is one way users might choose to
2564 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002565 *
2566 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002567 * - [in] child: certificate for which we're looking for a parent
2568 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002569 * - [out] r_parent: parent found (or NULL)
2570 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002571 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2572 * of the chain, 0 otherwise
2573 * - [in] path_cnt: number of intermediates seen so far
2574 * - [in] self_cnt: number of self-signed intermediates seen so far
2575 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002576 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002577 *
2578 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002579 * - 0 on success
2580 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002581 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002582static int x509_crt_find_parent_in(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002583 mbedtls_x509_crt *child,
2584 mbedtls_x509_crt *candidates,
2585 mbedtls_x509_crt **r_parent,
2586 int *r_signature_is_good,
2587 int top,
2588 unsigned path_cnt,
2589 unsigned self_cnt,
2590 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002591{
Janos Follath865b3eb2019-12-16 11:46:15 +00002592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002593 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002594 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002595
2596#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002597 /* did we have something in progress? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002598 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002599 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002600 parent = rs_ctx->parent;
2601 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002602 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002603
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002604 /* clear saved state */
2605 rs_ctx->parent = NULL;
2606 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002607 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002608
2609 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002610 goto check_signature;
2611 }
2612#endif
2613
2614 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002615 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002617 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002618 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002619 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002620 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002621 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002622
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002623 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002624 if (parent->max_pathlen > 0 &&
2625 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002626 continue;
2627 }
2628
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002629 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002630#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2631check_signature:
2632#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002633 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002634
2635#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002637 /* save state */
2638 rs_ctx->parent = parent;
2639 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002640 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002642 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002643 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002644#else
2645 (void) ret;
2646#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002647
2648 signature_is_good = ret == 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002650 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002652
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002653 /* optional time check */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002654 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2655 mbedtls_x509_time_is_future(&parent->valid_from)) {
2656 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002657 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002658 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002659 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002660
2661 continue;
2662 }
2663
Andy Gross1f627142019-01-30 10:25:53 -06002664 *r_parent = parent;
2665 *r_signature_is_good = signature_is_good;
2666
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002667 break;
2668 }
2669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002670 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002671 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002672 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002673 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002674
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002675 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002676}
2677
2678/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002679 * Find a parent in trusted CAs or the provided chain, or return NULL.
2680 *
2681 * Searches in trusted CAs first, and return the first suitable parent found
2682 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002683 *
2684 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002685 * - [in] child: certificate for which we're looking for a parent, followed
2686 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002687 * - [in] trust_ca: list of locally trusted certificates
2688 * - [out] parent: parent found (or NULL)
2689 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2690 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2691 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2692 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002693 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002694 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002695 *
2696 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002697 * - 0 on success
2698 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002699 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002700static int x509_crt_find_parent(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002701 mbedtls_x509_crt *child,
2702 mbedtls_x509_crt *trust_ca,
2703 mbedtls_x509_crt **parent,
2704 int *parent_is_trusted,
2705 int *signature_is_good,
2706 unsigned path_cnt,
2707 unsigned self_cnt,
2708 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002709{
Janos Follath865b3eb2019-12-16 11:46:15 +00002710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002711 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002712
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002713 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002714
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002715#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002716 /* restore then clear saved state if we have some stored */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002717 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002718 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002719 rs_ctx->parent_is_trusted = -1;
2720 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002721#endif
2722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002723 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002724 search_list = *parent_is_trusted ? trust_ca : child->next;
2725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002726 ret = x509_crt_find_parent_in(child, search_list,
2727 parent, signature_is_good,
2728 *parent_is_trusted,
2729 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002730
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002731#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002732 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002733 /* save state */
2734 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002735 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002736 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002737#else
2738 (void) ret;
2739#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002740
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002741 /* stop here if found or already in second iteration */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002742 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002743 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002744 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002745
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002746 /* prepare second iteration */
2747 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002748 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002749
2750 /* extra precaution against mistakes in the caller */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002751 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002752 *parent_is_trusted = 0;
2753 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002754 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002757}
2758
2759/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002760 * Check if an end-entity certificate is locally trusted
2761 *
2762 * Currently we require such certificates to be self-signed (actually only
2763 * check for self-issued as self-signatures are not checked)
2764 */
2765static int x509_crt_check_ee_locally_trusted(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 mbedtls_x509_crt *crt,
2767 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002768{
2769 mbedtls_x509_crt *cur;
2770
2771 /* must be self-issued */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002772 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2773 return -1;
2774 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002775
2776 /* look for an exact match with trusted cert */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002777 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2778 if (crt->raw.len == cur->raw.len &&
2779 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2780 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002781 }
2782 }
2783
2784 /* too bad */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002786}
2787
2788/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002789 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002790 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002791 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2792 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002793 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002794 * such that every cert in the chain is a child of the next one,
2795 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002796 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002797 * Verify that chain and return it with flags for all issues found.
2798 *
2799 * Special cases:
2800 * - EE == Rj -> return a one-element list containing it
2801 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2802 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002803 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002804 * Tests for (aspects of) this function should include at least:
2805 * - trusted EE
2806 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002807 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002808 * - if relevant: EE untrusted
2809 * - if relevant: EE -> intermediate, untrusted
2810 * with the aspect under test checked at each relevant level (EE, int, root).
2811 * For some aspects longer chains are required, but usually length 2 is
2812 * enough (but length 1 is not in general).
2813 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002814 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002815 * - [in] crt: the cert list EE, C1, ..., Cn
2816 * - [in] trust_ca: the trusted list R1, ..., Rp
2817 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002818 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002819 * Only valid when return value is 0, may contain garbage otherwise!
2820 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002821 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002822 *
2823 * Return value:
2824 * - non-zero if the chain could not be fully built and examined
2825 * - 0 is the chain was successfully built and examined,
2826 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002827 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002828static int x509_crt_verify_chain(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002829 mbedtls_x509_crt *crt,
2830 mbedtls_x509_crt *trust_ca,
2831 mbedtls_x509_crl *ca_crl,
2832 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2833 void *p_ca_cb,
2834 const mbedtls_x509_crt_profile *profile,
2835 mbedtls_x509_crt_verify_chain *ver_chain,
2836 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002837{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002838 /* Don't initialize any of those variables here, so that the compiler can
2839 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002841 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002842 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002843 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002844 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002845 int parent_is_trusted;
2846 int child_is_trusted;
2847 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002848 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002849 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002850
2851#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2852 /* resume if we had an operation in progress */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002854 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002855 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002856 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002857
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002858 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002859 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002860 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002861 flags = &cur->flags;
2862
2863 goto find_parent;
2864 }
2865#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002866
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002867 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002868 self_cnt = 0;
2869 parent_is_trusted = 0;
2870 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002872 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002873 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002874 cur = &ver_chain->items[ver_chain->len];
2875 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002876 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002877 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002878 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002879
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002880 /* Check time-validity (all certificates) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002882 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002883 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002885 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002886 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002887 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002888
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002889 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002890 if (child_is_trusted) {
2891 return 0;
2892 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002893
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002894 /* Check signature algorithm: MD & PK algs */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002895 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002896 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002899 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002900 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002902
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002903 /* Special case: EE certs that are locally trusted */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002904 if (ver_chain->len == 1 &&
2905 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2906 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002907 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002908
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002909#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2910find_parent:
2911#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002912
2913 /* Obtain list of potential trusted signers from CA callback,
2914 * or use statically provided list. */
2915#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002916 if (f_ca_cb != NULL) {
2917 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2918 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002919 ver_chain->trust_ca_cb_result = NULL;
2920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002921 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2922 if (ret != 0) {
2923 return MBEDTLS_ERR_X509_FATAL_ERROR;
2924 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002925
2926 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002927 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002928#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2929 {
2930 ((void) f_ca_cb);
2931 ((void) p_ca_cb);
2932 cur_trust_ca = trust_ca;
2933 }
2934
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002935 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002936 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2937 &parent_is_trusted, &signature_is_good,
2938 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002939
2940#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002941 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002942 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002943 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002944 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002945 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002947 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002948 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002949#else
2950 (void) ret;
2951#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002952
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002953 /* No parent? We're done here */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002954 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002955 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002956 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002957 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002958
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002959 /* Count intermediate self-issued (not necessarily self-signed) certs.
2960 * These can occur with some strategies for key rollover, see [SIRO],
2961 * and should be excluded from max_pathlen checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002962 if (ver_chain->len != 1 &&
2963 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002964 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002965 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002966
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002967 /* path_cnt is 0 for the first intermediate CA,
2968 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002969 if (!parent_is_trusted &&
2970 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002971 /* return immediately to avoid overflow the chain array */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002972 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002974
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002975 /* signature was checked while searching parent */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002976 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002977 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002978 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002979
2980 /* check size of signing key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002981 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002986 /* Check trusted CA's CRL for the given crt */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002987 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002988#else
2989 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002990#endif
2991
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002992 /* prepare for next iteration */
2993 child = parent;
2994 parent = NULL;
2995 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002996 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002997 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002998}
2999
3000/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003001 * Check for CN match
3002 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003003static int x509_crt_check_cn(const mbedtls_x509_buf *name,
3004 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003005{
3006 /* try exact match */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003007 if (name->len == cn_len &&
3008 x509_memcasecmp(cn, name->p, cn_len) == 0) {
3009 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003010 }
3011
3012 /* try wildcard match */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003013 if (x509_check_wildcard(cn, name) == 0) {
3014 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003015 }
3016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003017 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003018}
3019
3020/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003021 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3022 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003023static int x509_crt_check_san(const mbedtls_x509_buf *name,
3024 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003025{
3026 const unsigned char san_type = (unsigned char) name->tag &
3027 MBEDTLS_ASN1_TAG_VALUE_MASK;
3028
3029 /* dNSName */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
3031 return x509_crt_check_cn(name, cn, cn_len);
3032 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003033
3034 /* (We may handle other types here later.) */
3035
3036 /* Unrecognized type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003037 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003038}
3039
3040/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003041 * Verify the requested CN - only call this if cn is not NULL!
3042 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003043static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3044 const char *cn,
3045 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003046{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003047 const mbedtls_x509_name *name;
3048 const mbedtls_x509_sequence *cur;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003049 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
3052 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
3053 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003054 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003055 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003056 }
3057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003058 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003059 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003060 }
3061 } else {
3062 for (name = &crt->subject; name != NULL; name = name->next) {
3063 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3064 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
3065 break;
3066 }
3067 }
3068
3069 if (name == NULL) {
3070 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3071 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003072 }
3073}
3074
3075/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003076 * Merge the flags for all certs in the chain, after calling callback
3077 */
3078static int x509_crt_merge_flags_with_cb(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003079 uint32_t *flags,
3080 const mbedtls_x509_crt_verify_chain *ver_chain,
3081 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3082 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003083{
Janos Follath865b3eb2019-12-16 11:46:15 +00003084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003085 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003086 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003087 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003089 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003090 cur = &ver_chain->items[i-1];
3091 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003093 if (NULL != f_vrfy) {
3094 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3095 return ret;
3096 }
3097 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003098
3099 *flags |= cur_flags;
3100 }
3101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003103}
3104
3105/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003106 * Verify the certificate validity, with profile, restartable version
3107 *
3108 * This function:
3109 * - checks the requested CN (if any)
3110 * - checks the type and size of the EE cert's key,
3111 * as that isn't done as part of chain building/verification currently
3112 * - builds and verifies the chain
3113 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003114 *
3115 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3116 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3117 * verification routine to search for trusted signers, and CRLs will
3118 * be disabled. Otherwise, `trust_ca` will be used as the static list
3119 * of trusted signers, and `ca_crl` will be use as the static list
3120 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003121 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003122static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3123 mbedtls_x509_crt *trust_ca,
3124 mbedtls_x509_crl *ca_crl,
3125 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3126 void *p_ca_cb,
3127 const mbedtls_x509_crt_profile *profile,
3128 const char *cn, uint32_t *flags,
3129 int (*f_vrfy)(void *,
3130 mbedtls_x509_crt *,
3131 int,
3132 uint32_t *),
3133 void *p_vrfy,
3134 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003135{
Janos Follath865b3eb2019-12-16 11:46:15 +00003136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003137 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003138 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003139 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003140
3141 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003142 ee_flags = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003143 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003146 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3147 goto exit;
3148 }
3149
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003150 /* check name if requested */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003151 if (cn != NULL) {
3152 x509_crt_verify_name(crt, cn, &ee_flags);
3153 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003154
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003155 /* Check the type and size of the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003156 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003158 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003159 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003160 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003162 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003163 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003164 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003165
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003166 /* Check the chain */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003167 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3168 f_ca_cb, p_ca_cb, profile,
3169 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003171 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003172 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003173 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003174
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003175 /* Merge end-entity flags */
3176 ver_chain.items[0].flags |= ee_flags;
3177
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003178 /* Build final flags, calling callback on the way if any */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003179 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003180
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003181exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003182
3183#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003184 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3185 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003186 ver_chain.trust_ca_cb_result = NULL;
3187#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3188
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003189#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003190 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3191 mbedtls_x509_crt_restart_free(rs_ctx);
3192 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003193#endif
3194
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003195 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3196 * the SSL module for authmode optional, but non-zero return from the
3197 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003198 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003199 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003200 }
3201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003202 if (ret != 0) {
3203 *flags = (uint32_t) -1;
3204 return ret;
3205 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003207 if (*flags != 0) {
3208 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3209 }
3210
3211 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003212}
3213
Hanno Becker3116fb32019-03-28 13:34:42 +00003214
3215/*
3216 * Verify the certificate validity (default profile, not restartable)
3217 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003218int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3219 mbedtls_x509_crt *trust_ca,
3220 mbedtls_x509_crl *ca_crl,
3221 const char *cn, uint32_t *flags,
3222 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3223 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003224{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003225 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3226 NULL, NULL,
3227 &mbedtls_x509_crt_profile_default,
3228 cn, flags,
3229 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003230}
3231
3232/*
3233 * Verify the certificate validity (user-chosen profile, not restartable)
3234 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003235int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3236 mbedtls_x509_crt *trust_ca,
3237 mbedtls_x509_crl *ca_crl,
3238 const mbedtls_x509_crt_profile *profile,
3239 const char *cn, uint32_t *flags,
3240 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3241 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003242{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003243 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3244 NULL, NULL,
3245 profile, cn, flags,
3246 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003247}
3248
3249#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3250/*
3251 * Verify the certificate validity (user-chosen profile, CA callback,
3252 * not restartable).
3253 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003254int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3255 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3256 void *p_ca_cb,
3257 const mbedtls_x509_crt_profile *profile,
3258 const char *cn, uint32_t *flags,
3259 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3260 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003261{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003262 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3263 f_ca_cb, p_ca_cb,
3264 profile, cn, flags,
3265 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003266}
3267#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003269int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3270 mbedtls_x509_crt *trust_ca,
3271 mbedtls_x509_crl *ca_crl,
3272 const mbedtls_x509_crt_profile *profile,
3273 const char *cn, uint32_t *flags,
3274 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3275 void *p_vrfy,
3276 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003277{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003278 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3279 NULL, NULL,
3280 profile, cn, flags,
3281 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003282}
3283
3284
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003285/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003286 * Initialize a certificate chain
3287 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003288void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003289{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003290 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003291}
3292
3293/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003294 * Unallocate all certificate data
3295 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003296void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298 mbedtls_x509_crt *cert_cur = crt;
3299 mbedtls_x509_crt *cert_prv;
3300 mbedtls_x509_name *name_cur;
3301 mbedtls_x509_name *name_prv;
3302 mbedtls_x509_sequence *seq_cur;
3303 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 if (crt == NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003306 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003307 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003309 do {
3310 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003313 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003314#endif
3315
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003316 name_cur = cert_cur->issuer.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003317 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003318 name_prv = name_cur;
3319 name_cur = name_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3321 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003322 }
3323
3324 name_cur = cert_cur->subject.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003325 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003326 name_prv = name_cur;
3327 name_cur = name_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003328 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3329 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003330 }
3331
3332 seq_cur = cert_cur->ext_key_usage.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003333 while (seq_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003334 seq_prv = seq_cur;
3335 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 mbedtls_platform_zeroize(seq_prv,
3337 sizeof(mbedtls_x509_sequence));
3338 mbedtls_free(seq_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003339 }
3340
3341 seq_cur = cert_cur->subject_alt_names.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003342 while (seq_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003343 seq_prv = seq_cur;
3344 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003345 mbedtls_platform_zeroize(seq_prv,
3346 sizeof(mbedtls_x509_sequence));
3347 mbedtls_free(seq_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003348 }
3349
Ron Eldor74d9acc2019-03-21 14:00:03 +02003350 seq_cur = cert_cur->certificate_policies.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003351 while (seq_cur != NULL) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02003352 seq_prv = seq_cur;
3353 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003354 mbedtls_platform_zeroize(seq_prv,
3355 sizeof(mbedtls_x509_sequence));
3356 mbedtls_free(seq_prv);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003357 }
3358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003359 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3360 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3361 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003362 }
3363
3364 cert_cur = cert_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003365 } while (cert_cur != NULL);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003366
3367 cert_cur = crt;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003368 do {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003369 cert_prv = cert_cur;
3370 cert_cur = cert_cur->next;
3371
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003372 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3373 if (cert_prv != crt) {
3374 mbedtls_free(cert_prv);
3375 }
3376 } while (cert_cur != NULL);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003377}
3378
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003379#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3380/*
3381 * Initialize a restart context
3382 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003383void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003384{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003385 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003386
3387 ctx->parent = NULL;
3388 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003389 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003390
3391 ctx->parent_is_trusted = -1;
3392
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003393 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003394 ctx->self_cnt = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003396}
3397
3398/*
3399 * Free the components of a restart context
3400 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003402{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003403 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003404 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003405 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003407 mbedtls_pk_restart_free(&ctx->pk);
3408 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003409}
3410#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#endif /* MBEDTLS_X509_CRT_PARSE_C */