blob: abba05b8456a1cfbb5082505b9d3ad851098f787 [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"
Glenn Strauss6f545ac2022-10-25 15:02:14 -040052#include "x509_invasive.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040053
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010058#endif
59
Daniel Axtensf0710242020-05-28 11:43:41 +100060#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010061#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Glenn Straussc26bd762022-10-23 19:48:18 -040062#define WIN32_LEAN_AND_MEAN
63#ifndef _WIN32_WINNT
64#define _WIN32_WINNT 0x0600
65#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#include <windows.h>
67#else
68#include <time.h>
69#endif
Daniel Axtensf0710242020-05-28 11:43:41 +100070#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020077#if defined(__MBED__)
78#include <platform/mbed_retarget.h>
79#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020081#endif /* __MBED__ */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -060082#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000083#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084#endif
85
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020086/*
87 * Item in a verification chain: cert and flags for it
88 */
89typedef struct {
90 mbedtls_x509_crt *crt;
91 uint32_t flags;
92} x509_crt_verify_chain_item;
93
94/*
95 * Max size of verification chain: end-entity + intermediates + trusted root
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097#define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
Paul Bakker34617722014-06-13 17:20:13 +020098
Gilles Peskineffb92da2021-06-02 00:03:26 +020099/* Default profile. Do not remove items unless there are serious security
100 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
102{
Gilles Peskineae270bf2021-06-02 00:05:29 +0200103 /* Hashes from SHA-256 and above. Note that this selection
104 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
106 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
107 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200108 0xFFFFFFF, /* Any PK alg */
Valerio Settid4a5d462023-04-05 18:19:01 +0200109#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200110 /* Curves at or above 128-bit security level. Note that this selection
111 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
113 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
114 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
115 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
116 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
117 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
Gilles Peskine39957502021-06-17 23:17:52 +0200118 0,
Valerio Settid4a5d462023-04-05 18:19:01 +0200119#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200120 0,
Valerio Settid4a5d462023-04-05 18:19:01 +0200121#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200122 2048,
123};
124
Gilles Peskineffb92da2021-06-02 00:03:26 +0200125/* Next-generation profile. Currently identical to the default, but may
126 * be tightened at any time. */
127const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200128{
129 /* Hashes from SHA-256 and above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
131 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
132 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200133 0xFFFFFFF, /* Any PK alg */
134#if defined(MBEDTLS_ECP_C)
135 /* Curves at or above 128-bit security level. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
138 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
139 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
140 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
141 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
142 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200143#else
144 0,
145#endif
146 2048,
147};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200148
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200149/*
150 * NSA Suite B Profile
151 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200152const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
153{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200154 /* Only SHA-256 and 384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
156 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200157 /* Only ECDSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
159 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
Valerio Settid4a5d462023-04-05 18:19:01 +0200160#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200161 /* Only NIST P-256 and P-384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
163 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
Valerio Settid4a5d462023-04-05 18:19:01 +0200164#else /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200165 0,
Valerio Settid4a5d462023-04-05 18:19:01 +0200166#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200167 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200168};
169
170/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200171 * Empty / all-forbidden profile
172 */
173const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
174{
175 0,
176 0,
177 0,
178 (uint32_t) -1,
179};
180
181/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200183 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
186 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187{
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (md_alg == MBEDTLS_MD_NONE) {
189 return -1;
190 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
193 return 0;
194 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200197}
198
199/*
200 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200201 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
204 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200205{
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if (pk_alg == MBEDTLS_PK_NONE) {
207 return -1;
208 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
211 return 0;
212 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200215}
216
217/*
218 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200219 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
222 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200223{
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200225
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
228 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
229 return 0;
230 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200233 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200234#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200235
Valerio Settid4a5d462023-04-05 18:19:01 +0200236#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (pk_alg == MBEDTLS_PK_ECDSA ||
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200238 pk_alg == MBEDTLS_PK_ECKEY ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 pk_alg == MBEDTLS_PK_ECKEY_DH) {
240 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (gid == MBEDTLS_ECP_DP_NONE) {
243 return -1;
244 }
Philippe Antoineb5b25432018-05-11 11:06:29 +0200245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
247 return 0;
248 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200251 }
Valerio Settid4a5d462023-04-05 18:19:01 +0200252#endif /* MBEDTLS_ECP_LIGHT */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200255}
256
257/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000258 * Like memcmp, but case-insensitive and always returns -1 if different
259 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000261{
262 size_t i;
263 unsigned char diff;
264 const unsigned char *n1 = s1, *n2 = s2;
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000267 diff = n1[i] ^ n2[i];
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (diff == 0) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000270 continue;
271 }
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (diff == 32 &&
274 ((n1[i] >= 'a' && n1[i] <= 'z') ||
275 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
276 continue;
277 }
278
279 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000280 }
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000283}
284
285/*
286 * Return 0 if name matches wildcard, -1 otherwise
287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000289{
290 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000292
293 /* We can't have a match if there is no wildcard to match */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
295 return -1;
296 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 for (i = 0; i < cn_len; ++i) {
299 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000300 cn_idx = i;
301 break;
302 }
303 }
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (cn_idx == 0) {
306 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000307 }
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (cn_len - cn_idx == name->len - 1 &&
310 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
311 return 0;
312 }
313
314 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000315}
316
317/*
318 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
319 * variations (but not all).
320 *
321 * Return 0 if equal, -1 otherwise.
322 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000324{
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (a->tag == b->tag &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000326 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 memcmp(a->p, b->p, b->len) == 0) {
328 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000329 }
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
332 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
Hanno Becker1f8527f2018-11-02 09:19:16 +0000333 a->len == b->len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 x509_memcasecmp(a->p, b->p, b->len) == 0) {
335 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000336 }
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000339}
340
341/*
342 * Compare two X.509 Names (aka rdnSequence).
343 *
344 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
345 * we sometimes return unequal when the full algorithm would return equal,
346 * but never the other way. (In particular, we don't do Unicode normalisation
347 * or space folding.)
348 *
349 * Return 0 if equal, -1 otherwise.
350 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000352{
353 /* Avoid recursion, it might not be optimised by the compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 while (a != NULL || b != NULL) {
355 if (a == NULL || b == NULL) {
356 return -1;
357 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000358
359 /* type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (a->oid.tag != b->oid.tag ||
Hanno Becker1f8527f2018-11-02 09:19:16 +0000361 a->oid.len != b->oid.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
363 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000364 }
365
366 /* value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (x509_string_cmp(&a->val, &b->val) != 0) {
368 return -1;
369 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000370
371 /* structure of the list of sets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (a->next_merged != b->next_merged) {
373 return -1;
374 }
Hanno Becker1f8527f2018-11-02 09:19:16 +0000375
376 a = a->next;
377 b = b->next;
378 }
379
380 /* a == NULL == b */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000382}
383
384/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200385 * Reset (init or clear) a verify_chain
386 */
387static void x509_crt_verify_chain_reset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200389{
390 size_t i;
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200393 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000394 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200395 }
396
397 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000398
399#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
400 ver_chain->trust_ca_cb_result = NULL;
401#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200402}
403
404/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
406 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407static int x509_get_version(unsigned char **p,
408 const unsigned char *end,
409 int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410{
Janos Follath865b3eb2019-12-16 11:46:15 +0000411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 size_t len;
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
415 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
416 0)) != 0) {
417 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 *ver = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420 }
421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 }
424
425 end = *p + len;
426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
428 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
429 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (*p != end) {
432 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
433 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
434 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437}
438
439/*
440 * Validity ::= SEQUENCE {
441 * notBefore Time,
442 * notAfter Time }
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444static int x509_get_dates(unsigned char **p,
445 const unsigned char *end,
446 mbedtls_x509_time *from,
447 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448{
Janos Follath865b3eb2019-12-16 11:46:15 +0000449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450 size_t len;
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
453 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
454 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
455 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
457 end = *p + len;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
460 return ret;
461 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
464 return ret;
465 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (*p != end) {
468 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
469 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
470 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473}
474
475/*
476 * X.509 v2/v3 unique identifier (not parsed)
477 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478static int x509_get_uid(unsigned char **p,
479 const unsigned char *end,
480 mbedtls_x509_buf *uid, int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481{
Janos Follath865b3eb2019-12-16 11:46:15 +0000482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (*p == end) {
485 return 0;
486 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487
488 uid->tag = **p;
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
491 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
492 n)) != 0) {
493 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
494 return 0;
495 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 }
499
500 uid->p = *p;
501 *p += uid->len;
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504}
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506static int x509_get_basic_constraints(unsigned char **p,
507 const unsigned char *end,
508 int *ca_istrue,
509 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510{
Janos Follath865b3eb2019-12-16 11:46:15 +0000511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512 size_t len;
513
514 /*
515 * BasicConstraints ::= SEQUENCE {
516 * cA BOOLEAN DEFAULT FALSE,
517 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
518 */
519 *ca_istrue = 0; /* DEFAULT FALSE */
520 *max_pathlen = 0; /* endless */
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
523 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
524 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 }
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (*p == end) {
528 return 0;
529 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
532 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
533 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
534 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 if (ret != 0) {
537 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
538 }
539
540 if (*ca_istrue != 0) {
541 *ca_istrue = 1;
542 }
543 }
544
545 if (*p == end) {
546 return 0;
547 }
548
549 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
550 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
551 }
552
553 if (*p != end) {
554 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
555 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
556 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
Andrzej Kurek16050742020-04-14 09:49:52 -0400558 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
559 * overflow, which is an undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (*max_pathlen == INT_MAX) {
561 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
562 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
563 }
Andrzej Kurek16050742020-04-14 09:49:52 -0400564
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565 (*max_pathlen)++;
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568}
569
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570/*
571 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
572 *
573 * KeyPurposeId ::= OBJECT IDENTIFIER
574 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100575static int x509_get_ext_key_usage(unsigned char **p,
576 const unsigned char *end,
577 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578{
Janos Follath865b3eb2019-12-16 11:46:15 +0000579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
582 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
583 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
585 /* Sequence length must be >= 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ext_key_usage->buf.p == NULL) {
587 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
588 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
589 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592}
593
594/*
toth92ga41954d2021-02-12 16:11:17 +0100595 * SubjectKeyIdentifier ::= KeyIdentifier
596 *
597 * KeyIdentifier ::= OCTET STRING
598 */
599static int x509_get_subject_key_id(unsigned char **p,
600 const unsigned char *end,
601 mbedtls_x509_buf *subject_key_id)
602{
603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
604 size_t len = 0u;
605
606 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
607 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100608 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92ga41954d2021-02-12 16:11:17 +0100609 }
610
Przemek Stekiel61aed062023-05-08 11:14:36 +0200611 subject_key_id->len = len;
612 subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
613 subject_key_id->p = *p;
614 *p += len;
615
toth92gd96027a2021-04-27 15:41:25 +0200616 if (*p != end) {
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100617 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
618 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
toth92gd96027a2021-04-27 15:41:25 +0200619 }
620
toth92ga41954d2021-02-12 16:11:17 +0100621 return 0;
622}
623
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100624/*
toth92g8d435a02021-05-10 15:16:33 +0200625 * AuthorityKeyIdentifier ::= SEQUENCE {
626 * keyIdentifier [0] KeyIdentifier OPTIONAL,
627 * authorityCertIssuer [1] GeneralNames OPTIONAL,
628 * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
629 *
630 * KeyIdentifier ::= OCTET STRING
631 */
632static int x509_get_authority_key_id(unsigned char **p,
633 unsigned char *end,
634 mbedtls_x509_authority *authority_key_id)
635{
636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
637 size_t len = 0u;
638
639 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
Przemek Stekiel3520fe62023-01-30 14:38:18 +0100640 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
Przemek Stekiel75653b12023-02-01 11:31:32 +0100641 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200642 }
643
Przemek Stekiel6ec839a2023-02-01 11:06:08 +0100644 if (*p + len != end) {
645 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
646 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
647 }
648
Przemek Stekieled9fb782023-05-03 16:27:25 +0200649 ret = mbedtls_asn1_get_tag(p, end, &len,
650 MBEDTLS_ASN1_CONTEXT_SPECIFIC);
651
652 /* KeyIdentifier is an OPTIONAL field */
Przemek Stekiel61aed062023-05-08 11:14:36 +0200653 if (ret == 0) {
toth92g8d435a02021-05-10 15:16:33 +0200654 authority_key_id->keyIdentifier.len = len;
655 authority_key_id->keyIdentifier.p = *p;
toth92g9232e0a2021-05-11 12:55:58 +0200656 /* Setting tag of the keyIdentfier intentionally to 0x04.
657 * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
Przemek Stekiel9a7a7252023-04-17 16:06:57 +0200658 * its tag with the content is the payload of on OCTET STRING primitive */
toth92g8d435a02021-05-10 15:16:33 +0200659 authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
660
661 *p += len;
Przemek Stekiel61aed062023-05-08 11:14:36 +0200662 } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
663 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200664 }
665
666 if (*p < end) {
toth92g9232e0a2021-05-11 12:55:58 +0200667 /* Getting authorityCertIssuer using the required specific class tag [1] */
toth92g8d435a02021-05-10 15:16:33 +0200668 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
669 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
Przemek Stekiel4f3e7b92023-02-03 15:03:59 +0100670 1)) != 0) {
Przemek Stekielf5b8f782023-04-26 08:55:26 +0200671 /* authorityCertIssuer and authorityCertSerialNumber MUST both
672 be present or both be absent. At this point we expect to have both. */
673 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
toth92g8d435a02021-05-10 15:16:33 +0200674 }
Przemek Stekieled9fb782023-05-03 16:27:25 +0200675 /* "end" also includes the CertSerialNumber field so "len" shall be used */
676 ret = mbedtls_x509_get_subject_alt_name_ext(p,
677 (*p+len),
678 &authority_key_id->authorityCertIssuer);
679 if (ret != 0) {
680 return ret;
681 }
682
683 /* Getting authorityCertSerialNumber using the required specific class tag [2] */
684 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
685 MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
686 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
687 }
688 authority_key_id->authorityCertSerialNumber.len = len;
689 authority_key_id->authorityCertSerialNumber.p = *p;
690 authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
691 *p += len;
toth92g8d435a02021-05-10 15:16:33 +0200692 }
693
694 if (*p != end) {
695 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
696 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
697 }
698
699 return 0;
700}
701
702/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200703 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
704 *
705 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
706 *
707 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
708 *
709 * PolicyInformation ::= SEQUENCE {
710 * policyIdentifier CertPolicyId,
711 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
712 * PolicyQualifierInfo OPTIONAL }
713 *
714 * CertPolicyId ::= OBJECT IDENTIFIER
715 *
716 * PolicyQualifierInfo ::= SEQUENCE {
717 * policyQualifierId PolicyQualifierId,
718 * qualifier ANY DEFINED BY policyQualifierId }
719 *
720 * -- policyQualifierIds for Internet policy qualifiers
721 *
722 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
723 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
724 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
725 *
726 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
727 *
728 * Qualifier ::= CHOICE {
729 * cPSuri CPSuri,
730 * userNotice UserNotice }
731 *
732 * CPSuri ::= IA5String
733 *
734 * UserNotice ::= SEQUENCE {
735 * noticeRef NoticeReference OPTIONAL,
736 * explicitText DisplayText OPTIONAL }
737 *
738 * NoticeReference ::= SEQUENCE {
739 * organization DisplayText,
740 * noticeNumbers SEQUENCE OF INTEGER }
741 *
742 * DisplayText ::= CHOICE {
743 * ia5String IA5String (SIZE (1..200)),
744 * visibleString VisibleString (SIZE (1..200)),
745 * bmpString BMPString (SIZE (1..200)),
746 * utf8String UTF8String (SIZE (1..200)) }
747 *
748 * NOTE: we only parse and use anyPolicy without qualifiers at this point
749 * as defined in RFC 5280.
750 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751static int x509_get_certificate_policies(unsigned char **p,
752 const unsigned char *end,
753 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200754{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300755 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200756 size_t len;
757 mbedtls_asn1_buf *buf;
758 mbedtls_asn1_sequence *cur = certificate_policies;
759
760 /* Get main sequence tag */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 ret = mbedtls_asn1_get_tag(p, end, &len,
762 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
763 if (ret != 0) {
764 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
765 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if (*p + len != end) {
768 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
769 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
770 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200771
772 /*
773 * Cannot be an empty sequence.
774 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (len == 0) {
776 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
777 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
778 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200781 mbedtls_x509_buf policy_oid;
782 const unsigned char *policy_end;
783
784 /*
785 * Get the policy sequence
786 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
788 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
789 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
790 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200791
792 policy_end = *p + len;
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
795 MBEDTLS_ASN1_OID)) != 0) {
796 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
797 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200798
799 policy_oid.tag = MBEDTLS_ASN1_OID;
800 policy_oid.len = len;
801 policy_oid.p = *p;
802
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300803 /*
804 * Only AnyPolicy is currently supported when enforcing policy.
805 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300807 /*
808 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200809 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300810 */
811 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
812 }
813
Ron Eldor74d9acc2019-03-21 14:00:03 +0200814 /* Allocate and assign next pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (cur->buf.p != NULL) {
816 if (cur->next != NULL) {
817 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
818 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (cur->next == NULL) {
823 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
824 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
825 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200826
827 cur = cur->next;
828 }
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200831 buf->tag = policy_oid.tag;
832 buf->p = policy_oid.p;
833 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300834
835 *p += len;
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 /*
838 * If there is an optional qualifier, then *p < policy_end
839 * Check the Qualifier len to verify it doesn't exceed policy_end.
840 */
841 if (*p < policy_end) {
842 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
843 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
844 0) {
845 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
846 }
Ron Eldor08063792019-05-13 16:38:39 +0300847 /*
848 * Skip the optional policy qualifiers.
849 */
850 *p += len;
851 }
852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (*p != policy_end) {
854 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
855 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
856 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200857 }
858
859 /* Set final sequence entry's next pointer to NULL */
860 cur->next = NULL;
861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (*p != end) {
863 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
864 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
865 }
Ron Eldor74d9acc2019-03-21 14:00:03 +0200866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200868}
869
870/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871 * X.509 v3 extensions
872 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874static int x509_get_crt_ext(unsigned char **p,
875 const unsigned char *end,
876 mbedtls_x509_crt *crt,
877 mbedtls_x509_crt_ext_cb_t cb,
878 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879{
Janos Follath865b3eb2019-12-16 11:46:15 +0000880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200882 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (*p == end) {
885 return 0;
886 }
Hanno Becker12f62fb2019-02-12 17:22:36 +0000887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
889 return ret;
890 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891
Hanno Becker12f62fb2019-02-12 17:22:36 +0000892 end = crt->v3_ext.p + crt->v3_ext.len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 /*
895 * Extension ::= SEQUENCE {
896 * extnID OBJECT IDENTIFIER,
897 * critical BOOLEAN DEFAULT FALSE,
898 * extnValue OCTET STRING }
899 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901 int is_critical = 0; /* DEFAULT FALSE */
902 int ext_type = 0;
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
905 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
906 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
907 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
909 end_ext_data = *p + len;
910
911 /* Get extension ID */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
913 MBEDTLS_ASN1_OID)) != 0) {
914 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
915 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916
k-stachowiak463928a2018-07-24 12:50:59 +0200917 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200918 extn_oid.p = *p;
919 *p += extn_oid.len;
920
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 /* Get optional critical */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
923 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
924 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
925 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
927 /* Data should be octet string type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
929 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
930 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
931 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200933 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934 end_ext_octet = *p + len;
935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (end_ext_octet != end_ext_data) {
937 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
938 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
939 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940
941 /*
942 * Detect supported extensions
943 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200947 /* Give the callback (if any) a chance to handle the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (cb != NULL) {
949 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
950 if (ret != 0 && is_critical) {
951 return ret;
952 }
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200953 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200954 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200955 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200956
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 /* No parser found, skip extension */
958 *p = end_ext_octet;
959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 /* Data is marked as critical: fail */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
963 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 continue;
966 }
967
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100968 /* Forbid repeated extensions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 if ((crt->ext_types & ext_type) != 0) {
970 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
971 }
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100972
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973 crt->ext_types |= ext_type;
974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 switch (ext_type) {
976 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
977 /* Parse basic constraints */
978 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
979 &crt->ca_istrue, &crt->max_pathlen)) != 0) {
980 return ret;
981 }
982 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 case MBEDTLS_X509_EXT_KEY_USAGE:
985 /* Parse key usage */
Przemek Stekiel21c37282023-01-16 08:47:49 +0100986 if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
987 &crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return ret;
989 }
990 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
993 /* Parse extended key usage */
994 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
995 &crt->ext_key_usage)) != 0) {
996 return ret;
997 }
998 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
toth92ga41954d2021-02-12 16:11:17 +01001000 case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
1001 /* Parse subject key identifier */
1002 if ((ret = x509_get_subject_key_id(p, end_ext_data,
1003 &crt->subject_key_id)) != 0) {
1004 return ret;
1005 }
1006 break;
toth92g8d435a02021-05-10 15:16:33 +02001007
toth92ga41954d2021-02-12 16:11:17 +01001008 case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1009 /* Parse authority key identifier */
1010 if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1011 &crt->authority_key_id)) != 0) {
1012 return ret;
1013 }
1014 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
toth92g8d435a02021-05-10 15:16:33 +02001016 /* Parse subject alt name
1017 * SubjectAltName ::= GeneralNames
1018 */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001019 if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1020 &crt->subject_alt_names)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 return ret;
1022 }
1023 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1026 /* Parse netscape certificate type */
Przemek Stekiel21c37282023-01-16 08:47:49 +01001027 if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1028 &crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return ret;
1030 }
1031 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1034 /* Parse certificate policies type */
1035 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1036 &crt->certificate_policies)) != 0) {
1037 /* Give the callback (if any) a chance to handle the extension
1038 * if it contains unsupported policies */
1039 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1040 cb(p_ctx, crt, &extn_oid, is_critical,
1041 start_ext_octet, end_ext_octet) == 0) {
1042 break;
1043 }
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (is_critical) {
1046 return ret;
1047 } else
1048 /*
1049 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1050 * cannot interpret or enforce the policy. However, it is up to
1051 * the user to choose how to enforce the policies,
1052 * unless the extension is critical.
1053 */
1054 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1055 return ret;
1056 }
1057 }
1058 break;
1059
1060 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001061 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 * If this is a non-critical extension, which the oid layer
1063 * supports, but there isn't an x509 parser for it,
1064 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001065 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (is_critical) {
1067 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1068 } else {
1069 *p = end_ext_octet;
1070 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071 }
1072 }
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (*p != end) {
1075 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1076 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1077 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080}
1081
1082/*
1083 * Parse and fill a single X.509 certificate in DER format
1084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1086 const unsigned char *buf,
1087 size_t buflen,
1088 int make_copy,
1089 mbedtls_x509_crt_ext_cb_t cb,
1090 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091{
Janos Follath865b3eb2019-12-16 11:46:15 +00001092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093 size_t len;
1094 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1098 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1099 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100
1101 /*
1102 * Check for valid input
1103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if (crt == NULL || buf == NULL) {
1105 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001108 /* Use the original buffer until we figure out actual length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 p = (unsigned char *) buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001110 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 end = p + len;
1112
1113 /*
1114 * Certificate ::= SEQUENCE {
1115 * tbsCertificate TBSCertificate,
1116 * signatureAlgorithm AlgorithmIdentifier,
1117 * signatureValue BIT STRING }
1118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1120 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1121 mbedtls_x509_crt_free(crt);
1122 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123 }
1124
Janos Follathcc0e49d2016-02-17 14:34:12 +00001125 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001126 crt->raw.len = crt_end - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001128 /* Create and populate a new buffer for the raw field. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1130 if (crt->raw.p == NULL) {
1131 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1132 }
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001135 crt->own_buffer = 1;
1136
1137 p += crt->raw.len - len;
1138 end = crt_end = p + len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 } else {
1140 crt->raw.p = (unsigned char *) buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001141 crt->own_buffer = 0;
1142 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001143
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144 /*
1145 * TBSCertificate ::= SEQUENCE {
1146 */
1147 crt->tbs.p = p;
1148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1150 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1151 mbedtls_x509_crt_free(crt);
1152 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153 }
1154
1155 end = p + len;
1156 crt->tbs.len = end - crt->tbs.p;
1157
1158 /*
1159 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1160 *
1161 * CertificateSerialNumber ::= INTEGER
1162 *
1163 * signature AlgorithmIdentifier
1164 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1166 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1167 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1168 &sig_params1)) != 0) {
1169 mbedtls_x509_crt_free(crt);
1170 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 }
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (crt->version < 0 || crt->version > 2) {
1174 mbedtls_x509_crt_free(crt);
1175 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 }
1177
Andres AG7ca4a032017-03-09 16:16:11 +00001178 crt->version++;
1179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1181 &crt->sig_md, &crt->sig_pk,
1182 &crt->sig_opts)) != 0) {
1183 mbedtls_x509_crt_free(crt);
1184 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001185 }
1186
1187 /*
1188 * issuer Name
1189 */
1190 crt->issuer_raw.p = p;
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1193 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1194 mbedtls_x509_crt_free(crt);
1195 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 }
1197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1199 mbedtls_x509_crt_free(crt);
1200 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201 }
1202
1203 crt->issuer_raw.len = p - crt->issuer_raw.p;
1204
1205 /*
1206 * Validity ::= SEQUENCE {
1207 * notBefore Time,
1208 * notAfter Time }
1209 *
1210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1212 &crt->valid_to)) != 0) {
1213 mbedtls_x509_crt_free(crt);
1214 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 }
1216
1217 /*
1218 * subject Name
1219 */
1220 crt->subject_raw.p = p;
1221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1223 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1224 mbedtls_x509_crt_free(crt);
1225 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001226 }
1227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1229 mbedtls_x509_crt_free(crt);
1230 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231 }
1232
1233 crt->subject_raw.len = p - crt->subject_raw.p;
1234
1235 /*
1236 * SubjectPublicKeyInfo
1237 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001238 crt->pk_raw.p = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1240 mbedtls_x509_crt_free(crt);
1241 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001243 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001244
1245 /*
1246 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1247 * -- If present, version shall be v2 or v3
1248 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1249 * -- If present, version shall be v2 or v3
1250 * extensions [3] EXPLICIT Extensions OPTIONAL
1251 * -- If present, version shall be v3
1252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (crt->version == 2 || crt->version == 3) {
1254 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1255 if (ret != 0) {
1256 mbedtls_x509_crt_free(crt);
1257 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258 }
1259 }
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if (crt->version == 2 || crt->version == 3) {
1262 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1263 if (ret != 0) {
1264 mbedtls_x509_crt_free(crt);
1265 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 }
1267 }
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (crt->version == 3) {
1270 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1271 if (ret != 0) {
1272 mbedtls_x509_crt_free(crt);
1273 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 }
1275 }
1276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if (p != end) {
1278 mbedtls_x509_crt_free(crt);
1279 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1280 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 }
1282
1283 end = crt_end;
1284
1285 /*
1286 * }
1287 * -- end of TBSCertificate
1288 *
1289 * signatureAlgorithm AlgorithmIdentifier,
1290 * signatureValue BIT STRING
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1293 mbedtls_x509_crt_free(crt);
1294 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 }
1296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (crt->sig_oid.len != sig_oid2.len ||
1298 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001299 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001300 sig_params1.len != sig_params2.len ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 (sig_params1.len != 0 &&
1302 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1303 mbedtls_x509_crt_free(crt);
1304 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305 }
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1308 mbedtls_x509_crt_free(crt);
1309 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 }
1311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (p != end) {
1313 mbedtls_x509_crt_free(crt);
1314 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1315 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 }
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319}
1320
1321/*
1322 * Parse one X.509 certificate in DER format from a buffer and add them to a
1323 * chained list
1324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1326 const unsigned char *buf,
1327 size_t buflen,
1328 int make_copy,
1329 mbedtls_x509_crt_ext_cb_t cb,
1330 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331{
Janos Follath865b3eb2019-12-16 11:46:15 +00001332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334
1335 /*
1336 * Check for valid input
1337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (crt == NULL || buf == NULL) {
1339 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1340 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343 prev = crt;
1344 crt = crt->next;
1345 }
1346
1347 /*
1348 * Add new certificate on the end of the chain if needed.
1349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 if (crt->version != 0 && crt->next == NULL) {
1351 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (crt->next == NULL) {
1354 return MBEDTLS_ERR_X509_ALLOC_FAILED;
1355 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356
1357 prev = crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360 }
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1363 if (ret != 0) {
1364 if (prev) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365 prev->next = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (crt != chain) {
1369 mbedtls_free(crt);
1370 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373 }
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376}
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1379 const unsigned char *buf,
1380 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001381{
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001383}
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1386 const unsigned char *buf,
1387 size_t buflen,
1388 int make_copy,
1389 mbedtls_x509_crt_ext_cb_t cb,
1390 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001391{
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001393}
1394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1396 const unsigned char *buf,
1397 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001398{
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001400}
1401
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001403 * Parse one or more PEM certificates from a buffer and add them to the chained
1404 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1407 const unsigned char *buf,
1408 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001409{
Janos Follath98e28a72016-05-31 14:03:54 +01001410#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001411 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001413#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414
1415 /*
1416 * Check for valid input
1417 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (chain == NULL || buf == NULL) {
1419 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1420 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421
1422 /*
1423 * Determine buffer content. Buffer contains either one DER certificate or
1424 * one or more PEM certificates.
1425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1428 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001430 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1433 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1434 }
Janos Follath98e28a72016-05-31 14:03:54 +01001435#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
Janos Follath98e28a72016-05-31 14:03:54 +01001437#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_PEM_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001444 /* 1 rather than 0 since the terminating NULL byte is counted in */
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446 size_t use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001449 /* If we get there, we know the string is null-terminated */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 ret = mbedtls_pem_read_buffer(&pem,
1451 "-----BEGIN CERTIFICATE-----",
1452 "-----END CERTIFICATE-----",
1453 buf, NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456 /*
1457 * Was PEM encoded
1458 */
1459 buflen -= use_len;
1460 buf += use_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1462 return ret;
1463 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1464 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465
1466 /*
1467 * PEM header and footer were found
1468 */
1469 buflen -= use_len;
1470 buf += use_len;
1471
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001473 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475
Paul Bakker5a5fa922014-09-26 14:53:04 +02001476 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001487 /*
1488 * Quit parsing on a memory error
1489 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1491 return ret;
1492 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (first_error == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495 first_error = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497
1498 total_failed++;
1499 continue;
1500 }
1501
1502 success = 1;
1503 }
1504 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (success) {
1507 return total_failed;
1508 } else if (first_error) {
1509 return first_error;
1510 } else {
1511 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1512 }
Janos Follath98e28a72016-05-31 14:03:54 +01001513#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514}
1515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517/*
1518 * Load one or more certificates and add them to the chained list
1519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521{
Janos Follath865b3eb2019-12-16 11:46:15 +00001522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001523 size_t n;
1524 unsigned char *buf;
1525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1527 return ret;
1528 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 mbedtls_platform_zeroize(buf, n);
1533 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536}
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539{
1540 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001541#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542 int w_ret;
1543 WCHAR szDir[MAX_PATH];
1544 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001545 char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
Paul Bakker9af723c2014-05-01 13:03:14 +02001548 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549 HANDLE hFind;
1550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 if (len > MAX_PATH - 3) {
1552 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1553 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 memset(szDir, 0, sizeof(szDir));
1556 memset(filename, 0, MAX_PATH);
1557 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001558 filename[len++] = '\\';
1559 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560 filename[len++] = '*';
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1563 MAX_PATH - 3);
1564 if (w_ret == 0) {
1565 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1566 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 hFind = FindFirstFileW(szDir, &file_data);
1569 if (hFind == INVALID_HANDLE_VALUE) {
1570 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1571 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572
1573 len = MAX_PATH - len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 do {
1575 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
Tom Cosgroveb96c3092023-02-10 12:52:13 +00001582 -1,
1583 p, (int) len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 NULL, NULL);
1585 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001586 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1587 goto cleanup;
1588 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1591 if (w_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 ret += w_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 }
1596 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (GetLastError() != ERROR_NO_MORE_FILES) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601
Ron Eldor36d90422017-01-09 15:09:16 +02001602cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 FindClose(hFind);
Paul Bakkerbe089b02013-10-14 15:51:50 +02001604#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001605 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001606 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001608 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001609 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if (dir == NULL) {
1613 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1614 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615
Ron Eldor63140682017-01-09 19:27:59 +02001616#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1618 closedir(dir);
1619 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001620 }
Ron Eldor63140682017-01-09 19:27:59 +02001621#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 while ((entry = readdir(dir)) != NULL) {
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001626 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 "%s/%s", path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628
Dave Rodgman6dd757a2023-02-02 12:40:50 +00001629 if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001630 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1631 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 } else if (stat(entry_name, &sb) == -1) {
1633 if (errno == ENOENT) {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001634 /* Broken symbolic link - ignore this entry.
1635 stat(2) will return this error for either (a) a dangling
1636 symlink or (b) a missing file.
1637 Given that we have just obtained the filename from readdir,
1638 assume that it does exist and therefore treat this as a
1639 dangling symlink. */
1640 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 } else {
Dave Rodgmanfa40b022022-07-20 16:08:00 +01001642 /* Some other file error; report the error. */
Eduardo Silvae1bfffc2019-04-25 10:43:26 -06001643 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1644 goto cleanup;
1645 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646 }
1647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (!S_ISREG(sb.st_mode)) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651
1652 // Ignore parse errors
1653 //
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1655 if (t_ret < 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656 ret++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 } else {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658 ret += t_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001661
1662cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001664
Ron Eldor63140682017-01-09 19:27:59 +02001665#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 }
Ron Eldor63140682017-01-09 19:27:59 +02001669#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001670
Paul Bakkerbe089b02013-10-14 15:51:50 +02001671#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676
Peter Kolbus9a969b62018-12-11 13:55:56 -06001677#if !defined(MBEDTLS_X509_REMOVE_INFO)
Przemek Stekielf4194942023-04-24 09:52:17 +02001678#define PRINT_ITEM(i) \
1679 do { \
1680 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1681 MBEDTLS_X509_SAFE_SNPRINTF; \
1682 sep = ", "; \
1683 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001684
Przemek Stekielf4194942023-04-24 09:52:17 +02001685#define CERT_TYPE(type, name) \
1686 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001687 if (ns_cert_type & (type)) { \
1688 PRINT_ITEM(name); \
1689 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001690 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001691
Przemek Stekielf4194942023-04-24 09:52:17 +02001692#define KEY_USAGE(code, name) \
1693 do { \
Przemek Stekielf5b8f782023-04-26 08:55:26 +02001694 if (key_usage & (code)) { \
1695 PRINT_ITEM(name); \
1696 } \
Przemek Stekielf4194942023-04-24 09:52:17 +02001697 } while (0)
toth92g8d435a02021-05-10 15:16:33 +02001698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699static int x509_info_ext_key_usage(char **buf, size_t *size,
1700 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001701{
Janos Follath865b3eb2019-12-16 11:46:15 +00001702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001703 const char *desc;
1704 size_t n = *size;
1705 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001707 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 while (cur != NULL) {
1710 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001711 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 }
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001715 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001716
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001717 sep = ", ";
1718
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001719 cur = cur->next;
1720 }
1721
1722 *size = n;
1723 *buf = p;
1724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001726}
1727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728static int x509_info_cert_policies(char **buf, size_t *size,
1729 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001730{
Janos Follath865b3eb2019-12-16 11:46:15 +00001731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001732 const char *desc;
1733 size_t n = *size;
1734 char *p = *buf;
1735 const mbedtls_x509_sequence *cur = certificate_policies;
1736 const char *sep = "";
1737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 while (cur != NULL) {
1739 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02001740 desc = "???";
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001744 MBEDTLS_X509_SAFE_SNPRINTF;
1745
1746 sep = ", ";
1747
1748 cur = cur->next;
1749 }
1750
1751 *size = n;
1752 *buf = p;
1753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001755}
1756
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001757/*
1758 * Return an informational string about the certificate.
1759 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001760#define BEFORE_COLON 18
1761#define BC "18"
Gilles Peskine449bd832023-01-11 14:50:10 +01001762int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1763 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764{
Janos Follath865b3eb2019-12-16 11:46:15 +00001765 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001766 size_t n;
1767 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 char key_size_str[BEFORE_COLON];
1769
1770 p = buf;
1771 n = size;
1772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (NULL == crt) {
1774 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01001775 MBEDTLS_X509_SAFE_SNPRINTF;
1776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 return (int) (size - n);
Janos Follath98e28a72016-05-31 14:03:54 +01001778 }
1779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1781 prefix, crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001782 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1784 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001785 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001788 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001791 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001793 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001796 MBEDTLS_X509_SAFE_SNPRINTF;
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001798 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1801 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1802 crt->valid_from.year, crt->valid_from.mon,
1803 crt->valid_from.day, crt->valid_from.hour,
1804 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001805 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1808 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1809 crt->valid_to.year, crt->valid_to.mon,
1810 crt->valid_to.day, crt->valid_to.hour,
1811 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001812 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001815 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1818 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001819 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001821 /* Key size */
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1823 mbedtls_pk_get_name(&crt->pk))) != 0) {
1824 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825 }
1826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1828 (int) mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001829 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001830
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001831 /*
1832 * Optional extensions
1833 */
1834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1836 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1837 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001838 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if (crt->max_pathlen > 0) {
1841 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001842 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001843 }
1844 }
1845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1847 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001848 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001849
Przemek Stekiel21c37282023-01-16 08:47:49 +01001850 if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1851 &crt->subject_alt_names,
1852 prefix)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 return ret;
1854 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001855 }
1856
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1858 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001859 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001860
Przemek Stekiel21c37282023-01-16 08:47:49 +01001861 if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 return ret;
1863 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001864 }
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1867 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001868 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001869
Przemek Stekiel21c37282023-01-16 08:47:49 +01001870 if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 return ret;
1872 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001873 }
1874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1876 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001877 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 if ((ret = x509_info_ext_key_usage(&p, &n,
1880 &crt->ext_key_usage)) != 0) {
1881 return ret;
1882 }
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001883 }
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
1886 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001887 MBEDTLS_X509_SAFE_SNPRINTF;
1888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if ((ret = x509_info_cert_policies(&p, &n,
1890 &crt->certificate_policies)) != 0) {
1891 return ret;
1892 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001893 }
1894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001896 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 return (int) (size - n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001899}
1900
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001901struct x509_crt_verify_string {
1902 int code;
1903 const char *string;
1904};
1905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906#define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001907static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01001908 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001909 { 0, NULL }
1910};
Hanno Becker7ac83f92020-10-09 10:48:22 +01001911#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1914 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001915{
Janos Follath865b3eb2019-12-16 11:46:15 +00001916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001917 const struct x509_crt_verify_string *cur;
1918 char *p = buf;
1919 size_t n = size;
1920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1922 if ((flags & cur->code) == 0) {
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001923 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001927 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001928 flags ^= cur->code;
1929 }
1930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 if (flags != 0) {
1932 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1933 "(this should not happen)\n", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001934 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001935 }
1936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 return (int) (size - n);
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001938}
Hanno Becker612a2f12020-10-09 09:19:39 +01001939#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1942 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001943{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001944 unsigned int usage_must, usage_may;
1945 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 | MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1949 return 0;
1950 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001951
1952 usage_must = usage & ~may_mask;
1953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1955 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1956 }
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001957
1958 usage_may = usage & may_mask;
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1961 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1962 }
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001965}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1968 const char *usage_oid,
1969 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001972
1973 /* Extension is not mandatory, absent means no restriction */
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1975 return 0;
1976 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001977
1978 /*
1979 * Look for the requested usage (or wildcard ANY) in our list
1980 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (cur_oid->len == usage_len &&
1985 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1986 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001987 }
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1990 return 0;
1991 }
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001992 }
1993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001995}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001998/*
1999 * Return 1 if the certificate is revoked, or 0 otherwise.
2000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002001int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002002{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 while (cur != NULL && cur->serial.len != 0) {
2006 if (crt->serial.len == cur->serial.len &&
2007 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2008 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009 }
2010
2011 cur = cur->next;
2012 }
2013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002015}
2016
2017/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002018 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002019 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002020 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002021static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2022 mbedtls_x509_crl *crl_list,
2023 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024{
2025 int flags = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +02002026 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +01002027#if defined(MBEDTLS_USE_PSA_CRYPTO)
pespacek7599a772022-02-07 14:40:55 +01002028 psa_algorithm_t psa_algorithm;
2029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 const mbedtls_md_info_t *md_info;
pespacek7599a772022-02-07 14:40:55 +01002031#endif /* MBEDTLS_USE_PSA_CRYPTO */
2032 size_t hash_length;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (ca == NULL) {
2035 return flags;
2036 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 while (crl_list != NULL) {
2039 if (crl_list->version == 0 ||
2040 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002041 crl_list = crl_list->next;
2042 continue;
2043 }
2044
2045 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002046 * Check if the CA is configured to sign CRLs
2047 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 if (mbedtls_x509_crt_check_key_usage(ca,
2049 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002051 break;
2052 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002053
2054 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002055 * Check if CRL is correctly signed by the trusted CA
2056 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002058 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002062 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002064
pespacek7599a772022-02-07 14:40:55 +01002065#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 psa_algorithm = mbedtls_hash_info_psa_from_md(crl_list->sig_md);
2067 if (psa_hash_compute(psa_algorithm,
2068 crl_list->tbs.p,
2069 crl_list->tbs.len,
2070 hash,
2071 sizeof(hash),
2072 &hash_length) != PSA_SUCCESS) {
pespaceka7a64692022-02-14 15:18:43 +01002073 /* Note: this can't happen except after an internal error */
2074 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2075 break;
2076 }
pespacek7599a772022-02-07 14:40:55 +01002077#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2079 hash_length = mbedtls_md_get_size(md_info);
2080 if (mbedtls_md(md_info,
2081 crl_list->tbs.p,
2082 crl_list->tbs.len,
2083 hash) != 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002084 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086 break;
2087 }
pespaceka7a64692022-02-14 15:18:43 +01002088#endif /* MBEDTLS_USE_PSA_CRYPTO */
2089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (x509_profile_check_key(profile, &ca->pk) != 0) {
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002091 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 }
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002093
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2095 crl_list->sig_md, hash, hash_length,
2096 crl_list->sig.p, crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098 break;
2099 }
2100
2101 /*
2102 * Check for validity of CRL (Do not drop out)
2103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (mbedtls_x509_time_is_past(&crl_list->next_update)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (mbedtls_x509_time_is_future(&crl_list->this_update)) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002109 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 }
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002111
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112 /*
2113 * Check if certificate is revoked
2114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117 break;
2118 }
2119
2120 crl_list = crl_list->next;
2121 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002126
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002127/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002128 * Check the signature of a certificate by its parent
2129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2131 mbedtls_x509_crt *parent,
2132 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002133{
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002134 size_t hash_len;
Przemek Stekiel51669542022-09-13 12:57:05 +02002135 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002136#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2137 const mbedtls_md_info_t *md_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 md_info = mbedtls_md_info_from_type(child->sig_md);
2139 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002140
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002141 /* Note: hash errors can happen only after an internal error */
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
2143 return -1;
2144 }
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002145#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(child->sig_md);
pespacek7599a772022-02-07 14:40:55 +01002147 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 status = psa_hash_compute(hash_alg,
2150 child->tbs.p,
2151 child->tbs.len,
2152 hash,
2153 sizeof(hash),
2154 &hash_len);
2155 if (status != PSA_SUCCESS) {
2156 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002157 }
2158
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002159#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002160 /* Skip expensive computation on obvious mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
2162 return -1;
2163 }
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002164
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002165#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2167 return mbedtls_pk_verify_restartable(&parent->pk,
2168 child->sig_md, hash, hash_len,
2169 child->sig.p, child->sig.len, &rs_ctx->pk);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002170 }
2171#else
2172 (void) rs_ctx;
2173#endif
2174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2176 child->sig_md, hash, hash_len,
2177 child->sig.p, child->sig.len);
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002178}
2179
2180/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002181 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2182 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002183 *
2184 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002185 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2187 const mbedtls_x509_crt *parent,
2188 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002189{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002190 int need_ca_bit;
2191
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002192 /* Parent must be the issuer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2194 return -1;
2195 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002196
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002197 /* Parent must have the basicConstraints CA bit set as a general rule */
2198 need_ca_bit = 1;
2199
2200 /* Exception: v1/v2 certificates that are locally trusted. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (top && parent->version < 3) {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002202 need_ca_bit = 0;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002203 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (need_ca_bit && !parent->ca_istrue) {
2206 return -1;
2207 }
2208
2209 if (need_ca_bit &&
2210 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2211 return -1;
2212 }
2213
2214 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002215}
2216
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002217/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002218 * Find a suitable parent for child in candidates, or return NULL.
2219 *
2220 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002221 * 1. subject name matches child's issuer
2222 * 2. if necessary, the CA bit is set and key usage allows signing certs
2223 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002224 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002225 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002226 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002227 * If there's a suitable candidate which is also time-valid, return the first
2228 * such. Otherwise, return the first suitable candidate (or NULL if there is
2229 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002230 *
2231 * The rationale for this rule is that someone could have a list of trusted
2232 * roots with two versions on the same root with different validity periods.
2233 * (At least one user reported having such a list and wanted it to just work.)
2234 * The reason we don't just require time-validity is that generally there is
2235 * only one version, and if it's expired we want the flags to state that
2236 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002237 *
2238 * The rationale for rule 3 (signature for trusted roots) is that users might
2239 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002240 * way we select the correct one is by checking the signature (as we don't
2241 * rely on key identifier extensions). (This is one way users might choose to
2242 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002243 *
2244 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002245 * - [in] child: certificate for which we're looking for a parent
2246 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002247 * - [out] r_parent: parent found (or NULL)
2248 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002249 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2250 * of the chain, 0 otherwise
2251 * - [in] path_cnt: number of intermediates seen so far
2252 * - [in] self_cnt: number of self-signed intermediates seen so far
2253 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002254 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002255 *
2256 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002257 * - 0 on success
2258 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002259 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002260static int x509_crt_find_parent_in(
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 mbedtls_x509_crt *child,
2262 mbedtls_x509_crt *candidates,
2263 mbedtls_x509_crt **r_parent,
2264 int *r_signature_is_good,
2265 int top,
2266 unsigned path_cnt,
2267 unsigned self_cnt,
2268 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002269{
Janos Follath865b3eb2019-12-16 11:46:15 +00002270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002271 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002272 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002273
2274#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002275 /* did we have something in progress? */
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002277 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002278 parent = rs_ctx->parent;
2279 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002280 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002281
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002282 /* clear saved state */
2283 rs_ctx->parent = NULL;
2284 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002285 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002286
2287 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002288 goto check_signature;
2289 }
2290#endif
2291
2292 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002293 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002296 /* basic parenting skills (name, CA bit, key usage) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (x509_crt_check_parent(child, parent, top) != 0) {
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002298 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 }
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002300
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002301 /* +1 because stored max_pathlen is 1 higher that the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 if (parent->max_pathlen > 0 &&
2303 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002304 continue;
2305 }
2306
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002307 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002308#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2309check_signature:
2310#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002312
2313#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002315 /* save state */
2316 rs_ctx->parent = parent;
2317 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002318 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002321 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002322#else
2323 (void) ret;
2324#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002325
2326 signature_is_good = ret == 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (top && !signature_is_good) {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002328 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 }
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002330
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002331 /* optional time check */
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2333 mbedtls_x509_time_is_future(&parent->valid_from)) {
2334 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002335 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002336 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002337 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002338
2339 continue;
2340 }
2341
Andy Gross1f627142019-01-30 10:25:53 -06002342 *r_parent = parent;
2343 *r_signature_is_good = signature_is_good;
2344
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002345 break;
2346 }
2347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002349 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002350 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002351 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002354}
2355
2356/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002357 * Find a parent in trusted CAs or the provided chain, or return NULL.
2358 *
2359 * Searches in trusted CAs first, and return the first suitable parent found
2360 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002361 *
2362 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002363 * - [in] child: certificate for which we're looking for a parent, followed
2364 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002365 * - [in] trust_ca: list of locally trusted certificates
2366 * - [out] parent: parent found (or NULL)
2367 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2368 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2369 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2370 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002371 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002372 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002373 *
2374 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002375 * - 0 on success
2376 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002377 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002378static int x509_crt_find_parent(
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 mbedtls_x509_crt *child,
2380 mbedtls_x509_crt *trust_ca,
2381 mbedtls_x509_crt **parent,
2382 int *parent_is_trusted,
2383 int *signature_is_good,
2384 unsigned path_cnt,
2385 unsigned self_cnt,
2386 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002387{
Janos Follath865b3eb2019-12-16 11:46:15 +00002388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002389 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002390
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002391 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002392
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002393#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002394 /* restore then clear saved state if we have some stored */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002396 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002397 rs_ctx->parent_is_trusted = -1;
2398 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002399#endif
2400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002402 search_list = *parent_is_trusted ? trust_ca : child->next;
2403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 ret = x509_crt_find_parent_in(child, search_list,
2405 parent, signature_is_good,
2406 *parent_is_trusted,
2407 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002408
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002409#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002411 /* save state */
2412 rs_ctx->parent_is_trusted = *parent_is_trusted;
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002414 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002415#else
2416 (void) ret;
2417#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002418
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002419 /* stop here if found or already in second iteration */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 if (*parent != NULL || *parent_is_trusted == 0) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002421 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 }
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002423
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002424 /* prepare second iteration */
2425 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002426 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002427
2428 /* extra precaution against mistakes in the caller */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002430 *parent_is_trusted = 0;
2431 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002432 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002435}
2436
2437/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002438 * Check if an end-entity certificate is locally trusted
2439 *
2440 * Currently we require such certificates to be self-signed (actually only
2441 * check for self-issued as self-signatures are not checked)
2442 */
2443static int x509_crt_check_ee_locally_trusted(
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 mbedtls_x509_crt *crt,
2445 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002446{
2447 mbedtls_x509_crt *cur;
2448
2449 /* must be self-issued */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2451 return -1;
2452 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002453
2454 /* look for an exact match with trusted cert */
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2456 if (crt->raw.len == cur->raw.len &&
2457 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2458 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002459 }
2460 }
2461
2462 /* too bad */
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002464}
2465
2466/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002467 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002468 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002469 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2470 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002471 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002472 * such that every cert in the chain is a child of the next one,
2473 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002474 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002475 * Verify that chain and return it with flags for all issues found.
2476 *
2477 * Special cases:
2478 * - EE == Rj -> return a one-element list containing it
2479 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2480 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002481 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002482 * Tests for (aspects of) this function should include at least:
2483 * - trusted EE
2484 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002485 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002486 * - if relevant: EE untrusted
2487 * - if relevant: EE -> intermediate, untrusted
2488 * with the aspect under test checked at each relevant level (EE, int, root).
2489 * For some aspects longer chains are required, but usually length 2 is
2490 * enough (but length 1 is not in general).
2491 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002492 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002493 * - [in] crt: the cert list EE, C1, ..., Cn
2494 * - [in] trust_ca: the trusted list R1, ..., Rp
2495 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002496 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002497 * Only valid when return value is 0, may contain garbage otherwise!
2498 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002499 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002500 *
2501 * Return value:
2502 * - non-zero if the chain could not be fully built and examined
2503 * - 0 is the chain was successfully built and examined,
2504 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002505 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002506static int x509_crt_verify_chain(
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 mbedtls_x509_crt *crt,
2508 mbedtls_x509_crt *trust_ca,
2509 mbedtls_x509_crl *ca_crl,
2510 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2511 void *p_ca_cb,
2512 const mbedtls_x509_crt_profile *profile,
2513 mbedtls_x509_crt_verify_chain *ver_chain,
2514 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002515{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002516 /* Don't initialize any of those variables here, so that the compiler can
2517 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002519 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002520 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002521 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002522 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002523 int parent_is_trusted;
2524 int child_is_trusted;
2525 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002526 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002527 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002528
2529#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2530 /* resume if we had an operation in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002532 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002533 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002534 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002535
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002536 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002537 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002538 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002539 flags = &cur->flags;
2540
2541 goto find_parent;
2542 }
2543#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002544
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002545 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002546 self_cnt = 0;
2547 parent_is_trusted = 0;
2548 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002551 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002552 cur = &ver_chain->items[ver_chain->len];
2553 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002554 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002555 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002556 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002557
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002558 /* Check time-validity (all certificates) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (mbedtls_x509_time_is_past(&child->valid_to)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002560 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (mbedtls_x509_time_is_future(&child->valid_from)) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002564 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 }
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002566
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002567 /* Stop here for trusted roots (but not for trusted EE certs) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (child_is_trusted) {
2569 return 0;
2570 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002571
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002572 /* Check signature algorithm: MD & PK algs */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (x509_profile_check_md_alg(profile, child->sig_md) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002574 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 }
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002578 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 }
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002580
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002581 /* Special case: EE certs that are locally trusted */
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 if (ver_chain->len == 1 &&
2583 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2584 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002585 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002586
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002587#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2588find_parent:
2589#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002590
2591 /* Obtain list of potential trusted signers from CA callback,
2592 * or use statically provided list. */
2593#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (f_ca_cb != NULL) {
2595 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2596 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002597 ver_chain->trust_ca_cb_result = NULL;
2598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2600 if (ret != 0) {
2601 return MBEDTLS_ERR_X509_FATAL_ERROR;
2602 }
Hanno Beckerf53893b2019-03-28 13:45:55 +00002603
2604 cur_trust_ca = ver_chain->trust_ca_cb_result;
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 } else
Hanno Beckerf53893b2019-03-28 13:45:55 +00002606#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2607 {
2608 ((void) f_ca_cb);
2609 ((void) p_ca_cb);
2610 cur_trust_ca = trust_ca;
2611 }
2612
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002613 /* Look for a parent in trusted CAs or up the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2615 &parent_is_trusted, &signature_is_good,
2616 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002617
2618#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002620 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002621 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002622 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002623 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002626 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002627#else
2628 (void) ret;
2629#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002630
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002631 /* No parent? We're done here */
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002633 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002635 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002636
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002637 /* Count intermediate self-issued (not necessarily self-signed) certs.
2638 * These can occur with some strategies for key rollover, see [SIRO],
2639 * and should be excluded from max_pathlen checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (ver_chain->len != 1 &&
2641 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002642 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002643 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002644
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002645 /* path_cnt is 0 for the first intermediate CA,
2646 * and if parent is trusted it's not an intermediate CA */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (!parent_is_trusted &&
2648 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002649 /* return immediately to avoid overflow the chain array */
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002651 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002652
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002653 /* signature was checked while searching parent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 if (!signature_is_good) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002655 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 }
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002657
2658 /* check size of signing key */
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (x509_profile_check_key(profile, &parent->pk) != 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002660 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002664 /* Check trusted CA's CRL for the given crt */
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002666#else
2667 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002668#endif
2669
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002670 /* prepare for next iteration */
2671 child = parent;
2672 parent = NULL;
2673 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002674 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002675 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002676}
2677
Glenn Straussc26bd762022-10-23 19:48:18 -04002678#ifdef _WIN32
2679#ifdef _MSC_VER
2680#pragma comment(lib, "ws2_32.lib")
2681#include <winsock2.h>
2682#include <ws2tcpip.h>
2683#elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2684#include <winsock2.h>
2685#include <ws2tcpip.h>
2686#endif
2687#elif defined(__sun)
2688/* Solaris requires -lsocket -lnsl for inet_pton() */
2689#elif defined(__has_include)
2690#if __has_include(<sys/socket.h>)
2691#include <sys/socket.h>
2692#endif
2693#if __has_include(<arpa/inet.h>)
2694#include <arpa/inet.h>
2695#endif
2696#endif
2697
2698/* Use whether or not AF_INET6 is defined to indicate whether or not to use
2699 * the platform inet_pton() or a local implementation (below). The local
2700 * implementation may be used even in cases where the platform provides
2701 * inet_pton(), e.g. when there are different includes required and/or the
2702 * platform implementation requires dependencies on additional libraries.
2703 * Specifically, Windows requires custom includes and additional link
2704 * dependencies, and Solaris requires additional link dependencies.
2705 * Also, as a coarse heuristic, use the local implementation if the compiler
2706 * does not support __has_include(), or if the definition of AF_INET6 is not
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002707 * provided by headers included (or not) via __has_include() above.
2708 * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2709 * despite having a platform that has inet_pton. */
2710#if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2711/* Definition located further below to possibly reduce compiler inlining */
Glenn Strauss416c2952022-10-24 23:00:02 -04002712static int x509_inet_pton_ipv4(const char *src, void *dst);
2713
2714#define li_cton(c, n) \
2715 (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2716
2717static int x509_inet_pton_ipv6(const char *src, void *dst)
2718{
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002719 const unsigned char *p = (const unsigned char *) src;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002720 int nonzero_groups = 0, num_digits, zero_group_start = -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002721 uint16_t addr[8];
2722 do {
2723 /* note: allows excess leading 0's, e.g. 1:0002:3:... */
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002724 uint16_t group = num_digits = 0;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002725 for (uint8_t digit; num_digits < 4; num_digits++) {
2726 if (li_cton(*p, digit) == 0) {
2727 break;
2728 }
2729 group = (group << 4) | digit;
2730 p++;
Glenn Strauss416c2952022-10-24 23:00:02 -04002731 }
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002732 if (num_digits != 0) {
Andrzej Kurek0d578962023-04-13 09:09:56 -04002733 addr[nonzero_groups++] = MBEDTLS_IS_BIG_ENDIAN ? group :
2734 (group << 8) | (group >> 8);
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002735 if (*p == '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002736 break;
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002737 } else if (*p == '.') {
2738 /* Don't accept IPv4 too early or late */
2739 if ((nonzero_groups == 0 && zero_group_start == -1) ||
2740 nonzero_groups >= 7) {
2741 break;
2742 }
2743
2744 /* Walk back to prior ':', then parse as IPv4-mapped */
2745 int steps = 4;
2746 do {
2747 p--;
2748 steps--;
2749 } while (*p != ':' && steps > 0);
2750
2751 if (*p != ':') {
2752 break;
2753 }
2754 p++;
2755 nonzero_groups--;
2756 if (x509_inet_pton_ipv4((const char *) p,
2757 addr + nonzero_groups) != 0) {
2758 break;
2759 }
2760
Andrzej Kurek0d578962023-04-13 09:09:56 -04002761 nonzero_groups += 2;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002762 p = (const unsigned char *) "";
Glenn Strauss416c2952022-10-24 23:00:02 -04002763 break;
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002764 } else if (*p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002765 return -1;
2766 }
2767 } else {
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002768 /* Don't accept a second zero group or an invalid delimiter */
2769 if (zero_group_start != -1 || *p != ':') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002770 return -1;
2771 }
Andrzej Kurek8bc2cc92023-04-18 07:26:27 -04002772 zero_group_start = nonzero_groups;
2773
2774 /* Accept a zero group at start, but it has to be a double colon */
2775 if (zero_group_start == 0 && *++p != ':') {
2776 return -1;
2777 }
2778
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002779 if (p[1] == '\0') {
2780 ++p;
Glenn Strauss416c2952022-10-24 23:00:02 -04002781 break;
2782 }
2783 }
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002784 ++p;
Andrzej Kurek0d578962023-04-13 09:09:56 -04002785 } while (nonzero_groups < 8);
Andrzej Kurek90117db2023-04-18 07:32:47 -04002786
2787 if (*p != '\0') {
Glenn Strauss416c2952022-10-24 23:00:02 -04002788 return -1;
2789 }
2790
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002791 if (zero_group_start != -1) {
Andrzej Kurek90117db2023-04-18 07:32:47 -04002792 if (nonzero_groups > 6) {
2793 return -1;
2794 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002795 int zero_groups = 8 - nonzero_groups;
2796 int groups_after_zero = nonzero_groups - zero_group_start;
2797
2798 /* Move the non-zero part to after the zeroes */
2799 if (groups_after_zero) {
2800 memmove(addr + zero_group_start + zero_groups,
Andrzej Kurek7f5a1a42023-04-13 08:02:27 -04002801 addr + zero_group_start,
Andrzej Kurek0d578962023-04-13 09:09:56 -04002802 groups_after_zero * sizeof(*addr));
Glenn Strauss416c2952022-10-24 23:00:02 -04002803 }
Andrzej Kurek0d578962023-04-13 09:09:56 -04002804 memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
Andrzej Kurek90117db2023-04-18 07:32:47 -04002805 } else {
2806 if (nonzero_groups != 8) {
2807 return -1;
2808 }
Glenn Strauss416c2952022-10-24 23:00:02 -04002809 }
2810 memcpy(dst, addr, sizeof(addr));
2811 return 0;
2812}
2813
2814static int x509_inet_pton_ipv4(const char *src, void *dst)
2815{
2816 /* note: allows leading 0's, e.g. 000.000.000.000 */
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002817 const unsigned char *p = (const unsigned char *) src;
Glenn Strauss416c2952022-10-24 23:00:02 -04002818 uint8_t *res = (uint8_t *) dst;
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002819 uint8_t digit, num_digits = 0;
2820 uint8_t num_octets = 0;
Andrzej Kurek13b8b782023-04-12 09:43:47 -04002821 uint16_t octet;
2822
Glenn Strauss416c2952022-10-24 23:00:02 -04002823 do {
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002824 octet = num_digits = 0;
2825 do {
2826 digit = *p - '0';
2827 if (digit > 9) {
2828 break;
2829 }
2830 octet = octet * 10 + digit;
2831 num_digits++;
2832 p++;
2833 } while (num_digits < 3);
2834
2835 if (octet >= 256 || num_digits > 3 || num_digits == 0) {
Glenn Strauss416c2952022-10-24 23:00:02 -04002836 break;
Glenn Strauss416c2952022-10-24 23:00:02 -04002837 }
Andrzej Kurekea3e71f2023-04-18 04:39:12 -04002838 *res++ = (uint8_t) octet;
2839 num_octets++;
2840 } while (num_octets < 4 && *p++ == '.');
Andrzej Kurek6cbca6d2023-04-13 09:22:48 -04002841 return num_octets == 4 && *p == '\0' ? 0 : -1;
Glenn Strauss416c2952022-10-24 23:00:02 -04002842}
Glenn Straussc26bd762022-10-23 19:48:18 -04002843
2844#else
2845
2846static int x509_inet_pton_ipv6(const char *src, void *dst)
2847{
2848 return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2849}
2850
2851static int x509_inet_pton_ipv4(const char *src, void *dst)
2852{
2853 return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2854}
2855
Andrzej Kurek06969fc2023-04-12 10:34:50 -04002856#endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
Glenn Straussc26bd762022-10-23 19:48:18 -04002857
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002858MBEDTLS_STATIC_TESTABLE
2859size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
Glenn Straussc26bd762022-10-23 19:48:18 -04002860{
2861 return strchr(cn, ':') == NULL
2862 ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2863 : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2864}
2865
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002866/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002867 * Check for CN match
2868 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002869static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2870 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002871{
2872 /* try exact match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 if (name->len == cn_len &&
2874 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2875 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002876 }
2877
2878 /* try wildcard match */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 if (x509_check_wildcard(cn, name) == 0) {
2880 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002881 }
2882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002884}
2885
Glenn Straussc26bd762022-10-23 19:48:18 -04002886static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2887 const char *cn, size_t cn_len)
2888{
2889 uint32_t ip[4];
Glenn Strauss6f545ac2022-10-25 15:02:14 -04002890 cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
Glenn Straussc26bd762022-10-23 19:48:18 -04002891 if (cn_len == 0) {
2892 return -1;
2893 }
2894
2895 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2896 const unsigned char san_type = (unsigned char) cur->buf.tag &
2897 MBEDTLS_ASN1_TAG_VALUE_MASK;
2898 if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2899 cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2900 return 0;
2901 }
2902 }
2903
2904 return -1;
2905}
2906
Andrzej Kurek199eab92023-05-10 09:57:19 -04002907static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2908 const char *cn, size_t cn_len)
2909{
2910 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2911 const unsigned char san_type = (unsigned char) cur->buf.tag &
2912 MBEDTLS_ASN1_TAG_VALUE_MASK;
2913 if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2914 cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2915 return 0;
2916 }
2917 }
2918
2919 return -1;
2920}
2921
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002922/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002923 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2924 */
Glenn Straussc26bd762022-10-23 19:48:18 -04002925static int x509_crt_check_san(const mbedtls_x509_sequence *san,
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002927{
Glenn Straussc26bd762022-10-23 19:48:18 -04002928 int san_ip = 0;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002929 int san_uri = 0;
2930 /* Prioritize DNS name over other subtypes due to popularity */
Glenn Straussc26bd762022-10-23 19:48:18 -04002931 for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2932 switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
Andrzej Kurek199eab92023-05-10 09:57:19 -04002933 case MBEDTLS_X509_SAN_DNS_NAME:
Glenn Straussc26bd762022-10-23 19:48:18 -04002934 if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2935 return 0;
2936 }
2937 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002938 case MBEDTLS_X509_SAN_IP_ADDRESS:
Glenn Straussc26bd762022-10-23 19:48:18 -04002939 san_ip = 1;
2940 break;
Andrzej Kurek199eab92023-05-10 09:57:19 -04002941 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2942 san_uri = 1;
2943 break;
Glenn Straussc26bd762022-10-23 19:48:18 -04002944 /* (We may handle other types here later.) */
2945 default: /* Unrecognized type */
2946 break;
2947 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 }
Andrzej Kurek199eab92023-05-10 09:57:19 -04002949 if (san_ip) {
2950 if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2951 return 0;
2952 }
2953 }
2954 if (san_uri) {
2955 if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2956 return 0;
2957 }
2958 }
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002959
Andrzej Kurek199eab92023-05-10 09:57:19 -04002960 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002961}
2962
2963/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002964 * Verify the requested CN - only call this if cn is not NULL!
2965 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002966static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2967 const char *cn,
2968 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002969{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002970 const mbedtls_x509_name *name;
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002972
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002974 if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2975 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 }
2977 } else {
2978 for (name = &crt->subject; name != NULL; name = name->next) {
2979 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2980 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Glenn Straussc26bd762022-10-23 19:48:18 -04002981 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 }
2983 }
2984
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002985 }
Glenn Straussc26bd762022-10-23 19:48:18 -04002986
2987 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002988}
2989
2990/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002991 * Merge the flags for all certs in the chain, after calling callback
2992 */
2993static int x509_crt_merge_flags_with_cb(
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 uint32_t *flags,
2995 const mbedtls_x509_crt_verify_chain *ver_chain,
2996 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2997 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002998{
Janos Follath865b3eb2019-12-16 11:46:15 +00002999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003000 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003001 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003002 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 for (i = ver_chain->len; i != 0; --i) {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003005 cur = &ver_chain->items[i-1];
3006 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 if (NULL != f_vrfy) {
3009 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3010 return ret;
3011 }
3012 }
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003013
3014 *flags |= cur_flags;
3015 }
3016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003018}
3019
3020/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003021 * Verify the certificate validity, with profile, restartable version
3022 *
3023 * This function:
3024 * - checks the requested CN (if any)
3025 * - checks the type and size of the EE cert's key,
3026 * as that isn't done as part of chain building/verification currently
3027 * - builds and verifies the chain
3028 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003029 *
3030 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3031 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3032 * verification routine to search for trusted signers, and CRLs will
3033 * be disabled. Otherwise, `trust_ca` will be used as the static list
3034 * of trusted signers, and `ca_crl` will be use as the static list
3035 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003036 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003037static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3038 mbedtls_x509_crt *trust_ca,
3039 mbedtls_x509_crl *ca_crl,
3040 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3041 void *p_ca_cb,
3042 const mbedtls_x509_crt_profile *profile,
3043 const char *cn, uint32_t *flags,
3044 int (*f_vrfy)(void *,
3045 mbedtls_x509_crt *,
3046 int,
3047 uint32_t *),
3048 void *p_vrfy,
3049 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003050{
Janos Follath865b3eb2019-12-16 11:46:15 +00003051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003052 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003053 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003054 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003055
3056 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003057 ee_flags = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003061 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3062 goto exit;
3063 }
3064
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003065 /* check name if requested */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if (cn != NULL) {
3067 x509_crt_verify_name(crt, cn, &ee_flags);
3068 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003069
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003070 /* Check the type and size of the key */
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 if (x509_profile_check_pk_alg(profile, pk_type) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003074 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (x509_profile_check_key(profile, &crt->pk) != 0) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003078 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 }
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003080
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003081 /* Check the chain */
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3083 f_ca_cb, p_ca_cb, profile,
3084 &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 if (ret != 0) {
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003087 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 }
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003089
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003090 /* Merge end-entity flags */
3091 ver_chain.items[0].flags |= ee_flags;
3092
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003093 /* Build final flags, calling callback on the way if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003095
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003096exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003097
3098#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3100 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003101 ver_chain.trust_ca_cb_result = NULL;
3102#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3103
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003104#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3106 mbedtls_x509_crt_restart_free(rs_ctx);
3107 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003108#endif
3109
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003110 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3111 * the SSL module for authmode optional, but non-zero return from the
3112 * callback means a fatal error so it shouldn't be ignored */
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003114 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003115 }
3116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 if (ret != 0) {
3118 *flags = (uint32_t) -1;
3119 return ret;
3120 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (*flags != 0) {
3123 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3124 }
3125
3126 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003127}
3128
Hanno Becker3116fb32019-03-28 13:34:42 +00003129
3130/*
3131 * Verify the certificate validity (default profile, not restartable)
3132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003133int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3134 mbedtls_x509_crt *trust_ca,
3135 mbedtls_x509_crl *ca_crl,
3136 const char *cn, uint32_t *flags,
3137 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3138 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003139{
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3141 NULL, NULL,
3142 &mbedtls_x509_crt_profile_default,
3143 cn, flags,
3144 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003145}
3146
3147/*
3148 * Verify the certificate validity (user-chosen profile, not restartable)
3149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003150int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3151 mbedtls_x509_crt *trust_ca,
3152 mbedtls_x509_crl *ca_crl,
3153 const mbedtls_x509_crt_profile *profile,
3154 const char *cn, uint32_t *flags,
3155 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3156 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003157{
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3159 NULL, NULL,
3160 profile, cn, flags,
3161 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003162}
3163
3164#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3165/*
3166 * Verify the certificate validity (user-chosen profile, CA callback,
3167 * not restartable).
3168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003169int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3170 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3171 void *p_ca_cb,
3172 const mbedtls_x509_crt_profile *profile,
3173 const char *cn, uint32_t *flags,
3174 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3175 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003176{
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3178 f_ca_cb, p_ca_cb,
3179 profile, cn, flags,
3180 f_vrfy, p_vrfy, NULL);
Hanno Becker3116fb32019-03-28 13:34:42 +00003181}
3182#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3185 mbedtls_x509_crt *trust_ca,
3186 mbedtls_x509_crl *ca_crl,
3187 const mbedtls_x509_crt_profile *profile,
3188 const char *cn, uint32_t *flags,
3189 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3190 void *p_vrfy,
3191 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003192{
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3194 NULL, NULL,
3195 profile, cn, flags,
3196 f_vrfy, p_vrfy, rs_ctx);
Hanno Becker3116fb32019-03-28 13:34:42 +00003197}
3198
3199
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003200/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003201 * Initialize a certificate chain
3202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003203void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003204{
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003206}
3207
3208/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003209 * Unallocate all certificate data
3210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003211void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003212{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 mbedtls_x509_crt *cert_cur = crt;
3214 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 while (cert_cur != NULL) {
3217 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 mbedtls_free(cert_cur->sig_opts);
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003221#endif
3222
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3224 mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3225 mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3226 mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3227 mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3230 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3231 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003232 }
3233
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003234 cert_prv = cert_cur;
3235 cert_cur = cert_cur->next;
3236
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3238 if (cert_prv != crt) {
3239 mbedtls_free(cert_prv);
3240 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003241 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003242}
3243
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003244#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3245/*
3246 * Initialize a restart context
3247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003248void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003249{
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003251
3252 ctx->parent = NULL;
3253 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003254 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003255
3256 ctx->parent_is_trusted = -1;
3257
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003258 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003259 ctx->self_cnt = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003261}
3262
3263/*
3264 * Free the components of a restart context
3265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003266void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003267{
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 if (ctx == NULL) {
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003269 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 }
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003271
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 mbedtls_pk_restart_free(&ctx->pk);
3273 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003274}
3275#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277#endif /* MBEDTLS_X509_CRT_PARSE_C */