blob: 8fc8a5273ba1d92b0cb4d63fe5b34548603b6284 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020049#include "psa_util_internal.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020050#include "md_psa.h"
pespacek7599a772022-02-07 14:40:55 +010051#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti3f00b842023-05-15 12:57:06 +020052#include "pk_internal.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040053
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010058#endif
59
Daniel Axtensf0710242020-05-28 11:43:41 +100060#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010061#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Glenn Straussc26bd762022-10-23 19:48:18 -040062#define WIN32_LEAN_AND_MEAN
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063#include <windows.h>
Simon Butchere068aa72018-03-14 15:10:31 +000064#if defined(_MSC_VER) && _MSC_VER <= 1600
Kevin Kane0ec1e682016-12-15 09:27:16 -080065/* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and <intsafe.h> are included, as they
66 * redefine a number of <TYPE>_MAX constants. These constants are guaranteed to be the same, though, so
67 * we suppress the warning when including intsafe.h.
68 */
69#pragma warning( push )
70#pragma warning( disable : 4005 )
71#endif
72#include <intsafe.h>
Simon Butchere068aa72018-03-14 15:10:31 +000073#if defined(_MSC_VER) && _MSC_VER <= 1600
Kevin Kane0ec1e682016-12-15 09:27:16 -080074#pragma warning( pop )
75#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#else
77#include <time.h>
78#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100079#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000082#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020083#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084#include <sys/types.h>
85#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020086#if defined(__MBED__)
87#include <platform/mbed_retarget.h>
88#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020090#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060091#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000092#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#endif
94
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020095/*
96 * Item in a verification chain: cert and flags for it
97 */
98typedef struct {
99 mbedtls_x509_crt *crt;
100 uint32_t flags;
101} x509_crt_verify_chain_item;
102
103/*
104 * Max size of verification chain: end-entity + intermediates + trusted root
105 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +0200107
Gilles Peskineffb92da2021-06-02 00:03:26 +0200108/* Default profile. Do not remove items unless there are serious security
109 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200110const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
111{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200112 /* Hashes from SHA-256 and above. Note that this selection
113 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
115 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
116 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200117 0xFFFFFFF, /* Any PK alg */
Valerio Setti8c3404f2023-06-26 15:49:48 +0200118#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200119 /* Curves at or above 128-bit security level. Note that this selection
120 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
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) |
Gilles Peskine39957502021-06-17 23:17:52 +0200127 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200128#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200129 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200130#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200131 2048,
132};
133
Gilles Peskineffb92da2021-06-02 00:03:26 +0200134/* Next-generation profile. Currently identical to the default, but may
135 * be tightened at any time. */
136const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200137{
138 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
140 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
141 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200142 0xFFFFFFF, /* Any PK alg */
143#if defined(MBEDTLS_ECP_C)
144 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
146 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
147 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
148 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
149 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
150 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
151 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200152#else
153 0,
154#endif
155 2048,
156};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200157
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200158/*
159 * NSA Suite B Profile
160 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200161const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
162{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200163 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
165 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200166 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
168 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200169#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200170 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
172 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Setti8c3404f2023-06-26 15:49:48 +0200173#else /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200174 0,
Valerio Setti8c3404f2023-06-26 15:49:48 +0200175#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200176 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200177};
178
179/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200180 * Empty / all-forbidden profile
181 */
182const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
183{
184 0,
185 0,
186 0,
187 (uint32_t) -1,
188};
189
190/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200192 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200193 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
195 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196{
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if (md_alg == MBEDTLS_MD_NONE) {
198 return -1;
199 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
202 return 0;
203 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206}
207
208/*
209 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200210 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
213 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200214{
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (pk_alg == MBEDTLS_PK_NONE) {
216 return -1;
217 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
220 return 0;
221 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200224}
225
226/*
227 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200228 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200229 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100230static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
231 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200232{
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200234
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200235#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
237 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
238 return 0;
239 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200242 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200243#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200244
Valerio Setti8c3404f2023-06-26 15:49:48 +0200245#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200247 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 pk_alg == MBEDTLS_PK_ECKEY_DH) {
Valerio Setti97207782023-05-18 18:59:06 +0200249 const mbedtls_ecp_group_id gid = mbedtls_pk_get_group_id(pk);
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (gid == MBEDTLS_ECP_DP_NONE) {
252 return -1;
253 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
256 return 0;
257 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200260 }
Valerio Setti8c3404f2023-06-26 15:49:48 +0200261#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200264}
265
266/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000267 * Like memcmp, but case-insensitive and always returns -1 if different
268 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000270{
271 size_t i;
272 unsigned char diff;
273 const unsigned char *n1 = s1, *n2 = s2;
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000276 diff = n1[i] ^ n2[i];
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000279 continue;
280 }
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (diff == 32 &&
283 ((n1[i] >= 'a' && n1[i] <= 'z') ||
284 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
285 continue;
286 }
287
288 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000289 }
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000292}
293
294/*
295 * Return 0 if name matches wildcard, -1 otherwise
296 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100297static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000298{
299 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000301
302 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
304 return -1;
305 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 for (i = 0; i < cn_len; ++i) {
308 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000309 cn_idx = i;
310 break;
311 }
312 }
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (cn_idx == 0) {
315 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (cn_len - cn_idx == name->len - 1 &&
319 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
320 return 0;
321 }
322
323 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000324}
325
326/*
327 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
328 * variations (but not all).
329 *
330 * Return 0 if equal, -1 otherwise.
331 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000333{
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000335 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 memcmp(a->p, b->p, b->len) == 0) {
337 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000338 }
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
341 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000342 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 x509_memcasecmp(a->p, b->p, b->len) == 0) {
344 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000345 }
346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000348}
349
350/*
351 * Compare two X.509 Names (aka rdnSequence).
352 *
353 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
354 * we sometimes return unequal when the full algorithm would return equal,
355 * but never the other way. (In particular, we don't do Unicode normalisation
356 * or space folding.)
357 *
358 * Return 0 if equal, -1 otherwise.
359 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000361{
362 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 while (a != NULL || b != NULL) {
364 if (a == NULL || b == NULL) {
365 return -1;
366 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000367
368 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000370 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
372 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000373 }
374
375 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (x509_string_cmp(&a->val, &b->val) != 0) {
377 return -1;
378 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000379
380 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (a->next_merged != b->next_merged) {
382 return -1;
383 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000384
385 a = a->next;
386 b = b->next;
387 }
388
389 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000391}
392
393/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200394 * Reset (init or clear) a verify_chain
395 */
396static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200398{
399 size_t i;
400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200402 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000403 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200404 }
405
406 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000407
408#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
409 ver_chain->trust_ca_cb_result = NULL;
410#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200411}
412
413/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
415 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416static int x509_get_version(unsigned char **p,
417 const unsigned char *end,
418 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419{
Janos Follath865b3eb2019-12-16 11:46:15 +0000420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421 size_t len;
422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
424 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
425 0)) != 0) {
426 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429 }
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432 }
433
434 end = *p + len;
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
437 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
438 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (*p != end) {
441 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
442 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
443 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446}
447
448/*
449 * Validity ::= SEQUENCE {
450 * notBefore Time,
451 * notAfter Time }
452 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100453static int x509_get_dates(unsigned char **p,
454 const unsigned char *end,
455 mbedtls_x509_time *from,
456 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457{
Janos Follath865b3eb2019-12-16 11:46:15 +0000458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 size_t len;
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
462 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
463 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
464 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465
466 end = *p + len;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
469 return ret;
470 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
473 return ret;
474 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (*p != end) {
477 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
478 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
479 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482}
483
484/*
485 * X.509 v2/v3 unique identifier (not parsed)
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487static int x509_get_uid(unsigned char **p,
488 const unsigned char *end,
489 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490{
Janos Follath865b3eb2019-12-16 11:46:15 +0000491 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (*p == end) {
494 return 0;
495 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
497 uid->tag = **p;
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
500 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
501 n)) != 0) {
502 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
503 return 0;
504 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 }
508
509 uid->p = *p;
510 *p += uid->len;
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513}
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515static int x509_get_basic_constraints(unsigned char **p,
516 const unsigned char *end,
517 int *ca_istrue,
518 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519{
Janos Follath865b3eb2019-12-16 11:46:15 +0000520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521 size_t len;
522
523 /*
524 * BasicConstraints ::= SEQUENCE {
525 * cA BOOLEAN DEFAULT FALSE,
526 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
527 */
528 *ca_istrue = 0; /* DEFAULT FALSE */
529 *max_pathlen = 0; /* endless */
530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
532 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
533 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 }
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (*p == end) {
537 return 0;
538 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
541 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
542 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
543 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (ret != 0) {
546 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
547 }
548
549 if (*ca_istrue != 0) {
550 *ca_istrue = 1;
551 }
552 }
553
554 if (*p == end) {
555 return 0;
556 }
557
558 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
559 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
560 }
561
562 if (*p != end) {
563 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
564 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
565 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
Andrzej Kurek16050742020-04-14 09:49:52 -0400567 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
568 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (*max_pathlen == INT_MAX) {
570 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
571 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
572 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400573
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574 (*max_pathlen)++;
575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577}
578
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579/*
580 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
581 *
582 * KeyPurposeId ::= OBJECT IDENTIFIER
583 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100584static int x509_get_ext_key_usage(unsigned char **p,
585 const unsigned char *end,
586 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587{
Janos Follath865b3eb2019-12-16 11:46:15 +0000588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
591 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
592 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593
594 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (ext_key_usage->buf.p == NULL) {
596 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
597 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
598 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601}
602
603/*
toth92ga41954d2021-02-12 16:11:17 +0100604 * SubjectKeyIdentifier ::= KeyIdentifier
605 *
606 * KeyIdentifier ::= OCTET STRING
607 */
608static int x509_get_subject_key_id(unsigned char **p,
609 const unsigned char *end,
610 mbedtls_x509_buf *subject_key_id)
611{
612 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
613 size_t len = 0u;
614
615 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
616 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100617 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100618 }
619
Przemek Stekiel61aed062023-05-08 11:14:36 +0200620 subject_key_id->len = len;
621 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
622 subject_key_id->p = *p;
623 *p += len;
624
toth92gd96027a2021-04-27 15:41:25 +0200625 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100626 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
627 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200628 }
629
toth92ga41954d2021-02-12 16:11:17 +0100630 return 0;
631}
632
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100633/*
toth92g8d435a02021-05-10 15:16:33 +0200634 * AuthorityKeyIdentifier ::= SEQUENCE {
635 * keyIdentifier [0] KeyIdentifier OPTIONAL,
636 * authorityCertIssuer [1] GeneralNames OPTIONAL,
637 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
638 *
639 * KeyIdentifier ::= OCTET STRING
640 */
641static int x509_get_authority_key_id(unsigned char **p,
642 unsigned char *end,
643 mbedtls_x509_authority *authority_key_id)
644{
645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
646 size_t len = 0u;
647
648 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100649 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100650 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200651 }
652
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100653 if (*p + len != end) {
654 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
655 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
656 }
657
Przemek Stekieled9fb782023-05-03 16:27:25 +0200658 ret = mbedtls_asn1_get_tag(p, end, &len,
659 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
660
661 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200662 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200663 authority_key_id->keyIdentifier.len = len;
664 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200665 /* Setting tag of the keyIdentfier intentionally to 0x04.
666 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200667 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200668 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
669
670 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200671 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
672 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200673 }
674
675 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200676 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200677 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
678 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100679 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200680 /* authorityCertIssuer and authorityCertSerialNumber MUST both
681 be present or both be absent. At this point we expect to have both. */
682 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200683 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200684 /* "end" also includes the CertSerialNumber field so "len" shall be used */
685 ret = mbedtls_x509_get_subject_alt_name_ext(p,
686 (*p+len),
687 &authority_key_id->authorityCertIssuer);
688 if (ret != 0) {
689 return ret;
690 }
691
692 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
693 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
694 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
695 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
696 }
697 authority_key_id->authorityCertSerialNumber.len = len;
698 authority_key_id->authorityCertSerialNumber.p = *p;
699 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
700 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200701 }
702
703 if (*p != end) {
704 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
705 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
706 }
707
708 return 0;
709}
710
711/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200712 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
713 *
714 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
715 *
716 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
717 *
718 * PolicyInformation ::= SEQUENCE {
719 * policyIdentifier CertPolicyId,
720 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
721 * PolicyQualifierInfo OPTIONAL }
722 *
723 * CertPolicyId ::= OBJECT IDENTIFIER
724 *
725 * PolicyQualifierInfo ::= SEQUENCE {
726 * policyQualifierId PolicyQualifierId,
727 * qualifier ANY DEFINED BY policyQualifierId }
728 *
729 * -- policyQualifierIds for Internet policy qualifiers
730 *
731 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
732 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
733 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
734 *
735 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
736 *
737 * Qualifier ::= CHOICE {
738 * cPSuri CPSuri,
739 * userNotice UserNotice }
740 *
741 * CPSuri ::= IA5String
742 *
743 * UserNotice ::= SEQUENCE {
744 * noticeRef NoticeReference OPTIONAL,
745 * explicitText DisplayText OPTIONAL }
746 *
747 * NoticeReference ::= SEQUENCE {
748 * organization DisplayText,
749 * noticeNumbers SEQUENCE OF INTEGER }
750 *
751 * DisplayText ::= CHOICE {
752 * ia5String IA5String (SIZE (1..200)),
753 * visibleString VisibleString (SIZE (1..200)),
754 * bmpString BMPString (SIZE (1..200)),
755 * utf8String UTF8String (SIZE (1..200)) }
756 *
757 * NOTE: we only parse and use anyPolicy without qualifiers at this point
758 * as defined in RFC 5280.
759 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100760static int x509_get_certificate_policies(unsigned char **p,
761 const unsigned char *end,
762 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200763{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300764 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200765 size_t len;
766 mbedtls_asn1_buf *buf;
767 mbedtls_asn1_sequence *cur = certificate_policies;
768
769 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 ret = mbedtls_asn1_get_tag(p, end, &len,
771 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
772 if (ret != 0) {
773 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
774 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (*p + len != end) {
777 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
778 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
779 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200780
781 /*
782 * Cannot be an empty sequence.
783 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 if (len == 0) {
785 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
786 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
787 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200790 mbedtls_x509_buf policy_oid;
791 const unsigned char *policy_end;
792
793 /*
794 * Get the policy sequence
795 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
797 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
798 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
799 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200800
801 policy_end = *p + len;
802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
804 MBEDTLS_ASN1_OID)) != 0) {
805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
806 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200807
808 policy_oid.tag = MBEDTLS_ASN1_OID;
809 policy_oid.len = len;
810 policy_oid.p = *p;
811
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300812 /*
813 * Only AnyPolicy is currently supported when enforcing policy.
814 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300816 /*
817 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200818 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300819 */
820 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
821 }
822
Ron Eldor74d9acc2019-03-21 14:00:03 +0200823 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (cur->buf.p != NULL) {
825 if (cur->next != NULL) {
826 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
827 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (cur->next == NULL) {
832 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
833 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
834 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200835
836 cur = cur->next;
837 }
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200840 buf->tag = policy_oid.tag;
841 buf->p = policy_oid.p;
842 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300843
844 *p += len;
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 /*
847 * If there is an optional qualifier, then *p < policy_end
848 * Check the Qualifier len to verify it doesn't exceed policy_end.
849 */
850 if (*p < policy_end) {
851 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
852 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
853 0) {
854 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
855 }
Ron Eldor08063792019-05-13 16:38:39 +0300856 /*
857 * Skip the optional policy qualifiers.
858 */
859 *p += len;
860 }
861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (*p != policy_end) {
863 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
864 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
865 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200866 }
867
868 /* Set final sequence entry's next pointer to NULL */
869 cur->next = NULL;
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (*p != end) {
872 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
873 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
874 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200877}
878
879/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 * X.509 v3 extensions
881 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883static int x509_get_crt_ext(unsigned char **p,
884 const unsigned char *end,
885 mbedtls_x509_crt *crt,
886 mbedtls_x509_crt_ext_cb_t cb,
887 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888{
Janos Follath865b3eb2019-12-16 11:46:15 +0000889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200891 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (*p == end) {
894 return 0;
895 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
898 return ret;
899 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900
Hanno Becker12f62fb2019-02-12 17:22:36 +0000901 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 /*
904 * Extension ::= SEQUENCE {
905 * extnID OBJECT IDENTIFIER,
906 * critical BOOLEAN DEFAULT FALSE,
907 * extnValue OCTET STRING }
908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910 int is_critical = 0; /* DEFAULT FALSE */
911 int ext_type = 0;
912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
914 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
915 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
916 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917
918 end_ext_data = *p + len;
919
920 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
922 MBEDTLS_ASN1_OID)) != 0) {
923 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
924 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
k-stachowiak463928a2018-07-24 12:50:59 +0200926 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 extn_oid.p = *p;
928 *p += extn_oid.len;
929
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
932 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
933 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
934 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
936 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
938 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
939 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
940 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200942 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943 end_ext_octet = *p + len;
944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (end_ext_octet != end_ext_data) {
946 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
947 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
948 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949
950 /*
951 * Detect supported extensions
952 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200956 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (cb != NULL) {
958 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
959 if (ret != 0 && is_critical) {
960 return ret;
961 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200962 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200963 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200964 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200965
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966 /* No parser found, skip extension */
967 *p = end_ext_octet;
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
972 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 continue;
975 }
976
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100977 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if ((crt->ext_types & ext_type) != 0) {
979 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
980 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100981
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 crt->ext_types |= ext_type;
983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 switch (ext_type) {
985 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
986 /* Parse basic constraints */
987 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
988 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
989 return ret;
990 }
991 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 case MBEDTLS_X509_EXT_KEY_USAGE:
994 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100995 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
996 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return ret;
998 }
999 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
1002 /* Parse extended key usage */
1003 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
1004 &crt->ext_key_usage)) != 0) {
1005 return ret;
1006 }
1007 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008
toth92ga41954d2021-02-12 16:11:17 +01001009 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1010 /* Parse subject key identifier */
1011 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1012 &crt->subject_key_id)) != 0) {
1013 return ret;
1014 }
1015 break;
toth92g8d435a02021-05-10 15:16:33 +02001016
toth92ga41954d2021-02-12 16:11:17 +01001017 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1018 /* Parse authority key identifier */
1019 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1020 &crt->authority_key_id)) != 0) {
1021 return ret;
1022 }
1023 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001025 /* Parse subject alt name
1026 * SubjectAltName ::= GeneralNames
1027 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001028 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1029 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return ret;
1031 }
1032 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1035 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001036 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1037 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 return ret;
1039 }
1040 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1043 /* Parse certificate policies type */
1044 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1045 &crt->certificate_policies)) != 0) {
1046 /* Give the callback (if any) a chance to handle the extension
1047 * if it contains unsupported policies */
1048 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1049 cb(p_ctx, crt, &extn_oid, is_critical,
1050 start_ext_octet, end_ext_octet) == 0) {
1051 break;
1052 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (is_critical) {
1055 return ret;
1056 } else
1057 /*
1058 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1059 * cannot interpret or enforce the policy. However, it is up to
1060 * the user to choose how to enforce the policies,
1061 * unless the extension is critical.
1062 */
1063 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1064 return ret;
1065 }
1066 }
1067 break;
1068
1069 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001070 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 * If this is a non-critical extension, which the oid layer
1072 * supports, but there isn't an x509 parser for it,
1073 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001074 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (is_critical) {
1076 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1077 } else {
1078 *p = end_ext_octet;
1079 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080 }
1081 }
1082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (*p != end) {
1084 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1085 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1086 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089}
1090
1091/*
1092 * Parse and fill a single X.509 certificate in DER format
1093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1095 const unsigned char *buf,
1096 size_t buflen,
1097 int make_copy,
1098 mbedtls_x509_crt_ext_cb_t cb,
1099 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100{
Janos Follath865b3eb2019-12-16 11:46:15 +00001101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102 size_t len;
1103 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1107 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1108 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109
1110 /*
1111 * Check for valid input
1112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if (crt == NULL || buf == NULL) {
1114 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1115 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001117 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001119 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120 end = p + len;
1121
1122 /*
1123 * Certificate ::= SEQUENCE {
1124 * tbsCertificate TBSCertificate,
1125 * signatureAlgorithm AlgorithmIdentifier,
1126 * signatureValue BIT STRING }
1127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1129 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1130 mbedtls_x509_crt_free(crt);
1131 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 }
1133
Janos Follathcc0e49d2016-02-17 14:34:12 +00001134 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001135 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001137 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1139 if (crt->raw.p == NULL) {
1140 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1141 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001144 crt->own_buffer = 1;
1145
1146 p += crt->raw.len - len;
1147 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 } else {
1149 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001150 crt->own_buffer = 0;
1151 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001152
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153 /*
1154 * TBSCertificate ::= SEQUENCE {
1155 */
1156 crt->tbs.p = p;
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1159 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1160 mbedtls_x509_crt_free(crt);
1161 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 }
1163
1164 end = p + len;
1165 crt->tbs.len = end - crt->tbs.p;
1166
1167 /*
1168 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1169 *
1170 * CertificateSerialNumber ::= INTEGER
1171 *
1172 * signature AlgorithmIdentifier
1173 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1175 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1176 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1177 &sig_params1)) != 0) {
1178 mbedtls_x509_crt_free(crt);
1179 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 }
1181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 if (crt->version < 0 || crt->version > 2) {
1183 mbedtls_x509_crt_free(crt);
1184 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 }
1186
Andres AG7ca4a032017-03-09 16:16:11 +00001187 crt->version++;
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1190 &crt->sig_md, &crt->sig_pk,
1191 &crt->sig_opts)) != 0) {
1192 mbedtls_x509_crt_free(crt);
1193 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 }
1195
1196 /*
1197 * issuer Name
1198 */
1199 crt->issuer_raw.p = p;
1200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1202 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1203 mbedtls_x509_crt_free(crt);
1204 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205 }
1206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1208 mbedtls_x509_crt_free(crt);
1209 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001210 }
1211
1212 crt->issuer_raw.len = p - crt->issuer_raw.p;
1213
1214 /*
1215 * Validity ::= SEQUENCE {
1216 * notBefore Time,
1217 * notAfter Time }
1218 *
1219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1221 &crt->valid_to)) != 0) {
1222 mbedtls_x509_crt_free(crt);
1223 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224 }
1225
1226 /*
1227 * subject Name
1228 */
1229 crt->subject_raw.p = p;
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1232 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1233 mbedtls_x509_crt_free(crt);
1234 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 }
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1238 mbedtls_x509_crt_free(crt);
1239 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001240 }
1241
1242 crt->subject_raw.len = p - crt->subject_raw.p;
1243
1244 /*
1245 * SubjectPublicKeyInfo
1246 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001247 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1249 mbedtls_x509_crt_free(crt);
1250 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001252 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253
1254 /*
1255 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1256 * -- If present, version shall be v2 or v3
1257 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1258 * -- If present, version shall be v2 or v3
1259 * extensions [3] EXPLICIT Extensions OPTIONAL
1260 * -- If present, version shall be v3
1261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 if (crt->version == 2 || crt->version == 3) {
1263 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1264 if (ret != 0) {
1265 mbedtls_x509_crt_free(crt);
1266 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 }
1268 }
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (crt->version == 2 || crt->version == 3) {
1271 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1272 if (ret != 0) {
1273 mbedtls_x509_crt_free(crt);
1274 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001275 }
1276 }
1277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 if (crt->version == 3) {
1279 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1280 if (ret != 0) {
1281 mbedtls_x509_crt_free(crt);
1282 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 }
1284 }
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (p != end) {
1287 mbedtls_x509_crt_free(crt);
1288 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1289 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 }
1291
1292 end = crt_end;
1293
1294 /*
1295 * }
1296 * -- end of TBSCertificate
1297 *
1298 * signatureAlgorithm AlgorithmIdentifier,
1299 * signatureValue BIT STRING
1300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1302 mbedtls_x509_crt_free(crt);
1303 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 }
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (crt->sig_oid.len != sig_oid2.len ||
1307 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001308 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001309 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 (sig_params1.len != 0 &&
1311 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1312 mbedtls_x509_crt_free(crt);
1313 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 }
1315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1317 mbedtls_x509_crt_free(crt);
1318 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 }
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (p != end) {
1322 mbedtls_x509_crt_free(crt);
1323 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1324 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325 }
1326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328}
1329
1330/*
1331 * Parse one X.509 certificate in DER format from a buffer and add them to a
1332 * chained list
1333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1335 const unsigned char *buf,
1336 size_t buflen,
1337 int make_copy,
1338 mbedtls_x509_crt_ext_cb_t cb,
1339 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340{
Janos Follath865b3eb2019-12-16 11:46:15 +00001341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343
1344 /*
1345 * Check for valid input
1346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (crt == NULL || buf == NULL) {
1348 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1349 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352 prev = crt;
1353 crt = crt->next;
1354 }
1355
1356 /*
1357 * Add new certificate on the end of the chain if needed.
1358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (crt->version != 0 && crt->next == NULL) {
1360 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (crt->next == NULL) {
1363 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1364 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
1366 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369 }
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1372 if (ret != 0) {
1373 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 if (crt != chain) {
1378 mbedtls_free(crt);
1379 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385}
1386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1388 const unsigned char *buf,
1389 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001390{
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001392}
1393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1395 const unsigned char *buf,
1396 size_t buflen,
1397 int make_copy,
1398 mbedtls_x509_crt_ext_cb_t cb,
1399 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001400{
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001402}
1403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1405 const unsigned char *buf,
1406 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001407{
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001409}
1410
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001412 * Parse one or more PEM certificates from a buffer and add them to the chained
1413 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001415int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1416 const unsigned char *buf,
1417 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001418{
Janos Follath98e28a72016-05-31 14:03:54 +01001419#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001420 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001422#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423
1424 /*
1425 * Check for valid input
1426 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (chain == NULL || buf == NULL) {
1428 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1429 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430
1431 /*
1432 * Determine buffer content. Buffer contains either one DER certificate or
1433 * one or more PEM certificates.
1434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1437 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001439 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1442 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1443 }
Janos Follath98e28a72016-05-31 14:03:54 +01001444#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001446#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001458 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 ret = mbedtls_pem_read_buffer(&pem,
1460 "-----BEGIN CERTIFICATE-----",
1461 "-----END CERTIFICATE-----",
1462 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465 /*
1466 * Was PEM encoded
1467 */
1468 buflen -= use_len;
1469 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1471 return ret;
1472 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1473 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474
1475 /*
1476 * PEM header and footer were found
1477 */
1478 buflen -= use_len;
1479 buf += use_len;
1480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001484
Paul Bakker5a5fa922014-09-26 14:53:04 +02001485 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001496 /*
1497 * Quit parsing on a memory error
1498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1500 return ret;
1501 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506
1507 total_failed++;
1508 continue;
1509 }
1510
1511 success = 1;
1512 }
1513 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (success) {
1516 return total_failed;
1517 } else if (first_error) {
1518 return first_error;
1519 } else {
1520 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1521 }
Janos Follath98e28a72016-05-31 14:03:54 +01001522#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523}
1524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526/*
1527 * Load one or more certificates and add them to the chained list
1528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530{
Janos Follath865b3eb2019-12-16 11:46:15 +00001531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532 size_t n;
1533 unsigned char *buf;
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1536 return ret;
1537 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001541 mbedtls_zeroize_and_free(buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544}
1545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547{
1548 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001549#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Steve Lhomme369d7c72023-06-16 14:16:03 +02001550#if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_XP */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 int w_ret;
1552 WCHAR szDir[MAX_PATH];
1553 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001554 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 size_t len = strlen(path);
Kevin Kane0ec1e682016-12-15 09:27:16 -08001556 int lengthAsInt = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557
Paul Bakker9af723c2014-05-01 13:03:14 +02001558 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559 HANDLE hFind;
1560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if (len > MAX_PATH - 3) {
1562 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1563 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 memset(szDir, 0, sizeof(szDir));
1566 memset(filename, 0, MAX_PATH);
1567 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001568 filename[len++] = '\\';
1569 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570 filename[len++] = '*';
1571
Kevin Kane0ec1e682016-12-15 09:27:16 -08001572 if (FAILED (SizeTToInt(len, &lengthAsInt)))
1573 return(MBEDTLS_ERR_X509_FILE_IO_ERROR);
1574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1576 MAX_PATH - 3);
1577 if (w_ret == 0) {
1578 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1579 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 hFind = FindFirstFileW(szDir, &file_data);
1582 if (hFind == INVALID_HANDLE_VALUE) {
1583 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1584 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585
1586 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 do {
1588 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001595 -1,
1596 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 NULL, NULL);
Kevin Kane0ec1e682016-12-15 09:27:16 -08001598 if (FAILED(SizeTToInt(wcslen(file_data.cFileName), &lengthAsInt)))
1599 return(MBEDTLS_ERR_X509_FILE_IO_ERROR);
1600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001602 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1603 goto cleanup;
1604 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1607 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001608 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 }
1612 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617
Ron Eldor36d90422017-01-09 15:09:16 +02001618cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 FindClose(hFind);
Steve Lhomme369d7c72023-06-16 14:16:03 +02001620#else /* !_WIN32_WINNT_XP */
Antonio de Angelis1ee4d122023-08-16 12:26:37 +01001621#error "mbedtls_x509_crt_parse_path not available before Windows XP"
Steve Lhomme369d7c72023-06-16 14:16:03 +02001622#endif /* !_WIN32_WINNT_XP */
Paul Bakkerbe089b02013-10-14 15:51:50 +02001623#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001624 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001625 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001627 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001628 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if (dir == NULL) {
1632 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1633 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634
Ron Eldor63140682017-01-09 19:27:59 +02001635#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1637 closedir(dir);
1638 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001639 }
Ron Eldor63140682017-01-09 19:27:59 +02001640#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001645 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001648 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001649 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1650 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 } else if (stat(entry_name, &sb) == -1) {
1652 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001653 /* Broken symbolic link - ignore this entry.
1654 stat(2) will return this error for either (a) a dangling
1655 symlink or (b) a missing file.
1656 Given that we have just obtained the filename from readdir,
1657 assume that it does exist and therefore treat this as a
1658 dangling symlink. */
1659 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001661 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001662 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1663 goto cleanup;
1664 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001665 }
1666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670
1671 // Ignore parse errors
1672 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1674 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001680
1681cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001683
Ron Eldor63140682017-01-09 19:27:59 +02001684#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 }
Ron Eldor63140682017-01-09 19:27:59 +02001688#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001689
Paul Bakkerbe089b02013-10-14 15:51:50 +02001690#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695
Peter Kolbus9a969b62018-12-11 13:55:56 -06001696#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001697#define PRINT_ITEM(i) \
1698 do { \
1699 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1700 MBEDTLS_X509_SAFE_SNPRINTF; \
1701 sep = ", "; \
1702 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001703
Przemek Stekielf4194942023-04-24 09:52:17 +02001704#define CERT_TYPE(type, name) \
1705 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001706 if (ns_cert_type & (type)) { \
1707 PRINT_ITEM(name); \
1708 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001709 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001710
Przemek Stekielf4194942023-04-24 09:52:17 +02001711#define KEY_USAGE(code, name) \
1712 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001713 if (key_usage & (code)) { \
1714 PRINT_ITEM(name); \
1715 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001716 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718static int x509_info_ext_key_usage(char **buf, size_t *size,
1719 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001720{
Janos Follath865b3eb2019-12-16 11:46:15 +00001721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001722 const char *desc;
1723 size_t n = *size;
1724 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001726 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 while (cur != NULL) {
1729 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001730 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001734 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001735
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001736 sep = ", ";
1737
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001738 cur = cur->next;
1739 }
1740
1741 *size = n;
1742 *buf = p;
1743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001745}
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747static int x509_info_cert_policies(char **buf, size_t *size,
1748 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001749{
Janos Follath865b3eb2019-12-16 11:46:15 +00001750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001751 const char *desc;
1752 size_t n = *size;
1753 char *p = *buf;
1754 const mbedtls_x509_sequence *cur = certificate_policies;
1755 const char *sep = "";
1756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 while (cur != NULL) {
1758 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001759 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001763 MBEDTLS_X509_SAFE_SNPRINTF;
1764
1765 sep = ", ";
1766
1767 cur = cur->next;
1768 }
1769
1770 *size = n;
1771 *buf = p;
1772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001774}
1775
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776/*
1777 * Return an informational string about the certificate.
1778 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001779#define BEFORE_COLON 18
1780#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001781int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1782 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001783{
Janos Follath865b3eb2019-12-16 11:46:15 +00001784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001785 size_t n;
1786 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001787 char key_size_str[BEFORE_COLON];
1788
1789 p = buf;
1790 n = size;
1791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 if (NULL == crt) {
1793 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001794 MBEDTLS_X509_SAFE_SNPRINTF;
1795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001797 }
1798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1800 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001801 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1803 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001804 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001807 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001810 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001812 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001815 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001817 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1820 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1821 crt->valid_from.year, crt->valid_from.mon,
1822 crt->valid_from.day, crt->valid_from.hour,
1823 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001824 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1827 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1828 crt->valid_to.year, crt->valid_to.mon,
1829 crt->valid_to.day, crt->valid_to.hour,
1830 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001831 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001834 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1837 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001838 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001839
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001840 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1842 mbedtls_pk_get_name(&crt->pk))) != 0) {
1843 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001844 }
1845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1847 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001848 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001850 /*
1851 * Optional extensions
1852 */
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1855 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1856 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001857 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (crt->max_pathlen > 0) {
1860 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001861 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001862 }
1863 }
1864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1866 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001867 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001868
Przemek Stekiel21c37282023-01-16 08:47:49 +01001869 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1870 &crt->subject_alt_names,
1871 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 return ret;
1873 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001874 }
1875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1877 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001878 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001879
Przemek Stekiel21c37282023-01-16 08:47:49 +01001880 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 return ret;
1882 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001883 }
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1886 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001887 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001888
Przemek Stekiel21c37282023-01-16 08:47:49 +01001889 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 return ret;
1891 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001892 }
1893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1895 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001896 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 if ((ret = x509_info_ext_key_usage(&p, &n,
1899 &crt->ext_key_usage)) != 0) {
1900 return ret;
1901 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001902 }
1903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1905 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001906 MBEDTLS_X509_SAFE_SNPRINTF;
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 if ((ret = x509_info_cert_policies(&p, &n,
1909 &crt->certificate_policies)) != 0) {
1910 return ret;
1911 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001912 }
1913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001915 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918}
1919
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001920struct x509_crt_verify_string {
1921 int code;
1922 const char *string;
1923};
1924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001926static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001927 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001928 { 0, NULL }
1929};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001930#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1933 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001934{
Janos Follath865b3eb2019-12-16 11:46:15 +00001935 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001936 const struct x509_crt_verify_string *cur;
1937 char *p = buf;
1938 size_t n = size;
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1941 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001942 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001946 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001947 flags ^= cur->code;
1948 }
1949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if (flags != 0) {
1951 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1952 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001953 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001954 }
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001957}
Hanno Becker612a2f12020-10-09 09:19:39 +01001958#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1961 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001962{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001963 unsigned int usage_must, usage_may;
1964 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1968 return 0;
1969 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001970
1971 usage_must = usage & ~may_mask;
1972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1974 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1975 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001976
1977 usage_may = usage & may_mask;
1978
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1980 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1981 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001984}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1987 const char *usage_oid,
1988 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001989{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001991
1992 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1994 return 0;
1995 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001996
1997 /*
1998 * Look for the requested usage (or wildcard ANY) in our list
1999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if (cur_oid->len == usage_len &&
2004 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2005 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002006 }
2007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
2009 return 0;
2010 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002011 }
2012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002014}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002017/*
2018 * Return 1 if the certificate is revoked, or 0 otherwise.
2019 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002020int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002021{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 while (cur != NULL && cur->serial.len != 0) {
2025 if (crt->serial.len == cur->serial.len &&
2026 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2027 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028 }
2029
2030 cur = cur->next;
2031 }
2032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002034}
2035
2036/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002037 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002038 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002039 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2041 mbedtls_x509_crl *crl_list,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002042 const mbedtls_x509_crt_profile *profile,
2043 const mbedtls_x509_time *now)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044{
2045 int flags = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002046 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002047#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002048 psa_algorithm_t psa_algorithm;
2049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002051#endif /* MBEDTLS_USE_PSA_CRYPTO */
2052 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if (ca == NULL) {
2055 return flags;
2056 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 while (crl_list != NULL) {
2059 if (crl_list->version == 0 ||
2060 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061 crl_list = crl_list->next;
2062 continue;
2063 }
2064
2065 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002066 * Check if the CA is configured to sign CRLs
2067 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 if (mbedtls_x509_crt_check_key_usage(ca,
2069 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002071 break;
2072 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002073
2074 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075 * Check if CRL is correctly signed by the trusted CA
2076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002078 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002082 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002084
pespacek7599a772022-02-07 14:40:55 +01002085#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002086 psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (psa_hash_compute(psa_algorithm,
2088 crl_list->tbs.p,
2089 crl_list->tbs.len,
2090 hash,
2091 sizeof(hash),
2092 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002093 /* Note: this can't happen except after an internal error */
2094 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2095 break;
2096 }
pespacek7599a772022-02-07 14:40:55 +01002097#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2099 hash_length = mbedtls_md_get_size(md_info);
2100 if (mbedtls_md(md_info,
2101 crl_list->tbs.p,
2102 crl_list->tbs.len,
2103 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002104 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106 break;
2107 }
pespaceka7a64692022-02-14 15:18:43 +01002108#endif /* MBEDTLS_USE_PSA_CRYPTO */
2109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002111 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2115 crl_list->sig_md, hash, hash_length,
2116 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118 break;
2119 }
2120
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002121#if defined(MBEDTLS_HAVE_TIME_DATE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122 /*
2123 * Check for validity of CRL (Do not drop out)
2124 */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002125 if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002129 if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002130 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002132#else
2133 ((void) now);
2134#endif
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002135
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136 /*
2137 * Check if certificate is revoked
2138 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002141 break;
2142 }
2143
2144 crl_list = crl_list->next;
2145 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002150
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002151/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002152 * Check the signature of a certificate by its parent
2153 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2155 mbedtls_x509_crt *parent,
2156 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002157{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002158 size_t hash_len;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +02002159 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002160#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2161 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 md_info = mbedtls_md_info_from_type(child->sig_md);
2163 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002164
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002165 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2167 return -1;
2168 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002169#else
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02002170 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002171 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 status = psa_hash_compute(hash_alg,
2174 child->tbs.p,
2175 child->tbs.len,
2176 hash,
2177 sizeof(hash),
2178 &hash_len);
2179 if (status != PSA_SUCCESS) {
2180 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002181 }
2182
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002183#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002184 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2186 return -1;
2187 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002188
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002189#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2191 return mbedtls_pk_verify_restartable(&parent->pk,
2192 child->sig_md, hash, hash_len,
2193 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002194 }
2195#else
2196 (void) rs_ctx;
2197#endif
2198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2200 child->sig_md, hash, hash_len,
2201 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002202}
2203
2204/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002205 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2206 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002207 *
2208 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2211 const mbedtls_x509_crt *parent,
2212 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002213{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002214 int need_ca_bit;
2215
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002216 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2218 return -1;
2219 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002220
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002221 /* Parent must have the basicConstraints CA bit set as a general rule */
2222 need_ca_bit = 1;
2223
2224 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002226 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002227 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 if (need_ca_bit && !parent->ca_istrue) {
2230 return -1;
2231 }
2232
2233 if (need_ca_bit &&
2234 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2235 return -1;
2236 }
2237
2238 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002239}
2240
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002241/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002242 * Find a suitable parent for child in candidates, or return NULL.
2243 *
2244 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002245 * 1. subject name matches child's issuer
2246 * 2. if necessary, the CA bit is set and key usage allows signing certs
2247 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002248 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002249 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002250 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002251 * If there's a suitable candidate which is also time-valid, return the first
2252 * such. Otherwise, return the first suitable candidate (or NULL if there is
2253 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002254 *
2255 * The rationale for this rule is that someone could have a list of trusted
2256 * roots with two versions on the same root with different validity periods.
2257 * (At least one user reported having such a list and wanted it to just work.)
2258 * The reason we don't just require time-validity is that generally there is
2259 * only one version, and if it's expired we want the flags to state that
2260 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002261 *
2262 * The rationale for rule 3 (signature for trusted roots) is that users might
2263 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002264 * way we select the correct one is by checking the signature (as we don't
2265 * rely on key identifier extensions). (This is one way users might choose to
2266 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002267 *
2268 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002269 * - [in] child: certificate for which we're looking for a parent
2270 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002271 * - [out] r_parent: parent found (or NULL)
2272 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002273 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2274 * of the chain, 0 otherwise
2275 * - [in] path_cnt: number of intermediates seen so far
2276 * - [in] self_cnt: number of self-signed intermediates seen so far
2277 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002278 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002279 *
2280 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002281 * - 0 on success
2282 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002283 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002284static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 mbedtls_x509_crt *child,
2286 mbedtls_x509_crt *candidates,
2287 mbedtls_x509_crt **r_parent,
2288 int *r_signature_is_good,
2289 int top,
2290 unsigned path_cnt,
2291 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002292 mbedtls_x509_crt_restart_ctx *rs_ctx,
2293 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002294{
Janos Follath865b3eb2019-12-16 11:46:15 +00002295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002296 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002297 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002298
2299#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002300 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002302 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002303 parent = rs_ctx->parent;
2304 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002305 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002306
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002307 /* clear saved state */
2308 rs_ctx->parent = NULL;
2309 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002310 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002311
2312 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002313 goto check_signature;
2314 }
2315#endif
2316
2317 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002318 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002321 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002323 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002325
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002326 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (parent->max_pathlen > 0 &&
2328 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002329 continue;
2330 }
2331
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002332 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002333#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2334check_signature:
2335#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002337
2338#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002340 /* save state */
2341 rs_ctx->parent = parent;
2342 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002343 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002346 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002347#else
2348 (void) ret;
2349#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002350
2351 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002353 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002355
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002356#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002357 /* optional time check */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002358 if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2359 mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002361 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002362 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002363 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002364
2365 continue;
2366 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002367#else
2368 ((void) now);
2369#endif
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002370
Andy Gross1f627142019-01-30 10:25:53 -06002371 *r_parent = parent;
2372 *r_signature_is_good = signature_is_good;
2373
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002374 break;
2375 }
2376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002378 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002379 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002380 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002383}
2384
2385/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002386 * Find a parent in trusted CAs or the provided chain, or return NULL.
2387 *
2388 * Searches in trusted CAs first, and return the first suitable parent found
2389 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002390 *
2391 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002392 * - [in] child: certificate for which we're looking for a parent, followed
2393 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002394 * - [in] trust_ca: list of locally trusted certificates
2395 * - [out] parent: parent found (or NULL)
2396 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2397 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2398 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2399 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002400 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002401 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002402 *
2403 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002404 * - 0 on success
2405 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002406 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002407static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 mbedtls_x509_crt *child,
2409 mbedtls_x509_crt *trust_ca,
2410 mbedtls_x509_crt **parent,
2411 int *parent_is_trusted,
2412 int *signature_is_good,
2413 unsigned path_cnt,
2414 unsigned self_cnt,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002415 mbedtls_x509_crt_restart_ctx *rs_ctx,
2416 const mbedtls_x509_time *now)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002417{
Janos Follath865b3eb2019-12-16 11:46:15 +00002418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002419 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002420
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002421 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002422
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002423#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002424 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002426 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002427 rs_ctx->parent_is_trusted = -1;
2428 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002429#endif
2430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002432 search_list = *parent_is_trusted ? trust_ca : child->next;
2433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 ret = x509_crt_find_parent_in(child, search_list,
2435 parent, signature_is_good,
2436 *parent_is_trusted,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002437 path_cnt, self_cnt, rs_ctx, now);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002438
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002439#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002441 /* save state */
2442 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002444 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002445#else
2446 (void) ret;
2447#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002448
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002449 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002451 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002453
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002454 /* prepare second iteration */
2455 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002456 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002457
2458 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002460 *parent_is_trusted = 0;
2461 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002462 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002465}
2466
2467/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002468 * Check if an end-entity certificate is locally trusted
2469 *
2470 * Currently we require such certificates to be self-signed (actually only
2471 * check for self-issued as self-signatures are not checked)
2472 */
2473static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 mbedtls_x509_crt *crt,
2475 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002476{
2477 mbedtls_x509_crt *cur;
2478
2479 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2481 return -1;
2482 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002483
2484 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2486 if (crt->raw.len == cur->raw.len &&
2487 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2488 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002489 }
2490 }
2491
2492 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002494}
2495
2496/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002497 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002498 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002499 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2500 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002501 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002502 * such that every cert in the chain is a child of the next one,
2503 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002504 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002505 * Verify that chain and return it with flags for all issues found.
2506 *
2507 * Special cases:
2508 * - EE == Rj -> return a one-element list containing it
2509 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2510 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002511 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002512 * Tests for (aspects of) this function should include at least:
2513 * - trusted EE
2514 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002515 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002516 * - if relevant: EE untrusted
2517 * - if relevant: EE -> intermediate, untrusted
2518 * with the aspect under test checked at each relevant level (EE, int, root).
2519 * For some aspects longer chains are required, but usually length 2 is
2520 * enough (but length 1 is not in general).
2521 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002522 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002523 * - [in] crt: the cert list EE, C1, ..., Cn
2524 * - [in] trust_ca: the trusted list R1, ..., Rp
2525 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002526 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002527 * Only valid when return value is 0, may contain garbage otherwise!
2528 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002529 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002530 *
2531 * Return value:
2532 * - non-zero if the chain could not be fully built and examined
2533 * - 0 is the chain was successfully built and examined,
2534 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002535 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002536static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 mbedtls_x509_crt *crt,
2538 mbedtls_x509_crt *trust_ca,
2539 mbedtls_x509_crl *ca_crl,
2540 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2541 void *p_ca_cb,
2542 const mbedtls_x509_crt_profile *profile,
2543 mbedtls_x509_crt_verify_chain *ver_chain,
2544 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002545{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002546 /* Don't initialize any of those variables here, so that the compiler can
2547 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002548 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002549 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002550 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002551 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002552 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002553 int parent_is_trusted;
2554 int child_is_trusted;
2555 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002556 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002557 mbedtls_x509_crt *cur_trust_ca = NULL;
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002558 mbedtls_x509_time now;
2559
2560#if defined(MBEDTLS_HAVE_TIME_DATE)
2561 if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2562 return MBEDTLS_ERR_X509_FATAL_ERROR;
2563 }
2564#endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002565
2566#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2567 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002569 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002570 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002571 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002572
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002573 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002574 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002575 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002576 flags = &cur->flags;
2577
2578 goto find_parent;
2579 }
2580#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002581
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002582 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002583 self_cnt = 0;
2584 parent_is_trusted = 0;
2585 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002588 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002589 cur = &ver_chain->items[ver_chain->len];
2590 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002591 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002592 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002593 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002594
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002595#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002596 /* Check time-validity (all certificates) */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002597 if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002598 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002600
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002601 if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002602 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 }
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002604#endif
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002605
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002606 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (child_is_trusted) {
2608 return 0;
2609 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002610
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002611 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002613 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002617 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002619
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002620 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (ver_chain->len == 1 &&
2622 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2623 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002624 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002625
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002626#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2627find_parent:
2628#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002629
2630 /* Obtain list of potential trusted signers from CA callback,
2631 * or use statically provided list. */
2632#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 if (f_ca_cb != NULL) {
2634 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2635 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002636 ver_chain->trust_ca_cb_result = NULL;
2637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2639 if (ret != 0) {
2640 return MBEDTLS_ERR_X509_FATAL_ERROR;
2641 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002642
2643 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002645#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2646 {
2647 ((void) f_ca_cb);
2648 ((void) p_ca_cb);
2649 cur_trust_ca = trust_ca;
2650 }
2651
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002652 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2654 &parent_is_trusted, &signature_is_good,
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002655 ver_chain->len - 1, self_cnt, rs_ctx,
2656 &now);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002657
2658#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002660 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002661 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002662 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002663 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002666 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002667#else
2668 (void) ret;
2669#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002670
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002671 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002673 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002675 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002676
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002677 /* Count intermediate self-issued (not necessarily self-signed) certs.
2678 * These can occur with some strategies for key rollover, see [SIRO],
2679 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 if (ver_chain->len != 1 &&
2681 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002682 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002683 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002684
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002685 /* path_cnt is 0 for the first intermediate CA,
2686 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 if (!parent_is_trusted &&
2688 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002689 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002691 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002692
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002693 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002695 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002697
2698 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002700 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002704 /* Check trusted CA's CRL for the given crt */
Glenn Strauss4b2a6e82022-06-30 12:17:58 -04002705 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002706#else
2707 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002708#endif
2709
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002710 /* prepare for next iteration */
2711 child = parent;
2712 parent = NULL;
2713 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002714 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002715 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002716}
2717
Glenn Straussc26bd762022-10-23 19:48:18 -04002718#ifdef _WIN32
2719#ifdef _MSC_VER
2720#pragma comment(lib, "ws2_32.lib")
2721#include <winsock2.h>
2722#include <ws2tcpip.h>
2723#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2724#include <winsock2.h>
2725#include <ws2tcpip.h>
Steve Lhommeeb0f18a2023-06-16 14:12:19 +02002726#else
2727/* inet_pton() is not supported, fallback to software version */
2728#define MBEDTLS_TEST_SW_INET_PTON
Glenn Straussc26bd762022-10-23 19:48:18 -04002729#endif
2730#elif defined(__sun)
2731/* Solaris requires -lsocket -lnsl for inet_pton() */
2732#elif defined(__has_include)
2733#if __has_include(<sys/socket.h>)
2734#include <sys/socket.h>
2735#endif
2736#if __has_include(<arpa/inet.h>)
2737#include <arpa/inet.h>
2738#endif
2739#endif
2740
2741/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2742 * the platform inet_pton() or a local implementation (below). The local
2743 * implementation may be used even in cases where the platform provides
2744 * inet_pton(), e.g. when there are different includes required and/or the
2745 * platform implementation requires dependencies on additional libraries.
2746 * Specifically, Windows requires custom includes and additional link
2747 * dependencies, and Solaris requires additional link dependencies.
2748 * Also, as a coarse heuristic, use the local implementation if the compiler
2749 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002750 * provided by headers included (or not) via __has_include() above.
2751 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2752 * despite having a platform that has inet_pton. */
2753#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2754/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002755static int x509_inet_pton_ipv4(const char *src, void *dst);
2756
2757#define li_cton(c, n) \
2758 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2759
2760static int x509_inet_pton_ipv6(const char *src, void *dst)
2761{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002762 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002763 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002764 uint16_t addr[8];
2765 do {
2766 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002767 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002768 for (uint8_t digit; num_digits < 4; num_digits++) {
2769 if (li_cton(*p, digit) == 0) {
2770 break;
2771 }
2772 group = (group << 4) | digit;
2773 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002774 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002775 if (num_digits != 0) {
Dave Rodgmancfa72232023-09-05 16:20:33 +01002776 MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2777 nonzero_groups++;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002778 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002779 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002780 } else if (*p == '.') {
2781 /* Don't accept IPv4 too early or late */
2782 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2783 nonzero_groups >= 7) {
2784 break;
2785 }
2786
2787 /* Walk back to prior ':', then parse as IPv4-mapped */
2788 int steps = 4;
2789 do {
2790 p--;
2791 steps--;
2792 } while (*p != ':' && steps > 0);
2793
2794 if (*p != ':') {
2795 break;
2796 }
2797 p++;
2798 nonzero_groups--;
2799 if (x509_inet_pton_ipv4((const char *) p,
2800 addr + nonzero_groups) != 0) {
2801 break;
2802 }
2803
Andrzej Kurek0d578962023-04-13 09:09:56 -04002804 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002805 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002806 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002807 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002808 return -1;
2809 }
2810 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002811 /* Don't accept a second zero group or an invalid delimiter */
2812 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002813 return -1;
2814 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002815 zero_group_start = nonzero_groups;
2816
2817 /* Accept a zero group at start, but it has to be a double colon */
2818 if (zero_group_start == 0 && *++p != ':') {
2819 return -1;
2820 }
2821
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002822 if (p[1] == '\0') {
2823 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002824 break;
2825 }
2826 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002827 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002828 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002829
2830 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002831 return -1;
2832 }
2833
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002834 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002835 if (nonzero_groups > 6) {
2836 return -1;
2837 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002838 int zero_groups = 8 - nonzero_groups;
2839 int groups_after_zero = nonzero_groups - zero_group_start;
2840
2841 /* Move the non-zero part to after the zeroes */
2842 if (groups_after_zero) {
2843 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002844 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002845 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002846 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002847 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002848 } else {
2849 if (nonzero_groups != 8) {
2850 return -1;
2851 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002852 }
2853 memcpy(dst, addr, sizeof(addr));
2854 return 0;
2855}
2856
2857static int x509_inet_pton_ipv4(const char *src, void *dst)
2858{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002859 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002860 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002861 uint8_t digit, num_digits = 0;
2862 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002863 uint16_t octet;
2864
Glenn Strauss416c2952022-10-24 23:00:02 -04002865 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002866 octet = num_digits = 0;
2867 do {
2868 digit = *p - '0';
2869 if (digit > 9) {
2870 break;
2871 }
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002872
2873 /* Don't allow leading zeroes. These might mean octal format,
2874 * which this implementation does not support. */
2875 if (octet == 0 && num_digits > 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002876 return -1;
Andrzej Kurek6f400a32023-05-01 05:26:47 -04002877 }
2878
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002879 octet = octet * 10 + digit;
2880 num_digits++;
2881 p++;
2882 } while (num_digits < 3);
2883
2884 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Andrzej Kurek9c9880a2023-05-03 05:06:47 -04002885 return -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002886 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002887 *res++ = (uint8_t) octet;
2888 num_octets++;
2889 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002890 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002891}
Glenn Straussc26bd762022-10-23 19:48:18 -04002892
2893#else
2894
2895static int x509_inet_pton_ipv6(const char *src, void *dst)
2896{
2897 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2898}
2899
2900static int x509_inet_pton_ipv4(const char *src, void *dst)
2901{
2902 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2903}
2904
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002905#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002906
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002907size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002908{
2909 return strchr(cn, ':') == NULL
2910 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2911 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2912}
2913
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002914/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002915 * Check for CN match
2916 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002917static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2918 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002919{
2920 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (name->len == cn_len &&
2922 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2923 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002924 }
2925
2926 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 if (x509_check_wildcard(cn, name) == 0) {
2928 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002929 }
2930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002932}
2933
Glenn Straussc26bd762022-10-23 19:48:18 -04002934static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2935 const char *cn, size_t cn_len)
2936{
2937 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002938 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002939 if (cn_len == 0) {
2940 return -1;
2941 }
2942
2943 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2944 const unsigned char san_type = (unsigned char) cur->buf.tag &
2945 MBEDTLS_ASN1_TAG_VALUE_MASK;
2946 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2947 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2948 return 0;
2949 }
2950 }
2951
2952 return -1;
2953}
2954
Andrzej Kurek199eab92023-05-10 09:57:19 -04002955static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2956 const char *cn, size_t cn_len)
2957{
2958 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2959 const unsigned char san_type = (unsigned char) cur->buf.tag &
2960 MBEDTLS_ASN1_TAG_VALUE_MASK;
2961 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2962 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2963 return 0;
2964 }
2965 }
2966
2967 return -1;
2968}
2969
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002970/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002971 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2972 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002973static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002975{
Glenn Straussc26bd762022-10-23 19:48:18 -04002976 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002977 int san_uri = 0;
2978 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002979 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2980 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002981 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002982 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2983 return 0;
2984 }
2985 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002986 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002987 san_ip = 1;
2988 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002989 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2990 san_uri = 1;
2991 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002992 /* (We may handle other types here later.) */
2993 default: /* Unrecognized type */
2994 break;
2995 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002997 if (san_ip) {
2998 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2999 return 0;
3000 }
3001 }
3002 if (san_uri) {
3003 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
3004 return 0;
3005 }
3006 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003007
Andrzej Kurek199eab92023-05-10 09:57:19 -04003008 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003009}
3010
3011/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003012 * Verify the requested CN - only call this if cn is not NULL!
3013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003014static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
3015 const char *cn,
3016 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003017{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003018 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003020
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003022 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
3023 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 }
3025 } else {
3026 for (name = &crt->subject; name != NULL; name = name->next) {
3027 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
3028 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04003029 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 }
3031 }
3032
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003033 }
Glenn Straussc26bd762022-10-23 19:48:18 -04003034
3035 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003036}
3037
3038/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003039 * Merge the flags for all certs in the chain, after calling callback
3040 */
3041static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 uint32_t *flags,
3043 const mbedtls_x509_crt_verify_chain *ver_chain,
3044 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3045 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003046{
Janos Follath865b3eb2019-12-16 11:46:15 +00003047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003048 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003049 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003050 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003053 cur = &ver_chain->items[i-1];
3054 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 if (NULL != f_vrfy) {
3057 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3058 return ret;
3059 }
3060 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003061
3062 *flags |= cur_flags;
3063 }
3064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003066}
3067
3068/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003069 * Verify the certificate validity, with profile, restartable version
3070 *
3071 * This function:
3072 * - checks the requested CN (if any)
3073 * - checks the type and size of the EE cert's key,
3074 * as that isn't done as part of chain building/verification currently
3075 * - builds and verifies the chain
3076 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003077 *
3078 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3079 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3080 * verification routine to search for trusted signers, and CRLs will
3081 * be disabled. Otherwise, `trust_ca` will be used as the static list
3082 * of trusted signers, and `ca_crl` will be use as the static list
3083 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003085static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3086 mbedtls_x509_crt *trust_ca,
3087 mbedtls_x509_crl *ca_crl,
3088 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3089 void *p_ca_cb,
3090 const mbedtls_x509_crt_profile *profile,
3091 const char *cn, uint32_t *flags,
3092 int (*f_vrfy)(void *,
3093 mbedtls_x509_crt *,
3094 int,
3095 uint32_t *),
3096 void *p_vrfy,
3097 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003098{
Janos Follath865b3eb2019-12-16 11:46:15 +00003099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003100 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003101 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003102 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003103
3104 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003105 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003109 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3110 goto exit;
3111 }
3112
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003113 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 if (cn != NULL) {
3115 x509_crt_verify_name(crt, cn, &ee_flags);
3116 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003117
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003118 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003120
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003122 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003126 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003128
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003129 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3131 f_ca_cb, p_ca_cb, profile,
3132 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003135 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003137
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003138 /* Merge end-entity flags */
3139 ver_chain.items[0].flags |= ee_flags;
3140
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003141 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003143
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003144exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003145
3146#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3148 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003149 ver_chain.trust_ca_cb_result = NULL;
3150#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3151
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003152#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3154 mbedtls_x509_crt_restart_free(rs_ctx);
3155 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003156#endif
3157
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003158 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3159 * the SSL module for authmode optional, but non-zero return from the
3160 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003162 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003163 }
3164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 if (ret != 0) {
3166 *flags = (uint32_t) -1;
3167 return ret;
3168 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003169
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 if (*flags != 0) {
3171 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3172 }
3173
3174 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003175}
3176
Hanno Becker3116fb32019-03-28 13:34:42 +00003177
3178/*
3179 * Verify the certificate validity (default profile, not restartable)
3180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003181int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3182 mbedtls_x509_crt *trust_ca,
3183 mbedtls_x509_crl *ca_crl,
3184 const char *cn, uint32_t *flags,
3185 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3186 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003187{
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3189 NULL, NULL,
3190 &mbedtls_x509_crt_profile_default,
3191 cn, flags,
3192 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003193}
3194
3195/*
3196 * Verify the certificate validity (user-chosen profile, not restartable)
3197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003198int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3199 mbedtls_x509_crt *trust_ca,
3200 mbedtls_x509_crl *ca_crl,
3201 const mbedtls_x509_crt_profile *profile,
3202 const char *cn, uint32_t *flags,
3203 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3204 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003205{
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3207 NULL, NULL,
3208 profile, cn, flags,
3209 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003210}
3211
3212#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3213/*
3214 * Verify the certificate validity (user-chosen profile, CA callback,
3215 * not restartable).
3216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003217int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3218 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3219 void *p_ca_cb,
3220 const mbedtls_x509_crt_profile *profile,
3221 const char *cn, uint32_t *flags,
3222 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3223 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003224{
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3226 f_ca_cb, p_ca_cb,
3227 profile, cn, flags,
3228 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003229}
3230#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3231
Gilles Peskine449bd832023-01-11 14:50:10 +01003232int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3233 mbedtls_x509_crt *trust_ca,
3234 mbedtls_x509_crl *ca_crl,
3235 const mbedtls_x509_crt_profile *profile,
3236 const char *cn, uint32_t *flags,
3237 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3238 void *p_vrfy,
3239 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003240{
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3242 NULL, NULL,
3243 profile, cn, flags,
3244 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003245}
3246
3247
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003248/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003249 * Initialize a certificate chain
3250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003251void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003252{
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003254}
3255
3256/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003257 * Unallocate all certificate data
3258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003259void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261 mbedtls_x509_crt *cert_cur = crt;
3262 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003263
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 while (cert_cur != NULL) {
3265 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003267#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003269#endif
3270
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3272 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3273 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3274 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3275 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Przemek Stekiel690ff692023-05-15 09:54:02 +02003276 mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01003279 mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003280 }
3281
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003282 cert_prv = cert_cur;
3283 cert_cur = cert_cur->next;
3284
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3286 if (cert_prv != crt) {
3287 mbedtls_free(cert_prv);
3288 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003289 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003290}
3291
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003292#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3293/*
3294 * Initialize a restart context
3295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003296void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003297{
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003299
3300 ctx->parent = NULL;
3301 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003302 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003303
3304 ctx->parent_is_trusted = -1;
3305
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003306 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003307 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003309}
3310
3311/*
3312 * Free the components of a restart context
3313 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003314void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003315{
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003317 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003319
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 mbedtls_pk_restart_free(&ctx->pk);
3321 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003322}
3323#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003325#endif /* MBEDTLS_X509_CRT_PARSE_C */