blob: 6728fa0ae12119cfcf8b9bacdfc937e02a8b9db6 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006 */
7/*
8 * The ITU-T X.509 standard defines a certificate format for PKI.
9 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020010 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
11 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
12 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020013 *
14 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
15 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020016 *
17 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000025#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000028
Gilles Peskinea8cd2e62024-05-17 19:00:46 +020029#include <limits.h>
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#endif
35
Andrzej Kurekd4a65532018-10-31 06:18:39 -040036#if defined(MBEDTLS_USE_PSA_CRYPTO)
37#include "psa/crypto.h"
38#include "mbedtls/psa_util.h"
39#endif
40
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010045#endif
46
Daniel Axtens301db662020-05-28 11:43:41 +100047#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010048#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049#include <windows.h>
50#else
51#include <time.h>
52#endif
Daniel Axtens301db662020-05-28 11:43:41 +100053#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020057#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#include <sys/types.h>
59#include <sys/stat.h>
60#include <dirent.h>
Eduardo Silva32ffb2b2019-04-25 10:43:26 -060061#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000062#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063#endif
64
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020065/*
66 * Item in a verification chain: cert and flags for it
67 */
68typedef struct {
69 mbedtls_x509_crt *crt;
70 uint32_t flags;
71} x509_crt_verify_chain_item;
72
73/*
74 * Max size of verification chain: end-entity + intermediates + trusted root
75 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020077
Gilles Peskine0ecd7192021-06-07 21:24:26 +020078/* Default profile. Do not remove items unless there are serious security
79 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020080const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
81{
Gilles Peskine5e79cb32017-05-04 16:17:21 +020082 /* Only SHA-2 hashes */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
84 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
85 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
86 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020087 0xFFFFFFF, /* Any PK alg */
88 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020089 2048,
90};
91
92/*
93 * Next-default profile
94 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020095const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
96{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020097 /* Hashes from SHA-256 and above */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
99 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
100 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200101 0xFFFFFFF, /* Any PK alg */
102#if defined(MBEDTLS_ECP_C)
103 /* Curves at or above 128-bit security level */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
105 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
106 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
107 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
108 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
109 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
110 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200111#else
112 0,
113#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200114 2048,
115};
116
117/*
118 * NSA Suite B Profile
119 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200120const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
121{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200122 /* Only SHA-256 and 384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
124 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200125 /* Only ECDSA */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200128#if defined(MBEDTLS_ECP_C)
129 /* Only NIST P-256 and P-384 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
131 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200132#else
133 0,
134#endif
135 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200136};
137
138/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200139 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200140 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200141 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
143 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200144{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 if (md_alg == MBEDTLS_MD_NONE) {
146 return -1;
147 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
150 return 0;
151 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200154}
155
156/*
157 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200158 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200159 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
161 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200162{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 if (pk_alg == MBEDTLS_PK_NONE) {
164 return -1;
165 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
168 return 0;
169 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200172}
173
174/*
175 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200176 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200177 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
179 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200180{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200182
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
185 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
186 return 0;
187 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190 }
191#endif
192
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200193#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200195 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 pk_alg == MBEDTLS_PK_ECKEY_DH) {
197 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 if (gid == MBEDTLS_ECP_DP_NONE) {
200 return -1;
201 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
204 return 0;
205 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200208 }
209#endif
210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200212}
213
214/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000215 * Like memcmp, but case-insensitive and always returns -1 if different
216 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000218{
219 size_t i;
220 unsigned char diff;
221 const unsigned char *n1 = s1, *n2 = s2;
222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000224 diff = n1[i] ^ n2[i];
225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000227 continue;
228 }
229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 if (diff == 32 &&
231 ((n1[i] >= 'a' && n1[i] <= 'z') ||
232 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
233 continue;
234 }
235
236 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000237 }
238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000240}
241
242/*
243 * Return 0 if name matches wildcard, -1 otherwise
244 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000246{
247 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000249
250 /* We can't have a match if there is no wildcard to match */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
252 return -1;
253 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 for (i = 0; i < cn_len; ++i) {
256 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000257 cn_idx = i;
258 break;
259 }
260 }
261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 if (cn_idx == 0) {
263 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000264 }
265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 if (cn_len - cn_idx == name->len - 1 &&
267 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
268 return 0;
269 }
270
271 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000272}
273
274/*
275 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
276 * variations (but not all).
277 *
278 * Return 0 if equal, -1 otherwise.
279 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000281{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000283 a->len == b->len &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 memcmp(a->p, b->p, b->len) == 0) {
285 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000286 }
287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
289 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000290 a->len == b->len &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 x509_memcasecmp(a->p, b->p, b->len) == 0) {
292 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000293 }
294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000296}
297
298/*
299 * Compare two X.509 Names (aka rdnSequence).
300 *
301 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
302 * we sometimes return unequal when the full algorithm would return equal,
303 * but never the other way. (In particular, we don't do Unicode normalisation
304 * or space folding.)
305 *
306 * Return 0 if equal, -1 otherwise.
307 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000309{
310 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 while (a != NULL || b != NULL) {
312 if (a == NULL || b == NULL) {
313 return -1;
314 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000315
316 /* type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000318 a->oid.len != b->oid.len ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
320 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000321 }
322
323 /* value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 if (x509_string_cmp(&a->val, &b->val) != 0) {
325 return -1;
326 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000327
328 /* structure of the list of sets */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 if (a->next_merged != b->next_merged) {
330 return -1;
331 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000332
333 a = a->next;
334 b = b->next;
335 }
336
337 /* a == NULL == b */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000339}
340
341/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200342 * Reset (init or clear) a verify_chain
343 */
344static void x509_crt_verify_chain_reset(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200346{
347 size_t i;
348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200350 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000351 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200352 }
353
354 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000355
356#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
357 ver_chain->trust_ca_cb_result = NULL;
358#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200359}
360
361/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
363 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364static int x509_get_version(unsigned char **p,
365 const unsigned char *end,
366 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367{
Janos Follath865b3eb2019-12-16 11:46:15 +0000368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369 size_t len;
370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
372 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
373 0)) != 0) {
374 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375 *ver = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377 }
378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 }
381
382 end = *p + len;
383
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
385 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
386 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 if (*p != end) {
389 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
390 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
391 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394}
395
396/*
397 * Validity ::= SEQUENCE {
398 * notBefore Time,
399 * notAfter Time }
400 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401static int x509_get_dates(unsigned char **p,
402 const unsigned char *end,
403 mbedtls_x509_time *from,
404 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405{
Janos Follath865b3eb2019-12-16 11:46:15 +0000406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 size_t len;
408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
410 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
411 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
412 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
414 end = *p + len;
415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
417 return ret;
418 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
421 return ret;
422 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 if (*p != end) {
425 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
426 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
427 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430}
431
432/*
433 * X.509 v2/v3 unique identifier (not parsed)
434 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435static int x509_get_uid(unsigned char **p,
436 const unsigned char *end,
437 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438{
Janos Follath865b3eb2019-12-16 11:46:15 +0000439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 if (*p == end) {
442 return 0;
443 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445 uid->tag = **p;
446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
448 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
449 n)) != 0) {
450 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
451 return 0;
452 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 }
456
457 uid->p = *p;
458 *p += uid->len;
459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461}
462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463static int x509_get_basic_constraints(unsigned char **p,
464 const unsigned char *end,
465 int *ca_istrue,
466 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467{
Janos Follath865b3eb2019-12-16 11:46:15 +0000468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469 size_t len;
470
471 /*
472 * BasicConstraints ::= SEQUENCE {
473 * cA BOOLEAN DEFAULT FALSE,
474 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
475 */
476 *ca_istrue = 0; /* DEFAULT FALSE */
477 *max_pathlen = 0; /* endless */
478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
480 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
481 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 }
483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 if (*p == end) {
485 return 0;
486 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
489 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
490 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
491 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 if (ret != 0) {
494 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
495 }
496
497 if (*ca_istrue != 0) {
498 *ca_istrue = 1;
499 }
500 }
501
502 if (*p == end) {
503 return 0;
504 }
505
506 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
507 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
508 }
509
510 if (*p != end) {
511 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
512 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
513 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514
Andrzej Kurek16050742020-04-14 09:49:52 -0400515 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
516 * overflow, which is an undefined behavior. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 if (*max_pathlen == INT_MAX) {
518 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
519 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
520 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400521
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 (*max_pathlen)++;
523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525}
526
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100527static int x509_get_ns_cert_type(unsigned char **p,
528 const unsigned char *end,
529 unsigned char *ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530{
Janos Follath865b3eb2019-12-16 11:46:15 +0000531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
535 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
536 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 if (bs.len != 1) {
539 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
540 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
541 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542
543 /* Get actual bitstring */
544 *ns_cert_type = *bs.p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546}
547
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100548static int x509_get_key_usage(unsigned char **p,
549 const unsigned char *end,
550 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551{
Janos Follath865b3eb2019-12-16 11:46:15 +0000552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200553 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100556 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
557 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
558 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100560 if (bs.len < 1) {
561 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
562 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
563 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
565 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200566 *key_usage = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200568 *key_usage |= (unsigned int) bs.p[i] << (8*i);
569 }
570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572}
573
574/*
575 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
576 *
577 * KeyPurposeId ::= OBJECT IDENTIFIER
578 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579static int x509_get_ext_key_usage(unsigned char **p,
580 const unsigned char *end,
581 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582{
Janos Follath865b3eb2019-12-16 11:46:15 +0000583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100585 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
586 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
587 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
589 /* Sequence length must be >= 1 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 if (ext_key_usage->buf.p == NULL) {
591 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
592 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
593 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596}
597
598/*
599 * SubjectAltName ::= GeneralNames
600 *
601 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
602 *
603 * GeneralName ::= CHOICE {
604 * otherName [0] OtherName,
605 * rfc822Name [1] IA5String,
606 * dNSName [2] IA5String,
607 * x400Address [3] ORAddress,
608 * directoryName [4] Name,
609 * ediPartyName [5] EDIPartyName,
610 * uniformResourceIdentifier [6] IA5String,
611 * iPAddress [7] OCTET STRING,
612 * registeredID [8] OBJECT IDENTIFIER }
613 *
614 * OtherName ::= SEQUENCE {
615 * type-id OBJECT IDENTIFIER,
616 * value [0] EXPLICIT ANY DEFINED BY type-id }
617 *
618 * EDIPartyName ::= SEQUENCE {
619 * nameAssigner [0] DirectoryString OPTIONAL,
620 * partyName [1] DirectoryString }
621 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300622 * NOTE: we list all types, but only use dNSName and otherName
623 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100625static int x509_get_subject_alt_name(unsigned char **p,
626 const unsigned char *end,
627 mbedtls_x509_sequence *subject_alt_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628{
Janos Follath865b3eb2019-12-16 11:46:15 +0000629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632
633 /* Get main sequence tag */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
635 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
636 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
637 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 if (*p + len != end) {
640 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
641 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
642 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 while (*p < end) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300645 mbedtls_x509_subject_alternative_name dummy_san_buf;
hanno-becker75ab0762023-02-08 08:46:42 -0500646 mbedtls_x509_buf tmp_san_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100647 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
Ron Eldordbbd9662019-05-15 15:46:03 +0300648
hanno-becker75ab0762023-02-08 08:46:42 -0500649 tmp_san_buf.tag = **p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650 (*p)++;
hanno-becker75ab0762023-02-08 08:46:42 -0500651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100652 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
653 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
654 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655
hanno-becker75ab0762023-02-08 08:46:42 -0500656 tmp_san_buf.p = *p;
657 tmp_san_buf.len = tag_len;
658
659 if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100660 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
661 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
662 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100663 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
Ron Eldordbbd9662019-05-15 15:46:03 +0300665 /*
irwir74aee1c2019-09-21 18:21:48 +0300666 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300667 */
hanno-becker75ab0762023-02-08 08:46:42 -0500668 ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &dummy_san_buf);
Ron Eldordbbd9662019-05-15 15:46:03 +0300669 /*
670 * In case the extension is malformed, return an error,
671 * and clear the allocated sequences.
672 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100673 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300674 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
675 mbedtls_x509_sequence *seq_prv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100676 while (seq_cur != NULL) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300677 seq_prv = seq_cur;
678 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100679 mbedtls_platform_zeroize(seq_prv,
680 sizeof(mbedtls_x509_sequence));
681 mbedtls_free(seq_prv);
Ron Eldordbbd9662019-05-15 15:46:03 +0300682 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300683 subject_alt_name->next = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100684 return ret;
Ron Eldordbbd9662019-05-15 15:46:03 +0300685 }
686
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 /* Allocate and assign next pointer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 if (cur->buf.p != NULL) {
689 if (cur->next != NULL) {
690 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
691 }
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100695 if (cur->next == NULL) {
696 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
697 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
698 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 cur = cur->next;
701 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200702
hanno-becker75ab0762023-02-08 08:46:42 -0500703 cur->buf = tmp_san_buf;
704 *p += tmp_san_buf.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 }
706
707 /* Set final sequence entry's next pointer to NULL */
708 cur->next = NULL;
709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100710 if (*p != end) {
711 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
712 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
713 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716}
717
718/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200719 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
720 *
721 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
722 *
723 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
724 *
725 * PolicyInformation ::= SEQUENCE {
726 * policyIdentifier CertPolicyId,
727 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
728 * PolicyQualifierInfo OPTIONAL }
729 *
730 * CertPolicyId ::= OBJECT IDENTIFIER
731 *
732 * PolicyQualifierInfo ::= SEQUENCE {
733 * policyQualifierId PolicyQualifierId,
734 * qualifier ANY DEFINED BY policyQualifierId }
735 *
736 * -- policyQualifierIds for Internet policy qualifiers
737 *
738 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
739 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
740 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
741 *
742 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
743 *
744 * Qualifier ::= CHOICE {
745 * cPSuri CPSuri,
746 * userNotice UserNotice }
747 *
748 * CPSuri ::= IA5String
749 *
750 * UserNotice ::= SEQUENCE {
751 * noticeRef NoticeReference OPTIONAL,
752 * explicitText DisplayText OPTIONAL }
753 *
754 * NoticeReference ::= SEQUENCE {
755 * organization DisplayText,
756 * noticeNumbers SEQUENCE OF INTEGER }
757 *
758 * DisplayText ::= CHOICE {
759 * ia5String IA5String (SIZE (1..200)),
760 * visibleString VisibleString (SIZE (1..200)),
761 * bmpString BMPString (SIZE (1..200)),
762 * utf8String UTF8String (SIZE (1..200)) }
763 *
764 * NOTE: we only parse and use anyPolicy without qualifiers at this point
765 * as defined in RFC 5280.
766 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100767static int x509_get_certificate_policies(unsigned char **p,
768 const unsigned char *end,
769 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200770{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300771 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200772 size_t len;
773 mbedtls_asn1_buf *buf;
774 mbedtls_asn1_sequence *cur = certificate_policies;
775
776 /* Get main sequence tag */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 ret = mbedtls_asn1_get_tag(p, end, &len,
778 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
779 if (ret != 0) {
780 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
781 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100783 if (*p + len != end) {
784 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
785 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
786 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200787
788 /*
789 * Cannot be an empty sequence.
790 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 if (len == 0) {
792 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
793 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
794 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100796 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200797 mbedtls_x509_buf policy_oid;
798 const unsigned char *policy_end;
799
800 /*
801 * Get the policy sequence
802 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100803 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
804 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
806 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200807
808 policy_end = *p + len;
809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100810 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
811 MBEDTLS_ASN1_OID)) != 0) {
812 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
813 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200814
815 policy_oid.tag = MBEDTLS_ASN1_OID;
816 policy_oid.len = len;
817 policy_oid.p = *p;
818
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300819 /*
820 * Only AnyPolicy is currently supported when enforcing policy.
821 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100822 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300823 /*
824 * Set the parsing return code but continue parsing, in case this
825 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
826 * is configured.
827 */
828 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
829 }
830
Ron Eldor74d9acc2019-03-21 14:00:03 +0200831 /* Allocate and assign next pointer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100832 if (cur->buf.p != NULL) {
833 if (cur->next != NULL) {
834 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
835 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 if (cur->next == NULL) {
840 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
841 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
842 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200843
844 cur = cur->next;
845 }
846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100847 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200848 buf->tag = policy_oid.tag;
849 buf->p = policy_oid.p;
850 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300851
852 *p += len;
853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100854 /*
855 * If there is an optional qualifier, then *p < policy_end
856 * Check the Qualifier len to verify it doesn't exceed policy_end.
857 */
858 if (*p < policy_end) {
859 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
860 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
861 0) {
862 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
863 }
Ron Eldor08063792019-05-13 16:38:39 +0300864 /*
865 * Skip the optional policy qualifiers.
866 */
867 *p += len;
868 }
869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100870 if (*p != policy_end) {
871 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
872 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
873 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200874 }
875
876 /* Set final sequence entry's next pointer to NULL */
877 cur->next = NULL;
878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100879 if (*p != end) {
880 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
881 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
882 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100884 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200885}
886
887/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 * X.509 v3 extensions
889 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100891static int x509_get_crt_ext(unsigned char **p,
892 const unsigned char *end,
893 mbedtls_x509_crt *crt,
894 mbedtls_x509_crt_ext_cb_t cb,
895 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896{
Janos Follath865b3eb2019-12-16 11:46:15 +0000897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200899 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100901 if (*p == end) {
902 return 0;
903 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100905 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
906 return ret;
907 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
Hanno Becker12f62fb2019-02-12 17:22:36 +0000909 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100910 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 /*
912 * Extension ::= SEQUENCE {
913 * extnID OBJECT IDENTIFIER,
914 * critical BOOLEAN DEFAULT FALSE,
915 * extnValue OCTET STRING }
916 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100917 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 int is_critical = 0; /* DEFAULT FALSE */
919 int ext_type = 0;
920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100921 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
922 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
923 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
924 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
926 end_ext_data = *p + len;
927
928 /* Get extension ID */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100929 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
930 MBEDTLS_ASN1_OID)) != 0) {
931 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
932 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
k-stachowiak463928a2018-07-24 12:50:59 +0200934 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 extn_oid.p = *p;
936 *p += extn_oid.len;
937
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 /* Get optional critical */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
940 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
941 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
942 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
944 /* Data should be octet string type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100945 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
946 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
947 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
948 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200950 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951 end_ext_octet = *p + len;
952
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100953 if (end_ext_octet != end_ext_data) {
954 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
955 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
956 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957
958 /*
959 * Detect supported extensions
960 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100961 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100963 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200964 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100965 if (cb != NULL) {
966 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
967 if (ret != 0 && is_critical) {
968 return ret;
969 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200970 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200971 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200972 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200973
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 /* No parser found, skip extension */
975 *p = end_ext_octet;
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100978 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 /* Data is marked as critical: fail */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100980 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
981 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 }
983#endif
984 continue;
985 }
986
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100987 /* Forbid repeated extensions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100988 if ((crt->ext_types & ext_type) != 0) {
989 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
990 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100991
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 crt->ext_types |= ext_type;
993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100994 switch (ext_type) {
995 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
996 /* Parse basic constraints */
997 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
998 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
999 return ret;
1000 }
1001 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001003 case MBEDTLS_X509_EXT_KEY_USAGE:
1004 /* Parse key usage */
1005 if ((ret = x509_get_key_usage(p, end_ext_octet,
1006 &crt->key_usage)) != 0) {
1007 return ret;
1008 }
1009 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001011 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1012 /* Parse extended key usage */
1013 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1014 &crt->ext_key_usage)) != 0) {
1015 return ret;
1016 }
1017 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001019 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1020 /* Parse subject alt name */
1021 if ((ret = x509_get_subject_alt_name(p, end_ext_octet,
1022 &crt->subject_alt_names)) != 0) {
1023 return ret;
1024 }
1025 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001027 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1028 /* Parse netscape certificate type */
1029 if ((ret = x509_get_ns_cert_type(p, end_ext_octet,
1030 &crt->ns_cert_type)) != 0) {
1031 return ret;
1032 }
1033 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001035 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1036 /* Parse certificate policies type */
1037 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1038 &crt->certificate_policies)) != 0) {
1039 /* Give the callback (if any) a chance to handle the extension
1040 * if it contains unsupported policies */
1041 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1042 cb(p_ctx, crt, &extn_oid, is_critical,
1043 start_ext_octet, end_ext_octet) == 0) {
1044 break;
1045 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001046
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001047#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001048 if (is_critical) {
1049 return ret;
1050 } else
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001051#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001052 /*
1053 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1054 * cannot interpret or enforce the policy. However, it is up to
1055 * the user to choose how to enforce the policies,
1056 * unless the extension is critical.
1057 */
1058 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1059 return ret;
1060 }
1061 }
1062 break;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001064 default:
1065 /*
1066 * If this is a non-critical extension, which the oid layer
1067 * supports, but there isn't an x509 parser for it,
1068 * skip the extension.
1069 */
Ron Eldordf48efa2019-04-08 13:28:24 +03001070#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 if (is_critical) {
1072 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1073 } else
Ron Eldordf48efa2019-04-08 13:28:24 +03001074#endif
1075 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076 }
1077 }
1078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001079 if (*p != end) {
1080 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1081 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1082 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001084 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085}
1086
1087/*
1088 * Parse and fill a single X.509 certificate in DER format
1089 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001090static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1091 const unsigned char *buf,
1092 size_t buflen,
1093 int make_copy,
1094 mbedtls_x509_crt_ext_cb_t cb,
1095 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096{
Janos Follath865b3eb2019-12-16 11:46:15 +00001097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 size_t len;
1099 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001102 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1103 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1104 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105
1106 /*
1107 * Check for valid input
1108 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001109 if (crt == NULL || buf == NULL) {
1110 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001113 /* Use the original buffer until we figure out actual length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001114 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001115 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116 end = p + len;
1117
1118 /*
1119 * Certificate ::= SEQUENCE {
1120 * tbsCertificate TBSCertificate,
1121 * signatureAlgorithm AlgorithmIdentifier,
1122 * signatureValue BIT STRING }
1123 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001124 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1126 mbedtls_x509_crt_free(crt);
1127 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128 }
1129
Janos Follathcc0e49d2016-02-17 14:34:12 +00001130 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001131 crt->raw.len = crt_end - buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001132 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001133 /* Create and populate a new buffer for the raw field. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001134 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1135 if (crt->raw.p == NULL) {
1136 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1137 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001139 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001140 crt->own_buffer = 1;
1141
1142 p += crt->raw.len - len;
1143 end = crt_end = p + len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001144 } else {
1145 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001146 crt->own_buffer = 0;
1147 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001148
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001149 /*
1150 * TBSCertificate ::= SEQUENCE {
1151 */
1152 crt->tbs.p = p;
1153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001154 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1155 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1156 mbedtls_x509_crt_free(crt);
1157 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 }
1159
1160 end = p + len;
1161 crt->tbs.len = end - crt->tbs.p;
1162
1163 /*
1164 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1165 *
1166 * CertificateSerialNumber ::= INTEGER
1167 *
1168 * signature AlgorithmIdentifier
1169 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001170 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1171 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1172 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1173 &sig_params1)) != 0) {
1174 mbedtls_x509_crt_free(crt);
1175 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 }
1177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001178 if (crt->version < 0 || crt->version > 2) {
1179 mbedtls_x509_crt_free(crt);
1180 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 }
1182
Andres AG7ca4a032017-03-09 16:16:11 +00001183 crt->version++;
1184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001185 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1186 &crt->sig_md, &crt->sig_pk,
1187 &crt->sig_opts)) != 0) {
1188 mbedtls_x509_crt_free(crt);
1189 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 }
1191
1192 /*
1193 * issuer Name
1194 */
1195 crt->issuer_raw.p = p;
1196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001197 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1198 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1199 mbedtls_x509_crt_free(crt);
1200 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 }
1202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001203 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1204 mbedtls_x509_crt_free(crt);
1205 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001206 }
1207
1208 crt->issuer_raw.len = p - crt->issuer_raw.p;
1209
1210 /*
1211 * Validity ::= SEQUENCE {
1212 * notBefore Time,
1213 * notAfter Time }
1214 *
1215 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001216 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1217 &crt->valid_to)) != 0) {
1218 mbedtls_x509_crt_free(crt);
1219 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001220 }
1221
1222 /*
1223 * subject Name
1224 */
1225 crt->subject_raw.p = p;
1226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001227 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1228 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1229 mbedtls_x509_crt_free(crt);
1230 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231 }
1232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001233 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1234 mbedtls_x509_crt_free(crt);
1235 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001236 }
1237
1238 crt->subject_raw.len = p - crt->subject_raw.p;
1239
1240 /*
1241 * SubjectPublicKeyInfo
1242 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001243 crt->pk_raw.p = p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001244 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1245 mbedtls_x509_crt_free(crt);
1246 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001248 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249
1250 /*
1251 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1252 * -- If present, version shall be v2 or v3
1253 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1254 * -- If present, version shall be v2 or v3
1255 * extensions [3] EXPLICIT Extensions OPTIONAL
1256 * -- If present, version shall be v3
1257 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001258 if (crt->version == 2 || crt->version == 3) {
1259 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1260 if (ret != 0) {
1261 mbedtls_x509_crt_free(crt);
1262 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263 }
1264 }
1265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001266 if (crt->version == 2 || crt->version == 3) {
1267 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1268 if (ret != 0) {
1269 mbedtls_x509_crt_free(crt);
1270 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 }
1272 }
1273
David Horstmannab617512022-10-27 11:45:01 +01001274 int extensions_allowed = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 if (crt->version != 3) {
David Horstmannab617512022-10-27 11:45:01 +01001277 extensions_allowed = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001278 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001279#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001280 if (extensions_allowed) {
1281 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1282 if (ret != 0) {
1283 mbedtls_x509_crt_free(crt);
1284 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 }
1286 }
1287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001288 if (p != end) {
1289 mbedtls_x509_crt_free(crt);
1290 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1291 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 }
1293
1294 end = crt_end;
1295
1296 /*
1297 * }
1298 * -- end of TBSCertificate
1299 *
1300 * signatureAlgorithm AlgorithmIdentifier,
1301 * signatureValue BIT STRING
1302 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1304 mbedtls_x509_crt_free(crt);
1305 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 }
1307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001308 if (crt->sig_oid.len != sig_oid2.len ||
1309 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001310 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001311 sig_params1.len != sig_params2.len ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001312 (sig_params1.len != 0 &&
1313 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1314 mbedtls_x509_crt_free(crt);
1315 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 }
1317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001318 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1319 mbedtls_x509_crt_free(crt);
1320 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001321 }
1322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001323 if (p != end) {
1324 mbedtls_x509_crt_free(crt);
1325 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1326 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327 }
1328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001329 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330}
1331
1332/*
1333 * Parse one X.509 certificate in DER format from a buffer and add them to a
1334 * chained list
1335 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001336static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1337 const unsigned char *buf,
1338 size_t buflen,
1339 int make_copy,
1340 mbedtls_x509_crt_ext_cb_t cb,
1341 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342{
Janos Follath865b3eb2019-12-16 11:46:15 +00001343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001345
1346 /*
1347 * Check for valid input
1348 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001349 if (crt == NULL || buf == NULL) {
1350 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1351 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001353 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354 prev = crt;
1355 crt = crt->next;
1356 }
1357
1358 /*
1359 * Add new certificate on the end of the chain if needed.
1360 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001361 if (crt->version != 0 && crt->next == NULL) {
1362 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001364 if (crt->next == NULL) {
1365 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1366 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367
1368 prev = crt;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001369 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371 }
1372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001373 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1374 if (ret != 0) {
1375 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 prev->next = NULL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001377 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001379 if (crt != chain) {
1380 mbedtls_free(crt);
1381 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001383 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384 }
1385
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001386 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387}
1388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001389int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1390 const unsigned char *buf,
1391 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001392{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001393 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001394}
1395
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001396int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1397 const unsigned char *buf,
1398 size_t buflen,
1399 int make_copy,
1400 mbedtls_x509_crt_ext_cb_t cb,
1401 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001402{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001403 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001404}
1405
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001406int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1407 const unsigned char *buf,
1408 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001409{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001410 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001411}
1412
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001413/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001414 * Parse one or more PEM certificates from a buffer and add them to the chained
1415 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001417int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1418 const unsigned char *buf,
1419 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420{
Janos Follath98e28a72016-05-31 14:03:54 +01001421#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001422 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001424#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425
1426 /*
1427 * Check for valid input
1428 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001429 if (chain == NULL || buf == NULL) {
1430 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1431 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432
1433 /*
1434 * Determine buffer content. Buffer contains either one DER certificate or
1435 * one or more PEM certificates.
1436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001438 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1439 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001441 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001443 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1444 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1445 }
Janos Follath98e28a72016-05-31 14:03:54 +01001446#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001447 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001448#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001451 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001455 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001456 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457 size_t use_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001458 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001460 /* If we get there, we know the string is null-terminated */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001461 ret = mbedtls_pem_read_buffer(&pem,
1462 "-----BEGIN CERTIFICATE-----",
1463 "-----END CERTIFICATE-----",
1464 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001466 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467 /*
1468 * Was PEM encoded
1469 */
1470 buflen -= use_len;
1471 buf += use_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001472 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1473 return ret;
1474 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1475 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476
1477 /*
1478 * PEM header and footer were found
1479 */
1480 buflen -= use_len;
1481 buf += use_len;
1482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001483 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001484 first_error = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001485 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486
Paul Bakker5a5fa922014-09-26 14:53:04 +02001487 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001489 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001493 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001495 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001497 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001498 /*
1499 * Quit parsing on a memory error
1500 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001501 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1502 return ret;
1503 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001505 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506 first_error = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001507 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508
1509 total_failed++;
1510 continue;
1511 }
1512
1513 success = 1;
1514 }
1515 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001517 if (success) {
1518 return total_failed;
1519 } else if (first_error) {
1520 return first_error;
1521 } else {
1522 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1523 }
Janos Follath98e28a72016-05-31 14:03:54 +01001524#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525}
1526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528/*
1529 * Load one or more certificates and add them to the chained list
1530 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001531int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532{
Janos Follath865b3eb2019-12-16 11:46:15 +00001533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534 size_t n;
1535 unsigned char *buf;
1536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001537 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1538 return ret;
1539 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001541 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 mbedtls_platform_zeroize(buf, n);
1544 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001546 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547}
1548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001549int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550{
1551 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001552#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553 int w_ret;
1554 WCHAR szDir[MAX_PATH];
1555 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001556 char *p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001557 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Paul Bakker9af723c2014-05-01 13:03:14 +02001559 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560 HANDLE hFind;
1561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001562 if (len > MAX_PATH - 3) {
1563 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1564 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001566 memset(szDir, 0, sizeof(szDir));
1567 memset(filename, 0, MAX_PATH);
1568 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001569 filename[len++] = '\\';
1570 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571 filename[len++] = '*';
1572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001573 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1574 MAX_PATH - 3);
1575 if (w_ret == 0) {
1576 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1577 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001579 hFind = FindFirstFileW(szDir, &file_data);
1580 if (hFind == INVALID_HANDLE_VALUE) {
1581 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1582 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583
1584 len = MAX_PATH - len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001585 do {
1586 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001588 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001590 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgrove0289c192023-02-10 12:52:13 +00001593 -1,
1594 p, (int) len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001595 NULL, NULL);
1596 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001597 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1598 goto cleanup;
1599 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001601 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1602 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603 ret++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001604 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605 ret += w_ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001606 }
1607 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001611 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612
Ron Eldor36d90422017-01-09 15:09:16 +02001613cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001614 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001615#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001616 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001617 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001619 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001620 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001621 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001623 if (dir == NULL) {
1624 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1625 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626
Ron Eldor63140682017-01-09 19:27:59 +02001627#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1629 closedir(dir);
1630 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001631 }
Ron Eldor63140682017-01-09 19:27:59 +02001632#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001634 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001636 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman18688702023-02-02 12:40:50 +00001637 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001638 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
Dave Rodgman18688702023-02-02 12:40:50 +00001640 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001641 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1642 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001643 } else if (stat(entry_name, &sb) == -1) {
1644 if (errno == ENOENT) {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001645 /* Broken symbolic link - ignore this entry.
1646 stat(2) will return this error for either (a) a dangling
1647 symlink or (b) a missing file.
1648 Given that we have just obtained the filename from readdir,
1649 assume that it does exist and therefore treat this as a
1650 dangling symlink. */
1651 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001652 } else {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001653 /* Some other file error; report the error. */
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001654 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1655 goto cleanup;
1656 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657 }
1658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001659 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001661 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662
1663 // Ignore parse errors
1664 //
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001665 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1666 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001667 ret++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001668 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669 ret += t_ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001672
1673cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001674 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001675
Ron Eldor63140682017-01-09 19:27:59 +02001676#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001677 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001679 }
Ron Eldor63140682017-01-09 19:27:59 +02001680#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001681
Paul Bakkerbe089b02013-10-14 15:51:50 +02001682#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001684 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001688/*
1689 * OtherName ::= SEQUENCE {
1690 * type-id OBJECT IDENTIFIER,
1691 * value [0] EXPLICIT ANY DEFINED BY type-id }
1692 *
1693 * HardwareModuleName ::= SEQUENCE {
1694 * hwType OBJECT IDENTIFIER,
1695 * hwSerialNum OCTET STRING }
1696 *
1697 * NOTE: we currently only parse and use otherName of type HwModuleName,
1698 * as defined in RFC 4108.
1699 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001700static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1701 mbedtls_x509_san_other_name *other_name)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001702{
Janos Follathab23cd12019-05-09 13:53:57 +01001703 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001704 size_t len;
1705 unsigned char *p = subject_alt_name->p;
1706 const unsigned char *end = p + subject_alt_name->len;
1707 mbedtls_x509_buf cur_oid;
1708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001709 if ((subject_alt_name->tag &
1710 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1711 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001712 /*
1713 * The given subject alternative name is not of type "othername".
1714 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001715 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001716 }
1717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001718 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1719 MBEDTLS_ASN1_OID)) != 0) {
1720 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1721 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001722
1723 cur_oid.tag = MBEDTLS_ASN1_OID;
1724 cur_oid.p = p;
1725 cur_oid.len = len;
1726
1727 /*
1728 * Only HwModuleName is currently supported.
1729 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001730 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1731 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001732 }
David Horstmann4a46d232023-08-18 18:36:02 +01001733 other_name->type_id = cur_oid;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001734
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001735 p += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001736 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1737 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
1738 0) {
1739 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1740 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001741
hanno-beckerc4f885d2023-02-08 08:50:01 -05001742 if (end != p + len) {
1743 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1744 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1745 }
1746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001747 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1748 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1749 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1750 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001751
hanno-beckerc4f885d2023-02-08 08:50:01 -05001752 if (end != p + len) {
1753 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1754 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1755 }
1756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001757 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
1758 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1759 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001760
1761 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1762 other_name->value.hardware_module_name.oid.p = p;
1763 other_name->value.hardware_module_name.oid.len = len;
1764
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001765 p += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001766 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1767 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1768 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
1769 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001770
1771 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1772 other_name->value.hardware_module_name.val.p = p;
1773 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001774 p += len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001775 if (p != end) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001776 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1777 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001778 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001779 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001780}
1781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001782static int x509_info_subject_alt_name(char **buf, size_t *size,
1783 const mbedtls_x509_sequence
1784 *subject_alt_name,
1785 const char *prefix)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001786{
Janos Follath865b3eb2019-12-16 11:46:15 +00001787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -03001788 size_t i;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001789 size_t n = *size;
1790 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001792 mbedtls_x509_subject_alternative_name san;
1793 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001794
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001795 while (cur != NULL) {
1796 memset(&san, 0, sizeof(san));
1797 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1798 if (parse_ret != 0) {
1799 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1800 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001801 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001802 } else {
1803 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001804 MBEDTLS_X509_SAFE_SNPRINTF;
1805 }
1806 cur = cur->next;
1807 continue;
1808 }
1809
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001810 switch (san.type) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001811 /*
1812 * otherName
1813 */
Ron Eldora2913912019-05-16 16:17:38 +03001814 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001815 {
1816 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001818 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001819 MBEDTLS_X509_SAFE_SNPRINTF;
1820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001821 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
David Horstmanndcf73262023-08-18 19:12:59 +01001822 &other_name->type_id) == 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001823 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001824 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001825 ret =
1826 mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001827 MBEDTLS_X509_SAFE_SNPRINTF;
1828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001829 ret = mbedtls_oid_get_numeric_string(p,
1830 n,
1831 &other_name->value.hardware_module_name.oid);
Ron Eldor890819a2019-05-13 19:03:04 +03001832 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001834 ret =
1835 mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001836 MBEDTLS_X509_SAFE_SNPRINTF;
1837
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001838 for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
1839 ret = mbedtls_snprintf(p,
1840 n,
1841 "%02X",
1842 other_name->value.hardware_module_name.val.p[i]);
Victor Barpp Gomesfb4723a2022-09-29 10:00:32 -03001843 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01001844 }
Ron Eldor890819a2019-05-13 19:03:04 +03001845 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1846 }
1847 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001848
1849 /*
1850 * dNSName
1851 */
Ron Eldora2913912019-05-16 16:17:38 +03001852 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001853 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001854 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001855 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001856 if (san.san.unstructured_name.len >= n) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001857 *p = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001858 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001859 }
Ron Eldora2913912019-05-16 16:17:38 +03001860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001861 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
Ron Eldora2913912019-05-16 16:17:38 +03001862 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001863 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001864 }
1865 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001866
1867 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001868 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001869 */
1870 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001871 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Janos Follath22f605f2019-05-10 10:37:17 +01001872 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001873 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001874 }
1875
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001876 cur = cur->next;
1877 }
1878
1879 *p = '\0';
1880
1881 *size = n;
1882 *buf = p;
1883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001884 return 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001885}
1886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001887int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1888 mbedtls_x509_subject_alternative_name *san)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001889{
Janos Follath865b3eb2019-12-16 11:46:15 +00001890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001891 switch (san_buf->tag &
1892 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1893 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
Ron Eldor890819a2019-05-13 19:03:04 +03001894 /*
1895 * otherName
1896 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001897 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001898 {
Ron Eldor890819a2019-05-13 19:03:04 +03001899 mbedtls_x509_san_other_name other_name;
1900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001901 ret = x509_get_other_name(san_buf, &other_name);
1902 if (ret != 0) {
1903 return ret;
1904 }
Ron Eldor890819a2019-05-13 19:03:04 +03001905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001906 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Ron Eldor890819a2019-05-13 19:03:04 +03001907 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 memcpy(&san->san.other_name,
1909 &other_name, sizeof(other_name));
Ron Eldor890819a2019-05-13 19:03:04 +03001910
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001911 }
Ron Eldor890819a2019-05-13 19:03:04 +03001912 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001913
Ron Eldor890819a2019-05-13 19:03:04 +03001914 /*
1915 * dNSName
1916 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001917 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001918 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
Ron Eldor890819a2019-05-13 19:03:04 +03001920 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922 memcpy(&san->san.unstructured_name,
1923 san_buf, sizeof(*san_buf));
Janos Follath6c379b42019-05-10 14:17:16 +01001924
Ron Eldor890819a2019-05-13 19:03:04 +03001925 }
1926 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001927
Ron Eldor890819a2019-05-13 19:03:04 +03001928 /*
1929 * Type not supported
1930 */
1931 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001932 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Ron Eldor890819a2019-05-13 19:03:04 +03001933 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001934 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001935}
1936
Demi Marie Obenour0e207412022-12-04 04:24:22 -05001937#define PRINT_ITEM(i) \
1938 do { \
1939 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1940 MBEDTLS_X509_SAFE_SNPRINTF; \
1941 sep = ", "; \
1942 } while (0)
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001943
Demi Marie Obenour0e207412022-12-04 04:24:22 -05001944#define CERT_TYPE(type, name) \
1945 do { \
1946 if (ns_cert_type & (type)) { \
1947 PRINT_ITEM(name); \
1948 } \
1949 } while (0)
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951static int x509_info_cert_type(char **buf, size_t *size,
1952 unsigned char ns_cert_type)
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001953{
Janos Follath865b3eb2019-12-16 11:46:15 +00001954 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001955 size_t n = *size;
1956 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001957 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001958
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001959 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1960 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1961 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1962 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1963 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1964 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1965 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1966 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001967
1968 *size = n;
1969 *buf = p;
1970
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001971 return 0;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001972}
1973
Demi Marie Obenour0e207412022-12-04 04:24:22 -05001974#define KEY_USAGE(code, name) \
1975 do { \
1976 if (key_usage & (code)) { \
1977 PRINT_ITEM(name); \
1978 } \
1979 } while (0)
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981static int x509_info_key_usage(char **buf, size_t *size,
1982 unsigned int key_usage)
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001983{
Janos Follath865b3eb2019-12-16 11:46:15 +00001984 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001985 size_t n = *size;
1986 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001987 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001989 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1990 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1991 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1992 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1993 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1994 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1995 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1996 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1997 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001998
1999 *size = n;
2000 *buf = p;
2001
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002002 return 0;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002003}
2004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002005static int x509_info_ext_key_usage(char **buf, size_t *size,
2006 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002007{
Janos Follath865b3eb2019-12-16 11:46:15 +00002008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002009 const char *desc;
2010 size_t n = *size;
2011 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002013 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002015 while (cur != NULL) {
2016 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002017 desc = "???";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002018 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002020 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002021 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002022
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002023 sep = ", ";
2024
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002025 cur = cur->next;
2026 }
2027
2028 *size = n;
2029 *buf = p;
2030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002031 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002032}
2033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034static int x509_info_cert_policies(char **buf, size_t *size,
2035 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02002036{
Janos Follath865b3eb2019-12-16 11:46:15 +00002037 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002038 const char *desc;
2039 size_t n = *size;
2040 char *p = *buf;
2041 const mbedtls_x509_sequence *cur = certificate_policies;
2042 const char *sep = "";
2043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002044 while (cur != NULL) {
2045 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02002046 desc = "???";
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002047 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002049 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002050 MBEDTLS_X509_SAFE_SNPRINTF;
2051
2052 sep = ", ";
2053
2054 cur = cur->next;
2055 }
2056
2057 *size = n;
2058 *buf = p;
2059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002060 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002061}
2062
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063/*
2064 * Return an informational string about the certificate.
2065 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002066#define BEFORE_COLON 18
2067#define BC "18"
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002068int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
2069 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002070{
Janos Follath865b3eb2019-12-16 11:46:15 +00002071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002072 size_t n;
2073 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002074 char key_size_str[BEFORE_COLON];
2075
2076 p = buf;
2077 n = size;
2078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002079 if (NULL == crt) {
2080 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01002081 MBEDTLS_X509_SAFE_SNPRINTF;
2082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002083 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01002084 }
2085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002086 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
2087 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002088 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002089 ret = mbedtls_snprintf(p, n, "%sserial number : ",
2090 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002091 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002094 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002097 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002098 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002099 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002102 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002103 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002104 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002106 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
2107 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2108 crt->valid_from.year, crt->valid_from.mon,
2109 crt->valid_from.day, crt->valid_from.hour,
2110 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002111 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002113 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
2114 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2115 crt->valid_to.year, crt->valid_to.mon,
2116 crt->valid_to.day, crt->valid_to.hour,
2117 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002118 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002120 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002121 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002123 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
2124 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002125 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002126
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002127 /* Key size */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002128 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
2129 mbedtls_pk_get_name(&crt->pk))) != 0) {
2130 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131 }
2132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002133 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
2134 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002135 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002137 /*
2138 * Optional extensions
2139 */
2140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002141 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
2142 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
2143 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002144 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 if (crt->max_pathlen > 0) {
2147 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002148 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002149 }
2150 }
2151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002152 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2153 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002154 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002156 if ((ret = x509_info_subject_alt_name(&p, &n,
2157 &crt->subject_alt_names,
2158 prefix)) != 0) {
2159 return ret;
2160 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 }
2162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
2164 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002165 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002167 if ((ret = x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
2168 return ret;
2169 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002170 }
2171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002172 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2173 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002174 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002176 if ((ret = x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
2177 return ret;
2178 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002179 }
2180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002181 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2182 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002183 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002185 if ((ret = x509_info_ext_key_usage(&p, &n,
2186 &crt->ext_key_usage)) != 0) {
2187 return ret;
2188 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002189 }
2190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002191 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2192 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002193 MBEDTLS_X509_SAFE_SNPRINTF;
2194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195 if ((ret = x509_info_cert_policies(&p, &n,
2196 &crt->certificate_policies)) != 0) {
2197 return ret;
2198 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02002199 }
2200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002201 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002202 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002205}
2206
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002207struct x509_crt_verify_string {
2208 int code;
2209 const char *string;
2210};
2211
2212static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002213 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002214 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002215 { MBEDTLS_X509_BADCERT_CN_MISMATCH,
2216 "The certificate Common Name (CN) does not match with the expected CN" },
2217 { MBEDTLS_X509_BADCERT_NOT_TRUSTED,
2218 "The certificate is not correctly signed by the trusted CA" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002219 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2220 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002221 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2222 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2223 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002224 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002225 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2226 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2227 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2228 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002229 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002230 { MBEDTLS_X509_BADCERT_BAD_PK,
2231 "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2232 { MBEDTLS_X509_BADCERT_BAD_KEY,
2233 "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002234 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235 { MBEDTLS_X509_BADCRL_BAD_PK,
2236 "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2237 { MBEDTLS_X509_BADCRL_BAD_KEY,
2238 "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002239 { 0, NULL }
2240};
2241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002242int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
2243 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002244{
Janos Follath865b3eb2019-12-16 11:46:15 +00002245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002246 const struct x509_crt_verify_string *cur;
2247 char *p = buf;
2248 size_t n = size;
2249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
2251 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002252 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002253 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002255 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002256 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002257 flags ^= cur->code;
2258 }
2259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002260 if (flags != 0) {
2261 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
2262 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002263 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002264 }
2265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002266 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002267}
2268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002270int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2271 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002272{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002273 unsigned int usage_must, usage_may;
2274 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002275 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
2278 return 0;
2279 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002280
2281 usage_must = usage & ~may_mask;
2282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002283 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
2284 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2285 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002286
2287 usage_may = usage & may_mask;
2288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002289 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
2290 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2291 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002293 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002294}
2295#endif
2296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002298int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2299 const char *usage_oid,
2300 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002303
2304 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002305 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
2306 return 0;
2307 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002308
2309 /*
2310 * Look for the requested usage (or wildcard ANY) in our list
2311 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002312 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002313 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 if (cur_oid->len == usage_len &&
2316 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2317 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002318 }
2319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002320 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2321 return 0;
2322 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002323 }
2324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002325 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002326}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002330/*
2331 * Return 1 if the certificate is revoked, or 0 otherwise.
2332 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002333int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002337 while (cur != NULL && cur->serial.len != 0) {
2338 if (crt->serial.len == cur->serial.len &&
2339 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2340 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341 }
2342
2343 cur = cur->next;
2344 }
2345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002346 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347}
2348
2349/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002350 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002351 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002352 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002353static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2354 mbedtls_x509_crl *crl_list,
2355 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002356{
2357 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2359 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002361 if (ca == NULL) {
2362 return flags;
2363 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002365 while (crl_list != NULL) {
2366 if (crl_list->version == 0 ||
2367 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002368 crl_list = crl_list->next;
2369 continue;
2370 }
2371
2372 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002373 * Check if the CA is configured to sign CRLs
2374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002376 if (mbedtls_x509_crt_check_key_usage(ca,
2377 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002379 break;
2380 }
2381#endif
2382
2383 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002384 * Check if CRL is correctly signed by the trusted CA
2385 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002386 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002387 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002388 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002389
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002390 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002391 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002394 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2395 if (mbedtls_md(md_info, crl_list->tbs.p, crl_list->tbs.len, hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002396 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002398 break;
2399 }
2400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002401 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002402 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2406 crl_list->sig_md, hash, mbedtls_md_get_size(md_info),
2407 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002409 break;
2410 }
2411
2412 /*
2413 * Check for validity of CRL (Do not drop out)
2414 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002417 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002418
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002419 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002420 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002421 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002422
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002423 /*
2424 * Check if certificate is revoked
2425 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002428 break;
2429 }
2430
2431 crl_list = crl_list->next;
2432 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002434 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002437
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002438/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002439 * Check the signature of a certificate by its parent
2440 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002441static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2442 mbedtls_x509_crt *parent,
2443 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002444{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002445 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002446 size_t hash_len;
2447#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2448 const mbedtls_md_info_t *md_info;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002449 md_info = mbedtls_md_info_from_type(child->sig_md);
2450 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002451
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002452 /* Note: hash errors can happen only after an internal error */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002453 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2454 return -1;
2455 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002456#else
Jaeden Amero34973232019-02-20 10:32:28 +00002457 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002458 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(child->sig_md);
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002460 if (psa_hash_setup(&hash_operation, hash_alg) != PSA_SUCCESS) {
2461 return -1;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002462 }
2463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464 if (psa_hash_update(&hash_operation, child->tbs.p, child->tbs.len)
2465 != PSA_SUCCESS) {
2466 return -1;
2467 }
2468
2469 if (psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_len)
2470 != PSA_SUCCESS) {
2471 return -1;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002472 }
2473#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002474 /* Skip expensive computation on obvious mismatch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002475 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2476 return -1;
2477 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002478
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002479#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002480 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2481 return mbedtls_pk_verify_restartable(&parent->pk,
2482 child->sig_md, hash, hash_len,
2483 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002484 }
2485#else
2486 (void) rs_ctx;
2487#endif
2488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002489 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2490 child->sig_md, hash, hash_len,
2491 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002492}
2493
2494/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002495 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2496 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002497 *
2498 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002499 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002500static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2501 const mbedtls_x509_crt *parent,
2502 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002503{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002504 int need_ca_bit;
2505
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002506 /* Parent must be the issuer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002507 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2508 return -1;
2509 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002510
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002511 /* Parent must have the basicConstraints CA bit set as a general rule */
2512 need_ca_bit = 1;
2513
2514 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002515 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002516 need_ca_bit = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002517 }
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002519 if (need_ca_bit && !parent->ca_istrue) {
2520 return -1;
2521 }
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002524 if (need_ca_bit &&
2525 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2526 return -1;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002527 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002528#endif
2529
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002530 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002531}
2532
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002533/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002534 * Find a suitable parent for child in candidates, or return NULL.
2535 *
2536 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002537 * 1. subject name matches child's issuer
2538 * 2. if necessary, the CA bit is set and key usage allows signing certs
2539 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002540 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002541 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002542 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002543 * If there's a suitable candidate which is also time-valid, return the first
2544 * such. Otherwise, return the first suitable candidate (or NULL if there is
2545 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002546 *
2547 * The rationale for this rule is that someone could have a list of trusted
2548 * roots with two versions on the same root with different validity periods.
2549 * (At least one user reported having such a list and wanted it to just work.)
2550 * The reason we don't just require time-validity is that generally there is
2551 * only one version, and if it's expired we want the flags to state that
2552 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002553 *
2554 * The rationale for rule 3 (signature for trusted roots) is that users might
2555 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002556 * way we select the correct one is by checking the signature (as we don't
2557 * rely on key identifier extensions). (This is one way users might choose to
2558 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002559 *
2560 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002561 * - [in] child: certificate for which we're looking for a parent
2562 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002563 * - [out] r_parent: parent found (or NULL)
2564 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002565 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2566 * of the chain, 0 otherwise
2567 * - [in] path_cnt: number of intermediates seen so far
2568 * - [in] self_cnt: number of self-signed intermediates seen so far
2569 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002570 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002571 *
2572 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002573 * - 0 on success
2574 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002575 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002576static int x509_crt_find_parent_in(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 mbedtls_x509_crt *child,
2578 mbedtls_x509_crt *candidates,
2579 mbedtls_x509_crt **r_parent,
2580 int *r_signature_is_good,
2581 int top,
2582 unsigned path_cnt,
2583 unsigned self_cnt,
2584 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002585{
Janos Follath865b3eb2019-12-16 11:46:15 +00002586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002587 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002588 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002589
2590#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002591 /* did we have something in progress? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002592 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002593 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002594 parent = rs_ctx->parent;
2595 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002596 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002597
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002598 /* clear saved state */
2599 rs_ctx->parent = NULL;
2600 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002601 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002602
2603 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002604 goto check_signature;
2605 }
2606#endif
2607
2608 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002609 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002611 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002612 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002614 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002615 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002616
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002617 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002618 if (parent->max_pathlen > 0 &&
2619 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002620 continue;
2621 }
2622
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002623 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002624#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2625check_signature:
2626#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002627 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002628
2629#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002630 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002631 /* save state */
2632 rs_ctx->parent = parent;
2633 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002634 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002636 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002637 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002638#else
2639 (void) ret;
2640#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002641
2642 signature_is_good = ret == 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002643 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002644 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002645 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002646
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002647 /* optional time check */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2649 mbedtls_x509_time_is_future(&parent->valid_from)) {
2650 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002651 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002652 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002653 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002654
2655 continue;
2656 }
2657
Andy Gross1f627142019-01-30 10:25:53 -06002658 *r_parent = parent;
2659 *r_signature_is_good = signature_is_good;
2660
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002661 break;
2662 }
2663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002664 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002665 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002666 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002667 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002668
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002669 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002670}
2671
2672/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002673 * Find a parent in trusted CAs or the provided chain, or return NULL.
2674 *
2675 * Searches in trusted CAs first, and return the first suitable parent found
2676 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002677 *
2678 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002679 * - [in] child: certificate for which we're looking for a parent, followed
2680 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002681 * - [in] trust_ca: list of locally trusted certificates
2682 * - [out] parent: parent found (or NULL)
2683 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2684 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2685 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2686 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002687 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002688 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002689 *
2690 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002691 * - 0 on success
2692 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002693 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002694static int x509_crt_find_parent(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002695 mbedtls_x509_crt *child,
2696 mbedtls_x509_crt *trust_ca,
2697 mbedtls_x509_crt **parent,
2698 int *parent_is_trusted,
2699 int *signature_is_good,
2700 unsigned path_cnt,
2701 unsigned self_cnt,
2702 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002703{
Janos Follath865b3eb2019-12-16 11:46:15 +00002704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002705 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002706
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002707 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002708
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002709#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002710 /* restore then clear saved state if we have some stored */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002711 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002712 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002713 rs_ctx->parent_is_trusted = -1;
2714 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002715#endif
2716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002717 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002718 search_list = *parent_is_trusted ? trust_ca : child->next;
2719
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002720 ret = x509_crt_find_parent_in(child, search_list,
2721 parent, signature_is_good,
2722 *parent_is_trusted,
2723 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002724
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002725#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002726 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002727 /* save state */
2728 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002729 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002730 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002731#else
2732 (void) ret;
2733#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002734
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002735 /* stop here if found or already in second iteration */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002736 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002737 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002738 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002739
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002740 /* prepare second iteration */
2741 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002742 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002743
2744 /* extra precaution against mistakes in the caller */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002745 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002746 *parent_is_trusted = 0;
2747 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002748 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002751}
2752
2753/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002754 * Check if an end-entity certificate is locally trusted
2755 *
2756 * Currently we require such certificates to be self-signed (actually only
2757 * check for self-issued as self-signatures are not checked)
2758 */
2759static int x509_crt_check_ee_locally_trusted(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002760 mbedtls_x509_crt *crt,
2761 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002762{
2763 mbedtls_x509_crt *cur;
2764
2765 /* must be self-issued */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2767 return -1;
2768 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002769
2770 /* look for an exact match with trusted cert */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002771 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2772 if (crt->raw.len == cur->raw.len &&
2773 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2774 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002775 }
2776 }
2777
2778 /* too bad */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002779 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002780}
2781
2782/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002783 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002784 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002785 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2786 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002787 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002788 * such that every cert in the chain is a child of the next one,
2789 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002790 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002791 * Verify that chain and return it with flags for all issues found.
2792 *
2793 * Special cases:
2794 * - EE == Rj -> return a one-element list containing it
2795 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2796 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002797 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002798 * Tests for (aspects of) this function should include at least:
2799 * - trusted EE
2800 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002801 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002802 * - if relevant: EE untrusted
2803 * - if relevant: EE -> intermediate, untrusted
2804 * with the aspect under test checked at each relevant level (EE, int, root).
2805 * For some aspects longer chains are required, but usually length 2 is
2806 * enough (but length 1 is not in general).
2807 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002808 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002809 * - [in] crt: the cert list EE, C1, ..., Cn
2810 * - [in] trust_ca: the trusted list R1, ..., Rp
2811 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002812 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002813 * Only valid when return value is 0, may contain garbage otherwise!
2814 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002815 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002816 *
2817 * Return value:
2818 * - non-zero if the chain could not be fully built and examined
2819 * - 0 is the chain was successfully built and examined,
2820 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002821 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002822static int x509_crt_verify_chain(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002823 mbedtls_x509_crt *crt,
2824 mbedtls_x509_crt *trust_ca,
2825 mbedtls_x509_crl *ca_crl,
2826 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2827 void *p_ca_cb,
2828 const mbedtls_x509_crt_profile *profile,
2829 mbedtls_x509_crt_verify_chain *ver_chain,
2830 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002831{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002832 /* Don't initialize any of those variables here, so that the compiler can
2833 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002835 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002836 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002837 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002838 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002839 int parent_is_trusted;
2840 int child_is_trusted;
2841 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002842 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002843 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002844
2845#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2846 /* resume if we had an operation in progress */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002847 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002848 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002849 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002850 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002851
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002852 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002853 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002854 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002855 flags = &cur->flags;
2856
2857 goto find_parent;
2858 }
2859#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002860
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002861 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002862 self_cnt = 0;
2863 parent_is_trusted = 0;
2864 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002865
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002866 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002867 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002868 cur = &ver_chain->items[ver_chain->len];
2869 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002870 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002871 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002872 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002873
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002874 /* Check time-validity (all certificates) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002875 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002876 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002877 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002878
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002880 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002881 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002882
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002883 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002884 if (child_is_trusted) {
2885 return 0;
2886 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002887
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002888 /* Check signature algorithm: MD & PK algs */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002890 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002891 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002893 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002894 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002895 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002896
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002897 /* Special case: EE certs that are locally trusted */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002898 if (ver_chain->len == 1 &&
2899 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2900 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002901 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002902
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002903#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2904find_parent:
2905#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002906
2907 /* Obtain list of potential trusted signers from CA callback,
2908 * or use statically provided list. */
2909#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002910 if (f_ca_cb != NULL) {
2911 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2912 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002913 ver_chain->trust_ca_cb_result = NULL;
2914
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002915 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2916 if (ret != 0) {
2917 return MBEDTLS_ERR_X509_FATAL_ERROR;
2918 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002919
2920 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002921 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002922#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2923 {
2924 ((void) f_ca_cb);
2925 ((void) p_ca_cb);
2926 cur_trust_ca = trust_ca;
2927 }
2928
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002929 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2931 &parent_is_trusted, &signature_is_good,
2932 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002933
2934#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002935 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002936 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002937 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002938 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002939 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002940
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002941 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002942 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002943#else
2944 (void) ret;
2945#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002946
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002947 /* No parent? We're done here */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002948 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002949 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002950 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002951 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002952
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002953 /* Count intermediate self-issued (not necessarily self-signed) certs.
2954 * These can occur with some strategies for key rollover, see [SIRO],
2955 * and should be excluded from max_pathlen checks. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002956 if (ver_chain->len != 1 &&
2957 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002958 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002959 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002960
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002961 /* path_cnt is 0 for the first intermediate CA,
2962 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002963 if (!parent_is_trusted &&
2964 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965 /* return immediately to avoid overflow the chain array */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002966 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002967 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002968
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002969 /* signature was checked while searching parent */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002970 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002971 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002972 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973
2974 /* check size of signing key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002976 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002977 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002980 /* Check trusted CA's CRL for the given crt */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002981 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982#else
2983 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002984#endif
2985
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002986 /* prepare for next iteration */
2987 child = parent;
2988 parent = NULL;
2989 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002990 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002991 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002992}
2993
2994/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002995 * Check for CN match
2996 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002997static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2998 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002999{
3000 /* try exact match */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003001 if (name->len == cn_len &&
3002 x509_memcasecmp(cn, name->p, cn_len) == 0) {
3003 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003004 }
3005
3006 /* try wildcard match */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003007 if (x509_check_wildcard(cn, name) == 0) {
3008 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003009 }
3010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003011 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003012}
3013
3014/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003015 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3016 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003017static int x509_crt_check_san(const mbedtls_x509_buf *name,
3018 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003019{
3020 const unsigned char san_type = (unsigned char) name->tag &
3021 MBEDTLS_ASN1_TAG_VALUE_MASK;
3022
3023 /* dNSName */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003024 if (san_type == MBEDTLS_X509_SAN_DNS_NAME) {
3025 return x509_crt_check_cn(name, cn, cn_len);
3026 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003027
3028 /* (We may handle other types here later.) */
3029
3030 /* Unrecognized type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003031 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003032}
3033
3034/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003035 * Verify the requested CN - only call this if cn is not NULL!
3036 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003037static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3038 const char *cn,
3039 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003040{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003041 const mbedtls_x509_name *name;
3042 const mbedtls_x509_sequence *cur;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003043 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003045 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
3046 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
3047 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003048 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003049 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050 }
3051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052 if (cur == NULL) {
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003053 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003054 }
3055 } else {
3056 for (name = &crt->subject; name != NULL; name = name->next) {
3057 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3058 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
3059 break;
3060 }
3061 }
3062
3063 if (name == NULL) {
3064 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3065 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003066 }
3067}
3068
3069/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003070 * Merge the flags for all certs in the chain, after calling callback
3071 */
3072static int x509_crt_merge_flags_with_cb(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003073 uint32_t *flags,
3074 const mbedtls_x509_crt_verify_chain *ver_chain,
3075 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3076 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003077{
Janos Follath865b3eb2019-12-16 11:46:15 +00003078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003079 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003080 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003081 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003083 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003084 cur = &ver_chain->items[i-1];
3085 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003087 if (NULL != f_vrfy) {
3088 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3089 return ret;
3090 }
3091 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003092
3093 *flags |= cur_flags;
3094 }
3095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003097}
3098
3099/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003100 * Verify the certificate validity, with profile, restartable version
3101 *
3102 * This function:
3103 * - checks the requested CN (if any)
3104 * - checks the type and size of the EE cert's key,
3105 * as that isn't done as part of chain building/verification currently
3106 * - builds and verifies the chain
3107 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003108 *
3109 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3110 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3111 * verification routine to search for trusted signers, and CRLs will
3112 * be disabled. Otherwise, `trust_ca` will be used as the static list
3113 * of trusted signers, and `ca_crl` will be use as the static list
3114 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003115 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003116static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3117 mbedtls_x509_crt *trust_ca,
3118 mbedtls_x509_crl *ca_crl,
3119 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3120 void *p_ca_cb,
3121 const mbedtls_x509_crt_profile *profile,
3122 const char *cn, uint32_t *flags,
3123 int (*f_vrfy)(void *,
3124 mbedtls_x509_crt *,
3125 int,
3126 uint32_t *),
3127 void *p_vrfy,
3128 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003129{
Janos Follath865b3eb2019-12-16 11:46:15 +00003130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003131 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003132 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003133 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003134
3135 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003136 ee_flags = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003137 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003139 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003140 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3141 goto exit;
3142 }
3143
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003144 /* check name if requested */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003145 if (cn != NULL) {
3146 x509_crt_verify_name(crt, cn, &ee_flags);
3147 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003148
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003149 /* Check the type and size of the key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003150 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003152 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003153 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003154 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003156 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003157 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003158 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003159
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003160 /* Check the chain */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003161 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3162 f_ca_cb, p_ca_cb, profile,
3163 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003165 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003166 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003167 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003168
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003169 /* Merge end-entity flags */
3170 ver_chain.items[0].flags |= ee_flags;
3171
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003172 /* Build final flags, calling callback on the way if any */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003173 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003174
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003175exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003176
3177#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003178 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3179 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003180 ver_chain.trust_ca_cb_result = NULL;
3181#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3182
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003183#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003184 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3185 mbedtls_x509_crt_restart_free(rs_ctx);
3186 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003187#endif
3188
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003189 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3190 * the SSL module for authmode optional, but non-zero return from the
3191 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003192 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003193 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003194 }
3195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003196 if (ret != 0) {
3197 *flags = (uint32_t) -1;
3198 return ret;
3199 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 if (*flags != 0) {
3202 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3203 }
3204
3205 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003206}
3207
Hanno Becker3116fb32019-03-28 13:34:42 +00003208
3209/*
3210 * Verify the certificate validity (default profile, not restartable)
3211 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3213 mbedtls_x509_crt *trust_ca,
3214 mbedtls_x509_crl *ca_crl,
3215 const char *cn, uint32_t *flags,
3216 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3217 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003218{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003219 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3220 NULL, NULL,
3221 &mbedtls_x509_crt_profile_default,
3222 cn, flags,
3223 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003224}
3225
3226/*
3227 * Verify the certificate validity (user-chosen profile, not restartable)
3228 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003229int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3230 mbedtls_x509_crt *trust_ca,
3231 mbedtls_x509_crl *ca_crl,
3232 const mbedtls_x509_crt_profile *profile,
3233 const char *cn, uint32_t *flags,
3234 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3235 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003236{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003237 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3238 NULL, NULL,
3239 profile, cn, flags,
3240 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003241}
3242
3243#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3244/*
3245 * Verify the certificate validity (user-chosen profile, CA callback,
3246 * not restartable).
3247 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003248int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3249 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3250 void *p_ca_cb,
3251 const mbedtls_x509_crt_profile *profile,
3252 const char *cn, uint32_t *flags,
3253 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3254 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003255{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003256 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3257 f_ca_cb, p_ca_cb,
3258 profile, cn, flags,
3259 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003260}
3261#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003263int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3264 mbedtls_x509_crt *trust_ca,
3265 mbedtls_x509_crl *ca_crl,
3266 const mbedtls_x509_crt_profile *profile,
3267 const char *cn, uint32_t *flags,
3268 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3269 void *p_vrfy,
3270 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003271{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003272 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3273 NULL, NULL,
3274 profile, cn, flags,
3275 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003276}
3277
3278
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003279/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003280 * Initialize a certificate chain
3281 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003282void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003283{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003284 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003285}
3286
3287/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003288 * Unallocate all certificate data
3289 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003290void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 mbedtls_x509_crt *cert_cur = crt;
3293 mbedtls_x509_crt *cert_prv;
3294 mbedtls_x509_name *name_cur;
3295 mbedtls_x509_name *name_prv;
3296 mbedtls_x509_sequence *seq_cur;
3297 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003299 if (crt == NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003300 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003301 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003303 do {
3304 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003307 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003308#endif
3309
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003310 name_cur = cert_cur->issuer.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003311 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003312 name_prv = name_cur;
3313 name_cur = name_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003314 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3315 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003316 }
3317
3318 name_cur = cert_cur->subject.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003319 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003320 name_prv = name_cur;
3321 name_cur = name_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003322 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3323 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003324 }
3325
3326 seq_cur = cert_cur->ext_key_usage.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003327 while (seq_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003328 seq_prv = seq_cur;
3329 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003330 mbedtls_platform_zeroize(seq_prv,
3331 sizeof(mbedtls_x509_sequence));
3332 mbedtls_free(seq_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003333 }
3334
3335 seq_cur = cert_cur->subject_alt_names.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 while (seq_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003337 seq_prv = seq_cur;
3338 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003339 mbedtls_platform_zeroize(seq_prv,
3340 sizeof(mbedtls_x509_sequence));
3341 mbedtls_free(seq_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003342 }
3343
Ron Eldor74d9acc2019-03-21 14:00:03 +02003344 seq_cur = cert_cur->certificate_policies.next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003345 while (seq_cur != NULL) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02003346 seq_prv = seq_cur;
3347 seq_cur = seq_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003348 mbedtls_platform_zeroize(seq_prv,
3349 sizeof(mbedtls_x509_sequence));
3350 mbedtls_free(seq_prv);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003351 }
3352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003353 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3354 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3355 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003356 }
3357
3358 cert_cur = cert_cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003359 } while (cert_cur != NULL);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003360
3361 cert_cur = crt;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003362 do {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003363 cert_prv = cert_cur;
3364 cert_cur = cert_cur->next;
3365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003366 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3367 if (cert_prv != crt) {
3368 mbedtls_free(cert_prv);
3369 }
3370 } while (cert_cur != NULL);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003371}
3372
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003373#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3374/*
3375 * Initialize a restart context
3376 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003377void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003378{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003379 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003380
3381 ctx->parent = NULL;
3382 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003383 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003384
3385 ctx->parent_is_trusted = -1;
3386
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003387 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003388 ctx->self_cnt = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003389 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003390}
3391
3392/*
3393 * Free the components of a restart context
3394 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003395void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003396{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003397 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003398 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003399 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003401 mbedtls_pk_restart_free(&ctx->pk);
3402 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003403}
3404#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406#endif /* MBEDTLS_X509_CRT_PARSE_C */