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