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