blob: 8775bcbb7e1e849269b5c9912b116910a3ebb9ca [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel40afdd22022-09-06 13:08:28 +020051#include "hash_info.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010057#endif
58
Daniel Axtensf0710242020-05-28 11:43:41 +100059#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Glenn Straussc26bd762022-10-23 19:48:18 -040061#define WIN32_LEAN_AND_MEAN
62#ifndef _WIN32_WINNT
63#define _WIN32_WINNT 0x0600
64#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#include <windows.h>
66#else
67#include <time.h>
68#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100069#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000072#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020073#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#include <sys/types.h>
75#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020076#if defined(__MBED__)
77#include <platform/mbed_retarget.h>
78#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020080#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060081#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000082#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#endif
84
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020085/*
86 * Item in a verification chain: cert and flags for it
87 */
88typedef struct {
89 mbedtls_x509_crt *crt;
90 uint32_t flags;
91} x509_crt_verify_chain_item;
92
93/*
94 * Max size of verification chain: end-entity + intermediates + trusted root
95 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020097
Gilles Peskineffb92da2021-06-02 00:03:26 +020098/* Default profile. Do not remove items unless there are serious security
99 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200100const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
101{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200102 /* Hashes from SHA-256 and above. Note that this selection
103 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
105 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
106 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 0xFFFFFFF, /* Any PK alg */
108#if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200109 /* Curves at or above 128-bit security level. Note that this selection
110 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
112 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
113 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
114 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
115 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
116 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200117 0,
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200118#else
119 0,
120#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200121 2048,
122};
123
Gilles Peskineffb92da2021-06-02 00:03:26 +0200124/* Next-generation profile. Currently identical to the default, but may
125 * be tightened at any time. */
126const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200127{
128 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
130 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
131 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200132 0xFFFFFFF, /* Any PK alg */
133#if defined(MBEDTLS_ECP_C)
134 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
136 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
138 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
139 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
140 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
141 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200142#else
143 0,
144#endif
145 2048,
146};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200147
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200148/*
149 * NSA Suite B Profile
150 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200151const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
152{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200153 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
155 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200156 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
158 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200159#if defined(MBEDTLS_ECP_C)
160 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
162 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200163#else
164 0,
165#endif
166 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200167};
168
169/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200170 * Empty / all-forbidden profile
171 */
172const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
173{
174 0,
175 0,
176 0,
177 (uint32_t) -1,
178};
179
180/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200181 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200182 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
185 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200186{
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (md_alg == MBEDTLS_MD_NONE) {
188 return -1;
189 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
192 return 0;
193 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196}
197
198/*
199 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200200 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200201 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
203 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200204{
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (pk_alg == MBEDTLS_PK_NONE) {
206 return -1;
207 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
210 return 0;
211 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200214}
215
216/*
217 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200218 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200219 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
221 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200222{
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200224
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200225#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
227 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
228 return 0;
229 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200232 }
233#endif
234
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200235#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200237 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 pk_alg == MBEDTLS_PK_ECKEY_DH) {
239 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (gid == MBEDTLS_ECP_DP_NONE) {
242 return -1;
243 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
246 return 0;
247 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200250 }
251#endif
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200254}
255
256/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000257 * Like memcmp, but case-insensitive and always returns -1 if different
258 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000260{
261 size_t i;
262 unsigned char diff;
263 const unsigned char *n1 = s1, *n2 = s2;
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000266 diff = n1[i] ^ n2[i];
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000269 continue;
270 }
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (diff == 32 &&
273 ((n1[i] >= 'a' && n1[i] <= 'z') ||
274 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
275 continue;
276 }
277
278 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000279 }
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000282}
283
284/*
285 * Return 0 if name matches wildcard, -1 otherwise
286 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000288{
289 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000291
292 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
294 return -1;
295 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 for (i = 0; i < cn_len; ++i) {
298 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000299 cn_idx = i;
300 break;
301 }
302 }
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (cn_idx == 0) {
305 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000306 }
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (cn_len - cn_idx == name->len - 1 &&
309 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
310 return 0;
311 }
312
313 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000314}
315
316/*
317 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
318 * variations (but not all).
319 *
320 * Return 0 if equal, -1 otherwise.
321 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000323{
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000325 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 memcmp(a->p, b->p, b->len) == 0) {
327 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000328 }
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
331 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000332 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 x509_memcasecmp(a->p, b->p, b->len) == 0) {
334 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000335 }
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000338}
339
340/*
341 * Compare two X.509 Names (aka rdnSequence).
342 *
343 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
344 * we sometimes return unequal when the full algorithm would return equal,
345 * but never the other way. (In particular, we don't do Unicode normalisation
346 * or space folding.)
347 *
348 * Return 0 if equal, -1 otherwise.
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000351{
352 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 while (a != NULL || b != NULL) {
354 if (a == NULL || b == NULL) {
355 return -1;
356 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000357
358 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000360 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
362 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000363 }
364
365 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (x509_string_cmp(&a->val, &b->val) != 0) {
367 return -1;
368 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000369
370 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (a->next_merged != b->next_merged) {
372 return -1;
373 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000374
375 a = a->next;
376 b = b->next;
377 }
378
379 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000381}
382
383/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200384 * Reset (init or clear) a verify_chain
385 */
386static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200388{
389 size_t i;
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200392 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000393 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200394 }
395
396 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000397
398#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
399 ver_chain->trust_ca_cb_result = NULL;
400#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200401}
402
403/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
405 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100406static int x509_get_version(unsigned char **p,
407 const unsigned char *end,
408 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409{
Janos Follath865b3eb2019-12-16 11:46:15 +0000410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411 size_t len;
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
414 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
415 0)) != 0) {
416 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 }
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422 }
423
424 end = *p + len;
425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
427 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
428 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (*p != end) {
431 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
432 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
433 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436}
437
438/*
439 * Validity ::= SEQUENCE {
440 * notBefore Time,
441 * notAfter Time }
442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443static int x509_get_dates(unsigned char **p,
444 const unsigned char *end,
445 mbedtls_x509_time *from,
446 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447{
Janos Follath865b3eb2019-12-16 11:46:15 +0000448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449 size_t len;
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
452 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
453 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
454 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 end = *p + len;
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
459 return ret;
460 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
463 return ret;
464 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (*p != end) {
467 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
468 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
469 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472}
473
474/*
475 * X.509 v2/v3 unique identifier (not parsed)
476 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100477static int x509_get_uid(unsigned char **p,
478 const unsigned char *end,
479 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480{
Janos Follath865b3eb2019-12-16 11:46:15 +0000481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (*p == end) {
484 return 0;
485 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486
487 uid->tag = **p;
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
490 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
491 n)) != 0) {
492 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
493 return 0;
494 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 }
498
499 uid->p = *p;
500 *p += uid->len;
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503}
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505static int x509_get_basic_constraints(unsigned char **p,
506 const unsigned char *end,
507 int *ca_istrue,
508 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509{
Janos Follath865b3eb2019-12-16 11:46:15 +0000510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 size_t len;
512
513 /*
514 * BasicConstraints ::= SEQUENCE {
515 * cA BOOLEAN DEFAULT FALSE,
516 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
517 */
518 *ca_istrue = 0; /* DEFAULT FALSE */
519 *max_pathlen = 0; /* endless */
520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
522 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
523 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524 }
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (*p == end) {
527 return 0;
528 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
531 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
532 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
533 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (ret != 0) {
536 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
537 }
538
539 if (*ca_istrue != 0) {
540 *ca_istrue = 1;
541 }
542 }
543
544 if (*p == end) {
545 return 0;
546 }
547
548 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
549 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
550 }
551
552 if (*p != end) {
553 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
554 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
555 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
Andrzej Kurek16050742020-04-14 09:49:52 -0400557 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
558 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (*max_pathlen == INT_MAX) {
560 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
561 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
562 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400563
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564 (*max_pathlen)++;
565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567}
568
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569/*
570 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
571 *
572 * KeyPurposeId ::= OBJECT IDENTIFIER
573 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574static int x509_get_ext_key_usage(unsigned char **p,
575 const unsigned char *end,
576 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577{
Janos Follath865b3eb2019-12-16 11:46:15 +0000578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
581 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
582 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
584 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (ext_key_usage->buf.p == NULL) {
586 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
587 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
588 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591}
592
593/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200594 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
595 *
596 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
597 *
598 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
599 *
600 * PolicyInformation ::= SEQUENCE {
601 * policyIdentifier CertPolicyId,
602 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
603 * PolicyQualifierInfo OPTIONAL }
604 *
605 * CertPolicyId ::= OBJECT IDENTIFIER
606 *
607 * PolicyQualifierInfo ::= SEQUENCE {
608 * policyQualifierId PolicyQualifierId,
609 * qualifier ANY DEFINED BY policyQualifierId }
610 *
611 * -- policyQualifierIds for Internet policy qualifiers
612 *
613 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
614 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
615 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
616 *
617 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
618 *
619 * Qualifier ::= CHOICE {
620 * cPSuri CPSuri,
621 * userNotice UserNotice }
622 *
623 * CPSuri ::= IA5String
624 *
625 * UserNotice ::= SEQUENCE {
626 * noticeRef NoticeReference OPTIONAL,
627 * explicitText DisplayText OPTIONAL }
628 *
629 * NoticeReference ::= SEQUENCE {
630 * organization DisplayText,
631 * noticeNumbers SEQUENCE OF INTEGER }
632 *
633 * DisplayText ::= CHOICE {
634 * ia5String IA5String (SIZE (1..200)),
635 * visibleString VisibleString (SIZE (1..200)),
636 * bmpString BMPString (SIZE (1..200)),
637 * utf8String UTF8String (SIZE (1..200)) }
638 *
639 * NOTE: we only parse and use anyPolicy without qualifiers at this point
640 * as defined in RFC 5280.
641 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100642static int x509_get_certificate_policies(unsigned char **p,
643 const unsigned char *end,
644 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200645{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300646 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200647 size_t len;
648 mbedtls_asn1_buf *buf;
649 mbedtls_asn1_sequence *cur = certificate_policies;
650
651 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 ret = mbedtls_asn1_get_tag(p, end, &len,
653 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
654 if (ret != 0) {
655 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
656 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (*p + len != end) {
659 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
660 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
661 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200662
663 /*
664 * Cannot be an empty sequence.
665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (len == 0) {
667 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
668 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
669 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200672 mbedtls_x509_buf policy_oid;
673 const unsigned char *policy_end;
674
675 /*
676 * Get the policy sequence
677 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
679 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
680 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
681 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200682
683 policy_end = *p + len;
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
686 MBEDTLS_ASN1_OID)) != 0) {
687 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
688 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200689
690 policy_oid.tag = MBEDTLS_ASN1_OID;
691 policy_oid.len = len;
692 policy_oid.p = *p;
693
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300694 /*
695 * Only AnyPolicy is currently supported when enforcing policy.
696 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300698 /*
699 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200700 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300701 */
702 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
703 }
704
Ron Eldor74d9acc2019-03-21 14:00:03 +0200705 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (cur->buf.p != NULL) {
707 if (cur->next != NULL) {
708 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
709 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (cur->next == NULL) {
714 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
715 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
716 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200717
718 cur = cur->next;
719 }
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200722 buf->tag = policy_oid.tag;
723 buf->p = policy_oid.p;
724 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300725
726 *p += len;
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 /*
729 * If there is an optional qualifier, then *p < policy_end
730 * Check the Qualifier len to verify it doesn't exceed policy_end.
731 */
732 if (*p < policy_end) {
733 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
734 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
735 0) {
736 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
737 }
Ron Eldor08063792019-05-13 16:38:39 +0300738 /*
739 * Skip the optional policy qualifiers.
740 */
741 *p += len;
742 }
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (*p != policy_end) {
745 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
746 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
747 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200748 }
749
750 /* Set final sequence entry's next pointer to NULL */
751 cur->next = NULL;
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (*p != end) {
754 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
755 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
756 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200759}
760
761/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 * X.509 v3 extensions
763 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765static int x509_get_crt_ext(unsigned char **p,
766 const unsigned char *end,
767 mbedtls_x509_crt *crt,
768 mbedtls_x509_crt_ext_cb_t cb,
769 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770{
Janos Follath865b3eb2019-12-16 11:46:15 +0000771 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200772 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200773 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (*p == end) {
776 return 0;
777 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
780 return ret;
781 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782
Hanno Becker12f62fb2019-02-12 17:22:36 +0000783 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785 /*
786 * Extension ::= SEQUENCE {
787 * extnID OBJECT IDENTIFIER,
788 * critical BOOLEAN DEFAULT FALSE,
789 * extnValue OCTET STRING }
790 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 int is_critical = 0; /* DEFAULT FALSE */
793 int ext_type = 0;
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
796 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
797 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
798 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200799
800 end_ext_data = *p + len;
801
802 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
804 MBEDTLS_ASN1_OID)) != 0) {
805 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
806 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807
k-stachowiak463928a2018-07-24 12:50:59 +0200808 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200809 extn_oid.p = *p;
810 *p += extn_oid.len;
811
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200812 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
814 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
815 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
816 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817
818 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
820 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
821 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
822 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200824 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825 end_ext_octet = *p + len;
826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (end_ext_octet != end_ext_data) {
828 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
829 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
830 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200831
832 /*
833 * Detect supported extensions
834 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200838 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (cb != NULL) {
840 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
841 if (ret != 0 && is_critical) {
842 return ret;
843 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200844 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200845 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200846 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200847
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848 /* No parser found, skip extension */
849 *p = end_ext_octet;
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
854 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 continue;
857 }
858
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100859 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if ((crt->ext_types & ext_type) != 0) {
861 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
862 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100863
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864 crt->ext_types |= ext_type;
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 switch (ext_type) {
867 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
868 /* Parse basic constraints */
869 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
870 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
871 return ret;
872 }
873 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 case MBEDTLS_X509_EXT_KEY_USAGE:
876 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100877 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
878 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 return ret;
880 }
881 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
884 /* Parse extended key usage */
885 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
886 &crt->ext_key_usage)) != 0) {
887 return ret;
888 }
889 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
892 /* Parse subject alt name */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100893 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
894 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 return ret;
896 }
897 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
900 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100901 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
902 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 return ret;
904 }
905 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
908 /* Parse certificate policies type */
909 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
910 &crt->certificate_policies)) != 0) {
911 /* Give the callback (if any) a chance to handle the extension
912 * if it contains unsupported policies */
913 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
914 cb(p_ctx, crt, &extn_oid, is_critical,
915 start_ext_octet, end_ext_octet) == 0) {
916 break;
917 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if (is_critical) {
920 return ret;
921 } else
922 /*
923 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
924 * cannot interpret or enforce the policy. However, it is up to
925 * the user to choose how to enforce the policies,
926 * unless the extension is critical.
927 */
928 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
929 return ret;
930 }
931 }
932 break;
933
934 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300935 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 * If this is a non-critical extension, which the oid layer
937 * supports, but there isn't an x509 parser for it,
938 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300939 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (is_critical) {
941 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
942 } else {
943 *p = end_ext_octet;
944 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945 }
946 }
947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (*p != end) {
949 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
950 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
951 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954}
955
956/*
957 * Parse and fill a single X.509 certificate in DER format
958 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
960 const unsigned char *buf,
961 size_t buflen,
962 int make_copy,
963 mbedtls_x509_crt_ext_cb_t cb,
964 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965{
Janos Follath865b3eb2019-12-16 11:46:15 +0000966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 size_t len;
968 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
972 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
973 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974
975 /*
976 * Check for valid input
977 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (crt == NULL || buf == NULL) {
979 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
980 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981
Hanno Becker1a65dcd2019-01-31 08:57:44 +0000982 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +0000984 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 end = p + len;
986
987 /*
988 * Certificate ::= SEQUENCE {
989 * tbsCertificate TBSCertificate,
990 * signatureAlgorithm AlgorithmIdentifier,
991 * signatureValue BIT STRING }
992 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
994 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
995 mbedtls_x509_crt_free(crt);
996 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 }
998
Janos Follathcc0e49d2016-02-17 14:34:12 +0000999 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001000 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001002 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1004 if (crt->raw.p == NULL) {
1005 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1006 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001009 crt->own_buffer = 1;
1010
1011 p += crt->raw.len - len;
1012 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 } else {
1014 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001015 crt->own_buffer = 0;
1016 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001017
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 /*
1019 * TBSCertificate ::= SEQUENCE {
1020 */
1021 crt->tbs.p = p;
1022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1024 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1025 mbedtls_x509_crt_free(crt);
1026 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 }
1028
1029 end = p + len;
1030 crt->tbs.len = end - crt->tbs.p;
1031
1032 /*
1033 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1034 *
1035 * CertificateSerialNumber ::= INTEGER
1036 *
1037 * signature AlgorithmIdentifier
1038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1040 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1041 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1042 &sig_params1)) != 0) {
1043 mbedtls_x509_crt_free(crt);
1044 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045 }
1046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (crt->version < 0 || crt->version > 2) {
1048 mbedtls_x509_crt_free(crt);
1049 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 }
1051
Andres AG7ca4a032017-03-09 16:16:11 +00001052 crt->version++;
1053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1055 &crt->sig_md, &crt->sig_pk,
1056 &crt->sig_opts)) != 0) {
1057 mbedtls_x509_crt_free(crt);
1058 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059 }
1060
1061 /*
1062 * issuer Name
1063 */
1064 crt->issuer_raw.p = p;
1065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1067 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1068 mbedtls_x509_crt_free(crt);
1069 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070 }
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1073 mbedtls_x509_crt_free(crt);
1074 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075 }
1076
1077 crt->issuer_raw.len = p - crt->issuer_raw.p;
1078
1079 /*
1080 * Validity ::= SEQUENCE {
1081 * notBefore Time,
1082 * notAfter Time }
1083 *
1084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1086 &crt->valid_to)) != 0) {
1087 mbedtls_x509_crt_free(crt);
1088 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089 }
1090
1091 /*
1092 * subject Name
1093 */
1094 crt->subject_raw.p = p;
1095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1097 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1098 mbedtls_x509_crt_free(crt);
1099 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100 }
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1103 mbedtls_x509_crt_free(crt);
1104 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105 }
1106
1107 crt->subject_raw.len = p - crt->subject_raw.p;
1108
1109 /*
1110 * SubjectPublicKeyInfo
1111 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001112 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1114 mbedtls_x509_crt_free(crt);
1115 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001116 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001117 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118
1119 /*
1120 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1121 * -- If present, version shall be v2 or v3
1122 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1123 * -- If present, version shall be v2 or v3
1124 * extensions [3] EXPLICIT Extensions OPTIONAL
1125 * -- If present, version shall be v3
1126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (crt->version == 2 || crt->version == 3) {
1128 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1129 if (ret != 0) {
1130 mbedtls_x509_crt_free(crt);
1131 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 }
1133 }
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (crt->version == 2 || crt->version == 3) {
1136 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1137 if (ret != 0) {
1138 mbedtls_x509_crt_free(crt);
1139 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 }
1141 }
1142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (crt->version == 3) {
1144 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1145 if (ret != 0) {
1146 mbedtls_x509_crt_free(crt);
1147 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 }
1149 }
1150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (p != end) {
1152 mbedtls_x509_crt_free(crt);
1153 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1154 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 }
1156
1157 end = crt_end;
1158
1159 /*
1160 * }
1161 * -- end of TBSCertificate
1162 *
1163 * signatureAlgorithm AlgorithmIdentifier,
1164 * signatureValue BIT STRING
1165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1167 mbedtls_x509_crt_free(crt);
1168 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 }
1170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (crt->sig_oid.len != sig_oid2.len ||
1172 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001173 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001174 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 (sig_params1.len != 0 &&
1176 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1177 mbedtls_x509_crt_free(crt);
1178 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 }
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1182 mbedtls_x509_crt_free(crt);
1183 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 }
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (p != end) {
1187 mbedtls_x509_crt_free(crt);
1188 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1189 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 }
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001193}
1194
1195/*
1196 * Parse one X.509 certificate in DER format from a buffer and add them to a
1197 * chained list
1198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1200 const unsigned char *buf,
1201 size_t buflen,
1202 int make_copy,
1203 mbedtls_x509_crt_ext_cb_t cb,
1204 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205{
Janos Follath865b3eb2019-12-16 11:46:15 +00001206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208
1209 /*
1210 * Check for valid input
1211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (crt == NULL || buf == NULL) {
1213 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1214 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217 prev = crt;
1218 crt = crt->next;
1219 }
1220
1221 /*
1222 * Add new certificate on the end of the chain if needed.
1223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (crt->version != 0 && crt->next == NULL) {
1225 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 if (crt->next == NULL) {
1228 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1229 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001230
1231 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001234 }
1235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1237 if (ret != 0) {
1238 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 if (crt != chain) {
1243 mbedtls_free(crt);
1244 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 }
1248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001250}
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1253 const unsigned char *buf,
1254 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001255{
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001257}
1258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1260 const unsigned char *buf,
1261 size_t buflen,
1262 int make_copy,
1263 mbedtls_x509_crt_ext_cb_t cb,
1264 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001265{
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001267}
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1270 const unsigned char *buf,
1271 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001272{
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001274}
1275
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001277 * Parse one or more PEM certificates from a buffer and add them to the chained
1278 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1281 const unsigned char *buf,
1282 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283{
Janos Follath98e28a72016-05-31 14:03:54 +01001284#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001285 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001287#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288
1289 /*
1290 * Check for valid input
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (chain == NULL || buf == NULL) {
1293 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1294 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295
1296 /*
1297 * Determine buffer content. Buffer contains either one DER certificate or
1298 * one or more PEM certificates.
1299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1302 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001304 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1307 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1308 }
Janos Follath98e28a72016-05-31 14:03:54 +01001309#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001311#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001318 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001322
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001323 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 ret = mbedtls_pem_read_buffer(&pem,
1325 "-----BEGIN CERTIFICATE-----",
1326 "-----END CERTIFICATE-----",
1327 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001330 /*
1331 * Was PEM encoded
1332 */
1333 buflen -= use_len;
1334 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1336 return ret;
1337 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1338 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001339
1340 /*
1341 * PEM header and footer were found
1342 */
1343 buflen -= use_len;
1344 buf += use_len;
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349
Paul Bakker5a5fa922014-09-26 14:53:04 +02001350 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001353 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361 /*
1362 * Quit parsing on a memory error
1363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1365 return ret;
1366 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371
1372 total_failed++;
1373 continue;
1374 }
1375
1376 success = 1;
1377 }
1378 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 if (success) {
1381 return total_failed;
1382 } else if (first_error) {
1383 return first_error;
1384 } else {
1385 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1386 }
Janos Follath98e28a72016-05-31 14:03:54 +01001387#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001388}
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391/*
1392 * Load one or more certificates and add them to the chained list
1393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395{
Janos Follath865b3eb2019-12-16 11:46:15 +00001396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001397 size_t n;
1398 unsigned char *buf;
1399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1401 return ret;
1402 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 mbedtls_platform_zeroize(buf, n);
1407 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410}
1411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001413{
1414 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001415#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416 int w_ret;
1417 WCHAR szDir[MAX_PATH];
1418 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001419 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421
Paul Bakker9af723c2014-05-01 13:03:14 +02001422 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423 HANDLE hFind;
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (len > MAX_PATH - 3) {
1426 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1427 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 memset(szDir, 0, sizeof(szDir));
1430 memset(filename, 0, MAX_PATH);
1431 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001432 filename[len++] = '\\';
1433 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434 filename[len++] = '*';
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1437 MAX_PATH - 3);
1438 if (w_ret == 0) {
1439 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1440 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 hFind = FindFirstFileW(szDir, &file_data);
1443 if (hFind == INVALID_HANDLE_VALUE) {
1444 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1445 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446
1447 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 do {
1449 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001456 -1,
1457 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 NULL, NULL);
1459 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001460 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1461 goto cleanup;
1462 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1465 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001466 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 }
1470 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475
Ron Eldor36d90422017-01-09 15:09:16 +02001476cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001478#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001479 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001480 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001482 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001483 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (dir == NULL) {
1487 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1488 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489
Ron Eldor63140682017-01-09 19:27:59 +02001490#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1492 closedir(dir);
1493 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001494 }
Ron Eldor63140682017-01-09 19:27:59 +02001495#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001500 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001503 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001504 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1505 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 } else if (stat(entry_name, &sb) == -1) {
1507 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001508 /* Broken symbolic link - ignore this entry.
1509 stat(2) will return this error for either (a) a dangling
1510 symlink or (b) a missing file.
1511 Given that we have just obtained the filename from readdir,
1512 assume that it does exist and therefore treat this as a
1513 dangling symlink. */
1514 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001516 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001517 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1518 goto cleanup;
1519 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520 }
1521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525
1526 // Ignore parse errors
1527 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1529 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001535
1536cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001538
Ron Eldor63140682017-01-09 19:27:59 +02001539#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 }
Ron Eldor63140682017-01-09 19:27:59 +02001543#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001544
Paul Bakkerbe089b02013-10-14 15:51:50 +02001545#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550
Peter Kolbus9a969b62018-12-11 13:55:56 -06001551#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001552static int x509_info_ext_key_usage(char **buf, size_t *size,
1553 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001554{
Janos Follath865b3eb2019-12-16 11:46:15 +00001555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001556 const char *desc;
1557 size_t n = *size;
1558 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001560 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 while (cur != NULL) {
1563 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001564 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001568 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001569
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001570 sep = ", ";
1571
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001572 cur = cur->next;
1573 }
1574
1575 *size = n;
1576 *buf = p;
1577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001579}
1580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581static int x509_info_cert_policies(char **buf, size_t *size,
1582 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001583{
Janos Follath865b3eb2019-12-16 11:46:15 +00001584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001585 const char *desc;
1586 size_t n = *size;
1587 char *p = *buf;
1588 const mbedtls_x509_sequence *cur = certificate_policies;
1589 const char *sep = "";
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 while (cur != NULL) {
1592 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001593 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001597 MBEDTLS_X509_SAFE_SNPRINTF;
1598
1599 sep = ", ";
1600
1601 cur = cur->next;
1602 }
1603
1604 *size = n;
1605 *buf = p;
1606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001608}
1609
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610/*
1611 * Return an informational string about the certificate.
1612 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001613#define BEFORE_COLON 18
1614#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001615int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1616 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617{
Janos Follath865b3eb2019-12-16 11:46:15 +00001618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619 size_t n;
1620 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621 char key_size_str[BEFORE_COLON];
1622
1623 p = buf;
1624 n = size;
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (NULL == crt) {
1627 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001628 MBEDTLS_X509_SAFE_SNPRINTF;
1629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001631 }
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1634 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001635 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1637 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001638 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001641 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001644 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001646 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001649 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001651 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1654 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1655 crt->valid_from.year, crt->valid_from.mon,
1656 crt->valid_from.day, crt->valid_from.hour,
1657 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001658 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1661 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1662 crt->valid_to.year, crt->valid_to.mon,
1663 crt->valid_to.day, crt->valid_to.hour,
1664 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001665 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001666
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001668 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1671 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001672 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001674 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1676 mbedtls_pk_get_name(&crt->pk))) != 0) {
1677 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 }
1679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1681 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001682 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001684 /*
1685 * Optional extensions
1686 */
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1689 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1690 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001691 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 if (crt->max_pathlen > 0) {
1694 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001695 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001696 }
1697 }
1698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1700 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001701 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001702
Przemek Stekiel21c37282023-01-16 08:47:49 +01001703 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1704 &crt->subject_alt_names,
1705 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 return ret;
1707 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001708 }
1709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1711 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001712 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001713
Przemek Stekiel21c37282023-01-16 08:47:49 +01001714 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 return ret;
1716 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001717 }
1718
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1720 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001721 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001722
Przemek Stekiel21c37282023-01-16 08:47:49 +01001723 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 return ret;
1725 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001726 }
1727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1729 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001730 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if ((ret = x509_info_ext_key_usage(&p, &n,
1733 &crt->ext_key_usage)) != 0) {
1734 return ret;
1735 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001736 }
1737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1739 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001740 MBEDTLS_X509_SAFE_SNPRINTF;
1741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 if ((ret = x509_info_cert_policies(&p, &n,
1743 &crt->certificate_policies)) != 0) {
1744 return ret;
1745 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001746 }
1747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001749 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752}
1753
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001754struct x509_crt_verify_string {
1755 int code;
1756 const char *string;
1757};
1758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001760static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001761 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001762 { 0, NULL }
1763};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001764#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1767 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001768{
Janos Follath865b3eb2019-12-16 11:46:15 +00001769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001770 const struct x509_crt_verify_string *cur;
1771 char *p = buf;
1772 size_t n = size;
1773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1775 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001776 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001780 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001781 flags ^= cur->code;
1782 }
1783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if (flags != 0) {
1785 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1786 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001787 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001788 }
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001791}
Hanno Becker612a2f12020-10-09 09:19:39 +01001792#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001793
Gilles Peskine449bd832023-01-11 14:50:10 +01001794int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1795 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001796{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001797 unsigned int usage_must, usage_may;
1798 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1802 return 0;
1803 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001804
1805 usage_must = usage & ~may_mask;
1806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1808 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1809 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001810
1811 usage_may = usage & may_mask;
1812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1814 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1815 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001818}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1821 const char *usage_oid,
1822 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001823{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001825
1826 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1828 return 0;
1829 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001830
1831 /*
1832 * Look for the requested usage (or wildcard ANY) in our list
1833 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 if (cur_oid->len == usage_len &&
1838 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1839 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001840 }
1841
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1843 return 0;
1844 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001845 }
1846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001848}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001851/*
1852 * Return 1 if the certificate is revoked, or 0 otherwise.
1853 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001854int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 while (cur != NULL && cur->serial.len != 0) {
1859 if (crt->serial.len == cur->serial.len &&
1860 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1861 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001862 }
1863
1864 cur = cur->next;
1865 }
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001868}
1869
1870/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01001871 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02001872 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001873 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001874static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
1875 mbedtls_x509_crl *crl_list,
1876 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001877{
1878 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02001879 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01001880#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01001881 psa_algorithm_t psa_algorithm;
1882#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01001884#endif /* MBEDTLS_USE_PSA_CRYPTO */
1885 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 if (ca == NULL) {
1888 return flags;
1889 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001890
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 while (crl_list != NULL) {
1892 if (crl_list->version == 0 ||
1893 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001894 crl_list = crl_list->next;
1895 continue;
1896 }
1897
1898 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001899 * Check if the CA is configured to sign CRLs
1900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 if (mbedtls_x509_crt_check_key_usage(ca,
1902 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001904 break;
1905 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001906
1907 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001908 * Check if CRL is correctly signed by the trusted CA
1909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001911 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001915 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001917
pespacek7599a772022-02-07 14:40:55 +01001918#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
1920 if (psa_hash_compute(psa_algorithm,
1921 crl_list->tbs.p,
1922 crl_list->tbs.len,
1923 hash,
1924 sizeof(hash),
1925 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01001926 /* Note: this can't happen except after an internal error */
1927 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
1928 break;
1929 }
pespacek7599a772022-02-07 14:40:55 +01001930#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
1932 hash_length = mbedtls_md_get_size(md_info);
1933 if (mbedtls_md(md_info,
1934 crl_list->tbs.p,
1935 crl_list->tbs.len,
1936 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02001937 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001939 break;
1940 }
pespaceka7a64692022-02-14 15:18:43 +01001941#endif /* MBEDTLS_USE_PSA_CRYPTO */
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001944 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1948 crl_list->sig_md, hash, hash_length,
1949 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951 break;
1952 }
1953
1954 /*
1955 * Check for validity of CRL (Do not drop out)
1956 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001962 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001964
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001965 /*
1966 * Check if certificate is revoked
1967 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001970 break;
1971 }
1972
1973 crl_list = crl_list->next;
1974 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001979
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001980/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001981 * Check the signature of a certificate by its parent
1982 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001983static int x509_crt_check_signature(const mbedtls_x509_crt *child,
1984 mbedtls_x509_crt *parent,
1985 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02001986{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04001987 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02001988 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04001989#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1990 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 md_info = mbedtls_md_info_from_type(child->sig_md);
1992 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05001993
Andrzej Kurekd4a65532018-10-31 06:18:39 -04001994 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
1996 return -1;
1997 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04001998#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 status = psa_hash_compute(hash_alg,
2003 child->tbs.p,
2004 child->tbs.len,
2005 hash,
2006 sizeof(hash),
2007 &hash_len);
2008 if (status != PSA_SUCCESS) {
2009 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002010 }
2011
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002012#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002013 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2015 return -1;
2016 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002017
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002018#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2020 return mbedtls_pk_verify_restartable(&parent->pk,
2021 child->sig_md, hash, hash_len,
2022 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002023 }
2024#else
2025 (void) rs_ctx;
2026#endif
2027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2029 child->sig_md, hash, hash_len,
2030 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002031}
2032
2033/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002034 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2035 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002036 *
2037 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2040 const mbedtls_x509_crt *parent,
2041 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002042{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002043 int need_ca_bit;
2044
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002045 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2047 return -1;
2048 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002049
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002050 /* Parent must have the basicConstraints CA bit set as a general rule */
2051 need_ca_bit = 1;
2052
2053 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002055 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002056 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (need_ca_bit && !parent->ca_istrue) {
2059 return -1;
2060 }
2061
2062 if (need_ca_bit &&
2063 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2064 return -1;
2065 }
2066
2067 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002068}
2069
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002070/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002071 * Find a suitable parent for child in candidates, or return NULL.
2072 *
2073 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002074 * 1. subject name matches child's issuer
2075 * 2. if necessary, the CA bit is set and key usage allows signing certs
2076 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002077 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002078 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002079 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002080 * If there's a suitable candidate which is also time-valid, return the first
2081 * such. Otherwise, return the first suitable candidate (or NULL if there is
2082 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002083 *
2084 * The rationale for this rule is that someone could have a list of trusted
2085 * roots with two versions on the same root with different validity periods.
2086 * (At least one user reported having such a list and wanted it to just work.)
2087 * The reason we don't just require time-validity is that generally there is
2088 * only one version, and if it's expired we want the flags to state that
2089 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002090 *
2091 * The rationale for rule 3 (signature for trusted roots) is that users might
2092 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002093 * way we select the correct one is by checking the signature (as we don't
2094 * rely on key identifier extensions). (This is one way users might choose to
2095 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002096 *
2097 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002098 * - [in] child: certificate for which we're looking for a parent
2099 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002100 * - [out] r_parent: parent found (or NULL)
2101 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002102 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2103 * of the chain, 0 otherwise
2104 * - [in] path_cnt: number of intermediates seen so far
2105 * - [in] self_cnt: number of self-signed intermediates seen so far
2106 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002107 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002108 *
2109 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002110 * - 0 on success
2111 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002112 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002113static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 mbedtls_x509_crt *child,
2115 mbedtls_x509_crt *candidates,
2116 mbedtls_x509_crt **r_parent,
2117 int *r_signature_is_good,
2118 int top,
2119 unsigned path_cnt,
2120 unsigned self_cnt,
2121 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002122{
Janos Follath865b3eb2019-12-16 11:46:15 +00002123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002124 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002125 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002126
2127#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002128 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002130 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002131 parent = rs_ctx->parent;
2132 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002133 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002134
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002135 /* clear saved state */
2136 rs_ctx->parent = NULL;
2137 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002138 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002139
2140 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002141 goto check_signature;
2142 }
2143#endif
2144
2145 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002146 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002149 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002151 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002153
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002154 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (parent->max_pathlen > 0 &&
2156 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002157 continue;
2158 }
2159
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002160 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002161#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2162check_signature:
2163#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002165
2166#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002168 /* save state */
2169 rs_ctx->parent = parent;
2170 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002171 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002174 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002175#else
2176 (void) ret;
2177#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002178
2179 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002181 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002183
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002184 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2186 mbedtls_x509_time_is_future(&parent->valid_from)) {
2187 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002188 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002189 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002190 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002191
2192 continue;
2193 }
2194
Andy Gross1f627142019-01-30 10:25:53 -06002195 *r_parent = parent;
2196 *r_signature_is_good = signature_is_good;
2197
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002198 break;
2199 }
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002202 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002203 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002204 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002207}
2208
2209/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002210 * Find a parent in trusted CAs or the provided chain, or return NULL.
2211 *
2212 * Searches in trusted CAs first, and return the first suitable parent found
2213 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002214 *
2215 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002216 * - [in] child: certificate for which we're looking for a parent, followed
2217 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002218 * - [in] trust_ca: list of locally trusted certificates
2219 * - [out] parent: parent found (or NULL)
2220 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2221 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2222 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2223 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002224 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002225 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002226 *
2227 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002228 * - 0 on success
2229 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002230 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002231static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 mbedtls_x509_crt *child,
2233 mbedtls_x509_crt *trust_ca,
2234 mbedtls_x509_crt **parent,
2235 int *parent_is_trusted,
2236 int *signature_is_good,
2237 unsigned path_cnt,
2238 unsigned self_cnt,
2239 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002240{
Janos Follath865b3eb2019-12-16 11:46:15 +00002241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002242 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002243
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002244 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002245
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002246#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002247 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002249 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002250 rs_ctx->parent_is_trusted = -1;
2251 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002252#endif
2253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002255 search_list = *parent_is_trusted ? trust_ca : child->next;
2256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 ret = x509_crt_find_parent_in(child, search_list,
2258 parent, signature_is_good,
2259 *parent_is_trusted,
2260 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002261
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002262#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002264 /* save state */
2265 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002267 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002268#else
2269 (void) ret;
2270#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002271
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002272 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002274 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002276
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002277 /* prepare second iteration */
2278 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002279 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002280
2281 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002283 *parent_is_trusted = 0;
2284 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002285 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002288}
2289
2290/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002291 * Check if an end-entity certificate is locally trusted
2292 *
2293 * Currently we require such certificates to be self-signed (actually only
2294 * check for self-issued as self-signatures are not checked)
2295 */
2296static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 mbedtls_x509_crt *crt,
2298 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002299{
2300 mbedtls_x509_crt *cur;
2301
2302 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2304 return -1;
2305 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002306
2307 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2309 if (crt->raw.len == cur->raw.len &&
2310 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2311 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002312 }
2313 }
2314
2315 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002317}
2318
2319/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002320 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002321 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002322 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2323 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002324 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002325 * such that every cert in the chain is a child of the next one,
2326 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002327 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002328 * Verify that chain and return it with flags for all issues found.
2329 *
2330 * Special cases:
2331 * - EE == Rj -> return a one-element list containing it
2332 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2333 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002334 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002335 * Tests for (aspects of) this function should include at least:
2336 * - trusted EE
2337 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002338 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002339 * - if relevant: EE untrusted
2340 * - if relevant: EE -> intermediate, untrusted
2341 * with the aspect under test checked at each relevant level (EE, int, root).
2342 * For some aspects longer chains are required, but usually length 2 is
2343 * enough (but length 1 is not in general).
2344 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002345 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002346 * - [in] crt: the cert list EE, C1, ..., Cn
2347 * - [in] trust_ca: the trusted list R1, ..., Rp
2348 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002349 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002350 * Only valid when return value is 0, may contain garbage otherwise!
2351 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002352 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002353 *
2354 * Return value:
2355 * - non-zero if the chain could not be fully built and examined
2356 * - 0 is the chain was successfully built and examined,
2357 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002358 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002359static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 mbedtls_x509_crt *crt,
2361 mbedtls_x509_crt *trust_ca,
2362 mbedtls_x509_crl *ca_crl,
2363 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2364 void *p_ca_cb,
2365 const mbedtls_x509_crt_profile *profile,
2366 mbedtls_x509_crt_verify_chain *ver_chain,
2367 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002368{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002369 /* Don't initialize any of those variables here, so that the compiler can
2370 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002372 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002373 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002374 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002375 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002376 int parent_is_trusted;
2377 int child_is_trusted;
2378 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002379 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002380 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002381
2382#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2383 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002385 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002386 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002387 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002388
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002389 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002390 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002391 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002392 flags = &cur->flags;
2393
2394 goto find_parent;
2395 }
2396#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002397
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002398 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002399 self_cnt = 0;
2400 parent_is_trusted = 0;
2401 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002404 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002405 cur = &ver_chain->items[ver_chain->len];
2406 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002407 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002408 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002409 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002410
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002411 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002413 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002417 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002419
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002420 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if (child_is_trusted) {
2422 return 0;
2423 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002424
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002425 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002427 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002431 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002433
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002434 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (ver_chain->len == 1 &&
2436 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2437 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002438 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002439
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002440#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2441find_parent:
2442#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002443
2444 /* Obtain list of potential trusted signers from CA callback,
2445 * or use statically provided list. */
2446#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (f_ca_cb != NULL) {
2448 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2449 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002450 ver_chain->trust_ca_cb_result = NULL;
2451
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2453 if (ret != 0) {
2454 return MBEDTLS_ERR_X509_FATAL_ERROR;
2455 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002456
2457 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002459#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2460 {
2461 ((void) f_ca_cb);
2462 ((void) p_ca_cb);
2463 cur_trust_ca = trust_ca;
2464 }
2465
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002466 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2468 &parent_is_trusted, &signature_is_good,
2469 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002470
2471#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002473 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002474 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002475 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002476 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002479 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002480#else
2481 (void) ret;
2482#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002483
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002484 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002486 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002488 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002489
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002490 /* Count intermediate self-issued (not necessarily self-signed) certs.
2491 * These can occur with some strategies for key rollover, see [SIRO],
2492 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (ver_chain->len != 1 &&
2494 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002495 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002496 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002497
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002498 /* path_cnt is 0 for the first intermediate CA,
2499 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 if (!parent_is_trusted &&
2501 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002502 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002504 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002505
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002506 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002508 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002510
2511 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002513 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002517 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002519#else
2520 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002521#endif
2522
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002523 /* prepare for next iteration */
2524 child = parent;
2525 parent = NULL;
2526 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002527 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002528 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002529}
2530
Glenn Straussc26bd762022-10-23 19:48:18 -04002531#ifdef _WIN32
2532#ifdef _MSC_VER
2533#pragma comment(lib, "ws2_32.lib")
2534#include <winsock2.h>
2535#include <ws2tcpip.h>
2536#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2537#include <winsock2.h>
2538#include <ws2tcpip.h>
2539#endif
2540#elif defined(__sun)
2541/* Solaris requires -lsocket -lnsl for inet_pton() */
2542#elif defined(__has_include)
2543#if __has_include(<sys/socket.h>)
2544#include <sys/socket.h>
2545#endif
2546#if __has_include(<arpa/inet.h>)
2547#include <arpa/inet.h>
2548#endif
2549#endif
2550
2551/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2552 * the platform inet_pton() or a local implementation (below). The local
2553 * implementation may be used even in cases where the platform provides
2554 * inet_pton(), e.g. when there are different includes required and/or the
2555 * platform implementation requires dependencies on additional libraries.
2556 * Specifically, Windows requires custom includes and additional link
2557 * dependencies, and Solaris requires additional link dependencies.
2558 * Also, as a coarse heuristic, use the local implementation if the compiler
2559 * does not support __has_include(), or if the definition of AF_INET6 is not
2560 * provided by headers included (or not) via __has_include() above. */
2561#ifndef AF_INET6
2562
2563#define x509_cn_inet_pton(cn, dst) (0)
2564
2565#else
2566
2567static int x509_inet_pton_ipv6(const char *src, void *dst)
2568{
2569 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2570}
2571
2572static int x509_inet_pton_ipv4(const char *src, void *dst)
2573{
2574 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2575}
2576
2577#endif /* AF_INET6 */
2578
2579static size_t x509_cn_inet_pton(const char *cn, void *dst)
2580{
2581 return strchr(cn, ':') == NULL
2582 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2583 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2584}
2585
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002586/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002587 * Check for CN match
2588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002589static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2590 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002591{
2592 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (name->len == cn_len &&
2594 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2595 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002596 }
2597
2598 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (x509_check_wildcard(cn, name) == 0) {
2600 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002601 }
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002604}
2605
Glenn Straussc26bd762022-10-23 19:48:18 -04002606static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2607 const char *cn, size_t cn_len)
2608{
2609 uint32_t ip[4];
2610 cn_len = x509_cn_inet_pton(cn, ip);
2611 if (cn_len == 0) {
2612 return -1;
2613 }
2614
2615 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2616 const unsigned char san_type = (unsigned char) cur->buf.tag &
2617 MBEDTLS_ASN1_TAG_VALUE_MASK;
2618 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2619 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2620 return 0;
2621 }
2622 }
2623
2624 return -1;
2625}
2626
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002627/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002628 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2629 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002630static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002632{
Glenn Straussc26bd762022-10-23 19:48:18 -04002633 int san_ip = 0;
2634 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2635 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
2636 case MBEDTLS_X509_SAN_DNS_NAME: /* dNSName */
2637 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2638 return 0;
2639 }
2640 break;
2641 case MBEDTLS_X509_SAN_IP_ADDRESS: /* iPAddress */
2642 san_ip = 1;
2643 break;
2644 /* (We may handle other types here later.) */
2645 default: /* Unrecognized type */
2646 break;
2647 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002649
Glenn Straussc26bd762022-10-23 19:48:18 -04002650 return san_ip ? x509_crt_check_san_ip(san, cn, cn_len) : -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002651}
2652
2653/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002654 * Verify the requested CN - only call this if cn is not NULL!
2655 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002656static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2657 const char *cn,
2658 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002659{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002660 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002662
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002664 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2665 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 }
2667 } else {
2668 for (name = &crt->subject; name != NULL; name = name->next) {
2669 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2670 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002671 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 }
2673 }
2674
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002675 }
Glenn Straussc26bd762022-10-23 19:48:18 -04002676
2677 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002678}
2679
2680/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002681 * Merge the flags for all certs in the chain, after calling callback
2682 */
2683static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 uint32_t *flags,
2685 const mbedtls_x509_crt_verify_chain *ver_chain,
2686 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2687 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002688{
Janos Follath865b3eb2019-12-16 11:46:15 +00002689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002690 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002691 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002692 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002695 cur = &ver_chain->items[i-1];
2696 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (NULL != f_vrfy) {
2699 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
2700 return ret;
2701 }
2702 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002703
2704 *flags |= cur_flags;
2705 }
2706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002708}
2709
2710/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002711 * Verify the certificate validity, with profile, restartable version
2712 *
2713 * This function:
2714 * - checks the requested CN (if any)
2715 * - checks the type and size of the EE cert's key,
2716 * as that isn't done as part of chain building/verification currently
2717 * - builds and verifies the chain
2718 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00002719 *
2720 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
2721 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
2722 * verification routine to search for trusted signers, and CRLs will
2723 * be disabled. Otherwise, `trust_ca` will be used as the static list
2724 * of trusted signers, and `ca_crl` will be use as the static list
2725 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002726 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002727static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
2728 mbedtls_x509_crt *trust_ca,
2729 mbedtls_x509_crl *ca_crl,
2730 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2731 void *p_ca_cb,
2732 const mbedtls_x509_crt_profile *profile,
2733 const char *cn, uint32_t *flags,
2734 int (*f_vrfy)(void *,
2735 mbedtls_x509_crt *,
2736 int,
2737 uint32_t *),
2738 void *p_vrfy,
2739 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002740{
Janos Follath865b3eb2019-12-16 11:46:15 +00002741 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002742 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002743 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002744 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002745
2746 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002747 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002751 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2752 goto exit;
2753 }
2754
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002755 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 if (cn != NULL) {
2757 x509_crt_verify_name(crt, cn, &ee_flags);
2758 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002759
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002760 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002764 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002768 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002770
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002771 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
2773 f_ca_cb, p_ca_cb, profile,
2774 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002777 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02002779
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002780 /* Merge end-entity flags */
2781 ver_chain.items[0].flags |= ee_flags;
2782
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002783 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002785
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002786exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00002787
2788#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
2790 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002791 ver_chain.trust_ca_cb_result = NULL;
2792#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2793
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002794#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
2796 mbedtls_x509_crt_restart_free(rs_ctx);
2797 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002798#endif
2799
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02002800 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2801 * the SSL module for authmode optional, but non-zero return from the
2802 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02002804 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02002805 }
2806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 if (ret != 0) {
2808 *flags = (uint32_t) -1;
2809 return ret;
2810 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (*flags != 0) {
2813 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
2814 }
2815
2816 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002817}
2818
Hanno Becker3116fb32019-03-28 13:34:42 +00002819
2820/*
2821 * Verify the certificate validity (default profile, not restartable)
2822 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002823int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
2824 mbedtls_x509_crt *trust_ca,
2825 mbedtls_x509_crl *ca_crl,
2826 const char *cn, uint32_t *flags,
2827 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2828 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002829{
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2831 NULL, NULL,
2832 &mbedtls_x509_crt_profile_default,
2833 cn, flags,
2834 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002835}
2836
2837/*
2838 * Verify the certificate validity (user-chosen profile, not restartable)
2839 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002840int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
2841 mbedtls_x509_crt *trust_ca,
2842 mbedtls_x509_crl *ca_crl,
2843 const mbedtls_x509_crt_profile *profile,
2844 const char *cn, uint32_t *flags,
2845 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2846 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002847{
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2849 NULL, NULL,
2850 profile, cn, flags,
2851 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002852}
2853
2854#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2855/*
2856 * Verify the certificate validity (user-chosen profile, CA callback,
2857 * not restartable).
2858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002859int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
2860 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2861 void *p_ca_cb,
2862 const mbedtls_x509_crt_profile *profile,
2863 const char *cn, uint32_t *flags,
2864 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2865 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00002866{
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
2868 f_ca_cb, p_ca_cb,
2869 profile, cn, flags,
2870 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00002871}
2872#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
2875 mbedtls_x509_crt *trust_ca,
2876 mbedtls_x509_crl *ca_crl,
2877 const mbedtls_x509_crt_profile *profile,
2878 const char *cn, uint32_t *flags,
2879 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2880 void *p_vrfy,
2881 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00002882{
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
2884 NULL, NULL,
2885 profile, cn, flags,
2886 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00002887}
2888
2889
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002890/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002891 * Initialize a certificate chain
2892 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02002894{
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02002896}
2897
2898/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002899 * Unallocate all certificate data
2900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002902{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 mbedtls_x509_crt *cert_cur = crt;
2904 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 while (cert_cur != NULL) {
2907 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002911#endif
2912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
2914 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
2915 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
2916 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
2917 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002918
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
2920 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
2921 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002922 }
2923
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002924 cert_prv = cert_cur;
2925 cert_cur = cert_cur->next;
2926
Gilles Peskine449bd832023-01-11 14:50:10 +01002927 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
2928 if (cert_prv != crt) {
2929 mbedtls_free(cert_prv);
2930 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002931 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002932}
2933
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002934#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2935/*
2936 * Initialize a restart context
2937 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002938void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002939{
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002941
2942 ctx->parent = NULL;
2943 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002944 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002945
2946 ctx->parent_is_trusted = -1;
2947
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002948 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002949 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002951}
2952
2953/*
2954 * Free the components of a restart context
2955 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002956void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002957{
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002959 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 mbedtls_pk_restart_free(&ctx->pk);
2963 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002964}
2965#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
2966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#endif /* MBEDTLS_X509_CRT_PARSE_C */