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