| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <gtest/gtest.h> |
| 5 |
#include <thread> |
| 6 |
|
| 7 |
#include "../crypto/test/file_util.h" |
| 8 |
#include "../crypto/test/test_util.h" |
| 9 |
#include "internal.h" |
| 10 |
#include "ssl_common_test.h" |
| 11 |
|
| 12 |
BSSL_NAMESPACE_BEGIN |
| 13 |
|
| 14 |
TEST(SSLTest, SelectNextProto) { |
| 15 |
uint8_t *result = nullptr; |
| 16 |
uint8_t result_len = 0; |
| 17 |
|
| 18 |
// If there is an overlap, it should be returned. |
| 19 |
EXPECT_EQ(OPENSSL_NPN_NEGOTIATED, |
| 20 |
SSL_select_next_proto(&result, &result_len, |
| 21 |
(const uint8_t *)"\1a\2bb\3ccc", 9, |
| 22 |
(const uint8_t *)"\1x\1y\1a\1z", 8)); |
| 23 |
EXPECT_EQ(Bytes("a"), Bytes(result, result_len)); |
| 24 |
|
| 25 |
EXPECT_EQ(OPENSSL_NPN_NEGOTIATED, |
| 26 |
SSL_select_next_proto(&result, &result_len, |
| 27 |
(const uint8_t *)"\1a\2bb\3ccc", 9, |
| 28 |
(const uint8_t *)"\1x\1y\2bb\1z", 9)); |
| 29 |
EXPECT_EQ(Bytes("bb"), Bytes(result, result_len)); |
| 30 |
|
| 31 |
EXPECT_EQ(OPENSSL_NPN_NEGOTIATED, |
| 32 |
SSL_select_next_proto(&result, &result_len, |
| 33 |
(const uint8_t *)"\1a\2bb\3ccc", 9, |
| 34 |
(const uint8_t *)"\1x\1y\3ccc\1z", 10)); |
| 35 |
EXPECT_EQ(Bytes("ccc"), Bytes(result, result_len)); |
| 36 |
|
| 37 |
// Peer preference order takes precedence over local. |
| 38 |
EXPECT_EQ(OPENSSL_NPN_NEGOTIATED, |
| 39 |
SSL_select_next_proto(&result, &result_len, |
| 40 |
(const uint8_t *)"\1a\2bb\3ccc", 9, |
| 41 |
(const uint8_t *)"\3ccc\2bb\1a", 9)); |
| 42 |
EXPECT_EQ(Bytes("a"), Bytes(result, result_len)); |
| 43 |
|
| 44 |
// If there is no overlap, opportunistically select the first local protocol. |
| 45 |
// ALPN callers should ignore this, but NPN callers may use this per |
| 46 |
// draft-agl-tls-nextprotoneg-03, section 6. |
| 47 |
EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 48 |
SSL_select_next_proto(&result, &result_len, |
| 49 |
(const uint8_t *)"\1a\2bb\3ccc", 9, |
| 50 |
(const uint8_t *)"\1x\2yy\3zzz", 9)); |
| 51 |
EXPECT_EQ(Bytes("x"), Bytes(result, result_len)); |
| 52 |
|
| 53 |
// The peer preference order may be empty in NPN. This should be treated as no |
| 54 |
// overlap and continue to select an opportunistic protocol. |
| 55 |
EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 56 |
SSL_select_next_proto(&result, &result_len, nullptr, 0, |
| 57 |
(const uint8_t *)"\1x\2yy\3zzz", 9)); |
| 58 |
EXPECT_EQ(Bytes("x"), Bytes(result, result_len)); |
| 59 |
|
| 60 |
// Although calling this function with no local protocols is a caller error, |
| 61 |
// it should cleanly return an empty protocol. |
| 62 |
EXPECT_EQ( |
| 63 |
OPENSSL_NPN_NO_OVERLAP, |
| 64 |
SSL_select_next_proto(&result, &result_len, |
| 65 |
(const uint8_t *)"\1a\2bb\3ccc", 9, nullptr, 0)); |
| 66 |
EXPECT_EQ(Bytes(""), Bytes(result, result_len)); |
| 67 |
|
| 68 |
// Syntax errors are similarly caller errors. |
| 69 |
EXPECT_EQ( |
| 70 |
OPENSSL_NPN_NO_OVERLAP, |
| 71 |
SSL_select_next_proto(&result, &result_len, (const uint8_t *)"\4aaa", 4, |
| 72 |
(const uint8_t *)"\1a\2bb\3ccc", 9)); |
| 73 |
EXPECT_EQ(Bytes(""), Bytes(result, result_len)); |
| 74 |
EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 75 |
SSL_select_next_proto(&result, &result_len, |
| 76 |
(const uint8_t *)"\1a\2bb\3ccc", 9, |
| 77 |
(const uint8_t *)"\4aaa", 4)); |
| 78 |
EXPECT_EQ(Bytes(""), Bytes(result, result_len)); |
| 79 |
|
| 80 |
// Protocols in protocol lists may not be empty. |
| 81 |
EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 82 |
SSL_select_next_proto(&result, &result_len, |
| 83 |
(const uint8_t *)"\0\2bb\3ccc", 8, |
| 84 |
(const uint8_t *)"\1a\2bb\3ccc", 9)); |
| 85 |
EXPECT_EQ(OPENSSL_NPN_NO_OVERLAP, |
| 86 |
SSL_select_next_proto(&result, &result_len, |
| 87 |
(const uint8_t *)"\1a\2bb\3ccc", 9, |
| 88 |
(const uint8_t *)"\0\2bb\3ccc", 8)); |
| 89 |
EXPECT_EQ(Bytes(""), Bytes(result, result_len)); |
| 90 |
} |
| 91 |
|
| 92 |
// The client should gracefully handle no suitable ciphers being enabled. |
| 93 |
TEST(SSLTest, NoCiphersAvailable) { |
| 94 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 95 |
ASSERT_TRUE(ctx); |
| 96 |
|
| 97 |
// Configure |client_ctx| with a cipher list that does not intersect with its |
| 98 |
// version configuration. |
| 99 |
ASSERT_TRUE(SSL_CTX_set_strict_cipher_list( |
| 100 |
ctx.get(), "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")); |
| 101 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_1_VERSION)); |
| 102 |
|
| 103 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 104 |
ASSERT_TRUE(ssl); |
| 105 |
SSL_set_connect_state(ssl.get()); |
| 106 |
|
| 107 |
UniquePtr<BIO> rbio(BIO_new(BIO_s_mem())), wbio(BIO_new(BIO_s_mem())); |
| 108 |
ASSERT_TRUE(rbio); |
| 109 |
ASSERT_TRUE(wbio); |
| 110 |
SSL_set0_rbio(ssl.get(), rbio.release()); |
| 111 |
SSL_set0_wbio(ssl.get(), wbio.release()); |
| 112 |
|
| 113 |
int ret = SSL_do_handshake(ssl.get()); |
| 114 |
EXPECT_EQ(-1, ret); |
| 115 |
EXPECT_EQ(SSL_ERROR_SSL, SSL_get_error(ssl.get(), ret)); |
| 116 |
EXPECT_TRUE( |
| 117 |
ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_NO_CIPHERS_AVAILABLE)); |
| 118 |
} |
| 119 |
|
| 120 |
// Test that post-handshake tickets consumed by |SSL_shutdown| are ignored. |
| 121 |
TEST(SSLTest, ShutdownIgnoresTickets) { |
| 122 |
bssl::UniquePtr<SSL_CTX> ctx(CreateContextWithTestCertificate(TLS_method())); |
| 123 |
ASSERT_TRUE(ctx); |
| 124 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_3_VERSION)); |
| 125 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_3_VERSION)); |
| 126 |
|
| 127 |
SSL_CTX_set_session_cache_mode(ctx.get(), SSL_SESS_CACHE_BOTH); |
| 128 |
|
| 129 |
bssl::UniquePtr<SSL> client, server; |
| 130 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 131 |
|
| 132 |
SSL_CTX_sess_set_new_cb(ctx.get(), [](SSL *ssl, SSL_SESSION *session) -> int { |
| 133 |
ADD_FAILURE() << "New session callback called during SSL_shutdown"; |
| 134 |
return 0; |
| 135 |
}); |
| 136 |
|
| 137 |
// Send close_notify. |
| 138 |
EXPECT_EQ(0, SSL_shutdown(server.get())); |
| 139 |
EXPECT_EQ(0, SSL_shutdown(client.get())); |
| 140 |
|
| 141 |
// Receive close_notify. |
| 142 |
EXPECT_EQ(1, SSL_shutdown(server.get())); |
| 143 |
EXPECT_EQ(1, SSL_shutdown(client.get())); |
| 144 |
} |
| 145 |
|
| 146 |
TEST(SSLTest, SignatureAlgorithmProperties) { |
| 147 |
EXPECT_EQ(EVP_PKEY_NONE, SSL_get_signature_algorithm_key_type(0x1234)); |
| 148 |
EXPECT_EQ(nullptr, SSL_get_signature_algorithm_digest(0x1234)); |
| 149 |
EXPECT_FALSE(SSL_is_signature_algorithm_rsa_pss(0x1234)); |
| 150 |
|
| 151 |
EXPECT_EQ(EVP_PKEY_RSA, |
| 152 |
SSL_get_signature_algorithm_key_type(SSL_SIGN_RSA_PKCS1_MD5_SHA1)); |
| 153 |
EXPECT_EQ(EVP_md5_sha1(), |
| 154 |
SSL_get_signature_algorithm_digest(SSL_SIGN_RSA_PKCS1_MD5_SHA1)); |
| 155 |
EXPECT_FALSE(SSL_is_signature_algorithm_rsa_pss(SSL_SIGN_RSA_PKCS1_MD5_SHA1)); |
| 156 |
|
| 157 |
EXPECT_EQ(EVP_PKEY_EC, SSL_get_signature_algorithm_key_type( |
| 158 |
SSL_SIGN_ECDSA_SECP256R1_SHA256)); |
| 159 |
EXPECT_EQ(EVP_sha256(), SSL_get_signature_algorithm_digest( |
| 160 |
SSL_SIGN_ECDSA_SECP256R1_SHA256)); |
| 161 |
EXPECT_FALSE( |
| 162 |
SSL_is_signature_algorithm_rsa_pss(SSL_SIGN_ECDSA_SECP256R1_SHA256)); |
| 163 |
|
| 164 |
EXPECT_EQ(EVP_PKEY_RSA, |
| 165 |
SSL_get_signature_algorithm_key_type(SSL_SIGN_RSA_PSS_RSAE_SHA384)); |
| 166 |
EXPECT_EQ(EVP_sha384(), |
| 167 |
SSL_get_signature_algorithm_digest(SSL_SIGN_RSA_PSS_RSAE_SHA384)); |
| 168 |
EXPECT_TRUE(SSL_is_signature_algorithm_rsa_pss(SSL_SIGN_RSA_PSS_RSAE_SHA384)); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
TEST(SSLTest, HandoffDeclined) { |
| 174 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 175 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 176 |
CreateContextWithTestCertificate(TLS_method())); |
| 177 |
ASSERT_TRUE(client_ctx); |
| 178 |
ASSERT_TRUE(server_ctx); |
| 179 |
|
| 180 |
SSL_CTX_set_handoff_mode(server_ctx.get(), true); |
| 181 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_2_VERSION)); |
| 182 |
|
| 183 |
bssl::UniquePtr<SSL> client, server; |
| 184 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 185 |
server_ctx.get())); |
| 186 |
|
| 187 |
int client_ret = SSL_do_handshake(client.get()); |
| 188 |
int client_err = SSL_get_error(client.get(), client_ret); |
| 189 |
ASSERT_EQ(client_err, SSL_ERROR_WANT_READ); |
| 190 |
|
| 191 |
int server_ret = SSL_do_handshake(server.get()); |
| 192 |
int server_err = SSL_get_error(server.get(), server_ret); |
| 193 |
ASSERT_EQ(server_err, SSL_ERROR_HANDOFF); |
| 194 |
|
| 195 |
ScopedCBB cbb; |
| 196 |
SSL_CLIENT_HELLO hello; |
| 197 |
ASSERT_TRUE(CBB_init(cbb.get(), 256)); |
| 198 |
ASSERT_TRUE(SSL_serialize_handoff(server.get(), cbb.get(), &hello)); |
| 199 |
|
| 200 |
ASSERT_TRUE(SSL_decline_handoff(server.get())); |
| 201 |
|
| 202 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 203 |
|
| 204 |
uint8_t byte = 42; |
| 205 |
EXPECT_EQ(SSL_write(client.get(), &byte, 1), 1); |
| 206 |
EXPECT_EQ(SSL_read(server.get(), &byte, 1), 1); |
| 207 |
EXPECT_EQ(42, byte); |
| 208 |
|
| 209 |
byte = 43; |
| 210 |
EXPECT_EQ(SSL_write(server.get(), &byte, 1), 1); |
| 211 |
EXPECT_EQ(SSL_read(client.get(), &byte, 1), 1); |
| 212 |
EXPECT_EQ(43, byte); |
| 213 |
} |
| 214 |
|
| 215 |
static std::string SigAlgsToString(Span<const uint16_t> sigalgs) { |
| 216 |
std::string ret = "{"; |
| 217 |
|
| 218 |
for (uint16_t v : sigalgs) { |
| 219 |
if (ret.size() > 1) { |
| 220 |
ret += ", "; |
| 221 |
} |
| 222 |
|
| 223 |
char buf[8]; |
| 224 |
snprintf(buf, sizeof(buf) - 1, "0x%02x", v); |
| 225 |
buf[sizeof(buf) - 1] = 0; |
| 226 |
ret += std::string(buf); |
| 227 |
} |
| 228 |
|
| 229 |
ret += "}"; |
| 230 |
return ret; |
| 231 |
} |
| 232 |
|
| 233 |
static void ExpectSigAlgsEqual(Span<const uint16_t> expected, |
| 234 |
Span<const uint16_t> actual) { |
| 235 |
bool matches = false; |
| 236 |
if (expected.size() == actual.size()) { |
| 237 |
matches = true; |
| 238 |
|
| 239 |
for (size_t i = 0; i < expected.size(); i++) { |
| 240 |
if (expected[i] != actual[i]) { |
| 241 |
matches = false; |
| 242 |
break; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if (!matches) { |
| 248 |
ADD_FAILURE() << "expected: " << SigAlgsToString(expected) |
| 249 |
<< " got: " << SigAlgsToString(actual); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
TEST(SSLTest, SigAlgs) { |
| 254 |
static const struct { |
| 255 |
std::vector<int> input; |
| 256 |
bool ok; |
| 257 |
std::vector<uint16_t> expected; |
| 258 |
} kTests[] = { |
| 259 |
{{}, true, {}}, |
| 260 |
{{1}, false, {}}, |
| 261 |
{{1, 2, 3}, false, {}}, |
| 262 |
{{NID_sha256, EVP_PKEY_ED25519}, false, {}}, |
| 263 |
{{NID_sha256, EVP_PKEY_RSA, NID_sha256, EVP_PKEY_RSA}, false, {}}, |
| 264 |
|
| 265 |
{{NID_sha256, EVP_PKEY_RSA}, true, {SSL_SIGN_RSA_PKCS1_SHA256}}, |
| 266 |
{{NID_sha512, EVP_PKEY_RSA}, true, {SSL_SIGN_RSA_PKCS1_SHA512}}, |
| 267 |
{{NID_sha256, EVP_PKEY_RSA_PSS}, true, {SSL_SIGN_RSA_PSS_RSAE_SHA256}}, |
| 268 |
{{NID_undef, EVP_PKEY_ED25519}, true, {SSL_SIGN_ED25519}}, |
| 269 |
{{NID_undef, EVP_PKEY_ED25519, NID_sha384, EVP_PKEY_EC}, |
| 270 |
true, |
| 271 |
{SSL_SIGN_ED25519, SSL_SIGN_ECDSA_SECP384R1_SHA384}}, |
| 272 |
}; |
| 273 |
|
| 274 |
UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 275 |
ASSERT_TRUE(ctx); |
| 276 |
|
| 277 |
unsigned n = 1; |
| 278 |
for (const auto &test : kTests) { |
| 279 |
SCOPED_TRACE(n++); |
| 280 |
|
| 281 |
const bool ok = |
| 282 |
SSL_CTX_set1_sigalgs(ctx.get(), test.input.data(), test.input.size()); |
| 283 |
EXPECT_EQ(ok, test.ok); |
| 284 |
|
| 285 |
if (!ok) { |
| 286 |
ERR_clear_error(); |
| 287 |
} |
| 288 |
|
| 289 |
if (!test.ok) { |
| 290 |
continue; |
| 291 |
} |
| 292 |
|
| 293 |
ExpectSigAlgsEqual(test.expected, ctx->cert->sigalgs); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
TEST(SSLTest, SigAlgsList) { |
| 298 |
static const struct { |
| 299 |
const char *input; |
| 300 |
bool ok; |
| 301 |
std::vector<uint16_t> expected; |
| 302 |
} kTests[] = { |
| 303 |
{"", false, {}}, |
| 304 |
{":", false, {}}, |
| 305 |
{"+", false, {}}, |
| 306 |
{"RSA", false, {}}, |
| 307 |
{"RSA+", false, {}}, |
| 308 |
{"RSA+SHA256:", false, {}}, |
| 309 |
{":RSA+SHA256:", false, {}}, |
| 310 |
{":RSA+SHA256+:", false, {}}, |
| 311 |
{"!", false, {}}, |
| 312 |
{"\x01", false, {}}, |
| 313 |
{"RSA+SHA256:RSA+SHA384:RSA+SHA256", false, {}}, |
| 314 |
{"RSA-PSS+SHA256:rsa_pss_rsae_sha256", false, {}}, |
| 315 |
|
| 316 |
{"RSA+SHA256", true, {SSL_SIGN_RSA_PKCS1_SHA256}}, |
| 317 |
{"RSA+SHA256:ed25519", |
| 318 |
true, |
| 319 |
{SSL_SIGN_RSA_PKCS1_SHA256, SSL_SIGN_ED25519}}, |
| 320 |
{"ECDSA+SHA256:RSA+SHA512", |
| 321 |
true, |
| 322 |
{SSL_SIGN_ECDSA_SECP256R1_SHA256, SSL_SIGN_RSA_PKCS1_SHA512}}, |
| 323 |
{"ecdsa_secp256r1_sha256:rsa_pss_rsae_sha256", |
| 324 |
true, |
| 325 |
{SSL_SIGN_ECDSA_SECP256R1_SHA256, SSL_SIGN_RSA_PSS_RSAE_SHA256}}, |
| 326 |
{"RSA-PSS+SHA256", true, {SSL_SIGN_RSA_PSS_RSAE_SHA256}}, |
| 327 |
{"PSS+SHA256", true, {SSL_SIGN_RSA_PSS_RSAE_SHA256}}, |
| 328 |
}; |
| 329 |
|
| 330 |
UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 331 |
ASSERT_TRUE(ctx); |
| 332 |
|
| 333 |
unsigned n = 1; |
| 334 |
for (const auto &test : kTests) { |
| 335 |
SCOPED_TRACE(n++); |
| 336 |
|
| 337 |
const bool ok = SSL_CTX_set1_sigalgs_list(ctx.get(), test.input); |
| 338 |
EXPECT_EQ(ok, test.ok); |
| 339 |
|
| 340 |
if (!ok) { |
| 341 |
if (test.ok) { |
| 342 |
ERR_print_errors_fp(stderr); |
| 343 |
} |
| 344 |
ERR_clear_error(); |
| 345 |
} |
| 346 |
|
| 347 |
if (!test.ok) { |
| 348 |
continue; |
| 349 |
} |
| 350 |
|
| 351 |
ExpectSigAlgsEqual(test.expected, ctx->cert->sigalgs); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
TEST(SSLTest, ZeroSizedWriteFlushesHandshakeMessages) { |
| 356 |
// If there are pending handshake messages, an |SSL_write| of zero bytes |
| 357 |
// should flush them. |
| 358 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 359 |
CreateContextWithTestCertificate(TLS_method())); |
| 360 |
ASSERT_TRUE(server_ctx); |
| 361 |
EXPECT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 362 |
EXPECT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 363 |
|
| 364 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 365 |
ASSERT_TRUE(client_ctx); |
| 366 |
EXPECT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 367 |
EXPECT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 368 |
|
| 369 |
bssl::UniquePtr<SSL> client, server; |
| 370 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 371 |
server_ctx.get())); |
| 372 |
|
| 373 |
BIO *client_wbio = SSL_get_wbio(client.get()); |
| 374 |
EXPECT_EQ(0u, BIO_wpending(client_wbio)); |
| 375 |
EXPECT_TRUE(SSL_key_update(client.get(), SSL_KEY_UPDATE_NOT_REQUESTED)); |
| 376 |
EXPECT_EQ(0u, BIO_wpending(client_wbio)); |
| 377 |
EXPECT_EQ(0, SSL_write(client.get(), nullptr, 0)); |
| 378 |
EXPECT_NE(0u, BIO_wpending(client_wbio)); |
| 379 |
} |
| 380 |
|
| 381 |
TEST(SSLTest, SSLGetKeyUpdate) { |
| 382 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 383 |
CreateContextWithTestCertificate(TLS_method())); |
| 384 |
ASSERT_TRUE(server_ctx); |
| 385 |
EXPECT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 386 |
EXPECT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 387 |
|
| 388 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 389 |
ASSERT_TRUE(client_ctx); |
| 390 |
EXPECT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 391 |
EXPECT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 392 |
|
| 393 |
bssl::UniquePtr<SSL> client, server; |
| 394 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 395 |
server_ctx.get())); |
| 396 |
|
| 397 |
// Initial state should be |SSL_KEY_UPDATE_NONE|. |
| 398 |
EXPECT_EQ(SSL_get_key_update_type(client.get()), SSL_KEY_UPDATE_NONE); |
| 399 |
|
| 400 |
// Test setting |SSL_key_update| with |SSL_KEY_UPDATE_REQUESTED|. |
| 401 |
EXPECT_TRUE(SSL_key_update(client.get(), SSL_KEY_UPDATE_REQUESTED)); |
| 402 |
// |SSL_get_key_update_type| is used to determine whether a key update |
| 403 |
// operation has been scheduled but not yet performed. |
| 404 |
EXPECT_EQ(SSL_get_key_update_type(client.get()), SSL_KEY_UPDATE_REQUESTED); |
| 405 |
EXPECT_EQ(0, SSL_write(client.get(), nullptr, 0)); |
| 406 |
// Key update operation should have been performed by now. |
| 407 |
EXPECT_EQ(SSL_get_key_update_type(client.get()), SSL_KEY_UPDATE_NONE); |
| 408 |
|
| 409 |
// Test setting |SSL_key_update| with |SSL_KEY_UPDATE_NOT_REQUESTED|. |
| 410 |
EXPECT_TRUE(SSL_key_update(client.get(), SSL_KEY_UPDATE_NOT_REQUESTED)); |
| 411 |
EXPECT_EQ(SSL_get_key_update_type(client.get()), |
| 412 |
SSL_KEY_UPDATE_NOT_REQUESTED); |
| 413 |
EXPECT_EQ(0, SSL_write(client.get(), nullptr, 0)); |
| 414 |
// Key update operation should have been performed by now. |
| 415 |
EXPECT_EQ(SSL_get_key_update_type(client.get()), SSL_KEY_UPDATE_NONE); |
| 416 |
} |
| 417 |
#if defined(OPENSSL_THREADS) |
| 418 |
// SSL_CTX_get0_certificate needs to lock internally. Test this works. |
| 419 |
TEST(SSLTest, GetCertificateThreads) { |
| 420 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 421 |
ASSERT_TRUE(ctx); |
| 422 |
bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 423 |
ASSERT_TRUE(cert); |
| 424 |
ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 425 |
|
| 426 |
// Existing code expects |SSL_CTX_get0_certificate| to be callable from two |
| 427 |
// threads concurrently. It originally was an immutable operation. Now we |
| 428 |
// implement it with a thread-safe cache, so it is worth testing. |
| 429 |
X509 *cert2_thread = nullptr; |
| 430 |
std::thread thread( |
| 431 |
[&] { cert2_thread = SSL_CTX_get0_certificate(ctx.get()); }); |
| 432 |
X509 *cert2 = SSL_CTX_get0_certificate(ctx.get()); |
| 433 |
thread.join(); |
| 434 |
|
| 435 |
ASSERT_TRUE(cert2); |
| 436 |
ASSERT_TRUE(cert2_thread); |
| 437 |
EXPECT_EQ(cert2, cert2_thread); |
| 438 |
EXPECT_EQ(0, X509_cmp(cert.get(), cert2)); |
| 439 |
} |
| 440 |
#endif // OPENSSL_THREADS |
| 441 |
|
| 442 |
#ifdef OPENSSL_THREADS |
| 443 |
|
| 444 |
static void SetValueOnFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 445 |
int index, long argl, void *argp) { |
| 446 |
if (ptr != nullptr) { |
| 447 |
*static_cast<long *>(ptr) = argl; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
// Test that one thread can register ex_data while another thread is destroying |
| 452 |
// an object that uses it. |
| 453 |
TEST(SSLTest, ExDataThreads) { |
| 454 |
static bool already_run = false; |
| 455 |
if (already_run) { |
| 456 |
GTEST_SKIP() << "This test consumes process-global resources and can only " |
| 457 |
"be run once in a process. It is not compatible with " |
| 458 |
"--gtest_repeat."; |
| 459 |
} |
| 460 |
already_run = true; |
| 461 |
|
| 462 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 463 |
ASSERT_TRUE(ctx); |
| 464 |
|
| 465 |
// Register an initial index, so the threads can exercise having any ex_data. |
| 466 |
int first_index = |
| 467 |
SSL_get_ex_new_index(-1, nullptr, nullptr, nullptr, SetValueOnFree); |
| 468 |
ASSERT_GE(first_index, 0); |
| 469 |
|
| 470 |
// Callers may register indices concurrently with using other indices. This |
| 471 |
// may happen if one part of an application is initializing while another part |
| 472 |
// is already running. |
| 473 |
static constexpr int kNumIndices = 3; |
| 474 |
static constexpr int kNumSSLs = 10; |
| 475 |
int index[kNumIndices]; |
| 476 |
long values[kNumSSLs]; |
| 477 |
std::fill(std::begin(values), std::end(values), -2); |
| 478 |
std::vector<std::thread> threads; |
| 479 |
for (size_t i = 0; i < kNumIndices; i++) { |
| 480 |
threads.emplace_back([&, i] { |
| 481 |
index[i] = SSL_get_ex_new_index(static_cast<long>(i), nullptr, nullptr, |
| 482 |
nullptr, SetValueOnFree); |
| 483 |
ASSERT_GE(index[i], 0); |
| 484 |
}); |
| 485 |
} |
| 486 |
for (size_t i = 0; i < kNumSSLs; i++) { |
| 487 |
threads.emplace_back([&, i] { |
| 488 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 489 |
ASSERT_TRUE(ssl); |
| 490 |
ASSERT_TRUE(SSL_set_ex_data(ssl.get(), first_index, &values[i])); |
| 491 |
}); |
| 492 |
} |
| 493 |
for (auto &thread : threads) { |
| 494 |
thread.join(); |
| 495 |
} |
| 496 |
|
| 497 |
// Each of the SSL threads should have set their flag via ex_data. |
| 498 |
for (size_t i = 0; i < kNumSSLs; i++) { |
| 499 |
EXPECT_EQ(values[i], -1); |
| 500 |
} |
| 501 |
|
| 502 |
// Each of the newly-registered indices should be distinct and work correctly. |
| 503 |
static_assert(kNumIndices <= kNumSSLs, "values buffer too small"); |
| 504 |
std::fill(std::begin(values), std::end(values), -2); |
| 505 |
bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 506 |
ASSERT_TRUE(ssl); |
| 507 |
for (size_t i = 0; i < kNumIndices; i++) { |
| 508 |
for (size_t j = 0; j < i; j++) { |
| 509 |
EXPECT_NE(index[i], index[j]); |
| 510 |
} |
| 511 |
ASSERT_TRUE(SSL_set_ex_data(ssl.get(), index[i], &values[i])); |
| 512 |
} |
| 513 |
ssl = nullptr; |
| 514 |
for (size_t i = 0; i < kNumIndices; i++) { |
| 515 |
EXPECT_EQ(values[i], static_cast<long>(i)); |
| 516 |
} |
| 517 |
} |
| 518 |
#endif // OPENSSL_THREADS |
| 519 |
|
| 520 |
extern "C" { |
| 521 |
int BORINGSSL_enum_c_type_test(void); |
| 522 |
} |
| 523 |
|
| 524 |
TEST(SSLTest, EnumTypes) { |
| 525 |
EXPECT_EQ(sizeof(int), sizeof(ssl_private_key_result_t)); |
| 526 |
EXPECT_EQ(1, BORINGSSL_enum_c_type_test()); |
| 527 |
} |
| 528 |
|
| 529 |
static void WriteHelloRequest(SSL *server) { |
| 530 |
// This function assumes TLS 1.2 with ChaCha20-Poly1305. |
| 531 |
ASSERT_EQ(SSL_version(server), TLS1_2_VERSION); |
| 532 |
ASSERT_EQ(SSL_CIPHER_get_cipher_nid(SSL_get_current_cipher(server)), |
| 533 |
NID_chacha20_poly1305); |
| 534 |
|
| 535 |
// Encrypt a HelloRequest. |
| 536 |
uint8_t in[] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0}; |
| 537 |
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 538 |
// Fuzzer-mode records are unencrypted. |
| 539 |
uint8_t record[5 + sizeof(in)]; |
| 540 |
record[0] = SSL3_RT_HANDSHAKE; |
| 541 |
record[1] = 3; |
| 542 |
record[2] = 3; // TLS 1.2 |
| 543 |
record[3] = 0; |
| 544 |
record[4] = sizeof(record) - 5; |
| 545 |
memcpy(record + 5, in, sizeof(in)); |
| 546 |
#else |
| 547 |
// Extract key material from |server|. |
| 548 |
static const size_t kKeyLen = 32; |
| 549 |
static const size_t kNonceLen = 12; |
| 550 |
ASSERT_EQ(2u * (kKeyLen + kNonceLen), SSL_get_key_block_len(server)); |
| 551 |
uint8_t key_block[2u * (kKeyLen + kNonceLen)]; |
| 552 |
ASSERT_TRUE(SSL_generate_key_block(server, key_block, sizeof(key_block))); |
| 553 |
Span<uint8_t> key = MakeSpan(key_block + kKeyLen, kKeyLen); |
| 554 |
Span<uint8_t> nonce = |
| 555 |
MakeSpan(key_block + kKeyLen + kKeyLen + kNonceLen, kNonceLen); |
| 556 |
|
| 557 |
uint8_t ad[13]; |
| 558 |
uint64_t seq = SSL_get_write_sequence(server); |
| 559 |
for (size_t i = 0; i < 8; i++) { |
| 560 |
// The nonce is XORed with the sequence number. |
| 561 |
nonce[11 - i] ^= uint8_t(seq); |
| 562 |
ad[7 - i] = uint8_t(seq); |
| 563 |
seq >>= 8; |
| 564 |
} |
| 565 |
|
| 566 |
ad[8] = SSL3_RT_HANDSHAKE; |
| 567 |
ad[9] = 3; |
| 568 |
ad[10] = 3; // TLS 1.2 |
| 569 |
ad[11] = 0; |
| 570 |
ad[12] = sizeof(in); |
| 571 |
|
| 572 |
uint8_t record[5 + sizeof(in) + 16]; |
| 573 |
record[0] = SSL3_RT_HANDSHAKE; |
| 574 |
record[1] = 3; |
| 575 |
record[2] = 3; // TLS 1.2 |
| 576 |
record[3] = 0; |
| 577 |
record[4] = sizeof(record) - 5; |
| 578 |
|
| 579 |
ScopedEVP_AEAD_CTX aead; |
| 580 |
ASSERT_TRUE(EVP_AEAD_CTX_init(aead.get(), EVP_aead_chacha20_poly1305(), |
| 581 |
key.data(), key.size(), |
| 582 |
EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)); |
| 583 |
size_t len = 0; |
| 584 |
ASSERT_TRUE(EVP_AEAD_CTX_seal(aead.get(), record + 5, &len, |
| 585 |
sizeof(record) - 5, nonce.data(), nonce.size(), |
| 586 |
in, sizeof(in), ad, sizeof(ad))); |
| 587 |
ASSERT_EQ(sizeof(record) - 5, len); |
| 588 |
#endif // BORINGSSL_UNSAFE_FUZZER_MODE |
| 589 |
|
| 590 |
ASSERT_EQ(int(sizeof(record)), |
| 591 |
BIO_write(SSL_get_wbio(server), record, sizeof(record))); |
| 592 |
} |
| 593 |
|
| 594 |
TEST_P(SSLTest, WriteWhileExplicitRenegotiate) { |
| 595 |
bssl::UniquePtr<SSL_CTX> ctx(CreateContextWithTestCertificate(TLS_method())); |
| 596 |
ASSERT_TRUE(ctx); |
| 597 |
|
| 598 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_2_VERSION)); |
| 599 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_2_VERSION)); |
| 600 |
ASSERT_TRUE(SSL_CTX_set_strict_cipher_list( |
| 601 |
ctx.get(), "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256")); |
| 602 |
|
| 603 |
bssl::UniquePtr<SSL> client, server; |
| 604 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 605 |
SSL_set_renegotiate_mode(client.get(), ssl_renegotiate_explicit); |
| 606 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 607 |
|
| 608 |
if (GetParam().transfer_ssl) { |
| 609 |
// |server| is reset to hold the transferred SSL. |
| 610 |
TransferSSL(&server, ctx.get(), nullptr); |
| 611 |
} |
| 612 |
|
| 613 |
static const uint8_t kInput[] = {'h', 'e', 'l', 'l', 'o'}; |
| 614 |
|
| 615 |
// Write "hello" until the buffer is full, so |client| has a pending write. |
| 616 |
size_t num_writes = 0; |
| 617 |
for (;;) { |
| 618 |
int ret = SSL_write(client.get(), kInput, sizeof(kInput)); |
| 619 |
if (ret != int(sizeof(kInput))) { |
| 620 |
ASSERT_EQ(-1, ret); |
| 621 |
ASSERT_EQ(SSL_ERROR_WANT_WRITE, SSL_get_error(client.get(), ret)); |
| 622 |
break; |
| 623 |
} |
| 624 |
num_writes++; |
| 625 |
} |
| 626 |
|
| 627 |
ASSERT_NO_FATAL_FAILURE(WriteHelloRequest(server.get())); |
| 628 |
|
| 629 |
// |SSL_read| should pick up the HelloRequest. |
| 630 |
uint8_t byte = 0; |
| 631 |
ASSERT_EQ(-1, SSL_read(client.get(), &byte, 1)); |
| 632 |
ASSERT_EQ(SSL_ERROR_WANT_RENEGOTIATE, SSL_get_error(client.get(), -1)); |
| 633 |
|
| 634 |
// Drain the data from the |client|. |
| 635 |
uint8_t buf[sizeof(kInput)]; |
| 636 |
for (size_t i = 0; i < num_writes; i++) { |
| 637 |
ASSERT_EQ(int(sizeof(buf)), SSL_read(server.get(), buf, sizeof(buf))); |
| 638 |
EXPECT_EQ(Bytes(buf), Bytes(kInput)); |
| 639 |
} |
| 640 |
|
| 641 |
// |client| should be able to finish the pending write and continue to write, |
| 642 |
// despite the paused HelloRequest. |
| 643 |
ASSERT_EQ(int(sizeof(kInput)), |
| 644 |
SSL_write(client.get(), kInput, sizeof(kInput))); |
| 645 |
ASSERT_EQ(int(sizeof(buf)), SSL_read(server.get(), buf, sizeof(buf))); |
| 646 |
EXPECT_EQ(Bytes(buf), Bytes(kInput)); |
| 647 |
|
| 648 |
ASSERT_EQ(int(sizeof(kInput)), |
| 649 |
SSL_write(client.get(), kInput, sizeof(kInput))); |
| 650 |
ASSERT_EQ(int(sizeof(buf)), SSL_read(server.get(), buf, sizeof(buf))); |
| 651 |
EXPECT_EQ(Bytes(buf), Bytes(kInput)); |
| 652 |
|
| 653 |
// |SSL_read| is stuck until we acknowledge the HelloRequest. |
| 654 |
ASSERT_EQ(-1, SSL_read(client.get(), &byte, 1)); |
| 655 |
ASSERT_EQ(SSL_ERROR_WANT_RENEGOTIATE, SSL_get_error(client.get(), -1)); |
| 656 |
|
| 657 |
ASSERT_TRUE(SSL_renegotiate(client.get())); |
| 658 |
ASSERT_EQ(-1, SSL_read(client.get(), &byte, 1)); |
| 659 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client.get(), -1)); |
| 660 |
|
| 661 |
// We never renegotiate as a server. |
| 662 |
ASSERT_EQ(-1, SSL_read(server.get(), buf, sizeof(buf))); |
| 663 |
ASSERT_EQ(SSL_ERROR_SSL, SSL_get_error(server.get(), -1)); |
| 664 |
EXPECT_TRUE( |
| 665 |
ErrorEquals(ERR_get_error(), ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION)); |
| 666 |
|
| 667 |
EXPECT_EQ(SSL_CTX_sess_connect_renegotiate(ctx.get()), 1); |
| 668 |
EXPECT_EQ(SSL_CTX_sess_accept_renegotiate(ctx.get()), 0); |
| 669 |
} |
| 670 |
|
| 671 |
TEST(SSLTest, ConnectionPropertiesDuringRenegotiate) { |
| 672 |
// Configure known connection properties, so we can check against them. |
| 673 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 674 |
ASSERT_TRUE(ctx); |
| 675 |
bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 676 |
ASSERT_TRUE(cert); |
| 677 |
bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 678 |
ASSERT_TRUE(key); |
| 679 |
ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 680 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 681 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_2_VERSION)); |
| 682 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_2_VERSION)); |
| 683 |
ASSERT_TRUE(SSL_CTX_set_strict_cipher_list( |
| 684 |
ctx.get(), "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256")); |
| 685 |
ASSERT_TRUE(SSL_CTX_set1_groups_list(ctx.get(), "X25519")); |
| 686 |
ASSERT_TRUE(SSL_CTX_set1_sigalgs_list(ctx.get(), "rsa_pkcs1_sha256")); |
| 687 |
|
| 688 |
// Connect a client and server that accept renegotiation. |
| 689 |
bssl::UniquePtr<SSL> client, server; |
| 690 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 691 |
SSL_set_renegotiate_mode(client.get(), ssl_renegotiate_freely); |
| 692 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 693 |
|
| 694 |
auto check_properties = [&] { |
| 695 |
EXPECT_EQ(SSL_version(client.get()), TLS1_2_VERSION); |
| 696 |
const SSL_CIPHER *cipher = SSL_get_current_cipher(client.get()); |
| 697 |
ASSERT_TRUE(cipher); |
| 698 |
EXPECT_EQ(SSL_CIPHER_get_id(cipher), |
| 699 |
uint32_t{TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256}); |
| 700 |
EXPECT_EQ(SSL_get_group_id(client.get()), SSL_GROUP_X25519); |
| 701 |
EXPECT_EQ(SSL_get_negotiated_group(client.get()), NID_X25519); |
| 702 |
EXPECT_EQ(SSL_get_peer_signature_algorithm(client.get()), |
| 703 |
SSL_SIGN_RSA_PKCS1_SHA256); |
| 704 |
|
| 705 |
int psig_nid = 0; |
| 706 |
EXPECT_TRUE(SSL_get_peer_signature_type_nid(client.get(), &psig_nid)); |
| 707 |
EXPECT_EQ(psig_nid, EVP_PKEY_RSA); |
| 708 |
int digest_nid = 0; |
| 709 |
EXPECT_TRUE(SSL_get_peer_signature_nid(client.get(), &digest_nid)); |
| 710 |
EXPECT_EQ(digest_nid, NID_sha256); |
| 711 |
|
| 712 |
bssl::UniquePtr<X509> peer(SSL_get_peer_certificate(client.get())); |
| 713 |
ASSERT_TRUE(peer); |
| 714 |
EXPECT_EQ(X509_cmp(cert.get(), peer.get()), 0); |
| 715 |
}; |
| 716 |
check_properties(); |
| 717 |
|
| 718 |
// Client has not signed any TLS messages yet |
| 719 |
EXPECT_FALSE(SSL_get_peer_signature_type_nid(server.get(), nullptr)); |
| 720 |
EXPECT_FALSE(SSL_get_peer_signature_nid(server.get(), nullptr)); |
| 721 |
|
| 722 |
// The server sends a HelloRequest. |
| 723 |
ASSERT_NO_FATAL_FAILURE(WriteHelloRequest(server.get())); |
| 724 |
|
| 725 |
// Reading from the client will consume the HelloRequest, start a |
| 726 |
// renegotiation, and then block on a ServerHello from the server. |
| 727 |
uint8_t byte = 0; |
| 728 |
ASSERT_EQ(-1, SSL_read(client.get(), &byte, 1)); |
| 729 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client.get(), -1)); |
| 730 |
|
| 731 |
// Connection properties should continue to report values from the original |
| 732 |
// handshake. |
| 733 |
check_properties(); |
| 734 |
EXPECT_EQ(SSL_CTX_sess_connect_renegotiate(ctx.get()), 1); |
| 735 |
EXPECT_EQ(SSL_CTX_sess_accept_renegotiate(ctx.get()), 0); |
| 736 |
|
| 737 |
// Client does not sign any messages in renegotiation either |
| 738 |
EXPECT_FALSE(SSL_get_peer_signature_type_nid(server.get(), nullptr)); |
| 739 |
EXPECT_FALSE(SSL_get_peer_signature_nid(server.get(), nullptr)); |
| 740 |
} |
| 741 |
|
| 742 |
TEST(SSLTest, SSLGetSignatureData) { |
| 743 |
bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 744 |
ASSERT_TRUE(ctx); |
| 745 |
bssl::UniquePtr<X509> cert = GetECDSATestCertificate(); |
| 746 |
ASSERT_TRUE(cert); |
| 747 |
bssl::UniquePtr<EVP_PKEY> key = GetECDSATestKey(); |
| 748 |
ASSERT_TRUE(key); |
| 749 |
ASSERT_TRUE(SSL_CTX_use_certificate(ctx.get(), cert.get())); |
| 750 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(ctx.get(), key.get())); |
| 751 |
|
| 752 |
// Explicitly configure |SSL_VERIFY_PEER| so both the client and server |
| 753 |
// verify each other |
| 754 |
SSL_CTX_set_custom_verify( |
| 755 |
ctx.get(), SSL_VERIFY_PEER, |
| 756 |
[](SSL *ssl, uint8_t *out_alert) { return ssl_verify_ok; }); |
| 757 |
|
| 758 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_3_VERSION)); |
| 759 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_3_VERSION)); |
| 760 |
ASSERT_TRUE(SSL_CTX_set1_sigalgs_list(ctx.get(), "ECDSA+SHA256")); |
| 761 |
|
| 762 |
bssl::UniquePtr<SSL> client, server; |
| 763 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, ctx.get(), ctx.get())); |
| 764 |
|
| 765 |
// Before handshake, neither client nor server has signed any messages |
| 766 |
ASSERT_FALSE(SSL_get_peer_signature_nid(client.get(), nullptr)); |
| 767 |
ASSERT_FALSE(SSL_get_peer_signature_nid(server.get(), nullptr)); |
| 768 |
ASSERT_FALSE(SSL_get_peer_signature_type_nid(client.get(), nullptr)); |
| 769 |
ASSERT_FALSE(SSL_get_peer_signature_type_nid(server.get(), nullptr)); |
| 770 |
|
| 771 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 772 |
|
| 773 |
// Both client and server verified each other, both have signed TLS messages |
| 774 |
// now |
| 775 |
int client_digest = 0, client_sigtype = 0; |
| 776 |
ASSERT_TRUE(SSL_get_peer_signature_nid(server.get(), &client_digest)); |
| 777 |
ASSERT_TRUE(SSL_get_peer_signature_type_nid(server.get(), &client_sigtype)); |
| 778 |
ASSERT_EQ(client_sigtype, EVP_PKEY_EC); |
| 779 |
ASSERT_EQ(client_digest, NID_sha256); |
| 780 |
|
| 781 |
int server_digest = 0, server_sigtype = 0; |
| 782 |
ASSERT_TRUE(SSL_get_peer_signature_nid(client.get(), &server_digest)); |
| 783 |
ASSERT_TRUE(SSL_get_peer_signature_type_nid(client.get(), &server_sigtype)); |
| 784 |
ASSERT_EQ(server_sigtype, EVP_PKEY_EC); |
| 785 |
ASSERT_EQ(server_digest, NID_sha256); |
| 786 |
} |
| 787 |
|
| 788 |
TEST(SSLTest, CopyWithoutEarlyData) { |
| 789 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 790 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 791 |
CreateContextWithTestCertificate(TLS_method())); |
| 792 |
ASSERT_TRUE(client_ctx); |
| 793 |
ASSERT_TRUE(server_ctx); |
| 794 |
|
| 795 |
SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 796 |
SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 797 |
SSL_CTX_set_early_data_enabled(client_ctx.get(), 1); |
| 798 |
SSL_CTX_set_early_data_enabled(server_ctx.get(), 1); |
| 799 |
|
| 800 |
bssl::UniquePtr<SSL_SESSION> session = |
| 801 |
CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 802 |
ASSERT_TRUE(session); |
| 803 |
|
| 804 |
// The client should attempt early data with |session|. |
| 805 |
bssl::UniquePtr<SSL> client, server; |
| 806 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 807 |
server_ctx.get())); |
| 808 |
SSL_set_session(client.get(), session.get()); |
| 809 |
SSL_set_early_data_enabled(client.get(), 1); |
| 810 |
ASSERT_EQ(1, SSL_do_handshake(client.get())); |
| 811 |
EXPECT_TRUE(SSL_in_early_data(client.get())); |
| 812 |
|
| 813 |
// |SSL_SESSION_copy_without_early_data| should disable early data but |
| 814 |
// still resume the session. |
| 815 |
bssl::UniquePtr<SSL_SESSION> session2( |
| 816 |
SSL_SESSION_copy_without_early_data(session.get())); |
| 817 |
ASSERT_TRUE(session2); |
| 818 |
EXPECT_NE(session.get(), session2.get()); |
| 819 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 820 |
server_ctx.get())); |
| 821 |
SSL_set_session(client.get(), session2.get()); |
| 822 |
SSL_set_early_data_enabled(client.get(), 1); |
| 823 |
EXPECT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 824 |
EXPECT_TRUE(SSL_session_reused(client.get())); |
| 825 |
EXPECT_EQ(ssl_early_data_unsupported_for_session, |
| 826 |
SSL_get_early_data_reason(client.get())); |
| 827 |
|
| 828 |
// |SSL_SESSION_copy_without_early_data| should be a reference count increase |
| 829 |
// when passed an early-data-incapable session. |
| 830 |
bssl::UniquePtr<SSL_SESSION> session3( |
| 831 |
SSL_SESSION_copy_without_early_data(session2.get())); |
| 832 |
EXPECT_EQ(session2.get(), session3.get()); |
| 833 |
} |
| 834 |
|
| 835 |
TEST(SSLTest, ProcessTLS13NewSessionTicket) { |
| 836 |
// Configure client and server to negotiate TLS 1.3 only. |
| 837 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 838 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 839 |
CreateContextWithTestCertificate(TLS_method())); |
| 840 |
ASSERT_TRUE(client_ctx); |
| 841 |
ASSERT_TRUE(server_ctx); |
| 842 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 843 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 844 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 845 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 846 |
|
| 847 |
bssl::UniquePtr<SSL> client, server; |
| 848 |
ASSERT_TRUE(ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 849 |
server_ctx.get())); |
| 850 |
EXPECT_EQ(TLS1_3_VERSION, SSL_version(client.get())); |
| 851 |
|
| 852 |
// Process a TLS 1.3 NewSessionTicket. |
| 853 |
static const uint8_t kTicket[] = { |
| 854 |
0x04, 0x00, 0x00, 0xb2, 0x00, 0x02, 0xa3, 0x00, 0x04, 0x03, 0x02, 0x01, |
| 855 |
0x01, 0x00, 0x00, 0xa0, 0x01, 0x06, 0x09, 0x11, 0x16, 0x19, 0x21, 0x26, |
| 856 |
0x29, 0x31, 0x36, 0x39, 0x41, 0x46, 0x49, 0x51, 0x03, 0x06, 0x09, 0x13, |
| 857 |
0x16, 0x19, 0x23, 0x26, 0x29, 0x33, 0x36, 0x39, 0x43, 0x46, 0x49, 0x53, |
| 858 |
0xf7, 0x00, 0x29, 0xec, 0xf2, 0xc4, 0xa4, 0x41, 0xfc, 0x30, 0x17, 0x2e, |
| 859 |
0x9f, 0x7c, 0xa8, 0xaf, 0x75, 0x70, 0xf0, 0x1f, 0xc7, 0x98, 0xf7, 0xcf, |
| 860 |
0x5a, 0x5a, 0x6b, 0x5b, 0xfe, 0xf1, 0xe7, 0x3a, 0xe8, 0xf7, 0x6c, 0xd2, |
| 861 |
0xa8, 0xa6, 0x92, 0x5b, 0x96, 0x8d, 0xde, 0xdb, 0xd3, 0x20, 0x6a, 0xcb, |
| 862 |
0x69, 0x06, 0xf4, 0x91, 0x85, 0x2e, 0xe6, 0x5e, 0x0c, 0x59, 0xf2, 0x9e, |
| 863 |
0x9b, 0x79, 0x91, 0x24, 0x7e, 0x4a, 0x32, 0x3d, 0xbe, 0x4b, 0x80, 0x70, |
| 864 |
0xaf, 0xd0, 0x1d, 0xe2, 0xca, 0x05, 0x35, 0x09, 0x09, 0x05, 0x0f, 0xbb, |
| 865 |
0xc4, 0xae, 0xd7, 0xc4, 0xed, 0xd7, 0xae, 0x35, 0xc8, 0x73, 0x63, 0x78, |
| 866 |
0x64, 0xc9, 0x7a, 0x1f, 0xed, 0x7a, 0x9a, 0x47, 0x44, 0xfd, 0x50, 0xf7, |
| 867 |
0xb7, 0xe0, 0x64, 0xa9, 0x02, 0xc1, 0x5c, 0x23, 0x18, 0x3f, 0xc4, 0xcf, |
| 868 |
0x72, 0x02, 0x59, 0x2d, 0xe1, 0xaa, 0x61, 0x72, 0x00, 0x04, 0x5a, 0x5a, |
| 869 |
0x00, 0x00, |
| 870 |
}; |
| 871 |
bssl::UniquePtr<SSL_SESSION> session(SSL_process_tls13_new_session_ticket( |
| 872 |
client.get(), kTicket, sizeof(kTicket))); |
| 873 |
ASSERT_TRUE(session); |
| 874 |
ASSERT_TRUE(SSL_SESSION_has_ticket(session.get())); |
| 875 |
|
| 876 |
uint8_t *session_buf = nullptr; |
| 877 |
size_t session_length = 0; |
| 878 |
ASSERT_TRUE( |
| 879 |
SSL_SESSION_to_bytes(session.get(), &session_buf, &session_length)); |
| 880 |
bssl::UniquePtr<uint8_t> session_buf_free(session_buf); |
| 881 |
ASSERT_TRUE(session_buf); |
| 882 |
ASSERT_GT(session_length, 0u); |
| 883 |
|
| 884 |
// Servers cannot call |SSL_process_tls13_new_session_ticket|. |
| 885 |
ASSERT_FALSE(SSL_process_tls13_new_session_ticket(server.get(), kTicket, |
| 886 |
sizeof(kTicket))); |
| 887 |
|
| 888 |
// Clients cannot call |SSL_process_tls13_new_session_ticket| before the |
| 889 |
// handshake completes. |
| 890 |
bssl::UniquePtr<SSL> client2(SSL_new(client_ctx.get())); |
| 891 |
ASSERT_TRUE(client2); |
| 892 |
SSL_set_connect_state(client2.get()); |
| 893 |
ASSERT_FALSE(SSL_process_tls13_new_session_ticket(client2.get(), kTicket, |
| 894 |
sizeof(kTicket))); |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
TEST(SSLTest, EmptyWriteBlockedOnHandshakeData) { |
| 899 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 900 |
bssl::UniquePtr<SSL_CTX> server_ctx = |
| 901 |
CreateContextWithTestCertificate(TLS_method()); |
| 902 |
ASSERT_TRUE(client_ctx); |
| 903 |
ASSERT_TRUE(server_ctx); |
| 904 |
// Configure only TLS 1.3. This test requires post-handshake NewSessionTicket. |
| 905 |
ASSERT_TRUE(SSL_CTX_set_min_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 906 |
ASSERT_TRUE(SSL_CTX_set_max_proto_version(client_ctx.get(), TLS1_3_VERSION)); |
| 907 |
|
| 908 |
// Connect a client and server with tiny buffer between the two. |
| 909 |
bssl::UniquePtr<SSL> client(SSL_new(client_ctx.get())), |
| 910 |
server(SSL_new(server_ctx.get())); |
| 911 |
ASSERT_TRUE(client); |
| 912 |
ASSERT_TRUE(server); |
| 913 |
SSL_set_connect_state(client.get()); |
| 914 |
SSL_set_accept_state(server.get()); |
| 915 |
BIO *bio1 = nullptr, *bio2 = nullptr; |
| 916 |
ASSERT_TRUE(BIO_new_bio_pair(&bio1, 1, &bio2, 1)); |
| 917 |
SSL_set_bio(client.get(), bio1, bio1); |
| 918 |
SSL_set_bio(server.get(), bio2, bio2); |
| 919 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server.get())); |
| 920 |
|
| 921 |
// We defer NewSessionTicket to the first write, so the server has a pending |
| 922 |
// NewSessionTicket. See https://boringssl-review.googlesource.com/34948. This |
| 923 |
// means an empty write will flush the ticket. However, the transport only |
| 924 |
// allows one byte through, so this will fail with |SSL_ERROR_WANT_WRITE|. |
| 925 |
int ret = SSL_write(server.get(), nullptr, 0); |
| 926 |
ASSERT_EQ(ret, -1); |
| 927 |
ASSERT_EQ(SSL_get_error(server.get(), ret), SSL_ERROR_WANT_WRITE); |
| 928 |
|
| 929 |
// Attempting to write non-zero data should not trip |SSL_R_BAD_WRITE_RETRY|. |
| 930 |
const uint8_t kData[] = {'h', 'e', 'l', 'l', 'o'}; |
| 931 |
ret = SSL_write(server.get(), kData, sizeof(kData)); |
| 932 |
ASSERT_EQ(ret, -1); |
| 933 |
ASSERT_EQ(SSL_get_error(server.get(), ret), SSL_ERROR_WANT_WRITE); |
| 934 |
|
| 935 |
// Byte by byte, the data should eventually get through. |
| 936 |
uint8_t buf[sizeof(kData)]; |
| 937 |
for (;;) { |
| 938 |
ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 939 |
ASSERT_EQ(ret, -1); |
| 940 |
ASSERT_EQ(SSL_get_error(client.get(), ret), SSL_ERROR_WANT_READ); |
| 941 |
|
| 942 |
ret = SSL_write(server.get(), kData, sizeof(kData)); |
| 943 |
if (ret > 0) { |
| 944 |
ASSERT_EQ(ret, 5); |
| 945 |
break; |
| 946 |
} |
| 947 |
ASSERT_EQ(ret, -1); |
| 948 |
ASSERT_EQ(SSL_get_error(server.get(), ret), SSL_ERROR_WANT_WRITE); |
| 949 |
} |
| 950 |
|
| 951 |
ret = SSL_read(client.get(), buf, sizeof(buf)); |
| 952 |
ASSERT_EQ(ret, static_cast<int>(sizeof(kData))); |
| 953 |
ASSERT_EQ(Bytes(buf, ret), Bytes(kData)); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
TEST(SSLTest, HostMatching) { |
| 958 |
static const char kCertPEM[] = R"( |
| 959 |
-----BEGIN CERTIFICATE----- |
| 960 |
MIIB9jCCAZ2gAwIBAgIQeudG9R61BOxUvWkeVhU5DTAKBggqhkjOPQQDAjApMRAw |
| 961 |
DgYDVQQKEwdBY21lIENvMRUwEwYDVQQDEwxleGFtcGxlMy5jb20wHhcNMjExMjA2 |
| 962 |
MjA1NjU2WhcNMjIxMjA2MjA1NjU2WjApMRAwDgYDVQQKEwdBY21lIENvMRUwEwYD |
| 963 |
VQQDEwxleGFtcGxlMy5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAS7l2VO |
| 964 |
Bl2TjVm9WfGk24+hMbVFUNB+RVHWbCvFvNZAoWiIJ2z34RLGInyZvCZ8xLAvsuaW |
| 965 |
ULDDaoeDl1M0t4Hmo4GmMIGjMA4GA1UdDwEB/wQEAwIChDATBgNVHSUEDDAKBggr |
| 966 |
BgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTTJWurcc1t+VPQBko3 |
| 967 |
Gsw6cbcWSTBMBgNVHREERTBDggxleGFtcGxlMS5jb22CDGV4YW1wbGUyLmNvbYIP |
| 968 |
YSouZXhhbXBsZTQuY29tgg4qLmV4YW1wbGU1LmNvbYcEAQIDBDAKBggqhkjOPQQD |
| 969 |
AgNHADBEAiAAv0ljHJGrgyzZDkG6XvNZ5ewxRfnXcZuD0Y7E4giCZgIgNK1qjilu |
| 970 |
5DyVbfKeeJhOCtGxqE1dWLXyJBnoRomSYBY= |
| 971 |
-----END CERTIFICATE----- |
| 972 |
)"; |
| 973 |
bssl::UniquePtr<X509> cert(CertFromPEM(kCertPEM)); |
| 974 |
ASSERT_TRUE(cert); |
| 975 |
static const char kCertNoSANsPEM[] = R"( |
| 976 |
-----BEGIN CERTIFICATE----- |
| 977 |
MIIBqzCCAVGgAwIBAgIQeudG9R61BOxUvWkeVhU5DTAKBggqhkjOPQQDAjArMRIw |
| 978 |
EAYDVQQKEwlBY21lIENvIDIxFTATBgNVBAMTDGV4YW1wbGUzLmNvbTAeFw0yMTEy |
| 979 |
MDYyMDU2NTZaFw0yMjEyMDYyMDU2NTZaMCsxEjAQBgNVBAoTCUFjbWUgQ28gMjEV |
| 980 |
MBMGA1UEAxMMZXhhbXBsZTMuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE |
| 981 |
u5dlTgZdk41ZvVnxpNuPoTG1RVDQfkVR1mwrxbzWQKFoiCds9+ESxiJ8mbwmfMSw |
| 982 |
L7LmllCww2qHg5dTNLeB5qNXMFUwDgYDVR0PAQH/BAQDAgKEMBMGA1UdJQQMMAoG |
| 983 |
CCsGAQUFBwMBMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFNMla6txzW35U9AG |
| 984 |
SjcazDpxtxZJMAoGCCqGSM49BAMCA0gAMEUCIG3YWGWtpVhbcGV7wFKQwTfmvwHW |
| 985 |
pw4qCFZlool4hCwsAiEA+2fc6NfSbNpFEtQkDOMJW2ANiScAVEmImNqPfb2klz4= |
| 986 |
-----END CERTIFICATE----- |
| 987 |
)"; |
| 988 |
bssl::UniquePtr<X509> cert_no_sans(CertFromPEM(kCertNoSANsPEM)); |
| 989 |
ASSERT_TRUE(cert_no_sans); |
| 990 |
|
| 991 |
static const char kKeyPEM[] = R"( |
| 992 |
-----BEGIN PRIVATE KEY----- |
| 993 |
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQghsaSZhUzZAcQlLyJ |
| 994 |
MDuy7WPdyqNsAX9rmEP650LF/q2hRANCAAS7l2VOBl2TjVm9WfGk24+hMbVFUNB+ |
| 995 |
RVHWbCvFvNZAoWiIJ2z34RLGInyZvCZ8xLAvsuaWULDDaoeDl1M0t4Hm |
| 996 |
-----END PRIVATE KEY----- |
| 997 |
)"; |
| 998 |
bssl::UniquePtr<EVP_PKEY> key(KeyFromPEM(kKeyPEM)); |
| 999 |
ASSERT_TRUE(key); |
| 1000 |
|
| 1001 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1002 |
ASSERT_TRUE(client_ctx); |
| 1003 |
ASSERT_TRUE(X509_STORE_add_cert(SSL_CTX_get_cert_store(client_ctx.get()), |
| 1004 |
cert.get())); |
| 1005 |
ASSERT_TRUE(X509_STORE_add_cert(SSL_CTX_get_cert_store(client_ctx.get()), |
| 1006 |
cert_no_sans.get())); |
| 1007 |
SSL_CTX_set_verify(client_ctx.get(), |
| 1008 |
SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 1009 |
nullptr); |
| 1010 |
X509_VERIFY_PARAM_set_flags(SSL_CTX_get0_param(client_ctx.get()), |
| 1011 |
X509_V_FLAG_NO_CHECK_TIME); |
| 1012 |
|
| 1013 |
struct TestCase { |
| 1014 |
X509 *cert; |
| 1015 |
std::string hostname; |
| 1016 |
unsigned flags; |
| 1017 |
bool should_match; |
| 1018 |
}; |
| 1019 |
std::vector<TestCase> kTests = { |
| 1020 |
// These two names are present as SANs in the certificate. |
| 1021 |
{cert.get(), "example1.com", 0, true}, |
| 1022 |
{cert.get(), "example2.com", 0, true}, |
| 1023 |
// This is the CN of the certificate, but that shouldn't matter if a SAN |
| 1024 |
// extension is present. |
| 1025 |
{cert.get(), "example3.com", 0, false}, |
| 1026 |
// If the SAN is not present, we, for now, look for DNS names in the CN. |
| 1027 |
{cert_no_sans.get(), "example3.com", 0, true}, |
| 1028 |
// ... but this can be turned off. |
| 1029 |
{cert_no_sans.get(), "example3.com", X509_CHECK_FLAG_NEVER_CHECK_SUBJECT, |
| 1030 |
false}, |
| 1031 |
// a*.example4.com is a SAN, but is invalid. |
| 1032 |
{cert.get(), "abc.example4.com", 0, false}, |
| 1033 |
// *.example5.com is a SAN in the certificate, which is a normal and valid |
| 1034 |
// wildcard. |
| 1035 |
{cert.get(), "abc.example5.com", 0, true}, |
| 1036 |
// This name is not present. |
| 1037 |
{cert.get(), "notexample1.com", 0, false}, |
| 1038 |
// The IPv4 address 1.2.3.4 is a SAN, but that shouldn't match against a |
| 1039 |
// hostname that happens to be its textual representation. |
| 1040 |
{cert.get(), "1.2.3.4", 0, false}, |
| 1041 |
}; |
| 1042 |
|
| 1043 |
for (const TestCase &test : kTests) { |
| 1044 |
SCOPED_TRACE(test.hostname); |
| 1045 |
|
| 1046 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1047 |
ASSERT_TRUE(server_ctx); |
| 1048 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), test.cert)); |
| 1049 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx.get(), key.get())); |
| 1050 |
|
| 1051 |
ClientConfig config; |
| 1052 |
bssl::UniquePtr<SSL> client, server; |
| 1053 |
config.verify_hostname = test.hostname; |
| 1054 |
config.hostflags = test.flags; |
| 1055 |
EXPECT_EQ(test.should_match, |
| 1056 |
ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1057 |
server_ctx.get(), config)); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
TEST(SSLTest, NumTickets) { |
| 1062 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 1063 |
ASSERT_TRUE(server_ctx); |
| 1064 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 1065 |
ASSERT_TRUE(client_ctx); |
| 1066 |
bssl::UniquePtr<X509> cert = GetTestCertificate(); |
| 1067 |
ASSERT_TRUE(cert); |
| 1068 |
bssl::UniquePtr<EVP_PKEY> key = GetTestKey(); |
| 1069 |
ASSERT_TRUE(key); |
| 1070 |
ASSERT_TRUE(SSL_CTX_use_certificate(server_ctx.get(), cert.get())); |
| 1071 |
ASSERT_TRUE(SSL_CTX_use_PrivateKey(server_ctx.get(), key.get())); |
| 1072 |
SSL_CTX_set_session_cache_mode(server_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 1073 |
|
| 1074 |
SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 1075 |
static size_t ticket_count; |
| 1076 |
SSL_CTX_sess_set_new_cb(client_ctx.get(), [](SSL *, SSL_SESSION *) -> int { |
| 1077 |
ticket_count++; |
| 1078 |
return 0; |
| 1079 |
}); |
| 1080 |
|
| 1081 |
auto count_tickets = [&]() -> size_t { |
| 1082 |
ticket_count = 0; |
| 1083 |
bssl::UniquePtr<SSL> client, server; |
| 1084 |
if (!ConnectClientAndServer(&client, &server, client_ctx.get(), |
| 1085 |
server_ctx.get()) || |
| 1086 |
!FlushNewSessionTickets(client.get(), server.get())) { |
| 1087 |
ADD_FAILURE() << "Could not run handshake"; |
| 1088 |
return 0; |
| 1089 |
} |
| 1090 |
return ticket_count; |
| 1091 |
}; |
| 1092 |
|
| 1093 |
// By default, we should send two tickets. |
| 1094 |
EXPECT_EQ(count_tickets(), 2u); |
| 1095 |
|
| 1096 |
for (size_t num_tickets : {0, 1, 2, 3, 4, 5}) { |
| 1097 |
SCOPED_TRACE(num_tickets); |
| 1098 |
ASSERT_TRUE(SSL_CTX_set_num_tickets(server_ctx.get(), num_tickets)); |
| 1099 |
EXPECT_EQ(SSL_CTX_get_num_tickets(server_ctx.get()), num_tickets); |
| 1100 |
EXPECT_EQ(count_tickets(), num_tickets); |
| 1101 |
} |
| 1102 |
|
| 1103 |
// Configuring too many tickets causes us to stop at some point. |
| 1104 |
ASSERT_TRUE(SSL_CTX_set_num_tickets(server_ctx.get(), 100000)); |
| 1105 |
EXPECT_EQ(SSL_CTX_get_num_tickets(server_ctx.get()), 16u); |
| 1106 |
EXPECT_EQ(count_tickets(), 16u); |
| 1107 |
} |
| 1108 |
|
| 1109 |
TEST(SSLTest, CertSubjectsToStack) { |
| 1110 |
const std::string kCert1 = R"( |
| 1111 |
-----BEGIN CERTIFICATE----- |
| 1112 |
MIIBzzCCAXagAwIBAgIJANlMBNpJfb/rMAkGByqGSM49BAEwRTELMAkGA1UEBhMC |
| 1113 |
QVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdp |
| 1114 |
dHMgUHR5IEx0ZDAeFw0xNDA0MjMyMzIxNTdaFw0xNDA1MjMyMzIxNTdaMEUxCzAJ |
| 1115 |
BgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5l |
| 1116 |
dCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATmK2ni |
| 1117 |
v2Wfl74vHg2UikzVl2u3qR4NRvvdqakendy6WgHn1peoChj5w8SjHlbifINI2xYa |
| 1118 |
HPUdfvGULUvPciLBo1AwTjAdBgNVHQ4EFgQUq4TSrKuV8IJOFngHVVdf5CaNgtEw |
| 1119 |
HwYDVR0jBBgwFoAUq4TSrKuV8IJOFngHVVdf5CaNgtEwDAYDVR0TBAUwAwEB/zAJ |
| 1120 |
BgcqhkjOPQQBA0gAMEUCIQDyoDVeUTo2w4J5m+4nUIWOcAZ0lVfSKXQA9L4Vh13E |
| 1121 |
BwIgfB55FGohg/B6dGh5XxSZmmi08cueFV7mHzJSYV51yRQ= |
| 1122 |
-----END CERTIFICATE----- |
| 1123 |
)"; |
| 1124 |
const std::vector<uint8_t> kName1 = { |
| 1125 |
0x30, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, |
| 1126 |
0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, |
| 1127 |
0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, |
| 1128 |
0x31, 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x18, 0x49, |
| 1129 |
0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, |
| 1130 |
0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 0x64}; |
| 1131 |
const std::string kCert2 = R"( |
| 1132 |
-----BEGIN CERTIFICATE----- |
| 1133 |
MIICXjCCAcegAwIBAgIIWjO48ufpunYwDQYJKoZIhvcNAQELBQAwNjEaMBgGA1UE |
| 1134 |
ChMRQm9yaW5nU1NMIFRFU1RJTkcxGDAWBgNVBAMTD0ludGVybWVkaWF0ZSBDQTAg |
| 1135 |
Fw0xNTAxMDEwMDAwMDBaGA8yMTAwMDEwMTAwMDAwMFowMjEaMBgGA1UEChMRQm9y |
| 1136 |
aW5nU1NMIFRFU1RJTkcxFDASBgNVBAMTC2V4YW1wbGUuY29tMIGfMA0GCSqGSIb3 |
| 1137 |
DQEBAQUAA4GNADCBiQKBgQDD0U0ZYgqShJ7oOjsyNKyVXEHqeafmk/bAoPqY/h1c |
| 1138 |
oPw2E8KmeqiUSoTPjG5IXSblOxcqpbAXgnjPzo8DI3GNMhAf8SYNYsoH7gc7Uy7j |
| 1139 |
5x8bUrisGnuTHqkqH6d4/e7ETJ7i3CpR8bvK16DggEvQTudLipz8FBHtYhFakfdh |
| 1140 |
TwIDAQABo3cwdTAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEG |
| 1141 |
CCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwGQYDVR0OBBIEEKN5pvbur7mlXjeMEYA0 |
| 1142 |
4nUwGwYDVR0jBBQwEoAQjBpoqLV2211Xex+NFLIGozANBgkqhkiG9w0BAQsFAAOB |
| 1143 |
gQBj/p+JChp//LnXWC1k121LM/ii7hFzQzMrt70bny406SGz9jAjaPOX4S3gt38y |
| 1144 |
rhjpPukBlSzgQXFg66y6q5qp1nQTD1Cw6NkKBe9WuBlY3iYfmsf7WT8nhlT1CttU |
| 1145 |
xNCwyMX9mtdXdQicOfNjIGUCD5OLV5PgHFPRKiHHioBAhg== |
| 1146 |
-----END CERTIFICATE----- |
| 1147 |
)"; |
| 1148 |
const std::vector<uint8_t> kName2 = { |
| 1149 |
0x30, 0x32, 0x31, 0x1a, 0x30, 0x18, 0x06, 0x03, 0x55, 0x04, 0x0a, |
| 1150 |
0x13, 0x11, 0x42, 0x6f, 0x72, 0x69, 0x6e, 0x67, 0x53, 0x53, 0x4c, |
| 1151 |
0x20, 0x54, 0x45, 0x53, 0x54, 0x49, 0x4e, 0x47, 0x31, 0x14, 0x30, |
| 1152 |
0x12, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0b, 0x65, 0x78, 0x61, |
| 1153 |
0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d}; |
| 1154 |
|
| 1155 |
const struct { |
| 1156 |
std::vector<std::vector<uint8_t>> existing; |
| 1157 |
std::string pem; |
| 1158 |
std::vector<std::vector<uint8_t>> expected; |
| 1159 |
} kTests[] = { |
| 1160 |
// Do nothing. |
| 1161 |
{{}, "", {}}, |
| 1162 |
// Append to an empty list, skipping duplicates. |
| 1163 |
{{}, kCert1 + kCert2 + kCert1, {kName1, kName2}}, |
| 1164 |
// One of the names was already present. |
| 1165 |
{{kName1}, kCert1 + kCert2, {kName1, kName2}}, |
| 1166 |
// Both names were already present. |
| 1167 |
{{kName1, kName2}, kCert1 + kCert2, {kName1, kName2}}, |
| 1168 |
// Preserve existing duplicates. |
| 1169 |
{{kName1, kName2, kName2}, kCert1 + kCert2, {kName1, kName2, kName2}}, |
| 1170 |
}; |
| 1171 |
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTests); i++) { |
| 1172 |
SCOPED_TRACE(i); |
| 1173 |
const auto &t = kTests[i]; |
| 1174 |
|
| 1175 |
bssl::UniquePtr<STACK_OF(X509_NAME)> stack(sk_X509_NAME_new_null()); |
| 1176 |
ASSERT_TRUE(stack); |
| 1177 |
for (const auto &name : t.existing) { |
| 1178 |
const uint8_t *inp = name.data(); |
| 1179 |
bssl::UniquePtr<X509_NAME> name_obj( |
| 1180 |
d2i_X509_NAME(nullptr, &inp, name.size())); |
| 1181 |
ASSERT_TRUE(name_obj); |
| 1182 |
EXPECT_EQ(inp, name.data() + name.size()); |
| 1183 |
ASSERT_TRUE(bssl::PushToStack(stack.get(), std::move(name_obj))); |
| 1184 |
} |
| 1185 |
|
| 1186 |
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(t.pem.data(), t.pem.size())); |
| 1187 |
ASSERT_TRUE(bio); |
| 1188 |
ASSERT_TRUE(SSL_add_bio_cert_subjects_to_stack(stack.get(), bio.get())); |
| 1189 |
|
| 1190 |
// The function should have left |stack|'s comparison function alone. |
| 1191 |
EXPECT_EQ(nullptr, sk_X509_NAME_set_cmp_func(stack.get(), nullptr)); |
| 1192 |
|
| 1193 |
std::vector<std::vector<uint8_t>> expected = t.expected, result; |
| 1194 |
for (X509_NAME *name : stack.get()) { |
| 1195 |
uint8_t *der = nullptr; |
| 1196 |
int der_len = i2d_X509_NAME(name, &der); |
| 1197 |
ASSERT_GE(der_len, 0); |
| 1198 |
result.push_back(std::vector<uint8_t>(der, der + der_len)); |
| 1199 |
OPENSSL_free(der); |
| 1200 |
} |
| 1201 |
|
| 1202 |
// |SSL_add_bio_cert_subjects_to_stack| does not return the output in a |
| 1203 |
// well-defined order. |
| 1204 |
std::sort(expected.begin(), expected.end()); |
| 1205 |
std::sort(result.begin(), result.end()); |
| 1206 |
EXPECT_EQ(result, expected); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
TEST(SSLTest, EmptyClientCAList) { |
| 1211 |
if (SkipTempFileTests()) { |
| 1212 |
GTEST_SKIP(); |
| 1213 |
} |
| 1214 |
|
| 1215 |
TemporaryFile empty; |
| 1216 |
ASSERT_TRUE(empty.Init()); |
| 1217 |
bssl::UniquePtr<STACK_OF(X509_NAME)> names( |
| 1218 |
SSL_load_client_CA_file(empty.path().c_str())); |
| 1219 |
EXPECT_FALSE(names); |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
// Test that |SSL_can_release_private_key| reports true as early as expected. |
| 1224 |
// The internal asserts in the library check we do not report true too early. |
| 1225 |
TEST(SSLTest, CanReleasePrivateKey) { |
| 1226 |
bssl::UniquePtr<SSL_CTX> client_ctx = |
| 1227 |
CreateContextWithTestCertificate(TLS_method()); |
| 1228 |
ASSERT_TRUE(client_ctx); |
| 1229 |
SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_BOTH); |
| 1230 |
|
| 1231 |
// Note this assumes the transport buffer is large enough to fit the client |
| 1232 |
// and server first flights. We check this with |SSL_ERROR_WANT_READ|. If the |
| 1233 |
// transport buffer was too small it would return |SSL_ERROR_WANT_WRITE|. |
| 1234 |
auto check_first_server_round_trip = [&](SSL *client, SSL *server) { |
| 1235 |
// Write the ClientHello. |
| 1236 |
ASSERT_EQ(-1, SSL_do_handshake(client)); |
| 1237 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client, -1)); |
| 1238 |
|
| 1239 |
// Consume the ClientHello and write the server flight. |
| 1240 |
ASSERT_EQ(-1, SSL_do_handshake(server)); |
| 1241 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(server, -1)); |
| 1242 |
|
| 1243 |
EXPECT_TRUE(SSL_can_release_private_key(server)); |
| 1244 |
}; |
| 1245 |
|
| 1246 |
{ |
| 1247 |
SCOPED_TRACE("TLS 1.2 ECDHE"); |
| 1248 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 1249 |
CreateContextWithTestCertificate(TLS_method())); |
| 1250 |
ASSERT_TRUE(server_ctx); |
| 1251 |
ASSERT_TRUE( |
| 1252 |
SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_2_VERSION)); |
| 1253 |
ASSERT_TRUE(SSL_CTX_set_strict_cipher_list( |
| 1254 |
server_ctx.get(), "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")); |
| 1255 |
// Configure the server to request client certificates, so we can also test |
| 1256 |
// the client half. |
| 1257 |
SSL_CTX_set_custom_verify( |
| 1258 |
server_ctx.get(), SSL_VERIFY_PEER, |
| 1259 |
[](SSL *ssl, uint8_t *out_alert) { return ssl_verify_ok; }); |
| 1260 |
bssl::UniquePtr<SSL> client, server; |
| 1261 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 1262 |
server_ctx.get())); |
| 1263 |
check_first_server_round_trip(client.get(), server.get()); |
| 1264 |
|
| 1265 |
// Consume the server flight and write the client response. The client still |
| 1266 |
// has a Finished message to consume but can also release its key early. |
| 1267 |
ASSERT_EQ(-1, SSL_do_handshake(client.get())); |
| 1268 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client.get(), -1)); |
| 1269 |
EXPECT_TRUE(SSL_can_release_private_key(client.get())); |
| 1270 |
|
| 1271 |
// However, a client that has not disabled renegotiation can never release |
| 1272 |
// the key. |
| 1273 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 1274 |
server_ctx.get())); |
| 1275 |
SSL_set_renegotiate_mode(client.get(), ssl_renegotiate_freely); |
| 1276 |
check_first_server_round_trip(client.get(), server.get()); |
| 1277 |
ASSERT_EQ(-1, SSL_do_handshake(client.get())); |
| 1278 |
ASSERT_EQ(SSL_ERROR_WANT_READ, SSL_get_error(client.get(), -1)); |
| 1279 |
EXPECT_FALSE(SSL_can_release_private_key(client.get())); |
| 1280 |
} |
| 1281 |
|
| 1282 |
{ |
| 1283 |
SCOPED_TRACE("TLS 1.2 resumption"); |
| 1284 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 1285 |
CreateContextWithTestCertificate(TLS_method())); |
| 1286 |
ASSERT_TRUE(server_ctx); |
| 1287 |
ASSERT_TRUE( |
| 1288 |
SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_2_VERSION)); |
| 1289 |
bssl::UniquePtr<SSL_SESSION> session = |
| 1290 |
CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 1291 |
ASSERT_TRUE(session); |
| 1292 |
bssl::UniquePtr<SSL> client, server; |
| 1293 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 1294 |
server_ctx.get())); |
| 1295 |
SSL_set_session(client.get(), session.get()); |
| 1296 |
check_first_server_round_trip(client.get(), server.get()); |
| 1297 |
} |
| 1298 |
|
| 1299 |
{ |
| 1300 |
SCOPED_TRACE("TLS 1.3 1-RTT"); |
| 1301 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 1302 |
CreateContextWithTestCertificate(TLS_method())); |
| 1303 |
ASSERT_TRUE(server_ctx); |
| 1304 |
ASSERT_TRUE( |
| 1305 |
SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 1306 |
bssl::UniquePtr<SSL> client, server; |
| 1307 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 1308 |
server_ctx.get())); |
| 1309 |
check_first_server_round_trip(client.get(), server.get()); |
| 1310 |
} |
| 1311 |
|
| 1312 |
{ |
| 1313 |
SCOPED_TRACE("TLS 1.3 resumption"); |
| 1314 |
bssl::UniquePtr<SSL_CTX> server_ctx( |
| 1315 |
CreateContextWithTestCertificate(TLS_method())); |
| 1316 |
ASSERT_TRUE(server_ctx); |
| 1317 |
ASSERT_TRUE( |
| 1318 |
SSL_CTX_set_max_proto_version(server_ctx.get(), TLS1_3_VERSION)); |
| 1319 |
bssl::UniquePtr<SSL_SESSION> session = |
| 1320 |
CreateClientSession(client_ctx.get(), server_ctx.get()); |
| 1321 |
ASSERT_TRUE(session); |
| 1322 |
bssl::UniquePtr<SSL> client, server; |
| 1323 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 1324 |
server_ctx.get())); |
| 1325 |
SSL_set_session(client.get(), session.get()); |
| 1326 |
check_first_server_round_trip(client.get(), server.get()); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
BSSL_NAMESPACE_END |