| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include "openssl/bytestring.h" |
| 5 |
#include "openssl/ssl.h" |
| 6 |
|
| 7 |
#include "internal.h" |
| 8 |
#include "ssl_common_test.h" |
| 9 |
|
| 10 |
#include <gtest/gtest.h> |
| 11 |
|
| 12 |
BSSL_NAMESPACE_BEGIN |
| 13 |
|
| 14 |
namespace { |
| 15 |
|
| 16 |
// This is a basic unit-test class to verify completing handshake successfully, |
| 17 |
// sending the correct codepoint extension and having correct application |
| 18 |
// setting on different combination of ALPS codepoint settings. More integration |
| 19 |
// tests on runner.go. |
| 20 |
class AlpsNewCodepointTest : public testing::Test { |
| 21 |
protected: |
| 22 |
void SetUp() override { |
| 23 |
client_ctx_.reset(SSL_CTX_new(TLS_method())); |
| 24 |
server_ctx_ = CreateContextWithTestCertificate(TLS_method()); |
| 25 |
ASSERT_TRUE(client_ctx_); |
| 26 |
ASSERT_TRUE(server_ctx_); |
| 27 |
} |
| 28 |
|
| 29 |
void SetUpApplicationSetting() { |
| 30 |
static const uint8_t alpn[] = {0x03, 'f', 'o', 'o'}; |
| 31 |
static const uint8_t proto[] = {'f', 'o', 'o'}; |
| 32 |
static const uint8_t alps[] = {0x04, 'a', 'l', 'p', 's'}; |
| 33 |
// SSL_set_alpn_protos's return value is backwards. It returns zero on |
| 34 |
// success and one on failure. |
| 35 |
ASSERT_FALSE(SSL_set_alpn_protos(client_.get(), alpn, sizeof(alpn))); |
| 36 |
SSL_CTX_set_alpn_select_cb( |
| 37 |
server_ctx_.get(), |
| 38 |
[](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in, |
| 39 |
unsigned in_len, void *arg) -> int { |
| 40 |
return SSL_select_next_proto(const_cast<uint8_t **>(out), out_len, in, |
| 41 |
in_len, alpn, |
| 42 |
sizeof(alpn)) == OPENSSL_NPN_NEGOTIATED |
| 43 |
? SSL_TLSEXT_ERR_OK |
| 44 |
: SSL_TLSEXT_ERR_NOACK; |
| 45 |
}, |
| 46 |
nullptr); |
| 47 |
ASSERT_TRUE(SSL_add_application_settings(client_.get(), proto, |
| 48 |
sizeof(proto), nullptr, 0)); |
| 49 |
ASSERT_TRUE(SSL_add_application_settings( |
| 50 |
server_.get(), proto, sizeof(proto), alps, sizeof(alps))); |
| 51 |
} |
| 52 |
|
| 53 |
bssl::UniquePtr<SSL_CTX> client_ctx_; |
| 54 |
bssl::UniquePtr<SSL_CTX> server_ctx_; |
| 55 |
|
| 56 |
bssl::UniquePtr<SSL> client_; |
| 57 |
bssl::UniquePtr<SSL> server_; |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
TEST_F(AlpsNewCodepointTest, Enabled) { |
| 62 |
SetUpExpectedNewCodePoint(server_ctx_.get()); |
| 63 |
|
| 64 |
ASSERT_TRUE(CreateClientAndServer(&client_, &server_, client_ctx_.get(), |
| 65 |
server_ctx_.get())); |
| 66 |
|
| 67 |
SSL_set_alps_use_new_codepoint(client_.get(), 1); |
| 68 |
SSL_set_alps_use_new_codepoint(server_.get(), 1); |
| 69 |
|
| 70 |
SetUpApplicationSetting(); |
| 71 |
ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 72 |
ASSERT_TRUE(SSL_has_application_settings(client_.get())); |
| 73 |
} |
| 74 |
|
| 75 |
TEST_F(AlpsNewCodepointTest, Disabled) { |
| 76 |
// Both client and server disable alps new codepoint. |
| 77 |
SetUpExpectedOldCodePoint(server_ctx_.get()); |
| 78 |
|
| 79 |
ASSERT_TRUE(CreateClientAndServer(&client_, &server_, client_ctx_.get(), |
| 80 |
server_ctx_.get())); |
| 81 |
|
| 82 |
SSL_set_alps_use_new_codepoint(client_.get(), 0); |
| 83 |
SSL_set_alps_use_new_codepoint(server_.get(), 0); |
| 84 |
|
| 85 |
SetUpApplicationSetting(); |
| 86 |
ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 87 |
ASSERT_TRUE(SSL_has_application_settings(client_.get())); |
| 88 |
} |
| 89 |
|
| 90 |
TEST_F(AlpsNewCodepointTest, ClientOnly) { |
| 91 |
// If client set new codepoint but server doesn't set, server ignores it. |
| 92 |
SetUpExpectedNewCodePoint(server_ctx_.get()); |
| 93 |
|
| 94 |
ASSERT_TRUE(CreateClientAndServer(&client_, &server_, client_ctx_.get(), |
| 95 |
server_ctx_.get())); |
| 96 |
|
| 97 |
SSL_set_alps_use_new_codepoint(client_.get(), 1); |
| 98 |
SSL_set_alps_use_new_codepoint(server_.get(), 0); |
| 99 |
|
| 100 |
SetUpApplicationSetting(); |
| 101 |
ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 102 |
ASSERT_FALSE(SSL_has_application_settings(client_.get())); |
| 103 |
} |
| 104 |
|
| 105 |
TEST_F(AlpsNewCodepointTest, ServerOnly) { |
| 106 |
// If client doesn't set new codepoint, while server set. |
| 107 |
SetUpExpectedOldCodePoint(server_ctx_.get()); |
| 108 |
|
| 109 |
ASSERT_TRUE(CreateClientAndServer(&client_, &server_, client_ctx_.get(), |
| 110 |
server_ctx_.get())); |
| 111 |
|
| 112 |
SSL_set_alps_use_new_codepoint(client_.get(), 0); |
| 113 |
SSL_set_alps_use_new_codepoint(server_.get(), 1); |
| 114 |
|
| 115 |
SetUpApplicationSetting(); |
| 116 |
ASSERT_TRUE(CompleteHandshakes(client_.get(), server_.get())); |
| 117 |
ASSERT_FALSE(SSL_has_application_settings(client_.get())); |
| 118 |
} |
| 119 |
|
| 120 |
void MoveBIOs(SSL *dest, SSL *src) { |
| 121 |
BIO *rbio = SSL_get_rbio(src); |
| 122 |
BIO_up_ref(rbio); |
| 123 |
SSL_set0_rbio(dest, rbio); |
| 124 |
|
| 125 |
BIO *wbio = SSL_get_wbio(src); |
| 126 |
BIO_up_ref(wbio); |
| 127 |
SSL_set0_wbio(dest, wbio); |
| 128 |
|
| 129 |
SSL_set0_rbio(src, nullptr); |
| 130 |
SSL_set0_wbio(src, nullptr); |
| 131 |
} |
| 132 |
|
| 133 |
void VerifyHandoff(bool use_new_alps_codepoint) { |
| 134 |
static const uint8_t alpn[] = {0x03, 'f', 'o', 'o'}; |
| 135 |
static const uint8_t proto[] = {'f', 'o', 'o'}; |
| 136 |
static const uint8_t alps[] = {0x04, 'a', 'l', 'p', 's'}; |
| 137 |
|
| 138 |
bssl::UniquePtr<SSL_CTX> client_ctx(SSL_CTX_new(TLS_method())); |
| 139 |
bssl::UniquePtr<SSL_CTX> server_ctx(SSL_CTX_new(TLS_method())); |
| 140 |
bssl::UniquePtr<SSL_CTX> handshaker_ctx( |
| 141 |
CreateContextWithTestCertificate(TLS_method())); |
| 142 |
ASSERT_TRUE(client_ctx); |
| 143 |
ASSERT_TRUE(server_ctx); |
| 144 |
ASSERT_TRUE(handshaker_ctx); |
| 145 |
|
| 146 |
if (!use_new_alps_codepoint) { |
| 147 |
SetUpExpectedOldCodePoint(server_ctx.get()); |
| 148 |
} else { |
| 149 |
SetUpExpectedNewCodePoint(server_ctx.get()); |
| 150 |
} |
| 151 |
|
| 152 |
SSL_CTX_set_session_cache_mode(client_ctx.get(), SSL_SESS_CACHE_CLIENT); |
| 153 |
SSL_CTX_sess_set_new_cb(client_ctx.get(), SaveLastSession); |
| 154 |
SSL_CTX_set_handoff_mode(server_ctx.get(), true); |
| 155 |
uint8_t keys[48]; |
| 156 |
SSL_CTX_get_tlsext_ticket_keys(server_ctx.get(), &keys, sizeof(keys)); |
| 157 |
SSL_CTX_set_tlsext_ticket_keys(handshaker_ctx.get(), &keys, sizeof(keys)); |
| 158 |
|
| 159 |
for (bool early_data : {false, true}) { |
| 160 |
SCOPED_TRACE(early_data); |
| 161 |
for (bool is_resume : {false, true}) { |
| 162 |
SCOPED_TRACE(is_resume); |
| 163 |
bssl::UniquePtr<SSL> client, server; |
| 164 |
ASSERT_TRUE(CreateClientAndServer(&client, &server, client_ctx.get(), |
| 165 |
server_ctx.get())); |
| 166 |
SSL_set_early_data_enabled(client.get(), early_data); |
| 167 |
|
| 168 |
// Set up client ALPS settings. |
| 169 |
SSL_set_alps_use_new_codepoint(client.get(), use_new_alps_codepoint); |
| 170 |
ASSERT_TRUE(SSL_set_alpn_protos(client.get(), alpn, sizeof(alpn)) == 0); |
| 171 |
ASSERT_TRUE(SSL_add_application_settings(client.get(), proto, |
| 172 |
sizeof(proto), nullptr, 0)); |
| 173 |
if (is_resume) { |
| 174 |
ASSERT_TRUE(g_last_session); |
| 175 |
SSL_set_session(client.get(), g_last_session.get()); |
| 176 |
if (early_data) { |
| 177 |
EXPECT_GT(g_last_session->ticket_max_early_data, 0u); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
int client_ret = SSL_do_handshake(client.get()); |
| 183 |
int client_err = SSL_get_error(client.get(), client_ret); |
| 184 |
|
| 185 |
uint8_t byte_written = 0; |
| 186 |
if (early_data && is_resume) { |
| 187 |
ASSERT_EQ(client_err, 0); |
| 188 |
EXPECT_TRUE(SSL_in_early_data(client.get())); |
| 189 |
// Attempt to write early data. |
| 190 |
byte_written = 43; |
| 191 |
EXPECT_EQ(SSL_write(client.get(), &byte_written, 1), 1); |
| 192 |
} else { |
| 193 |
ASSERT_EQ(client_err, SSL_ERROR_WANT_READ); |
| 194 |
} |
| 195 |
|
| 196 |
int server_ret = SSL_do_handshake(server.get()); |
| 197 |
int server_err = SSL_get_error(server.get(), server_ret); |
| 198 |
ASSERT_EQ(server_err, SSL_ERROR_HANDOFF); |
| 199 |
|
| 200 |
ScopedCBB cbb; |
| 201 |
Array<uint8_t> handoff; |
| 202 |
SSL_CLIENT_HELLO hello; |
| 203 |
ASSERT_TRUE(CBB_init(cbb.get(), 256)); |
| 204 |
ASSERT_TRUE(SSL_serialize_handoff(server.get(), cbb.get(), &hello)); |
| 205 |
ASSERT_TRUE(CBBFinishArray(cbb.get(), &handoff)); |
| 206 |
|
| 207 |
bssl::UniquePtr<SSL> handshaker(SSL_new(handshaker_ctx.get())); |
| 208 |
ASSERT_TRUE(handshaker); |
| 209 |
// Note split handshakes determines 0-RTT support, for both the current |
| 210 |
// handshake and newly-issued tickets, entirely by |handshaker|. There is |
| 211 |
// no need to call |SSL_set_early_data_enabled| on |server|. |
| 212 |
SSL_set_early_data_enabled(handshaker.get(), 1); |
| 213 |
|
| 214 |
// Set up handshaker ALPS settings. |
| 215 |
SSL_set_alps_use_new_codepoint(handshaker.get(), use_new_alps_codepoint); |
| 216 |
SSL_CTX_set_alpn_select_cb( |
| 217 |
handshaker_ctx.get(), |
| 218 |
[](SSL *ssl, const uint8_t **out, uint8_t *out_len, const uint8_t *in, |
| 219 |
unsigned in_len, void *arg) -> int { |
| 220 |
return SSL_select_next_proto(const_cast<uint8_t **>(out), out_len, |
| 221 |
in, in_len, alpn, |
| 222 |
sizeof(alpn)) == OPENSSL_NPN_NEGOTIATED |
| 223 |
? SSL_TLSEXT_ERR_OK |
| 224 |
: SSL_TLSEXT_ERR_NOACK; |
| 225 |
}, |
| 226 |
nullptr); |
| 227 |
ASSERT_TRUE(SSL_add_application_settings( |
| 228 |
handshaker.get(), proto, sizeof(proto), alps, sizeof(alps))); |
| 229 |
|
| 230 |
ASSERT_TRUE(SSL_apply_handoff(handshaker.get(), handoff)); |
| 231 |
|
| 232 |
MoveBIOs(handshaker.get(), server.get()); |
| 233 |
|
| 234 |
int handshake_ret = SSL_do_handshake(handshaker.get()); |
| 235 |
int handshake_err = SSL_get_error(handshaker.get(), handshake_ret); |
| 236 |
ASSERT_EQ(handshake_err, SSL_ERROR_HANDBACK); |
| 237 |
|
| 238 |
// Double-check that additional calls to |SSL_do_handshake| continue |
| 239 |
// to get |SSL_ERROR_HANDBACK|. |
| 240 |
handshake_ret = SSL_do_handshake(handshaker.get()); |
| 241 |
handshake_err = SSL_get_error(handshaker.get(), handshake_ret); |
| 242 |
ASSERT_EQ(handshake_err, SSL_ERROR_HANDBACK); |
| 243 |
|
| 244 |
ScopedCBB cbb_handback; |
| 245 |
Array<uint8_t> handback; |
| 246 |
ASSERT_TRUE(CBB_init(cbb_handback.get(), 1024)); |
| 247 |
ASSERT_TRUE(SSL_serialize_handback(handshaker.get(), cbb_handback.get())); |
| 248 |
ASSERT_TRUE(CBBFinishArray(cbb_handback.get(), &handback)); |
| 249 |
|
| 250 |
bssl::UniquePtr<SSL> server2(SSL_new(server_ctx.get())); |
| 251 |
ASSERT_TRUE(server2); |
| 252 |
ASSERT_TRUE(SSL_apply_handback(server2.get(), handback)); |
| 253 |
|
| 254 |
MoveBIOs(server2.get(), handshaker.get()); |
| 255 |
ASSERT_TRUE(CompleteHandshakes(client.get(), server2.get())); |
| 256 |
EXPECT_EQ(is_resume, SSL_session_reused(client.get())); |
| 257 |
// Verify application settings. |
| 258 |
ASSERT_TRUE(SSL_has_application_settings(client.get())); |
| 259 |
|
| 260 |
if (early_data && is_resume) { |
| 261 |
// In this case, one byte of early data has already been written above. |
| 262 |
EXPECT_TRUE(SSL_early_data_accepted(client.get())); |
| 263 |
} else { |
| 264 |
byte_written = 42; |
| 265 |
EXPECT_EQ(SSL_write(client.get(), &byte_written, 1), 1); |
| 266 |
} |
| 267 |
uint8_t byte = 0; |
| 268 |
EXPECT_EQ(SSL_read(server2.get(), &byte, 1), 1); |
| 269 |
EXPECT_EQ(byte_written, byte); |
| 270 |
|
| 271 |
byte = 44; |
| 272 |
EXPECT_EQ(SSL_write(server2.get(), &byte, 1), 1); |
| 273 |
EXPECT_EQ(SSL_read(client.get(), &byte, 1), 1); |
| 274 |
EXPECT_EQ(44, byte); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
TEST(SSLTest, Handoff) { |
| 280 |
for (bool use_new_alps_codepoint : {false, true}) { |
| 281 |
SCOPED_TRACE(use_new_alps_codepoint); |
| 282 |
VerifyHandoff(use_new_alps_codepoint); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
} // namespace |
| 287 |
BSSL_NAMESPACE_END |