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