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