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