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