| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
// |
| 5 |
// Created by Smith, Justin on 6/16/25. |
| 6 |
// |
| 7 |
|
| 8 |
#include <openssl/ssl.h> |
| 9 |
#include <thread> |
| 10 |
#include "../crypto/test/test_util.h" |
| 11 |
#include "ssl_common_test.h" |
| 12 |
|
| 13 |
BSSL_NAMESPACE_BEGIN |
| 14 |
|
| 15 |
struct ECHConfigParams { |
| 16 |
uint16_t version = TLSEXT_TYPE_encrypted_client_hello; |
| 17 |
uint16_t config_id = 1; |
| 18 |
std::string public_name = "example.com"; |
| 19 |
const EVP_HPKE_KEY *key = nullptr; |
| 20 |
// kem_id, if zero, takes its value from |key|. |
| 21 |
uint16_t kem_id = 0; |
| 22 |
// public_key, if empty takes its value from |key|. |
| 23 |
std::vector<uint8_t> public_key; |
| 24 |
size_t max_name_len = 16; |
| 25 |
// cipher_suites is a list of code points which should contain pairs of KDF |
| 26 |
// and AEAD IDs. |
| 27 |
std::vector<uint16_t> cipher_suites = {EVP_HPKE_HKDF_SHA256, |
| 28 |
EVP_HPKE_AES_128_GCM}; |
| 29 |
std::vector<uint8_t> extensions; |
| 30 |
}; |
| 31 |
|
| 32 |
// MakeECHConfig serializes an ECHConfig from |params| and writes it to |
| 33 |
// |*out|. |
| 34 |
static bool MakeECHConfig(std::vector<uint8_t> *out, |
| 35 |
const ECHConfigParams ¶ms) { |
| 36 |
uint16_t kem_id = params.kem_id == 0 |
| 37 |
? EVP_HPKE_KEM_id(EVP_HPKE_KEY_kem(params.key)) |
| 38 |
: params.kem_id; |
| 39 |
std::vector<uint8_t> public_key = params.public_key; |
| 40 |
if (public_key.empty()) { |
| 41 |
public_key.resize(EVP_HPKE_MAX_PUBLIC_KEY_LENGTH); |
| 42 |
size_t len = 0; |
| 43 |
if (!EVP_HPKE_KEY_public_key(params.key, public_key.data(), &len, |
| 44 |
public_key.size())) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
public_key.resize(len); |
| 48 |
} |
| 49 |
|
| 50 |
bssl::ScopedCBB cbb; |
| 51 |
CBB contents, child; |
| 52 |
if (!CBB_init(cbb.get(), 64) || !CBB_add_u16(cbb.get(), params.version) || |
| 53 |
!CBB_add_u16_length_prefixed(cbb.get(), &contents) || |
| 54 |
!CBB_add_u8(&contents, params.config_id) || |
| 55 |
!CBB_add_u16(&contents, kem_id) || |
| 56 |
!CBB_add_u16_length_prefixed(&contents, &child) || |
| 57 |
!CBB_add_bytes(&child, public_key.data(), public_key.size()) || |
| 58 |
!CBB_add_u16_length_prefixed(&contents, &child)) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
for (uint16_t cipher_suite : params.cipher_suites) { |
| 62 |
if (!CBB_add_u16(&child, cipher_suite)) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
} |
| 66 |
if (!CBB_add_u8(&contents, params.max_name_len) || |
| 67 |
!CBB_add_u8_length_prefixed(&contents, &child) || |
| 68 |
!CBB_add_bytes( |
| 69 |
&child, reinterpret_cast<const uint8_t *>(params.public_name.data()), |
| 70 |
params.public_name.size()) || |
| 71 |
!CBB_add_u16_length_prefixed(&contents, &child) || |
| 72 |
!CBB_add_bytes(&child, params.extensions.data(), |
| 73 |
params.extensions.size()) || |
| 74 |
!CBB_flush(cbb.get())) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
out->assign(CBB_data(cbb.get()), CBB_data(cbb.get()) + CBB_len(cbb.get())); |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
static bssl::UniquePtr<SSL_ECH_KEYS> MakeTestECHKeys(uint8_t config_id = 1) { |
| 83 |
bssl::ScopedEVP_HPKE_KEY key; |
| 84 |
uint8_t *ech_config = nullptr; |
| 85 |
size_t ech_config_len = 0; |
| 86 |
if (!EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256()) || |
| 87 |
!SSL_marshal_ech_config(&ech_config, &ech_config_len, config_id, |
| 88 |
key.get(), "public.example", 16)) { |
| 89 |
return nullptr; |
| 90 |
} |
| 91 |
bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 92 |
|
| 93 |
// Install a non-retry config. |
| 94 |
bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 95 |
if (!keys || !SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config, |
| 96 |
ech_config_len, key.get())) { |
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
return keys; |
| 100 |
} |
| 101 |
|
| 102 |
static bool InstallECHConfigList(SSL *client, const SSL_ECH_KEYS *keys) { |
| 103 |
uint8_t *ech_config_list = nullptr; |
| 104 |
size_t ech_config_list_len = 0; |
| 105 |
if (!SSL_ECH_KEYS_marshal_retry_configs(keys, &ech_config_list, |
| 106 |
&ech_config_list_len)) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
bssl::UniquePtr<uint8_t> free_ech_config_list(ech_config_list); |
| 110 |
return SSL_set1_ech_config_list(client, ech_config_list, ech_config_list_len); |
| 111 |
} |
| 112 |
|
| 113 |
// Test that |SSL_marshal_ech_config| and |SSL_ECH_KEYS_marshal_retry_configs| |
| 114 |
// output values as expected. |
| 115 |
TEST(SSLTest, MarshalECHConfig) { |
| 116 |
static const uint8_t kPrivateKey[X25519_PRIVATE_KEY_LEN] = { |
| 117 |
0xbc, 0xb5, 0x51, 0x29, 0x31, 0x10, 0x30, 0xc9, 0xed, 0x26, 0xde, |
| 118 |
0xd4, 0xb3, 0xdf, 0x3a, 0xce, 0x06, 0x8a, 0xee, 0x17, 0xab, 0xce, |
| 119 |
0xd7, 0xdb, 0xf3, 0x11, 0xe5, 0xa8, 0xf3, 0xb1, 0x8e, 0x24}; |
| 120 |
bssl::ScopedEVP_HPKE_KEY key; |
| 121 |
ASSERT_TRUE(EVP_HPKE_KEY_init(key.get(), EVP_hpke_x25519_hkdf_sha256(), |
| 122 |
kPrivateKey, sizeof(kPrivateKey))); |
| 123 |
|
| 124 |
static const uint8_t kECHConfig[] = { |
| 125 |
// version |
| 126 |
0xfe, 0x0d, |
| 127 |
// length |
| 128 |
0x00, 0x41, |
| 129 |
// contents.config_id |
| 130 |
0x01, |
| 131 |
// contents.kem_id |
| 132 |
0x00, 0x20, |
| 133 |
// contents.public_key |
| 134 |
0x00, 0x20, 0xa6, 0x9a, 0x41, 0x48, 0x5d, 0x32, 0x96, 0xa4, 0xe0, 0xc3, |
| 135 |
0x6a, 0xee, 0xf6, 0x63, 0x0f, 0x59, 0x32, 0x6f, 0xdc, 0xff, 0x81, 0x29, |
| 136 |
0x59, 0xa5, 0x85, 0xd3, 0x9b, 0x3b, 0xde, 0x98, 0x55, 0x5c, |
| 137 |
// contents.cipher_suites |
| 138 |
0x00, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x03, |
| 139 |
// contents.maximum_name_length |
| 140 |
0x10, |
| 141 |
// contents.public_name |
| 142 |
0x0e, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x2e, 0x65, 0x78, 0x61, 0x6d, |
| 143 |
0x70, 0x6c, 0x65, |
| 144 |
// contents.extensions |
| 145 |
0x00, 0x00}; |
| 146 |
uint8_t *ech_config = nullptr; |
| 147 |
size_t ech_config_len = 0; |
| 148 |
ASSERT_TRUE(SSL_marshal_ech_config(&ech_config, &ech_config_len, |
| 149 |
/*config_id=*/1, key.get(), |
| 150 |
"public.example", 16)); |
| 151 |
bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 152 |
EXPECT_EQ(Bytes(kECHConfig), Bytes(ech_config, ech_config_len)); |
| 153 |
|
| 154 |
// Generate a second ECHConfig. |
| 155 |
bssl::ScopedEVP_HPKE_KEY key2; |
| 156 |
ASSERT_TRUE(EVP_HPKE_KEY_generate(key2.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 157 |
uint8_t *ech_config2 = nullptr; |
| 158 |
size_t ech_config2_len = 0; |
| 159 |
ASSERT_TRUE(SSL_marshal_ech_config(&ech_config2, &ech_config2_len, |
| 160 |
/*config_id=*/2, key2.get(), |
| 161 |
"public.example", 16)); |
| 162 |
bssl::UniquePtr<uint8_t> free_ech_config2(ech_config2); |
| 163 |
|
| 164 |
// Install both ECHConfigs in an |SSL_ECH_KEYS|. |
| 165 |
bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 166 |
ASSERT_TRUE(keys); |
| 167 |
ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config, |
| 168 |
ech_config_len, key.get())); |
| 169 |
ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config2, |
| 170 |
ech_config2_len, key2.get())); |
| 171 |
|
| 172 |
// The ECHConfigList should be correctly serialized. |
| 173 |
uint8_t *ech_config_list = nullptr; |
| 174 |
size_t ech_config_list_len = 0; |
| 175 |
ASSERT_TRUE(SSL_ECH_KEYS_marshal_retry_configs(keys.get(), &ech_config_list, |
| 176 |
&ech_config_list_len)); |
| 177 |
bssl::UniquePtr<uint8_t> free_ech_config_list(ech_config_list); |
| 178 |
|
| 179 |
// ECHConfigList is just the concatenation with a length prefix. |
| 180 |
size_t len = ech_config_len + ech_config2_len; |
| 181 |
std::vector<uint8_t> expected = {uint8_t(len >> 8), uint8_t(len)}; |
| 182 |
expected.insert(expected.end(), ech_config, ech_config + ech_config_len); |
| 183 |
expected.insert(expected.end(), ech_config2, ech_config2 + ech_config2_len); |
| 184 |
EXPECT_EQ(Bytes(expected), Bytes(ech_config_list, ech_config_list_len)); |
| 185 |
} |
| 186 |
|
| 187 |
TEST(SSLTest, ECHHasDuplicateConfigID) { |
| 188 |
const struct { |
| 189 |
std::vector<uint8_t> ids; |
| 190 |
bool has_duplicate; |
| 191 |
} kTests[] = { |
| 192 |
{{}, false}, |
| 193 |
{{1}, false}, |
| 194 |
{{1, 2, 3, 255}, false}, |
| 195 |
{{1, 2, 3, 1}, true}, |
| 196 |
}; |
| 197 |
for (const auto &test : kTests) { |
| 198 |
bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 199 |
ASSERT_TRUE(keys); |
| 200 |
for (const uint8_t id : test.ids) { |
| 201 |
bssl::ScopedEVP_HPKE_KEY key; |
| 202 |
ASSERT_TRUE( |
| 203 |
EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 204 |
uint8_t *ech_config = nullptr; |
| 205 |
size_t ech_config_len = 0; |
| 206 |
ASSERT_TRUE(SSL_marshal_ech_config(&ech_config, &ech_config_len, id, |
| 207 |
key.get(), "public.example", 16)); |
| 208 |
bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 209 |
ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 210 |
ech_config, ech_config_len, key.get())); |
| 211 |
} |
| 212 |
|
| 213 |
EXPECT_EQ(test.has_duplicate ? 1 : 0, |
| 214 |
SSL_ECH_KEYS_has_duplicate_config_id(keys.get())); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// Test that |SSL_ECH_KEYS_add| checks consistency between the public and |
| 219 |
// private key. |
| 220 |
TEST(SSLTest, ECHKeyConsistency) { |
| 221 |
bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 222 |
ASSERT_TRUE(keys); |
| 223 |
bssl::ScopedEVP_HPKE_KEY key; |
| 224 |
ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 225 |
uint8_t public_key[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH]; |
| 226 |
size_t public_key_len = 0; |
| 227 |
ASSERT_TRUE(EVP_HPKE_KEY_public_key(key.get(), public_key, &public_key_len, |
| 228 |
sizeof(public_key))); |
| 229 |
|
| 230 |
// Adding an ECHConfig with the matching public key succeeds. |
| 231 |
ECHConfigParams params; |
| 232 |
params.key = key.get(); |
| 233 |
std::vector<uint8_t> ech_config; |
| 234 |
ASSERT_TRUE(MakeECHConfig(&ech_config, params)); |
| 235 |
EXPECT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 236 |
ech_config.data(), ech_config.size(), |
| 237 |
key.get())); |
| 238 |
|
| 239 |
// Adding an ECHConfig with the wrong public key is an error. |
| 240 |
bssl::ScopedEVP_HPKE_KEY wrong_key; |
| 241 |
ASSERT_TRUE( |
| 242 |
EVP_HPKE_KEY_generate(wrong_key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 243 |
EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 244 |
ech_config.data(), ech_config.size(), |
| 245 |
wrong_key.get())); |
| 246 |
|
| 247 |
// Adding an ECHConfig with a truncated public key is an error. |
| 248 |
ECHConfigParams truncated; |
| 249 |
truncated.key = key.get(); |
| 250 |
truncated.public_key.assign(public_key, public_key + public_key_len - 1); |
| 251 |
ASSERT_TRUE(MakeECHConfig(&ech_config, truncated)); |
| 252 |
EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 253 |
ech_config.data(), ech_config.size(), |
| 254 |
key.get())); |
| 255 |
|
| 256 |
// Adding an ECHConfig with the right public key, but wrong KEM ID, is an |
| 257 |
// error. |
| 258 |
ECHConfigParams wrong_kem; |
| 259 |
wrong_kem.key = key.get(); |
| 260 |
wrong_kem.kem_id = 0x0010; // DHKEM(P-256, HKDF-SHA256) |
| 261 |
ASSERT_TRUE(MakeECHConfig(&ech_config, wrong_kem)); |
| 262 |
EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 263 |
ech_config.data(), ech_config.size(), |
| 264 |
key.get())); |
| 265 |
} |
| 266 |
|
| 267 |
// Test that |SSL_CTX_set1_ech_keys| fails when the config list |
| 268 |
// has no retry configs. |
| 269 |
TEST(SSLTest, ECHServerConfigsWithoutRetryConfigs) { |
| 270 |
bssl::ScopedEVP_HPKE_KEY key; |
| 271 |
ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 272 |
uint8_t *ech_config = nullptr; |
| 273 |
size_t ech_config_len = 0; |
| 274 |
ASSERT_TRUE(SSL_marshal_ech_config(&ech_config, &ech_config_len, |
| 275 |
/*config_id=*/1, key.get(), |
| 276 |
"public.example", 16)); |
| 277 |
bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 278 |
|
| 279 |
// Install a non-retry config. |
| 280 |
bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 281 |
ASSERT_TRUE(keys); |
| 282 |
ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/0, ech_config, |
| 283 |
ech_config_len, key.get())); |
| 284 |
|
| 285 |
// |keys| has no retry configs. |
| 286 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 287 |
ASSERT_TRUE(ctx); |
| 288 |
EXPECT_FALSE(SSL_CTX_set1_ech_keys(ctx.get(), keys.get())); |
| 289 |
|
| 290 |
// Add the same ECHConfig to the list, but this time mark it as a retry |
| 291 |
// config. |
| 292 |
ASSERT_TRUE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config, |
| 293 |
ech_config_len, key.get())); |
| 294 |
EXPECT_TRUE(SSL_CTX_set1_ech_keys(ctx.get(), keys.get())); |
| 295 |
} |
| 296 |
|
| 297 |
// Test that the server APIs reject ECHConfigs with unsupported features. |
| 298 |
TEST(SSLTest, UnsupportedECHConfig) { |
| 299 |
bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 300 |
ASSERT_TRUE(keys); |
| 301 |
bssl::ScopedEVP_HPKE_KEY key; |
| 302 |
ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 303 |
|
| 304 |
// Unsupported versions are rejected. |
| 305 |
ECHConfigParams unsupported_version; |
| 306 |
unsupported_version.version = 0xffff; |
| 307 |
unsupported_version.key = key.get(); |
| 308 |
std::vector<uint8_t> ech_config; |
| 309 |
ASSERT_TRUE(MakeECHConfig(&ech_config, unsupported_version)); |
| 310 |
EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 311 |
ech_config.data(), ech_config.size(), |
| 312 |
key.get())); |
| 313 |
|
| 314 |
// Unsupported cipher suites are rejected. (We only support HKDF-SHA256.) |
| 315 |
ECHConfigParams unsupported_kdf; |
| 316 |
unsupported_kdf.key = key.get(); |
| 317 |
unsupported_kdf.cipher_suites = {0x002 /* HKDF-SHA384 */, |
| 318 |
EVP_HPKE_AES_128_GCM}; |
| 319 |
ASSERT_TRUE(MakeECHConfig(&ech_config, unsupported_kdf)); |
| 320 |
EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 321 |
ech_config.data(), ech_config.size(), |
| 322 |
key.get())); |
| 323 |
ECHConfigParams unsupported_aead; |
| 324 |
unsupported_aead.key = key.get(); |
| 325 |
unsupported_aead.cipher_suites = {EVP_HPKE_HKDF_SHA256, 0xffff}; |
| 326 |
ASSERT_TRUE(MakeECHConfig(&ech_config, unsupported_aead)); |
| 327 |
EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 328 |
ech_config.data(), ech_config.size(), |
| 329 |
key.get())); |
| 330 |
|
| 331 |
|
| 332 |
// Unsupported extensions are rejected. |
| 333 |
ECHConfigParams extensions; |
| 334 |
extensions.key = key.get(); |
| 335 |
extensions.extensions = {0x00, 0x01, 0x00, 0x00}; |
| 336 |
ASSERT_TRUE(MakeECHConfig(&ech_config, extensions)); |
| 337 |
EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 338 |
ech_config.data(), ech_config.size(), |
| 339 |
key.get())); |
| 340 |
|
| 341 |
// Invalid public names are rejected. |
| 342 |
ECHConfigParams invalid_public_name; |
| 343 |
invalid_public_name.key = key.get(); |
| 344 |
invalid_public_name.public_name = "dns_names_have_no_underscores.example"; |
| 345 |
ASSERT_TRUE(MakeECHConfig(&ech_config, invalid_public_name)); |
| 346 |
EXPECT_FALSE(SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, |
| 347 |
ech_config.data(), ech_config.size(), |
| 348 |
key.get())); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
// Test that |SSL_get_client_random| reports the correct value on both client |
| 353 |
// and server in ECH. The client sends two different random values. When ECH is |
| 354 |
// accepted, we should report the inner one. |
| 355 |
TEST(SSLTest, ECHClientRandomsMatch) { |
| 356 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 357 |
CreateContextWithTestCertificate(TLS_method()); |
| 358 |
ASSERT_TRUE(server_ctx); |
| 359 |
bssl::UniquePtr<SSL_ECH_KEYS> keys = MakeTestECHKeys(); |
| 360 |
ASSERT_TRUE(keys); |
| 361 |
ASSERT_TRUE(SSL_CTX_set1_ech_keys(server_ctx.get(), keys.get())); |
| 362 |
|
| 363 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 364 |
ASSERT_TRUE(client_ctx); |
| 365 |
bssl::UniquePtr<SSL> client, server; |
| 366 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 367 |
server_ctx.get())); |
| 368 |
ASSERT_TRUE(InstallECHConfigList(client.get(), keys.get())); |
| 369 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 370 |
|
| 371 |
EXPECT_TRUE(SSL_ech_accepted(client.get())); |
| 372 |
EXPECT_TRUE(SSL_ech_accepted(server.get())); |
| 373 |
|
| 374 |
// An ECH server will fairly naturally record the inner ClientHello random, |
| 375 |
// but an ECH client may forget to update the random once ClientHelloInner is |
| 376 |
// selected. |
| 377 |
uint8_t client_random1[SSL3_RANDOM_SIZE]; |
| 378 |
uint8_t client_random2[SSL3_RANDOM_SIZE]; |
| 379 |
ASSERT_EQ(sizeof(client_random1), |
| 380 |
SSL_get_client_random(client.get(), client_random1, |
| 381 |
sizeof(client_random1))); |
| 382 |
ASSERT_EQ(sizeof(client_random2), |
| 383 |
SSL_get_client_random(server.get(), client_random2, |
| 384 |
sizeof(client_random2))); |
| 385 |
EXPECT_EQ(Bytes(client_random1), Bytes(client_random2)); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
// GetECHLength sets |*out_client_hello_len| and |*out_ech_len| to the lengths |
| 390 |
// of the ClientHello and ECH extension, respectively, when a client created |
| 391 |
// from |ctx| constructs a ClientHello with name |name| and an ECHConfig with |
| 392 |
// maximum name length |max_name_len|. |
| 393 |
static bool GetECHLength(SSL_CTX *ctx, size_t *out_client_hello_len, |
| 394 |
size_t *out_ech_len, size_t max_name_len, |
| 395 |
const char *name) { |
| 396 |
bssl::ScopedEVP_HPKE_KEY key; |
| 397 |
uint8_t *ech_config = nullptr; |
| 398 |
size_t ech_config_len = 0; |
| 399 |
if (!EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256()) || |
| 400 |
!SSL_marshal_ech_config(&ech_config, &ech_config_len, |
| 401 |
/*config_id=*/1, key.get(), "public.example", |
| 402 |
max_name_len)) { |
| 403 |
return false; |
| 404 |
} |
| 405 |
bssl::UniquePtr<uint8_t> free_ech_config(ech_config); |
| 406 |
|
| 407 |
bssl::UniquePtr<SSL_ECH_KEYS> keys(SSL_ECH_KEYS_new()); |
| 408 |
if (!keys || !SSL_ECH_KEYS_add(keys.get(), /*is_retry_config=*/1, ech_config, |
| 409 |
ech_config_len, key.get())) { |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx)); |
| 414 |
if (!ssl || !InstallECHConfigList(ssl.get(), keys.get()) || |
| 415 |
(name != nullptr && !SSL_set_tlsext_host_name(ssl.get(), name))) { |
| 416 |
return false; |
| 417 |
} |
| 418 |
SSL_set_connect_state(ssl.get()); |
| 419 |
|
| 420 |
std::vector<uint8_t> client_hello; |
| 421 |
SSL_CLIENT_HELLO parsed; |
| 422 |
const uint8_t *unused = nullptr; |
| 423 |
if (!GetClientHello(ssl.get(), &client_hello) || |
| 424 |
!ssl_client_hello_init( |
| 425 |
ssl.get(), &parsed, |
| 426 |
// Skip record and handshake headers. This assumes the ClientHello |
| 427 |
// fits in one record. |
| 428 |
MakeConstSpan(client_hello) |
| 429 |
.subspan(SSL3_RT_HEADER_LENGTH + SSL3_HM_HEADER_LENGTH)) || |
| 430 |
!SSL_early_callback_ctx_extension_get( |
| 431 |
&parsed, TLSEXT_TYPE_encrypted_client_hello, &unused, out_ech_len)) { |
| 432 |
return false; |
| 433 |
} |
| 434 |
*out_client_hello_len = client_hello.size(); |
| 435 |
return true; |
| 436 |
} |
| 437 |
|
| 438 |
TEST(SSLTest, ECHPadding) { |
| 439 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 440 |
ASSERT_TRUE(ctx); |
| 441 |
|
| 442 |
// Sample lengths with max_name_len = 128 as baseline. |
| 443 |
size_t client_hello_len_baseline = 0, ech_len_baseline = 0; |
| 444 |
ASSERT_TRUE(GetECHLength(ctx.get(), &client_hello_len_baseline, |
| 445 |
&ech_len_baseline, 128, "example.com")); |
| 446 |
|
| 447 |
// Check that all name lengths under the server's maximum look the same. |
| 448 |
for (size_t name_len : {1, 2, 32, 64, 127, 128}) { |
| 449 |
SCOPED_TRACE(name_len); |
| 450 |
size_t client_hello_len = 0, ech_len = 0; |
| 451 |
ASSERT_TRUE(GetECHLength(ctx.get(), &client_hello_len, &ech_len, 128, |
| 452 |
std::string(name_len, 'a').c_str())); |
| 453 |
EXPECT_EQ(client_hello_len, client_hello_len_baseline); |
| 454 |
EXPECT_EQ(ech_len, ech_len_baseline); |
| 455 |
} |
| 456 |
|
| 457 |
// When sending no SNI, we must still pad as if we are sending one. |
| 458 |
size_t client_hello_len = 0, ech_len = 0; |
| 459 |
ASSERT_TRUE( |
| 460 |
GetECHLength(ctx.get(), &client_hello_len, &ech_len, 128, nullptr)); |
| 461 |
EXPECT_EQ(client_hello_len, client_hello_len_baseline); |
| 462 |
EXPECT_EQ(ech_len, ech_len_baseline); |
| 463 |
|
| 464 |
// Name lengths above the maximum do not get named-based padding, but the |
| 465 |
// overall input is padded to a multiple of 32. |
| 466 |
size_t client_hello_len_baseline2 = 0, ech_len_baseline2 = 0; |
| 467 |
ASSERT_TRUE(GetECHLength(ctx.get(), &client_hello_len_baseline2, |
| 468 |
&ech_len_baseline2, 128, |
| 469 |
std::string(128 + 32, 'a').c_str())); |
| 470 |
EXPECT_EQ(ech_len_baseline2, ech_len_baseline + 32); |
| 471 |
// The ClientHello lengths may match if we are still under the threshold for |
| 472 |
// padding extension. |
| 473 |
EXPECT_GE(client_hello_len_baseline2, client_hello_len_baseline); |
| 474 |
|
| 475 |
for (size_t name_len = 128 + 1; name_len < 128 + 32; name_len++) { |
| 476 |
SCOPED_TRACE(name_len); |
| 477 |
ASSERT_TRUE(GetECHLength(ctx.get(), &client_hello_len, &ech_len, 128, |
| 478 |
std::string(name_len, 'a').c_str())); |
| 479 |
EXPECT_TRUE(ech_len == ech_len_baseline || ech_len == ech_len_baseline2) |
| 480 |
<< ech_len; |
| 481 |
EXPECT_TRUE(client_hello_len == client_hello_len_baseline || |
| 482 |
client_hello_len == client_hello_len_baseline2) |
| 483 |
<< client_hello_len; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
TEST(SSLTest, ECHPublicName) { |
| 488 |
auto str_to_span = [](const char *str) -> Span<const uint8_t> { |
| 489 |
return MakeConstSpan(reinterpret_cast<const uint8_t *>(str), strlen(str)); |
| 490 |
}; |
| 491 |
|
| 492 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span(""))); |
| 493 |
EXPECT_TRUE(ssl_is_valid_ech_public_name(str_to_span("example.com"))); |
| 494 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span(".example.com"))); |
| 495 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("example.com."))); |
| 496 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("example..com"))); |
| 497 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("www.-example.com"))); |
| 498 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("www.example-.com"))); |
| 499 |
EXPECT_FALSE( |
| 500 |
ssl_is_valid_ech_public_name(str_to_span("no_underscores.example"))); |
| 501 |
EXPECT_FALSE( |
| 502 |
ssl_is_valid_ech_public_name(str_to_span("invalid_chars.\x01.example"))); |
| 503 |
EXPECT_FALSE( |
| 504 |
ssl_is_valid_ech_public_name(str_to_span("invalid_chars.\xff.example"))); |
| 505 |
static const uint8_t kWithNUL[] = {'t', 'e', 's', 't', 0}; |
| 506 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(kWithNUL)); |
| 507 |
|
| 508 |
// Test an LDH label with every character and the maximum length. |
| 509 |
EXPECT_TRUE(ssl_is_valid_ech_public_name(str_to_span( |
| 510 |
"abcdefhijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789"))); |
| 511 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span( |
| 512 |
"abcdefhijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-01234567899"))); |
| 513 |
|
| 514 |
// Inputs with trailing numeric components are rejected. |
| 515 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("127.0.0.1"))); |
| 516 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("example.1"))); |
| 517 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("example.01"))); |
| 518 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("example.0x01"))); |
| 519 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("example.0X01"))); |
| 520 |
// Leading zeros and values that overflow |uint32_t| are still rejected. |
| 521 |
EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 522 |
str_to_span("example.123456789000000000000000"))); |
| 523 |
EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 524 |
str_to_span("example.012345678900000000000000"))); |
| 525 |
EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 526 |
str_to_span("example.0x123456789abcdefABCDEF0"))); |
| 527 |
EXPECT_FALSE(ssl_is_valid_ech_public_name( |
| 528 |
str_to_span("example.0x0123456789abcdefABCDEF"))); |
| 529 |
// Adding a non-digit or non-hex character makes it a valid DNS name again. |
| 530 |
// Single-component numbers are rejected. |
| 531 |
EXPECT_TRUE(ssl_is_valid_ech_public_name(str_to_span("example.1234567890a"))); |
| 532 |
EXPECT_TRUE( |
| 533 |
ssl_is_valid_ech_public_name(str_to_span("example.01234567890a"))); |
| 534 |
EXPECT_TRUE( |
| 535 |
ssl_is_valid_ech_public_name(str_to_span("example.0x123456789abcdefg"))); |
| 536 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("1"))); |
| 537 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("01"))); |
| 538 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("0x01"))); |
| 539 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("0X01"))); |
| 540 |
// Numbers with trailing dots are rejected. (They are already rejected by the |
| 541 |
// LDH label rules, but the WHATWG URL parser additionally rejects them.) |
| 542 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("1."))); |
| 543 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("01."))); |
| 544 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("0x01."))); |
| 545 |
EXPECT_FALSE(ssl_is_valid_ech_public_name(str_to_span("0X01."))); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
// When using the built-in verifier, test that |SSL_get0_ech_name_override| is |
| 550 |
// applied automatically. |
| 551 |
TEST(SSLTest, ECHBuiltinVerifier) { |
| 552 |
// Use different config IDs so that fuzzer mode, which breaks trial |
| 553 |
// decryption, will observe the key mismatch. |
| 554 |
bssl::UniquePtr<SSL_ECH_KEYS> keys = MakeTestECHKeys(/*config_id=*/1); |
| 555 |
ASSERT_TRUE(keys); |
| 556 |
bssl::UniquePtr<SSL_ECH_KEYS> wrong_keys = MakeTestECHKeys(/*config_id=*/2); |
| 557 |
ASSERT_TRUE(wrong_keys); |
| 558 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 559 |
CreateContextWithTestCertificate(TLS_method()); |
| 560 |
ASSERT_TRUE(server_ctx); |
| 561 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 562 |
ASSERT_TRUE(client_ctx); |
| 563 |
|
| 564 |
// Configure the client to verify certificates and expect the secret name. |
| 565 |
// This is the name the client is trying to connect to. If ECH is rejected, |
| 566 |
// BoringSSL will internally override this setting with the public name. |
| 567 |
bssl::UniquePtr<X509_STORE> store(X509_STORE_new()); |
| 568 |
ASSERT_TRUE(store); |
| 569 |
ASSERT_TRUE(X509_STORE_add_cert(store.get(), GetLeafRoot().get())); |
| 570 |
SSL_CTX_set_cert_store(client_ctx.get(), store.release()); |
| 571 |
SSL_CTX_set_verify(client_ctx.get(), SSL_VERIFY_PEER, nullptr); |
| 572 |
X509_VERIFY_PARAM_set_flags(SSL_CTX_get0_param(client_ctx.get()), |
| 573 |
X509_V_FLAG_NO_CHECK_TIME); |
| 574 |
static const char kSecretName[] = "secret.example"; |
| 575 |
ASSERT_TRUE(X509_VERIFY_PARAM_set1_host(SSL_CTX_get0_param(client_ctx.get()), |
| 576 |
kSecretName, strlen(kSecretName))); |
| 577 |
|
| 578 |
// For simplicity, we only run through a pair of representative scenarios here |
| 579 |
// and rely on runner.go to verify that |SSL_get0_ech_name_override| behaves |
| 580 |
// correctly. |
| 581 |
for (bool accept_ech : {false, true}) { |
| 582 |
SCOPED_TRACE(accept_ech); |
| 583 |
for (bool use_leaf_secret : {false, true}) { |
| 584 |
SCOPED_TRACE(use_leaf_secret); |
| 585 |
|
| 586 |
// The server will reject ECH when configured with the wrong keys. |
| 587 |
ASSERT_TRUE(SSL_CTX_set1_ech_keys( |
| 588 |
server_ctx.get(), accept_ech ? keys.get() : wrong_keys.get())); |
| 589 |
|
| 590 |
bssl::UniquePtr<SSL> client, server; |
| 591 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 592 |
server_ctx.get())); |
| 593 |
ASSERT_TRUE(InstallECHConfigList(client.get(), keys.get())); |
| 594 |
|
| 595 |
// Configure the server with the selected certificate. |
| 596 |
ASSERT_TRUE(SSL_use_certificate( |
| 597 |
server.get(), |
| 598 |
use_leaf_secret ? GetLeafSecret().get() : GetLeafPublic().get())); |
| 599 |
ASSERT_TRUE(SSL_use_PrivateKey(server.get(), GetLeafKey().get())); |
| 600 |
|
| 601 |
// The handshake may fail due to name mismatch or ECH reject. We check |
| 602 |
// |SSL_get_verify_result| to confirm the handshake got far enough. |
| 603 |
CompleteHandshakes(client.get(), server.get()); |
| 604 |
EXPECT_EQ(accept_ech == use_leaf_secret ? X509_V_OK |
| 605 |
: X509_V_ERR_HOSTNAME_MISMATCH, |
| 606 |
SSL_get_verify_result(client.get())); |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
#if defined(OPENSSL_THREADS) |
| 612 |
// Test that the server ECH config can be swapped out while the |SSL_CTX| is |
| 613 |
// in use on other threads. This test is intended to be run with TSan. |
| 614 |
TEST(SSLTest, ECHThreads) { |
| 615 |
// Generate a pair of ECHConfigs. |
| 616 |
bssl::ScopedEVP_HPKE_KEY key1; |
| 617 |
ASSERT_TRUE(EVP_HPKE_KEY_generate(key1.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 618 |
uint8_t *ech_config1 = nullptr; |
| 619 |
size_t ech_config1_len = 0; |
| 620 |
ASSERT_TRUE(SSL_marshal_ech_config(&ech_config1, &ech_config1_len, |
| 621 |
/*config_id=*/1, key1.get(), |
| 622 |
"public.example", 16)); |
| 623 |
bssl::UniquePtr<uint8_t> free_ech_config1(ech_config1); |
| 624 |
bssl::ScopedEVP_HPKE_KEY key2; |
| 625 |
ASSERT_TRUE(EVP_HPKE_KEY_generate(key2.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 626 |
uint8_t *ech_config2 = nullptr; |
| 627 |
size_t ech_config2_len = 0; |
| 628 |
ASSERT_TRUE(SSL_marshal_ech_config(&ech_config2, &ech_config2_len, |
| 629 |
/*config_id=*/2, key2.get(), |
| 630 |
"public.example", 16)); |
| 631 |
bssl::UniquePtr<uint8_t> free_ech_config2(ech_config2); |
| 632 |
|
| 633 |
// |keys1| contains the first config. |keys12| contains both. |
| 634 |
bssl::UniquePtr<SSL_ECH_KEYS> keys1(SSL_ECH_KEYS_new()); |
| 635 |
ASSERT_TRUE(keys1); |
| 636 |
ASSERT_TRUE(SSL_ECH_KEYS_add(keys1.get(), /*is_retry_config=*/1, ech_config1, |
| 637 |
ech_config1_len, key1.get())); |
| 638 |
bssl::UniquePtr<SSL_ECH_KEYS> keys12(SSL_ECH_KEYS_new()); |
| 639 |
ASSERT_TRUE(keys12); |
| 640 |
ASSERT_TRUE(SSL_ECH_KEYS_add(keys12.get(), /*is_retry_config=*/1, ech_config2, |
| 641 |
ech_config2_len, key2.get())); |
| 642 |
ASSERT_TRUE(SSL_ECH_KEYS_add(keys12.get(), /*is_retry_config=*/0, ech_config1, |
| 643 |
ech_config1_len, key1.get())); |
| 644 |
|
| 645 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 646 |
CreateContextWithTestCertificate(TLS_method()); |
| 647 |
ASSERT_TRUE(server_ctx); |
| 648 |
ASSERT_TRUE(SSL_CTX_set1_ech_keys(server_ctx.get(), keys1.get())); |
| 649 |
|
| 650 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 651 |
ASSERT_TRUE(client_ctx); |
| 652 |
bssl::UniquePtr<SSL> client, server; |
| 653 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 654 |
server_ctx.get())); |
| 655 |
ASSERT_TRUE(InstallECHConfigList(client.get(), keys1.get())); |
| 656 |
|
| 657 |
// In parallel, complete the connection and reconfigure the ECHConfig. Note |
| 658 |
// |keys12| supports all the keys in |keys1|, so the handshake should complete |
| 659 |
// the same whichever the server uses. |
| 660 |
std::vector<std::thread> threads; |
| 661 |
threads.emplace_back([&] { |
| 662 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 663 |
EXPECT_TRUE(SSL_ech_accepted(client.get())); |
| 664 |
EXPECT_TRUE(SSL_ech_accepted(server.get())); |
| 665 |
}); |
| 666 |
threads.emplace_back([&] { |
| 667 |
EXPECT_TRUE(SSL_CTX_set1_ech_keys(server_ctx.get(), keys12.get())); |
| 668 |
}); |
| 669 |
for (auto &thread : threads) { |
| 670 |
thread.join(); |
| 671 |
} |
| 672 |
} |
| 673 |
#endif // OPENSSL_THREADS |
| 674 |
|
| 675 |
// GetExtensionOrder sets |*out| to the list of extensions a client attached to |
| 676 |
// |ctx| will send in the ClientHello. If |ech_keys| is non-null, the client |
| 677 |
// will offer ECH with the public component. If |decrypt_ech| is true, |*out| |
| 678 |
// will be set to the ClientHelloInner's extensions, rather than |
| 679 |
// ClientHelloOuter. |
| 680 |
static bool GetExtensionOrder(SSL_CTX *client_ctx, std::vector<uint16_t> *out, |
| 681 |
SSL_ECH_KEYS *ech_keys, bool decrypt_ech) { |
| 682 |
struct AppData { |
| 683 |
std::vector<uint16_t> *out; |
| 684 |
bool decrypt_ech; |
| 685 |
bool callback_done = false; |
| 686 |
}; |
| 687 |
AppData app_data; |
| 688 |
app_data.out = out; |
| 689 |
app_data.decrypt_ech = decrypt_ech; |
| 690 |
|
| 691 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 692 |
CreateContextWithTestCertificate(TLS_method()); |
| 693 |
if (!server_ctx || // |
| 694 |
!SSL_CTX_set_app_data(server_ctx.get(), &app_data) || |
| 695 |
(decrypt_ech && !SSL_CTX_set1_ech_keys(server_ctx.get(), ech_keys))) { |
| 696 |
return false; |
| 697 |
} |
| 698 |
|
| 699 |
// Configure the server to record the ClientHello extension order. We use a |
| 700 |
// server rather than |GetClientHello| so it can decrypt ClientHelloInner. |
| 701 |
SSL_CTX_set_select_certificate_cb( |
| 702 |
server_ctx.get(), |
| 703 |
[](const SSL_CLIENT_HELLO *client_hello) -> ssl_select_cert_result_t { |
| 704 |
AppData *app_data_ptr = static_cast<AppData *>( |
| 705 |
SSL_CTX_get_app_data(SSL_get_SSL_CTX(client_hello->ssl))); |
| 706 |
EXPECT_EQ(app_data_ptr->decrypt_ech ? 1 : 0, |
| 707 |
SSL_ech_accepted(client_hello->ssl)); |
| 708 |
|
| 709 |
app_data_ptr->out->clear(); |
| 710 |
CBS extensions; |
| 711 |
CBS_init(&extensions, client_hello->extensions, |
| 712 |
client_hello->extensions_len); |
| 713 |
while (CBS_len(&extensions)) { |
| 714 |
uint16_t type = 0; |
| 715 |
CBS body; |
| 716 |
if (!CBS_get_u16(&extensions, &type) || |
| 717 |
!CBS_get_u16_length_prefixed(&extensions, &body)) { |
| 718 |
return ssl_select_cert_error; |
| 719 |
} |
| 720 |
app_data_ptr->out->push_back(type); |
| 721 |
} |
| 722 |
|
| 723 |
// Don't bother completing the handshake. |
| 724 |
app_data_ptr->callback_done = true; |
| 725 |
return ssl_select_cert_error; |
| 726 |
}); |
| 727 |
|
| 728 |
bssl::UniquePtr<SSL> client, server; |
| 729 |
if (!CreateClientAndServer(&client, &server, client_ctx, server_ctx.get()) || |
| 730 |
(ech_keys != nullptr && !InstallECHConfigList(client.get(), ech_keys))) { |
| 731 |
return false; |
| 732 |
} |
| 733 |
|
| 734 |
// Run the handshake far enough to process the ClientHello. |
| 735 |
SSL_do_handshake(client.get()); |
| 736 |
SSL_do_handshake(server.get()); |
| 737 |
return app_data.callback_done; |
| 738 |
} |
| 739 |
|
| 740 |
// Test that, when extension permutation is enabled, the ClientHello extension |
| 741 |
// order changes, both with and without ECH, and in both ClientHelloInner and |
| 742 |
// ClientHelloOuter. |
| 743 |
TEST(SSLTest, PermuteExtensions) { |
| 744 |
bssl::UniquePtr<SSL_ECH_KEYS> keys = MakeTestECHKeys(); |
| 745 |
ASSERT_TRUE(keys); |
| 746 |
for (bool offer_ech : {false, true}) { |
| 747 |
SCOPED_TRACE(offer_ech); |
| 748 |
SSL_ECH_KEYS *maybe_keys = offer_ech ? keys.get() : nullptr; |
| 749 |
for (bool decrypt_ech : {false, true}) { |
| 750 |
SCOPED_TRACE(decrypt_ech); |
| 751 |
if (!offer_ech && decrypt_ech) { |
| 752 |
continue; |
| 753 |
} |
| 754 |
|
| 755 |
// When extension permutation is disabled, the order should be consistent. |
| 756 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 757 |
ASSERT_TRUE(ctx); |
| 758 |
std::vector<uint16_t> order1, order2; |
| 759 |
ASSERT_TRUE( |
| 760 |
GetExtensionOrder(ctx.get(), &order1, maybe_keys, decrypt_ech)); |
| 761 |
ASSERT_TRUE( |
| 762 |
GetExtensionOrder(ctx.get(), &order2, maybe_keys, decrypt_ech)); |
| 763 |
EXPECT_EQ(order1, order2); |
| 764 |
|
| 765 |
ctx.reset(SSL_CTX_new(TLS_method())); |
| 766 |
ASSERT_TRUE(ctx); |
| 767 |
SSL_CTX_set_permute_extensions(ctx.get(), 1); |
| 768 |
|
| 769 |
// When extension permutation is enabled, each ClientHello should have a |
| 770 |
// different order. |
| 771 |
// |
| 772 |
// This test is inherently flaky, so we run it multiple times. We send at |
| 773 |
// least five extensions by default from TLS 1.3: supported_versions, |
| 774 |
// key_share, supported_groups, psk_key_exchange_modes, and |
| 775 |
// signature_algorithms. That means the probability of a false negative is |
| 776 |
// at most 1/120. Repeating the test 14 times lowers false negative rate |
| 777 |
// to under 2^-96. |
| 778 |
ASSERT_TRUE( |
| 779 |
GetExtensionOrder(ctx.get(), &order1, maybe_keys, decrypt_ech)); |
| 780 |
EXPECT_GE(order1.size(), 5u); |
| 781 |
static const int kNumIterations = 14; |
| 782 |
bool passed = false; |
| 783 |
for (int i = 0; i < kNumIterations; i++) { |
| 784 |
ASSERT_TRUE( |
| 785 |
GetExtensionOrder(ctx.get(), &order2, maybe_keys, decrypt_ech)); |
| 786 |
if (order1 != order2) { |
| 787 |
passed = true; |
| 788 |
break; |
| 789 |
} |
| 790 |
} |
| 791 |
EXPECT_TRUE(passed) << "Extensions were not permuted"; |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
BSSL_NAMESPACE_END |