blob: 4e7672e374357f9aecf751d15052300ba36c9f03 [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 Rodgman16799db2023-11-02 19:47:20 +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
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033#endif
34
Andrzej Kurekd4a65532018-10-31 06:18:39 -040035#if defined(MBEDTLS_USE_PSA_CRYPTO)
36#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020037#include "psa_util_internal.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020038#include "md_psa.h"
pespacek7599a772022-02-07 14:40:55 +010039#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti3f00b842023-05-15 12:57:06 +020040#include "pk_internal.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010046#endif
47
Daniel Axtensf0710242020-05-28 11:43:41 +100048#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010049#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Glenn Straussc26bd762022-10-23 19:48:18 -040050#define WIN32_LEAN_AND_MEAN
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051#include <windows.h>
52#else
53#include <time.h>
54#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100055#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020059#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#include <sys/types.h>
61#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020062#if defined(__MBED__)
63#include <platform/mbed_retarget.h>
64#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020066#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060067#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000068#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#endif
70
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020071/*
72 * Item in a verification chain: cert and flags for it
73 */
74typedef struct {
75 mbedtls_x509_crt *crt;
76 uint32_t flags;
77} x509_crt_verify_chain_item;
78
79/*
80 * Max size of verification chain: end-entity + intermediates + trusted root
81 */
Gilles Peskine449bd832023-01-11 14:50:10 +010082#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020083
Gilles Peskineffb92da2021-06-02 00:03:26 +020084/* Default profile. Do not remove items unless there are serious security
85 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020086const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
87{
Gilles Peskineae270bf2021-06-02 00:05:29 +020088 /* Hashes from SHA-256 and above. Note that this selection
89 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +010090 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
91 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
92 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020093 0xFFFFFFF, /* Any PK alg */
Valerio Setti8c3404f2023-06-26 15:49:48 +020094#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskineae270bf2021-06-02 00:05:29 +020095 /* Curves at or above 128-bit security level. Note that this selection
96 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +010097 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
98 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
99 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
100 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
101 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
102 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200103 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200104#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200106#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200107 2048,
108};
109
Gilles Peskineffb92da2021-06-02 00:03:26 +0200110/* Next-generation profile. Currently identical to the default, but may
111 * be tightened at any time. */
112const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200113{
114 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
116 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
117 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200118 0xFFFFFFF, /* Any PK alg */
119#if defined(MBEDTLS_ECP_C)
120 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
122 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
123 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
124 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
125 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200128#else
129 0,
130#endif
131 2048,
132};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200133
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200134/*
135 * NSA Suite B Profile
136 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200137const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
138{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200139 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
141 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200142 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
144 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200145#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200146 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
148 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200149#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200150 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200151#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200152 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200153};
154
155/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200156 * Empty / all-forbidden profile
157 */
158const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
159{
160 0,
161 0,
162 0,
163 (uint32_t) -1,
164};
165
166/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200167 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200168 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200169 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100170static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
171 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200172{
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (md_alg == MBEDTLS_MD_NONE) {
174 return -1;
175 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
178 return 0;
179 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182}
183
184/*
185 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200186 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
189 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190{
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (pk_alg == MBEDTLS_PK_NONE) {
192 return -1;
193 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
196 return 0;
197 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200}
201
202/*
203 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200204 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200205 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100206static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
207 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200208{
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200210
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
213 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
214 return 0;
215 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200219#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220
Valerio Setti8c3404f2023-06-26 15:49:48 +0200221#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200223 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Settif9362b72023-11-29 08:42:27 +0100225 const mbedtls_ecp_group_id gid = mbedtls_pk_get_ec_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (gid == MBEDTLS_ECP_DP_NONE) {
228 return -1;
229 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
232 return 0;
233 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200236 }
Valerio Setti8c3404f2023-06-26 15:49:48 +0200237#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200240}
241
242/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000243 * Like memcmp, but case-insensitive and always returns -1 if different
244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000246{
247 size_t i;
248 unsigned char diff;
249 const unsigned char *n1 = s1, *n2 = s2;
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000252 diff = n1[i] ^ n2[i];
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000255 continue;
256 }
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (diff == 32 &&
259 ((n1[i] >= 'a' && n1[i] <= 'z') ||
260 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
261 continue;
262 }
263
264 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000265 }
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000268}
269
270/*
271 * Return 0 if name matches wildcard, -1 otherwise
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000274{
275 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000277
278 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
280 return -1;
281 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 for (i = 0; i < cn_len; ++i) {
284 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000285 cn_idx = i;
286 break;
287 }
288 }
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (cn_idx == 0) {
291 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000292 }
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (cn_len - cn_idx == name->len - 1 &&
295 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
296 return 0;
297 }
298
299 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000300}
301
302/*
303 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
304 * variations (but not all).
305 *
306 * Return 0 if equal, -1 otherwise.
307 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000309{
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000311 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 memcmp(a->p, b->p, b->len) == 0) {
313 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000314 }
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
317 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000318 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 x509_memcasecmp(a->p, b->p, b->len) == 0) {
320 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000321 }
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000324}
325
326/*
327 * Compare two X.509 Names (aka rdnSequence).
328 *
329 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
330 * we sometimes return unequal when the full algorithm would return equal,
331 * but never the other way. (In particular, we don't do Unicode normalisation
332 * or space folding.)
333 *
334 * Return 0 if equal, -1 otherwise.
335 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000337{
338 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 while (a != NULL || b != NULL) {
340 if (a == NULL || b == NULL) {
341 return -1;
342 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000343
344 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000346 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
348 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000349 }
350
351 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (x509_string_cmp(&a->val, &b->val) != 0) {
353 return -1;
354 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000355
356 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (a->next_merged != b->next_merged) {
358 return -1;
359 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000360
361 a = a->next;
362 b = b->next;
363 }
364
365 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000367}
368
369/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200370 * Reset (init or clear) a verify_chain
371 */
372static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200374{
375 size_t i;
376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200378 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000379 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200380 }
381
382 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000383
384#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
385 ver_chain->trust_ca_cb_result = NULL;
386#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200387}
388
389/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
391 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392static int x509_get_version(unsigned char **p,
393 const unsigned char *end,
394 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395{
Janos Follath865b3eb2019-12-16 11:46:15 +0000396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 size_t len;
398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
400 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
401 0)) != 0) {
402 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 }
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408 }
409
410 end = *p + len;
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
413 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
414 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (*p != end) {
417 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
418 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
419 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422}
423
424/*
425 * Validity ::= SEQUENCE {
426 * notBefore Time,
427 * notAfter Time }
428 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429static int x509_get_dates(unsigned char **p,
430 const unsigned char *end,
431 mbedtls_x509_time *from,
432 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433{
Janos Follath865b3eb2019-12-16 11:46:15 +0000434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435 size_t len;
436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
438 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
439 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
440 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
442 end = *p + len;
443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
445 return ret;
446 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
449 return ret;
450 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (*p != end) {
453 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
454 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
455 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458}
459
460/*
461 * X.509 v2/v3 unique identifier (not parsed)
462 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100463static int x509_get_uid(unsigned char **p,
464 const unsigned char *end,
465 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466{
Janos Follath865b3eb2019-12-16 11:46:15 +0000467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (*p == end) {
470 return 0;
471 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
473 uid->tag = **p;
474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
476 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
477 n)) != 0) {
478 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
479 return 0;
480 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483 }
484
485 uid->p = *p;
486 *p += uid->len;
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489}
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491static int x509_get_basic_constraints(unsigned char **p,
492 const unsigned char *end,
493 int *ca_istrue,
494 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495{
Janos Follath865b3eb2019-12-16 11:46:15 +0000496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 size_t len;
498
499 /*
500 * BasicConstraints ::= SEQUENCE {
501 * cA BOOLEAN DEFAULT FALSE,
502 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
503 */
504 *ca_istrue = 0; /* DEFAULT FALSE */
505 *max_pathlen = 0; /* endless */
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
508 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
509 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 }
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (*p == end) {
513 return 0;
514 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
517 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
518 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
519 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (ret != 0) {
522 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
523 }
524
525 if (*ca_istrue != 0) {
526 *ca_istrue = 1;
527 }
528 }
529
530 if (*p == end) {
531 return 0;
532 }
533
534 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
535 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
536 }
537
538 if (*p != end) {
539 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
540 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
541 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542
Andrzej Kurek16050742020-04-14 09:49:52 -0400543 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
544 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (*max_pathlen == INT_MAX) {
546 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
547 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
548 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400549
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550 (*max_pathlen)++;
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553}
554
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555/*
556 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
557 *
558 * KeyPurposeId ::= OBJECT IDENTIFIER
559 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560static int x509_get_ext_key_usage(unsigned char **p,
561 const unsigned char *end,
562 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563{
Janos Follath865b3eb2019-12-16 11:46:15 +0000564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
567 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
568 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569
570 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (ext_key_usage->buf.p == NULL) {
572 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
573 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
574 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577}
578
579/*
toth92ga41954d2021-02-12 16:11:17 +0100580 * SubjectKeyIdentifier ::= KeyIdentifier
581 *
582 * KeyIdentifier ::= OCTET STRING
583 */
584static int x509_get_subject_key_id(unsigned char **p,
585 const unsigned char *end,
586 mbedtls_x509_buf *subject_key_id)
587{
588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
589 size_t len = 0u;
590
591 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
592 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100593 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100594 }
595
Przemek Stekiel61aed062023-05-08 11:14:36 +0200596 subject_key_id->len = len;
597 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
598 subject_key_id->p = *p;
599 *p += len;
600
toth92gd96027a2021-04-27 15:41:25 +0200601 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100602 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
603 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200604 }
605
toth92ga41954d2021-02-12 16:11:17 +0100606 return 0;
607}
608
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100609/*
toth92g8d435a02021-05-10 15:16:33 +0200610 * AuthorityKeyIdentifier ::= SEQUENCE {
611 * keyIdentifier [0] KeyIdentifier OPTIONAL,
612 * authorityCertIssuer [1] GeneralNames OPTIONAL,
613 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
614 *
615 * KeyIdentifier ::= OCTET STRING
616 */
617static int x509_get_authority_key_id(unsigned char **p,
618 unsigned char *end,
619 mbedtls_x509_authority *authority_key_id)
620{
621 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
622 size_t len = 0u;
623
624 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100625 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100626 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200627 }
628
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100629 if (*p + len != end) {
630 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
631 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
632 }
633
Przemek Stekieled9fb782023-05-03 16:27:25 +0200634 ret = mbedtls_asn1_get_tag(p, end, &len,
635 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
636
637 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200638 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200639 authority_key_id->keyIdentifier.len = len;
640 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200641 /* Setting tag of the keyIdentfier intentionally to 0x04.
642 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200643 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200644 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
645
646 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200647 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
648 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200649 }
650
651 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200652 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200653 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
654 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100655 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200656 /* authorityCertIssuer and authorityCertSerialNumber MUST both
657 be present or both be absent. At this point we expect to have both. */
658 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200659 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200660 /* "end" also includes the CertSerialNumber field so "len" shall be used */
661 ret = mbedtls_x509_get_subject_alt_name_ext(p,
662 (*p+len),
663 &authority_key_id->authorityCertIssuer);
664 if (ret != 0) {
665 return ret;
666 }
667
668 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
669 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
670 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
671 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
672 }
673 authority_key_id->authorityCertSerialNumber.len = len;
674 authority_key_id->authorityCertSerialNumber.p = *p;
675 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
676 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200677 }
678
679 if (*p != end) {
680 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
681 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
682 }
683
684 return 0;
685}
686
687/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200688 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
689 *
690 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
691 *
692 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
693 *
694 * PolicyInformation ::= SEQUENCE {
695 * policyIdentifier CertPolicyId,
696 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
697 * PolicyQualifierInfo OPTIONAL }
698 *
699 * CertPolicyId ::= OBJECT IDENTIFIER
700 *
701 * PolicyQualifierInfo ::= SEQUENCE {
702 * policyQualifierId PolicyQualifierId,
703 * qualifier ANY DEFINED BY policyQualifierId }
704 *
705 * -- policyQualifierIds for Internet policy qualifiers
706 *
707 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
708 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
709 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
710 *
711 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
712 *
713 * Qualifier ::= CHOICE {
714 * cPSuri CPSuri,
715 * userNotice UserNotice }
716 *
717 * CPSuri ::= IA5String
718 *
719 * UserNotice ::= SEQUENCE {
720 * noticeRef NoticeReference OPTIONAL,
721 * explicitText DisplayText OPTIONAL }
722 *
723 * NoticeReference ::= SEQUENCE {
724 * organization DisplayText,
725 * noticeNumbers SEQUENCE OF INTEGER }
726 *
727 * DisplayText ::= CHOICE {
728 * ia5String IA5String (SIZE (1..200)),
729 * visibleString VisibleString (SIZE (1..200)),
730 * bmpString BMPString (SIZE (1..200)),
731 * utf8String UTF8String (SIZE (1..200)) }
732 *
733 * NOTE: we only parse and use anyPolicy without qualifiers at this point
734 * as defined in RFC 5280.
735 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736static int x509_get_certificate_policies(unsigned char **p,
737 const unsigned char *end,
738 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200739{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300740 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200741 size_t len;
742 mbedtls_asn1_buf *buf;
743 mbedtls_asn1_sequence *cur = certificate_policies;
744
745 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 ret = mbedtls_asn1_get_tag(p, end, &len,
747 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
748 if (ret != 0) {
749 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
750 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (*p + len != end) {
753 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
754 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
755 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200756
757 /*
758 * Cannot be an empty sequence.
759 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (len == 0) {
761 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
762 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
763 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200766 mbedtls_x509_buf policy_oid;
767 const unsigned char *policy_end;
768
769 /*
770 * Get the policy sequence
771 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
773 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
774 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
775 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200776
777 policy_end = *p + len;
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
780 MBEDTLS_ASN1_OID)) != 0) {
781 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
782 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200783
784 policy_oid.tag = MBEDTLS_ASN1_OID;
785 policy_oid.len = len;
786 policy_oid.p = *p;
787
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300788 /*
789 * Only AnyPolicy is currently supported when enforcing policy.
790 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300792 /*
793 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200794 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300795 */
796 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
797 }
798
Ron Eldor74d9acc2019-03-21 14:00:03 +0200799 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if (cur->buf.p != NULL) {
801 if (cur->next != NULL) {
802 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
803 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (cur->next == NULL) {
808 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
809 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
810 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200811
812 cur = cur->next;
813 }
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200816 buf->tag = policy_oid.tag;
817 buf->p = policy_oid.p;
818 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300819
820 *p += len;
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 /*
823 * If there is an optional qualifier, then *p < policy_end
824 * Check the Qualifier len to verify it doesn't exceed policy_end.
825 */
826 if (*p < policy_end) {
827 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
828 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
829 0) {
830 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
831 }
Ron Eldor08063792019-05-13 16:38:39 +0300832 /*
833 * Skip the optional policy qualifiers.
834 */
835 *p += len;
836 }
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if (*p != policy_end) {
839 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
840 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
841 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200842 }
843
844 /* Set final sequence entry's next pointer to NULL */
845 cur->next = NULL;
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (*p != end) {
848 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
849 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
850 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200853}
854
855/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 * X.509 v3 extensions
857 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200858 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859static int x509_get_crt_ext(unsigned char **p,
860 const unsigned char *end,
861 mbedtls_x509_crt *crt,
862 mbedtls_x509_crt_ext_cb_t cb,
863 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864{
Janos Follath865b3eb2019-12-16 11:46:15 +0000865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200867 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (*p == end) {
870 return 0;
871 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
874 return ret;
875 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876
Hanno Becker12f62fb2019-02-12 17:22:36 +0000877 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 /*
880 * Extension ::= SEQUENCE {
881 * extnID OBJECT IDENTIFIER,
882 * critical BOOLEAN DEFAULT FALSE,
883 * extnValue OCTET STRING }
884 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 int is_critical = 0; /* DEFAULT FALSE */
887 int ext_type = 0;
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
890 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
891 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
892 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893
894 end_ext_data = *p + len;
895
896 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
898 MBEDTLS_ASN1_OID)) != 0) {
899 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
900 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901
k-stachowiak463928a2018-07-24 12:50:59 +0200902 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 extn_oid.p = *p;
904 *p += extn_oid.len;
905
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
908 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
909 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
910 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911
912 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
914 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
915 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
916 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200918 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919 end_ext_octet = *p + len;
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (end_ext_octet != end_ext_data) {
922 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
923 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
924 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
926 /*
927 * Detect supported extensions
928 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200932 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 if (cb != NULL) {
934 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
935 if (ret != 0 && is_critical) {
936 return ret;
937 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200938 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200939 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200940 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200941
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942 /* No parser found, skip extension */
943 *p = end_ext_octet;
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
948 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 continue;
951 }
952
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100953 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if ((crt->ext_types & ext_type) != 0) {
955 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
956 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100957
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 crt->ext_types |= ext_type;
959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 switch (ext_type) {
961 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
962 /* Parse basic constraints */
963 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
964 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
965 return ret;
966 }
967 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 case MBEDTLS_X509_EXT_KEY_USAGE:
970 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100971 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
972 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return ret;
974 }
975 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
978 /* Parse extended key usage */
979 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
980 &crt->ext_key_usage)) != 0) {
981 return ret;
982 }
983 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
toth92ga41954d2021-02-12 16:11:17 +0100985 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
986 /* Parse subject key identifier */
987 if ((ret = x509_get_subject_key_id(p, end_ext_data,
988 &crt->subject_key_id)) != 0) {
989 return ret;
990 }
991 break;
toth92g8d435a02021-05-10 15:16:33 +0200992
toth92ga41954d2021-02-12 16:11:17 +0100993 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
994 /* Parse authority key identifier */
995 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
996 &crt->authority_key_id)) != 0) {
997 return ret;
998 }
999 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001001 /* Parse subject alt name
1002 * SubjectAltName ::= GeneralNames
1003 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001004 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1005 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return ret;
1007 }
1008 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1011 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001012 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1013 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 return ret;
1015 }
1016 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1019 /* Parse certificate policies type */
1020 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1021 &crt->certificate_policies)) != 0) {
1022 /* Give the callback (if any) a chance to handle the extension
1023 * if it contains unsupported policies */
1024 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1025 cb(p_ctx, crt, &extn_oid, is_critical,
1026 start_ext_octet, end_ext_octet) == 0) {
1027 break;
1028 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (is_critical) {
1031 return ret;
1032 } else
1033 /*
1034 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1035 * cannot interpret or enforce the policy. However, it is up to
1036 * the user to choose how to enforce the policies,
1037 * unless the extension is critical.
1038 */
1039 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1040 return ret;
1041 }
1042 }
1043 break;
1044
1045 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001046 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 * If this is a non-critical extension, which the oid layer
1048 * supports, but there isn't an x509 parser for it,
1049 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (is_critical) {
1052 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1053 } else {
1054 *p = end_ext_octet;
1055 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 }
1057 }
1058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (*p != end) {
1060 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1061 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1062 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065}
1066
1067/*
1068 * Parse and fill a single X.509 certificate in DER format
1069 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1071 const unsigned char *buf,
1072 size_t buflen,
1073 int make_copy,
1074 mbedtls_x509_crt_ext_cb_t cb,
1075 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076{
Janos Follath865b3eb2019-12-16 11:46:15 +00001077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078 size_t len;
1079 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1083 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1084 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085
1086 /*
1087 * Check for valid input
1088 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if (crt == NULL || buf == NULL) {
1090 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1091 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001093 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001095 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096 end = p + len;
1097
1098 /*
1099 * Certificate ::= SEQUENCE {
1100 * tbsCertificate TBSCertificate,
1101 * signatureAlgorithm AlgorithmIdentifier,
1102 * signatureValue BIT STRING }
1103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1105 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1106 mbedtls_x509_crt_free(crt);
1107 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108 }
1109
Janos Follathcc0e49d2016-02-17 14:34:12 +00001110 end = crt_end = p + len;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001111 crt->raw.len = (size_t) (crt_end - buf);
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001113 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1115 if (crt->raw.p == NULL) {
1116 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1117 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001120 crt->own_buffer = 1;
1121
1122 p += crt->raw.len - len;
1123 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 } else {
1125 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001126 crt->own_buffer = 0;
1127 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001128
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129 /*
1130 * TBSCertificate ::= SEQUENCE {
1131 */
1132 crt->tbs.p = p;
1133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1135 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1136 mbedtls_x509_crt_free(crt);
1137 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138 }
1139
1140 end = p + len;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001141 crt->tbs.len = (size_t) (end - crt->tbs.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142
1143 /*
1144 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1145 *
1146 * CertificateSerialNumber ::= INTEGER
1147 *
1148 * signature AlgorithmIdentifier
1149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1151 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1152 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1153 &sig_params1)) != 0) {
1154 mbedtls_x509_crt_free(crt);
1155 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 }
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (crt->version < 0 || crt->version > 2) {
1159 mbedtls_x509_crt_free(crt);
1160 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161 }
1162
Andres AG7ca4a032017-03-09 16:16:11 +00001163 crt->version++;
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1166 &crt->sig_md, &crt->sig_pk,
1167 &crt->sig_opts)) != 0) {
1168 mbedtls_x509_crt_free(crt);
1169 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 }
1171
1172 /*
1173 * issuer Name
1174 */
1175 crt->issuer_raw.p = p;
1176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1178 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1179 mbedtls_x509_crt_free(crt);
1180 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 }
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1184 mbedtls_x509_crt_free(crt);
1185 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 }
1187
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001188 crt->issuer_raw.len = (size_t) (p - crt->issuer_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189
1190 /*
1191 * Validity ::= SEQUENCE {
1192 * notBefore Time,
1193 * notAfter Time }
1194 *
1195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1197 &crt->valid_to)) != 0) {
1198 mbedtls_x509_crt_free(crt);
1199 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 }
1201
1202 /*
1203 * subject Name
1204 */
1205 crt->subject_raw.p = p;
1206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1209 mbedtls_x509_crt_free(crt);
1210 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211 }
1212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1214 mbedtls_x509_crt_free(crt);
1215 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001216 }
1217
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001218 crt->subject_raw.len = (size_t) (p - crt->subject_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219
1220 /*
1221 * SubjectPublicKeyInfo
1222 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001223 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1225 mbedtls_x509_crt_free(crt);
1226 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001228 crt->pk_raw.len = (size_t) (p - crt->pk_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229
1230 /*
1231 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1232 * -- If present, version shall be v2 or v3
1233 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1234 * -- If present, version shall be v2 or v3
1235 * extensions [3] EXPLICIT Extensions OPTIONAL
1236 * -- If present, version shall be v3
1237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 if (crt->version == 2 || crt->version == 3) {
1239 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1240 if (ret != 0) {
1241 mbedtls_x509_crt_free(crt);
1242 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 }
1244 }
1245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (crt->version == 2 || crt->version == 3) {
1247 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1248 if (ret != 0) {
1249 mbedtls_x509_crt_free(crt);
1250 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 }
1252 }
1253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 if (crt->version == 3) {
1255 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1256 if (ret != 0) {
1257 mbedtls_x509_crt_free(crt);
1258 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259 }
1260 }
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (p != end) {
1263 mbedtls_x509_crt_free(crt);
1264 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1265 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 }
1267
1268 end = crt_end;
1269
1270 /*
1271 * }
1272 * -- end of TBSCertificate
1273 *
1274 * signatureAlgorithm AlgorithmIdentifier,
1275 * signatureValue BIT STRING
1276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1278 mbedtls_x509_crt_free(crt);
1279 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001280 }
1281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (crt->sig_oid.len != sig_oid2.len ||
1283 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001284 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001285 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 (sig_params1.len != 0 &&
1287 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1288 mbedtls_x509_crt_free(crt);
1289 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 }
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1293 mbedtls_x509_crt_free(crt);
1294 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 }
1296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (p != end) {
1298 mbedtls_x509_crt_free(crt);
1299 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1300 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 }
1302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304}
1305
1306/*
1307 * Parse one X.509 certificate in DER format from a buffer and add them to a
1308 * chained list
1309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001310static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1311 const unsigned char *buf,
1312 size_t buflen,
1313 int make_copy,
1314 mbedtls_x509_crt_ext_cb_t cb,
1315 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316{
Janos Follath865b3eb2019-12-16 11:46:15 +00001317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319
1320 /*
1321 * Check for valid input
1322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (crt == NULL || buf == NULL) {
1324 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1325 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 prev = crt;
1329 crt = crt->next;
1330 }
1331
1332 /*
1333 * Add new certificate on the end of the chain if needed.
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (crt->version != 0 && crt->next == NULL) {
1336 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (crt->next == NULL) {
1339 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1340 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341
1342 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001345 }
1346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1348 if (ret != 0) {
1349 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (crt != chain) {
1354 mbedtls_free(crt);
1355 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001358 }
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361}
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1364 const unsigned char *buf,
1365 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001366{
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001368}
1369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1371 const unsigned char *buf,
1372 size_t buflen,
1373 int make_copy,
1374 mbedtls_x509_crt_ext_cb_t cb,
1375 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001376{
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001378}
1379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1381 const unsigned char *buf,
1382 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001383{
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001385}
1386
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001388 * Parse one or more PEM certificates from a buffer and add them to the chained
1389 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1392 const unsigned char *buf,
1393 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394{
Janos Follath98e28a72016-05-31 14:03:54 +01001395#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001396 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001398#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001399
1400 /*
1401 * Check for valid input
1402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (chain == NULL || buf == NULL) {
1404 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1405 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406
1407 /*
1408 * Determine buffer content. Buffer contains either one DER certificate or
1409 * one or more PEM certificates.
1410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1413 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001415 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1418 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1419 }
Janos Follath98e28a72016-05-31 14:03:54 +01001420#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001422#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001429 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001434 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 ret = mbedtls_pem_read_buffer(&pem,
1436 "-----BEGIN CERTIFICATE-----",
1437 "-----END CERTIFICATE-----",
1438 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441 /*
1442 * Was PEM encoded
1443 */
1444 buflen -= use_len;
1445 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1447 return ret;
1448 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1449 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001450
1451 /*
1452 * PEM header and footer were found
1453 */
1454 buflen -= use_len;
1455 buf += use_len;
1456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
Paul Bakker5a5fa922014-09-26 14:53:04 +02001461 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001472 /*
1473 * Quit parsing on a memory error
1474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1476 return ret;
1477 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
1483 total_failed++;
1484 continue;
1485 }
1486
1487 success = 1;
1488 }
1489 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (success) {
1492 return total_failed;
1493 } else if (first_error) {
1494 return first_error;
1495 } else {
1496 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1497 }
Janos Follath98e28a72016-05-31 14:03:54 +01001498#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499}
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502/*
1503 * Load one or more certificates and add them to the chained list
1504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506{
Janos Follath865b3eb2019-12-16 11:46:15 +00001507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 size_t n;
1509 unsigned char *buf;
1510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1512 return ret;
1513 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001517 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520}
1521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523{
1524 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001525#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 int w_ret;
1527 WCHAR szDir[MAX_PATH];
1528 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001529 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531
Paul Bakker9af723c2014-05-01 13:03:14 +02001532 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533 HANDLE hFind;
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (len > MAX_PATH - 3) {
1536 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1537 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 memset(szDir, 0, sizeof(szDir));
1540 memset(filename, 0, MAX_PATH);
1541 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001542 filename[len++] = '\\';
1543 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544 filename[len++] = '*';
1545
Simon Butcher35e5dad2018-03-15 15:00:03 +00001546 /*
Minos Galanakis08a67cc2023-09-22 16:00:06 +01001547 * Note this function uses the code page CP_ACP which is the system default
1548 * ANSI codepage. The input string is always described in BYTES and the
1549 * output length is described in WCHARs.
Simon Butcher35e5dad2018-03-15 15:00:03 +00001550 */
Minos Galanakisa9bb34c2023-09-25 14:41:12 +01001551 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 MAX_PATH - 3);
1553 if (w_ret == 0) {
1554 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1555 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 hFind = FindFirstFileW(szDir, &file_data);
1558 if (hFind == INVALID_HANDLE_VALUE) {
1559 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1560 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
1562 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 do {
1564 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 }
Simon Butcherde573f52018-07-05 09:11:30 +01001569 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Minos Galanakis59108d32023-09-25 14:11:22 +01001570 -1, p, (int) len, NULL, NULL);
Minos Galanakisa277b212023-08-09 16:32:22 +01001571 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001572 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1573 goto cleanup;
1574 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1577 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 }
1582 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Ron Eldor36d90422017-01-09 15:09:16 +02001588cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001590#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001591 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001592 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001594 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001595 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (dir == NULL) {
1599 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1600 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601
Ron Eldor63140682017-01-09 19:27:59 +02001602#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1604 closedir(dir);
1605 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001606 }
Ron Eldor63140682017-01-09 19:27:59 +02001607#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001610
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001612 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001615 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001616 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1617 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 } else if (stat(entry_name, &sb) == -1) {
1619 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001620 /* Broken symbolic link - ignore this entry.
1621 stat(2) will return this error for either (a) a dangling
1622 symlink or (b) a missing file.
1623 Given that we have just obtained the filename from readdir,
1624 assume that it does exist and therefore treat this as a
1625 dangling symlink. */
1626 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001628 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001629 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1630 goto cleanup;
1631 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001632 }
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637
1638 // Ignore parse errors
1639 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1641 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001647
1648cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001650
Ron Eldor63140682017-01-09 19:27:59 +02001651#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 }
Ron Eldor63140682017-01-09 19:27:59 +02001655#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001656
Paul Bakkerbe089b02013-10-14 15:51:50 +02001657#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662
Peter Kolbus9a969b62018-12-11 13:55:56 -06001663#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001664#define PRINT_ITEM(i) \
1665 do { \
1666 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1667 MBEDTLS_X509_SAFE_SNPRINTF; \
1668 sep = ", "; \
1669 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001670
Przemek Stekielf4194942023-04-24 09:52:17 +02001671#define CERT_TYPE(type, name) \
1672 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001673 if (ns_cert_type & (type)) { \
1674 PRINT_ITEM(name); \
1675 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001676 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001677
Przemek Stekielf4194942023-04-24 09:52:17 +02001678#define KEY_USAGE(code, name) \
1679 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001680 if (key_usage & (code)) { \
1681 PRINT_ITEM(name); \
1682 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001683 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685static int x509_info_ext_key_usage(char **buf, size_t *size,
1686 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001687{
Janos Follath865b3eb2019-12-16 11:46:15 +00001688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001689 const char *desc;
1690 size_t n = *size;
1691 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001693 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001694
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 while (cur != NULL) {
1696 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001697 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001701 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001702
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001703 sep = ", ";
1704
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001705 cur = cur->next;
1706 }
1707
1708 *size = n;
1709 *buf = p;
1710
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001712}
1713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714static int x509_info_cert_policies(char **buf, size_t *size,
1715 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001716{
Janos Follath865b3eb2019-12-16 11:46:15 +00001717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001718 const char *desc;
1719 size_t n = *size;
1720 char *p = *buf;
1721 const mbedtls_x509_sequence *cur = certificate_policies;
1722 const char *sep = "";
1723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 while (cur != NULL) {
1725 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001726 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001728
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001730 MBEDTLS_X509_SAFE_SNPRINTF;
1731
1732 sep = ", ";
1733
1734 cur = cur->next;
1735 }
1736
1737 *size = n;
1738 *buf = p;
1739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001741}
1742
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743/*
1744 * Return an informational string about the certificate.
1745 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001746#define BEFORE_COLON 18
1747#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001748int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1749 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750{
Janos Follath865b3eb2019-12-16 11:46:15 +00001751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752 size_t n;
1753 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754 char key_size_str[BEFORE_COLON];
1755
1756 p = buf;
1757 n = size;
1758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 if (NULL == crt) {
1760 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001761 MBEDTLS_X509_SAFE_SNPRINTF;
1762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001764 }
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1767 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001768 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1770 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001771 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001774 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001777 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001779 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001782 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001784 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1787 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1788 crt->valid_from.year, crt->valid_from.mon,
1789 crt->valid_from.day, crt->valid_from.hour,
1790 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001791 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1794 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1795 crt->valid_to.year, crt->valid_to.mon,
1796 crt->valid_to.day, crt->valid_to.hour,
1797 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001798 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001801 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1804 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001805 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001807 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1809 mbedtls_pk_get_name(&crt->pk))) != 0) {
1810 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001811 }
1812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1814 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001815 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001817 /*
1818 * Optional extensions
1819 */
1820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1822 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1823 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001824 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 if (crt->max_pathlen > 0) {
1827 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001828 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001829 }
1830 }
1831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1833 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001834 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001835
Przemek Stekiel21c37282023-01-16 08:47:49 +01001836 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1837 &crt->subject_alt_names,
1838 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 return ret;
1840 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001841 }
1842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1844 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001845 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001846
Przemek Stekiel21c37282023-01-16 08:47:49 +01001847 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return ret;
1849 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001850 }
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1853 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001854 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001855
Przemek Stekiel21c37282023-01-16 08:47:49 +01001856 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 return ret;
1858 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001859 }
1860
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1862 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001863 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if ((ret = x509_info_ext_key_usage(&p, &n,
1866 &crt->ext_key_usage)) != 0) {
1867 return ret;
1868 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001869 }
1870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1872 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001873 MBEDTLS_X509_SAFE_SNPRINTF;
1874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if ((ret = x509_info_cert_policies(&p, &n,
1876 &crt->certificate_policies)) != 0) {
1877 return ret;
1878 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001879 }
1880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001882 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001885}
1886
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001887struct x509_crt_verify_string {
1888 int code;
1889 const char *string;
1890};
1891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001893static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001894 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001895 { 0, NULL }
1896};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001897#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1900 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001901{
Janos Follath865b3eb2019-12-16 11:46:15 +00001902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001903 const struct x509_crt_verify_string *cur;
1904 char *p = buf;
1905 size_t n = size;
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1908 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001909 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001913 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001914 flags ^= cur->code;
1915 }
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if (flags != 0) {
1918 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1919 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001920 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001921 }
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001924}
Hanno Becker612a2f12020-10-09 09:19:39 +01001925#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1928 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001929{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001930 unsigned int usage_must, usage_may;
1931 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1935 return 0;
1936 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001937
1938 usage_must = usage & ~may_mask;
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1941 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1942 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001943
1944 usage_may = usage & may_mask;
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1947 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1948 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001951}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1954 const char *usage_oid,
1955 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001956{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001958
1959 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1961 return 0;
1962 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001963
1964 /*
1965 * Look for the requested usage (or wildcard ANY) in our list
1966 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001969
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 if (cur_oid->len == usage_len &&
1971 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1972 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001973 }
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1976 return 0;
1977 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001978 }
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001981}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001984/*
1985 * Return 1 if the certificate is revoked, or 0 otherwise.
1986 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001987int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001988{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 while (cur != NULL && cur->serial.len != 0) {
1992 if (crt->serial.len == cur->serial.len &&
1993 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1994 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001995 }
1996
1997 cur = cur->next;
1998 }
1999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002001}
2002
2003/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002004 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002005 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002006 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002007static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2008 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002009 const mbedtls_x509_crt_profile *profile,
2010 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002011{
2012 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002013 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002014#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002015 psa_algorithm_t psa_algorithm;
2016#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002018#endif /* MBEDTLS_USE_PSA_CRYPTO */
2019 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (ca == NULL) {
2022 return flags;
2023 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 while (crl_list != NULL) {
2026 if (crl_list->version == 0 ||
2027 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028 crl_list = crl_list->next;
2029 continue;
2030 }
2031
2032 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002033 * Check if the CA is configured to sign CRLs
2034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (mbedtls_x509_crt_check_key_usage(ca,
2036 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002038 break;
2039 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002040
2041 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002042 * Check if CRL is correctly signed by the trusted CA
2043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002045 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002049 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002051
pespacek7599a772022-02-07 14:40:55 +01002052#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002053 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if (psa_hash_compute(psa_algorithm,
2055 crl_list->tbs.p,
2056 crl_list->tbs.len,
2057 hash,
2058 sizeof(hash),
2059 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002060 /* Note: this can't happen except after an internal error */
2061 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2062 break;
2063 }
pespacek7599a772022-02-07 14:40:55 +01002064#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2066 hash_length = mbedtls_md_get_size(md_info);
2067 if (mbedtls_md(md_info,
2068 crl_list->tbs.p,
2069 crl_list->tbs.len,
2070 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002071 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002073 break;
2074 }
pespaceka7a64692022-02-14 15:18:43 +01002075#endif /* MBEDTLS_USE_PSA_CRYPTO */
2076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002078 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2082 crl_list->sig_md, hash, hash_length,
2083 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085 break;
2086 }
2087
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002088#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089 /*
2090 * Check for validity of CRL (Do not drop out)
2091 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002092 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002096 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002097 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002099#else
2100 ((void) now);
2101#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002102
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002103 /*
2104 * Check if certificate is revoked
2105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108 break;
2109 }
2110
2111 crl_list = crl_list->next;
2112 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002118/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002119 * Check the signature of a certificate by its parent
2120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2122 mbedtls_x509_crt *parent,
2123 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002124{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002125 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002126 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002127#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2128 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 md_info = mbedtls_md_info_from_type(child->sig_md);
2130 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002131
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002132 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2134 return -1;
2135 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002136#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002137 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002139
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 status = psa_hash_compute(hash_alg,
2141 child->tbs.p,
2142 child->tbs.len,
2143 hash,
2144 sizeof(hash),
2145 &hash_len);
2146 if (status != PSA_SUCCESS) {
2147 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002148 }
2149
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002150#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002151 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2153 return -1;
2154 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002155
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002156#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2158 return mbedtls_pk_verify_restartable(&parent->pk,
2159 child->sig_md, hash, hash_len,
2160 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002161 }
2162#else
2163 (void) rs_ctx;
2164#endif
2165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2167 child->sig_md, hash, hash_len,
2168 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002169}
2170
2171/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002172 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2173 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002174 *
2175 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002176 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002177static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2178 const mbedtls_x509_crt *parent,
2179 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002180{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002181 int need_ca_bit;
2182
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002183 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2185 return -1;
2186 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002187
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002188 /* Parent must have the basicConstraints CA bit set as a general rule */
2189 need_ca_bit = 1;
2190
2191 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002193 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002194 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if (need_ca_bit && !parent->ca_istrue) {
2197 return -1;
2198 }
2199
2200 if (need_ca_bit &&
2201 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2202 return -1;
2203 }
2204
2205 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002206}
2207
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002208/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002209 * Find a suitable parent for child in candidates, or return NULL.
2210 *
2211 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002212 * 1. subject name matches child's issuer
2213 * 2. if necessary, the CA bit is set and key usage allows signing certs
2214 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002215 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002216 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002217 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002218 * If there's a suitable candidate which is also time-valid, return the first
2219 * such. Otherwise, return the first suitable candidate (or NULL if there is
2220 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002221 *
2222 * The rationale for this rule is that someone could have a list of trusted
2223 * roots with two versions on the same root with different validity periods.
2224 * (At least one user reported having such a list and wanted it to just work.)
2225 * The reason we don't just require time-validity is that generally there is
2226 * only one version, and if it's expired we want the flags to state that
2227 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002228 *
2229 * The rationale for rule 3 (signature for trusted roots) is that users might
2230 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002231 * way we select the correct one is by checking the signature (as we don't
2232 * rely on key identifier extensions). (This is one way users might choose to
2233 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002234 *
2235 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002236 * - [in] child: certificate for which we're looking for a parent
2237 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002238 * - [out] r_parent: parent found (or NULL)
2239 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002240 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2241 * of the chain, 0 otherwise
2242 * - [in] path_cnt: number of intermediates seen so far
2243 * - [in] self_cnt: number of self-signed intermediates seen so far
2244 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002245 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002246 *
2247 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002248 * - 0 on success
2249 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002250 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002251static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 mbedtls_x509_crt *child,
2253 mbedtls_x509_crt *candidates,
2254 mbedtls_x509_crt **r_parent,
2255 int *r_signature_is_good,
2256 int top,
2257 unsigned path_cnt,
2258 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002259 mbedtls_x509_crt_restart_ctx *rs_ctx,
2260 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002261{
Janos Follath865b3eb2019-12-16 11:46:15 +00002262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002263 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002264 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002265
2266#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002267 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002269 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002270 parent = rs_ctx->parent;
2271 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002272 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002273
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002274 /* clear saved state */
2275 rs_ctx->parent = NULL;
2276 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002277 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002278
2279 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002280 goto check_signature;
2281 }
2282#endif
2283
2284 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002285 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002288 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002290 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002292
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002293 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (parent->max_pathlen > 0 &&
2295 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002296 continue;
2297 }
2298
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002299 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002300#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2301check_signature:
2302#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002304
2305#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002307 /* save state */
2308 rs_ctx->parent = parent;
2309 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002310 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002313 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002314#else
2315 (void) ret;
2316#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002317
2318 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002320 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002322
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002323#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002324 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002325 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2326 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002328 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002329 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002330 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002331
2332 continue;
2333 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002334#else
2335 ((void) now);
2336#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002337
Andy Gross1f627142019-01-30 10:25:53 -06002338 *r_parent = parent;
2339 *r_signature_is_good = signature_is_good;
2340
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002341 break;
2342 }
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002345 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002346 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002347 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002350}
2351
2352/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002353 * Find a parent in trusted CAs or the provided chain, or return NULL.
2354 *
2355 * Searches in trusted CAs first, and return the first suitable parent found
2356 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002357 *
2358 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002359 * - [in] child: certificate for which we're looking for a parent, followed
2360 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002361 * - [in] trust_ca: list of locally trusted certificates
2362 * - [out] parent: parent found (or NULL)
2363 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2364 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2365 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2366 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002367 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002368 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002369 *
2370 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002371 * - 0 on success
2372 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002373 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002374static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 mbedtls_x509_crt *child,
2376 mbedtls_x509_crt *trust_ca,
2377 mbedtls_x509_crt **parent,
2378 int *parent_is_trusted,
2379 int *signature_is_good,
2380 unsigned path_cnt,
2381 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002382 mbedtls_x509_crt_restart_ctx *rs_ctx,
2383 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002384{
Janos Follath865b3eb2019-12-16 11:46:15 +00002385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002386 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002387
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002388 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002389
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002390#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002391 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002393 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002394 rs_ctx->parent_is_trusted = -1;
2395 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002396#endif
2397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002399 search_list = *parent_is_trusted ? trust_ca : child->next;
2400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 ret = x509_crt_find_parent_in(child, search_list,
2402 parent, signature_is_good,
2403 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002404 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002405
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002406#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002408 /* save state */
2409 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002411 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002412#else
2413 (void) ret;
2414#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002415
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002416 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002418 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002420
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002421 /* prepare second iteration */
2422 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002423 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002424
2425 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002427 *parent_is_trusted = 0;
2428 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002429 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002432}
2433
2434/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002435 * Check if an end-entity certificate is locally trusted
2436 *
2437 * Currently we require such certificates to be self-signed (actually only
2438 * check for self-issued as self-signatures are not checked)
2439 */
2440static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 mbedtls_x509_crt *crt,
2442 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002443{
2444 mbedtls_x509_crt *cur;
2445
2446 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2448 return -1;
2449 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002450
2451 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2453 if (crt->raw.len == cur->raw.len &&
2454 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2455 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002456 }
2457 }
2458
2459 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002461}
2462
2463/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002464 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002465 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002466 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2467 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002468 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002469 * such that every cert in the chain is a child of the next one,
2470 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002471 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002472 * Verify that chain and return it with flags for all issues found.
2473 *
2474 * Special cases:
2475 * - EE == Rj -> return a one-element list containing it
2476 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2477 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002478 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002479 * Tests for (aspects of) this function should include at least:
2480 * - trusted EE
2481 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002482 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002483 * - if relevant: EE untrusted
2484 * - if relevant: EE -> intermediate, untrusted
2485 * with the aspect under test checked at each relevant level (EE, int, root).
2486 * For some aspects longer chains are required, but usually length 2 is
2487 * enough (but length 1 is not in general).
2488 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002489 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002490 * - [in] crt: the cert list EE, C1, ..., Cn
2491 * - [in] trust_ca: the trusted list R1, ..., Rp
2492 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002493 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002494 * Only valid when return value is 0, may contain garbage otherwise!
2495 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002496 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002497 *
2498 * Return value:
2499 * - non-zero if the chain could not be fully built and examined
2500 * - 0 is the chain was successfully built and examined,
2501 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002502 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002503static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 mbedtls_x509_crt *crt,
2505 mbedtls_x509_crt *trust_ca,
2506 mbedtls_x509_crl *ca_crl,
2507 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2508 void *p_ca_cb,
2509 const mbedtls_x509_crt_profile *profile,
2510 mbedtls_x509_crt_verify_chain *ver_chain,
2511 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002512{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002513 /* Don't initialize any of those variables here, so that the compiler can
2514 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002515 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002516 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002517 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002518 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002519 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002520 int parent_is_trusted;
2521 int child_is_trusted;
2522 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002523 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002524 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002525 mbedtls_x509_time now;
2526
2527#if defined(MBEDTLS_HAVE_TIME_DATE)
2528 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2529 return MBEDTLS_ERR_X509_FATAL_ERROR;
2530 }
2531#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002532
2533#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2534 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002536 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002537 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002538 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002539
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002540 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002541 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002542 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002543 flags = &cur->flags;
2544
2545 goto find_parent;
2546 }
2547#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002548
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002549 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002550 self_cnt = 0;
2551 parent_is_trusted = 0;
2552 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002555 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002556 cur = &ver_chain->items[ver_chain->len];
2557 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002558 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002559 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002560 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002561
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002562#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002563 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002564 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002565 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002567
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002568 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002569 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002571#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002572
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002573 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (child_is_trusted) {
2575 return 0;
2576 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002577
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002578 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002580 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002584 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002586
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002587 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (ver_chain->len == 1 &&
2589 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2590 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002591 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002592
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002593#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2594find_parent:
2595#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002596
2597 /* Obtain list of potential trusted signers from CA callback,
2598 * or use statically provided list. */
2599#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (f_ca_cb != NULL) {
2601 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2602 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002603 ver_chain->trust_ca_cb_result = NULL;
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2606 if (ret != 0) {
2607 return MBEDTLS_ERR_X509_FATAL_ERROR;
2608 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002609
2610 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002612#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2613 {
2614 ((void) f_ca_cb);
2615 ((void) p_ca_cb);
2616 cur_trust_ca = trust_ca;
2617 }
2618
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002619 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2621 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002622 ver_chain->len - 1, self_cnt, rs_ctx,
2623 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002624
2625#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002627 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002628 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002629 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002630 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002633 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002634#else
2635 (void) ret;
2636#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002637
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002638 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002640 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002642 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002643
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002644 /* Count intermediate self-issued (not necessarily self-signed) certs.
2645 * These can occur with some strategies for key rollover, see [SIRO],
2646 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (ver_chain->len != 1 &&
2648 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002649 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002650 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002651
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002652 /* path_cnt is 0 for the first intermediate CA,
2653 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 if (!parent_is_trusted &&
2655 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002656 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002658 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002659
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002660 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002662 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002664
2665 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002667 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002671 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002672 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002673#else
2674 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002675#endif
2676
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002677 /* prepare for next iteration */
2678 child = parent;
2679 parent = NULL;
2680 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002681 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002682 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002683}
2684
Glenn Straussc26bd762022-10-23 19:48:18 -04002685#ifdef _WIN32
2686#ifdef _MSC_VER
2687#pragma comment(lib, "ws2_32.lib")
2688#include <winsock2.h>
2689#include <ws2tcpip.h>
2690#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2691#include <winsock2.h>
2692#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002693#else
2694/* inet_pton() is not supported, fallback to software version */
2695#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002696#endif
2697#elif defined(__sun)
2698/* Solaris requires -lsocket -lnsl for inet_pton() */
2699#elif defined(__has_include)
2700#if __has_include(<sys/socket.h>)
2701#include <sys/socket.h>
2702#endif
2703#if __has_include(<arpa/inet.h>)
2704#include <arpa/inet.h>
2705#endif
2706#endif
2707
2708/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2709 * the platform inet_pton() or a local implementation (below). The local
2710 * implementation may be used even in cases where the platform provides
2711 * inet_pton(), e.g. when there are different includes required and/or the
2712 * platform implementation requires dependencies on additional libraries.
2713 * Specifically, Windows requires custom includes and additional link
2714 * dependencies, and Solaris requires additional link dependencies.
2715 * Also, as a coarse heuristic, use the local implementation if the compiler
2716 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002717 * provided by headers included (or not) via __has_include() above.
2718 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2719 * despite having a platform that has inet_pton. */
2720#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2721/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002722static int x509_inet_pton_ipv4(const char *src, void *dst);
2723
2724#define li_cton(c, n) \
2725 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2726
2727static int x509_inet_pton_ipv6(const char *src, void *dst)
2728{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002729 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002730 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002731 uint16_t addr[8];
2732 do {
2733 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002734 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002735 for (uint8_t digit; num_digits < 4; num_digits++) {
2736 if (li_cton(*p, digit) == 0) {
2737 break;
2738 }
2739 group = (group << 4) | digit;
2740 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002741 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002742 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002743 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2744 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002745 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002746 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002747 } else if (*p == '.') {
2748 /* Don't accept IPv4 too early or late */
2749 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2750 nonzero_groups >= 7) {
2751 break;
2752 }
2753
2754 /* Walk back to prior ':', then parse as IPv4-mapped */
2755 int steps = 4;
2756 do {
2757 p--;
2758 steps--;
2759 } while (*p != ':' && steps > 0);
2760
2761 if (*p != ':') {
2762 break;
2763 }
2764 p++;
2765 nonzero_groups--;
2766 if (x509_inet_pton_ipv4((const char *) p,
2767 addr + nonzero_groups) != 0) {
2768 break;
2769 }
2770
Andrzej Kurek0d578962023-04-13 09:09:56 -04002771 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002772 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002773 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002774 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002775 return -1;
2776 }
2777 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002778 /* Don't accept a second zero group or an invalid delimiter */
2779 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002780 return -1;
2781 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002782 zero_group_start = nonzero_groups;
2783
2784 /* Accept a zero group at start, but it has to be a double colon */
2785 if (zero_group_start == 0 && *++p != ':') {
2786 return -1;
2787 }
2788
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002789 if (p[1] == '\0') {
2790 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002791 break;
2792 }
2793 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002794 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002795 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002796
2797 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002798 return -1;
2799 }
2800
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002801 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002802 if (nonzero_groups > 6) {
2803 return -1;
2804 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002805 int zero_groups = 8 - nonzero_groups;
2806 int groups_after_zero = nonzero_groups - zero_group_start;
2807
2808 /* Move the non-zero part to after the zeroes */
2809 if (groups_after_zero) {
2810 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002811 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002812 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002813 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002814 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002815 } else {
2816 if (nonzero_groups != 8) {
2817 return -1;
2818 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002819 }
2820 memcpy(dst, addr, sizeof(addr));
2821 return 0;
2822}
2823
2824static int x509_inet_pton_ipv4(const char *src, void *dst)
2825{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002826 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002827 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002828 uint8_t digit, num_digits = 0;
2829 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002830 uint16_t octet;
2831
Glenn Strauss416c2952022-10-24 23:00:02 -04002832 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002833 octet = num_digits = 0;
2834 do {
2835 digit = *p - '0';
2836 if (digit > 9) {
2837 break;
2838 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002839
2840 /* Don't allow leading zeroes. These might mean octal format,
2841 * which this implementation does not support. */
2842 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002843 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002844 }
2845
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002846 octet = octet * 10 + digit;
2847 num_digits++;
2848 p++;
2849 } while (num_digits < 3);
2850
2851 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002852 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002853 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002854 *res++ = (uint8_t) octet;
2855 num_octets++;
2856 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002857 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002858}
Glenn Straussc26bd762022-10-23 19:48:18 -04002859
2860#else
2861
2862static int x509_inet_pton_ipv6(const char *src, void *dst)
2863{
2864 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2865}
2866
2867static int x509_inet_pton_ipv4(const char *src, void *dst)
2868{
2869 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2870}
2871
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002872#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002873
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002874size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002875{
2876 return strchr(cn, ':') == NULL
2877 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2878 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2879}
2880
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002881/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002882 * Check for CN match
2883 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002884static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2885 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002886{
2887 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 if (name->len == cn_len &&
2889 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2890 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002891 }
2892
2893 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 if (x509_check_wildcard(cn, name) == 0) {
2895 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002896 }
2897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002899}
2900
Glenn Straussc26bd762022-10-23 19:48:18 -04002901static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2902 const char *cn, size_t cn_len)
2903{
2904 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002905 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002906 if (cn_len == 0) {
2907 return -1;
2908 }
2909
2910 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2911 const unsigned char san_type = (unsigned char) cur->buf.tag &
2912 MBEDTLS_ASN1_TAG_VALUE_MASK;
2913 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2914 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2915 return 0;
2916 }
2917 }
2918
2919 return -1;
2920}
2921
Andrzej Kurek199eab92023-05-10 09:57:19 -04002922static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2923 const char *cn, size_t cn_len)
2924{
2925 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2926 const unsigned char san_type = (unsigned char) cur->buf.tag &
2927 MBEDTLS_ASN1_TAG_VALUE_MASK;
2928 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2929 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2930 return 0;
2931 }
2932 }
2933
2934 return -1;
2935}
2936
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002937/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002938 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2939 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002940static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002942{
Glenn Straussc26bd762022-10-23 19:48:18 -04002943 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002944 int san_uri = 0;
2945 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002946 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2947 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002948 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002949 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2950 return 0;
2951 }
2952 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002953 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002954 san_ip = 1;
2955 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002956 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2957 san_uri = 1;
2958 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002959 /* (We may handle other types here later.) */
2960 default: /* Unrecognized type */
2961 break;
2962 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002964 if (san_ip) {
2965 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2966 return 0;
2967 }
2968 }
2969 if (san_uri) {
2970 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2971 return 0;
2972 }
2973 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002974
Andrzej Kurek199eab92023-05-10 09:57:19 -04002975 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002976}
2977
2978/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002979 * Verify the requested CN - only call this if cn is not NULL!
2980 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002981static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2982 const char *cn,
2983 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002984{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002985 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002989 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2990 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 }
2992 } else {
2993 for (name = &crt->subject; name != NULL; name = name->next) {
2994 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2995 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002996 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 }
2998 }
2999
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003000 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003001
3002 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003003}
3004
3005/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003006 * Merge the flags for all certs in the chain, after calling callback
3007 */
3008static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 uint32_t *flags,
3010 const mbedtls_x509_crt_verify_chain *ver_chain,
3011 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3012 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003013{
Janos Follath865b3eb2019-12-16 11:46:15 +00003014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003015 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003016 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003017 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003020 cur = &ver_chain->items[i-1];
3021 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (NULL != f_vrfy) {
3024 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3025 return ret;
3026 }
3027 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003028
3029 *flags |= cur_flags;
3030 }
3031
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003033}
3034
3035/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003036 * Verify the certificate validity, with profile, restartable version
3037 *
3038 * This function:
3039 * - checks the requested CN (if any)
3040 * - checks the type and size of the EE cert's key,
3041 * as that isn't done as part of chain building/verification currently
3042 * - builds and verifies the chain
3043 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003044 *
3045 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3046 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3047 * verification routine to search for trusted signers, and CRLs will
3048 * be disabled. Otherwise, `trust_ca` will be used as the static list
3049 * of trusted signers, and `ca_crl` will be use as the static list
3050 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003052static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3053 mbedtls_x509_crt *trust_ca,
3054 mbedtls_x509_crl *ca_crl,
3055 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3056 void *p_ca_cb,
3057 const mbedtls_x509_crt_profile *profile,
3058 const char *cn, uint32_t *flags,
3059 int (*f_vrfy)(void *,
3060 mbedtls_x509_crt *,
3061 int,
3062 uint32_t *),
3063 void *p_vrfy,
3064 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003065{
Janos Follath865b3eb2019-12-16 11:46:15 +00003066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003067 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003068 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003069 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003070
3071 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003072 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003076 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3077 goto exit;
3078 }
3079
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003080 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 if (cn != NULL) {
3082 x509_crt_verify_name(crt, cn, &ee_flags);
3083 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003084
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003085 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003089 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003093 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003095
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003096 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3098 f_ca_cb, p_ca_cb, profile,
3099 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003104
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003105 /* Merge end-entity flags */
3106 ver_chain.items[0].flags |= ee_flags;
3107
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003108 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003110
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003111exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003112
3113#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3115 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003116 ver_chain.trust_ca_cb_result = NULL;
3117#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3118
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003119#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3121 mbedtls_x509_crt_restart_free(rs_ctx);
3122 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003123#endif
3124
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003125 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3126 * the SSL module for authmode optional, but non-zero return from the
3127 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003129 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003130 }
3131
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 if (ret != 0) {
3133 *flags = (uint32_t) -1;
3134 return ret;
3135 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 if (*flags != 0) {
3138 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3139 }
3140
3141 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003142}
3143
Hanno Becker3116fb32019-03-28 13:34:42 +00003144
3145/*
3146 * Verify the certificate validity (default profile, not restartable)
3147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003148int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3149 mbedtls_x509_crt *trust_ca,
3150 mbedtls_x509_crl *ca_crl,
3151 const char *cn, uint32_t *flags,
3152 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3153 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003154{
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3156 NULL, NULL,
3157 &mbedtls_x509_crt_profile_default,
3158 cn, flags,
3159 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003160}
3161
3162/*
3163 * Verify the certificate validity (user-chosen profile, not restartable)
3164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003165int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3166 mbedtls_x509_crt *trust_ca,
3167 mbedtls_x509_crl *ca_crl,
3168 const mbedtls_x509_crt_profile *profile,
3169 const char *cn, uint32_t *flags,
3170 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3171 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003172{
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3174 NULL, NULL,
3175 profile, cn, flags,
3176 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003177}
3178
3179#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3180/*
3181 * Verify the certificate validity (user-chosen profile, CA callback,
3182 * not restartable).
3183 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003184int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3185 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3186 void *p_ca_cb,
3187 const mbedtls_x509_crt_profile *profile,
3188 const char *cn, uint32_t *flags,
3189 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3190 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003191{
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3193 f_ca_cb, p_ca_cb,
3194 profile, cn, flags,
3195 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003196}
3197#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3198
Gilles Peskine449bd832023-01-11 14:50:10 +01003199int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3200 mbedtls_x509_crt *trust_ca,
3201 mbedtls_x509_crl *ca_crl,
3202 const mbedtls_x509_crt_profile *profile,
3203 const char *cn, uint32_t *flags,
3204 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3205 void *p_vrfy,
3206 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003207{
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3209 NULL, NULL,
3210 profile, cn, flags,
3211 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003212}
3213
3214
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003215/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003216 * Initialize a certificate chain
3217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003218void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003219{
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003221}
3222
3223/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003224 * Unallocate all certificate data
3225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 mbedtls_x509_crt *cert_cur = crt;
3229 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003230
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 while (cert_cur != NULL) {
3232 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003236#endif
3237
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3239 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3240 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3241 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3242 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003243 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003244
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003246 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003247 }
3248
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003249 cert_prv = cert_cur;
3250 cert_cur = cert_cur->next;
3251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3253 if (cert_prv != crt) {
3254 mbedtls_free(cert_prv);
3255 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003256 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003257}
3258
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003259#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3260/*
3261 * Initialize a restart context
3262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003263void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003264{
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003266
3267 ctx->parent = NULL;
3268 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003269 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003270
3271 ctx->parent_is_trusted = -1;
3272
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003273 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003274 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003276}
3277
3278/*
3279 * Free the components of a restart context
3280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003282{
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003284 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003286
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 mbedtls_pk_restart_free(&ctx->pk);
3288 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003289}
3290#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292#endif /* MBEDTLS_X509_CRT_PARSE_C */