| 1 |
// Copyright (c) 2018, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "handshake_util.h" |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#if defined(HANDSHAKER_SUPPORTED) |
| 8 |
#include <errno.h> |
| 9 |
#include <fcntl.h> |
| 10 |
#include <spawn.h> |
| 11 |
#include <sys/socket.h> |
| 12 |
#include <sys/stat.h> |
| 13 |
#include <sys/types.h> |
| 14 |
#include <sys/wait.h> |
| 15 |
#include <unistd.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <functional> |
| 19 |
#include <map> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
#include "async_bio.h" |
| 23 |
#include "packeted_bio.h" |
| 24 |
#include "test_config.h" |
| 25 |
#include "test_state.h" |
| 26 |
|
| 27 |
#include <openssl/bytestring.h> |
| 28 |
#include <openssl/ssl.h> |
| 29 |
|
| 30 |
using namespace bssl; |
| 31 |
|
| 32 |
bool RetryAsync(SSL *ssl, int ret) { |
| 33 |
const TestConfig *config = GetTestConfig(ssl); |
| 34 |
TestState *test_state = GetTestState(ssl); |
| 35 |
if (ret >= 0) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
int ssl_err = SSL_get_error(ssl, ret); |
| 40 |
if (ssl_err == SSL_ERROR_WANT_RENEGOTIATE && config->renegotiate_explicit) { |
| 41 |
test_state->explicit_renegotiates++; |
| 42 |
return SSL_renegotiate(ssl); |
| 43 |
} |
| 44 |
|
| 45 |
if (test_state->quic_transport && ssl_err == SSL_ERROR_WANT_READ) { |
| 46 |
return test_state->quic_transport->ReadHandshake(); |
| 47 |
} |
| 48 |
|
| 49 |
if (!config->async) { |
| 50 |
// Only asynchronous tests should trigger other retries. |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
if (test_state->packeted_bio != nullptr && |
| 55 |
PacketedBioAdvanceClock(test_state->packeted_bio)) { |
| 56 |
// The DTLS retransmit logic silently ignores write failures. So the test |
| 57 |
// may progress, allow writes through synchronously. |
| 58 |
AsyncBioEnforceWriteQuota(test_state->async_bio, false); |
| 59 |
int timeout_ret = DTLSv1_handle_timeout(ssl); |
| 60 |
AsyncBioEnforceWriteQuota(test_state->async_bio, true); |
| 61 |
|
| 62 |
if (timeout_ret < 0) { |
| 63 |
fprintf(stderr, "Error retransmitting.\n"); |
| 64 |
return false; |
| 65 |
} |
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
// See if we needed to read or write more. If so, allow one byte through on |
| 70 |
// the appropriate end to maximally stress the state machine. |
| 71 |
switch (ssl_err) { |
| 72 |
case SSL_ERROR_WANT_READ: |
| 73 |
AsyncBioAllowRead(test_state->async_bio, 1); |
| 74 |
return true; |
| 75 |
case SSL_ERROR_WANT_WRITE: |
| 76 |
AsyncBioAllowWrite(test_state->async_bio, 1); |
| 77 |
return true; |
| 78 |
case SSL_ERROR_WANT_X509_LOOKUP: |
| 79 |
test_state->cert_ready = true; |
| 80 |
return true; |
| 81 |
case SSL_ERROR_PENDING_SESSION: |
| 82 |
test_state->session = std::move(test_state->pending_session); |
| 83 |
return true; |
| 84 |
case SSL_ERROR_PENDING_CERTIFICATE: |
| 85 |
test_state->early_callback_ready = true; |
| 86 |
return true; |
| 87 |
case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: |
| 88 |
test_state->private_key_retries++; |
| 89 |
return true; |
| 90 |
case SSL_ERROR_WANT_CERTIFICATE_VERIFY: |
| 91 |
test_state->custom_verify_ready = true; |
| 92 |
return true; |
| 93 |
default: |
| 94 |
return false; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
int CheckIdempotentError(const char *name, SSL *ssl, |
| 99 |
std::function<int()> func) { |
| 100 |
int ret = func(); |
| 101 |
int ssl_err = SSL_get_error(ssl, ret); |
| 102 |
uint32_t err = ERR_peek_error(); |
| 103 |
if (ssl_err == SSL_ERROR_SSL || ssl_err == SSL_ERROR_ZERO_RETURN) { |
| 104 |
int ret2 = func(); |
| 105 |
int ssl_err2 = SSL_get_error(ssl, ret2); |
| 106 |
uint32_t err2 = ERR_peek_error(); |
| 107 |
if (ret != ret2 || ssl_err != ssl_err2 || err != err2) { |
| 108 |
fprintf(stderr, "Repeating %s did not replay the error.\n", name); |
| 109 |
char buf[256]; |
| 110 |
ERR_error_string_n(err, buf, sizeof(buf)); |
| 111 |
fprintf(stderr, "Wanted: %d %d %s\n", ret, ssl_err, buf); |
| 112 |
ERR_error_string_n(err2, buf, sizeof(buf)); |
| 113 |
fprintf(stderr, "Got: %d %d %s\n", ret2, ssl_err2, buf); |
| 114 |
// runner treats exit code 90 as always failing. Otherwise, it may |
| 115 |
// accidentally consider the result an expected protocol failure. |
| 116 |
exit(90); |
| 117 |
} |
| 118 |
} |
| 119 |
return ret; |
| 120 |
} |
| 121 |
|
| 122 |
#if defined(HANDSHAKER_SUPPORTED) |
| 123 |
|
| 124 |
// MoveBIOs moves the |BIO|s of |src| to |dst|. It is used for handoff. |
| 125 |
static void MoveBIOs(SSL *dest, SSL *src) { |
| 126 |
BIO *rbio = SSL_get_rbio(src); |
| 127 |
BIO_up_ref(rbio); |
| 128 |
SSL_set0_rbio(dest, rbio); |
| 129 |
|
| 130 |
BIO *wbio = SSL_get_wbio(src); |
| 131 |
BIO_up_ref(wbio); |
| 132 |
SSL_set0_wbio(dest, wbio); |
| 133 |
|
| 134 |
SSL_set0_rbio(src, nullptr); |
| 135 |
SSL_set0_wbio(src, nullptr); |
| 136 |
} |
| 137 |
|
| 138 |
static bool HandoffReady(SSL *ssl, int ret) { |
| 139 |
return ret < 0 && SSL_get_error(ssl, ret) == SSL_ERROR_HANDOFF; |
| 140 |
} |
| 141 |
|
| 142 |
static ssize_t read_eintr(int fd, void *out, size_t len) { |
| 143 |
ssize_t ret; |
| 144 |
do { |
| 145 |
ret = read(fd, out, len); |
| 146 |
} while (ret < 0 && errno == EINTR); |
| 147 |
return ret; |
| 148 |
} |
| 149 |
|
| 150 |
static ssize_t write_eintr(int fd, const void *in, size_t len) { |
| 151 |
ssize_t ret; |
| 152 |
do { |
| 153 |
ret = write(fd, in, len); |
| 154 |
} while (ret < 0 && errno == EINTR); |
| 155 |
return ret; |
| 156 |
} |
| 157 |
|
| 158 |
static ssize_t waitpid_eintr(pid_t pid, int *wstatus, int options) { |
| 159 |
pid_t ret; |
| 160 |
do { |
| 161 |
ret = waitpid(pid, wstatus, options); |
| 162 |
} while (ret < 0 && errno == EINTR); |
| 163 |
return ret; |
| 164 |
} |
| 165 |
|
| 166 |
// Proxy relays data between |socket|, which is connected to the client, and the |
| 167 |
// handshaker, which is connected to the numerically specified file descriptors, |
| 168 |
// until the handshaker returns control. |
| 169 |
static bool Proxy(BIO *socket, bool async, int control, int rfd, int wfd) { |
| 170 |
for (;;) { |
| 171 |
fd_set rfds; |
| 172 |
FD_ZERO(&rfds); |
| 173 |
FD_SET(wfd, &rfds); |
| 174 |
FD_SET(control, &rfds); |
| 175 |
int fd_max = wfd > control ? wfd : control; |
| 176 |
if (select(fd_max + 1, &rfds, nullptr, nullptr, nullptr) == -1) { |
| 177 |
perror("select"); |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
char buf[64]; |
| 182 |
ssize_t bytes; |
| 183 |
if (FD_ISSET(wfd, &rfds) && |
| 184 |
(bytes = read_eintr(wfd, buf, sizeof(buf))) > 0) { |
| 185 |
char *b = buf; |
| 186 |
while (bytes) { |
| 187 |
int written = BIO_write(socket, b, bytes); |
| 188 |
if (!written) { |
| 189 |
fprintf(stderr, "BIO_write wrote nothing\n"); |
| 190 |
return false; |
| 191 |
} |
| 192 |
if (written < 0) { |
| 193 |
if (async) { |
| 194 |
AsyncBioAllowWrite(socket, 1); |
| 195 |
continue; |
| 196 |
} |
| 197 |
fprintf(stderr, "BIO_write failed\n"); |
| 198 |
return false; |
| 199 |
} |
| 200 |
b += written; |
| 201 |
bytes -= written; |
| 202 |
} |
| 203 |
// Flush all pending data from the handshaker to the client before |
| 204 |
// considering control messages. |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
if (!FD_ISSET(control, &rfds)) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
char msg; |
| 213 |
if (read_eintr(control, &msg, 1) != 1) { |
| 214 |
perror("read"); |
| 215 |
return false; |
| 216 |
} |
| 217 |
switch (msg) { |
| 218 |
case kControlMsgDone: |
| 219 |
return true; |
| 220 |
case kControlMsgError: |
| 221 |
return false; |
| 222 |
case kControlMsgWantRead: |
| 223 |
break; |
| 224 |
default: |
| 225 |
fprintf(stderr, "Unknown control message from handshaker: %c\n", msg); |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
auto proxy_data = [&](uint8_t *out, size_t len) -> bool { |
| 230 |
if (async) { |
| 231 |
AsyncBioAllowRead(socket, len); |
| 232 |
} |
| 233 |
|
| 234 |
while (len > 0) { |
| 235 |
int bytes_read = BIO_read(socket, out, len); |
| 236 |
if (bytes_read < 1) { |
| 237 |
fprintf(stderr, "BIO_read failed\n"); |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
ssize_t bytes_written = write_eintr(rfd, out, bytes_read); |
| 242 |
if (bytes_written == -1) { |
| 243 |
perror("write"); |
| 244 |
return false; |
| 245 |
} |
| 246 |
if (bytes_written != bytes_read) { |
| 247 |
fprintf(stderr, "short write (%zd of %d bytes)\n", bytes_written, |
| 248 |
bytes_read); |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
len -= bytes_read; |
| 253 |
out += bytes_read; |
| 254 |
} |
| 255 |
return true; |
| 256 |
}; |
| 257 |
|
| 258 |
// Process one SSL record at a time. That way, we don't send the handshaker |
| 259 |
// anything it doesn't want to process, e.g. early data. |
| 260 |
uint8_t header[SSL3_RT_HEADER_LENGTH]; |
| 261 |
if (!proxy_data(header, sizeof(header))) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
if (header[1] != 3) { |
| 265 |
fprintf(stderr, "bad header\n"); |
| 266 |
return false; |
| 267 |
} |
| 268 |
size_t remaining = (header[3] << 8) + header[4]; |
| 269 |
while (remaining > 0) { |
| 270 |
uint8_t readbuf[64]; |
| 271 |
size_t len = remaining > sizeof(readbuf) ? sizeof(readbuf) : remaining; |
| 272 |
if (!proxy_data(readbuf, len)) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
remaining -= len; |
| 276 |
} |
| 277 |
|
| 278 |
// The handshaker blocks on the control channel, so we have to signal |
| 279 |
// it that the data have been written. |
| 280 |
msg = kControlMsgWriteCompleted; |
| 281 |
if (write_eintr(control, &msg, 1) != 1) { |
| 282 |
perror("write"); |
| 283 |
return false; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
class ScopedFD { |
| 289 |
public: |
| 290 |
ScopedFD() : fd_(-1) {} |
| 291 |
explicit ScopedFD(int fd) : fd_(fd) {} |
| 292 |
~ScopedFD() { Reset(); } |
| 293 |
|
| 294 |
ScopedFD(ScopedFD &&other) { *this = std::move(other); } |
| 295 |
ScopedFD &operator=(ScopedFD &&other) { |
| 296 |
Reset(other.fd_); |
| 297 |
other.fd_ = -1; |
| 298 |
return *this; |
| 299 |
} |
| 300 |
|
| 301 |
int fd() const { return fd_; } |
| 302 |
|
| 303 |
void Reset(int fd = -1) { |
| 304 |
if (fd_ >= 0) { |
| 305 |
close(fd_); |
| 306 |
} |
| 307 |
fd_ = fd; |
| 308 |
} |
| 309 |
|
| 310 |
private: |
| 311 |
int fd_; |
| 312 |
}; |
| 313 |
|
| 314 |
class ScopedProcess { |
| 315 |
public: |
| 316 |
ScopedProcess() : pid_(-1) {} |
| 317 |
~ScopedProcess() { Reset(); } |
| 318 |
|
| 319 |
ScopedProcess(ScopedProcess &&other) { *this = std::move(other); } |
| 320 |
ScopedProcess &operator=(ScopedProcess &&other) { |
| 321 |
Reset(other.pid_); |
| 322 |
other.pid_ = -1; |
| 323 |
return *this; |
| 324 |
} |
| 325 |
|
| 326 |
pid_t pid() const { return pid_; } |
| 327 |
|
| 328 |
void Reset(pid_t pid = -1) { |
| 329 |
if (pid_ >= 0) { |
| 330 |
kill(pid_, SIGTERM); |
| 331 |
int unused; |
| 332 |
Wait(&unused); |
| 333 |
} |
| 334 |
pid_ = pid; |
| 335 |
} |
| 336 |
|
| 337 |
bool Wait(int *out_status) { |
| 338 |
if (pid_ < 0) { |
| 339 |
return false; |
| 340 |
} |
| 341 |
if (waitpid_eintr(pid_, out_status, 0) != pid_) { |
| 342 |
return false; |
| 343 |
} |
| 344 |
pid_ = -1; |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
private: |
| 349 |
pid_t pid_; |
| 350 |
}; |
| 351 |
|
| 352 |
class FileActionsDestroyer { |
| 353 |
public: |
| 354 |
explicit FileActionsDestroyer(posix_spawn_file_actions_t *actions) |
| 355 |
: actions_(actions) {} |
| 356 |
~FileActionsDestroyer() { posix_spawn_file_actions_destroy(actions_); } |
| 357 |
FileActionsDestroyer(const FileActionsDestroyer &) = delete; |
| 358 |
FileActionsDestroyer &operator=(const FileActionsDestroyer &) = delete; |
| 359 |
|
| 360 |
private: |
| 361 |
posix_spawn_file_actions_t *actions_; |
| 362 |
}; |
| 363 |
|
| 364 |
// StartHandshaker starts the handshaker process and, on success, returns a |
| 365 |
// handle to the process in |*out|. It sets |*out_control| to a control pipe to |
| 366 |
// the process. |map_fds| maps from desired fd number in the child process to |
| 367 |
// the source fd in the calling process. |close_fds| is the list of additional |
| 368 |
// fds to close, which may overlap with |map_fds|. Other than stdin, stdout, and |
| 369 |
// stderr, the status of fds not listed in either set is undefined. |
| 370 |
static bool StartHandshaker(ScopedProcess *out, ScopedFD *out_control, |
| 371 |
const TestConfig *config, bool is_resume, |
| 372 |
std::map<int, int> map_fds, |
| 373 |
std::vector<int> close_fds) { |
| 374 |
if (config->handshaker_path.empty()) { |
| 375 |
fprintf(stderr, "no -handshaker-path specified\n"); |
| 376 |
return false; |
| 377 |
} |
| 378 |
struct stat dummy; |
| 379 |
if (stat(config->handshaker_path.c_str(), &dummy) == -1) { |
| 380 |
perror(config->handshaker_path.c_str()); |
| 381 |
return false; |
| 382 |
} |
| 383 |
|
| 384 |
std::vector<const char *> args; |
| 385 |
args.push_back(config->handshaker_path.c_str()); |
| 386 |
static const char kResumeFlag[] = "-handshaker-resume"; |
| 387 |
if (is_resume) { |
| 388 |
args.push_back(kResumeFlag); |
| 389 |
} |
| 390 |
// config->handshaker_args omits argv[0]. |
| 391 |
for (const char *arg : config->handshaker_args) { |
| 392 |
args.push_back(arg); |
| 393 |
} |
| 394 |
args.push_back(nullptr); |
| 395 |
|
| 396 |
// A datagram socket guarantees that writes are all-or-nothing. |
| 397 |
int control[2]; |
| 398 |
if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, control) != 0) { |
| 399 |
perror("socketpair"); |
| 400 |
return false; |
| 401 |
} |
| 402 |
ScopedFD scoped_control0(control[0]), scoped_control1(control[1]); |
| 403 |
close_fds.push_back(control[0]); |
| 404 |
map_fds[kFdControl] = control[1]; |
| 405 |
|
| 406 |
posix_spawn_file_actions_t actions; |
| 407 |
if (posix_spawn_file_actions_init(&actions) != 0) { |
| 408 |
return false; |
| 409 |
} |
| 410 |
FileActionsDestroyer actions_destroyer(&actions); |
| 411 |
for (int fd : close_fds) { |
| 412 |
if (posix_spawn_file_actions_addclose(&actions, fd) != 0) { |
| 413 |
return false; |
| 414 |
} |
| 415 |
} |
| 416 |
if (!map_fds.empty()) { |
| 417 |
int max_fd = STDERR_FILENO; |
| 418 |
for (const auto &pair : map_fds) { |
| 419 |
max_fd = std::max(max_fd, pair.first); |
| 420 |
max_fd = std::max(max_fd, pair.second); |
| 421 |
} |
| 422 |
// |map_fds| may contain cycles, so make a copy of all the source fds. |
| 423 |
// |posix_spawn| can only use |dup2|, not |dup|, so we assume |max_fd| is |
| 424 |
// the last fd we care about inheriting. |temp_fds| maps from fd number in |
| 425 |
// the parent process to a temporary fd number in the child process. |
| 426 |
std::map<int, int> temp_fds; |
| 427 |
int next_fd = max_fd + 1; |
| 428 |
for (const auto &pair : map_fds) { |
| 429 |
if (temp_fds.count(pair.second)) { |
| 430 |
continue; |
| 431 |
} |
| 432 |
temp_fds[pair.second] = next_fd; |
| 433 |
if (posix_spawn_file_actions_adddup2(&actions, pair.second, next_fd) != |
| 434 |
0 || |
| 435 |
posix_spawn_file_actions_addclose(&actions, pair.second) != 0) { |
| 436 |
return false; |
| 437 |
} |
| 438 |
next_fd++; |
| 439 |
} |
| 440 |
for (const auto &pair : map_fds) { |
| 441 |
if (posix_spawn_file_actions_adddup2(&actions, temp_fds[pair.second], |
| 442 |
pair.first) != 0) { |
| 443 |
return false; |
| 444 |
} |
| 445 |
} |
| 446 |
// Clean up temporary fds. |
| 447 |
for (int fd = max_fd + 1; fd < next_fd; fd++) { |
| 448 |
if (posix_spawn_file_actions_addclose(&actions, fd) != 0) { |
| 449 |
return false; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
fflush(stdout); |
| 455 |
fflush(stderr); |
| 456 |
|
| 457 |
// MSan doesn't know that |posix_spawn| initializes its output, so initialize |
| 458 |
// it to -1. |
| 459 |
pid_t pid = -1; |
| 460 |
if (posix_spawn(&pid, args[0], &actions, nullptr, |
| 461 |
const_cast<char *const *>(args.data()), environ) != 0) { |
| 462 |
return false; |
| 463 |
} |
| 464 |
|
| 465 |
out->Reset(pid); |
| 466 |
*out_control = std::move(scoped_control0); |
| 467 |
return true; |
| 468 |
} |
| 469 |
|
| 470 |
// RunHandshaker forks and execs the handshaker binary, handing off |input|, |
| 471 |
// and, after proxying some amount of handshake traffic, handing back |out|. |
| 472 |
static bool RunHandshaker(BIO *bio, const TestConfig *config, bool is_resume, |
| 473 |
Span<const uint8_t> input, |
| 474 |
std::vector<uint8_t> *out) { |
| 475 |
int rfd[2], wfd[2]; |
| 476 |
// We use pipes, rather than some other mechanism, for their buffers. During |
| 477 |
// the handshake, this process acts as a dumb proxy until receiving the |
| 478 |
// handback signal, which arrives asynchronously. The race condition means |
| 479 |
// that this process could incorrectly proxy post-handshake data from the |
| 480 |
// client to the handshaker. |
| 481 |
// |
| 482 |
// To avoid this, this process never proxies data to the handshaker that the |
| 483 |
// handshaker has not explicitly requested as a result of hitting |
| 484 |
// |SSL_ERROR_WANT_READ|. Pipes allow the data to sit in a buffer while the |
| 485 |
// two processes synchronize over the |control| channel. |
| 486 |
if (pipe(rfd) != 0) { |
| 487 |
perror("pipe"); |
| 488 |
return false; |
| 489 |
} |
| 490 |
ScopedFD rfd0_closer(rfd[0]), rfd1_closer(rfd[1]); |
| 491 |
|
| 492 |
if (pipe(wfd) != 0) { |
| 493 |
perror("pipe"); |
| 494 |
return false; |
| 495 |
} |
| 496 |
ScopedFD wfd0_closer(wfd[0]), wfd1_closer(wfd[1]); |
| 497 |
|
| 498 |
ScopedProcess handshaker; |
| 499 |
ScopedFD control; |
| 500 |
if (!StartHandshaker( |
| 501 |
&handshaker, &control, config, is_resume, |
| 502 |
{{kFdProxyToHandshaker, rfd[0]}, {kFdHandshakerToProxy, wfd[1]}}, |
| 503 |
{rfd[1], wfd[0]})) { |
| 504 |
return false; |
| 505 |
} |
| 506 |
|
| 507 |
rfd0_closer.Reset(); |
| 508 |
wfd1_closer.Reset(); |
| 509 |
|
| 510 |
if (write_eintr(control.fd(), input.data(), input.size()) == -1) { |
| 511 |
perror("write"); |
| 512 |
return false; |
| 513 |
} |
| 514 |
bool ok = Proxy(bio, config->async, control.fd(), rfd[1], wfd[0]); |
| 515 |
int wstatus; |
| 516 |
if (!handshaker.Wait(&wstatus)) { |
| 517 |
perror("waitpid"); |
| 518 |
return false; |
| 519 |
} |
| 520 |
if (ok && wstatus) { |
| 521 |
fprintf(stderr, "handshaker exited irregularly\n"); |
| 522 |
return false; |
| 523 |
} |
| 524 |
if (!ok) { |
| 525 |
return false; // This is a "good", i.e. expected, error. |
| 526 |
} |
| 527 |
|
| 528 |
constexpr size_t kBufSize = 1024 * 1024; |
| 529 |
std::vector<uint8_t> buf(kBufSize); |
| 530 |
ssize_t len = read_eintr(control.fd(), buf.data(), buf.size()); |
| 531 |
if (len == -1) { |
| 532 |
perror("read"); |
| 533 |
return false; |
| 534 |
} |
| 535 |
buf.resize(len); |
| 536 |
*out = std::move(buf); |
| 537 |
return true; |
| 538 |
} |
| 539 |
|
| 540 |
static bool RequestHandshakeHint(const TestConfig *config, bool is_resume, |
| 541 |
Span<const uint8_t> input, bool *out_has_hints, |
| 542 |
std::vector<uint8_t> *out_hints) { |
| 543 |
ScopedProcess handshaker; |
| 544 |
ScopedFD control; |
| 545 |
if (!StartHandshaker(&handshaker, &control, config, is_resume, {}, {})) { |
| 546 |
return false; |
| 547 |
} |
| 548 |
|
| 549 |
if (write_eintr(control.fd(), input.data(), input.size()) == -1) { |
| 550 |
perror("write"); |
| 551 |
return false; |
| 552 |
} |
| 553 |
|
| 554 |
char msg; |
| 555 |
if (read_eintr(control.fd(), &msg, 1) != 1) { |
| 556 |
perror("read"); |
| 557 |
return false; |
| 558 |
} |
| 559 |
|
| 560 |
switch (msg) { |
| 561 |
case kControlMsgDone: { |
| 562 |
constexpr size_t kBufSize = 1024 * 1024; |
| 563 |
out_hints->resize(kBufSize); |
| 564 |
ssize_t len = |
| 565 |
read_eintr(control.fd(), out_hints->data(), out_hints->size()); |
| 566 |
if (len == -1) { |
| 567 |
perror("read"); |
| 568 |
return false; |
| 569 |
} |
| 570 |
out_hints->resize(len); |
| 571 |
*out_has_hints = true; |
| 572 |
break; |
| 573 |
} |
| 574 |
case kControlMsgError: |
| 575 |
*out_has_hints = false; |
| 576 |
break; |
| 577 |
default: |
| 578 |
fprintf(stderr, "Unknown control message from handshaker: %c\n", msg); |
| 579 |
return false; |
| 580 |
} |
| 581 |
|
| 582 |
int wstatus; |
| 583 |
if (!handshaker.Wait(&wstatus)) { |
| 584 |
perror("waitpid"); |
| 585 |
return false; |
| 586 |
} |
| 587 |
if (wstatus) { |
| 588 |
fprintf(stderr, "handshaker exited irregularly\n"); |
| 589 |
return false; |
| 590 |
} |
| 591 |
|
| 592 |
return true; |
| 593 |
} |
| 594 |
|
| 595 |
// PrepareHandoff accepts the |ClientHello| from |ssl| and serializes state to |
| 596 |
// be passed to the handshaker. The serialized state includes both the SSL |
| 597 |
// handoff, as well test-related state. |
| 598 |
static bool PrepareHandoff(SSL *ssl, SettingsWriter *writer, |
| 599 |
std::vector<uint8_t> *out_handoff) { |
| 600 |
SSL_set_handoff_mode(ssl, 1); |
| 601 |
|
| 602 |
const TestConfig *config = GetTestConfig(ssl); |
| 603 |
int ret = -1; |
| 604 |
do { |
| 605 |
ret = CheckIdempotentError( |
| 606 |
"SSL_do_handshake", ssl, |
| 607 |
[&]() -> int { return SSL_do_handshake(ssl); }); |
| 608 |
} while (!HandoffReady(ssl, ret) && |
| 609 |
config->async && |
| 610 |
RetryAsync(ssl, ret)); |
| 611 |
if (!HandoffReady(ssl, ret)) { |
| 612 |
fprintf(stderr, "Handshake failed while waiting for handoff.\n"); |
| 613 |
return false; |
| 614 |
} |
| 615 |
|
| 616 |
ScopedCBB cbb; |
| 617 |
SSL_CLIENT_HELLO hello; |
| 618 |
if (!CBB_init(cbb.get(), 512) || |
| 619 |
!SSL_serialize_handoff(ssl, cbb.get(), &hello) || |
| 620 |
!writer->WriteHandoff({CBB_data(cbb.get()), CBB_len(cbb.get())}) || |
| 621 |
!SerializeContextState(SSL_get_SSL_CTX(ssl), cbb.get()) || |
| 622 |
!GetTestState(ssl)->Serialize(cbb.get())) { |
| 623 |
fprintf(stderr, "Handoff serialisation failed.\n"); |
| 624 |
return false; |
| 625 |
} |
| 626 |
out_handoff->assign(CBB_data(cbb.get()), |
| 627 |
CBB_data(cbb.get()) + CBB_len(cbb.get())); |
| 628 |
return true; |
| 629 |
} |
| 630 |
|
| 631 |
// DoSplitHandshake delegates the SSL handshake to a separate process, called |
| 632 |
// the handshaker. This process proxies I/O between the handshaker and the |
| 633 |
// client, using the |BIO| from |ssl|. After a successful handshake, |ssl| is |
| 634 |
// replaced with a new |SSL| object, in a way that is intended to be invisible |
| 635 |
// to the caller. |
| 636 |
bool DoSplitHandshake(UniquePtr<SSL> *ssl, SettingsWriter *writer, |
| 637 |
bool is_resume) { |
| 638 |
assert(SSL_get_rbio(ssl->get()) == SSL_get_wbio(ssl->get())); |
| 639 |
std::vector<uint8_t> handshaker_input; |
| 640 |
const TestConfig *config = GetTestConfig(ssl->get()); |
| 641 |
// out is the response from the handshaker, which includes a serialized |
| 642 |
// handback message, but also serialized updates to the |TestState|. |
| 643 |
std::vector<uint8_t> out; |
| 644 |
if (!PrepareHandoff(ssl->get(), writer, &handshaker_input) || |
| 645 |
!RunHandshaker(SSL_get_rbio(ssl->get()), config, is_resume, |
| 646 |
handshaker_input, &out)) { |
| 647 |
fprintf(stderr, "Handoff failed.\n"); |
| 648 |
return false; |
| 649 |
} |
| 650 |
|
| 651 |
SSL_CTX *ctx = SSL_get_SSL_CTX(ssl->get()); |
| 652 |
UniquePtr<SSL> ssl_handback = config->NewSSL(ctx, nullptr, nullptr); |
| 653 |
if (!ssl_handback) { |
| 654 |
return false; |
| 655 |
} |
| 656 |
CBS output, handback; |
| 657 |
CBS_init(&output, out.data(), out.size()); |
| 658 |
if (!CBS_get_u24_length_prefixed(&output, &handback) || |
| 659 |
!DeserializeContextState(&output, ctx) || |
| 660 |
!SetTestState(ssl_handback.get(), TestState::Deserialize(&output, ctx)) || |
| 661 |
!GetTestState(ssl_handback.get()) || !writer->WriteHandback(handback) || |
| 662 |
!SSL_apply_handback(ssl_handback.get(), handback)) { |
| 663 |
fprintf(stderr, "Handback failed.\n"); |
| 664 |
return false; |
| 665 |
} |
| 666 |
MoveBIOs(ssl_handback.get(), ssl->get()); |
| 667 |
GetTestState(ssl_handback.get())->async_bio = |
| 668 |
GetTestState(ssl->get())->async_bio; |
| 669 |
GetTestState(ssl->get())->async_bio = nullptr; |
| 670 |
|
| 671 |
*ssl = std::move(ssl_handback); |
| 672 |
return true; |
| 673 |
} |
| 674 |
|
| 675 |
bool GetHandshakeHint(SSL *ssl, SettingsWriter *writer, bool is_resume, |
| 676 |
const SSL_CLIENT_HELLO *client_hello) { |
| 677 |
ScopedCBB input; |
| 678 |
CBB child; |
| 679 |
if (!CBB_init(input.get(), client_hello->client_hello_len + 256) || |
| 680 |
!CBB_add_u24_length_prefixed(input.get(), &child) || |
| 681 |
!CBB_add_bytes(&child, client_hello->client_hello, |
| 682 |
client_hello->client_hello_len) || |
| 683 |
!CBB_add_u24_length_prefixed(input.get(), &child) || |
| 684 |
!SSL_serialize_capabilities(ssl, &child) || // |
| 685 |
!CBB_flush(input.get())) { |
| 686 |
return false; |
| 687 |
} |
| 688 |
|
| 689 |
bool has_hints; |
| 690 |
std::vector<uint8_t> hints; |
| 691 |
if (!RequestHandshakeHint( |
| 692 |
GetTestConfig(ssl), is_resume, |
| 693 |
MakeConstSpan(CBB_data(input.get()), CBB_len(input.get())), |
| 694 |
&has_hints, &hints)) { |
| 695 |
return false; |
| 696 |
} |
| 697 |
if (has_hints && |
| 698 |
(!writer->WriteHints(hints) || |
| 699 |
!SSL_set_handshake_hints(ssl, hints.data(), hints.size()))) { |
| 700 |
return false; |
| 701 |
} |
| 702 |
|
| 703 |
return true; |
| 704 |
} |
| 705 |
|
| 706 |
#endif // defined(HANDSHAKER_SUPPORTED) |