Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 2 | * X.509 certificate parsing and verification |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 18 | */ |
| 19 | /* |
| 20 | * The ITU-T X.509 standard defines a certificate format for PKI. |
| 21 | * |
Manuel Pégourié-Gonnard | 1c082f3 | 2014-06-12 22:34:55 +0200 | [diff] [blame] | 22 | * 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 Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 25 | * |
| 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é-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 28 | * |
| 29 | * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 30 | */ |
| 31 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 32 | #include "common.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/x509_crt.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 37 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/oid.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 39 | #include "mbedtls/platform_util.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 40 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 41 | #include <string.h> |
| 42 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 44 | #include "mbedtls/pem.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 45 | #endif |
| 46 | |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 47 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 48 | #include "psa/crypto.h" |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 49 | #include "psa_util_internal.h" |
Manuel Pégourié-Gonnard | 02b10d8 | 2023-03-28 12:33:20 +0200 | [diff] [blame] | 50 | #include "md_psa.h" |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 51 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Valerio Setti | 3f00b84 | 2023-05-15 12:57:06 +0200 | [diff] [blame] | 52 | #include "pk_internal.h" |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 53 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/platform.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 55 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 57 | #include "mbedtls/threading.h" |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 58 | #endif |
| 59 | |
Daniel Axtens | f071024 | 2020-05-28 11:43:41 +1000 | [diff] [blame] | 60 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 61 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 62 | #define WIN32_LEAN_AND_MEAN |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 63 | #include <windows.h> |
Simon Butcher | e068aa7 | 2018-03-14 15:10:31 +0000 | [diff] [blame] | 64 | #if defined(_MSC_VER) && _MSC_VER <= 1600 |
Minos Galanakis | 12b493f | 2023-08-11 15:22:45 +0100 | [diff] [blame] | 65 | #define MBEDTLS_POP_TARGET_PRAGMA |
| 66 | #endif |
| 67 | #if defined(MBEDTLS_POP_TARGET_PRAGMA) |
Simon Butcher | def90f4 | 2018-03-14 17:02:16 +0000 | [diff] [blame] | 68 | /* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and |
| 69 | * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants. |
| 70 | * These constants are guaranteed to be the same, though, so we suppress the |
| 71 | * warning when including intsafe.h. |
Kevin Kane | 0ec1e68 | 2016-12-15 09:27:16 -0800 | [diff] [blame] | 72 | */ |
Minos Galanakis | a277b21 | 2023-08-09 16:32:22 +0100 | [diff] [blame] | 73 | #pragma warning(push ) |
| 74 | #pragma warning(disable : 4005) |
Kevin Kane | 0ec1e68 | 2016-12-15 09:27:16 -0800 | [diff] [blame] | 75 | #endif |
| 76 | #include <intsafe.h> |
Minos Galanakis | 12b493f | 2023-08-11 15:22:45 +0100 | [diff] [blame] | 77 | #if defined(MBEDTLS_POP_TARGET_PRAGMA) |
Minos Galanakis | a277b21 | 2023-08-09 16:32:22 +0100 | [diff] [blame] | 78 | #pragma warning(pop) |
Kevin Kane | 0ec1e68 | 2016-12-15 09:27:16 -0800 | [diff] [blame] | 79 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 80 | #else |
| 81 | #include <time.h> |
| 82 | #endif |
Daniel Axtens | f071024 | 2020-05-28 11:43:41 +1000 | [diff] [blame] | 83 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 84 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | #if defined(MBEDTLS_FS_IO) |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 86 | #include <stdio.h> |
Paul Bakker | 5ff3f91 | 2014-04-04 15:08:20 +0200 | [diff] [blame] | 87 | #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 88 | #include <sys/types.h> |
| 89 | #include <sys/stat.h> |
Martino Facchin | 0ec05ec | 2021-05-04 11:47:36 +0200 | [diff] [blame] | 90 | #if defined(__MBED__) |
| 91 | #include <platform/mbed_retarget.h> |
| 92 | #else |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 93 | #include <dirent.h> |
Martino Facchin | 0ec05ec | 2021-05-04 11:47:36 +0200 | [diff] [blame] | 94 | #endif /* __MBED__ */ |
Eduardo Silva | e1bfffc | 2019-04-25 10:43:26 -0600 | [diff] [blame] | 95 | #include <errno.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 96 | #endif /* !_WIN32 || EFIX64 || EFI32 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 97 | #endif |
| 98 | |
Manuel Pégourié-Gonnard | c547d1a | 2017-07-05 13:28:45 +0200 | [diff] [blame] | 99 | /* |
| 100 | * Item in a verification chain: cert and flags for it |
| 101 | */ |
| 102 | typedef struct { |
| 103 | mbedtls_x509_crt *crt; |
| 104 | uint32_t flags; |
| 105 | } x509_crt_verify_chain_item; |
| 106 | |
| 107 | /* |
| 108 | * Max size of verification chain: end-entity + intermediates + trusted root |
| 109 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | #define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2) |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 111 | |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 112 | /* Default profile. Do not remove items unless there are serious security |
| 113 | * concerns. */ |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 114 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default = |
| 115 | { |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 116 | /* Hashes from SHA-256 and above. Note that this selection |
| 117 | * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) | |
| 119 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | |
| 120 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512), |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 121 | 0xFFFFFFF, /* Any PK alg */ |
Valerio Setti | 8c3404f | 2023-06-26 15:49:48 +0200 | [diff] [blame] | 122 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Gilles Peskine | ae270bf | 2021-06-02 00:05:29 +0200 | [diff] [blame] | 123 | /* Curves at or above 128-bit security level. Note that this selection |
| 124 | * should be aligned with ssl_preset_default_curves in ssl_tls.c. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) | |
| 126 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) | |
| 127 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) | |
| 128 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) | |
| 129 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) | |
| 130 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) | |
Gilles Peskine | 3995750 | 2021-06-17 23:17:52 +0200 | [diff] [blame] | 131 | 0, |
Valerio Setti | 8c3404f | 2023-06-26 15:49:48 +0200 | [diff] [blame] | 132 | #else /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 133 | 0, |
Valerio Setti | 8c3404f | 2023-06-26 15:49:48 +0200 | [diff] [blame] | 134 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 135 | 2048, |
| 136 | }; |
| 137 | |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 138 | /* Next-generation profile. Currently identical to the default, but may |
| 139 | * be tightened at any time. */ |
| 140 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next = |
Gilles Peskine | 2c69fa2 | 2021-06-02 00:33:33 +0200 | [diff] [blame] | 141 | { |
| 142 | /* Hashes from SHA-256 and above. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) | |
| 144 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | |
| 145 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512), |
Gilles Peskine | 2c69fa2 | 2021-06-02 00:33:33 +0200 | [diff] [blame] | 146 | 0xFFFFFFF, /* Any PK alg */ |
| 147 | #if defined(MBEDTLS_ECP_C) |
| 148 | /* Curves at or above 128-bit security level. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) | |
| 150 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) | |
| 151 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) | |
| 152 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) | |
| 153 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) | |
| 154 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) | |
| 155 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1), |
Gilles Peskine | 2c69fa2 | 2021-06-02 00:33:33 +0200 | [diff] [blame] | 156 | #else |
| 157 | 0, |
| 158 | #endif |
| 159 | 2048, |
| 160 | }; |
Gilles Peskine | ffb92da | 2021-06-02 00:03:26 +0200 | [diff] [blame] | 161 | |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 162 | /* |
| 163 | * NSA Suite B Profile |
| 164 | */ |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 165 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb = |
| 166 | { |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 167 | /* Only SHA-256 and 384 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) | |
| 169 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384), |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 170 | /* Only ECDSA */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) | |
| 172 | MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY), |
Valerio Setti | 8c3404f | 2023-06-26 15:49:48 +0200 | [diff] [blame] | 173 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 174 | /* Only NIST P-256 and P-384 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) | |
| 176 | MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1), |
Valerio Setti | 8c3404f | 2023-06-26 15:49:48 +0200 | [diff] [blame] | 177 | #else /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 178 | 0, |
Valerio Setti | 8c3404f | 2023-06-26 15:49:48 +0200 | [diff] [blame] | 179 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Manuel Pégourié-Gonnard | f8ea856 | 2015-06-15 15:33:19 +0200 | [diff] [blame] | 180 | 0, |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | /* |
Manuel Pégourié-Gonnard | 9d4c2c4 | 2021-06-18 09:48:27 +0200 | [diff] [blame] | 184 | * Empty / all-forbidden profile |
| 185 | */ |
| 186 | const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none = |
| 187 | { |
| 188 | 0, |
| 189 | 0, |
| 190 | 0, |
| 191 | (uint32_t) -1, |
| 192 | }; |
| 193 | |
| 194 | /* |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 195 | * Check md_alg against profile |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 196 | * Return 0 if md_alg is acceptable for this profile, -1 otherwise |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 197 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile, |
| 199 | mbedtls_md_type_t md_alg) |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 200 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | if (md_alg == MBEDTLS_MD_NONE) { |
| 202 | return -1; |
| 203 | } |
Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) { |
| 206 | return 0; |
| 207 | } |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | return -1; |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Check pk_alg against profile |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 214 | * Return 0 if pk_alg is acceptable for this profile, -1 otherwise |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 215 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile, |
| 217 | mbedtls_pk_type_t pk_alg) |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 218 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | if (pk_alg == MBEDTLS_PK_NONE) { |
| 220 | return -1; |
| 221 | } |
Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) { |
| 224 | return 0; |
| 225 | } |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | return -1; |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Check key against profile |
Manuel Pégourié-Gonnard | 3f81691 | 2017-10-26 10:24:16 +0200 | [diff] [blame] | 232 | * Return 0 if pk is acceptable for this profile, -1 otherwise |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 233 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile, |
| 235 | const mbedtls_pk_context *pk) |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 236 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk); |
Manuel Pégourié-Gonnard | 19773ff | 2017-10-24 10:51:26 +0200 | [diff] [blame] | 238 | |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 239 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) { |
| 241 | if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) { |
| 242 | return 0; |
| 243 | } |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | return -1; |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 246 | } |
Valerio Setti | d4a5d46 | 2023-04-05 18:19:01 +0200 | [diff] [blame] | 247 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 248 | |
Valerio Setti | 8c3404f | 2023-06-26 15:49:48 +0200 | [diff] [blame] | 249 | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | if (pk_alg == MBEDTLS_PK_ECDSA || |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 251 | pk_alg == MBEDTLS_PK_ECKEY || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | pk_alg == MBEDTLS_PK_ECKEY_DH) { |
Valerio Setti | 9720778 | 2023-05-18 18:59:06 +0200 | [diff] [blame] | 253 | const mbedtls_ecp_group_id gid = mbedtls_pk_get_group_id(pk); |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 254 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | if (gid == MBEDTLS_ECP_DP_NONE) { |
| 256 | return -1; |
| 257 | } |
Philippe Antoine | b5b2543 | 2018-05-11 11:06:29 +0200 | [diff] [blame] | 258 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) { |
| 260 | return 0; |
| 261 | } |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | return -1; |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 264 | } |
Valerio Setti | 8c3404f | 2023-06-26 15:49:48 +0200 | [diff] [blame] | 265 | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | return -1; |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 271 | * Like memcmp, but case-insensitive and always returns -1 if different |
| 272 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | static int x509_memcasecmp(const void *s1, const void *s2, size_t len) |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 274 | { |
| 275 | size_t i; |
| 276 | unsigned char diff; |
| 277 | const unsigned char *n1 = s1, *n2 = s2; |
| 278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | for (i = 0; i < len; i++) { |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 280 | diff = n1[i] ^ n2[i]; |
| 281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | if (diff == 0) { |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 283 | continue; |
| 284 | } |
| 285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | if (diff == 32 && |
| 287 | ((n1[i] >= 'a' && n1[i] <= 'z') || |
| 288 | (n1[i] >= 'A' && n1[i] <= 'Z'))) { |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | return -1; |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | return 0; |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Return 0 if name matches wildcard, -1 otherwise |
| 300 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name) |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 302 | { |
| 303 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | size_t cn_idx = 0, cn_len = strlen(cn); |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 305 | |
| 306 | /* We can't have a match if there is no wildcard to match */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') { |
| 308 | return -1; |
| 309 | } |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | for (i = 0; i < cn_len; ++i) { |
| 312 | if (cn[i] == '.') { |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 313 | cn_idx = i; |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | if (cn_idx == 0) { |
| 319 | return -1; |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | if (cn_len - cn_idx == name->len - 1 && |
| 323 | x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) { |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | return -1; |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Compare two X.509 strings, case-insensitive, and allowing for some encoding |
| 332 | * variations (but not all). |
| 333 | * |
| 334 | * Return 0 if equal, -1 otherwise. |
| 335 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b) |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 337 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | if (a->tag == b->tag && |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 339 | a->len == b->len && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | memcmp(a->p, b->p, b->len) == 0) { |
| 341 | return 0; |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) && |
| 345 | (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) && |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 346 | a->len == b->len && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | x509_memcasecmp(a->p, b->p, b->len) == 0) { |
| 348 | return 0; |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | return -1; |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Compare two X.509 Names (aka rdnSequence). |
| 356 | * |
| 357 | * See RFC 5280 section 7.1, though we don't implement the whole algorithm: |
| 358 | * we sometimes return unequal when the full algorithm would return equal, |
| 359 | * but never the other way. (In particular, we don't do Unicode normalisation |
| 360 | * or space folding.) |
| 361 | * |
| 362 | * Return 0 if equal, -1 otherwise. |
| 363 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b) |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 365 | { |
| 366 | /* Avoid recursion, it might not be optimised by the compiler */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | while (a != NULL || b != NULL) { |
| 368 | if (a == NULL || b == NULL) { |
| 369 | return -1; |
| 370 | } |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 371 | |
| 372 | /* type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | if (a->oid.tag != b->oid.tag || |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 374 | a->oid.len != b->oid.len || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 375 | memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) { |
| 376 | return -1; |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | if (x509_string_cmp(&a->val, &b->val) != 0) { |
| 381 | return -1; |
| 382 | } |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 383 | |
| 384 | /* structure of the list of sets */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | if (a->next_merged != b->next_merged) { |
| 386 | return -1; |
| 387 | } |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 388 | |
| 389 | a = a->next; |
| 390 | b = b->next; |
| 391 | } |
| 392 | |
| 393 | /* a == NULL == b */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | return 0; |
Hanno Becker | 1f8527f | 2018-11-02 09:19:16 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 398 | * Reset (init or clear) a verify_chain |
| 399 | */ |
| 400 | static void x509_crt_verify_chain_reset( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | mbedtls_x509_crt_verify_chain *ver_chain) |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 402 | { |
| 403 | size_t i; |
| 404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) { |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 406 | ver_chain->items[i].crt = NULL; |
Hanno Becker | a9375b3 | 2019-01-10 09:19:26 +0000 | [diff] [blame] | 407 | ver_chain->items[i].flags = (uint32_t) -1; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | ver_chain->len = 0; |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 411 | |
| 412 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 413 | ver_chain->trust_ca_cb_result = NULL; |
| 414 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 418 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 419 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | static int x509_get_version(unsigned char **p, |
| 421 | const unsigned char *end, |
| 422 | int *ver) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 423 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 424 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 425 | size_t len; |
| 426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 427 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 428 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 429 | 0)) != 0) { |
| 430 | if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 431 | *ver = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | end = *p + len; |
| 439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) { |
| 441 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret); |
| 442 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | if (*p != end) { |
| 445 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, |
| 446 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 447 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Validity ::= SEQUENCE { |
| 454 | * notBefore Time, |
| 455 | * notAfter Time } |
| 456 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 457 | static int x509_get_dates(unsigned char **p, |
| 458 | const unsigned char *end, |
| 459 | mbedtls_x509_time *from, |
| 460 | mbedtls_x509_time *to) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 461 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 462 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 463 | size_t len; |
| 464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 466 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 467 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret); |
| 468 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 469 | |
| 470 | end = *p + len; |
| 471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) { |
| 473 | return ret; |
| 474 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) { |
| 477 | return ret; |
| 478 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 480 | if (*p != end) { |
| 481 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, |
| 482 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 483 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* |
| 489 | * X.509 v2/v3 unique identifier (not parsed) |
| 490 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | static int x509_get_uid(unsigned char **p, |
| 492 | const unsigned char *end, |
| 493 | mbedtls_x509_buf *uid, int n) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 494 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 495 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | if (*p == end) { |
| 498 | return 0; |
| 499 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 500 | |
| 501 | uid->tag = **p; |
| 502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len, |
| 504 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 505 | n)) != 0) { |
| 506 | if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 507 | return 0; |
| 508 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | uid->p = *p; |
| 514 | *p += uid->len; |
| 515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | static int x509_get_basic_constraints(unsigned char **p, |
| 520 | const unsigned char *end, |
| 521 | int *ca_istrue, |
| 522 | int *max_pathlen) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 523 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 524 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 525 | size_t len; |
| 526 | |
| 527 | /* |
| 528 | * BasicConstraints ::= SEQUENCE { |
| 529 | * cA BOOLEAN DEFAULT FALSE, |
| 530 | * pathLenConstraint INTEGER (0..MAX) OPTIONAL } |
| 531 | */ |
| 532 | *ca_istrue = 0; /* DEFAULT FALSE */ |
| 533 | *max_pathlen = 0; /* endless */ |
| 534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 535 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 536 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 537 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 540 | if (*p == end) { |
| 541 | return 0; |
| 542 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) { |
| 545 | if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 546 | ret = mbedtls_asn1_get_int(p, end, ca_istrue); |
| 547 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | if (ret != 0) { |
| 550 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 551 | } |
| 552 | |
| 553 | if (*ca_istrue != 0) { |
| 554 | *ca_istrue = 1; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (*p == end) { |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) { |
| 563 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 564 | } |
| 565 | |
| 566 | if (*p != end) { |
| 567 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 568 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 569 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 570 | |
Andrzej Kurek | 1605074 | 2020-04-14 09:49:52 -0400 | [diff] [blame] | 571 | /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer |
| 572 | * overflow, which is an undefined behavior. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | if (*max_pathlen == INT_MAX) { |
| 574 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 575 | MBEDTLS_ERR_ASN1_INVALID_LENGTH); |
| 576 | } |
Andrzej Kurek | 1605074 | 2020-04-14 09:49:52 -0400 | [diff] [blame] | 577 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 578 | (*max_pathlen)++; |
| 579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 580 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 583 | /* |
| 584 | * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId |
| 585 | * |
| 586 | * KeyPurposeId ::= OBJECT IDENTIFIER |
| 587 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | static int x509_get_ext_key_usage(unsigned char **p, |
| 589 | const unsigned char *end, |
| 590 | mbedtls_x509_sequence *ext_key_usage) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 591 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 592 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) { |
| 595 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 596 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 597 | |
| 598 | /* Sequence length must be >= 1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | if (ext_key_usage->buf.p == NULL) { |
| 600 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 601 | MBEDTLS_ERR_ASN1_INVALID_LENGTH); |
| 602 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 603 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
toth92g | a41954d | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 608 | * SubjectKeyIdentifier ::= KeyIdentifier |
| 609 | * |
| 610 | * KeyIdentifier ::= OCTET STRING |
| 611 | */ |
| 612 | static int x509_get_subject_key_id(unsigned char **p, |
| 613 | const unsigned char *end, |
| 614 | mbedtls_x509_buf *subject_key_id) |
| 615 | { |
| 616 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 617 | size_t len = 0u; |
| 618 | |
| 619 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 620 | MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
Przemek Stekiel | 75653b1 | 2023-02-01 11:31:32 +0100 | [diff] [blame] | 621 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
toth92g | a41954d | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Przemek Stekiel | 61aed06 | 2023-05-08 11:14:36 +0200 | [diff] [blame] | 624 | subject_key_id->len = len; |
| 625 | subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING; |
| 626 | subject_key_id->p = *p; |
| 627 | *p += len; |
| 628 | |
toth92g | d96027a | 2021-04-27 15:41:25 +0200 | [diff] [blame] | 629 | if (*p != end) { |
Przemek Stekiel | 3520fe6 | 2023-01-30 14:38:18 +0100 | [diff] [blame] | 630 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 631 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
toth92g | d96027a | 2021-04-27 15:41:25 +0200 | [diff] [blame] | 632 | } |
| 633 | |
toth92g | a41954d | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
Przemek Stekiel | 4f3e7b9 | 2023-02-03 15:03:59 +0100 | [diff] [blame] | 637 | /* |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 638 | * AuthorityKeyIdentifier ::= SEQUENCE { |
| 639 | * keyIdentifier [0] KeyIdentifier OPTIONAL, |
| 640 | * authorityCertIssuer [1] GeneralNames OPTIONAL, |
| 641 | * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL } |
| 642 | * |
| 643 | * KeyIdentifier ::= OCTET STRING |
| 644 | */ |
| 645 | static int x509_get_authority_key_id(unsigned char **p, |
| 646 | unsigned char *end, |
| 647 | mbedtls_x509_authority *authority_key_id) |
| 648 | { |
| 649 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 650 | size_t len = 0u; |
| 651 | |
| 652 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
Przemek Stekiel | 3520fe6 | 2023-01-30 14:38:18 +0100 | [diff] [blame] | 653 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
Przemek Stekiel | 75653b1 | 2023-02-01 11:31:32 +0100 | [diff] [blame] | 654 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Przemek Stekiel | 6ec839a | 2023-02-01 11:06:08 +0100 | [diff] [blame] | 657 | if (*p + len != end) { |
| 658 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 659 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 660 | } |
| 661 | |
Przemek Stekiel | ed9fb78 | 2023-05-03 16:27:25 +0200 | [diff] [blame] | 662 | ret = mbedtls_asn1_get_tag(p, end, &len, |
| 663 | MBEDTLS_ASN1_CONTEXT_SPECIFIC); |
| 664 | |
| 665 | /* KeyIdentifier is an OPTIONAL field */ |
Przemek Stekiel | 61aed06 | 2023-05-08 11:14:36 +0200 | [diff] [blame] | 666 | if (ret == 0) { |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 667 | authority_key_id->keyIdentifier.len = len; |
| 668 | authority_key_id->keyIdentifier.p = *p; |
toth92g | 9232e0a | 2021-05-11 12:55:58 +0200 | [diff] [blame] | 669 | /* Setting tag of the keyIdentfier intentionally to 0x04. |
| 670 | * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL), |
Przemek Stekiel | 9a7a725 | 2023-04-17 16:06:57 +0200 | [diff] [blame] | 671 | * its tag with the content is the payload of on OCTET STRING primitive */ |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 672 | authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING; |
| 673 | |
| 674 | *p += len; |
Przemek Stekiel | 61aed06 | 2023-05-08 11:14:36 +0200 | [diff] [blame] | 675 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 676 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | if (*p < end) { |
toth92g | 9232e0a | 2021-05-11 12:55:58 +0200 | [diff] [blame] | 680 | /* Getting authorityCertIssuer using the required specific class tag [1] */ |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 681 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 682 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
Przemek Stekiel | 4f3e7b9 | 2023-02-03 15:03:59 +0100 | [diff] [blame] | 683 | 1)) != 0) { |
Przemek Stekiel | f5b8f78 | 2023-04-26 08:55:26 +0200 | [diff] [blame] | 684 | /* authorityCertIssuer and authorityCertSerialNumber MUST both |
| 685 | be present or both be absent. At this point we expect to have both. */ |
| 686 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 687 | } |
Przemek Stekiel | ed9fb78 | 2023-05-03 16:27:25 +0200 | [diff] [blame] | 688 | /* "end" also includes the CertSerialNumber field so "len" shall be used */ |
| 689 | ret = mbedtls_x509_get_subject_alt_name_ext(p, |
| 690 | (*p+len), |
| 691 | &authority_key_id->authorityCertIssuer); |
| 692 | if (ret != 0) { |
| 693 | return ret; |
| 694 | } |
| 695 | |
| 696 | /* Getting authorityCertSerialNumber using the required specific class tag [2] */ |
| 697 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 698 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) { |
| 699 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 700 | } |
| 701 | authority_key_id->authorityCertSerialNumber.len = len; |
| 702 | authority_key_id->authorityCertSerialNumber.p = *p; |
| 703 | authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER; |
| 704 | *p += len; |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | if (*p != end) { |
| 708 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS + |
| 709 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 710 | } |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | /* |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 716 | * 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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 764 | static int x509_get_certificate_policies(unsigned char **p, |
| 765 | const unsigned char *end, |
| 766 | mbedtls_x509_sequence *certificate_policies) |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 767 | { |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 768 | int ret, parse_ret = 0; |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 769 | size_t len; |
| 770 | mbedtls_asn1_buf *buf; |
| 771 | mbedtls_asn1_sequence *cur = certificate_policies; |
| 772 | |
| 773 | /* Get main sequence tag */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | ret = mbedtls_asn1_get_tag(p, end, &len, |
| 775 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); |
| 776 | if (ret != 0) { |
| 777 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 778 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | if (*p + len != end) { |
| 781 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 782 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 783 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 784 | |
| 785 | /* |
| 786 | * Cannot be an empty sequence. |
| 787 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 788 | if (len == 0) { |
| 789 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 790 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 791 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 793 | while (*p < end) { |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 794 | mbedtls_x509_buf policy_oid; |
| 795 | const unsigned char *policy_end; |
| 796 | |
| 797 | /* |
| 798 | * Get the policy sequence |
| 799 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 801 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 802 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 803 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 804 | |
| 805 | policy_end = *p + len; |
| 806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 807 | if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len, |
| 808 | MBEDTLS_ASN1_OID)) != 0) { |
| 809 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 810 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 811 | |
| 812 | policy_oid.tag = MBEDTLS_ASN1_OID; |
| 813 | policy_oid.len = len; |
| 814 | policy_oid.p = *p; |
| 815 | |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 816 | /* |
| 817 | * Only AnyPolicy is currently supported when enforcing policy. |
| 818 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) { |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 820 | /* |
| 821 | * Set the parsing return code but continue parsing, in case this |
TRodziewicz | 3ecb92e | 2021-05-11 18:22:05 +0200 | [diff] [blame] | 822 | * extension is critical. |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 823 | */ |
| 824 | parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 825 | } |
| 826 | |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 827 | /* Allocate and assign next pointer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 828 | if (cur->buf.p != NULL) { |
| 829 | if (cur->next != NULL) { |
| 830 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS; |
| 831 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence)); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | if (cur->next == NULL) { |
| 836 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 837 | MBEDTLS_ERR_ASN1_ALLOC_FAILED); |
| 838 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 839 | |
| 840 | cur = cur->next; |
| 841 | } |
| 842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 843 | buf = &(cur->buf); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 844 | buf->tag = policy_oid.tag; |
| 845 | buf->p = policy_oid.p; |
| 846 | buf->len = policy_oid.len; |
Ron Eldor | 0806379 | 2019-05-13 16:38:39 +0300 | [diff] [blame] | 847 | |
| 848 | *p += len; |
| 849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | /* |
| 851 | * If there is an optional qualifier, then *p < policy_end |
| 852 | * Check the Qualifier len to verify it doesn't exceed policy_end. |
| 853 | */ |
| 854 | if (*p < policy_end) { |
| 855 | if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len, |
| 856 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != |
| 857 | 0) { |
| 858 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 859 | } |
Ron Eldor | 0806379 | 2019-05-13 16:38:39 +0300 | [diff] [blame] | 860 | /* |
| 861 | * Skip the optional policy qualifiers. |
| 862 | */ |
| 863 | *p += len; |
| 864 | } |
| 865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | if (*p != policy_end) { |
| 867 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 868 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 869 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /* Set final sequence entry's next pointer to NULL */ |
| 873 | cur->next = NULL; |
| 874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 875 | if (*p != end) { |
| 876 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 877 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 878 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 880 | return parse_ret; |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 884 | * X.509 v3 extensions |
| 885 | * |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 886 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 887 | static int x509_get_crt_ext(unsigned char **p, |
| 888 | const unsigned char *end, |
| 889 | mbedtls_x509_crt *crt, |
| 890 | mbedtls_x509_crt_ext_cb_t cb, |
| 891 | void *p_ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 892 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 893 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 894 | size_t len; |
Nicola Di Lieto | c84b1e6 | 2020-06-13 11:08:16 +0200 | [diff] [blame] | 895 | unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | if (*p == end) { |
| 898 | return 0; |
| 899 | } |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 900 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 901 | if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) { |
| 902 | return ret; |
| 903 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 904 | |
Hanno Becker | 12f62fb | 2019-02-12 17:22:36 +0000 | [diff] [blame] | 905 | end = crt->v3_ext.p + crt->v3_ext.len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 906 | while (*p < end) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 907 | /* |
| 908 | * Extension ::= SEQUENCE { |
| 909 | * extnID OBJECT IDENTIFIER, |
| 910 | * critical BOOLEAN DEFAULT FALSE, |
| 911 | * extnValue OCTET STRING } |
| 912 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 913 | mbedtls_x509_buf extn_oid = { 0, 0, NULL }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 914 | int is_critical = 0; /* DEFAULT FALSE */ |
| 915 | int ext_type = 0; |
| 916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 917 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 918 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 919 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 920 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 921 | |
| 922 | end_ext_data = *p + len; |
| 923 | |
| 924 | /* Get extension ID */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len, |
| 926 | MBEDTLS_ASN1_OID)) != 0) { |
| 927 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 928 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 929 | |
k-stachowiak | 463928a | 2018-07-24 12:50:59 +0200 | [diff] [blame] | 930 | extn_oid.tag = MBEDTLS_ASN1_OID; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 931 | extn_oid.p = *p; |
| 932 | *p += extn_oid.len; |
| 933 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 934 | /* Get optional critical */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 935 | if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 && |
| 936 | (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) { |
| 937 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 938 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 939 | |
| 940 | /* Data should be octet string type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 941 | if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len, |
| 942 | MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 943 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret); |
| 944 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 945 | |
Nicola Di Lieto | c84b1e6 | 2020-06-13 11:08:16 +0200 | [diff] [blame] | 946 | start_ext_octet = *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 947 | end_ext_octet = *p + len; |
| 948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | if (end_ext_octet != end_ext_data) { |
| 950 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 951 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 952 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 953 | |
| 954 | /* |
| 955 | * Detect supported extensions |
| 956 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 957 | ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 959 | if (ret != 0) { |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 960 | /* Give the callback (if any) a chance to handle the extension */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | if (cb != NULL) { |
| 962 | ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet); |
| 963 | if (ret != 0 && is_critical) { |
| 964 | return ret; |
| 965 | } |
Nicola Di Lieto | fae25a1 | 2020-05-28 08:55:08 +0200 | [diff] [blame] | 966 | *p = end_ext_octet; |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 967 | continue; |
Nicola Di Lieto | fae25a1 | 2020-05-28 08:55:08 +0200 | [diff] [blame] | 968 | } |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 969 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 970 | /* No parser found, skip extension */ |
| 971 | *p = end_ext_octet; |
| 972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 973 | if (is_critical) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 974 | /* Data is marked as critical: fail */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 975 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 976 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 977 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 978 | continue; |
| 979 | } |
| 980 | |
Manuel Pégourié-Gonnard | 8a5e3d4 | 2014-11-12 17:47:28 +0100 | [diff] [blame] | 981 | /* Forbid repeated extensions */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 982 | if ((crt->ext_types & ext_type) != 0) { |
| 983 | return MBEDTLS_ERR_X509_INVALID_EXTENSIONS; |
| 984 | } |
Manuel Pégourié-Gonnard | 8a5e3d4 | 2014-11-12 17:47:28 +0100 | [diff] [blame] | 985 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 986 | crt->ext_types |= ext_type; |
| 987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 988 | switch (ext_type) { |
| 989 | case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS: |
| 990 | /* Parse basic constraints */ |
| 991 | if ((ret = x509_get_basic_constraints(p, end_ext_octet, |
| 992 | &crt->ca_istrue, &crt->max_pathlen)) != 0) { |
| 993 | return ret; |
| 994 | } |
| 995 | break; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 997 | case MBEDTLS_X509_EXT_KEY_USAGE: |
| 998 | /* Parse key usage */ |
Przemek Stekiel | 21c3728 | 2023-01-16 08:47:49 +0100 | [diff] [blame] | 999 | if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet, |
| 1000 | &crt->key_usage)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1001 | return ret; |
| 1002 | } |
| 1003 | break; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE: |
| 1006 | /* Parse extended key usage */ |
| 1007 | if ((ret = x509_get_ext_key_usage(p, end_ext_octet, |
| 1008 | &crt->ext_key_usage)) != 0) { |
| 1009 | return ret; |
| 1010 | } |
| 1011 | break; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1012 | |
toth92g | a41954d | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 1013 | case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER: |
| 1014 | /* Parse subject key identifier */ |
| 1015 | if ((ret = x509_get_subject_key_id(p, end_ext_data, |
| 1016 | &crt->subject_key_id)) != 0) { |
| 1017 | return ret; |
| 1018 | } |
| 1019 | break; |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 1020 | |
toth92g | a41954d | 2021-02-12 16:11:17 +0100 | [diff] [blame] | 1021 | case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER: |
| 1022 | /* Parse authority key identifier */ |
| 1023 | if ((ret = x509_get_authority_key_id(p, end_ext_octet, |
| 1024 | &crt->authority_key_id)) != 0) { |
| 1025 | return ret; |
| 1026 | } |
| 1027 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1028 | case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME: |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 1029 | /* Parse subject alt name |
| 1030 | * SubjectAltName ::= GeneralNames |
| 1031 | */ |
Przemek Stekiel | 21c3728 | 2023-01-16 08:47:49 +0100 | [diff] [blame] | 1032 | if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet, |
| 1033 | &crt->subject_alt_names)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1034 | return ret; |
| 1035 | } |
| 1036 | break; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1038 | case MBEDTLS_X509_EXT_NS_CERT_TYPE: |
| 1039 | /* Parse netscape certificate type */ |
Przemek Stekiel | 21c3728 | 2023-01-16 08:47:49 +0100 | [diff] [blame] | 1040 | if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet, |
| 1041 | &crt->ns_cert_type)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1042 | return ret; |
| 1043 | } |
| 1044 | break; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1046 | case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES: |
| 1047 | /* Parse certificate policies type */ |
| 1048 | if ((ret = x509_get_certificate_policies(p, end_ext_octet, |
| 1049 | &crt->certificate_policies)) != 0) { |
| 1050 | /* Give the callback (if any) a chance to handle the extension |
| 1051 | * if it contains unsupported policies */ |
| 1052 | if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL && |
| 1053 | cb(p_ctx, crt, &extn_oid, is_critical, |
| 1054 | start_ext_octet, end_ext_octet) == 0) { |
| 1055 | break; |
| 1056 | } |
Nicola Di Lieto | c84b1e6 | 2020-06-13 11:08:16 +0200 | [diff] [blame] | 1057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | if (is_critical) { |
| 1059 | return ret; |
| 1060 | } else |
| 1061 | /* |
| 1062 | * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we |
| 1063 | * cannot interpret or enforce the policy. However, it is up to |
| 1064 | * the user to choose how to enforce the policies, |
| 1065 | * unless the extension is critical. |
| 1066 | */ |
| 1067 | if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) { |
| 1068 | return ret; |
| 1069 | } |
| 1070 | } |
| 1071 | break; |
| 1072 | |
| 1073 | default: |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 1074 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | * If this is a non-critical extension, which the oid layer |
| 1076 | * supports, but there isn't an x509 parser for it, |
| 1077 | * skip the extension. |
Ron Eldor | 8b0c3c9 | 2019-05-15 12:20:00 +0300 | [diff] [blame] | 1078 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1079 | if (is_critical) { |
| 1080 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 1081 | } else { |
| 1082 | *p = end_ext_octet; |
| 1083 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1087 | if (*p != end) { |
| 1088 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, |
| 1089 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 1090 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1091 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1092 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Parse and fill a single X.509 certificate in DER format |
| 1097 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1098 | static int x509_crt_parse_der_core(mbedtls_x509_crt *crt, |
| 1099 | const unsigned char *buf, |
| 1100 | size_t buflen, |
| 1101 | int make_copy, |
| 1102 | mbedtls_x509_crt_ext_cb_t cb, |
| 1103 | void *p_ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1104 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1105 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1106 | size_t len; |
| 1107 | unsigned char *p, *end, *crt_end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1108 | mbedtls_x509_buf sig_params1, sig_params2, sig_oid2; |
Manuel Pégourié-Gonnard | 59a75d5 | 2014-01-22 10:12:57 +0100 | [diff] [blame] | 1109 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1110 | memset(&sig_params1, 0, sizeof(mbedtls_x509_buf)); |
| 1111 | memset(&sig_params2, 0, sizeof(mbedtls_x509_buf)); |
| 1112 | memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1113 | |
| 1114 | /* |
| 1115 | * Check for valid input |
| 1116 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1117 | if (crt == NULL || buf == NULL) { |
| 1118 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1119 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1120 | |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1121 | /* Use the original buffer until we figure out actual length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1122 | p = (unsigned char *) buf; |
Janos Follath | cc0e49d | 2016-02-17 14:34:12 +0000 | [diff] [blame] | 1123 | len = buflen; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1124 | end = p + len; |
| 1125 | |
| 1126 | /* |
| 1127 | * Certificate ::= SEQUENCE { |
| 1128 | * tbsCertificate TBSCertificate, |
| 1129 | * signatureAlgorithm AlgorithmIdentifier, |
| 1130 | * signatureValue BIT STRING } |
| 1131 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1132 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1133 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1134 | mbedtls_x509_crt_free(crt); |
| 1135 | return MBEDTLS_ERR_X509_INVALID_FORMAT; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
Janos Follath | cc0e49d | 2016-02-17 14:34:12 +0000 | [diff] [blame] | 1138 | end = crt_end = p + len; |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1139 | crt->raw.len = crt_end - buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1140 | if (make_copy != 0) { |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1141 | /* Create and populate a new buffer for the raw field. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1142 | crt->raw.p = p = mbedtls_calloc(1, crt->raw.len); |
| 1143 | if (crt->raw.p == NULL) { |
| 1144 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 1145 | } |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1147 | memcpy(crt->raw.p, buf, crt->raw.len); |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1148 | crt->own_buffer = 1; |
| 1149 | |
| 1150 | p += crt->raw.len - len; |
| 1151 | end = crt_end = p + len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1152 | } else { |
| 1153 | crt->raw.p = (unsigned char *) buf; |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1154 | crt->own_buffer = 0; |
| 1155 | } |
Janos Follath | cc0e49d | 2016-02-17 14:34:12 +0000 | [diff] [blame] | 1156 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1157 | /* |
| 1158 | * TBSCertificate ::= SEQUENCE { |
| 1159 | */ |
| 1160 | crt->tbs.p = p; |
| 1161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1163 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1164 | mbedtls_x509_crt_free(crt); |
| 1165 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | end = p + len; |
| 1169 | crt->tbs.len = end - crt->tbs.p; |
| 1170 | |
| 1171 | /* |
| 1172 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 1173 | * |
| 1174 | * CertificateSerialNumber ::= INTEGER |
| 1175 | * |
| 1176 | * signature AlgorithmIdentifier |
| 1177 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1178 | if ((ret = x509_get_version(&p, end, &crt->version)) != 0 || |
| 1179 | (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 || |
| 1180 | (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid, |
| 1181 | &sig_params1)) != 0) { |
| 1182 | mbedtls_x509_crt_free(crt); |
| 1183 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1186 | if (crt->version < 0 || crt->version > 2) { |
| 1187 | mbedtls_x509_crt_free(crt); |
| 1188 | return MBEDTLS_ERR_X509_UNKNOWN_VERSION; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1189 | } |
| 1190 | |
Andres AG | 7ca4a03 | 2017-03-09 16:16:11 +0000 | [diff] [blame] | 1191 | crt->version++; |
| 1192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1, |
| 1194 | &crt->sig_md, &crt->sig_pk, |
| 1195 | &crt->sig_opts)) != 0) { |
| 1196 | mbedtls_x509_crt_free(crt); |
| 1197 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * issuer Name |
| 1202 | */ |
| 1203 | crt->issuer_raw.p = p; |
| 1204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1205 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1206 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1207 | mbedtls_x509_crt_free(crt); |
| 1208 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1211 | if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) { |
| 1212 | mbedtls_x509_crt_free(crt); |
| 1213 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | crt->issuer_raw.len = p - crt->issuer_raw.p; |
| 1217 | |
| 1218 | /* |
| 1219 | * Validity ::= SEQUENCE { |
| 1220 | * notBefore Time, |
| 1221 | * notAfter Time } |
| 1222 | * |
| 1223 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1224 | if ((ret = x509_get_dates(&p, end, &crt->valid_from, |
| 1225 | &crt->valid_to)) != 0) { |
| 1226 | mbedtls_x509_crt_free(crt); |
| 1227 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * subject Name |
| 1232 | */ |
| 1233 | crt->subject_raw.p = p; |
| 1234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1236 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1237 | mbedtls_x509_crt_free(crt); |
| 1238 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1241 | if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) { |
| 1242 | mbedtls_x509_crt_free(crt); |
| 1243 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | crt->subject_raw.len = p - crt->subject_raw.p; |
| 1247 | |
| 1248 | /* |
| 1249 | * SubjectPublicKeyInfo |
| 1250 | */ |
Hanno Becker | 494dd7a | 2019-02-06 16:13:41 +0000 | [diff] [blame] | 1251 | crt->pk_raw.p = p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1252 | if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) { |
| 1253 | mbedtls_x509_crt_free(crt); |
| 1254 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1255 | } |
Hanno Becker | 494dd7a | 2019-02-06 16:13:41 +0000 | [diff] [blame] | 1256 | crt->pk_raw.len = p - crt->pk_raw.p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1257 | |
| 1258 | /* |
| 1259 | * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, |
| 1260 | * -- If present, version shall be v2 or v3 |
| 1261 | * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, |
| 1262 | * -- If present, version shall be v2 or v3 |
| 1263 | * extensions [3] EXPLICIT Extensions OPTIONAL |
| 1264 | * -- If present, version shall be v3 |
| 1265 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1266 | if (crt->version == 2 || crt->version == 3) { |
| 1267 | ret = x509_get_uid(&p, end, &crt->issuer_id, 1); |
| 1268 | if (ret != 0) { |
| 1269 | mbedtls_x509_crt_free(crt); |
| 1270 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1274 | if (crt->version == 2 || crt->version == 3) { |
| 1275 | ret = x509_get_uid(&p, end, &crt->subject_id, 2); |
| 1276 | if (ret != 0) { |
| 1277 | mbedtls_x509_crt_free(crt); |
| 1278 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1282 | if (crt->version == 3) { |
| 1283 | ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx); |
| 1284 | if (ret != 0) { |
| 1285 | mbedtls_x509_crt_free(crt); |
| 1286 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1290 | if (p != end) { |
| 1291 | mbedtls_x509_crt_free(crt); |
| 1292 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, |
| 1293 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | end = crt_end; |
| 1297 | |
| 1298 | /* |
| 1299 | * } |
| 1300 | * -- end of TBSCertificate |
| 1301 | * |
| 1302 | * signatureAlgorithm AlgorithmIdentifier, |
| 1303 | * signatureValue BIT STRING |
| 1304 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1305 | if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) { |
| 1306 | mbedtls_x509_crt_free(crt); |
| 1307 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1310 | if (crt->sig_oid.len != sig_oid2.len || |
| 1311 | memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 || |
Paul Elliott | ca17ebf | 2020-11-24 17:30:18 +0000 | [diff] [blame] | 1312 | sig_params1.tag != sig_params2.tag || |
Manuel Pégourié-Gonnard | dddbb1d | 2014-06-05 17:02:24 +0200 | [diff] [blame] | 1313 | sig_params1.len != sig_params2.len || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | (sig_params1.len != 0 && |
| 1315 | memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) { |
| 1316 | mbedtls_x509_crt_free(crt); |
| 1317 | return MBEDTLS_ERR_X509_SIG_MISMATCH; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1318 | } |
| 1319 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1320 | if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) { |
| 1321 | mbedtls_x509_crt_free(crt); |
| 1322 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1323 | } |
| 1324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1325 | if (p != end) { |
| 1326 | mbedtls_x509_crt_free(crt); |
| 1327 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, |
| 1328 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1329 | } |
| 1330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1331 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* |
| 1335 | * Parse one X.509 certificate in DER format from a buffer and add them to a |
| 1336 | * chained list |
| 1337 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1338 | static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain, |
| 1339 | const unsigned char *buf, |
| 1340 | size_t buflen, |
| 1341 | int make_copy, |
| 1342 | mbedtls_x509_crt_ext_cb_t cb, |
| 1343 | void *p_ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1344 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1345 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1346 | mbedtls_x509_crt *crt = chain, *prev = NULL; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1347 | |
| 1348 | /* |
| 1349 | * Check for valid input |
| 1350 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1351 | if (crt == NULL || buf == NULL) { |
| 1352 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1353 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1355 | while (crt->version != 0 && crt->next != NULL) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1356 | prev = crt; |
| 1357 | crt = crt->next; |
| 1358 | } |
| 1359 | |
| 1360 | /* |
| 1361 | * Add new certificate on the end of the chain if needed. |
| 1362 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | if (crt->version != 0 && crt->next == NULL) { |
| 1364 | crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1366 | if (crt->next == NULL) { |
| 1367 | return MBEDTLS_ERR_X509_ALLOC_FAILED; |
| 1368 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1369 | |
| 1370 | prev = crt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1371 | mbedtls_x509_crt_init(crt->next); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1372 | crt = crt->next; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1375 | ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx); |
| 1376 | if (ret != 0) { |
| 1377 | if (prev) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1378 | prev->next = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1379 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1381 | if (crt != chain) { |
| 1382 | mbedtls_free(crt); |
| 1383 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1385 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1386 | } |
| 1387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1388 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1391 | int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain, |
| 1392 | const unsigned char *buf, |
| 1393 | size_t buflen) |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1394 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1395 | return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL); |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1398 | int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain, |
| 1399 | const unsigned char *buf, |
| 1400 | size_t buflen, |
| 1401 | int make_copy, |
| 1402 | mbedtls_x509_crt_ext_cb_t cb, |
| 1403 | void *p_ctx) |
Nicola Di Lieto | 502d4b4 | 2020-04-25 14:41:25 +0200 | [diff] [blame] | 1404 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1405 | return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx); |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1408 | int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain, |
| 1409 | const unsigned char *buf, |
| 1410 | size_t buflen) |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1411 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1412 | return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL); |
Hanno Becker | 1a65dcd | 2019-01-31 08:57:44 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1415 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1416 | * Parse one or more PEM certificates from a buffer and add them to the chained |
| 1417 | * list |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1418 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, |
| 1420 | const unsigned char *buf, |
| 1421 | size_t buflen) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1422 | { |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1423 | #if defined(MBEDTLS_PEM_PARSE_C) |
Andres AG | c0db511 | 2016-12-07 15:05:53 +0000 | [diff] [blame] | 1424 | int success = 0, first_error = 0, total_failed = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1425 | int buf_format = MBEDTLS_X509_FORMAT_DER; |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1426 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1427 | |
| 1428 | /* |
| 1429 | * Check for valid input |
| 1430 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1431 | if (chain == NULL || buf == NULL) { |
| 1432 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1433 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1434 | |
| 1435 | /* |
| 1436 | * Determine buffer content. Buffer contains either one DER certificate or |
| 1437 | * one or more PEM certificates. |
| 1438 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1439 | #if defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1440 | if (buflen != 0 && buf[buflen - 1] == '\0' && |
| 1441 | strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1442 | buf_format = MBEDTLS_X509_FORMAT_PEM; |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1443 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1445 | if (buf_format == MBEDTLS_X509_FORMAT_DER) { |
| 1446 | return mbedtls_x509_crt_parse_der(chain, buf, buflen); |
| 1447 | } |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1448 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1449 | return mbedtls_x509_crt_parse_der(chain, buf, buflen); |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1450 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1451 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1452 | #if defined(MBEDTLS_PEM_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1453 | if (buf_format == MBEDTLS_X509_FORMAT_PEM) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1454 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1455 | mbedtls_pem_context pem; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1456 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1457 | /* 1 rather than 0 since the terminating NULL byte is counted in */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | while (buflen > 1) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1459 | size_t use_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | mbedtls_pem_init(&pem); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1461 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1462 | /* If we get there, we know the string is null-terminated */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1463 | ret = mbedtls_pem_read_buffer(&pem, |
| 1464 | "-----BEGIN CERTIFICATE-----", |
| 1465 | "-----END CERTIFICATE-----", |
| 1466 | buf, NULL, 0, &use_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1468 | if (ret == 0) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1469 | /* |
| 1470 | * Was PEM encoded |
| 1471 | */ |
| 1472 | buflen -= use_len; |
| 1473 | buf += use_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1474 | } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) { |
| 1475 | return ret; |
| 1476 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1477 | mbedtls_pem_free(&pem); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1478 | |
| 1479 | /* |
| 1480 | * PEM header and footer were found |
| 1481 | */ |
| 1482 | buflen -= use_len; |
| 1483 | buf += use_len; |
| 1484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1485 | if (first_error == 0) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1486 | first_error = ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1487 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1488 | |
Paul Bakker | 5a5fa92 | 2014-09-26 14:53:04 +0200 | [diff] [blame] | 1489 | total_failed++; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1490 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1491 | } else { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1492 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1493 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1495 | ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1497 | mbedtls_pem_free(&pem); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1499 | if (ret != 0) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1500 | /* |
| 1501 | * Quit parsing on a memory error |
| 1502 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1503 | if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) { |
| 1504 | return ret; |
| 1505 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1507 | if (first_error == 0) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1508 | first_error = ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1509 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1510 | |
| 1511 | total_failed++; |
| 1512 | continue; |
| 1513 | } |
| 1514 | |
| 1515 | success = 1; |
| 1516 | } |
| 1517 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1519 | if (success) { |
| 1520 | return total_failed; |
| 1521 | } else if (first_error) { |
| 1522 | return first_error; |
| 1523 | } else { |
| 1524 | return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT; |
| 1525 | } |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1526 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1527 | } |
| 1528 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1529 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1530 | /* |
| 1531 | * Load one or more certificates and add them to the chained list |
| 1532 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1533 | int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1534 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1535 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1536 | size_t n; |
| 1537 | unsigned char *buf; |
| 1538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1539 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 1540 | return ret; |
| 1541 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1543 | ret = mbedtls_x509_crt_parse(chain, buf, n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1544 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 1545 | mbedtls_zeroize_and_free(buf, n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1547 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1548 | } |
| 1549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1550 | int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1551 | { |
| 1552 | int ret = 0; |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 1553 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Steve Lhomme | 369d7c7 | 2023-06-16 14:16:03 +0200 | [diff] [blame] | 1554 | #if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_XP */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1555 | int w_ret; |
| 1556 | WCHAR szDir[MAX_PATH]; |
| 1557 | char filename[MAX_PATH]; |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1558 | char *p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1559 | size_t len = strlen(path); |
Simon Butcher | de573f5 | 2018-07-05 09:11:30 +0100 | [diff] [blame] | 1560 | int length_as_int = 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1561 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1562 | WIN32_FIND_DATAW file_data; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1563 | HANDLE hFind; |
| 1564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1565 | if (len > MAX_PATH - 3) { |
| 1566 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1567 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | memset(szDir, 0, sizeof(szDir)); |
| 1570 | memset(filename, 0, MAX_PATH); |
| 1571 | memcpy(filename, path, len); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1572 | filename[len++] = '\\'; |
| 1573 | p = filename + len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1574 | filename[len++] = '*'; |
| 1575 | |
Minos Galanakis | a277b21 | 2023-08-09 16:32:22 +0100 | [diff] [blame] | 1576 | if (FAILED(SizeTToInt(len, &length_as_int))) { |
| 1577 | return MBEDTLS_ERR_X509_FILE_IO_ERROR; |
| 1578 | } |
Kevin Kane | 0ec1e68 | 2016-12-15 09:27:16 -0800 | [diff] [blame] | 1579 | |
Simon Butcher | 35e5dad | 2018-03-15 15:00:03 +0000 | [diff] [blame] | 1580 | /* |
| 1581 | * Note this function uses the code page CP_ACP, and assumes the incoming |
| 1582 | * string is encoded in ANSI, before translating it into Unicode. If the |
| 1583 | * incoming string were changed to be UTF-8, then the length check needs to |
| 1584 | * change to check the number of characters, not the number of bytes, in the |
| 1585 | * incoming string are less than MAX_PATH to avoid a buffer overrun with |
| 1586 | * MultiByteToWideChar(). |
| 1587 | */ |
Simon Butcher | de573f5 | 2018-07-05 09:11:30 +0100 | [diff] [blame] | 1588 | w_ret = MultiByteToWideChar(CP_ACP, 0, filename, length_as_int, szDir, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1589 | MAX_PATH - 3); |
| 1590 | if (w_ret == 0) { |
| 1591 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1592 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1594 | hFind = FindFirstFileW(szDir, &file_data); |
| 1595 | if (hFind == INVALID_HANDLE_VALUE) { |
| 1596 | return MBEDTLS_ERR_X509_FILE_IO_ERROR; |
| 1597 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1598 | |
| 1599 | len = MAX_PATH - len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1600 | do { |
| 1601 | memset(p, 0, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1604 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1605 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1606 | |
Minos Galanakis | a277b21 | 2023-08-09 16:32:22 +0100 | [diff] [blame] | 1607 | if (FAILED(SizeTToInt(wcslen(file_data.cFileName), &length_as_int))) { |
| 1608 | return MBEDTLS_ERR_X509_FILE_IO_ERROR; |
| 1609 | } |
Kevin Kane | 0ec1e68 | 2016-12-15 09:27:16 -0800 | [diff] [blame] | 1610 | |
Simon Butcher | de573f5 | 2018-07-05 09:11:30 +0100 | [diff] [blame] | 1611 | w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName, |
| 1612 | length_as_int, |
| 1613 | p, (int) len - 1, |
| 1614 | NULL, NULL); |
Minos Galanakis | a277b21 | 2023-08-09 16:32:22 +0100 | [diff] [blame] | 1615 | if (w_ret == 0) { |
Ron Eldor | 36d9042 | 2017-01-09 15:09:16 +0200 | [diff] [blame] | 1616 | ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; |
| 1617 | goto cleanup; |
| 1618 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1620 | w_ret = mbedtls_x509_crt_parse_file(chain, filename); |
| 1621 | if (w_ret < 0) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1622 | ret++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | } else { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1624 | ret += w_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1625 | } |
| 1626 | } while (FindNextFileW(hFind, &file_data) != 0); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1627 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1628 | if (GetLastError() != ERROR_NO_MORE_FILES) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1629 | ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1630 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1631 | |
Ron Eldor | 36d9042 | 2017-01-09 15:09:16 +0200 | [diff] [blame] | 1632 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1633 | FindClose(hFind); |
Steve Lhomme | 369d7c7 | 2023-06-16 14:16:03 +0200 | [diff] [blame] | 1634 | #else /* !_WIN32_WINNT_XP */ |
Antonio de Angelis | 1ee4d12 | 2023-08-16 12:26:37 +0100 | [diff] [blame] | 1635 | #error "mbedtls_x509_crt_parse_path not available before Windows XP" |
Steve Lhomme | 369d7c7 | 2023-06-16 14:16:03 +0200 | [diff] [blame] | 1636 | #endif /* !_WIN32_WINNT_XP */ |
Paul Bakker | be089b0 | 2013-10-14 15:51:50 +0200 | [diff] [blame] | 1637 | #else /* _WIN32 */ |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1638 | int t_ret; |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1639 | int snp_ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1640 | struct stat sb; |
Manuel Pégourié-Gonnard | 964bf9b | 2013-11-26 16:47:11 +0100 | [diff] [blame] | 1641 | struct dirent *entry; |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1642 | char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1643 | DIR *dir = opendir(path); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1645 | if (dir == NULL) { |
| 1646 | return MBEDTLS_ERR_X509_FILE_IO_ERROR; |
| 1647 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1648 | |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1649 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1650 | if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) { |
| 1651 | closedir(dir); |
| 1652 | return ret; |
Manuel Pégourié-Gonnard | f9b85d9 | 2015-06-22 18:39:57 +0200 | [diff] [blame] | 1653 | } |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1654 | #endif /* MBEDTLS_THREADING_C */ |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1656 | memset(&sb, 0, sizeof(sb)); |
Paul Elliott | fb91a48 | 2021-03-05 14:17:51 +0000 | [diff] [blame] | 1657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1658 | while ((entry = readdir(dir)) != NULL) { |
Dave Rodgman | 6dd757a | 2023-02-02 12:40:50 +0000 | [diff] [blame] | 1659 | snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1660 | "%s/%s", path, entry->d_name); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1661 | |
Dave Rodgman | 6dd757a | 2023-02-02 12:40:50 +0000 | [diff] [blame] | 1662 | if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) { |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1663 | ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 1664 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1665 | } else if (stat(entry_name, &sb) == -1) { |
| 1666 | if (errno == ENOENT) { |
Dave Rodgman | fa40b02 | 2022-07-20 16:08:00 +0100 | [diff] [blame] | 1667 | /* Broken symbolic link - ignore this entry. |
| 1668 | stat(2) will return this error for either (a) a dangling |
| 1669 | symlink or (b) a missing file. |
| 1670 | Given that we have just obtained the filename from readdir, |
| 1671 | assume that it does exist and therefore treat this as a |
| 1672 | dangling symlink. */ |
| 1673 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | } else { |
Dave Rodgman | fa40b02 | 2022-07-20 16:08:00 +0100 | [diff] [blame] | 1675 | /* Some other file error; report the error. */ |
Eduardo Silva | e1bfffc | 2019-04-25 10:43:26 -0600 | [diff] [blame] | 1676 | ret = MBEDTLS_ERR_X509_FILE_IO_ERROR; |
| 1677 | goto cleanup; |
| 1678 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1679 | } |
| 1680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1681 | if (!S_ISREG(sb.st_mode)) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1682 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1683 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1684 | |
| 1685 | // Ignore parse errors |
| 1686 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1687 | t_ret = mbedtls_x509_crt_parse_file(chain, entry_name); |
| 1688 | if (t_ret < 0) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1689 | ret++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1690 | } else { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1691 | ret += t_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1692 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1693 | } |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1694 | |
| 1695 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1696 | closedir(dir); |
Andres AG | f911319 | 2016-09-02 14:06:04 +0100 | [diff] [blame] | 1697 | |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1698 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1699 | if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1700 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1701 | } |
Ron Eldor | 6314068 | 2017-01-09 19:27:59 +0200 | [diff] [blame] | 1702 | #endif /* MBEDTLS_THREADING_C */ |
Manuel Pégourié-Gonnard | 5ad68e4 | 2013-11-28 17:11:54 +0100 | [diff] [blame] | 1703 | |
Paul Bakker | be089b0 | 2013-10-14 15:51:50 +0200 | [diff] [blame] | 1704 | #endif /* _WIN32 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1707 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1708 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1709 | |
Peter Kolbus | 9a969b6 | 2018-12-11 13:55:56 -0600 | [diff] [blame] | 1710 | #if !defined(MBEDTLS_X509_REMOVE_INFO) |
Przemek Stekiel | f419494 | 2023-04-24 09:52:17 +0200 | [diff] [blame] | 1711 | #define PRINT_ITEM(i) \ |
| 1712 | do { \ |
| 1713 | ret = mbedtls_snprintf(p, n, "%s" i, sep); \ |
| 1714 | MBEDTLS_X509_SAFE_SNPRINTF; \ |
| 1715 | sep = ", "; \ |
| 1716 | } while (0) |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 1717 | |
Przemek Stekiel | f419494 | 2023-04-24 09:52:17 +0200 | [diff] [blame] | 1718 | #define CERT_TYPE(type, name) \ |
| 1719 | do { \ |
Przemek Stekiel | f5b8f78 | 2023-04-26 08:55:26 +0200 | [diff] [blame] | 1720 | if (ns_cert_type & (type)) { \ |
| 1721 | PRINT_ITEM(name); \ |
| 1722 | } \ |
Przemek Stekiel | f419494 | 2023-04-24 09:52:17 +0200 | [diff] [blame] | 1723 | } while (0) |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 1724 | |
Przemek Stekiel | f419494 | 2023-04-24 09:52:17 +0200 | [diff] [blame] | 1725 | #define KEY_USAGE(code, name) \ |
| 1726 | do { \ |
Przemek Stekiel | f5b8f78 | 2023-04-26 08:55:26 +0200 | [diff] [blame] | 1727 | if (key_usage & (code)) { \ |
| 1728 | PRINT_ITEM(name); \ |
| 1729 | } \ |
Przemek Stekiel | f419494 | 2023-04-24 09:52:17 +0200 | [diff] [blame] | 1730 | } while (0) |
toth92g | 8d435a0 | 2021-05-10 15:16:33 +0200 | [diff] [blame] | 1731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1732 | static int x509_info_ext_key_usage(char **buf, size_t *size, |
| 1733 | const mbedtls_x509_sequence *extended_key_usage) |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1734 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1735 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1736 | const char *desc; |
| 1737 | size_t n = *size; |
| 1738 | char *p = *buf; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1739 | const mbedtls_x509_sequence *cur = extended_key_usage; |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1740 | const char *sep = ""; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1742 | while (cur != NULL) { |
| 1743 | if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0) { |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1744 | desc = "???"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | } |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1747 | ret = mbedtls_snprintf(p, n, "%s%s", sep, desc); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1748 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1749 | |
Manuel Pégourié-Gonnard | 7b30cfc | 2014-04-01 18:00:07 +0200 | [diff] [blame] | 1750 | sep = ", "; |
| 1751 | |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1752 | cur = cur->next; |
| 1753 | } |
| 1754 | |
| 1755 | *size = n; |
| 1756 | *buf = p; |
| 1757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1758 | return 0; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1759 | } |
| 1760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1761 | static int x509_info_cert_policies(char **buf, size_t *size, |
| 1762 | const mbedtls_x509_sequence *certificate_policies) |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1763 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1764 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1765 | const char *desc; |
| 1766 | size_t n = *size; |
| 1767 | char *p = *buf; |
| 1768 | const mbedtls_x509_sequence *cur = certificate_policies; |
| 1769 | const char *sep = ""; |
| 1770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | while (cur != NULL) { |
| 1772 | if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0) { |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1773 | desc = "???"; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1774 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1776 | ret = mbedtls_snprintf(p, n, "%s%s", sep, desc); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1777 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1778 | |
| 1779 | sep = ", "; |
| 1780 | |
| 1781 | cur = cur->next; |
| 1782 | } |
| 1783 | |
| 1784 | *size = n; |
| 1785 | *buf = p; |
| 1786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1787 | return 0; |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1788 | } |
| 1789 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1790 | /* |
| 1791 | * Return an informational string about the certificate. |
| 1792 | */ |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1793 | #define BEFORE_COLON 18 |
| 1794 | #define BC "18" |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1795 | int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix, |
| 1796 | const mbedtls_x509_crt *crt) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1797 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1798 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1799 | size_t n; |
| 1800 | char *p; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1801 | char key_size_str[BEFORE_COLON]; |
| 1802 | |
| 1803 | p = buf; |
| 1804 | n = size; |
| 1805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1806 | if (NULL == crt) { |
| 1807 | ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n"); |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1808 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | return (int) (size - n); |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 1811 | } |
| 1812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1813 | ret = mbedtls_snprintf(p, n, "%scert. version : %d\n", |
| 1814 | prefix, crt->version); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1815 | MBEDTLS_X509_SAFE_SNPRINTF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1816 | ret = mbedtls_snprintf(p, n, "%sserial number : ", |
| 1817 | prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1818 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1820 | ret = mbedtls_x509_serial_gets(p, n, &crt->serial); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1821 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1824 | MBEDTLS_X509_SAFE_SNPRINTF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1825 | ret = mbedtls_x509_dn_gets(p, n, &crt->issuer); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1826 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1827 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1828 | ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1829 | MBEDTLS_X509_SAFE_SNPRINTF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1830 | ret = mbedtls_x509_dn_gets(p, n, &crt->subject); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1831 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1833 | ret = mbedtls_snprintf(p, n, "\n%sissued on : " \ |
| 1834 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 1835 | crt->valid_from.year, crt->valid_from.mon, |
| 1836 | crt->valid_from.day, crt->valid_from.hour, |
| 1837 | crt->valid_from.min, crt->valid_from.sec); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1838 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1840 | ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \ |
| 1841 | "%04d-%02d-%02d %02d:%02d:%02d", prefix, |
| 1842 | crt->valid_to.year, crt->valid_to.mon, |
| 1843 | crt->valid_to.day, crt->valid_to.hour, |
| 1844 | crt->valid_to.min, crt->valid_to.sec); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1845 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1847 | ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1848 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1850 | ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk, |
| 1851 | crt->sig_md, crt->sig_opts); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1852 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1853 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1854 | /* Key size */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1855 | if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON, |
| 1856 | mbedtls_pk_get_name(&crt->pk))) != 0) { |
| 1857 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1858 | } |
| 1859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1860 | ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str, |
| 1861 | (int) mbedtls_pk_get_bitlen(&crt->pk)); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1862 | MBEDTLS_X509_SAFE_SNPRINTF; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1863 | |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1864 | /* |
| 1865 | * Optional extensions |
| 1866 | */ |
| 1867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1868 | if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) { |
| 1869 | ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix, |
| 1870 | crt->ca_istrue ? "true" : "false"); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1871 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1872 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1873 | if (crt->max_pathlen > 0) { |
| 1874 | ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1875 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1879 | if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) { |
| 1880 | ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1881 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | bce2b30 | 2014-04-01 13:43:28 +0200 | [diff] [blame] | 1882 | |
Przemek Stekiel | 21c3728 | 2023-01-16 08:47:49 +0100 | [diff] [blame] | 1883 | if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n, |
| 1884 | &crt->subject_alt_names, |
| 1885 | prefix)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | return ret; |
| 1887 | } |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1890 | if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) { |
| 1891 | ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1892 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 919f8f5 | 2014-04-01 13:01:11 +0200 | [diff] [blame] | 1893 | |
Przemek Stekiel | 21c3728 | 2023-01-16 08:47:49 +0100 | [diff] [blame] | 1894 | if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | return ret; |
| 1896 | } |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1897 | } |
| 1898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) { |
| 1900 | ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1901 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | 65c2ddc | 2014-04-01 14:12:11 +0200 | [diff] [blame] | 1902 | |
Przemek Stekiel | 21c3728 | 2023-01-16 08:47:49 +0100 | [diff] [blame] | 1903 | if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1904 | return ret; |
| 1905 | } |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1906 | } |
| 1907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) { |
| 1909 | ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1910 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | f6f4ab4 | 2014-04-01 17:32:44 +0200 | [diff] [blame] | 1911 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1912 | if ((ret = x509_info_ext_key_usage(&p, &n, |
| 1913 | &crt->ext_key_usage)) != 0) { |
| 1914 | return ret; |
| 1915 | } |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1916 | } |
| 1917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1918 | if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) { |
| 1919 | ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1920 | MBEDTLS_X509_SAFE_SNPRINTF; |
| 1921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1922 | if ((ret = x509_info_cert_policies(&p, &n, |
| 1923 | &crt->certificate_policies)) != 0) { |
| 1924 | return ret; |
| 1925 | } |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 1926 | } |
| 1927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1928 | ret = mbedtls_snprintf(p, n, "\n"); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1929 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b28487d | 2014-04-01 12:19:09 +0200 | [diff] [blame] | 1930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1931 | return (int) (size - n); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1932 | } |
| 1933 | |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1934 | struct x509_crt_verify_string { |
| 1935 | int code; |
| 1936 | const char *string; |
| 1937 | }; |
| 1938 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1939 | #define X509_CRT_ERROR_INFO(err, err_str, info) { err, info }, |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1940 | static const struct x509_crt_verify_string x509_crt_verify_strings[] = { |
Hanno Becker | 7ac83f9 | 2020-10-09 10:48:22 +0100 | [diff] [blame] | 1941 | MBEDTLS_X509_CRT_ERROR_INFO_LIST |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1942 | { 0, NULL } |
| 1943 | }; |
Hanno Becker | 7ac83f9 | 2020-10-09 10:48:22 +0100 | [diff] [blame] | 1944 | #undef X509_CRT_ERROR_INFO |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1946 | int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix, |
| 1947 | uint32_t flags) |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1948 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1949 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1950 | const struct x509_crt_verify_string *cur; |
| 1951 | char *p = buf; |
| 1952 | size_t n = size; |
| 1953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1954 | for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) { |
| 1955 | if ((flags & cur->code) == 0) { |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1956 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | } |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1959 | ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1960 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1961 | flags ^= cur->code; |
| 1962 | } |
| 1963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1964 | if (flags != 0) { |
| 1965 | ret = mbedtls_snprintf(p, n, "%sUnknown reason " |
| 1966 | "(this should not happen)\n", prefix); |
Manuel Pégourié-Gonnard | 1685368 | 2015-06-22 11:12:02 +0200 | [diff] [blame] | 1967 | MBEDTLS_X509_SAFE_SNPRINTF; |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1968 | } |
| 1969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1970 | return (int) (size - n); |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1971 | } |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 1972 | #endif /* MBEDTLS_X509_REMOVE_INFO */ |
Manuel Pégourié-Gonnard | b5f48ad | 2015-04-20 10:38:13 +0100 | [diff] [blame] | 1973 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1974 | int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt, |
| 1975 | unsigned int usage) |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 1976 | { |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 1977 | unsigned int usage_must, usage_may; |
| 1978 | unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1979 | | MBEDTLS_X509_KU_DECIPHER_ONLY; |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 1980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1981 | if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) { |
| 1982 | return 0; |
| 1983 | } |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 1984 | |
| 1985 | usage_must = usage & ~may_mask; |
| 1986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1987 | if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) { |
| 1988 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1989 | } |
Manuel Pégourié-Gonnard | 655a964 | 2015-06-23 10:48:44 +0200 | [diff] [blame] | 1990 | |
| 1991 | usage_may = usage & may_mask; |
| 1992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1993 | if (((crt->key_usage & may_mask) | usage_may) != usage_may) { |
| 1994 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 1995 | } |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 1996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1997 | return 0; |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 1998 | } |
Manuel Pégourié-Gonnard | 603116c | 2014-04-09 09:50:03 +0200 | [diff] [blame] | 1999 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2000 | int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt, |
| 2001 | const char *usage_oid, |
| 2002 | size_t usage_len) |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2003 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2004 | const mbedtls_x509_sequence *cur; |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2005 | |
| 2006 | /* Extension is not mandatory, absent means no restriction */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2007 | if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) { |
| 2008 | return 0; |
| 2009 | } |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2010 | |
| 2011 | /* |
| 2012 | * Look for the requested usage (or wildcard ANY) in our list |
| 2013 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2014 | for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2015 | const mbedtls_x509_buf *cur_oid = &cur->buf; |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2016 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2017 | if (cur_oid->len == usage_len && |
| 2018 | memcmp(cur_oid->p, usage_oid, usage_len) == 0) { |
| 2019 | return 0; |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2022 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) { |
| 2023 | return 0; |
| 2024 | } |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2025 | } |
| 2026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2027 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2028 | } |
Manuel Pégourié-Gonnard | 7afb8a0 | 2014-04-10 17:53:56 +0200 | [diff] [blame] | 2029 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2030 | #if defined(MBEDTLS_X509_CRL_PARSE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2031 | /* |
| 2032 | * Return 1 if the certificate is revoked, or 0 otherwise. |
| 2033 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2034 | int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2035 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2036 | const mbedtls_x509_crl_entry *cur = &crl->entry; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2038 | while (cur != NULL && cur->serial.len != 0) { |
| 2039 | if (crt->serial.len == cur->serial.len && |
| 2040 | memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) { |
| 2041 | return 1; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | cur = cur->next; |
| 2045 | } |
| 2046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2047 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | /* |
Manuel Pégourié-Gonnard | eeef947 | 2016-02-22 11:36:55 +0100 | [diff] [blame] | 2051 | * Check that the given certificate is not revoked according to the CRL. |
Manuel Pégourié-Gonnard | 08eacec | 2017-10-18 14:20:24 +0200 | [diff] [blame] | 2052 | * Skip validation if no CRL for the given CA is present. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2053 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2054 | static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca, |
| 2055 | mbedtls_x509_crl *crl_list, |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2056 | const mbedtls_x509_crt_profile *profile, |
| 2057 | const mbedtls_x509_time *now) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2058 | { |
| 2059 | int flags = 0; |
Manuel Pégourié-Gonnard | 8857984 | 2023-03-28 11:20:23 +0200 | [diff] [blame] | 2060 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2061 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2062 | psa_algorithm_t psa_algorithm; |
| 2063 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2064 | const mbedtls_md_info_t *md_info; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2065 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2066 | size_t hash_length; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2068 | if (ca == NULL) { |
| 2069 | return flags; |
| 2070 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | while (crl_list != NULL) { |
| 2073 | if (crl_list->version == 0 || |
| 2074 | x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) { |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2075 | crl_list = crl_list->next; |
| 2076 | continue; |
| 2077 | } |
| 2078 | |
| 2079 | /* |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 2080 | * Check if the CA is configured to sign CRLs |
| 2081 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2082 | if (mbedtls_x509_crt_check_key_usage(ca, |
| 2083 | MBEDTLS_X509_KU_CRL_SIGN) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2084 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 2085 | break; |
| 2086 | } |
Manuel Pégourié-Gonnard | 99d4f19 | 2014-04-08 15:10:07 +0200 | [diff] [blame] | 2087 | |
| 2088 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2089 | * Check if CRL is correctly signed by the trusted CA |
| 2090 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) { |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 2092 | flags |= MBEDTLS_X509_BADCRL_BAD_MD; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2093 | } |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 2094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) { |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 2096 | flags |= MBEDTLS_X509_BADCRL_BAD_PK; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2097 | } |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 2098 | |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2099 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 2100 | psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | if (psa_hash_compute(psa_algorithm, |
| 2102 | crl_list->tbs.p, |
| 2103 | crl_list->tbs.len, |
| 2104 | hash, |
| 2105 | sizeof(hash), |
| 2106 | &hash_length) != PSA_SUCCESS) { |
pespacek | a7a6469 | 2022-02-14 15:18:43 +0100 | [diff] [blame] | 2107 | /* Note: this can't happen except after an internal error */ |
| 2108 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
| 2109 | break; |
| 2110 | } |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2111 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2112 | md_info = mbedtls_md_info_from_type(crl_list->sig_md); |
| 2113 | hash_length = mbedtls_md_get_size(md_info); |
| 2114 | if (mbedtls_md(md_info, |
| 2115 | crl_list->tbs.p, |
| 2116 | crl_list->tbs.len, |
| 2117 | hash) != 0) { |
Manuel Pégourié-Gonnard | 329e78c | 2017-06-26 12:22:17 +0200 | [diff] [blame] | 2118 | /* Note: this can't happen except after an internal error */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2119 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2120 | break; |
| 2121 | } |
pespacek | a7a6469 | 2022-02-14 15:18:43 +0100 | [diff] [blame] | 2122 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 2123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2124 | if (x509_profile_check_key(profile, &ca->pk) != 0) { |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 2125 | flags |= MBEDTLS_X509_BADCERT_BAD_KEY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | } |
Manuel Pégourié-Gonnard | 9505164 | 2015-06-15 10:39:46 +0200 | [diff] [blame] | 2127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2128 | if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk, |
| 2129 | crl_list->sig_md, hash, hash_length, |
| 2130 | crl_list->sig.p, crl_list->sig.len) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2131 | flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2132 | break; |
| 2133 | } |
| 2134 | |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2135 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2136 | /* |
| 2137 | * Check for validity of CRL (Do not drop out) |
| 2138 | */ |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2139 | if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2140 | flags |= MBEDTLS_X509_BADCRL_EXPIRED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2141 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2142 | |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2143 | if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 2144 | flags |= MBEDTLS_X509_BADCRL_FUTURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2145 | } |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2146 | #else |
| 2147 | ((void) now); |
| 2148 | #endif |
Manuel Pégourié-Gonnard | 9533765 | 2014-03-10 13:15:18 +0100 | [diff] [blame] | 2149 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2150 | /* |
| 2151 | * Check if certificate is revoked |
| 2152 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2153 | if (mbedtls_x509_crt_is_revoked(crt, crl_list)) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2154 | flags |= MBEDTLS_X509_BADCERT_REVOKED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2155 | break; |
| 2156 | } |
| 2157 | |
| 2158 | crl_list = crl_list->next; |
| 2159 | } |
Manuel Pégourié-Gonnard | cbb1f6e | 2015-06-15 16:17:55 +0200 | [diff] [blame] | 2160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2161 | return flags; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2162 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2163 | #endif /* MBEDTLS_X509_CRL_PARSE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2164 | |
Manuel Pégourié-Gonnard | 8842124 | 2014-10-17 11:36:18 +0200 | [diff] [blame] | 2165 | /* |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2166 | * Check the signature of a certificate by its parent |
| 2167 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2168 | static int x509_crt_check_signature(const mbedtls_x509_crt *child, |
| 2169 | mbedtls_x509_crt *parent, |
| 2170 | mbedtls_x509_crt_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2171 | { |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2172 | size_t hash_len; |
Manuel Pégourié-Gonnard | 8857984 | 2023-03-28 11:20:23 +0200 | [diff] [blame] | 2173 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2174 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2175 | const mbedtls_md_info_t *md_info; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2176 | md_info = mbedtls_md_info_from_type(child->sig_md); |
| 2177 | hash_len = mbedtls_md_get_size(md_info); |
Andrzej Kurek | 8b38ff5 | 2018-11-20 03:20:09 -0500 | [diff] [blame] | 2178 | |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2179 | /* Note: hash errors can happen only after an internal error */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) { |
| 2181 | return -1; |
| 2182 | } |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2183 | #else |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 2184 | psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md); |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 2185 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2187 | status = psa_hash_compute(hash_alg, |
| 2188 | child->tbs.p, |
| 2189 | child->tbs.len, |
| 2190 | hash, |
| 2191 | sizeof(hash), |
| 2192 | &hash_len); |
| 2193 | if (status != PSA_SUCCESS) { |
| 2194 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2195 | } |
| 2196 | |
Andrzej Kurek | d4a6553 | 2018-10-31 06:18:39 -0400 | [diff] [blame] | 2197 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2198 | /* Skip expensive computation on obvious mismatch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) { |
| 2200 | return -1; |
| 2201 | } |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2202 | |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2203 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2204 | if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) { |
| 2205 | return mbedtls_pk_verify_restartable(&parent->pk, |
| 2206 | child->sig_md, hash, hash_len, |
| 2207 | child->sig.p, child->sig.len, &rs_ctx->pk); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2208 | } |
| 2209 | #else |
| 2210 | (void) rs_ctx; |
| 2211 | #endif |
| 2212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2213 | return mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk, |
| 2214 | child->sig_md, hash, hash_len, |
| 2215 | child->sig.p, child->sig.len); |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | /* |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 2219 | * Check if 'parent' is a suitable parent (signing CA) for 'child'. |
| 2220 | * Return 0 if yes, -1 if not. |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 2221 | * |
| 2222 | * top means parent is a locally-trusted certificate |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 2223 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2224 | static int x509_crt_check_parent(const mbedtls_x509_crt *child, |
| 2225 | const mbedtls_x509_crt *parent, |
| 2226 | int top) |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 2227 | { |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 2228 | int need_ca_bit; |
| 2229 | |
Manuel Pégourié-Gonnard | c4eff16 | 2014-06-19 12:18:08 +0200 | [diff] [blame] | 2230 | /* Parent must be the issuer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2231 | if (x509_name_cmp(&child->issuer, &parent->subject) != 0) { |
| 2232 | return -1; |
| 2233 | } |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 2234 | |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 2235 | /* Parent must have the basicConstraints CA bit set as a general rule */ |
| 2236 | need_ca_bit = 1; |
| 2237 | |
| 2238 | /* Exception: v1/v2 certificates that are locally trusted. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | if (top && parent->version < 3) { |
Manuel Pégourié-Gonnard | d249b7a | 2014-06-24 11:49:16 +0200 | [diff] [blame] | 2240 | need_ca_bit = 0; |
Manuel Pégourié-Gonnard | c4eff16 | 2014-06-19 12:18:08 +0200 | [diff] [blame] | 2241 | } |
Manuel Pégourié-Gonnard | 312010e | 2014-04-09 14:30:11 +0200 | [diff] [blame] | 2242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2243 | if (need_ca_bit && !parent->ca_istrue) { |
| 2244 | return -1; |
| 2245 | } |
| 2246 | |
| 2247 | if (need_ca_bit && |
| 2248 | mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) { |
| 2249 | return -1; |
| 2250 | } |
| 2251 | |
| 2252 | return 0; |
Manuel Pégourié-Gonnard | 3fed0b3 | 2014-04-08 13:18:01 +0200 | [diff] [blame] | 2253 | } |
| 2254 | |
Manuel Pégourié-Gonnard | 35407c7 | 2017-06-29 10:45:25 +0200 | [diff] [blame] | 2255 | /* |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2256 | * Find a suitable parent for child in candidates, or return NULL. |
| 2257 | * |
| 2258 | * Here suitable is defined as: |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2259 | * 1. subject name matches child's issuer |
| 2260 | * 2. if necessary, the CA bit is set and key usage allows signing certs |
| 2261 | * 3. for trusted roots, the signature is correct |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2262 | * (for intermediates, the signature is checked and the result reported) |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2263 | * 4. pathlen constraints are satisfied |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2264 | * |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 2265 | * If there's a suitable candidate which is also time-valid, return the first |
| 2266 | * such. Otherwise, return the first suitable candidate (or NULL if there is |
| 2267 | * none). |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2268 | * |
| 2269 | * The rationale for this rule is that someone could have a list of trusted |
| 2270 | * roots with two versions on the same root with different validity periods. |
| 2271 | * (At least one user reported having such a list and wanted it to just work.) |
| 2272 | * The reason we don't just require time-validity is that generally there is |
| 2273 | * only one version, and if it's expired we want the flags to state that |
| 2274 | * rather than NOT_TRUSTED, as would be the case if we required it here. |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2275 | * |
| 2276 | * The rationale for rule 3 (signature for trusted roots) is that users might |
| 2277 | * have two versions of the same CA with different keys in their list, and the |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2278 | * way we select the correct one is by checking the signature (as we don't |
| 2279 | * rely on key identifier extensions). (This is one way users might choose to |
| 2280 | * handle key rollover, another relies on self-issued certs, see [SIRO].) |
Manuel Pégourié-Gonnard | 98a6778 | 2017-08-17 10:52:20 +0200 | [diff] [blame] | 2281 | * |
| 2282 | * Arguments: |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2283 | * - [in] child: certificate for which we're looking for a parent |
| 2284 | * - [in] candidates: chained list of potential parents |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2285 | * - [out] r_parent: parent found (or NULL) |
| 2286 | * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0 |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2287 | * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top |
| 2288 | * of the chain, 0 otherwise |
| 2289 | * - [in] path_cnt: number of intermediates seen so far |
| 2290 | * - [in] self_cnt: number of self-signed intermediates seen so far |
| 2291 | * (will never be greater than path_cnt) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2292 | * - [in-out] rs_ctx: context for restarting operations |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2293 | * |
| 2294 | * Return value: |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2295 | * - 0 on success |
| 2296 | * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2297 | */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2298 | static int x509_crt_find_parent_in( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2299 | mbedtls_x509_crt *child, |
| 2300 | mbedtls_x509_crt *candidates, |
| 2301 | mbedtls_x509_crt **r_parent, |
| 2302 | int *r_signature_is_good, |
| 2303 | int top, |
| 2304 | unsigned path_cnt, |
| 2305 | unsigned self_cnt, |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2306 | mbedtls_x509_crt_restart_ctx *rs_ctx, |
| 2307 | const mbedtls_x509_time *now) |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2308 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2309 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2310 | mbedtls_x509_crt *parent, *fallback_parent; |
Benjamin Kier | 3605073 | 2019-05-30 14:49:17 -0400 | [diff] [blame] | 2311 | int signature_is_good = 0, fallback_signature_is_good; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2312 | |
| 2313 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2314 | /* did we have something in progress? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2315 | if (rs_ctx != NULL && rs_ctx->parent != NULL) { |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2316 | /* restore saved state */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2317 | parent = rs_ctx->parent; |
| 2318 | fallback_parent = rs_ctx->fallback_parent; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2319 | fallback_signature_is_good = rs_ctx->fallback_signature_is_good; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2320 | |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2321 | /* clear saved state */ |
| 2322 | rs_ctx->parent = NULL; |
| 2323 | rs_ctx->fallback_parent = NULL; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2324 | rs_ctx->fallback_signature_is_good = 0; |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2325 | |
| 2326 | /* resume where we left */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2327 | goto check_signature; |
| 2328 | } |
| 2329 | #endif |
| 2330 | |
| 2331 | fallback_parent = NULL; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2332 | fallback_signature_is_good = 0; |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | for (parent = candidates; parent != NULL; parent = parent->next) { |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2335 | /* basic parenting skills (name, CA bit, key usage) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2336 | if (x509_crt_check_parent(child, parent, top) != 0) { |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2337 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2338 | } |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2339 | |
Manuel Pégourié-Gonnard | 9c6118c | 2017-06-29 12:38:42 +0200 | [diff] [blame] | 2340 | /* +1 because stored max_pathlen is 1 higher that the actual value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | if (parent->max_pathlen > 0 && |
| 2342 | (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) { |
Manuel Pégourié-Gonnard | 9c6118c | 2017-06-29 12:38:42 +0200 | [diff] [blame] | 2343 | continue; |
| 2344 | } |
| 2345 | |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2346 | /* Signature */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2347 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 2348 | check_signature: |
| 2349 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2350 | ret = x509_crt_check_signature(child, parent, rs_ctx); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2351 | |
| 2352 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2353 | if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2354 | /* save state */ |
| 2355 | rs_ctx->parent = parent; |
| 2356 | rs_ctx->fallback_parent = fallback_parent; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2357 | rs_ctx->fallback_signature_is_good = fallback_signature_is_good; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2359 | return ret; |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2360 | } |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2361 | #else |
| 2362 | (void) ret; |
| 2363 | #endif |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2364 | |
| 2365 | signature_is_good = ret == 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | if (top && !signature_is_good) { |
Manuel Pégourié-Gonnard | f82a4d5 | 2017-07-03 19:26:25 +0200 | [diff] [blame] | 2367 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2368 | } |
Manuel Pégourié-Gonnard | 2f09d59 | 2017-07-03 18:30:43 +0200 | [diff] [blame] | 2369 | |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2370 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 2371 | /* optional time check */ |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2372 | if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */ |
| 2373 | mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | if (fallback_parent == NULL) { |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2375 | fallback_parent = parent; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2376 | fallback_signature_is_good = signature_is_good; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2377 | } |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2378 | |
| 2379 | continue; |
| 2380 | } |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2381 | #else |
| 2382 | ((void) now); |
| 2383 | #endif |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2384 | |
Andy Gross | 1f62714 | 2019-01-30 10:25:53 -0600 | [diff] [blame] | 2385 | *r_parent = parent; |
| 2386 | *r_signature_is_good = signature_is_good; |
| 2387 | |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2388 | break; |
| 2389 | } |
| 2390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2391 | if (parent == NULL) { |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2392 | *r_parent = fallback_parent; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 2393 | *r_signature_is_good = fallback_signature_is_good; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2394 | } |
Manuel Pégourié-Gonnard | 3e329b8 | 2017-06-29 12:55:27 +0200 | [diff] [blame] | 2395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2396 | return 0; |
Manuel Pégourié-Gonnard | 2f1c33d | 2017-06-29 12:27:23 +0200 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | /* |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2400 | * Find a parent in trusted CAs or the provided chain, or return NULL. |
| 2401 | * |
| 2402 | * Searches in trusted CAs first, and return the first suitable parent found |
| 2403 | * (see find_parent_in() for definition of suitable). |
Manuel Pégourié-Gonnard | 98a6778 | 2017-08-17 10:52:20 +0200 | [diff] [blame] | 2404 | * |
| 2405 | * Arguments: |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2406 | * - [in] child: certificate for which we're looking for a parent, followed |
| 2407 | * by a chain of possible intermediates |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2408 | * - [in] trust_ca: list of locally trusted certificates |
| 2409 | * - [out] parent: parent found (or NULL) |
| 2410 | * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0 |
| 2411 | * - [out] signature_is_good: 1 if child signature by parent is valid, or 0 |
| 2412 | * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child) |
| 2413 | * - [in] self_cnt: number of self-signed certs in the chain so far |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2414 | * (will always be no greater than path_cnt) |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2415 | * - [in-out] rs_ctx: context for restarting operations |
Manuel Pégourié-Gonnard | e57d743 | 2018-03-07 10:00:57 +0100 | [diff] [blame] | 2416 | * |
| 2417 | * Return value: |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 2418 | * - 0 on success |
| 2419 | * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2420 | */ |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2421 | static int x509_crt_find_parent( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2422 | mbedtls_x509_crt *child, |
| 2423 | mbedtls_x509_crt *trust_ca, |
| 2424 | mbedtls_x509_crt **parent, |
| 2425 | int *parent_is_trusted, |
| 2426 | int *signature_is_good, |
| 2427 | unsigned path_cnt, |
| 2428 | unsigned self_cnt, |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2429 | mbedtls_x509_crt_restart_ctx *rs_ctx, |
| 2430 | const mbedtls_x509_time *now) |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2431 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2432 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2433 | mbedtls_x509_crt *search_list; |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2434 | |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2435 | *parent_is_trusted = 1; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2436 | |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2437 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2438 | /* restore then clear saved state if we have some stored */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2439 | if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) { |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2440 | *parent_is_trusted = rs_ctx->parent_is_trusted; |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2441 | rs_ctx->parent_is_trusted = -1; |
| 2442 | } |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2443 | #endif |
| 2444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2445 | while (1) { |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2446 | search_list = *parent_is_trusted ? trust_ca : child->next; |
| 2447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2448 | ret = x509_crt_find_parent_in(child, search_list, |
| 2449 | parent, signature_is_good, |
| 2450 | *parent_is_trusted, |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2451 | path_cnt, self_cnt, rs_ctx, now); |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2452 | |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2453 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2454 | if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2455 | /* save state */ |
| 2456 | rs_ctx->parent_is_trusted = *parent_is_trusted; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2457 | return ret; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2458 | } |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2459 | #else |
| 2460 | (void) ret; |
| 2461 | #endif |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2462 | |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2463 | /* stop here if found or already in second iteration */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2464 | if (*parent != NULL || *parent_is_trusted == 0) { |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2465 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2466 | } |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2467 | |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2468 | /* prepare second iteration */ |
| 2469 | *parent_is_trusted = 0; |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2470 | } |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2471 | |
| 2472 | /* extra precaution against mistakes in the caller */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2473 | if (*parent == NULL) { |
Manuel Pégourié-Gonnard | a5a3e40 | 2018-10-16 11:27:23 +0200 | [diff] [blame] | 2474 | *parent_is_trusted = 0; |
| 2475 | *signature_is_good = 0; |
Manuel Pégourié-Gonnard | 18547b5 | 2017-08-14 16:11:43 +0200 | [diff] [blame] | 2476 | } |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2478 | return 0; |
Manuel Pégourié-Gonnard | 6368612 | 2017-07-04 01:01:39 +0200 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | /* |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2482 | * Check if an end-entity certificate is locally trusted |
| 2483 | * |
| 2484 | * Currently we require such certificates to be self-signed (actually only |
| 2485 | * check for self-issued as self-signatures are not checked) |
| 2486 | */ |
| 2487 | static int x509_crt_check_ee_locally_trusted( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2488 | mbedtls_x509_crt *crt, |
| 2489 | mbedtls_x509_crt *trust_ca) |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2490 | { |
| 2491 | mbedtls_x509_crt *cur; |
| 2492 | |
| 2493 | /* must be self-issued */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2494 | if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) { |
| 2495 | return -1; |
| 2496 | } |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2497 | |
| 2498 | /* look for an exact match with trusted cert */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2499 | for (cur = trust_ca; cur != NULL; cur = cur->next) { |
| 2500 | if (crt->raw.len == cur->raw.len && |
| 2501 | memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) { |
| 2502 | return 0; |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | /* too bad */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2507 | return -1; |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | /* |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2511 | * Build and verify a certificate chain |
Manuel Pégourié-Gonnard | 35407c7 | 2017-06-29 10:45:25 +0200 | [diff] [blame] | 2512 | * |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2513 | * Given a peer-provided list of certificates EE, C1, ..., Cn and |
| 2514 | * a list of trusted certs R1, ... Rp, try to build and verify a chain |
Manuel Pégourié-Gonnard | 562df40 | 2017-08-08 18:09:14 +0200 | [diff] [blame] | 2515 | * EE, Ci1, ... Ciq [, Rj] |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2516 | * such that every cert in the chain is a child of the next one, |
| 2517 | * jumping to a trusted root as early as possible. |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2518 | * |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2519 | * Verify that chain and return it with flags for all issues found. |
| 2520 | * |
| 2521 | * Special cases: |
| 2522 | * - EE == Rj -> return a one-element list containing it |
| 2523 | * - EE, Ci1, ..., Ciq cannot be continued with a trusted root |
| 2524 | * -> return that chain with NOT_TRUSTED set on Ciq |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2525 | * |
Manuel Pégourié-Gonnard | d19a41d | 2017-07-14 11:05:59 +0200 | [diff] [blame] | 2526 | * Tests for (aspects of) this function should include at least: |
| 2527 | * - trusted EE |
| 2528 | * - EE -> trusted root |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 2529 | * - EE -> intermediate CA -> trusted root |
Manuel Pégourié-Gonnard | d19a41d | 2017-07-14 11:05:59 +0200 | [diff] [blame] | 2530 | * - if relevant: EE untrusted |
| 2531 | * - if relevant: EE -> intermediate, untrusted |
| 2532 | * with the aspect under test checked at each relevant level (EE, int, root). |
| 2533 | * For some aspects longer chains are required, but usually length 2 is |
| 2534 | * enough (but length 1 is not in general). |
| 2535 | * |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2536 | * Arguments: |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2537 | * - [in] crt: the cert list EE, C1, ..., Cn |
| 2538 | * - [in] trust_ca: the trusted list R1, ..., Rp |
| 2539 | * - [in] ca_crl, profile: as in verify_with_profile() |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 2540 | * - [out] ver_chain: the built and verified chain |
Manuel Pégourié-Gonnard | a968843 | 2017-08-23 11:23:59 +0200 | [diff] [blame] | 2541 | * Only valid when return value is 0, may contain garbage otherwise! |
| 2542 | * Restart note: need not be the same when calling again to resume. |
Manuel Pégourié-Gonnard | 98a6778 | 2017-08-17 10:52:20 +0200 | [diff] [blame] | 2543 | * - [in-out] rs_ctx: context for restarting operations |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2544 | * |
| 2545 | * Return value: |
| 2546 | * - non-zero if the chain could not be fully built and examined |
| 2547 | * - 0 is the chain was successfully built and examined, |
| 2548 | * even if it was found to be invalid |
Manuel Pégourié-Gonnard | 35407c7 | 2017-06-29 10:45:25 +0200 | [diff] [blame] | 2549 | */ |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 2550 | static int x509_crt_verify_chain( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2551 | mbedtls_x509_crt *crt, |
| 2552 | mbedtls_x509_crt *trust_ca, |
| 2553 | mbedtls_x509_crl *ca_crl, |
| 2554 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 2555 | void *p_ca_cb, |
| 2556 | const mbedtls_x509_crt_profile *profile, |
| 2557 | mbedtls_x509_crt_verify_chain *ver_chain, |
| 2558 | mbedtls_x509_crt_restart_ctx *rs_ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2559 | { |
Manuel Pégourié-Gonnard | a968843 | 2017-08-23 11:23:59 +0200 | [diff] [blame] | 2560 | /* Don't initialize any of those variables here, so that the compiler can |
| 2561 | * catch potential issues with jumping ahead when restarting */ |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2562 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2563 | uint32_t *flags; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 2564 | mbedtls_x509_crt_verify_chain_item *cur; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2565 | mbedtls_x509_crt *child; |
Manuel Pégourié-Gonnard | 58dcd2d | 2017-07-03 21:35:04 +0200 | [diff] [blame] | 2566 | mbedtls_x509_crt *parent; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2567 | int parent_is_trusted; |
| 2568 | int child_is_trusted; |
| 2569 | int signature_is_good; |
Manuel Pégourié-Gonnard | bb216bd | 2017-08-28 13:25:55 +0200 | [diff] [blame] | 2570 | unsigned self_cnt; |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 2571 | mbedtls_x509_crt *cur_trust_ca = NULL; |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2572 | mbedtls_x509_time now; |
| 2573 | |
| 2574 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
| 2575 | if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) { |
| 2576 | return MBEDTLS_ERR_X509_FATAL_ERROR; |
| 2577 | } |
| 2578 | #endif |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2579 | |
| 2580 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 2581 | /* resume if we had an operation in progress */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2582 | if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) { |
Manuel Pégourié-Gonnard | 3627a8b | 2017-08-23 11:20:48 +0200 | [diff] [blame] | 2583 | /* restore saved state */ |
Manuel Pégourié-Gonnard | a968843 | 2017-08-23 11:23:59 +0200 | [diff] [blame] | 2584 | *ver_chain = rs_ctx->ver_chain; /* struct copy */ |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 2585 | self_cnt = rs_ctx->self_cnt; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2586 | |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 2587 | /* restore derived state */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2588 | cur = &ver_chain->items[ver_chain->len - 1]; |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 2589 | child = cur->crt; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2590 | flags = &cur->flags; |
| 2591 | |
| 2592 | goto find_parent; |
| 2593 | } |
| 2594 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 8f8c282 | 2017-07-03 21:25:10 +0200 | [diff] [blame] | 2595 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2596 | child = crt; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2597 | self_cnt = 0; |
| 2598 | parent_is_trusted = 0; |
| 2599 | child_is_trusted = 0; |
Manuel Pégourié-Gonnard | f86f491 | 2017-07-05 16:43:44 +0200 | [diff] [blame] | 2600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2601 | while (1) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2602 | /* Add certificate to the verification chain */ |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 2603 | cur = &ver_chain->items[ver_chain->len]; |
| 2604 | cur->crt = child; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 2605 | cur->flags = 0; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 2606 | ver_chain->len++; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 2607 | flags = &cur->flags; |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 2608 | |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2609 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2610 | /* Check time-validity (all certificates) */ |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2611 | if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2612 | *flags |= MBEDTLS_X509_BADCERT_EXPIRED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2613 | } |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 2614 | |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2615 | if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2616 | *flags |= MBEDTLS_X509_BADCERT_FUTURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2617 | } |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2618 | #endif |
Manuel Pégourié-Gonnard | cb39610 | 2017-07-04 00:00:24 +0200 | [diff] [blame] | 2619 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2620 | /* Stop here for trusted roots (but not for trusted EE certs) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2621 | if (child_is_trusted) { |
| 2622 | return 0; |
| 2623 | } |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 2624 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2625 | /* Check signature algorithm: MD & PK algs */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2626 | if (x509_profile_check_md_alg(profile, child->sig_md) != 0) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2627 | *flags |= MBEDTLS_X509_BADCERT_BAD_MD; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2628 | } |
Manuel Pégourié-Gonnard | 66fac75 | 2017-07-03 21:39:21 +0200 | [diff] [blame] | 2629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2630 | if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2631 | *flags |= MBEDTLS_X509_BADCERT_BAD_PK; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2632 | } |
Manuel Pégourié-Gonnard | 27e9479 | 2017-07-04 00:49:31 +0200 | [diff] [blame] | 2633 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2634 | /* Special case: EE certs that are locally trusted */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | if (ver_chain->len == 1 && |
| 2636 | x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) { |
| 2637 | return 0; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2638 | } |
Manuel Pégourié-Gonnard | 8f8c282 | 2017-07-03 21:25:10 +0200 | [diff] [blame] | 2639 | |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2640 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 2641 | find_parent: |
| 2642 | #endif |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 2643 | |
| 2644 | /* Obtain list of potential trusted signers from CA callback, |
| 2645 | * or use statically provided list. */ |
| 2646 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2647 | if (f_ca_cb != NULL) { |
| 2648 | mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result); |
| 2649 | mbedtls_free(ver_chain->trust_ca_cb_result); |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 2650 | ver_chain->trust_ca_cb_result = NULL; |
| 2651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2652 | ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result); |
| 2653 | if (ret != 0) { |
| 2654 | return MBEDTLS_ERR_X509_FATAL_ERROR; |
| 2655 | } |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 2656 | |
| 2657 | cur_trust_ca = ver_chain->trust_ca_cb_result; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2658 | } else |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 2659 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 2660 | { |
| 2661 | ((void) f_ca_cb); |
| 2662 | ((void) p_ca_cb); |
| 2663 | cur_trust_ca = trust_ca; |
| 2664 | } |
| 2665 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2666 | /* Look for a parent in trusted CAs or up the chain */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2667 | ret = x509_crt_find_parent(child, cur_trust_ca, &parent, |
| 2668 | &parent_is_trusted, &signature_is_good, |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2669 | ver_chain->len - 1, self_cnt, rs_ctx, |
| 2670 | &now); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2671 | |
| 2672 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2673 | if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2674 | /* save state */ |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 2675 | rs_ctx->in_progress = x509_crt_rs_find_parent; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2676 | rs_ctx->self_cnt = self_cnt; |
Manuel Pégourié-Gonnard | a968843 | 2017-08-23 11:23:59 +0200 | [diff] [blame] | 2677 | rs_ctx->ver_chain = *ver_chain; /* struct copy */ |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2679 | return ret; |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 2680 | } |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 2681 | #else |
| 2682 | (void) ret; |
| 2683 | #endif |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2684 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2685 | /* No parent? We're done here */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2686 | if (parent == NULL) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2687 | *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | return 0; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2689 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2690 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2691 | /* Count intermediate self-issued (not necessarily self-signed) certs. |
| 2692 | * These can occur with some strategies for key rollover, see [SIRO], |
| 2693 | * and should be excluded from max_pathlen checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2694 | if (ver_chain->len != 1 && |
| 2695 | x509_name_cmp(&child->issuer, &child->subject) == 0) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2696 | self_cnt++; |
Manuel Pégourié-Gonnard | 24611f9 | 2017-08-09 10:28:07 +0200 | [diff] [blame] | 2697 | } |
Manuel Pégourié-Gonnard | fd6c85c | 2014-11-20 16:34:20 +0100 | [diff] [blame] | 2698 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2699 | /* path_cnt is 0 for the first intermediate CA, |
| 2700 | * and if parent is trusted it's not an intermediate CA */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2701 | if (!parent_is_trusted && |
| 2702 | ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2703 | /* return immediately to avoid overflow the chain array */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2704 | return MBEDTLS_ERR_X509_FATAL_ERROR; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2705 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2706 | |
Manuel Pégourié-Gonnard | 98a6778 | 2017-08-17 10:52:20 +0200 | [diff] [blame] | 2707 | /* signature was checked while searching parent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2708 | if (!signature_is_good) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2709 | *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2710 | } |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2711 | |
| 2712 | /* check size of signing key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2713 | if (x509_profile_check_key(profile, &parent->pk) != 0) { |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2714 | *flags |= MBEDTLS_X509_BADCERT_BAD_KEY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2715 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2717 | #if defined(MBEDTLS_X509_CRL_PARSE_C) |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2718 | /* Check trusted CA's CRL for the given crt */ |
Glenn Strauss | 4b2a6e8 | 2022-06-30 12:17:58 -0400 | [diff] [blame] | 2719 | *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now); |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2720 | #else |
| 2721 | (void) ca_crl; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2722 | #endif |
| 2723 | |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2724 | /* prepare for next iteration */ |
| 2725 | child = parent; |
| 2726 | parent = NULL; |
| 2727 | child_is_trusted = parent_is_trusted; |
Manuel Pégourié-Gonnard | be4ff42 | 2017-07-14 12:04:14 +0200 | [diff] [blame] | 2728 | signature_is_good = 0; |
Manuel Pégourié-Gonnard | ce6e52f | 2017-07-05 17:05:03 +0200 | [diff] [blame] | 2729 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2730 | } |
| 2731 | |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2732 | #ifdef _WIN32 |
| 2733 | #ifdef _MSC_VER |
| 2734 | #pragma comment(lib, "ws2_32.lib") |
| 2735 | #include <winsock2.h> |
| 2736 | #include <ws2tcpip.h> |
| 2737 | #elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600 |
| 2738 | #include <winsock2.h> |
| 2739 | #include <ws2tcpip.h> |
Steve Lhomme | eb0f18a | 2023-06-16 14:12:19 +0200 | [diff] [blame] | 2740 | #else |
| 2741 | /* inet_pton() is not supported, fallback to software version */ |
| 2742 | #define MBEDTLS_TEST_SW_INET_PTON |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2743 | #endif |
| 2744 | #elif defined(__sun) |
| 2745 | /* Solaris requires -lsocket -lnsl for inet_pton() */ |
| 2746 | #elif defined(__has_include) |
| 2747 | #if __has_include(<sys/socket.h>) |
| 2748 | #include <sys/socket.h> |
| 2749 | #endif |
| 2750 | #if __has_include(<arpa/inet.h>) |
| 2751 | #include <arpa/inet.h> |
| 2752 | #endif |
| 2753 | #endif |
| 2754 | |
| 2755 | /* Use whether or not AF_INET6 is defined to indicate whether or not to use |
| 2756 | * the platform inet_pton() or a local implementation (below). The local |
| 2757 | * implementation may be used even in cases where the platform provides |
| 2758 | * inet_pton(), e.g. when there are different includes required and/or the |
| 2759 | * platform implementation requires dependencies on additional libraries. |
| 2760 | * Specifically, Windows requires custom includes and additional link |
| 2761 | * dependencies, and Solaris requires additional link dependencies. |
| 2762 | * Also, as a coarse heuristic, use the local implementation if the compiler |
| 2763 | * does not support __has_include(), or if the definition of AF_INET6 is not |
Andrzej Kurek | 06969fc | 2023-04-12 10:34:50 -0400 | [diff] [blame] | 2764 | * provided by headers included (or not) via __has_include() above. |
| 2765 | * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names |
| 2766 | * despite having a platform that has inet_pton. */ |
| 2767 | #if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names |
| 2768 | /* Definition located further below to possibly reduce compiler inlining */ |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2769 | static int x509_inet_pton_ipv4(const char *src, void *dst); |
| 2770 | |
| 2771 | #define li_cton(c, n) \ |
| 2772 | (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0)) |
| 2773 | |
| 2774 | static int x509_inet_pton_ipv6(const char *src, void *dst) |
| 2775 | { |
Andrzej Kurek | 6cbca6d | 2023-04-13 09:22:48 -0400 | [diff] [blame] | 2776 | const unsigned char *p = (const unsigned char *) src; |
Andrzej Kurek | 0d57896 | 2023-04-13 09:09:56 -0400 | [diff] [blame] | 2777 | int nonzero_groups = 0, num_digits, zero_group_start = -1; |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2778 | uint16_t addr[8]; |
| 2779 | do { |
| 2780 | /* note: allows excess leading 0's, e.g. 1:0002:3:... */ |
Andrzej Kurek | 7f5a1a4 | 2023-04-13 08:02:27 -0400 | [diff] [blame] | 2781 | uint16_t group = num_digits = 0; |
Andrzej Kurek | 8bc2cc9 | 2023-04-18 07:26:27 -0400 | [diff] [blame] | 2782 | for (uint8_t digit; num_digits < 4; num_digits++) { |
| 2783 | if (li_cton(*p, digit) == 0) { |
| 2784 | break; |
| 2785 | } |
| 2786 | group = (group << 4) | digit; |
| 2787 | p++; |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2788 | } |
Andrzej Kurek | 7f5a1a4 | 2023-04-13 08:02:27 -0400 | [diff] [blame] | 2789 | if (num_digits != 0) { |
Dave Rodgman | cfa7223 | 2023-09-05 16:20:33 +0100 | [diff] [blame] | 2790 | MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups); |
| 2791 | nonzero_groups++; |
Andrzej Kurek | 6cbca6d | 2023-04-13 09:22:48 -0400 | [diff] [blame] | 2792 | if (*p == '\0') { |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2793 | break; |
Andrzej Kurek | 8bc2cc9 | 2023-04-18 07:26:27 -0400 | [diff] [blame] | 2794 | } else if (*p == '.') { |
| 2795 | /* Don't accept IPv4 too early or late */ |
| 2796 | if ((nonzero_groups == 0 && zero_group_start == -1) || |
| 2797 | nonzero_groups >= 7) { |
| 2798 | break; |
| 2799 | } |
| 2800 | |
| 2801 | /* Walk back to prior ':', then parse as IPv4-mapped */ |
| 2802 | int steps = 4; |
| 2803 | do { |
| 2804 | p--; |
| 2805 | steps--; |
| 2806 | } while (*p != ':' && steps > 0); |
| 2807 | |
| 2808 | if (*p != ':') { |
| 2809 | break; |
| 2810 | } |
| 2811 | p++; |
| 2812 | nonzero_groups--; |
| 2813 | if (x509_inet_pton_ipv4((const char *) p, |
| 2814 | addr + nonzero_groups) != 0) { |
| 2815 | break; |
| 2816 | } |
| 2817 | |
Andrzej Kurek | 0d57896 | 2023-04-13 09:09:56 -0400 | [diff] [blame] | 2818 | nonzero_groups += 2; |
Andrzej Kurek | 6cbca6d | 2023-04-13 09:22:48 -0400 | [diff] [blame] | 2819 | p = (const unsigned char *) ""; |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2820 | break; |
Andrzej Kurek | 6cbca6d | 2023-04-13 09:22:48 -0400 | [diff] [blame] | 2821 | } else if (*p != ':') { |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2822 | return -1; |
| 2823 | } |
| 2824 | } else { |
Andrzej Kurek | 8bc2cc9 | 2023-04-18 07:26:27 -0400 | [diff] [blame] | 2825 | /* Don't accept a second zero group or an invalid delimiter */ |
| 2826 | if (zero_group_start != -1 || *p != ':') { |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2827 | return -1; |
| 2828 | } |
Andrzej Kurek | 8bc2cc9 | 2023-04-18 07:26:27 -0400 | [diff] [blame] | 2829 | zero_group_start = nonzero_groups; |
| 2830 | |
| 2831 | /* Accept a zero group at start, but it has to be a double colon */ |
| 2832 | if (zero_group_start == 0 && *++p != ':') { |
| 2833 | return -1; |
| 2834 | } |
| 2835 | |
Andrzej Kurek | 6cbca6d | 2023-04-13 09:22:48 -0400 | [diff] [blame] | 2836 | if (p[1] == '\0') { |
| 2837 | ++p; |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2838 | break; |
| 2839 | } |
| 2840 | } |
Andrzej Kurek | 6cbca6d | 2023-04-13 09:22:48 -0400 | [diff] [blame] | 2841 | ++p; |
Andrzej Kurek | 0d57896 | 2023-04-13 09:09:56 -0400 | [diff] [blame] | 2842 | } while (nonzero_groups < 8); |
Andrzej Kurek | 90117db | 2023-04-18 07:32:47 -0400 | [diff] [blame] | 2843 | |
| 2844 | if (*p != '\0') { |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2845 | return -1; |
| 2846 | } |
| 2847 | |
Andrzej Kurek | 7f5a1a4 | 2023-04-13 08:02:27 -0400 | [diff] [blame] | 2848 | if (zero_group_start != -1) { |
Andrzej Kurek | 90117db | 2023-04-18 07:32:47 -0400 | [diff] [blame] | 2849 | if (nonzero_groups > 6) { |
| 2850 | return -1; |
| 2851 | } |
Andrzej Kurek | 0d57896 | 2023-04-13 09:09:56 -0400 | [diff] [blame] | 2852 | int zero_groups = 8 - nonzero_groups; |
| 2853 | int groups_after_zero = nonzero_groups - zero_group_start; |
| 2854 | |
| 2855 | /* Move the non-zero part to after the zeroes */ |
| 2856 | if (groups_after_zero) { |
| 2857 | memmove(addr + zero_group_start + zero_groups, |
Andrzej Kurek | 7f5a1a4 | 2023-04-13 08:02:27 -0400 | [diff] [blame] | 2858 | addr + zero_group_start, |
Andrzej Kurek | 0d57896 | 2023-04-13 09:09:56 -0400 | [diff] [blame] | 2859 | groups_after_zero * sizeof(*addr)); |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2860 | } |
Andrzej Kurek | 0d57896 | 2023-04-13 09:09:56 -0400 | [diff] [blame] | 2861 | memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr)); |
Andrzej Kurek | 90117db | 2023-04-18 07:32:47 -0400 | [diff] [blame] | 2862 | } else { |
| 2863 | if (nonzero_groups != 8) { |
| 2864 | return -1; |
| 2865 | } |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2866 | } |
| 2867 | memcpy(dst, addr, sizeof(addr)); |
| 2868 | return 0; |
| 2869 | } |
| 2870 | |
| 2871 | static int x509_inet_pton_ipv4(const char *src, void *dst) |
| 2872 | { |
Andrzej Kurek | 6cbca6d | 2023-04-13 09:22:48 -0400 | [diff] [blame] | 2873 | const unsigned char *p = (const unsigned char *) src; |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2874 | uint8_t *res = (uint8_t *) dst; |
Andrzej Kurek | ea3e71f | 2023-04-18 04:39:12 -0400 | [diff] [blame] | 2875 | uint8_t digit, num_digits = 0; |
| 2876 | uint8_t num_octets = 0; |
Andrzej Kurek | 13b8b78 | 2023-04-12 09:43:47 -0400 | [diff] [blame] | 2877 | uint16_t octet; |
| 2878 | |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2879 | do { |
Andrzej Kurek | ea3e71f | 2023-04-18 04:39:12 -0400 | [diff] [blame] | 2880 | octet = num_digits = 0; |
| 2881 | do { |
| 2882 | digit = *p - '0'; |
| 2883 | if (digit > 9) { |
| 2884 | break; |
| 2885 | } |
Andrzej Kurek | 6f400a3 | 2023-05-01 05:26:47 -0400 | [diff] [blame] | 2886 | |
| 2887 | /* Don't allow leading zeroes. These might mean octal format, |
| 2888 | * which this implementation does not support. */ |
| 2889 | if (octet == 0 && num_digits > 0) { |
Andrzej Kurek | 9c9880a | 2023-05-03 05:06:47 -0400 | [diff] [blame] | 2890 | return -1; |
Andrzej Kurek | 6f400a3 | 2023-05-01 05:26:47 -0400 | [diff] [blame] | 2891 | } |
| 2892 | |
Andrzej Kurek | ea3e71f | 2023-04-18 04:39:12 -0400 | [diff] [blame] | 2893 | octet = octet * 10 + digit; |
| 2894 | num_digits++; |
| 2895 | p++; |
| 2896 | } while (num_digits < 3); |
| 2897 | |
| 2898 | if (octet >= 256 || num_digits > 3 || num_digits == 0) { |
Andrzej Kurek | 9c9880a | 2023-05-03 05:06:47 -0400 | [diff] [blame] | 2899 | return -1; |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2900 | } |
Andrzej Kurek | ea3e71f | 2023-04-18 04:39:12 -0400 | [diff] [blame] | 2901 | *res++ = (uint8_t) octet; |
| 2902 | num_octets++; |
| 2903 | } while (num_octets < 4 && *p++ == '.'); |
Andrzej Kurek | 6cbca6d | 2023-04-13 09:22:48 -0400 | [diff] [blame] | 2904 | return num_octets == 4 && *p == '\0' ? 0 : -1; |
Glenn Strauss | 416c295 | 2022-10-24 23:00:02 -0400 | [diff] [blame] | 2905 | } |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2906 | |
| 2907 | #else |
| 2908 | |
| 2909 | static int x509_inet_pton_ipv6(const char *src, void *dst) |
| 2910 | { |
| 2911 | return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1; |
| 2912 | } |
| 2913 | |
| 2914 | static int x509_inet_pton_ipv4(const char *src, void *dst) |
| 2915 | { |
| 2916 | return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1; |
| 2917 | } |
| 2918 | |
Andrzej Kurek | 06969fc | 2023-04-12 10:34:50 -0400 | [diff] [blame] | 2919 | #endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2920 | |
Glenn Strauss | 6f545ac | 2022-10-25 15:02:14 -0400 | [diff] [blame] | 2921 | size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst) |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2922 | { |
| 2923 | return strchr(cn, ':') == NULL |
| 2924 | ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0 |
| 2925 | : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0; |
| 2926 | } |
| 2927 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 2928 | /* |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2929 | * Check for CN match |
| 2930 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2931 | static int x509_crt_check_cn(const mbedtls_x509_buf *name, |
| 2932 | const char *cn, size_t cn_len) |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2933 | { |
| 2934 | /* try exact match */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2935 | if (name->len == cn_len && |
| 2936 | x509_memcasecmp(cn, name->p, cn_len) == 0) { |
| 2937 | return 0; |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | /* try wildcard match */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2941 | if (x509_check_wildcard(cn, name) == 0) { |
| 2942 | return 0; |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2945 | return -1; |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2946 | } |
| 2947 | |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2948 | static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san, |
| 2949 | const char *cn, size_t cn_len) |
| 2950 | { |
| 2951 | uint32_t ip[4]; |
Glenn Strauss | 6f545ac | 2022-10-25 15:02:14 -0400 | [diff] [blame] | 2952 | cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip); |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2953 | if (cn_len == 0) { |
| 2954 | return -1; |
| 2955 | } |
| 2956 | |
| 2957 | for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) { |
| 2958 | const unsigned char san_type = (unsigned char) cur->buf.tag & |
| 2959 | MBEDTLS_ASN1_TAG_VALUE_MASK; |
| 2960 | if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS && |
| 2961 | cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) { |
| 2962 | return 0; |
| 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | return -1; |
| 2967 | } |
| 2968 | |
Andrzej Kurek | 199eab9 | 2023-05-10 09:57:19 -0400 | [diff] [blame] | 2969 | static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san, |
| 2970 | const char *cn, size_t cn_len) |
| 2971 | { |
| 2972 | for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) { |
| 2973 | const unsigned char san_type = (unsigned char) cur->buf.tag & |
| 2974 | MBEDTLS_ASN1_TAG_VALUE_MASK; |
| 2975 | if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER && |
| 2976 | cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) { |
| 2977 | return 0; |
| 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | return -1; |
| 2982 | } |
| 2983 | |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 2984 | /* |
Manuel Pégourié-Gonnard | f3e4bd8 | 2020-07-21 13:22:41 +0200 | [diff] [blame] | 2985 | * Check for SAN match, see RFC 5280 Section 4.2.1.6 |
| 2986 | */ |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2987 | static int x509_crt_check_san(const mbedtls_x509_sequence *san, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2988 | const char *cn, size_t cn_len) |
Manuel Pégourié-Gonnard | f3e4bd8 | 2020-07-21 13:22:41 +0200 | [diff] [blame] | 2989 | { |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2990 | int san_ip = 0; |
Andrzej Kurek | 199eab9 | 2023-05-10 09:57:19 -0400 | [diff] [blame] | 2991 | int san_uri = 0; |
| 2992 | /* Prioritize DNS name over other subtypes due to popularity */ |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2993 | for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) { |
| 2994 | switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) { |
Andrzej Kurek | 199eab9 | 2023-05-10 09:57:19 -0400 | [diff] [blame] | 2995 | case MBEDTLS_X509_SAN_DNS_NAME: |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 2996 | if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) { |
| 2997 | return 0; |
| 2998 | } |
| 2999 | break; |
Andrzej Kurek | 199eab9 | 2023-05-10 09:57:19 -0400 | [diff] [blame] | 3000 | case MBEDTLS_X509_SAN_IP_ADDRESS: |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 3001 | san_ip = 1; |
| 3002 | break; |
Andrzej Kurek | 199eab9 | 2023-05-10 09:57:19 -0400 | [diff] [blame] | 3003 | case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: |
| 3004 | san_uri = 1; |
| 3005 | break; |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 3006 | /* (We may handle other types here later.) */ |
| 3007 | default: /* Unrecognized type */ |
| 3008 | break; |
| 3009 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3010 | } |
Andrzej Kurek | 199eab9 | 2023-05-10 09:57:19 -0400 | [diff] [blame] | 3011 | if (san_ip) { |
| 3012 | if (x509_crt_check_san_ip(san, cn, cn_len) == 0) { |
| 3013 | return 0; |
| 3014 | } |
| 3015 | } |
| 3016 | if (san_uri) { |
| 3017 | if (x509_crt_check_san_uri(san, cn, cn_len) == 0) { |
| 3018 | return 0; |
| 3019 | } |
| 3020 | } |
Manuel Pégourié-Gonnard | f3e4bd8 | 2020-07-21 13:22:41 +0200 | [diff] [blame] | 3021 | |
Andrzej Kurek | 199eab9 | 2023-05-10 09:57:19 -0400 | [diff] [blame] | 3022 | return -1; |
Manuel Pégourié-Gonnard | f3e4bd8 | 2020-07-21 13:22:41 +0200 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | /* |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3026 | * Verify the requested CN - only call this if cn is not NULL! |
| 3027 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3028 | static void x509_crt_verify_name(const mbedtls_x509_crt *crt, |
| 3029 | const char *cn, |
| 3030 | uint32_t *flags) |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3031 | { |
Manuel Pégourié-Gonnard | a468eb1 | 2017-07-04 01:31:59 +0200 | [diff] [blame] | 3032 | const mbedtls_x509_name *name; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3033 | size_t cn_len = strlen(cn); |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3035 | if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) { |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 3036 | if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) { |
| 3037 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | } |
| 3039 | } else { |
| 3040 | for (name = &crt->subject; name != NULL; name = name->next) { |
| 3041 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 && |
| 3042 | x509_crt_check_cn(&name->val, cn, cn_len) == 0) { |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 3043 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3047 | } |
Glenn Strauss | c26bd76 | 2022-10-23 19:48:18 -0400 | [diff] [blame] | 3048 | |
| 3049 | *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH; |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | /* |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3053 | * Merge the flags for all certs in the chain, after calling callback |
| 3054 | */ |
| 3055 | static int x509_crt_merge_flags_with_cb( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3056 | uint32_t *flags, |
| 3057 | const mbedtls_x509_crt_verify_chain *ver_chain, |
| 3058 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3059 | void *p_vrfy) |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3060 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3061 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bb216bd | 2017-08-28 13:25:55 +0200 | [diff] [blame] | 3062 | unsigned i; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3063 | uint32_t cur_flags; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3064 | const mbedtls_x509_crt_verify_chain_item *cur; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3066 | for (i = ver_chain->len; i != 0; --i) { |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3067 | cur = &ver_chain->items[i-1]; |
| 3068 | cur_flags = cur->flags; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3069 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3070 | if (NULL != f_vrfy) { |
| 3071 | if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) { |
| 3072 | return ret; |
| 3073 | } |
| 3074 | } |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3075 | |
| 3076 | *flags |= cur_flags; |
| 3077 | } |
| 3078 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | return 0; |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | /* |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3083 | * Verify the certificate validity, with profile, restartable version |
| 3084 | * |
| 3085 | * This function: |
| 3086 | * - checks the requested CN (if any) |
| 3087 | * - checks the type and size of the EE cert's key, |
| 3088 | * as that isn't done as part of chain building/verification currently |
| 3089 | * - builds and verifies the chain |
| 3090 | * - then calls the callback and merges the flags |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3091 | * |
| 3092 | * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb` |
| 3093 | * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the |
| 3094 | * verification routine to search for trusted signers, and CRLs will |
| 3095 | * be disabled. Otherwise, `trust_ca` will be used as the static list |
| 3096 | * of trusted signers, and `ca_crl` will be use as the static list |
| 3097 | * of CRLs. |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3098 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3099 | static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt, |
| 3100 | mbedtls_x509_crt *trust_ca, |
| 3101 | mbedtls_x509_crl *ca_crl, |
| 3102 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 3103 | void *p_ca_cb, |
| 3104 | const mbedtls_x509_crt_profile *profile, |
| 3105 | const char *cn, uint32_t *flags, |
| 3106 | int (*f_vrfy)(void *, |
| 3107 | mbedtls_x509_crt *, |
| 3108 | int, |
| 3109 | uint32_t *), |
| 3110 | void *p_vrfy, |
| 3111 | mbedtls_x509_crt_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3112 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3113 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 3114 | mbedtls_pk_type_t pk_type; |
Manuel Pégourié-Gonnard | c11e4ba | 2017-08-14 17:17:14 +0200 | [diff] [blame] | 3115 | mbedtls_x509_crt_verify_chain ver_chain; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3116 | uint32_t ee_flags; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3117 | |
| 3118 | *flags = 0; |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3119 | ee_flags = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3120 | x509_crt_verify_chain_reset(&ver_chain); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | if (profile == NULL) { |
Manuel Pégourié-Gonnard | d15795a | 2017-06-22 12:19:27 +0200 | [diff] [blame] | 3123 | ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 3124 | goto exit; |
| 3125 | } |
| 3126 | |
Manuel Pégourié-Gonnard | 1300e99 | 2017-07-04 01:13:44 +0200 | [diff] [blame] | 3127 | /* check name if requested */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3128 | if (cn != NULL) { |
| 3129 | x509_crt_verify_name(crt, cn, &ee_flags); |
| 3130 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3131 | |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 3132 | /* Check the type and size of the key */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3133 | pk_type = mbedtls_pk_get_type(&crt->pk); |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 3134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3135 | if (x509_profile_check_pk_alg(profile, pk_type) != 0) { |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3136 | ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3137 | } |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 3138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3139 | if (x509_profile_check_key(profile, &crt->pk) != 0) { |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3140 | ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3141 | } |
Manuel Pégourié-Gonnard | 65eefc8 | 2015-10-23 14:08:48 +0200 | [diff] [blame] | 3142 | |
Manuel Pégourié-Gonnard | bdc5440 | 2017-07-04 00:33:39 +0200 | [diff] [blame] | 3143 | /* Check the chain */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3144 | ret = x509_crt_verify_chain(crt, trust_ca, ca_crl, |
| 3145 | f_ca_cb, p_ca_cb, profile, |
| 3146 | &ver_chain, rs_ctx); |
Manuel Pégourié-Gonnard | a4a5d1d | 2017-07-17 10:26:19 +0200 | [diff] [blame] | 3147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3148 | if (ret != 0) { |
Manuel Pégourié-Gonnard | c547d1a | 2017-07-05 13:28:45 +0200 | [diff] [blame] | 3149 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3150 | } |
Manuel Pégourié-Gonnard | c547d1a | 2017-07-05 13:28:45 +0200 | [diff] [blame] | 3151 | |
Manuel Pégourié-Gonnard | 83e923b | 2017-08-23 10:55:41 +0200 | [diff] [blame] | 3152 | /* Merge end-entity flags */ |
| 3153 | ver_chain.items[0].flags |= ee_flags; |
| 3154 | |
Manuel Pégourié-Gonnard | a707e1d | 2017-07-05 17:18:42 +0200 | [diff] [blame] | 3155 | /* Build final flags, calling callback on the way if any */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3156 | ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3157 | |
Manuel Pégourié-Gonnard | d15795a | 2017-06-22 12:19:27 +0200 | [diff] [blame] | 3158 | exit: |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 3159 | |
| 3160 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3161 | mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result); |
| 3162 | mbedtls_free(ver_chain.trust_ca_cb_result); |
Hanno Becker | f53893b | 2019-03-28 13:45:55 +0000 | [diff] [blame] | 3163 | ver_chain.trust_ca_cb_result = NULL; |
| 3164 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 3165 | |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3166 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3167 | if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 3168 | mbedtls_x509_crt_restart_free(rs_ctx); |
| 3169 | } |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3170 | #endif |
| 3171 | |
Manuel Pégourié-Gonnard | 9107b5f | 2017-07-06 12:16:25 +0200 | [diff] [blame] | 3172 | /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by |
| 3173 | * the SSL module for authmode optional, but non-zero return from the |
| 3174 | * callback means a fatal error so it shouldn't be ignored */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3175 | if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) { |
Manuel Pégourié-Gonnard | 31458a1 | 2017-06-26 10:11:49 +0200 | [diff] [blame] | 3176 | ret = MBEDTLS_ERR_X509_FATAL_ERROR; |
Manuel Pégourié-Gonnard | d15795a | 2017-06-22 12:19:27 +0200 | [diff] [blame] | 3177 | } |
| 3178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3179 | if (ret != 0) { |
| 3180 | *flags = (uint32_t) -1; |
| 3181 | return ret; |
| 3182 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3184 | if (*flags != 0) { |
| 3185 | return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED; |
| 3186 | } |
| 3187 | |
| 3188 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3189 | } |
| 3190 | |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3191 | |
| 3192 | /* |
| 3193 | * Verify the certificate validity (default profile, not restartable) |
| 3194 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3195 | int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt, |
| 3196 | mbedtls_x509_crt *trust_ca, |
| 3197 | mbedtls_x509_crl *ca_crl, |
| 3198 | const char *cn, uint32_t *flags, |
| 3199 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3200 | void *p_vrfy) |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3201 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3202 | return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl, |
| 3203 | NULL, NULL, |
| 3204 | &mbedtls_x509_crt_profile_default, |
| 3205 | cn, flags, |
| 3206 | f_vrfy, p_vrfy, NULL); |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | /* |
| 3210 | * Verify the certificate validity (user-chosen profile, not restartable) |
| 3211 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3212 | int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt, |
| 3213 | mbedtls_x509_crt *trust_ca, |
| 3214 | mbedtls_x509_crl *ca_crl, |
| 3215 | const mbedtls_x509_crt_profile *profile, |
| 3216 | const char *cn, uint32_t *flags, |
| 3217 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3218 | void *p_vrfy) |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3219 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3220 | return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl, |
| 3221 | NULL, NULL, |
| 3222 | profile, cn, flags, |
| 3223 | f_vrfy, p_vrfy, NULL); |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) |
| 3227 | /* |
| 3228 | * Verify the certificate validity (user-chosen profile, CA callback, |
| 3229 | * not restartable). |
| 3230 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3231 | int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt, |
| 3232 | mbedtls_x509_crt_ca_cb_t f_ca_cb, |
| 3233 | void *p_ca_cb, |
| 3234 | const mbedtls_x509_crt_profile *profile, |
| 3235 | const char *cn, uint32_t *flags, |
| 3236 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3237 | void *p_vrfy) |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3238 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3239 | return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL, |
| 3240 | f_ca_cb, p_ca_cb, |
| 3241 | profile, cn, flags, |
| 3242 | f_vrfy, p_vrfy, NULL); |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3243 | } |
| 3244 | #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ |
| 3245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3246 | int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt, |
| 3247 | mbedtls_x509_crt *trust_ca, |
| 3248 | mbedtls_x509_crl *ca_crl, |
| 3249 | const mbedtls_x509_crt_profile *profile, |
| 3250 | const char *cn, uint32_t *flags, |
| 3251 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
| 3252 | void *p_vrfy, |
| 3253 | mbedtls_x509_crt_restart_ctx *rs_ctx) |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3254 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3255 | return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl, |
| 3256 | NULL, NULL, |
| 3257 | profile, cn, flags, |
| 3258 | f_vrfy, p_vrfy, rs_ctx); |
Hanno Becker | 3116fb3 | 2019-03-28 13:34:42 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3262 | /* |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 3263 | * Initialize a certificate chain |
| 3264 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3265 | void mbedtls_x509_crt_init(mbedtls_x509_crt *crt) |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 3266 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3267 | memset(crt, 0, sizeof(mbedtls_x509_crt)); |
Paul Bakker | 369d2eb | 2013-09-18 11:58:25 +0200 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | /* |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3271 | * Unallocate all certificate data |
| 3272 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3273 | void mbedtls_x509_crt_free(mbedtls_x509_crt *crt) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3274 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3275 | mbedtls_x509_crt *cert_cur = crt; |
| 3276 | mbedtls_x509_crt *cert_prv; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | while (cert_cur != NULL) { |
| 3279 | mbedtls_pk_free(&cert_cur->pk); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3281 | #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3282 | mbedtls_free(cert_cur->sig_opts); |
Manuel Pégourié-Gonnard | f75f2f7 | 2014-06-05 15:14:28 +0200 | [diff] [blame] | 3283 | #endif |
| 3284 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3285 | mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next); |
| 3286 | mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next); |
| 3287 | mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next); |
| 3288 | mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next); |
| 3289 | mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next); |
Przemek Stekiel | 690ff69 | 2023-05-15 09:54:02 +0200 | [diff] [blame] | 3290 | mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next); |
Ron Eldor | 74d9acc | 2019-03-21 14:00:03 +0200 | [diff] [blame] | 3291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3292 | if (cert_cur->raw.p != NULL && cert_cur->own_buffer) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 3293 | mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3294 | } |
| 3295 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3296 | cert_prv = cert_cur; |
| 3297 | cert_cur = cert_cur->next; |
| 3298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3299 | mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt)); |
| 3300 | if (cert_prv != crt) { |
| 3301 | mbedtls_free(cert_prv); |
| 3302 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3303 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 3304 | } |
| 3305 | |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3306 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
| 3307 | /* |
| 3308 | * Initialize a restart context |
| 3309 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3310 | void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3311 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | mbedtls_pk_restart_init(&ctx->pk); |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3313 | |
| 3314 | ctx->parent = NULL; |
| 3315 | ctx->fallback_parent = NULL; |
Manuel Pégourié-Gonnard | 78d7e8c | 2018-07-02 12:33:14 +0200 | [diff] [blame] | 3316 | ctx->fallback_signature_is_good = 0; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3317 | |
| 3318 | ctx->parent_is_trusted = -1; |
| 3319 | |
Manuel Pégourié-Gonnard | daf0491 | 2017-08-23 12:32:19 +0200 | [diff] [blame] | 3320 | ctx->in_progress = x509_crt_rs_none; |
Manuel Pégourié-Gonnard | 8b59049 | 2017-08-14 18:04:19 +0200 | [diff] [blame] | 3321 | ctx->self_cnt = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | x509_crt_verify_chain_reset(&ctx->ver_chain); |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | /* |
| 3326 | * Free the components of a restart context |
| 3327 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3328 | void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3329 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3330 | if (ctx == NULL) { |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3331 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3332 | } |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3334 | mbedtls_pk_restart_free(&ctx->pk); |
| 3335 | mbedtls_x509_crt_restart_init(ctx); |
Manuel Pégourié-Gonnard | bc3f44a | 2017-07-11 11:02:20 +0200 | [diff] [blame] | 3336 | } |
| 3337 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
| 3338 | |
Minos Galanakis | 12b493f | 2023-08-11 15:22:45 +0100 | [diff] [blame] | 3339 | #if defined(MBEDTLS_POP_TARGET_PRAGMA) |
| 3340 | #undef MBEDTLS_POP_TARGET_PRAGMA |
| 3341 | #endif |
| 3342 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3343 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |