blob: 334c01761a8339a688ea044ae5442c423a0bebdf [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 *
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020029 * [SIRO]
30 * https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031 */
32
Gilles Peskinedb09ef62020-06-03 01:43:33 +020033#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020037# include "mbedtls/x509_crt.h"
38# include "mbedtls/error.h"
39# include "mbedtls/oid.h"
40# include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000041
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# include <string.h>
Rich Evans00ab4702015-02-06 13:43:58 +000043
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044# if defined(MBEDTLS_PEM_PARSE_C)
45# include "mbedtls/pem.h"
46# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048# if defined(MBEDTLS_USE_PSA_CRYPTO)
49# include "psa/crypto.h"
50# include "mbedtls/psa_util.h"
51# endif
Andrzej Kurekd4a65532018-10-31 06:18:39 -040052
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053# if defined(MBEDTLS_PLATFORM_C)
54# include "mbedtls/platform.h"
55# else
56# include <stdio.h>
57# include <stdlib.h>
58# define mbedtls_free free
59# define mbedtls_calloc calloc
60# define mbedtls_snprintf snprintf
61# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020063# if defined(MBEDTLS_THREADING_C)
64# include "mbedtls/threading.h"
65# endif
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010066
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067# if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
68# include <windows.h>
69# else
70# include <time.h>
71# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073# if defined(MBEDTLS_FS_IO)
74# include <stdio.h>
75# if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
76# include <sys/types.h>
77# include <sys/stat.h>
78# if defined(__MBED__)
79# include <platform/mbed_retarget.h>
80# else
81# include <dirent.h>
82# endif /* __MBED__ */
83# endif /* !_WIN32 || EFIX64 || EFI32 */
84# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085
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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020097# 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. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200101const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default = {
Gilles Peskineae270bf2021-06-02 00:05:29 +0200102 /* Hashes from SHA-256 and above. Note that this selection
103 * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
105 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
106 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 0xFFFFFFF, /* Any PK alg */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108# if defined(MBEDTLS_ECP_C)
Gilles Peskineae270bf2021-06-02 00:05:29 +0200109 /* Curves at or above 128-bit security level. Note that this selection
110 * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200111 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
112 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
113 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
114 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
115 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
116 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) | 0,
117# else
Gilles Peskine39957502021-06-17 23:17:52 +0200118 0,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200119# endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200120 2048,
121};
122
Gilles Peskineffb92da2021-06-02 00:03:26 +0200123/* Next-generation profile. Currently identical to the default, but may
124 * be tightened at any time. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200125const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next = {
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200126 /* Hashes from SHA-256 and above. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
128 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
129 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200130 0xFFFFFFF, /* Any PK alg */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200131# if defined(MBEDTLS_ECP_C)
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200132 /* Curves at or above 128-bit security level. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200133 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
134 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
135 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
136 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
137 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
138 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
139 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
140# else
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200141 0,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200142# endif
Gilles Peskine2c69fa22021-06-02 00:33:33 +0200143 2048,
144};
Gilles Peskineffb92da2021-06-02 00:03:26 +0200145
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200146/*
147 * NSA Suite B Profile
148 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb = {
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200150 /* Only SHA-256 and 384 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200151 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
152 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200153 /* Only ECDSA */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
155 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
156# if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200157 /* Only NIST P-256 and P-384 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200158 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
159 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
160# else
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200161 0,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200162# endif
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200163 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200164};
165
166/*
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200167 * Empty / all-forbidden profile
168 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200169const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none = {
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200170 0,
171 0,
172 0,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173 (uint32_t)-1,
Manuel Pégourié-Gonnard9d4c2c42021-06-18 09:48:27 +0200174};
175
176/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200177 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200178 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200179 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
181 mbedtls_md_type_t md_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200182{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200183 if (md_alg == MBEDTLS_MD_NONE)
184 return -1;
Philippe Antoineb5b25432018-05-11 11:06:29 +0200185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0)
187 return 0;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200188
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200189 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200190}
191
192/*
193 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200194 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
197 mbedtls_pk_type_t pk_alg)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200198{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200199 if (pk_alg == MBEDTLS_PK_NONE)
200 return -1;
Philippe Antoineb5b25432018-05-11 11:06:29 +0200201
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200202 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0)
203 return 0;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200204
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200205 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206}
207
208/*
209 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200210 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
213 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200214{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200215 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200216
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217# if defined(MBEDTLS_RSA_C)
218 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
219 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen)
220 return 0;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200221
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200222 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200223 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200224# endif
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200225
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200226# if defined(MBEDTLS_ECP_C)
227 if (pk_alg == MBEDTLS_PK_ECDSA || pk_alg == MBEDTLS_PK_ECKEY ||
228 pk_alg == MBEDTLS_PK_ECKEY_DH) {
229 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200230
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200231 if (gid == MBEDTLS_ECP_DP_NONE)
232 return -1;
Philippe Antoineb5b25432018-05-11 11:06:29 +0200233
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200234 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0)
235 return 0;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200236
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200237 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200238 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200239# endif
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200240
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200241 return -1;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200242}
243
244/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000245 * Like memcmp, but case-insensitive and always returns -1 if different
246 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200247static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000248{
249 size_t i;
250 unsigned char diff;
251 const unsigned char *n1 = s1, *n2 = s2;
252
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200253 for (i = 0; i < len; i++) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000254 diff = n1[i] ^ n2[i];
255
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200256 if (diff == 0)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000257 continue;
258
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200259 if (diff == 32 && ((n1[i] >= 'a' && n1[i] <= 'z') ||
260 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000261 continue;
262 }
263
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200264 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000265 }
266
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200267 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000268}
269
270/*
271 * Return 0 if name matches wildcard, -1 otherwise
272 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200273static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000274{
275 size_t i;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200276 size_t cn_idx = 0, cn_len = strlen(cn);
Hanno Becker1f8527f2018-11-02 09:19:16 +0000277
278 /* We can't have a match if there is no wildcard to match */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200279 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.')
280 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000281
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200282 for (i = 0; i < cn_len; ++i) {
283 if (cn[i] == '.') {
Hanno Becker1f8527f2018-11-02 09:19:16 +0000284 cn_idx = i;
285 break;
286 }
287 }
288
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200289 if (cn_idx == 0)
290 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000291
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200292 if (cn_len - cn_idx == name->len - 1 &&
293 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
294 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000295 }
296
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200297 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000298}
299
300/*
301 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
302 * variations (but not all).
303 *
304 * Return 0 if equal, -1 otherwise.
305 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200306static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000307{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200308 if (a->tag == b->tag && a->len == b->len &&
309 memcmp(a->p, b->p, b->len) == 0) {
310 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000311 }
312
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200313 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING ||
314 a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
315 (b->tag == MBEDTLS_ASN1_UTF8_STRING ||
316 b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
317 a->len == b->len && x509_memcasecmp(a->p, b->p, b->len) == 0) {
318 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000319 }
320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200321 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000322}
323
324/*
325 * Compare two X.509 Names (aka rdnSequence).
326 *
327 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
328 * we sometimes return unequal when the full algorithm would return equal,
329 * but never the other way. (In particular, we don't do Unicode normalisation
330 * or space folding.)
331 *
332 * Return 0 if equal, -1 otherwise.
333 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
Hanno Becker1f8527f2018-11-02 09:19:16 +0000335{
336 /* Avoid recursion, it might not be optimised by the compiler */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200337 while (a != NULL || b != NULL) {
338 if (a == NULL || b == NULL)
339 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000340
341 /* type */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200342 if (a->oid.tag != b->oid.tag || a->oid.len != b->oid.len ||
343 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
344 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000345 }
346
347 /* value */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200348 if (x509_string_cmp(&a->val, &b->val) != 0)
349 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000350
351 /* structure of the list of sets */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200352 if (a->next_merged != b->next_merged)
353 return -1;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000354
355 a = a->next;
356 b = b->next;
357 }
358
359 /* a == NULL == b */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200360 return 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000361}
362
363/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200364 * Reset (init or clear) a verify_chain
365 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200366static void
367x509_crt_verify_chain_reset(mbedtls_x509_crt_verify_chain *ver_chain)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200368{
369 size_t i;
370
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200371 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200372 ver_chain->items[i].crt = NULL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200373 ver_chain->items[i].flags = (uint32_t)-1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200374 }
375
376 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000377
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200378# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Beckerf53893b2019-03-28 13:45:55 +0000379 ver_chain->trust_ca_cb_result = NULL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200380# endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200381}
382
383/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
385 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200386static int
387x509_get_version(unsigned char **p, const unsigned char *end, int *ver)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388{
Janos Follath865b3eb2019-12-16 11:46:15 +0000389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 size_t len;
391
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200392 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
393 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
394 MBEDTLS_ASN1_CONSTRUCTED | 0)) != 0) {
395 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396 *ver = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200397 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 }
399
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200400 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 }
402
403 end = *p + len;
404
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200405 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0)
406 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200408 if (*p != end)
409 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
410 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200412 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413}
414
415/*
416 * Validity ::= SEQUENCE {
417 * notBefore Time,
418 * notAfter Time }
419 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200420static int x509_get_dates(unsigned char **p,
421 const unsigned char *end,
422 mbedtls_x509_time *from,
423 mbedtls_x509_time *to)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424{
Janos Follath865b3eb2019-12-16 11:46:15 +0000425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 size_t len;
427
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200428 if ((ret = mbedtls_asn1_get_tag(
429 p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
430 0)
431 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 end = *p + len;
434
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200435 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0)
436 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200438 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0)
439 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200441 if (*p != end)
442 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
443 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200445 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446}
447
448/*
449 * X.509 v2/v3 unique identifier (not parsed)
450 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200451static int x509_get_uid(unsigned char **p,
452 const unsigned char *end,
453 mbedtls_x509_buf *uid,
454 int n)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455{
Janos Follath865b3eb2019-12-16 11:46:15 +0000456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200458 if (*p == end)
459 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 uid->tag = **p;
462
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200463 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
464 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
465 MBEDTLS_ASN1_CONSTRUCTED | n)) != 0) {
466 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
467 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200469 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470 }
471
472 uid->p = *p;
473 *p += uid->len;
474
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200475 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476}
477
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200478static int x509_get_basic_constraints(unsigned char **p,
479 const unsigned char *end,
480 int *ca_istrue,
481 int *max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482{
Janos Follath865b3eb2019-12-16 11:46:15 +0000483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 size_t len;
485
486 /*
487 * BasicConstraints ::= SEQUENCE {
488 * cA BOOLEAN DEFAULT FALSE,
489 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
490 */
491 *ca_istrue = 0; /* DEFAULT FALSE */
492 *max_pathlen = 0; /* endless */
493
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200494 if ((ret = mbedtls_asn1_get_tag(
495 p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
496 0)
497 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200499 if (*p == end)
500 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200502 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
503 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
504 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200506 if (ret != 0)
507 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200509 if (*ca_istrue != 0)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510 *ca_istrue = 1;
511 }
512
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200513 if (*p == end)
514 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200516 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0)
517 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200519 if (*p != end)
520 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
521 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
Andrzej Kurek16050742020-04-14 09:49:52 -0400523 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
524 * overflow, which is an undefined behavior. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200525 if (*max_pathlen == INT_MAX)
526 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
527 MBEDTLS_ERR_ASN1_INVALID_LENGTH));
Andrzej Kurek16050742020-04-14 09:49:52 -0400528
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529 (*max_pathlen)++;
530
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200531 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532}
533
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200534static int x509_get_ns_cert_type(unsigned char **p,
535 const unsigned char *end,
536 unsigned char *ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537{
Janos Follath865b3eb2019-12-16 11:46:15 +0000538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200541 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0)
542 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200544 if (bs.len != 1)
545 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
546 MBEDTLS_ERR_ASN1_INVALID_LENGTH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
548 /* Get actual bitstring */
549 *ns_cert_type = *bs.p;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200550 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551}
552
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200553static int x509_get_key_usage(unsigned char **p,
554 const unsigned char *end,
555 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556{
Janos Follath865b3eb2019-12-16 11:46:15 +0000557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200558 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200561 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0)
562 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200564 if (bs.len < 1)
565 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
566 MBEDTLS_ERR_ASN1_INVALID_LENGTH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200569 *key_usage = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200570 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
571 *key_usage |= (unsigned int)bs.p[i] << (8 * i);
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200572 }
573
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200574 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575}
576
577/*
578 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
579 *
580 * KeyPurposeId ::= OBJECT IDENTIFIER
581 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200582static int x509_get_ext_key_usage(unsigned char **p,
583 const unsigned char *end,
584 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585{
Janos Follath865b3eb2019-12-16 11:46:15 +0000586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200588 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage,
589 MBEDTLS_ASN1_OID)) != 0)
590 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
592 /* Sequence length must be >= 1 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200593 if (ext_key_usage->buf.p == NULL)
594 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
595 MBEDTLS_ERR_ASN1_INVALID_LENGTH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200597 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598}
599
600/*
601 * SubjectAltName ::= GeneralNames
602 *
603 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
604 *
605 * GeneralName ::= CHOICE {
606 * otherName [0] OtherName,
607 * rfc822Name [1] IA5String,
608 * dNSName [2] IA5String,
609 * x400Address [3] ORAddress,
610 * directoryName [4] Name,
611 * ediPartyName [5] EDIPartyName,
612 * uniformResourceIdentifier [6] IA5String,
613 * iPAddress [7] OCTET STRING,
614 * registeredID [8] OBJECT IDENTIFIER }
615 *
616 * OtherName ::= SEQUENCE {
617 * type-id OBJECT IDENTIFIER,
618 * value [0] EXPLICIT ANY DEFINED BY type-id }
619 *
620 * EDIPartyName ::= SEQUENCE {
621 * nameAssigner [0] DirectoryString OPTIONAL,
622 * partyName [1] DirectoryString }
623 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300624 * NOTE: we list all types, but only use dNSName and otherName
625 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200627static int x509_get_subject_alt_name(unsigned char **p,
628 const unsigned char *end,
629 mbedtls_x509_sequence *subject_alt_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630{
Janos Follath865b3eb2019-12-16 11:46:15 +0000631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636
637 /* Get main sequence tag */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200638 if ((ret = mbedtls_asn1_get_tag(
639 p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
640 0)
641 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200643 if (*p + len != end)
644 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
645 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200647 while (*p < end) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300648 mbedtls_x509_subject_alternative_name dummy_san_buf;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200649 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
Ron Eldordbbd9662019-05-15 15:46:03 +0300650
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 tag = **p;
652 (*p)++;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200653 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0)
654 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200656 if ((tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
657 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
658 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
659 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG));
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100660 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661
Ron Eldordbbd9662019-05-15 15:46:03 +0300662 /*
irwir74aee1c2019-09-21 18:21:48 +0300663 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300664 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200665 ret = mbedtls_x509_parse_subject_alt_name(&(cur->buf), &dummy_san_buf);
Ron Eldordbbd9662019-05-15 15:46:03 +0300666 /*
667 * In case the extension is malformed, return an error,
668 * and clear the allocated sequences.
669 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200670 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300671 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
672 mbedtls_x509_sequence *seq_prv;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200673 while (seq_cur != NULL) {
Ron Eldordbbd9662019-05-15 15:46:03 +0300674 seq_prv = seq_cur;
675 seq_cur = seq_cur->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200676 mbedtls_platform_zeroize(seq_prv,
677 sizeof(mbedtls_x509_sequence));
678 mbedtls_free(seq_prv);
Ron Eldordbbd9662019-05-15 15:46:03 +0300679 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300680 subject_alt_name->next = NULL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200681 return ret;
Ron Eldordbbd9662019-05-15 15:46:03 +0300682 }
683
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 /* Allocate and assign next pointer */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200685 if (cur->buf.p != NULL) {
686 if (cur->next != NULL)
687 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100688
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200689 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200691 if (cur->next == NULL)
692 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
693 MBEDTLS_ERR_ASN1_ALLOC_FAILED));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 cur = cur->next;
696 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200697
698 buf = &(cur->buf);
699 buf->tag = tag;
700 buf->p = *p;
701 buf->len = tag_len;
702 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 }
704
705 /* Set final sequence entry's next pointer to NULL */
706 cur->next = NULL;
707
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200708 if (*p != end)
709 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
710 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200712 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713}
714
715/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200716 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
717 *
718 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
719 *
720 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
721 *
722 * PolicyInformation ::= SEQUENCE {
723 * policyIdentifier CertPolicyId,
724 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
725 * PolicyQualifierInfo OPTIONAL }
726 *
727 * CertPolicyId ::= OBJECT IDENTIFIER
728 *
729 * PolicyQualifierInfo ::= SEQUENCE {
730 * policyQualifierId PolicyQualifierId,
731 * qualifier ANY DEFINED BY policyQualifierId }
732 *
733 * -- policyQualifierIds for Internet policy qualifiers
734 *
735 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
736 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
737 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
738 *
739 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
740 *
741 * Qualifier ::= CHOICE {
742 * cPSuri CPSuri,
743 * userNotice UserNotice }
744 *
745 * CPSuri ::= IA5String
746 *
747 * UserNotice ::= SEQUENCE {
748 * noticeRef NoticeReference OPTIONAL,
749 * explicitText DisplayText OPTIONAL }
750 *
751 * NoticeReference ::= SEQUENCE {
752 * organization DisplayText,
753 * noticeNumbers SEQUENCE OF INTEGER }
754 *
755 * DisplayText ::= CHOICE {
756 * ia5String IA5String (SIZE (1..200)),
757 * visibleString VisibleString (SIZE (1..200)),
758 * bmpString BMPString (SIZE (1..200)),
759 * utf8String UTF8String (SIZE (1..200)) }
760 *
761 * NOTE: we only parse and use anyPolicy without qualifiers at this point
762 * as defined in RFC 5280.
763 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200764static int
765x509_get_certificate_policies(unsigned char **p,
766 const unsigned char *end,
767 mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +0200768{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300769 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200770 size_t len;
771 mbedtls_asn1_buf *buf;
772 mbedtls_asn1_sequence *cur = certificate_policies;
773
774 /* Get main sequence tag */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200775 ret = mbedtls_asn1_get_tag(
776 p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
777 if (ret != 0)
778 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200779
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200780 if (*p + len != end)
781 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
782 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200783
784 /*
785 * Cannot be an empty sequence.
786 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200787 if (len == 0)
788 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
789 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200790
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200791 while (*p < end) {
Ron Eldor74d9acc2019-03-21 14:00:03 +0200792 mbedtls_x509_buf policy_oid;
793 const unsigned char *policy_end;
794
795 /*
796 * Get the policy sequence
797 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200798 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
799 MBEDTLS_ASN1_CONSTRUCTED |
800 MBEDTLS_ASN1_SEQUENCE)) != 0)
801 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200802
803 policy_end = *p + len;
804
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200805 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
806 MBEDTLS_ASN1_OID)) != 0)
807 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200808
809 policy_oid.tag = MBEDTLS_ASN1_OID;
810 policy_oid.len = len;
811 policy_oid.p = *p;
812
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300813 /*
814 * Only AnyPolicy is currently supported when enforcing policy.
815 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200816 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300817 /*
818 * Set the parsing return code but continue parsing, in case this
TRodziewicz3ecb92e2021-05-11 18:22:05 +0200819 * extension is critical.
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300820 */
821 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
822 }
823
Ron Eldor74d9acc2019-03-21 14:00:03 +0200824 /* Allocate and assign next pointer */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200825 if (cur->buf.p != NULL) {
826 if (cur->next != NULL)
827 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200828
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200829 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200830
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200831 if (cur->next == NULL)
832 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
833 MBEDTLS_ERR_ASN1_ALLOC_FAILED));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200834
835 cur = cur->next;
836 }
837
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200838 buf = &(cur->buf);
Ron Eldor74d9acc2019-03-21 14:00:03 +0200839 buf->tag = policy_oid.tag;
840 buf->p = policy_oid.p;
841 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300842
843 *p += len;
844
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200845 /*
846 * If there is an optional qualifier, then *p < policy_end
847 * Check the Qualifier len to verify it doesn't exceed policy_end.
848 */
849 if (*p < policy_end) {
850 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
851 MBEDTLS_ASN1_CONSTRUCTED |
852 MBEDTLS_ASN1_SEQUENCE)) != 0)
853 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
854 ret);
Ron Eldor08063792019-05-13 16:38:39 +0300855 /*
856 * Skip the optional policy qualifiers.
857 */
858 *p += len;
859 }
860
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200861 if (*p != policy_end)
862 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
863 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200864 }
865
866 /* Set final sequence entry's next pointer to NULL */
867 cur->next = NULL;
868
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200869 if (*p != end)
870 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
871 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Ron Eldor74d9acc2019-03-21 14:00:03 +0200872
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200873 return parse_ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200874}
875
876/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877 * X.509 v3 extensions
878 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200880static int x509_get_crt_ext(unsigned char **p,
881 const unsigned char *end,
882 mbedtls_x509_crt *crt,
883 mbedtls_x509_crt_ext_cb_t cb,
884 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885{
Janos Follath865b3eb2019-12-16 11:46:15 +0000886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200888 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200890 if (*p == end)
891 return 0;
Hanno Becker12f62fb2019-02-12 17:22:36 +0000892
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200893 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0)
894 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895
Hanno Becker12f62fb2019-02-12 17:22:36 +0000896 end = crt->v3_ext.p + crt->v3_ext.len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200897 while (*p < end) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 /*
899 * Extension ::= SEQUENCE {
900 * extnID OBJECT IDENTIFIER,
901 * critical BOOLEAN DEFAULT FALSE,
902 * extnValue OCTET STRING }
903 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200904 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 int is_critical = 0; /* DEFAULT FALSE */
906 int ext_type = 0;
907
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200908 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
909 MBEDTLS_ASN1_CONSTRUCTED |
910 MBEDTLS_ASN1_SEQUENCE)) != 0)
911 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200912
913 end_ext_data = *p + len;
914
915 /* Get extension ID */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200916 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
917 MBEDTLS_ASN1_OID)) != 0)
918 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
k-stachowiak463928a2018-07-24 12:50:59 +0200920 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921 extn_oid.p = *p;
922 *p += extn_oid.len;
923
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924 /* Get optional critical */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200925 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
926 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))
927 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928
929 /* Data should be octet string type */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200930 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
931 MBEDTLS_ASN1_OCTET_STRING)) != 0)
932 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200934 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935 end_ext_octet = *p + len;
936
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200937 if (end_ext_octet != end_ext_data)
938 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
939 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940
941 /*
942 * Detect supported extensions
943 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200944 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200946 if (ret != 0) {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200947 /* Give the callback (if any) a chance to handle the extension */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200948 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;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200952 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200953 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200954 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200955
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 /* No parser found, skip extension */
957 *p = end_ext_octet;
958
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200959 if (is_critical) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 /* Data is marked as critical: fail */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200961 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
962 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 continue;
965 }
966
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100967 /* Forbid repeated extensions */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200968 if ((crt->ext_types & ext_type) != 0)
969 return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100970
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 crt->ext_types |= ext_type;
972
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200973 switch (ext_type) {
974 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
975 /* Parse basic constraints */
976 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
977 &crt->ca_istrue,
978 &crt->max_pathlen)) != 0)
979 return ret;
980 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200982 case MBEDTLS_X509_EXT_KEY_USAGE:
983 /* Parse key usage */
984 if ((ret = x509_get_key_usage(p, end_ext_octet,
985 &crt->key_usage)) != 0)
986 return ret;
987 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200989 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
990 /* Parse extended key usage */
991 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
992 &crt->ext_key_usage)) != 0)
993 return ret;
994 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200996 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
997 /* Parse subject alt name */
998 if ((ret = x509_get_subject_alt_name(
999 p, end_ext_octet, &crt->subject_alt_names)) != 0)
1000 return ret;
1001 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001003 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1004 /* Parse netscape certificate type */
1005 if ((ret = x509_get_ns_cert_type(p, end_ext_octet,
1006 &crt->ns_cert_type)) != 0)
1007 return ret;
1008 break;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001010 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1011 /* Parse certificate policies type */
1012 if ((ret = x509_get_certificate_policies(
1013 p, end_ext_octet, &crt->certificate_policies)) != 0) {
1014 /* Give the callback (if any) a chance to handle the
1015 * extension if it contains unsupported policies */
1016 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE &&
1017 cb != NULL &&
1018 cb(p_ctx, crt, &extn_oid, is_critical, start_ext_octet,
1019 end_ext_octet) == 0)
1020 break;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001021
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001022 if (is_critical)
1023 return ret;
1024 else
1025 /*
1026 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned,
1027 * then we cannot interpret or enforce the policy.
1028 * However, it is up to the user to choose how to
1029 * enforce the policies, unless the extension is
1030 * critical.
1031 */
1032 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE)
1033 return ret;
1034 }
1035 break;
1036
1037 default:
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001038 /*
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001039 * If this is a non-critical extension, which the oid layer
1040 * supports, but there isn't an x509 parser for it,
1041 * skip the extension.
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001042 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001043 if (is_critical)
1044 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1045 else
1046 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001047 }
1048 }
1049
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001050 if (*p != end)
1051 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1052 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001054 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055}
1056
1057/*
1058 * Parse and fill a single X.509 certificate in DER format
1059 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001060static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1061 const unsigned char *buf,
1062 size_t buflen,
1063 int make_copy,
1064 mbedtls_x509_crt_ext_cb_t cb,
1065 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066{
Janos Follath865b3eb2019-12-16 11:46:15 +00001067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001068 size_t len;
1069 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001071
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001072 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1073 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1074 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075
1076 /*
1077 * Check for valid input
1078 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001079 if (crt == NULL || buf == NULL)
1080 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001082 /* Use the original buffer until we figure out actual length. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001083 p = (unsigned char *)buf;
Janos Follathcc0e49d2016-02-17 14:34:12 +00001084 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085 end = p + len;
1086
1087 /*
1088 * Certificate ::= SEQUENCE {
1089 * tbsCertificate TBSCertificate,
1090 * signatureAlgorithm AlgorithmIdentifier,
1091 * signatureValue BIT STRING }
1092 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001093 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1094 MBEDTLS_ASN1_CONSTRUCTED |
1095 MBEDTLS_ASN1_SEQUENCE)) != 0) {
1096 mbedtls_x509_crt_free(crt);
1097 return MBEDTLS_ERR_X509_INVALID_FORMAT;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 }
1099
Janos Follathcc0e49d2016-02-17 14:34:12 +00001100 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001101 crt->raw.len = crt_end - buf;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001102 if (make_copy != 0) {
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001103 /* Create and populate a new buffer for the raw field. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001104 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1105 if (crt->raw.p == NULL)
1106 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001107
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001108 memcpy(crt->raw.p, buf, crt->raw.len);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001109 crt->own_buffer = 1;
1110
1111 p += crt->raw.len - len;
1112 end = crt_end = p + len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001113 } else {
1114 crt->raw.p = (unsigned char *)buf;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001115 crt->own_buffer = 0;
1116 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001117
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118 /*
1119 * TBSCertificate ::= SEQUENCE {
1120 */
1121 crt->tbs.p = p;
1122
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001123 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1124 MBEDTLS_ASN1_CONSTRUCTED |
1125 MBEDTLS_ASN1_SEQUENCE)) != 0) {
1126 mbedtls_x509_crt_free(crt);
1127 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128 }
1129
1130 end = p + len;
1131 crt->tbs.len = end - crt->tbs.p;
1132
1133 /*
1134 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1135 *
1136 * CertificateSerialNumber ::= INTEGER
1137 *
1138 * signature AlgorithmIdentifier
1139 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001140 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1141 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1142 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid, &sig_params1)) !=
1143 0) {
1144 mbedtls_x509_crt_free(crt);
1145 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001146 }
1147
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001148 if (crt->version < 0 || crt->version > 2) {
1149 mbedtls_x509_crt_free(crt);
1150 return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001151 }
1152
Andres AG7ca4a032017-03-09 16:16:11 +00001153 crt->version++;
1154
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001155 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1156 &crt->sig_md, &crt->sig_pk,
1157 &crt->sig_opts)) != 0) {
1158 mbedtls_x509_crt_free(crt);
1159 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 }
1161
1162 /*
1163 * issuer Name
1164 */
1165 crt->issuer_raw.p = p;
1166
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001167 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1168 MBEDTLS_ASN1_CONSTRUCTED |
1169 MBEDTLS_ASN1_SEQUENCE)) != 0) {
1170 mbedtls_x509_crt_free(crt);
1171 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 }
1173
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001174 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1175 mbedtls_x509_crt_free(crt);
1176 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001177 }
1178
1179 crt->issuer_raw.len = p - crt->issuer_raw.p;
1180
1181 /*
1182 * Validity ::= SEQUENCE {
1183 * notBefore Time,
1184 * notAfter Time }
1185 *
1186 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001187 if ((ret = x509_get_dates(&p, end, &crt->valid_from, &crt->valid_to)) !=
1188 0) {
1189 mbedtls_x509_crt_free(crt);
1190 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 }
1192
1193 /*
1194 * subject Name
1195 */
1196 crt->subject_raw.p = p;
1197
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001198 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1199 MBEDTLS_ASN1_CONSTRUCTED |
1200 MBEDTLS_ASN1_SEQUENCE)) != 0) {
1201 mbedtls_x509_crt_free(crt);
1202 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 }
1204
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001205 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1206 mbedtls_x509_crt_free(crt);
1207 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208 }
1209
1210 crt->subject_raw.len = p - crt->subject_raw.p;
1211
1212 /*
1213 * SubjectPublicKeyInfo
1214 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001215 crt->pk_raw.p = p;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001216 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1217 mbedtls_x509_crt_free(crt);
1218 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001220 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221
1222 /*
1223 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1224 * -- If present, version shall be v2 or v3
1225 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1226 * -- If present, version shall be v2 or v3
1227 * extensions [3] EXPLICIT Extensions OPTIONAL
1228 * -- If present, version shall be v3
1229 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001230 if (crt->version == 2 || crt->version == 3) {
1231 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1232 if (ret != 0) {
1233 mbedtls_x509_crt_free(crt);
1234 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 }
1236 }
1237
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001238 if (crt->version == 2 || crt->version == 3) {
1239 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1240 if (ret != 0) {
1241 mbedtls_x509_crt_free(crt);
1242 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 }
1244 }
1245
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001246 if (crt->version == 3) {
1247 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1248 if (ret != 0) {
1249 mbedtls_x509_crt_free(crt);
1250 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251 }
1252 }
1253
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001254 if (p != end) {
1255 mbedtls_x509_crt_free(crt);
1256 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1257 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258 }
1259
1260 end = crt_end;
1261
1262 /*
1263 * }
1264 * -- end of TBSCertificate
1265 *
1266 * signatureAlgorithm AlgorithmIdentifier,
1267 * signatureValue BIT STRING
1268 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001269 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1270 mbedtls_x509_crt_free(crt);
1271 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 }
1273
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001274 if (crt->sig_oid.len != sig_oid2.len ||
1275 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001276 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001277 sig_params1.len != sig_params2.len ||
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001278 (sig_params1.len != 0 &&
1279 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1280 mbedtls_x509_crt_free(crt);
1281 return MBEDTLS_ERR_X509_SIG_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 }
1283
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001284 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1285 mbedtls_x509_crt_free(crt);
1286 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001287 }
1288
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001289 if (p != end) {
1290 mbedtls_x509_crt_free(crt);
1291 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1292 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 }
1294
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001295 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296}
1297
1298/*
1299 * Parse one X.509 certificate in DER format from a buffer and add them to a
1300 * chained list
1301 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001302static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1303 const unsigned char *buf,
1304 size_t buflen,
1305 int make_copy,
1306 mbedtls_x509_crt_ext_cb_t cb,
1307 void *p_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001308{
Janos Follath865b3eb2019-12-16 11:46:15 +00001309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311
1312 /*
1313 * Check for valid input
1314 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001315 if (crt == NULL || buf == NULL)
1316 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001318 while (crt->version != 0 && crt->next != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 prev = crt;
1320 crt = crt->next;
1321 }
1322
1323 /*
1324 * Add new certificate on the end of the chain if needed.
1325 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001326 if (crt->version != 0 && crt->next == NULL) {
1327 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001329 if (crt->next == NULL)
1330 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001331
1332 prev = crt;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001333 mbedtls_x509_crt_init(crt->next);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335 }
1336
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001337 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1338 if (ret != 0) {
1339 if (prev)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340 prev->next = NULL;
1341
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001342 if (crt != chain)
1343 mbedtls_free(crt);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001345 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346 }
1347
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001348 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349}
1350
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001351int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1352 const unsigned char *buf,
1353 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001354{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001355 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL,
1356 NULL);
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001357}
1358
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001359int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1360 const unsigned char *buf,
1361 size_t buflen,
1362 int make_copy,
1363 mbedtls_x509_crt_ext_cb_t cb,
1364 void *p_ctx)
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001365{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001366 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy,
1367 cb, p_ctx);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001368}
1369
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001370int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1371 const unsigned char *buf,
1372 size_t buflen)
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001373{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001374 return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL,
1375 NULL);
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001376}
1377
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001379 * Parse one or more PEM certificates from a buffer and add them to the chained
1380 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001382int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1383 const unsigned char *buf,
1384 size_t buflen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001386# if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001387 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 int buf_format = MBEDTLS_X509_FORMAT_DER;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001389# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001390
1391 /*
1392 * Check for valid input
1393 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001394 if (chain == NULL || buf == NULL)
1395 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001396
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001397 /*
1398 * Determine buffer content. Buffer contains either one DER certificate
1399 * or one or more PEM certificates.
1400 */
1401# if defined(MBEDTLS_PEM_PARSE_C)
1402 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1403 strstr((const char *)buf, "-----BEGIN CERTIFICATE-----") != NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001405 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001406
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001407 if (buf_format == MBEDTLS_X509_FORMAT_DER)
1408 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1409# else
1410 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1411# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001413# if defined(MBEDTLS_PEM_PARSE_C)
1414 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001418 /* 1 rather than 0 since the terminating NULL byte is counted in */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001419 while (buflen > 1) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420 size_t use_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001421 mbedtls_pem_init(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001423 /* If we get there, we know the string is null-terminated */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001424 ret = mbedtls_pem_read_buffer(&pem, "-----BEGIN CERTIFICATE-----",
1425 "-----END CERTIFICATE-----", buf,
1426 NULL, 0, &use_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001427
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001428 if (ret == 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429 /*
1430 * Was PEM encoded
1431 */
1432 buflen -= use_len;
1433 buf += use_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001434 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1435 return ret;
1436 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1437 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001438
1439 /*
1440 * PEM header and footer were found
1441 */
1442 buflen -= use_len;
1443 buf += use_len;
1444
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001445 if (first_error == 0)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446 first_error = ret;
1447
Paul Bakker5a5fa922014-09-26 14:53:04 +02001448 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449 continue;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001450 } else
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 break;
1452
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001453 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001455 mbedtls_pem_free(&pem);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001457 if (ret != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458 /*
1459 * Quit parsing on a memory error
1460 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001461 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED)
1462 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001464 if (first_error == 0)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001465 first_error = ret;
1466
1467 total_failed++;
1468 continue;
1469 }
1470
1471 success = 1;
1472 }
1473 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001475 if (success)
1476 return total_failed;
1477 else if (first_error)
1478 return first_error;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001480 return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1481# endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482}
1483
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001484# if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485/*
1486 * Load one or more certificates and add them to the chained list
1487 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001488int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001489{
Janos Follath865b3eb2019-12-16 11:46:15 +00001490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491 size_t n;
1492 unsigned char *buf;
1493
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001494 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0)
1495 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001496
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001497 ret = mbedtls_x509_crt_parse(chain, buf, n);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001498
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001499 mbedtls_platform_zeroize(buf, n);
1500 mbedtls_free(buf);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001502 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503}
1504
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001505int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001506{
1507 int ret = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001508# if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509 int w_ret;
1510 WCHAR szDir[MAX_PATH];
1511 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001512 char *p;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001513 size_t len = strlen(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
Paul Bakker9af723c2014-05-01 13:03:14 +02001515 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 HANDLE hFind;
1517
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001518 if (len > MAX_PATH - 3)
1519 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001521 memset(szDir, 0, sizeof(szDir));
1522 memset(filename, 0, MAX_PATH);
1523 memcpy(filename, path, len);
Paul Bakker9af723c2014-05-01 13:03:14 +02001524 filename[len++] = '\\';
1525 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 filename[len++] = '*';
1527
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001528 w_ret =
1529 MultiByteToWideChar(CP_ACP, 0, filename, (int)len, szDir, MAX_PATH - 3);
1530 if (w_ret == 0)
1531 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001533 hFind = FindFirstFileW(szDir, &file_data);
1534 if (hFind == INVALID_HANDLE_VALUE)
1535 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536
1537 len = MAX_PATH - len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001538 do {
1539 memset(p, 0, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001541 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542 continue;
1543
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001544 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1545 lstrlenW(file_data.cFileName), p,
1546 (int)len - 1, NULL, NULL);
1547 if (w_ret == 0) {
Ron Eldor36d90422017-01-09 15:09:16 +02001548 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1549 goto cleanup;
1550 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001552 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1553 if (w_ret < 0)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554 ret++;
1555 else
1556 ret += w_ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001557 } while (FindNextFileW(hFind, &file_data) != 0);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001559 if (GetLastError() != ERROR_NO_MORE_FILES)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
Ron Eldor36d90422017-01-09 15:09:16 +02001562cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001563 FindClose(hFind);
1564# else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001565 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001566 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001568 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001569 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001570 DIR *dir = opendir(path);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001572 if (dir == NULL)
1573 return MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001575# if defined(MBEDTLS_THREADING_C)
1576 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1577 closedir(dir);
1578 return ret;
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001579 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001580# endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001581
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001582 memset(&sb, 0, sizeof(sb));
Paul Elliottfb91a482021-03-05 14:17:51 +00001583
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001584 while ((entry = readdir(dir)) != NULL) {
1585 snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name), "%s/%s",
1586 path, entry->d_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001588 if (snp_ret < 0 || (size_t)snp_ret >= sizeof(entry_name)) {
Andres AGf9113192016-09-02 14:06:04 +01001589 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1590 goto cleanup;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001591 } else if (stat(entry_name, &sb) == -1) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001593 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 }
1595
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001596 if (!S_ISREG(sb.st_mode))
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597 continue;
1598
1599 // Ignore parse errors
1600 //
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001601 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1602 if (t_ret < 0)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603 ret++;
1604 else
1605 ret += t_ret;
1606 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001607
1608cleanup:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001609 closedir(dir);
Andres AGf9113192016-09-02 14:06:04 +01001610
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001611# if defined(MBEDTLS_THREADING_C)
1612 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001614# endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001615
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001616# endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001618 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001620# endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001622/*
1623 * OtherName ::= SEQUENCE {
1624 * type-id OBJECT IDENTIFIER,
1625 * value [0] EXPLICIT ANY DEFINED BY type-id }
1626 *
1627 * HardwareModuleName ::= SEQUENCE {
1628 * hwType OBJECT IDENTIFIER,
1629 * hwSerialNum OCTET STRING }
1630 *
1631 * NOTE: we currently only parse and use otherName of type HwModuleName,
1632 * as defined in RFC 4108.
1633 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001634static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1635 mbedtls_x509_san_other_name *other_name)
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001636{
Janos Follathab23cd12019-05-09 13:53:57 +01001637 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001638 size_t len;
1639 unsigned char *p = subject_alt_name->p;
1640 const unsigned char *end = p + subject_alt_name->len;
1641 mbedtls_x509_buf cur_oid;
1642
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001643 if ((subject_alt_name->tag &
1644 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1645 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001646 /*
1647 * The given subject alternative name is not of type "othername".
1648 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001649 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001650 }
1651
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001652 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0)
1653 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001654
1655 cur_oid.tag = MBEDTLS_ASN1_OID;
1656 cur_oid.p = p;
1657 cur_oid.len = len;
1658
1659 /*
1660 * Only HwModuleName is currently supported.
1661 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001662 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1663 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001664 }
1665
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001666 if (p + len >= end) {
1667 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1668 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1669 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Ron Eldor890819a2019-05-13 19:03:04 +03001670 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001671 p += len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001672 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1673 MBEDTLS_ASN1_CONSTRUCTED |
1674 MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0)
1675 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001676
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001677 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1678 MBEDTLS_ASN1_CONSTRUCTED |
1679 MBEDTLS_ASN1_SEQUENCE)) != 0)
1680 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001681
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001682 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0)
1683 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001684
1685 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1686 other_name->value.hardware_module_name.oid.p = p;
1687 other_name->value.hardware_module_name.oid.len = len;
1688
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001689 if (p + len >= end) {
1690 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1691 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1692 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Ron Eldor890819a2019-05-13 19:03:04 +03001693 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001694 p += len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001695 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1696 MBEDTLS_ASN1_OCTET_STRING)) != 0)
1697 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001698
1699 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1700 other_name->value.hardware_module_name.val.p = p;
1701 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001702 p += len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001703 if (p != end) {
1704 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1705 return (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1706 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH));
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001707 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001708 return 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001709}
1710
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001711int mbedtls_x509_parse_subject_alt_name(
1712 const mbedtls_x509_buf *san_buf,
1713 mbedtls_x509_subject_alternative_name *san)
Peter Kolbus9a969b62018-12-11 13:55:56 -06001714{
1715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001716 switch (san_buf->tag &
1717 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) {
Peter Kolbus9a969b62018-12-11 13:55:56 -06001718 /*
1719 * otherName
1720 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001721 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
1722 {
1723 mbedtls_x509_san_other_name other_name;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001724
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001725 ret = x509_get_other_name(san_buf, &other_name);
1726 if (ret != 0)
1727 return ret;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001728
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001729 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1730 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1731 memcpy(&san->san.other_name, &other_name, sizeof(other_name));
1732 }
1733 break;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001734
1735 /*
1736 * dNSName
1737 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001738 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
1739 {
1740 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1741 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001742
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001743 memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
1744 }
1745 break;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001746
1747 /*
1748 * Type not supported
1749 */
1750 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001751 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001752 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001753 return 0;
Peter Kolbus9a969b62018-12-11 13:55:56 -06001754}
1755
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001756# if !defined(MBEDTLS_X509_REMOVE_INFO)
1757static int
1758x509_info_subject_alt_name(char **buf,
1759 size_t *size,
1760 const mbedtls_x509_sequence *subject_alt_name,
1761 const char *prefix)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001762{
Janos Follath865b3eb2019-12-16 11:46:15 +00001763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001764 size_t n = *size;
1765 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001767 mbedtls_x509_subject_alternative_name san;
1768 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001769
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001770 while (cur != NULL) {
1771 memset(&san, 0, sizeof(san));
1772 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1773 if (parse_ret != 0) {
1774 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1775 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001776 MBEDTLS_X509_SAFE_SNPRINTF;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001777 } else {
1778 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
Ron Eldor890819a2019-05-13 19:03:04 +03001779 MBEDTLS_X509_SAFE_SNPRINTF;
1780 }
1781 cur = cur->next;
1782 continue;
1783 }
1784
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001785 switch (san.type) {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001786 /*
1787 * otherName
1788 */
Ron Eldora2913912019-05-16 16:17:38 +03001789 case MBEDTLS_X509_SAN_OTHER_NAME:
Janos Follath22f605f2019-05-10 10:37:17 +01001790 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001791 mbedtls_x509_san_other_name *other_name =
1792 &san.san.other_name;
1793
1794 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001795 MBEDTLS_X509_SAFE_SNPRINTF;
1796
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001797 if (MBEDTLS_OID_CMP(
1798 MBEDTLS_OID_ON_HW_MODULE_NAME,
1799 &other_name->value.hardware_module_name.oid) != 0) {
1800 ret = mbedtls_snprintf(
1801 p, n, "\n%s hardware module name :", prefix);
1802 MBEDTLS_X509_SAFE_SNPRINTF;
1803 ret = mbedtls_snprintf(
1804 p, n, "\n%s hardware type : ",
1805 prefix);
1806 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001807
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001808 ret = mbedtls_oid_get_numeric_string(
1809 p, n, &other_name->value.hardware_module_name.oid);
1810 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001811
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001812 ret = mbedtls_snprintf(
1813 p, n, "\n%s hardware serial number : ",
1814 prefix);
1815 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath22f605f2019-05-10 10:37:17 +01001816
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001817 if (other_name->value.hardware_module_name.val.len >=
1818 n) {
1819 *p = '\0';
1820 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1821 }
Ron Eldora2913912019-05-16 16:17:38 +03001822
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001823 memcpy(p, other_name->value.hardware_module_name.val.p,
1824 other_name->value.hardware_module_name.val.len);
1825 p += other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001826
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001827 n -= other_name->value.hardware_module_name.val.len;
1828
1829 } /* MBEDTLS_OID_ON_HW_MODULE_NAME */
1830 }
1831 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001832
1833 /*
1834 * dNSName
1835 */
Ron Eldora2913912019-05-16 16:17:38 +03001836 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001837 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001838 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
1839 MBEDTLS_X509_SAFE_SNPRINTF;
1840 if (san.san.unstructured_name.len >= n) {
1841 *p = '\0';
1842 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1843 }
Ron Eldora2913912019-05-16 16:17:38 +03001844
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001845 memcpy(p, san.san.unstructured_name.p,
1846 san.san.unstructured_name.len);
1847 p += san.san.unstructured_name.len;
1848 n -= san.san.unstructured_name.len;
1849 }
1850 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001851
1852 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001853 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001854 */
1855 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001856 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
Janos Follath22f605f2019-05-10 10:37:17 +01001857 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001858 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001859 }
1860
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001861 cur = cur->next;
1862 }
1863
1864 *p = '\0';
1865
1866 *size = n;
1867 *buf = p;
1868
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001869 return 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001870}
1871
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001872# define PRINT_ITEM(i) \
1873 { \
1874 ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1875 MBEDTLS_X509_SAFE_SNPRINTF; \
1876 sep = ", "; \
1877 }
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001878
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001879# define CERT_TYPE(type, name) \
1880 if (ns_cert_type & (type)) \
1881 PRINT_ITEM(name);
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001882
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001883static int
1884x509_info_cert_type(char **buf, size_t *size, unsigned char ns_cert_type)
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001885{
Janos Follath865b3eb2019-12-16 11:46:15 +00001886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001887 size_t n = *size;
1888 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001889 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001890
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001891 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1892 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1893 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1894 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1895 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1896 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1897 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1898 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001899
1900 *size = n;
1901 *buf = p;
1902
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001903 return 0;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001904}
1905
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001906# define KEY_USAGE(code, name) \
1907 if (key_usage & (code)) \
1908 PRINT_ITEM(name);
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001909
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001910static int x509_info_key_usage(char **buf, size_t *size, unsigned int key_usage)
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001911{
Janos Follath865b3eb2019-12-16 11:46:15 +00001912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001913 size_t n = *size;
1914 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001915 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001916
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001917 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1918 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1919 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1920 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1921 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1922 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1923 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1924 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1925 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001926
1927 *size = n;
1928 *buf = p;
1929
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001930 return 0;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001931}
1932
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001933static int
1934x509_info_ext_key_usage(char **buf,
1935 size_t *size,
1936 const mbedtls_x509_sequence *extended_key_usage)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001937{
Janos Follath865b3eb2019-12-16 11:46:15 +00001938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001939 const char *desc;
1940 size_t n = *size;
1941 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001943 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001944
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001945 while (cur != NULL) {
1946 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0)
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001947 desc = "???";
1948
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001949 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001950 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001951
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001952 sep = ", ";
1953
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001954 cur = cur->next;
1955 }
1956
1957 *size = n;
1958 *buf = p;
1959
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001960 return 0;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001961}
1962
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001963static int
1964x509_info_cert_policies(char **buf,
1965 size_t *size,
1966 const mbedtls_x509_sequence *certificate_policies)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001967{
Janos Follath865b3eb2019-12-16 11:46:15 +00001968 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001969 const char *desc;
1970 size_t n = *size;
1971 char *p = *buf;
1972 const mbedtls_x509_sequence *cur = certificate_policies;
1973 const char *sep = "";
1974
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001975 while (cur != NULL) {
1976 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0)
Ron Eldor74d9acc2019-03-21 14:00:03 +02001977 desc = "???";
1978
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001979 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
Ron Eldor74d9acc2019-03-21 14:00:03 +02001980 MBEDTLS_X509_SAFE_SNPRINTF;
1981
1982 sep = ", ";
1983
1984 cur = cur->next;
1985 }
1986
1987 *size = n;
1988 *buf = p;
1989
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001990 return 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +02001991}
1992
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001993/*
1994 * Return an informational string about the certificate.
1995 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001996# define BEFORE_COLON 18
1997# define BC "18"
1998int mbedtls_x509_crt_info(char *buf,
1999 size_t size,
2000 const char *prefix,
2001 const mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002002{
Janos Follath865b3eb2019-12-16 11:46:15 +00002003 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002004 size_t n;
2005 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002006 char key_size_str[BEFORE_COLON];
2007
2008 p = buf;
2009 n = size;
2010
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002011 if (NULL == crt) {
2012 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
Janos Follath98e28a72016-05-31 14:03:54 +01002013 MBEDTLS_X509_SAFE_SNPRINTF;
2014
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002015 return ((int)(size - n));
Janos Follath98e28a72016-05-31 14:03:54 +01002016 }
2017
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002018 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n", prefix,
2019 crt->version);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002020 MBEDTLS_X509_SAFE_SNPRINTF;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002021 ret = mbedtls_snprintf(p, n, "%sserial number : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002022 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002024 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002025 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002026
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002027 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002028 MBEDTLS_X509_SAFE_SNPRINTF;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002029 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002030 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002031
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002032 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002033 MBEDTLS_X509_SAFE_SNPRINTF;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002034 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002035 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002036
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002037 ret = mbedtls_snprintf(p, n,
2038 "\n%sissued on : "
2039 "%04d-%02d-%02d %02d:%02d:%02d",
2040 prefix, crt->valid_from.year, crt->valid_from.mon,
2041 crt->valid_from.day, crt->valid_from.hour,
2042 crt->valid_from.min, crt->valid_from.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002043 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002045 ret = mbedtls_snprintf(p, n,
2046 "\n%sexpires on : "
2047 "%04d-%02d-%02d %02d:%02d:%02d",
2048 prefix, crt->valid_to.year, crt->valid_to.mon,
2049 crt->valid_to.day, crt->valid_to.hour,
2050 crt->valid_to.min, crt->valid_to.sec);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002051 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002052
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002053 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002054 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002055
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002056 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
2057 crt->sig_md, crt->sig_opts);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002058 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002060 /* Key size */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002061 if ((ret = mbedtls_x509_key_size_helper(
2062 key_size_str, BEFORE_COLON, mbedtls_pk_get_name(&crt->pk))) != 0) {
2063 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002064 }
2065
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002066 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
2067 (int)mbedtls_pk_get_bitlen(&crt->pk));
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002068 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002069
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002070 /*
2071 * Optional extensions
2072 */
2073
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002074 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
2075 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
2076 crt->ca_istrue ? "true" : "false");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002077 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002078
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002079 if (crt->max_pathlen > 0) {
2080 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d",
2081 crt->max_pathlen - 1);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002082 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002083 }
2084 }
2085
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002086 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2087 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002088 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002089
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002090 if ((ret = x509_info_subject_alt_name(&p, &n, &crt->subject_alt_names,
2091 prefix)) != 0)
2092 return ret;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002093 }
2094
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002095 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
2096 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002097 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002098
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002099 if ((ret = x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0)
2100 return ret;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002101 }
2102
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002103 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2104 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002105 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002106
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002107 if ((ret = x509_info_key_usage(&p, &n, crt->key_usage)) != 0)
2108 return ret;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002109 }
2110
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002111 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2112 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002113 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002114
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002115 if ((ret = x509_info_ext_key_usage(&p, &n, &crt->ext_key_usage)) != 0)
2116 return ret;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002117 }
2118
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002119 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2120 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
Ron Eldor74d9acc2019-03-21 14:00:03 +02002121 MBEDTLS_X509_SAFE_SNPRINTF;
2122
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002123 if ((ret = x509_info_cert_policies(&p, &n,
2124 &crt->certificate_policies)) != 0)
2125 return ret;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002126 }
2127
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002128 ret = mbedtls_snprintf(p, n, "\n");
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002129 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002130
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002131 return ((int)(size - n));
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132}
2133
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002134struct x509_crt_verify_string {
2135 int code;
2136 const char *string;
2137};
2138
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002139# define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002140static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002141 MBEDTLS_X509_CRT_ERROR_INFO_LIST{ 0, NULL }
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002142};
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002143# undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002144
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002145int mbedtls_x509_crt_verify_info(char *buf,
2146 size_t size,
2147 const char *prefix,
2148 uint32_t flags)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002149{
Janos Follath865b3eb2019-12-16 11:46:15 +00002150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002151 const struct x509_crt_verify_string *cur;
2152 char *p = buf;
2153 size_t n = size;
2154
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002155 for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
2156 if ((flags & cur->code) == 0)
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002157 continue;
2158
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002159 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002160 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002161 flags ^= cur->code;
2162 }
2163
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002164 if (flags != 0) {
2165 ret = mbedtls_snprintf(p, n,
2166 "%sUnknown reason "
2167 "(this should not happen)\n",
2168 prefix);
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002169 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002170 }
2171
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002172 return ((int)(size - n));
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002173}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002174# endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002175
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002176int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2177 unsigned int usage)
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002178{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002179 unsigned int usage_must, usage_may;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002180 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY |
2181 MBEDTLS_X509_KU_DECIPHER_ONLY;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002182
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002183 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0)
2184 return 0;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002185
2186 usage_must = usage & ~may_mask;
2187
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002188 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must)
2189 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002190
2191 usage_may = usage & may_mask;
2192
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002193 if (((crt->key_usage & may_mask) | usage_may) != usage_may)
2194 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002195
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002196 return 0;
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002197}
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002198
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002199int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2200 const char *usage_oid,
2201 size_t usage_len)
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002204
2205 /* Extension is not mandatory, absent means no restriction */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002206 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0)
2207 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002208
2209 /*
2210 * Look for the requested usage (or wildcard ANY) in our list
2211 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002212 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002214
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002215 if (cur_oid->len == usage_len &&
2216 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2217 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002218 }
2219
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002220 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0)
2221 return 0;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002222 }
2223
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002224 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002225}
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002226
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002227# if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002228/*
2229 * Return 1 if the certificate is revoked, or 0 otherwise.
2230 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002231int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt,
2232 const mbedtls_x509_crl *crl)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002233{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002235
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002236 while (cur != NULL && cur->serial.len != 0) {
2237 if (crt->serial.len == cur->serial.len &&
2238 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2239 return 1;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002240 }
2241
2242 cur = cur->next;
2243 }
2244
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002245 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002246}
2247
2248/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002249 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002250 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002251 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002252static int x509_crt_verifycrl(mbedtls_x509_crt *crt,
2253 mbedtls_x509_crt *ca,
2254 mbedtls_x509_crl *crl_list,
2255 const mbedtls_x509_crt_profile *profile)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002256{
2257 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2259 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002260
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002261 if (ca == NULL)
2262 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002263
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002264 while (crl_list != NULL) {
2265 if (crl_list->version == 0 ||
2266 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002267 crl_list = crl_list->next;
2268 continue;
2269 }
2270
2271 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002272 * Check if the CA is configured to sign CRLs
2273 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002274 if (mbedtls_x509_crt_check_key_usage(ca, MBEDTLS_X509_KU_CRL_SIGN) !=
2275 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002277 break;
2278 }
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002279
2280 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002281 * Check if CRL is correctly signed by the trusted CA
2282 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002283 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002284 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2285
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002286 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002287 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2288
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002289 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2290 if (mbedtls_md(md_info, crl_list->tbs.p, crl_list->tbs.len, hash) !=
2291 0) {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002292 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002294 break;
2295 }
2296
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002297 if (x509_profile_check_key(profile, &ca->pk) != 0)
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002298 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002299
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002300 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2301 crl_list->sig_md, hash,
2302 mbedtls_md_get_size(md_info), crl_list->sig.p,
2303 crl_list->sig.len) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305 break;
2306 }
2307
2308 /*
2309 * Check for validity of CRL (Do not drop out)
2310 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002311 if (mbedtls_x509_time_is_past(&crl_list->next_update))
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002313
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002314 if (mbedtls_x509_time_is_future(&crl_list->this_update))
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002315 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002316
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317 /*
2318 * Check if certificate is revoked
2319 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002320 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002321 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002322 break;
2323 }
2324
2325 crl_list = crl_list->next;
2326 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002327
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002328 return flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002330# endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002331
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002332/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002333 * Check the signature of a certificate by its parent
2334 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002335static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2336 mbedtls_x509_crt *parent,
2337 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002338{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002339 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002340 size_t hash_len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002341# if !defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002342 const mbedtls_md_info_t *md_info;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002343 md_info = mbedtls_md_info_from_type(child->sig_md);
2344 hash_len = mbedtls_md_get_size(md_info);
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002345
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002346 /* Note: hash errors can happen only after an internal error */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002347 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0)
2348 return -1;
2349# else
Jaeden Amero34973232019-02-20 10:32:28 +00002350 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002351 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(child->sig_md);
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002352
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002353 if (psa_hash_setup(&hash_operation, hash_alg) != PSA_SUCCESS)
2354 return -1;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002355
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002356 if (psa_hash_update(&hash_operation, child->tbs.p, child->tbs.len) !=
2357 PSA_SUCCESS) {
2358 return -1;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002359 }
2360
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002361 if (psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_len) !=
2362 PSA_SUCCESS) {
2363 return -1;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002364 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002365# endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002366 /* Skip expensive computation on obvious mismatch */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002367 if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk))
2368 return -1;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002369
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002370# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2371 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2372 return (mbedtls_pk_verify_restartable(&parent->pk, child->sig_md, hash,
2373 hash_len, child->sig.p,
2374 child->sig.len, &rs_ctx->pk));
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002375 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002376# else
2377 (void)rs_ctx;
2378# endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002379
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002380 return (mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2381 child->sig_md, hash, hash_len, child->sig.p,
2382 child->sig.len));
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002383}
2384
2385/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002386 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2387 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002388 *
2389 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002390 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002391static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2392 const mbedtls_x509_crt *parent,
2393 int top)
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002394{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002395 int need_ca_bit;
2396
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002397 /* Parent must be the issuer */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002398 if (x509_name_cmp(&child->issuer, &parent->subject) != 0)
2399 return -1;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002400
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002401 /* Parent must have the basicConstraints CA bit set as a general rule */
2402 need_ca_bit = 1;
2403
2404 /* Exception: v1/v2 certificates that are locally trusted. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002405 if (top && parent->version < 3)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002406 need_ca_bit = 0;
2407
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002408 if (need_ca_bit && !parent->ca_istrue)
2409 return -1;
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002410
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002411 if (need_ca_bit && mbedtls_x509_crt_check_key_usage(
2412 parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2413 return -1;
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002414 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002415
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002416 return 0;
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002417}
2418
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002419/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002420 * Find a suitable parent for child in candidates, or return NULL.
2421 *
2422 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002423 * 1. subject name matches child's issuer
2424 * 2. if necessary, the CA bit is set and key usage allows signing certs
2425 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002426 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002427 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002428 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002429 * If there's a suitable candidate which is also time-valid, return the first
2430 * such. Otherwise, return the first suitable candidate (or NULL if there is
2431 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002432 *
2433 * The rationale for this rule is that someone could have a list of trusted
2434 * roots with two versions on the same root with different validity periods.
2435 * (At least one user reported having such a list and wanted it to just work.)
2436 * The reason we don't just require time-validity is that generally there is
2437 * only one version, and if it's expired we want the flags to state that
2438 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002439 *
2440 * The rationale for rule 3 (signature for trusted roots) is that users might
2441 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002442 * way we select the correct one is by checking the signature (as we don't
2443 * rely on key identifier extensions). (This is one way users might choose to
2444 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002445 *
2446 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002447 * - [in] child: certificate for which we're looking for a parent
2448 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002449 * - [out] r_parent: parent found (or NULL)
2450 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002451 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2452 * of the chain, 0 otherwise
2453 * - [in] path_cnt: number of intermediates seen so far
2454 * - [in] self_cnt: number of self-signed intermediates seen so far
2455 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002456 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002457 *
2458 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002459 * - 0 on success
2460 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002461 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002462static int x509_crt_find_parent_in(mbedtls_x509_crt *child,
2463 mbedtls_x509_crt *candidates,
2464 mbedtls_x509_crt **r_parent,
2465 int *r_signature_is_good,
2466 int top,
2467 unsigned path_cnt,
2468 unsigned self_cnt,
2469 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002470{
Janos Follath865b3eb2019-12-16 11:46:15 +00002471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002472 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002473 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002474
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002475# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002476 /* did we have something in progress? */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002477 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002478 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002479 parent = rs_ctx->parent;
2480 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002481 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002482
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002483 /* clear saved state */
2484 rs_ctx->parent = NULL;
2485 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002486 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002487
2488 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002489 goto check_signature;
2490 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002491# endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002492
2493 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002494 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002495
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002496 for (parent = candidates; parent != NULL; parent = parent->next) {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002497 /* basic parenting skills (name, CA bit, key usage) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002498 if (x509_crt_check_parent(child, parent, top) != 0)
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002499 continue;
2500
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002501 /* +1 because stored max_pathlen is 1 higher that the actual value */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002502 if (parent->max_pathlen > 0 &&
2503 (size_t)parent->max_pathlen < 1 + path_cnt - self_cnt) {
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002504 continue;
2505 }
2506
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002507 /* Signature */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002508# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002509check_signature:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002510# endif
2511 ret = x509_crt_check_signature(child, parent, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002512
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002513# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2514 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002515 /* save state */
2516 rs_ctx->parent = parent;
2517 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002518 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002519
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002520 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002521 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002522# else
2523 (void)ret;
2524# endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002525
2526 signature_is_good = ret == 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002527 if (top && !signature_is_good)
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002528 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002529
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002530 /* optional time check */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002531 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2532 mbedtls_x509_time_is_future(&parent->valid_from)) {
2533 if (fallback_parent == NULL) {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002534 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002535 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002536 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002537
2538 continue;
2539 }
2540
Andy Gross1f627142019-01-30 10:25:53 -06002541 *r_parent = parent;
2542 *r_signature_is_good = signature_is_good;
2543
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002544 break;
2545 }
2546
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002547 if (parent == NULL) {
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002548 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002549 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002550 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002551
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002552 return 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002553}
2554
2555/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002556 * Find a parent in trusted CAs or the provided chain, or return NULL.
2557 *
2558 * Searches in trusted CAs first, and return the first suitable parent found
2559 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002560 *
2561 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002562 * - [in] child: certificate for which we're looking for a parent, followed
2563 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002564 * - [in] trust_ca: list of locally trusted certificates
2565 * - [out] parent: parent found (or NULL)
2566 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2567 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2568 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2569 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002570 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002571 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002572 *
2573 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002574 * - 0 on success
2575 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002576 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002577static int x509_crt_find_parent(mbedtls_x509_crt *child,
2578 mbedtls_x509_crt *trust_ca,
2579 mbedtls_x509_crt **parent,
2580 int *parent_is_trusted,
2581 int *signature_is_good,
2582 unsigned path_cnt,
2583 unsigned self_cnt,
2584 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002585{
Janos Follath865b3eb2019-12-16 11:46:15 +00002586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002587 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002588
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002589 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002590
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002591# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002592 /* restore then clear saved state if we have some stored */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002593 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002594 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002595 rs_ctx->parent_is_trusted = -1;
2596 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002597# endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002598
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002599 while (1) {
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002600 search_list = *parent_is_trusted ? trust_ca : child->next;
2601
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002602 ret = x509_crt_find_parent_in(child, search_list, parent,
2603 signature_is_good, *parent_is_trusted,
2604 path_cnt, self_cnt, rs_ctx);
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002605
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002606# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2607 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002608 /* save state */
2609 rs_ctx->parent_is_trusted = *parent_is_trusted;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002610 return ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002611 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002612# else
2613 (void)ret;
2614# endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002615
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002616 /* stop here if found or already in second iteration */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002617 if (*parent != NULL || *parent_is_trusted == 0)
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002618 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002619
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002620 /* prepare second iteration */
2621 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002622 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002623
2624 /* extra precaution against mistakes in the caller */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002625 if (*parent == NULL) {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002626 *parent_is_trusted = 0;
2627 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002628 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002629
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002630 return 0;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002631}
2632
2633/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002634 * Check if an end-entity certificate is locally trusted
2635 *
2636 * Currently we require such certificates to be self-signed (actually only
2637 * check for self-issued as self-signatures are not checked)
2638 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002639static int x509_crt_check_ee_locally_trusted(mbedtls_x509_crt *crt,
2640 mbedtls_x509_crt *trust_ca)
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002641{
2642 mbedtls_x509_crt *cur;
2643
2644 /* must be self-issued */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002645 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0)
2646 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002647
2648 /* look for an exact match with trusted cert */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002649 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2650 if (crt->raw.len == cur->raw.len &&
2651 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2652 return 0;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002653 }
2654 }
2655
2656 /* too bad */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002657 return -1;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002658}
2659
2660/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002661 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002662 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002663 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2664 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002665 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002666 * such that every cert in the chain is a child of the next one,
2667 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002668 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002669 * Verify that chain and return it with flags for all issues found.
2670 *
2671 * Special cases:
2672 * - EE == Rj -> return a one-element list containing it
2673 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2674 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002675 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002676 * Tests for (aspects of) this function should include at least:
2677 * - trusted EE
2678 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002679 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002680 * - if relevant: EE untrusted
2681 * - if relevant: EE -> intermediate, untrusted
2682 * with the aspect under test checked at each relevant level (EE, int, root).
2683 * For some aspects longer chains are required, but usually length 2 is
2684 * enough (but length 1 is not in general).
2685 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002686 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002687 * - [in] crt: the cert list EE, C1, ..., Cn
2688 * - [in] trust_ca: the trusted list R1, ..., Rp
2689 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002690 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002691 * Only valid when return value is 0, may contain garbage otherwise!
2692 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002693 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002694 *
2695 * Return value:
2696 * - non-zero if the chain could not be fully built and examined
2697 * - 0 is the chain was successfully built and examined,
2698 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002699 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002700static int x509_crt_verify_chain(mbedtls_x509_crt *crt,
2701 mbedtls_x509_crt *trust_ca,
2702 mbedtls_x509_crl *ca_crl,
2703 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2704 void *p_ca_cb,
2705 const mbedtls_x509_crt_profile *profile,
2706 mbedtls_x509_crt_verify_chain *ver_chain,
2707 mbedtls_x509_crt_restart_ctx *rs_ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002708{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002709 /* Don't initialize any of those variables here, so that the compiler can
2710 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002712 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002713 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002714 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002715 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002716 int parent_is_trusted;
2717 int child_is_trusted;
2718 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002719 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002720 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002721
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002722# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002723 /* resume if we had an operation in progress */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002724 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002725 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002726 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002727 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002728
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002729 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002730 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002731 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002732 flags = &cur->flags;
2733
2734 goto find_parent;
2735 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002736# endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002737
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002738 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002739 self_cnt = 0;
2740 parent_is_trusted = 0;
2741 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002742
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002743 while (1) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002744 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002745 cur = &ver_chain->items[ver_chain->len];
2746 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002747 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002748 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002749 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002750
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002751 /* Check time-validity (all certificates) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002752 if (mbedtls_x509_time_is_past(&child->valid_to))
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002753 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002754
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002755 if (mbedtls_x509_time_is_future(&child->valid_from))
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002756 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002757
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002758 /* Stop here for trusted roots (but not for trusted EE certs) */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002759 if (child_is_trusted)
2760 return 0;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002761
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002762 /* Check signature algorithm: MD & PK algs */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002763 if (x509_profile_check_md_alg(profile, child->sig_md) != 0)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002764 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002765
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002766 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002767 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002768
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002769 /* Special case: EE certs that are locally trusted */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002770 if (ver_chain->len == 1 &&
2771 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2772 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002773 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002774
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002775# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002776find_parent:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002777# endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002778
2779 /* Obtain list of potential trusted signers from CA callback,
2780 * or use statically provided list. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002781# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2782 if (f_ca_cb != NULL) {
2783 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2784 mbedtls_free(ver_chain->trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002785 ver_chain->trust_ca_cb_result = NULL;
2786
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002787 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2788 if (ret != 0)
2789 return MBEDTLS_ERR_X509_FATAL_ERROR;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002790
2791 cur_trust_ca = ver_chain->trust_ca_cb_result;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002792 } else
2793# endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002794 {
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002795 ((void)f_ca_cb);
2796 ((void)p_ca_cb);
Hanno Beckerf53893b2019-03-28 13:45:55 +00002797 cur_trust_ca = trust_ca;
2798 }
2799
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002800 /* Look for a parent in trusted CAs or up the chain */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002801 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2802 &parent_is_trusted, &signature_is_good,
2803 ver_chain->len - 1, self_cnt, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002804
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002805# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2806 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002807 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002808 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002809 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002810 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002811
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002812 return ret;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002813 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002814# else
2815 (void)ret;
2816# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002817
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002818 /* No parent? We're done here */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002819 if (parent == NULL) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002820 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002821 return 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002822 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002823
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002824 /* Count intermediate self-issued (not necessarily self-signed) certs.
2825 * These can occur with some strategies for key rollover, see [SIRO],
2826 * and should be excluded from max_pathlen checks. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002827 if (ver_chain->len != 1 &&
2828 x509_name_cmp(&child->issuer, &child->subject) == 0) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002829 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002830 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002831
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002832 /* path_cnt is 0 for the first intermediate CA,
2833 * and if parent is trusted it's not an intermediate CA */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002834 if (!parent_is_trusted &&
2835 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002836 /* return immediately to avoid overflow the chain array */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002837 return MBEDTLS_ERR_X509_FATAL_ERROR;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002838 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002839
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002840 /* signature was checked while searching parent */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002841 if (!signature_is_good)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002842 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2843
2844 /* check size of signing key */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002845 if (x509_profile_check_key(profile, &parent->pk) != 0)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002846 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002847
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002848# if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002849 /* Check trusted CA's CRL for the given crt */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002850 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
2851# else
2852 (void)ca_crl;
2853# endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002854
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002855 /* prepare for next iteration */
2856 child = parent;
2857 parent = NULL;
2858 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002859 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002860 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002861}
2862
2863/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002864 * Check for CN match
2865 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002866static int
2867x509_crt_check_cn(const mbedtls_x509_buf *name, const char *cn, size_t cn_len)
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002868{
2869 /* try exact match */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002870 if (name->len == cn_len && x509_memcasecmp(cn, name->p, cn_len) == 0) {
2871 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002872 }
2873
2874 /* try wildcard match */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002875 if (x509_check_wildcard(cn, name) == 0) {
2876 return 0;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002877 }
2878
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002879 return -1;
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002880}
2881
2882/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002883 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2884 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002885static int
2886x509_crt_check_san(const mbedtls_x509_buf *name, const char *cn, size_t cn_len)
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002887{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002888 const unsigned char san_type = (unsigned char)name->tag &
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002889 MBEDTLS_ASN1_TAG_VALUE_MASK;
2890
2891 /* dNSName */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002892 if (san_type == MBEDTLS_X509_SAN_DNS_NAME)
2893 return x509_crt_check_cn(name, cn, cn_len);
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002894
2895 /* (We may handle other types here later.) */
2896
2897 /* Unrecognized type */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002898 return -1;
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002899}
2900
2901/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002902 * Verify the requested CN - only call this if cn is not NULL!
2903 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002904static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2905 const char *cn,
2906 uint32_t *flags)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002907{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002908 const mbedtls_x509_name *name;
2909 const mbedtls_x509_sequence *cur;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002910 size_t cn_len = strlen(cn);
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002911
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002912 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2913 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
2914 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002915 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002916 }
2917
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002918 if (cur == NULL)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002919 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002920 } else {
2921 for (name = &crt->subject; name != NULL; name = name->next) {
2922 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2923 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002924 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002925 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002926 }
2927
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002928 if (name == NULL)
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002929 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2930 }
2931}
2932
2933/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002934 * Merge the flags for all certs in the chain, after calling callback
2935 */
2936static int x509_crt_merge_flags_with_cb(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002937 uint32_t *flags,
2938 const mbedtls_x509_crt_verify_chain *ver_chain,
2939 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2940 void *p_vrfy)
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002941{
Janos Follath865b3eb2019-12-16 11:46:15 +00002942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002943 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002944 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002945 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002946
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002947 for (i = ver_chain->len; i != 0; --i) {
2948 cur = &ver_chain->items[i - 1];
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002949 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002950
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002951 if (NULL != f_vrfy)
2952 if ((ret = f_vrfy(p_vrfy, cur->crt, (int)i - 1, &cur_flags)) != 0)
2953 return ret;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002954
2955 *flags |= cur_flags;
2956 }
2957
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002958 return 0;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002959}
2960
2961/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002962 * Verify the certificate validity, with profile, restartable version
2963 *
2964 * This function:
2965 * - checks the requested CN (if any)
2966 * - checks the type and size of the EE cert's key,
2967 * as that isn't done as part of chain building/verification currently
2968 * - builds and verifies the chain
2969 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00002970 *
2971 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
2972 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
2973 * verification routine to search for trusted signers, and CRLs will
2974 * be disabled. Otherwise, `trust_ca` will be used as the static list
2975 * of trusted signers, and `ca_crl` will be use as the static list
2976 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002977 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002978static int x509_crt_verify_restartable_ca_cb(
2979 mbedtls_x509_crt *crt,
2980 mbedtls_x509_crt *trust_ca,
2981 mbedtls_x509_crl *ca_crl,
2982 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2983 void *p_ca_cb,
2984 const mbedtls_x509_crt_profile *profile,
2985 const char *cn,
2986 uint32_t *flags,
2987 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2988 void *p_vrfy,
2989 mbedtls_x509_crt_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002990{
Janos Follath865b3eb2019-12-16 11:46:15 +00002991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02002992 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002993 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002994 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002995
2996 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002997 ee_flags = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02002998 x509_crt_verify_chain_reset(&ver_chain);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002999
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003000 if (profile == NULL) {
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003001 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3002 goto exit;
3003 }
3004
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003005 /* check name if requested */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003006 if (cn != NULL)
3007 x509_crt_verify_name(crt, cn, &ee_flags);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003008
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003009 /* Check the type and size of the key */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003010 pk_type = mbedtls_pk_get_type(&crt->pk);
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003011
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003012 if (x509_profile_check_pk_alg(profile, pk_type) != 0)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003013 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003014
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003015 if (x509_profile_check_key(profile, &crt->pk) != 0)
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003016 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003017
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003018 /* Check the chain */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003019 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl, f_ca_cb, p_ca_cb,
3020 profile, &ver_chain, rs_ctx);
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003021
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003022 if (ret != 0)
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003023 goto exit;
3024
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003025 /* Merge end-entity flags */
3026 ver_chain.items[0].flags |= ee_flags;
3027
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003028 /* Build final flags, calling callback on the way if any */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003029 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003030
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003031exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003032
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003033# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3034 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3035 mbedtls_free(ver_chain.trust_ca_cb_result);
Hanno Beckerf53893b2019-03-28 13:45:55 +00003036 ver_chain.trust_ca_cb_result = NULL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003037# endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Hanno Beckerf53893b2019-03-28 13:45:55 +00003038
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003039# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3040 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS)
3041 mbedtls_x509_crt_restart_free(rs_ctx);
3042# endif
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003043
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003044 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3045 * the SSL module for authmode optional, but non-zero return from the
3046 * callback means a fatal error so it shouldn't be ignored */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003047 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED)
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003048 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3049
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003050 if (ret != 0) {
3051 *flags = (uint32_t)-1;
3052 return ret;
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003053 }
3054
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003055 if (*flags != 0)
3056 return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003057
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003058 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003059}
3060
Hanno Becker3116fb32019-03-28 13:34:42 +00003061/*
3062 * Verify the certificate validity (default profile, not restartable)
3063 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003064int mbedtls_x509_crt_verify(
3065 mbedtls_x509_crt *crt,
3066 mbedtls_x509_crt *trust_ca,
3067 mbedtls_x509_crl *ca_crl,
3068 const char *cn,
3069 uint32_t *flags,
3070 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3071 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003072{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003073 return (x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl, NULL, NULL,
3074 &mbedtls_x509_crt_profile_default,
3075 cn, flags, f_vrfy, p_vrfy, NULL));
Hanno Becker3116fb32019-03-28 13:34:42 +00003076}
3077
3078/*
3079 * Verify the certificate validity (user-chosen profile, not restartable)
3080 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003081int mbedtls_x509_crt_verify_with_profile(
3082 mbedtls_x509_crt *crt,
3083 mbedtls_x509_crt *trust_ca,
3084 mbedtls_x509_crl *ca_crl,
3085 const mbedtls_x509_crt_profile *profile,
3086 const char *cn,
3087 uint32_t *flags,
3088 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3089 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003090{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003091 return (x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl, NULL, NULL,
3092 profile, cn, flags, f_vrfy,
3093 p_vrfy, NULL));
Hanno Becker3116fb32019-03-28 13:34:42 +00003094}
3095
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003096# if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Hanno Becker3116fb32019-03-28 13:34:42 +00003097/*
3098 * Verify the certificate validity (user-chosen profile, CA callback,
3099 * not restartable).
3100 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003101int mbedtls_x509_crt_verify_with_ca_cb(
3102 mbedtls_x509_crt *crt,
3103 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3104 void *p_ca_cb,
3105 const mbedtls_x509_crt_profile *profile,
3106 const char *cn,
3107 uint32_t *flags,
3108 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3109 void *p_vrfy)
Hanno Becker3116fb32019-03-28 13:34:42 +00003110{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003111 return (x509_crt_verify_restartable_ca_cb(crt, NULL, NULL, f_ca_cb, p_ca_cb,
3112 profile, cn, flags, f_vrfy,
3113 p_vrfy, NULL));
Hanno Becker3116fb32019-03-28 13:34:42 +00003114}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003115# endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Hanno Becker3116fb32019-03-28 13:34:42 +00003116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003117int mbedtls_x509_crt_verify_restartable(
3118 mbedtls_x509_crt *crt,
3119 mbedtls_x509_crt *trust_ca,
3120 mbedtls_x509_crl *ca_crl,
3121 const mbedtls_x509_crt_profile *profile,
3122 const char *cn,
3123 uint32_t *flags,
3124 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3125 void *p_vrfy,
3126 mbedtls_x509_crt_restart_ctx *rs_ctx)
Hanno Becker3116fb32019-03-28 13:34:42 +00003127{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003128 return (x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl, NULL, NULL,
3129 profile, cn, flags, f_vrfy,
3130 p_vrfy, rs_ctx));
Hanno Becker3116fb32019-03-28 13:34:42 +00003131}
3132
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003133/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003134 * Initialize a certificate chain
3135 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003136void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
Paul Bakker369d2eb2013-09-18 11:58:25 +02003137{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003138 memset(crt, 0, sizeof(mbedtls_x509_crt));
Paul Bakker369d2eb2013-09-18 11:58:25 +02003139}
3140
3141/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003142 * Unallocate all certificate data
3143 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003144void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 mbedtls_x509_crt *cert_cur = crt;
3147 mbedtls_x509_crt *cert_prv;
3148 mbedtls_x509_name *name_cur;
3149 mbedtls_x509_name *name_prv;
3150 mbedtls_x509_sequence *seq_cur;
3151 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003152
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003153 if (crt == NULL)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003154 return;
3155
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003156 do {
3157 mbedtls_pk_free(&cert_cur->pk);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003158
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003159# if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3160 mbedtls_free(cert_cur->sig_opts);
3161# endif
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003162
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003163 name_cur = cert_cur->issuer.next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003164 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003165 name_prv = name_cur;
3166 name_cur = name_cur->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003167 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3168 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003169 }
3170
3171 name_cur = cert_cur->subject.next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003172 while (name_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003173 name_prv = name_cur;
3174 name_cur = name_cur->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003175 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3176 mbedtls_free(name_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003177 }
3178
3179 seq_cur = cert_cur->ext_key_usage.next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003180 while (seq_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003181 seq_prv = seq_cur;
3182 seq_cur = seq_cur->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003183 mbedtls_platform_zeroize(seq_prv, sizeof(mbedtls_x509_sequence));
3184 mbedtls_free(seq_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003185 }
3186
3187 seq_cur = cert_cur->subject_alt_names.next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003188 while (seq_cur != NULL) {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003189 seq_prv = seq_cur;
3190 seq_cur = seq_cur->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003191 mbedtls_platform_zeroize(seq_prv, sizeof(mbedtls_x509_sequence));
3192 mbedtls_free(seq_prv);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003193 }
3194
Ron Eldor74d9acc2019-03-21 14:00:03 +02003195 seq_cur = cert_cur->certificate_policies.next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003196 while (seq_cur != NULL) {
Ron Eldor74d9acc2019-03-21 14:00:03 +02003197 seq_prv = seq_cur;
3198 seq_cur = seq_cur->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003199 mbedtls_platform_zeroize(seq_prv, sizeof(mbedtls_x509_sequence));
3200 mbedtls_free(seq_prv);
Ron Eldor74d9acc2019-03-21 14:00:03 +02003201 }
3202
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003203 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3204 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3205 mbedtls_free(cert_cur->raw.p);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003206 }
3207
3208 cert_cur = cert_cur->next;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003209 } while (cert_cur != NULL);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003210
3211 cert_cur = crt;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003212 do {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003213 cert_prv = cert_cur;
3214 cert_cur = cert_cur->next;
3215
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003216 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3217 if (cert_prv != crt)
3218 mbedtls_free(cert_prv);
3219 } while (cert_cur != NULL);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003220}
3221
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003222# if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003223/*
3224 * Initialize a restart context
3225 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003226void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003227{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003228 mbedtls_pk_restart_init(&ctx->pk);
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003229
3230 ctx->parent = NULL;
3231 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003232 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003233
3234 ctx->parent_is_trusted = -1;
3235
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003236 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003237 ctx->self_cnt = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003238 x509_crt_verify_chain_reset(&ctx->ver_chain);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003239}
3240
3241/*
3242 * Free the components of a restart context
3243 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003244void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003245{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003246 if (ctx == NULL)
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003247 return;
3248
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003249 mbedtls_pk_restart_free(&ctx->pk);
3250 mbedtls_x509_crt_restart_init(ctx);
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003251}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02003252# endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254#endif /* MBEDTLS_X509_CRT_PARSE_C */