| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/base.h> |
| 5 |
|
| 6 |
#if !defined(OPENSSL_WINDOWS) |
| 7 |
#include <arpa/inet.h> |
| 8 |
#include <netinet/in.h> |
| 9 |
#include <netinet/tcp.h> |
| 10 |
#include <signal.h> |
| 11 |
#include <sys/socket.h> |
| 12 |
#include <sys/time.h> |
| 13 |
#include <unistd.h> |
| 14 |
#else |
| 15 |
#include <io.h> |
| 16 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 17 |
#include <winsock2.h> |
| 18 |
#include <ws2tcpip.h> |
| 19 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <assert.h> |
| 23 |
#include <errno.h> |
| 24 |
|
| 25 |
#ifndef __STDC_FORMAT_MACROS |
| 26 |
#define __STDC_FORMAT_MACROS |
| 27 |
#endif |
| 28 |
#include <inttypes.h> |
| 29 |
|
| 30 |
#include <string.h> |
| 31 |
#include <time.h> |
| 32 |
|
| 33 |
#include <openssl/aead.h> |
| 34 |
#include <openssl/bio.h> |
| 35 |
#include <openssl/bytestring.h> |
| 36 |
#include <openssl/cipher.h> |
| 37 |
#include <openssl/crypto.h> |
| 38 |
#include <openssl/digest.h> |
| 39 |
#include <openssl/err.h> |
| 40 |
#include <openssl/evp.h> |
| 41 |
#include <openssl/hmac.h> |
| 42 |
#include <openssl/nid.h> |
| 43 |
#include <openssl/rand.h> |
| 44 |
#include <openssl/ssl.h> |
| 45 |
#include <openssl/x509.h> |
| 46 |
|
| 47 |
#include <functional> |
| 48 |
#include <memory> |
| 49 |
#include <string> |
| 50 |
#include <vector> |
| 51 |
|
| 52 |
#include "../../crypto/internal.h" |
| 53 |
#include "../../crypto/ube/vm_ube_detect.h" |
| 54 |
#include "../internal.h" |
| 55 |
#include "async_bio.h" |
| 56 |
#include "handshake_util.h" |
| 57 |
#include "mock_quic_transport.h" |
| 58 |
#include "packeted_bio.h" |
| 59 |
#include "settings_writer.h" |
| 60 |
#include "ssl_transfer.h" |
| 61 |
#include "test_config.h" |
| 62 |
#include "test_state.h" |
| 63 |
|
| 64 |
|
| 65 |
#if !defined(OPENSSL_WINDOWS) |
| 66 |
using Socket = int; |
| 67 |
#define INVALID_SOCKET (-1) |
| 68 |
|
| 69 |
static int closesocket(int sock) { return close(sock); } |
| 70 |
static void PrintSocketError(const char *func) { perror(func); } |
| 71 |
#else |
| 72 |
using Socket = SOCKET; |
| 73 |
|
| 74 |
static void PrintSocketError(const char *func) { |
| 75 |
int error = WSAGetLastError(); |
| 76 |
char *buffer; |
| 77 |
DWORD len = FormatMessageA( |
| 78 |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 0, error, 0, |
| 79 |
reinterpret_cast<char *>(&buffer), 0, nullptr); |
| 80 |
std::string msg = "unknown error"; |
| 81 |
if (len > 0) { |
| 82 |
msg.assign(buffer, len); |
| 83 |
while (!msg.empty() && (msg.back() == '\r' || msg.back() == '\n')) { |
| 84 |
msg.resize(msg.size() - 1); |
| 85 |
} |
| 86 |
} |
| 87 |
LocalFree(buffer); |
| 88 |
fprintf(stderr, "%s: %s (%d)\n", func, msg.c_str(), error); |
| 89 |
} |
| 90 |
#endif |
| 91 |
|
| 92 |
class OwnedSocket { |
| 93 |
public: |
| 94 |
OwnedSocket() = default; |
| 95 |
explicit OwnedSocket(Socket sock) : sock_(sock) {} |
| 96 |
OwnedSocket(OwnedSocket &&other) { *this = std::move(other); } |
| 97 |
~OwnedSocket() { reset(); } |
| 98 |
OwnedSocket &operator=(OwnedSocket &&other) { |
| 99 |
if (this != &other) { |
| 100 |
drain_on_close_ = other.drain_on_close_; |
| 101 |
reset(other.release()); |
| 102 |
} |
| 103 |
return *this; |
| 104 |
} |
| 105 |
|
| 106 |
bool is_valid() const { return sock_ != INVALID_SOCKET; } |
| 107 |
void set_drain_on_close(bool drain) { drain_on_close_ = drain; } |
| 108 |
|
| 109 |
void reset(Socket sock = INVALID_SOCKET) { |
| 110 |
if (is_valid()) { |
| 111 |
if (drain_on_close_) { |
| 112 |
#if defined(OPENSSL_WINDOWS) |
| 113 |
shutdown(sock_, SD_SEND); |
| 114 |
#else |
| 115 |
shutdown(sock_, SHUT_WR); |
| 116 |
#endif |
| 117 |
while (true) { |
| 118 |
char buf[1024]; |
| 119 |
if (recv(sock_, buf, sizeof(buf), 0) <= 0) { |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
closesocket(sock_); |
| 125 |
} |
| 126 |
|
| 127 |
drain_on_close_ = false; |
| 128 |
sock_ = sock; |
| 129 |
} |
| 130 |
|
| 131 |
Socket get() const { return sock_; } |
| 132 |
|
| 133 |
Socket release() { |
| 134 |
Socket sock = sock_; |
| 135 |
sock_ = INVALID_SOCKET; |
| 136 |
drain_on_close_ = false; |
| 137 |
return sock; |
| 138 |
} |
| 139 |
|
| 140 |
private: |
| 141 |
Socket sock_ = INVALID_SOCKET; |
| 142 |
bool drain_on_close_ = false; |
| 143 |
}; |
| 144 |
|
| 145 |
static int Usage(const char *program) { |
| 146 |
fprintf(stderr, "Usage: %s [flags...]\n", program); |
| 147 |
return 1; |
| 148 |
} |
| 149 |
|
| 150 |
template <typename T> |
| 151 |
struct Free { |
| 152 |
void operator()(T *buf) { free(buf); } |
| 153 |
}; |
| 154 |
|
| 155 |
// Connect returns a new socket connected to the runner, or -1 on error. |
| 156 |
static OwnedSocket Connect(const TestConfig *config) { |
| 157 |
sockaddr_storage addr; |
| 158 |
socklen_t addr_len = 0; |
| 159 |
if (config->ipv6) { |
| 160 |
sockaddr_in6 sin6; |
| 161 |
OPENSSL_memset(&sin6, 0, sizeof(sin6)); |
| 162 |
sin6.sin6_family = AF_INET6; |
| 163 |
sin6.sin6_port = htons(config->port); |
| 164 |
if (!inet_pton(AF_INET6, "::1", &sin6.sin6_addr)) { |
| 165 |
PrintSocketError("inet_pton"); |
| 166 |
return OwnedSocket(); |
| 167 |
} |
| 168 |
addr_len = sizeof(sin6); |
| 169 |
memcpy(&addr, &sin6, addr_len); |
| 170 |
} else { |
| 171 |
sockaddr_in sin; |
| 172 |
OPENSSL_memset(&sin, 0, sizeof(sin)); |
| 173 |
sin.sin_family = AF_INET; |
| 174 |
sin.sin_port = htons(config->port); |
| 175 |
if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) { |
| 176 |
PrintSocketError("inet_pton"); |
| 177 |
return OwnedSocket(); |
| 178 |
} |
| 179 |
addr_len = sizeof(sin); |
| 180 |
memcpy(&addr, &sin, addr_len); |
| 181 |
} |
| 182 |
|
| 183 |
OwnedSocket sock(socket(addr.ss_family, SOCK_STREAM, 0)); |
| 184 |
if (!sock.is_valid()) { |
| 185 |
PrintSocketError("socket"); |
| 186 |
return OwnedSocket(); |
| 187 |
} |
| 188 |
int nodelay = 1; |
| 189 |
if (setsockopt(sock.get(), IPPROTO_TCP, TCP_NODELAY, |
| 190 |
reinterpret_cast<const char *>(&nodelay), |
| 191 |
sizeof(nodelay)) != 0) { |
| 192 |
PrintSocketError("setsockopt"); |
| 193 |
return OwnedSocket(); |
| 194 |
} |
| 195 |
|
| 196 |
if (connect(sock.get(), reinterpret_cast<const sockaddr *>(&addr), |
| 197 |
addr_len) != 0) { |
| 198 |
PrintSocketError("connect"); |
| 199 |
return OwnedSocket(); |
| 200 |
} |
| 201 |
|
| 202 |
return sock; |
| 203 |
} |
| 204 |
|
| 205 |
static bool TransferSSL(const TestConfig *config, bssl::UniquePtr<SSL> *in, |
| 206 |
SSL **ssl) { |
| 207 |
if (!config || !in || !ssl) { |
| 208 |
// No ssl transfer logic is executed. |
| 209 |
return true; |
| 210 |
} |
| 211 |
SSLTransfer sslTransfer; |
| 212 |
if (!sslTransfer.MarkOrReset(config, in)) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
*ssl = in->get(); |
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
// DoRead reads from |ssl|, resolving any asynchronous operations. It returns |
| 220 |
// the result value of the final |SSL_read| call. |
| 221 |
static int DoRead(bssl::UniquePtr<SSL> *in, uint8_t *out, size_t max_out) { |
| 222 |
SSL *ssl = in->get(); |
| 223 |
const TestConfig *config = GetTestConfig(ssl); |
| 224 |
TestState *test_state = GetTestState(ssl); |
| 225 |
if (test_state->quic_transport) { |
| 226 |
return test_state->quic_transport->ReadApplicationData(out, max_out); |
| 227 |
} |
| 228 |
int ret; |
| 229 |
do { |
| 230 |
if (config->async) { |
| 231 |
// The DTLS retransmit logic silently ignores write failures. So the test |
| 232 |
// may progress, allow writes through synchronously. |SSL_read| may |
| 233 |
// trigger a retransmit, so disconnect the write quota. |
| 234 |
AsyncBioEnforceWriteQuota(test_state->async_bio, false); |
| 235 |
} |
| 236 |
ret = CheckIdempotentError("SSL_peek/SSL_read", ssl, [&]() -> int { |
| 237 |
return config->peek_then_read ? SSL_peek(ssl, out, max_out) |
| 238 |
: SSL_read(ssl, out, max_out); |
| 239 |
}); |
| 240 |
if (config->async) { |
| 241 |
AsyncBioEnforceWriteQuota(test_state->async_bio, true); |
| 242 |
} |
| 243 |
|
| 244 |
// Run the exporter after each read. This is to test that the exporter fails |
| 245 |
// during a renegotiation. |
| 246 |
if (config->use_exporter_between_reads) { |
| 247 |
uint8_t buf; |
| 248 |
if (!SSL_export_keying_material(ssl, &buf, 1, NULL, 0, NULL, 0, 0)) { |
| 249 |
fprintf(stderr, "failed to export keying material\n"); |
| 250 |
return -1; |
| 251 |
} |
| 252 |
} |
| 253 |
} while (RetryAsync(ssl, ret)); |
| 254 |
|
| 255 |
if (!TransferSSL(config, in, &ssl)) { |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
if (config->peek_then_read && ret > 0) { |
| 260 |
std::unique_ptr<uint8_t[]> buf(new uint8_t[static_cast<size_t>(ret)]); |
| 261 |
|
| 262 |
// SSL_peek should synchronously return the same data. |
| 263 |
int ret2 = SSL_peek(ssl, buf.get(), ret); |
| 264 |
if (ret2 != ret || OPENSSL_memcmp(buf.get(), out, ret) != 0) { |
| 265 |
fprintf(stderr, "First and second SSL_peek did not match.\n"); |
| 266 |
return -1; |
| 267 |
} |
| 268 |
|
| 269 |
// Do transfer again to test SSL_peek. |
| 270 |
if (!TransferSSL(config, in, &ssl)) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
// SSL_read should synchronously return the same data and consume it. |
| 275 |
ret2 = SSL_read(ssl, buf.get(), ret); |
| 276 |
if (ret2 != ret || OPENSSL_memcmp(buf.get(), out, ret) != 0) { |
| 277 |
fprintf(stderr, "SSL_peek and SSL_read did not match.\n"); |
| 278 |
return -1; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return ret; |
| 283 |
} |
| 284 |
|
| 285 |
// WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous |
| 286 |
// operations. It returns the result of the final |SSL_write| call. |
| 287 |
static int WriteAll(SSL *ssl, const void *in_, size_t in_len) { |
| 288 |
TestState *test_state = GetTestState(ssl); |
| 289 |
const uint8_t *in = reinterpret_cast<const uint8_t *>(in_); |
| 290 |
if (test_state->quic_transport) { |
| 291 |
if (!test_state->quic_transport->WriteApplicationData(in, in_len)) { |
| 292 |
return -1; |
| 293 |
} |
| 294 |
return in_len; |
| 295 |
} |
| 296 |
int ret; |
| 297 |
do { |
| 298 |
ret = SSL_write(ssl, in, in_len); |
| 299 |
if (ret > 0) { |
| 300 |
in += ret; |
| 301 |
in_len -= ret; |
| 302 |
} |
| 303 |
} while (RetryAsync(ssl, ret) || (ret > 0 && in_len > 0)); |
| 304 |
return ret; |
| 305 |
} |
| 306 |
|
| 307 |
// DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It |
| 308 |
// returns the result of the final |SSL_shutdown| call. |
| 309 |
static int DoShutdown(SSL *ssl) { |
| 310 |
int ret; |
| 311 |
do { |
| 312 |
ret = SSL_shutdown(ssl); |
| 313 |
} while (RetryAsync(ssl, ret)); |
| 314 |
return ret; |
| 315 |
} |
| 316 |
|
| 317 |
// DoSendFatalAlert calls |SSL_send_fatal_alert|, resolving any asynchronous |
| 318 |
// operations. It returns the result of the final |SSL_send_fatal_alert| call. |
| 319 |
static int DoSendFatalAlert(SSL *ssl, uint8_t alert) { |
| 320 |
int ret; |
| 321 |
do { |
| 322 |
ret = SSL_send_fatal_alert(ssl, alert); |
| 323 |
} while (RetryAsync(ssl, ret)); |
| 324 |
return ret; |
| 325 |
} |
| 326 |
|
| 327 |
static uint16_t GetProtocolVersion(const SSL *ssl) { |
| 328 |
uint16_t version = SSL_version(ssl); |
| 329 |
if (!SSL_is_dtls(ssl)) { |
| 330 |
return version; |
| 331 |
} |
| 332 |
return 0x0201 + ~version; |
| 333 |
} |
| 334 |
|
| 335 |
static bool CheckListContains(const char *type, |
| 336 |
size_t (*list_func)(const char **, size_t), |
| 337 |
const char *str) { |
| 338 |
std::vector<const char *> list(list_func(nullptr, 0)); |
| 339 |
list_func(list.data(), list.size()); |
| 340 |
for (const char *expected : list) { |
| 341 |
if (str == nullptr) { |
| 342 |
break; |
| 343 |
} |
| 344 |
if (strcmp(expected, str) == 0) { |
| 345 |
return true; |
| 346 |
} |
| 347 |
} |
| 348 |
fprintf(stderr, "Unexpected %s: %s\n", type, (str == nullptr) ? "<null>" : str); |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
// CheckAuthProperties checks, after the initial handshake is completed or |
| 353 |
// after a renegotiation, that authentication-related properties match |config|. |
| 354 |
static bool CheckAuthProperties(SSL *ssl, bool is_resume, |
| 355 |
const TestConfig *config) { |
| 356 |
if (!config->expect_ocsp_response.empty()) { |
| 357 |
const uint8_t *data; |
| 358 |
size_t len; |
| 359 |
SSL_get0_ocsp_response(ssl, &data, &len); |
| 360 |
if (config->expect_ocsp_response.size() != len || |
| 361 |
OPENSSL_memcmp(config->expect_ocsp_response.data(), data, len) != 0) { |
| 362 |
fprintf(stderr, "OCSP response mismatch\n"); |
| 363 |
return false; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
if (!config->expect_signed_cert_timestamps.empty()) { |
| 368 |
const uint8_t *data; |
| 369 |
size_t len; |
| 370 |
SSL_get0_signed_cert_timestamp_list(ssl, &data, &len); |
| 371 |
if (config->expect_signed_cert_timestamps.size() != len || |
| 372 |
OPENSSL_memcmp(config->expect_signed_cert_timestamps.data(), data, |
| 373 |
len) != 0) { |
| 374 |
fprintf(stderr, "SCT list mismatch\n"); |
| 375 |
return false; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
if (config->expect_verify_result) { |
| 380 |
int expected_verify_result = |
| 381 |
config->verify_fail ? X509_V_ERR_APPLICATION_VERIFICATION : X509_V_OK; |
| 382 |
|
| 383 |
if (SSL_get_verify_result(ssl) != expected_verify_result) { |
| 384 |
fprintf(stderr, "Wrong certificate verification result\n"); |
| 385 |
return false; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
if (!config->expect_peer_cert_file.empty()) { |
| 390 |
bssl::UniquePtr<X509> expect_leaf; |
| 391 |
bssl::UniquePtr<STACK_OF(X509)> expect_chain; |
| 392 |
if (!LoadCertificate(&expect_leaf, &expect_chain, |
| 393 |
config->expect_peer_cert_file)) { |
| 394 |
return false; |
| 395 |
} |
| 396 |
|
| 397 |
// For historical reasons, clients report a chain with a leaf and servers |
| 398 |
// without. |
| 399 |
if (!config->is_server) { |
| 400 |
if (!sk_X509_insert(expect_chain.get(), expect_leaf.get(), 0)) { |
| 401 |
return false; |
| 402 |
} |
| 403 |
X509_up_ref(expect_leaf.get()); // sk_X509_insert takes ownership. |
| 404 |
} |
| 405 |
|
| 406 |
bssl::UniquePtr<X509> leaf(SSL_get_peer_certificate(ssl)); |
| 407 |
STACK_OF(X509) *chain = SSL_get_peer_cert_chain(ssl); |
| 408 |
if (X509_cmp(leaf.get(), expect_leaf.get()) != 0) { |
| 409 |
fprintf(stderr, "Received a different leaf certificate than expected.\n"); |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
if (sk_X509_num(chain) != sk_X509_num(expect_chain.get())) { |
| 414 |
fprintf(stderr, "Received a chain of length %zu instead of %zu.\n", |
| 415 |
sk_X509_num(chain), sk_X509_num(expect_chain.get())); |
| 416 |
return false; |
| 417 |
} |
| 418 |
|
| 419 |
for (size_t i = 0; i < sk_X509_num(chain); i++) { |
| 420 |
if (X509_cmp(sk_X509_value(chain, i), |
| 421 |
sk_X509_value(expect_chain.get(), i)) != 0) { |
| 422 |
fprintf(stderr, "Chain certificate %zu did not match.\n", i + 1); |
| 423 |
return false; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
if (!!SSL_SESSION_has_peer_sha256(SSL_get_session(ssl)) != |
| 429 |
config->expect_sha256_client_cert) { |
| 430 |
fprintf(stderr, |
| 431 |
"Unexpected SHA-256 client cert state: expected:%d is_resume:%d.\n", |
| 432 |
config->expect_sha256_client_cert, is_resume); |
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
if (config->expect_sha256_client_cert && |
| 437 |
SSL_SESSION_get0_peer_certificates(SSL_get_session(ssl)) != nullptr) { |
| 438 |
fprintf(stderr, "Have both client cert and SHA-256 hash: is_resume:%d.\n", |
| 439 |
is_resume); |
| 440 |
return false; |
| 441 |
} |
| 442 |
|
| 443 |
const uint8_t *peer_sha256; |
| 444 |
size_t peer_sha256_len; |
| 445 |
SSL_SESSION_get0_peer_sha256(SSL_get_session(ssl), &peer_sha256, |
| 446 |
&peer_sha256_len); |
| 447 |
if (SSL_SESSION_has_peer_sha256(SSL_get_session(ssl))) { |
| 448 |
if (peer_sha256_len != 32) { |
| 449 |
fprintf(stderr, "Peer SHA-256 hash had length %zu instead of 32\n", |
| 450 |
peer_sha256_len); |
| 451 |
return false; |
| 452 |
} |
| 453 |
} else { |
| 454 |
if (peer_sha256_len != 0) { |
| 455 |
fprintf(stderr, "Unexpected peer SHA-256 hash of length %zu\n", |
| 456 |
peer_sha256_len); |
| 457 |
return false; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
return true; |
| 462 |
} |
| 463 |
|
| 464 |
// CheckHandshakeProperties checks, immediately after |ssl| completes its |
| 465 |
// initial handshake (or False Starts), whether all the properties are |
| 466 |
// consistent with the test configuration and invariants. |
| 467 |
static bool CheckHandshakeProperties(SSL *ssl, bool is_resume, |
| 468 |
const TestConfig *config) { |
| 469 |
if (!CheckAuthProperties(ssl, is_resume, config)) { |
| 470 |
return false; |
| 471 |
} |
| 472 |
|
| 473 |
if (SSL_get_current_cipher(ssl) == nullptr) { |
| 474 |
fprintf(stderr, "null cipher after handshake\n"); |
| 475 |
return false; |
| 476 |
} |
| 477 |
|
| 478 |
if (config->expect_version != 0 && |
| 479 |
SSL_version(ssl) != int{config->expect_version}) { |
| 480 |
uint16_t actual_version = static_cast<uint16_t>(SSL_version(ssl)); |
| 481 |
fprintf(stderr, "want version %04x, got %04x\n", config->expect_version, |
| 482 |
actual_version); |
| 483 |
return false; |
| 484 |
} |
| 485 |
|
| 486 |
bool expect_resume = |
| 487 |
is_resume && (!config->expect_session_miss || SSL_in_early_data(ssl)); |
| 488 |
if (!!SSL_session_reused(ssl) != expect_resume) { |
| 489 |
fprintf(stderr, "session unexpectedly was%s reused\n", |
| 490 |
SSL_session_reused(ssl) ? "" : " not"); |
| 491 |
return false; |
| 492 |
} |
| 493 |
|
| 494 |
bool expect_handshake_done = |
| 495 |
(is_resume || !config->false_start) && !SSL_in_early_data(ssl); |
| 496 |
if (expect_handshake_done != GetTestState(ssl)->handshake_done) { |
| 497 |
fprintf(stderr, "handshake was%s completed\n", |
| 498 |
GetTestState(ssl)->handshake_done ? "" : " not"); |
| 499 |
return false; |
| 500 |
} |
| 501 |
|
| 502 |
if (expect_handshake_done && !config->is_server) { |
| 503 |
bool expect_new_session = |
| 504 |
!config->expect_no_session && |
| 505 |
(!SSL_session_reused(ssl) || config->expect_ticket_renewal) && |
| 506 |
// Session tickets are sent post-handshake in TLS 1.3. |
| 507 |
GetProtocolVersion(ssl) < TLS1_3_VERSION; |
| 508 |
if (expect_new_session != GetTestState(ssl)->got_new_session) { |
| 509 |
fprintf(stderr, |
| 510 |
"new session was%s cached, but we expected the opposite\n", |
| 511 |
GetTestState(ssl)->got_new_session ? "" : " not"); |
| 512 |
return false; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
if (!is_resume) { |
| 517 |
if (config->expect_session_id && !GetTestState(ssl)->got_new_session) { |
| 518 |
fprintf(stderr, "session was not cached on the server.\n"); |
| 519 |
return false; |
| 520 |
} |
| 521 |
if (config->expect_no_session_id && GetTestState(ssl)->got_new_session) { |
| 522 |
fprintf(stderr, "session was unexpectedly cached on the server.\n"); |
| 523 |
return false; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
// early_callback_called is updated in the handshaker, so we don't see it |
| 528 |
// here. |
| 529 |
if (!config->handoff && config->is_server && |
| 530 |
!GetTestState(ssl)->early_callback_called) { |
| 531 |
fprintf(stderr, "early callback not called\n"); |
| 532 |
return false; |
| 533 |
} |
| 534 |
|
| 535 |
if (!config->expect_server_name.empty()) { |
| 536 |
const char *server_name = |
| 537 |
SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
| 538 |
if (server_name == nullptr || server_name != config->expect_server_name) { |
| 539 |
fprintf(stderr, "servername mismatch (got %s; want %s)\n", server_name, |
| 540 |
config->expect_server_name.c_str()); |
| 541 |
return false; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
if (!config->expect_next_proto.empty()) { |
| 546 |
const uint8_t *next_proto; |
| 547 |
unsigned next_proto_len; |
| 548 |
SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len); |
| 549 |
if (next_proto_len != config->expect_next_proto.size() || |
| 550 |
OPENSSL_memcmp(next_proto, config->expect_next_proto.data(), |
| 551 |
next_proto_len) != 0) { |
| 552 |
fprintf(stderr, "negotiated next proto mismatch\n"); |
| 553 |
return false; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
// On the server, the protocol selected in the ALPN callback must be echoed |
| 558 |
// out of |SSL_get0_alpn_selected|. On the client, it should report what the |
| 559 |
// test expected. |
| 560 |
const std::string &expect_alpn = |
| 561 |
config->is_server ? config->select_alpn : config->expect_alpn; |
| 562 |
const uint8_t *alpn_proto; |
| 563 |
unsigned alpn_proto_len; |
| 564 |
SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len); |
| 565 |
if (alpn_proto_len != expect_alpn.size() || |
| 566 |
OPENSSL_memcmp(alpn_proto, expect_alpn.data(), alpn_proto_len) != 0) { |
| 567 |
fprintf(stderr, "negotiated alpn proto mismatch\n"); |
| 568 |
return false; |
| 569 |
} |
| 570 |
|
| 571 |
if (SSL_has_application_settings(ssl) != |
| 572 |
(config->expect_peer_application_settings ? 1 : 0)) { |
| 573 |
fprintf(stderr, |
| 574 |
"connection %s application settings, but expected the opposite\n", |
| 575 |
SSL_has_application_settings(ssl) ? "has" : "does not have"); |
| 576 |
return false; |
| 577 |
} |
| 578 |
std::string expect_settings = config->expect_peer_application_settings |
| 579 |
? *config->expect_peer_application_settings |
| 580 |
: ""; |
| 581 |
const uint8_t *peer_settings; |
| 582 |
size_t peer_settings_len; |
| 583 |
SSL_get0_peer_application_settings(ssl, &peer_settings, &peer_settings_len); |
| 584 |
if (expect_settings != |
| 585 |
std::string(reinterpret_cast<const char *>(peer_settings), |
| 586 |
peer_settings_len)) { |
| 587 |
fprintf(stderr, "peer application settings mismatch\n"); |
| 588 |
return false; |
| 589 |
} |
| 590 |
|
| 591 |
if (!config->expect_quic_transport_params.empty() && expect_handshake_done) { |
| 592 |
const uint8_t *peer_params; |
| 593 |
size_t peer_params_len; |
| 594 |
SSL_get_peer_quic_transport_params(ssl, &peer_params, &peer_params_len); |
| 595 |
if (peer_params_len != config->expect_quic_transport_params.size() || |
| 596 |
OPENSSL_memcmp(peer_params, config->expect_quic_transport_params.data(), |
| 597 |
peer_params_len) != 0) { |
| 598 |
fprintf(stderr, "QUIC transport params mismatch\n"); |
| 599 |
return false; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
if (!config->expect_channel_id.empty()) { |
| 604 |
uint8_t channel_id[64]; |
| 605 |
if (!SSL_get_tls_channel_id(ssl, channel_id, sizeof(channel_id))) { |
| 606 |
fprintf(stderr, "no channel id negotiated\n"); |
| 607 |
return false; |
| 608 |
} |
| 609 |
if (config->expect_channel_id.size() != 64 || |
| 610 |
OPENSSL_memcmp(config->expect_channel_id.data(), channel_id, 64) != 0) { |
| 611 |
fprintf(stderr, "channel id mismatch\n"); |
| 612 |
return false; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
if (config->expect_extended_master_secret && !SSL_get_extms_support(ssl)) { |
| 617 |
fprintf(stderr, "No EMS for connection when expected\n"); |
| 618 |
return false; |
| 619 |
} |
| 620 |
|
| 621 |
if (config->expect_secure_renegotiation && |
| 622 |
!SSL_get_secure_renegotiation_support(ssl)) { |
| 623 |
fprintf(stderr, "No secure renegotiation for connection when expected\n"); |
| 624 |
return false; |
| 625 |
} |
| 626 |
|
| 627 |
if (config->expect_no_secure_renegotiation && |
| 628 |
SSL_get_secure_renegotiation_support(ssl)) { |
| 629 |
fprintf(stderr, |
| 630 |
"Secure renegotiation unexpectedly negotiated for connection\n"); |
| 631 |
return false; |
| 632 |
} |
| 633 |
|
| 634 |
if (config->expect_peer_signature_algorithm != 0 && |
| 635 |
config->expect_peer_signature_algorithm != |
| 636 |
SSL_get_peer_signature_algorithm(ssl)) { |
| 637 |
fprintf(stderr, "Peer signature algorithm was %04x, wanted %04x.\n", |
| 638 |
SSL_get_peer_signature_algorithm(ssl), |
| 639 |
config->expect_peer_signature_algorithm); |
| 640 |
return false; |
| 641 |
} |
| 642 |
|
| 643 |
if (config->expect_curve_id != 0) { |
| 644 |
uint16_t curve_id = SSL_get_curve_id(ssl); |
| 645 |
if (config->expect_curve_id != curve_id) { |
| 646 |
fprintf(stderr, "curve_id was %04x, wanted %04x\n", curve_id, |
| 647 |
config->expect_curve_id); |
| 648 |
return false; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
uint16_t cipher_id = SSL_CIPHER_get_protocol_id(SSL_get_current_cipher(ssl)); |
| 653 |
if (!config->tls13_ciphersuites.empty() && |
| 654 |
config->expect_cipher != cipher_id) { |
| 655 |
fprintf(stderr, "Cipher ID was %04x, wanted %04x\n", cipher_id, |
| 656 |
config->expect_cipher_aes); |
| 657 |
return false; |
| 658 |
} |
| 659 |
|
| 660 |
if (config->tls13_ciphersuites.empty() && config->expect_cipher_aes != 0 && |
| 661 |
EVP_has_aes_hardware() && config->expect_cipher_aes != cipher_id) { |
| 662 |
fprintf(stderr, "Cipher ID was %04x, wanted %04x (has AES hardware)\n", |
| 663 |
cipher_id, config->expect_cipher_aes); |
| 664 |
return false; |
| 665 |
} |
| 666 |
|
| 667 |
if (config->tls13_ciphersuites.empty() && config->expect_cipher_no_aes != 0 && |
| 668 |
!EVP_has_aes_hardware() && config->expect_cipher_no_aes != cipher_id) { |
| 669 |
fprintf(stderr, "Cipher ID was %04x, wanted %04x (no AES hardware)\n", |
| 670 |
cipher_id, config->expect_cipher_no_aes); |
| 671 |
return false; |
| 672 |
} |
| 673 |
|
| 674 |
if (config->expect_cipher != 0 && config->expect_cipher != cipher_id) { |
| 675 |
fprintf(stderr, "Cipher ID was %04x, wanted %04x\n", cipher_id, |
| 676 |
config->expect_cipher); |
| 677 |
return false; |
| 678 |
} |
| 679 |
|
| 680 |
// The early data status is only applicable after the handshake is confirmed. |
| 681 |
if (!SSL_in_early_data(ssl)) { |
| 682 |
if ((config->expect_accept_early_data && !SSL_early_data_accepted(ssl)) || |
| 683 |
(config->expect_reject_early_data && SSL_early_data_accepted(ssl))) { |
| 684 |
fprintf(stderr, |
| 685 |
"Early data was%s accepted, but we expected the opposite\n", |
| 686 |
SSL_early_data_accepted(ssl) ? "" : " not"); |
| 687 |
return false; |
| 688 |
} |
| 689 |
|
| 690 |
const char *early_data_reason = |
| 691 |
SSL_early_data_reason_string(SSL_get_early_data_reason(ssl)); |
| 692 |
if (!config->expect_early_data_reason.empty() && |
| 693 |
config->expect_early_data_reason != early_data_reason) { |
| 694 |
fprintf(stderr, "Early data reason was \"%s\", expected \"%s\"\n", |
| 695 |
early_data_reason, config->expect_early_data_reason.c_str()); |
| 696 |
return false; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
if (!config->psk.empty()) { |
| 701 |
if (SSL_get_peer_cert_chain(ssl) != nullptr) { |
| 702 |
fprintf(stderr, "Received peer certificate on a PSK cipher.\n"); |
| 703 |
return false; |
| 704 |
} |
| 705 |
} else if (!config->is_server || config->require_any_client_certificate) { |
| 706 |
if (SSL_get_peer_cert_chain(ssl) == nullptr) { |
| 707 |
fprintf(stderr, "Received no peer certificate but expected one.\n"); |
| 708 |
return false; |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
if (is_resume && config->expect_ticket_age_skew != 0 && |
| 713 |
SSL_get_ticket_age_skew(ssl) != config->expect_ticket_age_skew) { |
| 714 |
fprintf(stderr, "Ticket age skew was %" PRId32 ", wanted %d\n", |
| 715 |
SSL_get_ticket_age_skew(ssl), config->expect_ticket_age_skew); |
| 716 |
return false; |
| 717 |
} |
| 718 |
|
| 719 |
if (config->expect_delegated_credential_used != |
| 720 |
!!SSL_delegated_credential_used(ssl)) { |
| 721 |
fprintf(stderr, |
| 722 |
"Got %s delegated credential usage, but wanted opposite. \n", |
| 723 |
SSL_delegated_credential_used(ssl) ? "" : "no"); |
| 724 |
return false; |
| 725 |
} |
| 726 |
|
| 727 |
if ((config->expect_hrr && !SSL_used_hello_retry_request(ssl)) || |
| 728 |
(config->expect_no_hrr && SSL_used_hello_retry_request(ssl))) { |
| 729 |
fprintf(stderr, "Got %sHRR, but wanted opposite.\n", |
| 730 |
SSL_used_hello_retry_request(ssl) ? "" : "no "); |
| 731 |
return false; |
| 732 |
} |
| 733 |
|
| 734 |
if (config->expect_ech_accept != !!SSL_ech_accepted(ssl)) { |
| 735 |
fprintf(stderr, "ECH was %saccepted, but wanted opposite.\n", |
| 736 |
SSL_ech_accepted(ssl) ? "" : "not "); |
| 737 |
return false; |
| 738 |
} |
| 739 |
|
| 740 |
if (config->expect_key_usage_invalid != !!SSL_was_key_usage_invalid(ssl)) { |
| 741 |
fprintf(stderr, "X.509 key usage was %svalid, but wanted opposite.\n", |
| 742 |
SSL_was_key_usage_invalid(ssl) ? "in" : ""); |
| 743 |
return false; |
| 744 |
} |
| 745 |
|
| 746 |
// Check all the selected parameters are covered by the string APIs. |
| 747 |
if (!CheckListContains("version", SSL_get_all_version_names, |
| 748 |
SSL_get_version(ssl)) || |
| 749 |
!CheckListContains( |
| 750 |
"cipher", SSL_get_all_standard_cipher_names, |
| 751 |
SSL_CIPHER_standard_name(SSL_get_current_cipher(ssl))) || |
| 752 |
!CheckListContains("OpenSSL cipher name", SSL_get_all_cipher_names, |
| 753 |
SSL_CIPHER_get_name(SSL_get_current_cipher(ssl))) || |
| 754 |
(SSL_get_group_id(ssl) != 0 && |
| 755 |
!CheckListContains("group", SSL_get_all_group_names, |
| 756 |
SSL_get_group_name(SSL_get_group_id(ssl)))) || |
| 757 |
(SSL_get_peer_signature_algorithm(ssl) != 0 && |
| 758 |
!CheckListContains( |
| 759 |
"sigalg", SSL_get_all_signature_algorithm_names, |
| 760 |
SSL_get_signature_algorithm_name( |
| 761 |
SSL_get_peer_signature_algorithm(ssl), /*include_curve=*/0))) || |
| 762 |
(SSL_get_peer_signature_algorithm(ssl) != 0 && |
| 763 |
!CheckListContains( |
| 764 |
"sigalg with curve", SSL_get_all_signature_algorithm_names, |
| 765 |
SSL_get_signature_algorithm_name( |
| 766 |
SSL_get_peer_signature_algorithm(ssl), /*include_curve=*/1)))) { |
| 767 |
return false; |
| 768 |
} |
| 769 |
|
| 770 |
// Test that handshake hints correctly skipped the expected operations. |
| 771 |
if (config->handshake_hints && !config->allow_hint_mismatch) { |
| 772 |
const TestState *state = GetTestState(ssl); |
| 773 |
// If the private key operation is performed in the first roundtrip, a hint |
| 774 |
// match should have skipped it. This is ECDHE-based cipher suites in TLS |
| 775 |
// 1.2 and non-HRR handshakes in TLS 1.3. |
| 776 |
bool private_key_allowed; |
| 777 |
if (SSL_version(ssl) == TLS1_3_VERSION) { |
| 778 |
private_key_allowed = SSL_used_hello_retry_request(ssl); |
| 779 |
} else { |
| 780 |
private_key_allowed = |
| 781 |
SSL_CIPHER_get_kx_nid(SSL_get_current_cipher(ssl)) == NID_kx_rsa; |
| 782 |
} |
| 783 |
if (!private_key_allowed && state->used_private_key) { |
| 784 |
fprintf( |
| 785 |
stderr, |
| 786 |
"Performed private key operation, but hint should have skipped it\n"); |
| 787 |
return false; |
| 788 |
} |
| 789 |
|
| 790 |
if (state->ticket_decrypt_done) { |
| 791 |
fprintf(stderr, |
| 792 |
"Performed ticket decryption, but hint should have skipped it\n"); |
| 793 |
return false; |
| 794 |
} |
| 795 |
|
| 796 |
// TODO(davidben): Decide what we want to do with TLS 1.2 stateful |
| 797 |
// resumption. |
| 798 |
} |
| 799 |
return true; |
| 800 |
} |
| 801 |
|
| 802 |
static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session, |
| 803 |
bssl::UniquePtr<SSL> *ssl_uniqueptr, |
| 804 |
const TestConfig *config, bool is_resume, bool is_retry, |
| 805 |
SettingsWriter *writer); |
| 806 |
|
| 807 |
// DoConnection tests an SSL connection against the peer. On success, it returns |
| 808 |
// true and sets |*out_session| to the negotiated SSL session. If the test is a |
| 809 |
// resumption attempt, |is_resume| is true and |session| is the session from the |
| 810 |
// previous exchange. |
| 811 |
static bool DoConnection(bssl::UniquePtr<SSL_SESSION> *out_session, |
| 812 |
SSL_CTX *ssl_ctx, const TestConfig *config, |
| 813 |
const TestConfig *retry_config, bool is_resume, |
| 814 |
SSL_SESSION *session, SettingsWriter *writer) { |
| 815 |
bssl::UniquePtr<SSL> ssl = config->NewSSL( |
| 816 |
ssl_ctx, session, std::unique_ptr<TestState>(new TestState)); |
| 817 |
if (!ssl) { |
| 818 |
return false; |
| 819 |
} |
| 820 |
if (config->is_server) { |
| 821 |
SSL_set_accept_state(ssl.get()); |
| 822 |
} else { |
| 823 |
SSL_set_connect_state(ssl.get()); |
| 824 |
} |
| 825 |
if (config->handshake_hints) { |
| 826 |
#if defined(HANDSHAKER_SUPPORTED) |
| 827 |
GetTestState(ssl.get())->get_handshake_hints_cb = |
| 828 |
[&](const SSL_CLIENT_HELLO *client_hello) { |
| 829 |
return GetHandshakeHint(ssl.get(), writer, is_resume, client_hello); |
| 830 |
}; |
| 831 |
#else |
| 832 |
fprintf(stderr, "The external handshaker can only be used on Linux\n"); |
| 833 |
return false; |
| 834 |
#endif |
| 835 |
} |
| 836 |
|
| 837 |
OwnedSocket sock = Connect(config); |
| 838 |
if (!sock.is_valid()) { |
| 839 |
return false; |
| 840 |
} |
| 841 |
|
| 842 |
// Half-close and drain the socket before releasing it. This seems to be |
| 843 |
// necessary for graceful shutdown on Windows. It will also avoid write |
| 844 |
// failures in the test runner. |
| 845 |
sock.set_drain_on_close(true); |
| 846 |
|
| 847 |
// Windows uses |SOCKET| for socket types, but OpenSSL's API requires casting |
| 848 |
// them to |int|. |
| 849 |
bssl::UniquePtr<BIO> bio( |
| 850 |
BIO_new_socket(static_cast<int>(sock.get()), BIO_NOCLOSE)); |
| 851 |
if (!bio) { |
| 852 |
return false; |
| 853 |
} |
| 854 |
|
| 855 |
uint8_t shim_id[8]; |
| 856 |
CRYPTO_store_u64_le(shim_id, config->shim_id); |
| 857 |
if (!BIO_write_all(bio.get(), shim_id, sizeof(shim_id))) { |
| 858 |
return false; |
| 859 |
} |
| 860 |
|
| 861 |
if (config->is_dtls) { |
| 862 |
bssl::UniquePtr<BIO> packeted = PacketedBioCreate(GetClock()); |
| 863 |
if (!packeted) { |
| 864 |
return false; |
| 865 |
} |
| 866 |
GetTestState(ssl.get())->packeted_bio = packeted.get(); |
| 867 |
BIO_push(packeted.get(), bio.release()); |
| 868 |
bio = std::move(packeted); |
| 869 |
} |
| 870 |
if (config->async && !config->is_quic) { |
| 871 |
// Note async tests only affect callbacks in QUIC. The IO path does not |
| 872 |
// behave differently when synchronous or asynchronous our QUIC APIs. |
| 873 |
bssl::UniquePtr<BIO> async_scoped = |
| 874 |
config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate(); |
| 875 |
if (!async_scoped) { |
| 876 |
return false; |
| 877 |
} |
| 878 |
BIO_push(async_scoped.get(), bio.release()); |
| 879 |
GetTestState(ssl.get())->async_bio = async_scoped.get(); |
| 880 |
bio = std::move(async_scoped); |
| 881 |
} |
| 882 |
if (config->is_quic) { |
| 883 |
GetTestState(ssl.get())->quic_transport.reset( |
| 884 |
new MockQuicTransport(std::move(bio), ssl.get())); |
| 885 |
} else { |
| 886 |
SSL_set_bio(ssl.get(), bio.get(), bio.get()); |
| 887 |
bio.release(); // SSL_set_bio takes ownership. |
| 888 |
} |
| 889 |
|
| 890 |
bool ret = DoExchange(out_session, &ssl, config, is_resume, false, writer); |
| 891 |
if (!config->is_server && is_resume && config->expect_reject_early_data) { |
| 892 |
// We must have failed due to an early data rejection. |
| 893 |
if (ret) { |
| 894 |
fprintf(stderr, "0-RTT exchange unexpected succeeded.\n"); |
| 895 |
return false; |
| 896 |
} |
| 897 |
if (SSL_get_error(ssl.get(), -1) != SSL_ERROR_EARLY_DATA_REJECTED) { |
| 898 |
fprintf(stderr, |
| 899 |
"SSL_get_error did not signal SSL_ERROR_EARLY_DATA_REJECTED.\n"); |
| 900 |
return false; |
| 901 |
} |
| 902 |
|
| 903 |
// Before reseting, early state should still be available. |
| 904 |
if (!SSL_in_early_data(ssl.get()) || |
| 905 |
!CheckHandshakeProperties(ssl.get(), is_resume, config)) { |
| 906 |
fprintf(stderr, "SSL_in_early_data returned false before reset.\n"); |
| 907 |
return false; |
| 908 |
} |
| 909 |
|
| 910 |
// Client pre- and post-0-RTT reject states are considered logically |
| 911 |
// different connections with different test expections. Check that the test |
| 912 |
// did not mistakenly configure reason expectations on the wrong one. |
| 913 |
if (!config->expect_early_data_reason.empty()) { |
| 914 |
fprintf(stderr, |
| 915 |
"Test error: client reject -expect-early-data-reason flags " |
| 916 |
"should be configured with -on-retry, not -on-resume.\n"); |
| 917 |
return false; |
| 918 |
} |
| 919 |
|
| 920 |
// Reset the connection and try again at 1-RTT. |
| 921 |
SSL_reset_early_data_reject(ssl.get()); |
| 922 |
GetTestState(ssl.get())->cert_verified = false; |
| 923 |
|
| 924 |
// After reseting, the socket should report it is no longer in an early data |
| 925 |
// state. |
| 926 |
if (SSL_in_early_data(ssl.get())) { |
| 927 |
fprintf(stderr, "SSL_in_early_data returned true after reset.\n"); |
| 928 |
return false; |
| 929 |
} |
| 930 |
|
| 931 |
if (!SetTestConfig(ssl.get(), retry_config)) { |
| 932 |
return false; |
| 933 |
} |
| 934 |
|
| 935 |
assert(!config->handoff); |
| 936 |
config = retry_config; |
| 937 |
ret = DoExchange(out_session, &ssl, retry_config, is_resume, true, writer); |
| 938 |
} |
| 939 |
|
| 940 |
// An ECH rejection appears as a failed connection. Note |ssl| may use a |
| 941 |
// different config on ECH rejection. |
| 942 |
if (config->expect_no_ech_retry_configs || |
| 943 |
!config->expect_ech_retry_configs.empty()) { |
| 944 |
bssl::Span<const uint8_t> expected = |
| 945 |
config->expect_no_ech_retry_configs |
| 946 |
? bssl::Span<const uint8_t>() |
| 947 |
: bssl::MakeConstSpan(reinterpret_cast<const uint8_t *>( |
| 948 |
config->expect_ech_retry_configs.data()), |
| 949 |
config->expect_ech_retry_configs.size()); |
| 950 |
if (ret) { |
| 951 |
fprintf(stderr, "Expected ECH rejection, but connection succeeded.\n"); |
| 952 |
return false; |
| 953 |
} |
| 954 |
uint32_t err = ERR_peek_error(); |
| 955 |
if (SSL_get_error(ssl.get(), -1) != SSL_ERROR_SSL || |
| 956 |
ERR_GET_LIB(err) != ERR_LIB_SSL || |
| 957 |
ERR_GET_REASON(err) != SSL_R_ECH_REJECTED) { |
| 958 |
fprintf(stderr, "Expected ECH rejection, but connection succeeded.\n"); |
| 959 |
return false; |
| 960 |
} |
| 961 |
const uint8_t *retry_configs; |
| 962 |
size_t retry_configs_len; |
| 963 |
SSL_get0_ech_retry_configs(ssl.get(), &retry_configs, &retry_configs_len); |
| 964 |
if (bssl::MakeConstSpan(retry_configs, retry_configs_len) != expected) { |
| 965 |
fprintf(stderr, "ECH retry configs did not match expectations.\n"); |
| 966 |
// Clear the error queue. Otherwise |SSL_R_ECH_REJECTED| will be printed |
| 967 |
// to stderr and the test framework will think the test had the expected |
| 968 |
// expectations. |
| 969 |
ERR_clear_error(); |
| 970 |
return false; |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
if (!ret) { |
| 975 |
// Print the |SSL_get_error| code. Otherwise, some failures are silent and |
| 976 |
// hard to debug. |
| 977 |
int ssl_err = SSL_get_error(ssl.get(), -1); |
| 978 |
if (ssl_err != SSL_ERROR_NONE) { |
| 979 |
fprintf(stderr, "SSL error: %s\n", SSL_error_description(ssl_err)); |
| 980 |
if (ssl_err == SSL_ERROR_SYSCALL) { |
| 981 |
int err = errno; |
| 982 |
fprintf(stderr, "Error occurred: errno = %d, description = %s\n", err, strerror(err)); |
| 983 |
|
| 984 |
} |
| 985 |
} |
| 986 |
return false; |
| 987 |
} |
| 988 |
|
| 989 |
if (!GetTestState(ssl.get())->msg_callback_ok) { |
| 990 |
return false; |
| 991 |
} |
| 992 |
|
| 993 |
if (!config->expect_msg_callback.empty() && |
| 994 |
GetTestState(ssl.get())->msg_callback_text != |
| 995 |
config->expect_msg_callback) { |
| 996 |
fprintf(stderr, "Bad message callback trace. Wanted:\n%s\nGot:\n%s\n", |
| 997 |
config->expect_msg_callback.c_str(), |
| 998 |
GetTestState(ssl.get())->msg_callback_text.c_str()); |
| 999 |
return false; |
| 1000 |
} |
| 1001 |
|
| 1002 |
return true; |
| 1003 |
} |
| 1004 |
|
| 1005 |
static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session, |
| 1006 |
bssl::UniquePtr<SSL> *ssl_uniqueptr, |
| 1007 |
const TestConfig *config, bool is_resume, bool is_retry, |
| 1008 |
SettingsWriter *writer) { |
| 1009 |
int ret; |
| 1010 |
SSL *ssl = ssl_uniqueptr->get(); |
| 1011 |
SSL_CTX *session_ctx = SSL_get_SSL_CTX(ssl); |
| 1012 |
TestState *test_state = GetTestState(ssl); |
| 1013 |
uint8_t tls_unique_before_transfer[16]; |
| 1014 |
size_t tls_unique_len_before_transfer = 0; |
| 1015 |
|
| 1016 |
if (!config->implicit_handshake) { |
| 1017 |
if (config->handoff) { |
| 1018 |
#if defined(HANDSHAKER_SUPPORTED) |
| 1019 |
if (!DoSplitHandshake(ssl_uniqueptr, writer, is_resume)) { |
| 1020 |
return false; |
| 1021 |
} |
| 1022 |
ssl = ssl_uniqueptr->get(); |
| 1023 |
test_state = GetTestState(ssl); |
| 1024 |
#else |
| 1025 |
fprintf(stderr, "The external handshaker can only be used on Linux\n"); |
| 1026 |
return false; |
| 1027 |
#endif |
| 1028 |
} |
| 1029 |
|
| 1030 |
do { |
| 1031 |
ret = CheckIdempotentError("SSL_do_handshake", ssl, [&]() -> int { |
| 1032 |
return SSL_do_handshake(ssl); |
| 1033 |
}); |
| 1034 |
} while (RetryAsync(ssl, ret)); |
| 1035 |
|
| 1036 |
SSLTransfer sslTransfer; |
| 1037 |
sslTransfer.MarkTest(config, ssl); |
| 1038 |
// Fetch tls_unique_len_before_transfer before SSL transfer. |
| 1039 |
if (config->do_ssl_transfer == 1 && sslTransfer.IsSupported(ssl)) { |
| 1040 |
if (config->tls_unique) { |
| 1041 |
if (!SSL_get_tls_unique(ssl, tls_unique_before_transfer, |
| 1042 |
&tls_unique_len_before_transfer, |
| 1043 |
sizeof(tls_unique_before_transfer))) { |
| 1044 |
fprintf(stderr, "failed to get tls-unique before ssl transfer\n"); |
| 1045 |
return false; |
| 1046 |
} |
| 1047 |
if (tls_unique_len_before_transfer != 12) { |
| 1048 |
fprintf(stderr, "expected 12 bytes of tls-unique but got %u", |
| 1049 |
static_cast<unsigned>(tls_unique_len_before_transfer)); |
| 1050 |
return false; |
| 1051 |
} |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
if (!sslTransfer.ResetSSL(config, ssl_uniqueptr)) { |
| 1056 |
return false; |
| 1057 |
} |
| 1058 |
ssl = ssl_uniqueptr->get(); |
| 1059 |
|
| 1060 |
if (config->forbid_renegotiation_after_handshake) { |
| 1061 |
SSL_set_renegotiate_mode(ssl, ssl_renegotiate_never); |
| 1062 |
} |
| 1063 |
|
| 1064 |
if (ret != 1 || !CheckHandshakeProperties(ssl, is_resume, config)) { |
| 1065 |
return false; |
| 1066 |
} |
| 1067 |
|
| 1068 |
CopySessions(session_ctx, SSL_get_SSL_CTX(ssl)); |
| 1069 |
|
| 1070 |
if (is_resume && !is_retry && !config->is_server && |
| 1071 |
config->expect_no_offer_early_data && SSL_in_early_data(ssl)) { |
| 1072 |
fprintf(stderr, "Client unexpectedly offered early data.\n"); |
| 1073 |
return false; |
| 1074 |
} |
| 1075 |
|
| 1076 |
if (config->handshake_twice) { |
| 1077 |
do { |
| 1078 |
ret = SSL_do_handshake(ssl); |
| 1079 |
} while (RetryAsync(ssl, ret)); |
| 1080 |
if (ret != 1) { |
| 1081 |
return false; |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
// Skip the |config->async| logic as this should be a no-op. |
| 1086 |
if (config->no_op_extra_handshake && SSL_do_handshake(ssl) != 1) { |
| 1087 |
fprintf(stderr, "Extra SSL_do_handshake was not a no-op.\n"); |
| 1088 |
return false; |
| 1089 |
} |
| 1090 |
|
| 1091 |
if (config->early_write_after_message != 0) { |
| 1092 |
if (!SSL_in_early_data(ssl) || config->is_server) { |
| 1093 |
fprintf(stderr, |
| 1094 |
"-early-write-after-message only works for 0-RTT connections " |
| 1095 |
"on servers.\n"); |
| 1096 |
return false; |
| 1097 |
} |
| 1098 |
if (!config->shim_writes_first || !config->async) { |
| 1099 |
fprintf(stderr, |
| 1100 |
"-early-write-after-message requires -shim-writes-first and " |
| 1101 |
"-async.\n"); |
| 1102 |
return false; |
| 1103 |
} |
| 1104 |
// Run the handshake until the specified message. Note that, if a |
| 1105 |
// handshake record contains multiple messages, |SSL_do_handshake| usually |
| 1106 |
// processes both atomically. The test must ensure there is a record |
| 1107 |
// boundary after the desired message. Checking |last_message_received| |
| 1108 |
// confirms this. |
| 1109 |
do { |
| 1110 |
ret = SSL_do_handshake(ssl); |
| 1111 |
} while (test_state->last_message_received != |
| 1112 |
config->early_write_after_message && |
| 1113 |
RetryAsync(ssl, ret)); |
| 1114 |
if (ret == 1) { |
| 1115 |
fprintf(stderr, "Handshake unexpectedly succeeded.\n"); |
| 1116 |
return false; |
| 1117 |
} |
| 1118 |
if (test_state->last_message_received != |
| 1119 |
config->early_write_after_message) { |
| 1120 |
// The handshake failed before we saw the target message. The generic |
| 1121 |
// error-handling logic in the caller will print the error. |
| 1122 |
return false; |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
// Reset the state to assert later that the callback isn't called in |
| 1127 |
// renegotations. |
| 1128 |
test_state->got_new_session = false; |
| 1129 |
} |
| 1130 |
|
| 1131 |
if (config->export_keying_material > 0) { |
| 1132 |
std::vector<uint8_t> result( |
| 1133 |
static_cast<size_t>(config->export_keying_material)); |
| 1134 |
if (!SSL_export_keying_material( |
| 1135 |
ssl, result.data(), result.size(), config->export_label.data(), |
| 1136 |
config->export_label.size(), |
| 1137 |
reinterpret_cast<const uint8_t *>(config->export_context.data()), |
| 1138 |
config->export_context.size(), config->use_export_context)) { |
| 1139 |
fprintf(stderr, "failed to export keying material\n"); |
| 1140 |
return false; |
| 1141 |
} |
| 1142 |
if (WriteAll(ssl, result.data(), result.size()) < 0) { |
| 1143 |
return false; |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
if (config->export_traffic_secrets) { |
| 1148 |
bssl::Span<const uint8_t> read_secret, write_secret; |
| 1149 |
if (!SSL_get_traffic_secrets(ssl, &read_secret, &write_secret)) { |
| 1150 |
fprintf(stderr, "failed to export traffic secrets\n"); |
| 1151 |
return false; |
| 1152 |
} |
| 1153 |
|
| 1154 |
assert(read_secret.size() <= 0xffff); |
| 1155 |
assert(write_secret.size() == read_secret.size()); |
| 1156 |
const uint16_t secret_len = read_secret.size(); |
| 1157 |
if (WriteAll(ssl, &secret_len, sizeof(secret_len)) < 0 || |
| 1158 |
WriteAll(ssl, read_secret.data(), read_secret.size()) < 0 || |
| 1159 |
WriteAll(ssl, write_secret.data(), write_secret.size()) < 0) { |
| 1160 |
return false; |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
if (config->tls_unique) { |
| 1165 |
uint8_t tls_unique[16]; |
| 1166 |
size_t tls_unique_len; |
| 1167 |
if (!SSL_get_tls_unique(ssl, tls_unique, &tls_unique_len, |
| 1168 |
sizeof(tls_unique))) { |
| 1169 |
fprintf(stderr, "failed to get tls-unique\n"); |
| 1170 |
return false; |
| 1171 |
} |
| 1172 |
|
| 1173 |
if (tls_unique_len != 12) { |
| 1174 |
fprintf(stderr, "expected 12 bytes of tls-unique but got %u", |
| 1175 |
static_cast<unsigned>(tls_unique_len)); |
| 1176 |
return false; |
| 1177 |
} |
| 1178 |
|
| 1179 |
if (tls_unique_len_before_transfer == 12 && |
| 1180 |
!OPENSSL_memcmp(tls_unique, tls_unique_before_transfer, 12)) { |
| 1181 |
fprintf(stderr, |
| 1182 |
"tls_unique_before_transfer is different from tls_unique."); |
| 1183 |
return false; |
| 1184 |
} |
| 1185 |
|
| 1186 |
if (WriteAll(ssl, tls_unique, tls_unique_len) < 0) { |
| 1187 |
return false; |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
if (config->send_alert) { |
| 1192 |
if (DoSendFatalAlert(ssl, SSL_AD_DECOMPRESSION_FAILURE) < 0) { |
| 1193 |
return false; |
| 1194 |
} |
| 1195 |
return true; |
| 1196 |
} |
| 1197 |
|
| 1198 |
if (config->write_different_record_sizes) { |
| 1199 |
if (config->is_dtls) { |
| 1200 |
fprintf(stderr, "write_different_record_sizes not supported for DTLS\n"); |
| 1201 |
return false; |
| 1202 |
} |
| 1203 |
// This mode writes a number of different record sizes in an attempt to |
| 1204 |
// trip up the CBC record splitting code. |
| 1205 |
static const size_t kBufLen = 32769; |
| 1206 |
std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]); |
| 1207 |
OPENSSL_memset(buf.get(), 0x42, kBufLen); |
| 1208 |
static const size_t kRecordSizes[] = { |
| 1209 |
0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769}; |
| 1210 |
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRecordSizes); i++) { |
| 1211 |
const size_t len = kRecordSizes[i]; |
| 1212 |
if (len > kBufLen) { |
| 1213 |
fprintf(stderr, "Bad kRecordSizes value.\n"); |
| 1214 |
return false; |
| 1215 |
} |
| 1216 |
if (WriteAll(ssl, buf.get(), len) < 0) { |
| 1217 |
return false; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
} else { |
| 1221 |
static const char kInitialWrite[] = "hello"; |
| 1222 |
bool pending_initial_write = false; |
| 1223 |
if (config->read_with_unfinished_write) { |
| 1224 |
if (!config->async) { |
| 1225 |
fprintf(stderr, "-read-with-unfinished-write requires -async.\n"); |
| 1226 |
return false; |
| 1227 |
} |
| 1228 |
if (config->is_quic) { |
| 1229 |
fprintf(stderr, |
| 1230 |
"-read-with-unfinished-write is incompatible with QUIC.\n"); |
| 1231 |
return false; |
| 1232 |
} |
| 1233 |
|
| 1234 |
// Let only one byte of the record through. |
| 1235 |
AsyncBioAllowWrite(test_state->async_bio, 1); |
| 1236 |
int write_ret = SSL_write(ssl, kInitialWrite, strlen(kInitialWrite)); |
| 1237 |
if (SSL_get_error(ssl, write_ret) != SSL_ERROR_WANT_WRITE) { |
| 1238 |
fprintf(stderr, "Failed to leave unfinished write.\n"); |
| 1239 |
return false; |
| 1240 |
} |
| 1241 |
pending_initial_write = true; |
| 1242 |
} else if (config->shim_writes_first) { |
| 1243 |
if (WriteAll(ssl, kInitialWrite, strlen(kInitialWrite)) < 0) { |
| 1244 |
return false; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
if (!config->shim_shuts_down) { |
| 1248 |
for (;;) { |
| 1249 |
// Read only 512 bytes at a time in TLS to ensure records may be |
| 1250 |
// returned in multiple reads. |
| 1251 |
size_t read_size = config->is_dtls ? 16384 : 512; |
| 1252 |
if (config->read_size > 0) { |
| 1253 |
read_size = config->read_size; |
| 1254 |
} |
| 1255 |
std::unique_ptr<uint8_t[]> buf(new uint8_t[read_size]); |
| 1256 |
int n = DoRead(ssl_uniqueptr, buf.get(), read_size); |
| 1257 |
ssl = ssl_uniqueptr->get(); |
| 1258 |
int err = SSL_get_error(ssl, n); |
| 1259 |
if (err == SSL_ERROR_ZERO_RETURN || |
| 1260 |
(n == 0 && err == SSL_ERROR_SYSCALL)) { |
| 1261 |
if (n != 0) { |
| 1262 |
fprintf(stderr, "Invalid SSL_get_error output\n"); |
| 1263 |
return false; |
| 1264 |
} |
| 1265 |
// Stop on either clean or unclean shutdown. |
| 1266 |
break; |
| 1267 |
} else if (err != SSL_ERROR_NONE) { |
| 1268 |
if (n > 0) { |
| 1269 |
fprintf(stderr, "Invalid SSL_get_error output\n"); |
| 1270 |
return false; |
| 1271 |
} |
| 1272 |
return false; |
| 1273 |
} |
| 1274 |
// Successfully read data. |
| 1275 |
if (n <= 0) { |
| 1276 |
fprintf(stderr, "Invalid SSL_get_error output\n"); |
| 1277 |
return false; |
| 1278 |
} |
| 1279 |
|
| 1280 |
if (!config->is_server && is_resume && !is_retry && |
| 1281 |
config->expect_reject_early_data) { |
| 1282 |
fprintf(stderr, |
| 1283 |
"Unexpectedly received data instead of 0-RTT reject.\n"); |
| 1284 |
return false; |
| 1285 |
} |
| 1286 |
|
| 1287 |
// After a successful read, with or without False Start, the handshake |
| 1288 |
// must be complete unless we are doing early data. |
| 1289 |
if (!test_state->handshake_done && !SSL_early_data_accepted(ssl)) { |
| 1290 |
fprintf(stderr, "handshake was not completed after SSL_read\n"); |
| 1291 |
return false; |
| 1292 |
} |
| 1293 |
|
| 1294 |
// Clear the initial write, if unfinished. |
| 1295 |
if (pending_initial_write) { |
| 1296 |
if (WriteAll(ssl, kInitialWrite, strlen(kInitialWrite)) < 0) { |
| 1297 |
return false; |
| 1298 |
} |
| 1299 |
pending_initial_write = false; |
| 1300 |
} |
| 1301 |
|
| 1302 |
if (config->key_update && |
| 1303 |
!SSL_key_update(ssl, SSL_KEY_UPDATE_NOT_REQUESTED)) { |
| 1304 |
fprintf(stderr, "SSL_key_update failed.\n"); |
| 1305 |
return false; |
| 1306 |
} |
| 1307 |
|
| 1308 |
for (int i = 0; i < n; i++) { |
| 1309 |
buf[i] ^= 0xff; |
| 1310 |
} |
| 1311 |
if (WriteAll(ssl, buf.get(), n) < 0) { |
| 1312 |
return false; |
| 1313 |
} |
| 1314 |
} |
| 1315 |
} |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (config->read_ahead_buffer_size > 0) { |
| 1319 |
SSL_set_read_ahead(ssl, 1); |
| 1320 |
SSL_set_default_read_buffer_len(ssl, config->read_ahead_buffer_size); |
| 1321 |
} |
| 1322 |
|
| 1323 |
if (!config->is_server && !config->false_start && |
| 1324 |
!config->implicit_handshake && |
| 1325 |
// Session tickets are sent post-handshake in TLS 1.3. |
| 1326 |
GetProtocolVersion(ssl) < TLS1_3_VERSION && test_state->got_new_session) { |
| 1327 |
fprintf(stderr, "new session was established after the handshake\n"); |
| 1328 |
return false; |
| 1329 |
} |
| 1330 |
|
| 1331 |
if (GetProtocolVersion(ssl) >= TLS1_3_VERSION && !config->is_server) { |
| 1332 |
bool expect_new_session = |
| 1333 |
!config->expect_no_session && !config->shim_shuts_down; |
| 1334 |
if (expect_new_session != test_state->got_new_session) { |
| 1335 |
fprintf(stderr, |
| 1336 |
"new session was%s cached, but we expected the opposite\n", |
| 1337 |
test_state->got_new_session ? "" : " not"); |
| 1338 |
return false; |
| 1339 |
} |
| 1340 |
|
| 1341 |
if (expect_new_session) { |
| 1342 |
bool got_early_data = test_state->new_session->ticket_max_early_data != 0; |
| 1343 |
if (config->expect_ticket_supports_early_data != got_early_data) { |
| 1344 |
fprintf(stderr, |
| 1345 |
"new session did%s support early data, but we expected the " |
| 1346 |
"opposite\n", |
| 1347 |
got_early_data ? "" : " not"); |
| 1348 |
return false; |
| 1349 |
} |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
if (out_session) { |
| 1354 |
*out_session = std::move(test_state->new_session); |
| 1355 |
} |
| 1356 |
|
| 1357 |
ret = DoShutdown(ssl); |
| 1358 |
|
| 1359 |
if (config->shim_shuts_down && config->check_close_notify) { |
| 1360 |
// We initiate shutdown, so |SSL_shutdown| will return in two stages. First |
| 1361 |
// it returns zero when our close_notify is sent, then one when the peer's |
| 1362 |
// is received. |
| 1363 |
if (ret != 0) { |
| 1364 |
fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret); |
| 1365 |
return false; |
| 1366 |
} |
| 1367 |
ret = DoShutdown(ssl); |
| 1368 |
} |
| 1369 |
|
| 1370 |
if (ret != 1) { |
| 1371 |
fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret); |
| 1372 |
return false; |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (SSL_total_renegotiations(ssl) > 0) { |
| 1376 |
if (!SSL_get_session(ssl)->not_resumable) { |
| 1377 |
fprintf(stderr, |
| 1378 |
"Renegotiations should never produce resumable sessions.\n"); |
| 1379 |
return false; |
| 1380 |
} |
| 1381 |
|
| 1382 |
if (SSL_session_reused(ssl)) { |
| 1383 |
fprintf(stderr, "Renegotiations should never resume sessions.\n"); |
| 1384 |
return false; |
| 1385 |
} |
| 1386 |
|
| 1387 |
// Re-check authentication properties after a renegotiation. The reported |
| 1388 |
// values should remain unchanged even if the server sent different SCT |
| 1389 |
// lists. |
| 1390 |
if (!CheckAuthProperties(ssl, is_resume, config)) { |
| 1391 |
return false; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
if (SSL_total_renegotiations(ssl) != config->expect_total_renegotiations) { |
| 1396 |
fprintf(stderr, "Expected %d renegotiations, got %d\n", |
| 1397 |
config->expect_total_renegotiations, SSL_total_renegotiations(ssl)); |
| 1398 |
return false; |
| 1399 |
} |
| 1400 |
|
| 1401 |
if (config->renegotiate_explicit && |
| 1402 |
SSL_total_renegotiations(ssl) != test_state->explicit_renegotiates) { |
| 1403 |
fprintf(stderr, "Performed %d renegotiations, but triggered %d of them\n", |
| 1404 |
SSL_total_renegotiations(ssl), test_state->explicit_renegotiates); |
| 1405 |
return false; |
| 1406 |
} |
| 1407 |
|
| 1408 |
if (SSL_clear_num_renegotiations(ssl) != |
| 1409 |
config->expect_total_renegotiations || |
| 1410 |
SSL_total_renegotiations(ssl) != 0) { |
| 1411 |
fprintf(stderr, "Expected renegotiation count to be reset to 0, got %d\n", |
| 1412 |
SSL_total_renegotiations(ssl)); |
| 1413 |
return false; |
| 1414 |
} |
| 1415 |
|
| 1416 |
return true; |
| 1417 |
} |
| 1418 |
|
| 1419 |
class StderrDelimiter { |
| 1420 |
public: |
| 1421 |
~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); } |
| 1422 |
}; |
| 1423 |
|
| 1424 |
int main(int argc, char **argv) { |
| 1425 |
#if defined(OPENSSL_LINUX) && defined(AWSLC_VM_UBE_TESTING) |
| 1426 |
if (1 != HAZMAT_init_sysgenid_file()) { |
| 1427 |
abort(); |
| 1428 |
} |
| 1429 |
#endif |
| 1430 |
|
| 1431 |
// To distinguish ASan's output from ours, add a trailing message to stderr. |
| 1432 |
// Anything following this line will be considered an error. |
| 1433 |
StderrDelimiter delimiter; |
| 1434 |
|
| 1435 |
#if defined(OPENSSL_WINDOWS) |
| 1436 |
// Initialize Winsock. |
| 1437 |
WORD wsa_version = MAKEWORD(2, 2); |
| 1438 |
WSADATA wsa_data; |
| 1439 |
int wsa_err = WSAStartup(wsa_version, &wsa_data); |
| 1440 |
if (wsa_err != 0) { |
| 1441 |
fprintf(stderr, "WSAStartup failed: %d\n", wsa_err); |
| 1442 |
return 1; |
| 1443 |
} |
| 1444 |
if (wsa_data.wVersion != wsa_version) { |
| 1445 |
fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion); |
| 1446 |
return 1; |
| 1447 |
} |
| 1448 |
#else |
| 1449 |
signal(SIGPIPE, SIG_IGN); |
| 1450 |
#endif |
| 1451 |
|
| 1452 |
CRYPTO_library_init(); |
| 1453 |
|
| 1454 |
TestConfig initial_config, resume_config, retry_config; |
| 1455 |
if (!ParseConfig(argc - 1, argv + 1, /*is_shim=*/true, &initial_config, |
| 1456 |
&resume_config, &retry_config)) { |
| 1457 |
return Usage(argv[0]); |
| 1458 |
} |
| 1459 |
|
| 1460 |
if (initial_config.is_handshaker_supported) { |
| 1461 |
#if defined(HANDSHAKER_SUPPORTED) |
| 1462 |
printf("Yes\n"); |
| 1463 |
#else |
| 1464 |
printf("No\n"); |
| 1465 |
#endif |
| 1466 |
return 0; |
| 1467 |
} |
| 1468 |
|
| 1469 |
if (initial_config.wait_for_debugger) { |
| 1470 |
#if defined(OPENSSL_WINDOWS) |
| 1471 |
fprintf(stderr, "-wait-for-debugger is not supported on Windows.\n"); |
| 1472 |
return 1; |
| 1473 |
#else |
| 1474 |
// The debugger will resume the process. |
| 1475 |
raise(SIGSTOP); |
| 1476 |
#endif |
| 1477 |
} |
| 1478 |
|
| 1479 |
bssl::UniquePtr<SSL_CTX> ssl_ctx; |
| 1480 |
|
| 1481 |
bssl::UniquePtr<SSL_SESSION> session; |
| 1482 |
for (int i = 0; i < initial_config.resume_count + 1; i++) { |
| 1483 |
bool is_resume = i > 0; |
| 1484 |
TestConfig *config = is_resume ? &resume_config : &initial_config; |
| 1485 |
ssl_ctx = config->SetupCtx(ssl_ctx.get()); |
| 1486 |
if (!ssl_ctx) { |
| 1487 |
ERR_print_errors_fp(stderr); |
| 1488 |
return 1; |
| 1489 |
} |
| 1490 |
|
| 1491 |
if (is_resume && !initial_config.is_server && !session) { |
| 1492 |
fprintf(stderr, "No session to offer.\n"); |
| 1493 |
return 1; |
| 1494 |
} |
| 1495 |
|
| 1496 |
bssl::UniquePtr<SSL_SESSION> offer_session = std::move(session); |
| 1497 |
SettingsWriter writer; |
| 1498 |
if (!writer.Init(i, config, offer_session.get())) { |
| 1499 |
fprintf(stderr, "Error writing settings.\n"); |
| 1500 |
return 1; |
| 1501 |
} |
| 1502 |
bool ok = DoConnection(&session, ssl_ctx.get(), config, &retry_config, |
| 1503 |
is_resume, offer_session.get(), &writer); |
| 1504 |
if (!writer.Commit()) { |
| 1505 |
fprintf(stderr, "Error writing settings.\n"); |
| 1506 |
return 1; |
| 1507 |
} |
| 1508 |
if (!ok) { |
| 1509 |
fprintf(stderr, "Connection %d failed.\n", i + 1); |
| 1510 |
ERR_print_errors_fp(stderr); |
| 1511 |
return 1; |
| 1512 |
} |
| 1513 |
|
| 1514 |
if (config->resumption_delay != 0) { |
| 1515 |
AdvanceClock(config->resumption_delay); |
| 1516 |
} |
| 1517 |
} |
| 1518 |
|
| 1519 |
return 0; |
| 1520 |
} |