blob: faea404dbaccf822f6729dd7b31a168a4b3f199e [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
Harry Ramsey0f6bc412024-10-04 10:36:54 +010020#include "x509_internal.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#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020036#include "psa_util_internal.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010037#include "mbedtls/psa_util.h"
Valerio Setti3f00b842023-05-15 12:57:06 +020038#include "pk_internal.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040039
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010044#endif
45
Daniel Axtensf0710242020-05-28 11:43:41 +100046#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Sergey Markelov4ed0fde2024-08-14 15:06:03 -070048#ifndef WIN32_LEAN_AND_MEAN
Glenn Straussc26bd762022-10-23 19:48:18 -040049#define WIN32_LEAN_AND_MEAN
Sergey Markelov4ed0fde2024-08-14 15:06:03 -070050#endif
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 */
Elena Uziunaite8dde3b32024-07-05 12:10:21 +010094#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
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,
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100104#else /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200105 0,
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100106#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
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),
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100145#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
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),
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100149#else /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200150 0,
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100151#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
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
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100221#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
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 }
Elena Uziunaite8dde3b32024-07-05 12:10:21 +0100237#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
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) {
Gilles Peskine8085f512024-09-15 12:45:44 +0200680 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
681 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92g8d435a02021-05-10 15:16:33 +0200682 }
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,
Valerio Setti68878cc2025-04-10 23:30:26 +02001166 &crt->sig_md, &crt->sig_pk)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 mbedtls_x509_crt_free(crt);
1168 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 }
1170
1171 /*
1172 * issuer Name
1173 */
1174 crt->issuer_raw.p = p;
1175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1177 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1178 mbedtls_x509_crt_free(crt);
1179 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 }
1181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1183 mbedtls_x509_crt_free(crt);
1184 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 }
1186
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001187 crt->issuer_raw.len = (size_t) (p - crt->issuer_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188
1189 /*
1190 * Validity ::= SEQUENCE {
1191 * notBefore Time,
1192 * notAfter Time }
1193 *
1194 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1196 &crt->valid_to)) != 0) {
1197 mbedtls_x509_crt_free(crt);
1198 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199 }
1200
1201 /*
1202 * subject Name
1203 */
1204 crt->subject_raw.p = p;
1205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1207 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1208 mbedtls_x509_crt_free(crt);
1209 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 }
1211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1213 mbedtls_x509_crt_free(crt);
1214 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 }
1216
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001217 crt->subject_raw.len = (size_t) (p - crt->subject_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001218
1219 /*
1220 * SubjectPublicKeyInfo
1221 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001222 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1224 mbedtls_x509_crt_free(crt);
1225 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001227 crt->pk_raw.len = (size_t) (p - crt->pk_raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228
1229 /*
1230 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1231 * -- If present, version shall be v2 or v3
1232 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1233 * -- If present, version shall be v2 or v3
1234 * extensions [3] EXPLICIT Extensions OPTIONAL
1235 * -- If present, version shall be v3
1236 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (crt->version == 2 || crt->version == 3) {
1238 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1239 if (ret != 0) {
1240 mbedtls_x509_crt_free(crt);
1241 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242 }
1243 }
1244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (crt->version == 2 || crt->version == 3) {
1246 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1247 if (ret != 0) {
1248 mbedtls_x509_crt_free(crt);
1249 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001250 }
1251 }
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (crt->version == 3) {
1254 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1255 if (ret != 0) {
1256 mbedtls_x509_crt_free(crt);
1257 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258 }
1259 }
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if (p != end) {
1262 mbedtls_x509_crt_free(crt);
1263 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1264 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265 }
1266
1267 end = crt_end;
1268
1269 /*
1270 * }
1271 * -- end of TBSCertificate
1272 *
1273 * signatureAlgorithm AlgorithmIdentifier,
1274 * signatureValue BIT STRING
1275 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1277 mbedtls_x509_crt_free(crt);
1278 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 }
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (crt->sig_oid.len != sig_oid2.len ||
1282 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001283 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001284 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 (sig_params1.len != 0 &&
1286 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1287 mbedtls_x509_crt_free(crt);
1288 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001289 }
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1292 mbedtls_x509_crt_free(crt);
1293 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 }
1295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (p != end) {
1297 mbedtls_x509_crt_free(crt);
1298 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1299 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 }
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303}
1304
1305/*
1306 * Parse one X.509 certificate in DER format from a buffer and add them to a
1307 * chained list
1308 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001309static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1310 const unsigned char *buf,
1311 size_t buflen,
1312 int make_copy,
1313 mbedtls_x509_crt_ext_cb_t cb,
1314 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315{
Janos Follath865b3eb2019-12-16 11:46:15 +00001316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318
1319 /*
1320 * Check for valid input
1321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (crt == NULL || buf == NULL) {
1323 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1324 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327 prev = crt;
1328 crt = crt->next;
1329 }
1330
1331 /*
1332 * Add new certificate on the end of the chain if needed.
1333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (crt->version != 0 && crt->next == NULL) {
1335 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (crt->next == NULL) {
1338 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1339 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340
1341 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344 }
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1347 if (ret != 0) {
1348 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 if (crt != chain) {
1353 mbedtls_free(crt);
1354 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357 }
1358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360}
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1363 const unsigned char *buf,
1364 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001365{
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001367}
1368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1370 const unsigned char *buf,
1371 size_t buflen,
1372 int make_copy,
1373 mbedtls_x509_crt_ext_cb_t cb,
1374 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001375{
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001377}
1378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1380 const unsigned char *buf,
1381 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001382{
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001384}
1385
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001387 * Parse one or more PEM certificates from a buffer and add them to the chained
1388 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001389 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001390int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1391 const unsigned char *buf,
1392 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001393{
Janos Follath98e28a72016-05-31 14:03:54 +01001394#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001395 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001397#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001398
1399 /*
1400 * Check for valid input
1401 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 if (chain == NULL || buf == NULL) {
1403 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1404 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405
1406 /*
1407 * Determine buffer content. Buffer contains either one DER certificate or
1408 * one or more PEM certificates.
1409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1412 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001414 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1417 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1418 }
Janos Follath98e28a72016-05-31 14:03:54 +01001419#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001421#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001428 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001433 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 ret = mbedtls_pem_read_buffer(&pem,
1435 "-----BEGIN CERTIFICATE-----",
1436 "-----END CERTIFICATE-----",
1437 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 /*
1441 * Was PEM encoded
1442 */
1443 buflen -= use_len;
1444 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1446 return ret;
1447 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1448 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449
1450 /*
1451 * PEM header and footer were found
1452 */
1453 buflen -= use_len;
1454 buf += use_len;
1455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459
Paul Bakker5a5fa922014-09-26 14:53:04 +02001460 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471 /*
1472 * Quit parsing on a memory error
1473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1475 return ret;
1476 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481
1482 total_failed++;
1483 continue;
1484 }
1485
1486 success = 1;
1487 }
1488 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (success) {
1491 return total_failed;
1492 } else if (first_error) {
1493 return first_error;
1494 } else {
1495 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1496 }
Janos Follath98e28a72016-05-31 14:03:54 +01001497#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001498}
1499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501/*
1502 * Load one or more certificates and add them to the chained list
1503 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505{
Janos Follath865b3eb2019-12-16 11:46:15 +00001506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507 size_t n;
1508 unsigned char *buf;
1509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1511 return ret;
1512 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001516 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519}
1520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522{
1523 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001524#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525 int w_ret;
1526 WCHAR szDir[MAX_PATH];
1527 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001528 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530
Paul Bakker9af723c2014-05-01 13:03:14 +02001531 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532 HANDLE hFind;
1533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (len > MAX_PATH - 3) {
1535 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1536 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 memset(szDir, 0, sizeof(szDir));
1539 memset(filename, 0, MAX_PATH);
1540 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001541 filename[len++] = '\\';
1542 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543 filename[len++] = '*';
1544
Simon Butcher35e5dad2018-03-15 15:00:03 +00001545 /*
Minos Galanakis08a67cc2023-09-22 16:00:06 +01001546 * Note this function uses the code page CP_ACP which is the system default
1547 * ANSI codepage. The input string is always described in BYTES and the
1548 * output length is described in WCHARs.
Simon Butcher35e5dad2018-03-15 15:00:03 +00001549 */
Minos Galanakisa9bb34c2023-09-25 14:41:12 +01001550 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 MAX_PATH - 3);
1552 if (w_ret == 0) {
1553 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1554 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 hFind = FindFirstFileW(szDir, &file_data);
1557 if (hFind == INVALID_HANDLE_VALUE) {
1558 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1559 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
1561 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 do {
1563 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 }
Simon Butcherde573f52018-07-05 09:11:30 +01001568 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Minos Galanakis59108d32023-09-25 14:11:22 +01001569 -1, p, (int) len, NULL, NULL);
Minos Galanakisa277b212023-08-09 16:32:22 +01001570 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001571 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1572 goto cleanup;
1573 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1576 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 }
1581 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
Ron Eldor36d90422017-01-09 15:09:16 +02001587cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001589#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001590 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001591 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001593 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001594 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if (dir == NULL) {
1598 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1599 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Ron Eldor63140682017-01-09 19:27:59 +02001601#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1603 closedir(dir);
1604 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001605 }
Ron Eldor63140682017-01-09 19:27:59 +02001606#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001611 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001614 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001615 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1616 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 } else if (stat(entry_name, &sb) == -1) {
1618 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001619 /* Broken symbolic link - ignore this entry.
1620 stat(2) will return this error for either (a) a dangling
1621 symlink or (b) a missing file.
1622 Given that we have just obtained the filename from readdir,
1623 assume that it does exist and therefore treat this as a
1624 dangling symlink. */
1625 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001627 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001628 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1629 goto cleanup;
1630 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 }
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636
1637 // Ignore parse errors
1638 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1640 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001646
1647cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001649
Ron Eldor63140682017-01-09 19:27:59 +02001650#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 }
Ron Eldor63140682017-01-09 19:27:59 +02001654#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001655
Paul Bakkerbe089b02013-10-14 15:51:50 +02001656#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661
Peter Kolbus9a969b62018-12-11 13:55:56 -06001662#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001663#define PRINT_ITEM(i) \
1664 do { \
1665 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1666 MBEDTLS_X509_SAFE_SNPRINTF; \
1667 sep = ", "; \
1668 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001669
Przemek Stekielf4194942023-04-24 09:52:17 +02001670#define CERT_TYPE(type, name) \
1671 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001672 if (ns_cert_type & (type)) { \
1673 PRINT_ITEM(name); \
1674 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001675 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001676
Przemek Stekielf4194942023-04-24 09:52:17 +02001677#define KEY_USAGE(code, name) \
1678 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001679 if (key_usage & (code)) { \
1680 PRINT_ITEM(name); \
1681 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001682 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684static int x509_info_ext_key_usage(char **buf, size_t *size,
1685 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001686{
Janos Follath865b3eb2019-12-16 11:46:15 +00001687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001688 const char *desc;
1689 size_t n = *size;
1690 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001692 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 while (cur != NULL) {
1695 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001696 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001700 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001701
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001702 sep = ", ";
1703
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001704 cur = cur->next;
1705 }
1706
1707 *size = n;
1708 *buf = p;
1709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001711}
1712
Gilles Peskine449bd832023-01-11 14:50:10 +01001713static int x509_info_cert_policies(char **buf, size_t *size,
1714 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001715{
Janos Follath865b3eb2019-12-16 11:46:15 +00001716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001717 const char *desc;
1718 size_t n = *size;
1719 char *p = *buf;
1720 const mbedtls_x509_sequence *cur = certificate_policies;
1721 const char *sep = "";
1722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 while (cur != NULL) {
1724 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001725 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001729 MBEDTLS_X509_SAFE_SNPRINTF;
1730
1731 sep = ", ";
1732
1733 cur = cur->next;
1734 }
1735
1736 *size = n;
1737 *buf = p;
1738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001740}
1741
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001742/*
1743 * Return an informational string about the certificate.
1744 */
Stefan Gloorb5c079b2025-02-21 10:33:51 +01001745#define MBEDTLS_BEFORE_COLON 18
1746#define MBEDTLS_BEFORE_COLON_STR "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001747int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1748 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749{
Janos Follath865b3eb2019-12-16 11:46:15 +00001750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751 size_t n;
1752 char *p;
Stefan Gloorb5c079b2025-02-21 10:33:51 +01001753 char key_size_str[MBEDTLS_BEFORE_COLON];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754
1755 p = buf;
1756 n = size;
1757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (NULL == crt) {
1759 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001760 MBEDTLS_X509_SAFE_SNPRINTF;
1761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001763 }
1764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1766 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001767 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1769 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001770 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001773 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001776 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001778 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001781 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001783 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1786 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1787 crt->valid_from.year, crt->valid_from.mon,
1788 crt->valid_from.day, crt->valid_from.hour,
1789 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001790 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1793 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1794 crt->valid_to.year, crt->valid_to.mon,
1795 crt->valid_to.day, crt->valid_to.hour,
1796 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001797 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001800 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801
Valerio Settid24dfad2025-04-23 11:13:02 +02001802 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk, crt->sig_md);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001803 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001804
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001805 /* Key size */
Stefan Gloorb5c079b2025-02-21 10:33:51 +01001806 if ((ret = mbedtls_x509_key_size_helper(key_size_str, MBEDTLS_BEFORE_COLON,
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 mbedtls_pk_get_name(&crt->pk))) != 0) {
1808 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001809 }
1810
Stefan Gloorb5c079b2025-02-21 10:33:51 +01001811 ret = mbedtls_snprintf(p, n, "\n%s%-" MBEDTLS_BEFORE_COLON_STR "s: %d bits",
1812 prefix, key_size_str, (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001813 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001814
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001815 /*
1816 * Optional extensions
1817 */
1818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1820 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1821 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001822 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 if (crt->max_pathlen > 0) {
1825 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001826 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001827 }
1828 }
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1831 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001832 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001833
Przemek Stekiel21c37282023-01-16 08:47:49 +01001834 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1835 &crt->subject_alt_names,
1836 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 return ret;
1838 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001839 }
1840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1842 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001843 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001844
Przemek Stekiel21c37282023-01-16 08:47:49 +01001845 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 return ret;
1847 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001848 }
1849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1851 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001852 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001853
Przemek Stekiel21c37282023-01-16 08:47:49 +01001854 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 return ret;
1856 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001857 }
1858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1860 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001861 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if ((ret = x509_info_ext_key_usage(&p, &n,
1864 &crt->ext_key_usage)) != 0) {
1865 return ret;
1866 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001867 }
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1870 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001871 MBEDTLS_X509_SAFE_SNPRINTF;
1872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 if ((ret = x509_info_cert_policies(&p, &n,
1874 &crt->certificate_policies)) != 0) {
1875 return ret;
1876 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001877 }
1878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001880 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001883}
1884
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001885struct x509_crt_verify_string {
1886 int code;
1887 const char *string;
1888};
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001891static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001892 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001893 { 0, NULL }
1894};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001895#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1898 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001899{
Janos Follath865b3eb2019-12-16 11:46:15 +00001900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001901 const struct x509_crt_verify_string *cur;
1902 char *p = buf;
1903 size_t n = size;
1904
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1906 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001907 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001911 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001912 flags ^= cur->code;
1913 }
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 if (flags != 0) {
1916 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1917 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001918 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001919 }
1920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001922}
Hanno Becker612a2f12020-10-09 09:19:39 +01001923#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1926 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001927{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001928 unsigned int usage_must, usage_may;
1929 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1933 return 0;
1934 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001935
1936 usage_must = usage & ~may_mask;
1937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1939 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1940 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001941
1942 usage_may = usage & may_mask;
1943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1945 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1946 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001949}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1952 const char *usage_oid,
1953 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001954{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001956
1957 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1959 return 0;
1960 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001961
1962 /*
1963 * Look for the requested usage (or wildcard ANY) in our list
1964 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if (cur_oid->len == usage_len &&
1969 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1970 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001971 }
1972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1974 return 0;
1975 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001976 }
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001979}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001982/*
1983 * Return 1 if the certificate is revoked, or 0 otherwise.
1984 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 while (cur != NULL && cur->serial.len != 0) {
1990 if (crt->serial.len == cur->serial.len &&
1991 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1992 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001993 }
1994
1995 cur = cur->next;
1996 }
1997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001999}
2000
2001/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002002 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002003 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002005static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2006 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002007 const mbedtls_x509_crt_profile *profile,
2008 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009{
2010 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002011 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002012 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +01002013 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if (ca == NULL) {
2016 return flags;
2017 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 while (crl_list != NULL) {
2020 if (crl_list->version == 0 ||
2021 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022 crl_list = crl_list->next;
2023 continue;
2024 }
2025
2026 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002027 * Check if the CA is configured to sign CRLs
2028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (mbedtls_x509_crt_check_key_usage(ca,
2030 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002032 break;
2033 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002034
2035 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002036 * Check if CRL is correctly signed by the trusted CA
2037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002039 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002043 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002045
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002046 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 if (psa_hash_compute(psa_algorithm,
2048 crl_list->tbs.p,
2049 crl_list->tbs.len,
2050 hash,
2051 sizeof(hash),
2052 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002053 /* Note: this can't happen except after an internal error */
2054 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2055 break;
2056 }
pespaceka7a64692022-02-14 15:18:43 +01002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002059 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002061
Valerio Setti7f6f4e62025-04-23 11:29:51 +02002062 if (mbedtls_pk_verify_ext(crl_list->sig_pk, NULL, &ca->pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 crl_list->sig_md, hash, hash_length,
2064 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002066 break;
2067 }
2068
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002069#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002070 /*
2071 * Check for validity of CRL (Do not drop out)
2072 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002073 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002077 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002078 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002080#else
2081 ((void) now);
2082#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002083
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084 /*
2085 * Check if certificate is revoked
2086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089 break;
2090 }
2091
2092 crl_list = crl_list->next;
2093 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002099/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002100 * Check the signature of a certificate by its parent
2101 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002102static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2103 mbedtls_x509_crt *parent,
2104 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002105{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002106 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002107 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002108 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 status = psa_hash_compute(hash_alg,
2112 child->tbs.p,
2113 child->tbs.len,
2114 hash,
2115 sizeof(hash),
2116 &hash_len);
2117 if (status != PSA_SUCCESS) {
2118 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002119 }
2120
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002121 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2123 return -1;
2124 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002125
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002126#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2128 return mbedtls_pk_verify_restartable(&parent->pk,
2129 child->sig_md, hash, hash_len,
2130 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002131 }
2132#else
2133 (void) rs_ctx;
2134#endif
2135
Valerio Setti7f6f4e62025-04-23 11:29:51 +02002136 return mbedtls_pk_verify_ext(child->sig_pk, NULL, &parent->pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 child->sig_md, hash, hash_len,
2138 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002139}
2140
2141/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002142 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2143 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002144 *
2145 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2148 const mbedtls_x509_crt *parent,
2149 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002150{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002151 int need_ca_bit;
2152
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002153 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2155 return -1;
2156 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002157
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002158 /* Parent must have the basicConstraints CA bit set as a general rule */
2159 need_ca_bit = 1;
2160
2161 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002163 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002164 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (need_ca_bit && !parent->ca_istrue) {
2167 return -1;
2168 }
2169
2170 if (need_ca_bit &&
2171 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2172 return -1;
2173 }
2174
2175 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002176}
2177
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002178/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002179 * Find a suitable parent for child in candidates, or return NULL.
2180 *
2181 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002182 * 1. subject name matches child's issuer
2183 * 2. if necessary, the CA bit is set and key usage allows signing certs
2184 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002185 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002186 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002187 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002188 * If there's a suitable candidate which is also time-valid, return the first
2189 * such. Otherwise, return the first suitable candidate (or NULL if there is
2190 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002191 *
2192 * The rationale for this rule is that someone could have a list of trusted
2193 * roots with two versions on the same root with different validity periods.
2194 * (At least one user reported having such a list and wanted it to just work.)
2195 * The reason we don't just require time-validity is that generally there is
2196 * only one version, and if it's expired we want the flags to state that
2197 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002198 *
2199 * The rationale for rule 3 (signature for trusted roots) is that users might
2200 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002201 * way we select the correct one is by checking the signature (as we don't
2202 * rely on key identifier extensions). (This is one way users might choose to
2203 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002204 *
2205 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002206 * - [in] child: certificate for which we're looking for a parent
2207 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002208 * - [out] r_parent: parent found (or NULL)
2209 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002210 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2211 * of the chain, 0 otherwise
2212 * - [in] path_cnt: number of intermediates seen so far
2213 * - [in] self_cnt: number of self-signed intermediates seen so far
2214 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002215 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002216 *
2217 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002218 * - 0 on success
2219 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002220 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002221static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 mbedtls_x509_crt *child,
2223 mbedtls_x509_crt *candidates,
2224 mbedtls_x509_crt **r_parent,
2225 int *r_signature_is_good,
2226 int top,
2227 unsigned path_cnt,
2228 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002229 mbedtls_x509_crt_restart_ctx *rs_ctx,
2230 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002231{
Janos Follath865b3eb2019-12-16 11:46:15 +00002232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002233 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002234 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002235
2236#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002237 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002239 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002240 parent = rs_ctx->parent;
2241 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002242 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002243
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002244 /* clear saved state */
2245 rs_ctx->parent = NULL;
2246 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002247 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002248
2249 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002250 goto check_signature;
2251 }
2252#endif
2253
2254 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002255 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002258 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002260 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002262
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002263 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (parent->max_pathlen > 0 &&
2265 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002266 continue;
2267 }
2268
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002269 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002270#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2271check_signature:
2272#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002274
2275#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002277 /* save state */
2278 rs_ctx->parent = parent;
2279 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002280 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002283 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002284#else
2285 (void) ret;
2286#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002287
2288 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002290 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002292
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002293#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002294 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002295 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2296 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002298 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002299 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002300 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002301
2302 continue;
2303 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002304#else
2305 ((void) now);
2306#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002307
Andy Gross1f627142019-01-30 10:25:53 -06002308 *r_parent = parent;
2309 *r_signature_is_good = signature_is_good;
2310
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002311 break;
2312 }
2313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002315 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002316 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002317 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002320}
2321
2322/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002323 * Find a parent in trusted CAs or the provided chain, or return NULL.
2324 *
2325 * Searches in trusted CAs first, and return the first suitable parent found
2326 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002327 *
2328 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002329 * - [in] child: certificate for which we're looking for a parent, followed
2330 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002331 * - [in] trust_ca: list of locally trusted certificates
2332 * - [out] parent: parent found (or NULL)
2333 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2334 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2335 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2336 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002337 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002338 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002339 *
2340 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002341 * - 0 on success
2342 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002343 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002344static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 mbedtls_x509_crt *child,
2346 mbedtls_x509_crt *trust_ca,
2347 mbedtls_x509_crt **parent,
2348 int *parent_is_trusted,
2349 int *signature_is_good,
2350 unsigned path_cnt,
2351 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002352 mbedtls_x509_crt_restart_ctx *rs_ctx,
2353 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002354{
Janos Follath865b3eb2019-12-16 11:46:15 +00002355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002356 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002357
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002358 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002359
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002360#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002361 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002363 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002364 rs_ctx->parent_is_trusted = -1;
2365 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002366#endif
2367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002369 search_list = *parent_is_trusted ? trust_ca : child->next;
2370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 ret = x509_crt_find_parent_in(child, search_list,
2372 parent, signature_is_good,
2373 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002374 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002375
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002376#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002378 /* save state */
2379 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002381 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002382#else
2383 (void) ret;
2384#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002385
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002386 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002388 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002390
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002391 /* prepare second iteration */
2392 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002393 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002394
2395 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002397 *parent_is_trusted = 0;
2398 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002399 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002402}
2403
2404/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002405 * Check if an end-entity certificate is locally trusted
2406 *
2407 * Currently we require such certificates to be self-signed (actually only
2408 * check for self-issued as self-signatures are not checked)
2409 */
2410static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 mbedtls_x509_crt *crt,
2412 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002413{
2414 mbedtls_x509_crt *cur;
2415
2416 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2418 return -1;
2419 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002420
2421 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2423 if (crt->raw.len == cur->raw.len &&
2424 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2425 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002426 }
2427 }
2428
2429 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002431}
2432
2433/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002434 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002435 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002436 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2437 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002438 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002439 * such that every cert in the chain is a child of the next one,
2440 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002441 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002442 * Verify that chain and return it with flags for all issues found.
2443 *
2444 * Special cases:
2445 * - EE == Rj -> return a one-element list containing it
2446 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2447 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002448 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002449 * Tests for (aspects of) this function should include at least:
2450 * - trusted EE
2451 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002452 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002453 * - if relevant: EE untrusted
2454 * - if relevant: EE -> intermediate, untrusted
2455 * with the aspect under test checked at each relevant level (EE, int, root).
2456 * For some aspects longer chains are required, but usually length 2 is
2457 * enough (but length 1 is not in general).
2458 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002459 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002460 * - [in] crt: the cert list EE, C1, ..., Cn
2461 * - [in] trust_ca: the trusted list R1, ..., Rp
2462 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002463 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002464 * Only valid when return value is 0, may contain garbage otherwise!
2465 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002466 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002467 *
2468 * Return value:
2469 * - non-zero if the chain could not be fully built and examined
2470 * - 0 is the chain was successfully built and examined,
2471 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002472 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002473static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 mbedtls_x509_crt *crt,
2475 mbedtls_x509_crt *trust_ca,
2476 mbedtls_x509_crl *ca_crl,
2477 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2478 void *p_ca_cb,
2479 const mbedtls_x509_crt_profile *profile,
2480 mbedtls_x509_crt_verify_chain *ver_chain,
2481 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002482{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002483 /* Don't initialize any of those variables here, so that the compiler can
2484 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002486 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002487 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002488 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002489 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002490 int parent_is_trusted;
2491 int child_is_trusted;
2492 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002493 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002494 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002495 mbedtls_x509_time now;
2496
2497#if defined(MBEDTLS_HAVE_TIME_DATE)
2498 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2499 return MBEDTLS_ERR_X509_FATAL_ERROR;
2500 }
2501#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002502
2503#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2504 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002506 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002507 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002508 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002509
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002510 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002511 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002512 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002513 flags = &cur->flags;
2514
2515 goto find_parent;
2516 }
2517#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002518
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002519 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002520 self_cnt = 0;
2521 parent_is_trusted = 0;
2522 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002525 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002526 cur = &ver_chain->items[ver_chain->len];
2527 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002528 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002529 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002530 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002531
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002532#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002533 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002534 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002535 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002537
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002538 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002539 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002541#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002542
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002543 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (child_is_trusted) {
2545 return 0;
2546 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002547
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002548 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002550 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002554 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002556
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002557 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (ver_chain->len == 1 &&
2559 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2560 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002561 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002562
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002563#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2564find_parent:
2565#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002566
2567 /* Obtain list of potential trusted signers from CA callback,
2568 * or use statically provided list. */
2569#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (f_ca_cb != NULL) {
2571 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2572 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002573 ver_chain->trust_ca_cb_result = NULL;
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2576 if (ret != 0) {
2577 return MBEDTLS_ERR_X509_FATAL_ERROR;
2578 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002579
2580 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002582#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2583 {
2584 ((void) f_ca_cb);
2585 ((void) p_ca_cb);
2586 cur_trust_ca = trust_ca;
2587 }
2588
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002589 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2591 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002592 ver_chain->len - 1, self_cnt, rs_ctx,
2593 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002594
2595#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002597 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002598 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002599 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002600 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002603 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002604#else
2605 (void) ret;
2606#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002607
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002608 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002610 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002612 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002613
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002614 /* Count intermediate self-issued (not necessarily self-signed) certs.
2615 * These can occur with some strategies for key rollover, see [SIRO],
2616 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (ver_chain->len != 1 &&
2618 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002619 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002620 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002621
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002622 /* path_cnt is 0 for the first intermediate CA,
2623 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (!parent_is_trusted &&
2625 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002626 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002628 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002629
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002630 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002632 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002634
2635 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002637 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002641 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002642 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002643#else
2644 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002645#endif
2646
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002647 /* prepare for next iteration */
2648 child = parent;
2649 parent = NULL;
2650 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002651 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002652 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002653}
2654
Glenn Straussc26bd762022-10-23 19:48:18 -04002655#ifdef _WIN32
2656#ifdef _MSC_VER
2657#pragma comment(lib, "ws2_32.lib")
2658#include <winsock2.h>
2659#include <ws2tcpip.h>
2660#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2661#include <winsock2.h>
2662#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002663#else
2664/* inet_pton() is not supported, fallback to software version */
2665#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002666#endif
2667#elif defined(__sun)
2668/* Solaris requires -lsocket -lnsl for inet_pton() */
2669#elif defined(__has_include)
2670#if __has_include(<sys/socket.h>)
2671#include <sys/socket.h>
2672#endif
2673#if __has_include(<arpa/inet.h>)
2674#include <arpa/inet.h>
2675#endif
2676#endif
2677
2678/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2679 * the platform inet_pton() or a local implementation (below). The local
2680 * implementation may be used even in cases where the platform provides
2681 * inet_pton(), e.g. when there are different includes required and/or the
2682 * platform implementation requires dependencies on additional libraries.
2683 * Specifically, Windows requires custom includes and additional link
2684 * dependencies, and Solaris requires additional link dependencies.
2685 * Also, as a coarse heuristic, use the local implementation if the compiler
2686 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002687 * provided by headers included (or not) via __has_include() above.
2688 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2689 * despite having a platform that has inet_pton. */
2690#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2691/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002692static int x509_inet_pton_ipv4(const char *src, void *dst);
2693
2694#define li_cton(c, n) \
2695 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2696
2697static int x509_inet_pton_ipv6(const char *src, void *dst)
2698{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002699 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002700 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002701 uint16_t addr[8];
2702 do {
2703 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002704 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002705 for (uint8_t digit; num_digits < 4; num_digits++) {
2706 if (li_cton(*p, digit) == 0) {
2707 break;
2708 }
2709 group = (group << 4) | digit;
2710 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002711 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002712 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002713 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2714 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002715 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002716 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002717 } else if (*p == '.') {
2718 /* Don't accept IPv4 too early or late */
2719 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2720 nonzero_groups >= 7) {
2721 break;
2722 }
2723
2724 /* Walk back to prior ':', then parse as IPv4-mapped */
2725 int steps = 4;
2726 do {
2727 p--;
2728 steps--;
2729 } while (*p != ':' && steps > 0);
2730
2731 if (*p != ':') {
2732 break;
2733 }
2734 p++;
2735 nonzero_groups--;
2736 if (x509_inet_pton_ipv4((const char *) p,
2737 addr + nonzero_groups) != 0) {
2738 break;
2739 }
2740
Andrzej Kurek0d578962023-04-13 09:09:56 -04002741 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002742 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002743 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002744 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002745 return -1;
2746 }
2747 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002748 /* Don't accept a second zero group or an invalid delimiter */
2749 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002750 return -1;
2751 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002752 zero_group_start = nonzero_groups;
2753
2754 /* Accept a zero group at start, but it has to be a double colon */
2755 if (zero_group_start == 0 && *++p != ':') {
2756 return -1;
2757 }
2758
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002759 if (p[1] == '\0') {
2760 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002761 break;
2762 }
2763 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002764 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002765 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002766
2767 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002768 return -1;
2769 }
2770
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002771 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002772 if (nonzero_groups > 6) {
2773 return -1;
2774 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002775 int zero_groups = 8 - nonzero_groups;
2776 int groups_after_zero = nonzero_groups - zero_group_start;
2777
2778 /* Move the non-zero part to after the zeroes */
2779 if (groups_after_zero) {
2780 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002781 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002782 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002783 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002784 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002785 } else {
2786 if (nonzero_groups != 8) {
2787 return -1;
2788 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002789 }
2790 memcpy(dst, addr, sizeof(addr));
2791 return 0;
2792}
2793
2794static int x509_inet_pton_ipv4(const char *src, void *dst)
2795{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002796 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002797 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002798 uint8_t digit, num_digits = 0;
2799 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002800 uint16_t octet;
2801
Glenn Strauss416c2952022-10-24 23:00:02 -04002802 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002803 octet = num_digits = 0;
2804 do {
2805 digit = *p - '0';
2806 if (digit > 9) {
2807 break;
2808 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002809
2810 /* Don't allow leading zeroes. These might mean octal format,
2811 * which this implementation does not support. */
2812 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002813 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002814 }
2815
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002816 octet = octet * 10 + digit;
2817 num_digits++;
2818 p++;
2819 } while (num_digits < 3);
2820
2821 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002822 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002823 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002824 *res++ = (uint8_t) octet;
2825 num_octets++;
2826 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002827 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002828}
Glenn Straussc26bd762022-10-23 19:48:18 -04002829
2830#else
2831
2832static int x509_inet_pton_ipv6(const char *src, void *dst)
2833{
2834 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2835}
2836
2837static int x509_inet_pton_ipv4(const char *src, void *dst)
2838{
2839 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2840}
2841
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002842#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002843
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002844size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002845{
2846 return strchr(cn, ':') == NULL
2847 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2848 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2849}
2850
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002851/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002852 * Check for CN match
2853 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002854static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2855 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002856{
2857 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 if (name->len == cn_len &&
2859 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2860 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002861 }
2862
2863 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 if (x509_check_wildcard(cn, name) == 0) {
2865 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002866 }
2867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002869}
2870
Glenn Straussc26bd762022-10-23 19:48:18 -04002871static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2872 const char *cn, size_t cn_len)
2873{
2874 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002875 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002876 if (cn_len == 0) {
2877 return -1;
2878 }
2879
2880 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2881 const unsigned char san_type = (unsigned char) cur->buf.tag &
2882 MBEDTLS_ASN1_TAG_VALUE_MASK;
2883 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2884 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2885 return 0;
2886 }
2887 }
2888
2889 return -1;
2890}
2891
Andrzej Kurek199eab92023-05-10 09:57:19 -04002892static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2893 const char *cn, size_t cn_len)
2894{
2895 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2896 const unsigned char san_type = (unsigned char) cur->buf.tag &
2897 MBEDTLS_ASN1_TAG_VALUE_MASK;
2898 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2899 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2900 return 0;
2901 }
2902 }
2903
2904 return -1;
2905}
2906
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002907/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002908 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2909 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002910static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002912{
Glenn Straussc26bd762022-10-23 19:48:18 -04002913 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002914 int san_uri = 0;
2915 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002916 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2917 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002918 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002919 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2920 return 0;
2921 }
2922 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002923 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002924 san_ip = 1;
2925 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002926 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2927 san_uri = 1;
2928 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002929 /* (We may handle other types here later.) */
2930 default: /* Unrecognized type */
2931 break;
2932 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002934 if (san_ip) {
2935 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2936 return 0;
2937 }
2938 }
2939 if (san_uri) {
2940 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2941 return 0;
2942 }
2943 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002944
Andrzej Kurek199eab92023-05-10 09:57:19 -04002945 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002946}
2947
2948/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002949 * Verify the requested CN - only call this if cn is not NULL!
2950 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002951static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2952 const char *cn,
2953 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002954{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002955 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002959 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2960 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 }
2962 } else {
2963 for (name = &crt->subject; name != NULL; name = name->next) {
2964 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2965 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002966 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 }
2968 }
2969
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002970 }
Glenn Straussc26bd762022-10-23 19:48:18 -04002971
2972 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002973}
2974
2975/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002976 * Merge the flags for all certs in the chain, after calling callback
2977 */
2978static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 uint32_t *flags,
2980 const mbedtls_x509_crt_verify_chain *ver_chain,
2981 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2982 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002983{
Janos Follath865b3eb2019-12-16 11:46:15 +00002984 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002985 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002986 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002987 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002990 cur = &ver_chain->items[i-1];
2991 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 if (NULL != f_vrfy) {
2994 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
2995 return ret;
2996 }
2997 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002998
2999 *flags |= cur_flags;
3000 }
3001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003003}
3004
3005/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003006 * Verify the certificate validity, with profile, restartable version
3007 *
3008 * This function:
3009 * - checks the requested CN (if any)
3010 * - checks the type and size of the EE cert's key,
3011 * as that isn't done as part of chain building/verification currently
3012 * - builds and verifies the chain
3013 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003014 *
3015 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3016 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3017 * verification routine to search for trusted signers, and CRLs will
3018 * be disabled. Otherwise, `trust_ca` will be used as the static list
3019 * of trusted signers, and `ca_crl` will be use as the static list
3020 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003022static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3023 mbedtls_x509_crt *trust_ca,
3024 mbedtls_x509_crl *ca_crl,
3025 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3026 void *p_ca_cb,
3027 const mbedtls_x509_crt_profile *profile,
3028 const char *cn, uint32_t *flags,
3029 int (*f_vrfy)(void *,
3030 mbedtls_x509_crt *,
3031 int,
3032 uint32_t *),
3033 void *p_vrfy,
3034 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003035{
Janos Follath865b3eb2019-12-16 11:46:15 +00003036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003037 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003038 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003039 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003040
3041 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003042 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003046 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3047 goto exit;
3048 }
3049
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003050 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 if (cn != NULL) {
3052 x509_crt_verify_name(crt, cn, &ee_flags);
3053 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003054
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003055 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003059 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003063 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003065
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003066 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3068 f_ca_cb, p_ca_cb, profile,
3069 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003072 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003074
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003075 /* Merge end-entity flags */
3076 ver_chain.items[0].flags |= ee_flags;
3077
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003078 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003080
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003081exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003082
3083#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3085 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003086 ver_chain.trust_ca_cb_result = NULL;
3087#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3088
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003089#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3091 mbedtls_x509_crt_restart_free(rs_ctx);
3092 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003093#endif
3094
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003095 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3096 * the SSL module for authmode optional, but non-zero return from the
3097 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003099 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003100 }
3101
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 if (ret != 0) {
3103 *flags = (uint32_t) -1;
3104 return ret;
3105 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003106
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 if (*flags != 0) {
3108 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3109 }
3110
3111 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003112}
3113
Hanno Becker3116fb32019-03-28 13:34:42 +00003114
3115/*
3116 * Verify the certificate validity (default profile, not restartable)
3117 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003118int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3119 mbedtls_x509_crt *trust_ca,
3120 mbedtls_x509_crl *ca_crl,
3121 const char *cn, uint32_t *flags,
3122 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3123 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003124{
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3126 NULL, NULL,
3127 &mbedtls_x509_crt_profile_default,
3128 cn, flags,
3129 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003130}
3131
3132/*
3133 * Verify the certificate validity (user-chosen profile, not restartable)
3134 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003135int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3136 mbedtls_x509_crt *trust_ca,
3137 mbedtls_x509_crl *ca_crl,
3138 const mbedtls_x509_crt_profile *profile,
3139 const char *cn, uint32_t *flags,
3140 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3141 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003142{
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3144 NULL, NULL,
3145 profile, cn, flags,
3146 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003147}
3148
3149#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3150/*
3151 * Verify the certificate validity (user-chosen profile, CA callback,
3152 * not restartable).
3153 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003154int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3155 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3156 void *p_ca_cb,
3157 const mbedtls_x509_crt_profile *profile,
3158 const char *cn, uint32_t *flags,
3159 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3160 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003161{
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3163 f_ca_cb, p_ca_cb,
3164 profile, cn, flags,
3165 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003166}
3167#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3170 mbedtls_x509_crt *trust_ca,
3171 mbedtls_x509_crl *ca_crl,
3172 const mbedtls_x509_crt_profile *profile,
3173 const char *cn, uint32_t *flags,
3174 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3175 void *p_vrfy,
3176 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003177{
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3179 NULL, NULL,
3180 profile, cn, flags,
3181 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003182}
3183
3184
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003185/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003186 * Initialize a certificate chain
3187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003188void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003189{
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003191}
3192
3193/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003194 * Unallocate all certificate data
3195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003196void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 mbedtls_x509_crt *cert_cur = crt;
3199 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 while (cert_cur != NULL) {
3202 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3205 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3206 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3207 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3208 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003209 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003210
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003212 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003213 }
3214
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003215 cert_prv = cert_cur;
3216 cert_cur = cert_cur->next;
3217
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3219 if (cert_prv != crt) {
3220 mbedtls_free(cert_prv);
3221 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003222 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003223}
3224
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003225#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3226/*
3227 * Initialize a restart context
3228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003229void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003230{
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003232
3233 ctx->parent = NULL;
3234 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003235 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003236
3237 ctx->parent_is_trusted = -1;
3238
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003239 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003240 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003242}
3243
3244/*
3245 * Free the components of a restart context
3246 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003247void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003248{
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003250 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 mbedtls_pk_restart_free(&ctx->pk);
3254 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003255}
3256#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3257
Minos Galanakis2abbac72024-01-18 17:05:21 +00003258int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt *crt)
3259{
3260 if ((crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) != 0) {
3261 return crt->MBEDTLS_PRIVATE(ca_istrue);
3262 }
3263 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
3264}
3265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266#endif /* MBEDTLS_X509_CRT_PARSE_C */