17 #if !defined(OPENSSL_WINDOWS)
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
20 #include <netinet/tcp.h>
22 #include <sys/socket.h>
59 #include "../../crypto/internal.h"
60 #include "../internal.h"
70 #if !defined(OPENSSL_WINDOWS)
80 fprintf(
stderr,
"%s: %d\n",
func, WSAGetLastError());
84 static int Usage(
const char *program) {
85 fprintf(
stderr,
"Usage: %s [flags...]\n", program);
99 for (
int af : {
AF_INET6, AF_INET }) {
100 int sock =
socket(af, SOCK_STREAM, 0);
106 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
107 reinterpret_cast<const char*
>(&nodelay),
sizeof(nodelay)) != 0) {
127 }
else if (af == AF_INET) {
128 sockaddr_in *sin = (sockaddr_in *) &ss;
130 sin->sin_port = htons(
port);
131 if (!inet_pton(AF_INET,
"127.0.0.1", &sin->sin_addr)) {
138 if (connect(sock,
reinterpret_cast<const sockaddr*
>(&ss),
len) == 0) {
155 #if defined(OPENSSL_WINDOWS)
156 shutdown(
sock_, SD_SEND);
158 shutdown(
sock_, SHUT_WR);
179 return test_state->quic_transport->ReadApplicationData(
out, max_out);
199 if (
config->use_exporter_between_reads) {
202 fprintf(
stderr,
"failed to export keying material\n");
209 std::unique_ptr<uint8_t[]>
buf(
new uint8_t[
static_cast<size_t>(
ret)]);
215 fprintf(
stderr,
"First and second SSL_peek did not match.\n");
223 fprintf(
stderr,
"SSL_peek and SSL_read did not match.\n");
237 if (!
test_state->quic_transport->WriteApplicationData(
in, in_len)) {
285 if (!
config->expect_ocsp_response.empty()) {
289 if (
config->expect_ocsp_response.size() !=
len ||
291 fprintf(
stderr,
"OCSP response mismatch\n");
296 if (!
config->expect_signed_cert_timestamps.empty()) {
300 if (
config->expect_signed_cert_timestamps.size() !=
len ||
303 fprintf(
stderr,
"SCT list mismatch\n");
308 if (
config->expect_verify_result) {
309 int expected_verify_result =
config->verify_fail ?
314 fprintf(
stderr,
"Wrong certificate verification result\n");
319 if (!
config->expect_peer_cert_file.empty()) {
320 bssl::UniquePtr<X509> expect_leaf;
323 config->expect_peer_cert_file)) {
330 if (!sk_X509_insert(expect_chain.get(), expect_leaf.get(), 0)) {
338 if (
X509_cmp(leaf.get(), expect_leaf.get()) != 0) {
339 fprintf(
stderr,
"Received a different leaf certificate than expected.\n");
344 fprintf(
stderr,
"Received a chain of length %zu instead of %zu.\n",
352 fprintf(
stderr,
"Chain certificate %zu did not match.\n",
360 config->expect_sha256_client_cert) {
362 "Unexpected SHA-256 client cert state: expected:%d is_resume:%d.\n",
363 config->expect_sha256_client_cert, is_resume);
367 if (
config->expect_sha256_client_cert &&
369 fprintf(
stderr,
"Have both client cert and SHA-256 hash: is_resume:%d.\n",
375 size_t peer_sha256_len;
379 if (peer_sha256_len != 32) {
380 fprintf(
stderr,
"Peer SHA-256 hash had length %zu instead of 32\n",
385 if (peer_sha256_len != 0) {
386 fprintf(
stderr,
"Unexpected peer SHA-256 hash of length %zu\n",
405 fprintf(
stderr,
"null cipher after handshake\n");
409 if (
config->expect_version != 0 &&
411 fprintf(
stderr,
"want version %04x, got %04x\n",
config->expect_version,
419 fprintf(
stderr,
"session unexpectedly was%s reused\n",
424 bool expect_handshake_done =
427 fprintf(
stderr,
"handshake was%s completed\n",
432 if (expect_handshake_done && !
config->is_server) {
433 bool expect_new_session =
434 !
config->expect_no_session &&
440 "new session was%s cached, but we expected the opposite\n",
448 fprintf(
stderr,
"session was not cached on the server.\n");
452 fprintf(
stderr,
"session was unexpectedly cached on the server.\n");
461 fprintf(
stderr,
"early callback not called\n");
465 if (!
config->expect_server_name.empty()) {
470 fprintf(
stderr,
"servername mismatch (got %s; want %s)\n",
476 if (!
config->expect_next_proto.empty()) {
478 unsigned next_proto_len;
480 if (next_proto_len !=
config->expect_next_proto.size() ||
482 next_proto_len) != 0) {
483 fprintf(
stderr,
"negotiated next proto mismatch\n");
494 unsigned alpn_proto_len;
496 if (alpn_proto_len != expect_alpn.size() ||
497 OPENSSL_memcmp(alpn_proto, expect_alpn.data(), alpn_proto_len) != 0) {
498 fprintf(
stderr,
"negotiated alpn proto mismatch\n");
503 (
config->expect_peer_application_settings ? 1 : 0)) {
505 "connection %s application settings, but expected the opposite\n",
510 ? *
config->expect_peer_application_settings
513 size_t peer_settings_len;
515 if (expect_settings !=
516 std::string(
reinterpret_cast<const char *
>(peer_settings),
517 peer_settings_len)) {
518 fprintf(
stderr,
"peer application settings mismatch\n");
522 if (!
config->expect_quic_transport_params.empty() && expect_handshake_done) {
524 size_t peer_params_len;
526 if (peer_params_len !=
config->expect_quic_transport_params.size() ||
528 config->expect_quic_transport_params.data(),
529 peer_params_len) != 0) {
530 fprintf(
stderr,
"QUIC transport params mismatch\n");
535 if (!
config->expect_channel_id.empty()) {
538 fprintf(
stderr,
"no channel id negotiated\n");
541 if (
config->expect_channel_id.size() != 64 ||
544 fprintf(
stderr,
"channel id mismatch\n");
550 fprintf(
stderr,
"No EMS for connection when expected\n");
554 if (
config->expect_secure_renegotiation &&
556 fprintf(
stderr,
"No secure renegotiation for connection when expected\n");
560 if (
config->expect_no_secure_renegotiation &&
563 "Secure renegotiation unexpectedly negotiated for connection\n");
567 if (
config->expect_peer_signature_algorithm != 0 &&
568 config->expect_peer_signature_algorithm !=
570 fprintf(
stderr,
"Peer signature algorithm was %04x, wanted %04x.\n",
572 config->expect_peer_signature_algorithm);
576 if (
config->expect_curve_id != 0) {
579 fprintf(
stderr,
"curve_id was %04x, wanted %04x\n", curve_id,
586 if (
config->expect_cipher_aes != 0 &&
589 fprintf(
stderr,
"Cipher ID was %04x, wanted %04x (has AES hardware)\n",
594 if (
config->expect_cipher_no_aes != 0 &&
596 static_cast<uint16_t>(
config->expect_cipher_no_aes) != cipher_id) {
597 fprintf(
stderr,
"Cipher ID was %04x, wanted %04x (no AES hardware)\n",
602 if (
config->expect_cipher != 0 &&
604 fprintf(
stderr,
"Cipher ID was %04x, wanted %04x\n", cipher_id,
614 "Early data was%s accepted, but we expected the opposite\n",
619 const char *early_data_reason =
621 if (!
config->expect_early_data_reason.empty() &&
622 config->expect_early_data_reason != early_data_reason) {
623 fprintf(
stderr,
"Early data reason was \"%s\", expected \"%s\"\n",
624 early_data_reason,
config->expect_early_data_reason.c_str());
629 if (!
config->psk.empty()) {
631 fprintf(
stderr,
"Received peer certificate on a PSK cipher.\n");
634 }
else if (!
config->is_server ||
config->require_any_client_certificate) {
636 fprintf(
stderr,
"Received no peer certificate but expected one.\n");
641 if (is_resume &&
config->expect_ticket_age_skew != 0 &&
643 fprintf(
stderr,
"Ticket age skew was %" PRId32
", wanted %d\n",
648 if (
config->expect_delegated_credential_used !=
651 "Got %s delegated credential usage, but wanted opposite. \n",
658 fprintf(
stderr,
"Got %sHRR, but wanted opposite.\n",
664 fprintf(
stderr,
"ECH was %saccepted, but wanted opposite.\n",
673 if (
config->handshake_hints && !
config->allow_hint_mismatch &&
679 "Performed private key operation, but hint should have skipped it\n");
683 if (
state->ticket_decrypt_done) {
685 "Performed ticket decryption, but hint should have skipped it\n");
692 static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session,
693 bssl::UniquePtr<SSL> *ssl_uniqueptr,
703 const TestConfig *retry_config,
bool is_resume,
705 bssl::UniquePtr<SSL> ssl =
config->NewSSL(
706 ssl_ctx, session, std::unique_ptr<TestState>(
new TestState));
715 if (
config->handshake_hints) {
716 #if defined(HANDSHAKER_SUPPORTED)
719 return GetHandshakeHint(ssl.get(),
writer, is_resume, client_hello);
722 fprintf(
stderr,
"The external handshaker can only be used on Linux\n");
743 BIO_push(packeted.get(), bio.release());
749 bssl::UniquePtr<BIO> async_scoped =
754 BIO_push(async_scoped.get(), bio.release());
767 if (!
config->is_server && is_resume &&
config->expect_reject_early_data) {
770 fprintf(
stderr,
"0-RTT exchange unexpected succeeded.\n");
775 "SSL_get_error did not signal SSL_ERROR_EARLY_DATA_REJECTED.\n");
782 fprintf(
stderr,
"SSL_in_early_data returned false before reset.\n");
789 if (!
config->expect_early_data_reason.empty()) {
791 "Test error: client reject -expect-early-data-reason flags "
792 "should be configured with -on-retry, not -on-resume.\n");
803 fprintf(
stderr,
"SSL_in_early_data returned true after reset.\n");
818 if (
config->expect_no_ech_retry_configs ||
819 !
config->expect_ech_retry_configs.empty()) {
820 bssl::Span<const uint8_t> expected =
821 config->expect_no_ech_retry_configs
822 ? bssl::Span<const uint8_t>()
824 config->expect_ech_retry_configs.data()),
825 config->expect_ech_retry_configs.size());
827 fprintf(
stderr,
"Expected ECH rejection, but connection succeeded.\n");
834 fprintf(
stderr,
"Expected ECH rejection, but connection succeeded.\n");
838 size_t retry_configs_len;
841 fprintf(
stderr,
"ECH retry configs did not match expectations.\n");
864 if (!
config->expect_msg_callback.empty() &&
866 config->expect_msg_callback) {
867 fprintf(
stderr,
"Bad message callback trace. Wanted:\n%s\nGot:\n%s\n",
868 config->expect_msg_callback.c_str(),
876 static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session,
877 bssl::UniquePtr<SSL> *ssl_uniqueptr,
881 SSL *ssl = ssl_uniqueptr->get();
885 if (!
config->implicit_handshake) {
887 #if defined(HANDSHAKER_SUPPORTED)
888 if (!DoSplitHandshake(ssl_uniqueptr,
writer, is_resume)) {
891 ssl = ssl_uniqueptr->get();
894 fprintf(
stderr,
"The external handshaker can only be used on Linux\n");
905 if (
config->forbid_renegotiation_after_handshake) {
915 if (is_resume && !is_retry && !
config->is_server &&
917 fprintf(
stderr,
"Client unexpectedly offered early data.\n");
921 if (
config->handshake_twice) {
931 if (
config->no_op_extra_handshake &&
933 fprintf(
stderr,
"Extra SSL_do_handshake was not a no-op.\n");
937 if (
config->early_write_after_message != 0) {
940 "-early-write-after-message only works for 0-RTT connections "
946 "-early-write-after-message requires -shim-writes-first and "
958 config->early_write_after_message &&
961 fprintf(
stderr,
"Handshake unexpectedly succeeded.\n");
965 config->early_write_after_message) {
977 if (
config->export_keying_material > 0) {
978 std::vector<uint8_t>
result(
979 static_cast<size_t>(
config->export_keying_material));
982 config->export_label.size(),
983 reinterpret_cast<const uint8_t *
>(
config->export_context.data()),
984 config->export_context.size(),
config->use_export_context)) {
985 fprintf(
stderr,
"failed to export keying material\n");
993 if (
config->export_traffic_secrets) {
994 bssl::Span<const uint8_t> read_secret, write_secret;
996 fprintf(
stderr,
"failed to export traffic secrets\n");
1000 assert(read_secret.size() <= 0xffff);
1001 assert(write_secret.size() == read_secret.size());
1002 const uint16_t secret_len = read_secret.size();
1003 if (
WriteAll(ssl, &secret_len,
sizeof(secret_len)) < 0 ||
1004 WriteAll(ssl, read_secret.data(), read_secret.size()) < 0 ||
1005 WriteAll(ssl, write_secret.data(), write_secret.size()) < 0) {
1010 if (
config->tls_unique) {
1012 size_t tls_unique_len;
1014 sizeof(tls_unique))) {
1015 fprintf(
stderr,
"failed to get tls-unique\n");
1019 if (tls_unique_len != 12) {
1020 fprintf(
stderr,
"expected 12 bytes of tls-unique but got %u",
1021 static_cast<unsigned>(tls_unique_len));
1025 if (
WriteAll(ssl, tls_unique, tls_unique_len) < 0) {
1030 if (
config->send_alert) {
1037 if (
config->write_different_record_sizes) {
1039 fprintf(
stderr,
"write_different_record_sizes not supported for DTLS\n");
1044 static const size_t kBufLen = 32769;
1045 std::unique_ptr<uint8_t[]>
buf(
new uint8_t[kBufLen]);
1047 static const size_t kRecordSizes[] = {
1048 0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1050 const size_t len = kRecordSizes[
i];
1051 if (
len > kBufLen) {
1052 fprintf(
stderr,
"Bad kRecordSizes value.\n");
1060 static const char kInitialWrite[] =
"hello";
1061 bool pending_initial_write =
false;
1062 if (
config->read_with_unfinished_write) {
1064 fprintf(
stderr,
"-read-with-unfinished-write requires -async.\n");
1069 "-read-with-unfinished-write is incompatible with QUIC.\n");
1076 SSL_write(ssl, kInitialWrite, strlen(kInitialWrite));
1078 fprintf(
stderr,
"Failed to leave unfinished write.\n");
1081 pending_initial_write =
true;
1082 }
else if (
config->shim_writes_first) {
1083 if (
WriteAll(ssl, kInitialWrite, strlen(kInitialWrite)) < 0) {
1087 if (!
config->shim_shuts_down) {
1092 if (
config->read_size > 0) {
1102 fprintf(
stderr,
"Invalid SSL_get_error output\n");
1109 fprintf(
stderr,
"Invalid SSL_get_error output\n");
1116 fprintf(
stderr,
"Invalid SSL_get_error output\n");
1120 if (!
config->is_server && is_resume && !is_retry &&
1121 config->expect_reject_early_data) {
1123 "Unexpectedly received data instead of 0-RTT reject.\n");
1131 fprintf(
stderr,
"handshake was not completed after SSL_read\n");
1136 if (pending_initial_write) {
1137 if (
WriteAll(ssl, kInitialWrite, strlen(kInitialWrite)) < 0) {
1140 pending_initial_write =
false;
1143 if (
config->key_update &&
1145 fprintf(
stderr,
"SSL_key_update failed.\n");
1149 for (
int i = 0;
i <
n;
i++) {
1160 !
config->implicit_handshake &&
1164 fprintf(
stderr,
"new session was established after the handshake\n");
1169 bool expect_new_session =
1170 !
config->expect_no_session && !
config->shim_shuts_down;
1171 if (expect_new_session !=
test_state->got_new_session) {
1173 "new session was%s cached, but we expected the opposite\n",
1178 if (expect_new_session) {
1179 bool got_early_data =
1180 test_state->new_session->ticket_max_early_data != 0;
1181 if (
config->expect_ticket_supports_early_data != got_early_data) {
1183 "new session did%s support early data, but we expected the "
1185 got_early_data ?
"" :
" not");
1197 if (
config->shim_shuts_down &&
config->check_close_notify) {
1202 fprintf(
stderr,
"Unexpected SSL_shutdown result: %d != 0\n",
ret);
1209 fprintf(
stderr,
"Unexpected SSL_shutdown result: %d != 1\n",
ret);
1216 "Renegotiations should never produce resumable sessions.\n");
1221 fprintf(
stderr,
"Renegotiations should never resume sessions.\n");
1234 fprintf(
stderr,
"Expected %d renegotiations, got %d\n",
1239 if (
config->renegotiate_explicit &&
1242 fprintf(
stderr,
"Performed %d renegotiations, but triggered %d of them\n",
1261 #if defined(OPENSSL_WINDOWS)
1263 WORD wsa_version = MAKEWORD(2, 2);
1265 int wsa_err = WSAStartup(wsa_version, &wsa_data);
1267 fprintf(
stderr,
"WSAStartup failed: %d\n", wsa_err);
1270 if (wsa_data.wVersion != wsa_version) {
1271 fprintf(
stderr,
"Didn't get expected version: %x\n", wsa_data.wVersion);
1275 signal(SIGPIPE, SIG_IGN);
1280 TestConfig initial_config, resume_config, retry_config;
1281 if (!
ParseConfig(argc - 1, argv + 1,
true, &initial_config,
1282 &resume_config, &retry_config)) {
1283 return Usage(argv[0]);
1287 #if defined(HANDSHAKER_SUPPORTED)
1296 #if defined(OPENSSL_WINDOWS)
1297 fprintf(
stderr,
"-wait-for-debugger is not supported on Windows.\n");
1305 bssl::UniquePtr<SSL_CTX> ssl_ctx;
1307 bssl::UniquePtr<SSL_SESSION> session;
1309 bool is_resume =
i > 0;
1311 ssl_ctx =
config->SetupCtx(ssl_ctx.get());
1317 if (is_resume && !initial_config.
is_server && !session) {
1318 fprintf(
stderr,
"No session to offer.\n");
1322 bssl::UniquePtr<SSL_SESSION> offer_session =
std::move(session);
1325 fprintf(
stderr,
"Error writing settings.\n");
1329 is_resume, offer_session.get(), &
writer);
1331 fprintf(
stderr,
"Error writing settings.\n");
1335 fprintf(
stderr,
"Connection %d failed.\n",
i + 1);
1340 if (
config->resumption_delay != 0) {