#include #include #include #include #include "../../helpers/NetTlsSupport.hpp" #include #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace std::chrono_literals; using Void::Testing::NetSupport::GeneratedCert; using Void::Testing::NetSupport::LoopbackListener; using Void::Testing::NetSupport::ScratchDir; struct TlsServer { LoopbackListener listener; SSL_CTX* ctx{nullptr}; std::thread worker; TlsServer(const GeneratedCert& cert, std::function behavior) { ctx = SSL_CTX_new(TLS_server_method()); Void::Testing::require(ctx != nullptr); Void::Testing::require( SSL_CTX_use_certificate_file( ctx, cert.cert_file.c_str(), SSL_FILETYPE_PEM) == 1); Void::Testing::require( SSL_CTX_use_PrivateKey_file( ctx, cert.key_file.c_str(), SSL_FILETYPE_PEM) == 1); worker = std::thread( [this, behavior = std::move(behavior)] { const int connection = listener.accept_one(); if (connection < 0) { return; } SSL* ssl = SSL_new(ctx); SSL_set_fd(ssl, connection); if (SSL_accept(ssl) == 1) { behavior(ssl); SSL_shutdown(ssl); } SSL_free(ssl); ::close(connection); }); } TlsServer(const TlsServer&) = delete; TlsServer& operator=(const TlsServer&) = delete; ~TlsServer() { if (worker.joinable()) { worker.join(); } if (ctx != nullptr) { SSL_CTX_free(ctx); } } }; /// Accepts a TCP connection and never speaks TLS, so handshake waits. struct StallServer { LoopbackListener listener; std::thread worker; StallServer() { worker = std::thread([this] { const int connection = listener.accept_one(); if (connection < 0) { return; } char byte = 0; while (::recv(connection, &byte, 1, 0) > 0) { } ::close(connection); }); } StallServer(const StallServer&) = delete; StallServer& operator=(const StallServer&) = delete; ~StallServer() { if (worker.joinable()) { worker.join(); } } }; [[nodiscard]] Void::Net::TlsClientOptions make_options(std::uint16_t port, std::string ca_file = {}) { Void::Net::TlsClientOptions options; options.host = "127.0.0.1"; options.port = port; options.ca_file = std::move(ca_file); options.connect_timeout = 2s; options.handshake_timeout = 5s; options.io_timeout = 5s; return options; } [[nodiscard]] std::string read_text(Void::Net::TlsClientStream& stream, std::size_t expected) { std::string text; std::vector buffer(expected); while (text.size() < expected) { auto span = std::span{buffer}.subspan( text.size()); auto result = stream.read(span, 5s); Void::Testing::require(result.has_value()); if (*result == 0) { break; } text.append(reinterpret_cast(span.data()), *result); } return text; } } // namespace Void::Testing::Test tls_client_verifies_trust_store_and_hostname() { ScratchDir scratch{"void_tls_trust_"}; const auto good = Void::Testing::NetSupport::generate_self_signed_cert( scratch.path, "IP:127.0.0.1", "good"); // a) Untrusted self-signed peer is rejected by the default trust store. { TlsServer server{good, [](SSL* ssl) { const char* text = "untrusted"; (void)SSL_write(ssl, text, 9); }}; auto stream = Void::Net::TlsClientStream::connect( make_options(server.listener.port)); Void::Testing::require(!stream.has_value()); Void::Testing::expect_eq( stream.error().code().value, static_cast( Void::Net::NetErrorCode::tls_error)); } // b) Trusted CA + matching IP SAN succeeds. { TlsServer server{good, [](SSL* ssl) { const char* text = "trusted-ok"; (void)SSL_write(ssl, text, 10); }}; auto stream = Void::Net::TlsClientStream::connect( make_options(server.listener.port, good.cert_file.string())); Void::Testing::require(stream.has_value()); Void::Testing::expect(stream->secure()); Void::Testing::expect_eq(read_text(*stream, 10), std::string{"trusted-ok"}); } // c) Trusted CA but hostname mismatch is still rejected. { TlsServer server{good, [](SSL* ssl) { const char* text = "mismatch"; (void)SSL_write(ssl, text, 8); }}; auto options = make_options(server.listener.port, good.cert_file.string()); options.server_name = "wrong.invalid"; auto stream = Void::Net::TlsClientStream::connect(options); Void::Testing::require(!stream.has_value()); Void::Testing::expect_eq( stream.error().code().value, static_cast( Void::Net::NetErrorCode::tls_error)); } // d) An unreadable trust store is a hard failure, never a downgrade. { TlsServer server{good, [](SSL* ssl) { const char* text = "bad-store"; (void)SSL_write(ssl, text, 9); }}; auto stream = Void::Net::TlsClientStream::connect( make_options(server.listener.port, (scratch.path / "missing-ca.pem").string())); Void::Testing::require(!stream.has_value()); Void::Testing::expect_eq( stream.error().code().value, static_cast( Void::Net::NetErrorCode::tls_error)); } } Void::Testing::Test tls_client_never_falls_back_to_plaintext() { // A plain TCP peer speaking garbage must fail the handshake and leave // the stream closed, not usable in plaintext. LoopbackListener listener; std::thread worker([&listener] { const int connection = listener.accept_one(); if (connection < 0) { return; } const std::string garbage = "this is not a TLS server hello"; (void)::send(connection, garbage.data(), garbage.size(), MSG_NOSIGNAL); char buffer[64]; while (::recv(connection, buffer, sizeof(buffer), 0) > 0) { } ::close(connection); }); auto stream = Void::Net::TlsClientStream::connect( make_options(listener.port)); Void::Testing::require(!stream.has_value()); Void::Testing::expect_eq( stream.error().code().value, static_cast(Void::Net::NetErrorCode::tls_error)); worker.join(); } Void::Testing::Test tls_client_deadline_and_cancellation() { // Handshake deadline. { StallServer server; auto options = make_options(server.listener.port); options.handshake_timeout = 100ms; const auto start = std::chrono::steady_clock::now(); auto stream = Void::Net::TlsClientStream::connect(options); const auto elapsed = std::chrono::steady_clock::now() - start; Void::Testing::require(!stream.has_value()); Void::Testing::expect_eq( stream.error().code().value, static_cast(Void::Net::NetErrorCode::timeout)); Void::Testing::expect(elapsed < 3s); } // Cancellation while the handshake is waiting. { StallServer server; auto options = make_options(server.listener.port); std::stop_source stop; options.stop = stop.get_token(); std::jthread killer([&stop] { std::this_thread::sleep_for(50ms); stop.request_stop(); }); const auto start = std::chrono::steady_clock::now(); auto stream = Void::Net::TlsClientStream::connect(options); const auto elapsed = std::chrono::steady_clock::now() - start; killer.join(); Void::Testing::require(!stream.has_value()); Void::Testing::expect_eq( stream.error().code().value, static_cast( Void::Net::NetErrorCode::cancelled)); Void::Testing::expect(elapsed < 3s); } } Void::Testing::Test tls_client_partial_writes_complete() { ScratchDir scratch{"void_tls_write_"}; const auto cert = Void::Testing::NetSupport::generate_self_signed_cert( scratch.path, "IP:127.0.0.1", "write"); constexpr std::size_t kTotal = 4U * 1024U * 1024U; std::atomic received{0}; std::atomic done{false}; TlsServer server{ cert, [&received, &done](SSL* ssl) { std::vector buffer(8U * 1024U); while (received.load(std::memory_order_acquire) < kTotal) { const int count = SSL_read(ssl, buffer.data(), static_cast(buffer.size())); if (count <= 0) { break; } received.fetch_add(static_cast(count), std::memory_order_acq_rel); std::this_thread::sleep_for(1ms); } done.store(true, std::memory_order_release); }}; auto stream = Void::Net::TlsClientStream::connect( make_options(server.listener.port, cert.cert_file.string())); Void::Testing::require(stream.has_value()); std::vector payload(kTotal); for (std::size_t i = 0; i < kTotal; ++i) { payload[i] = static_cast(i & 0xFFU); } auto written = stream->write_all(payload, 10s); Void::Testing::expect(written.has_value()); const auto deadline = std::chrono::steady_clock::now() + 10s; while (!done.load(std::memory_order_acquire) && std::chrono::steady_clock::now() < deadline) { std::this_thread::sleep_for(2ms); } Void::Testing::expect_eq(received.load(std::memory_order_acquire), kTotal); (void)stream->shutdown(); } Void::Testing::Test tls_client_starttls_upgrade() { ScratchDir scratch{"void_tls_starttls_"}; const auto cert = Void::Testing::NetSupport::generate_self_signed_cert( scratch.path, "IP:127.0.0.1", "starttls"); LoopbackListener listener; SSL_CTX* ctx = SSL_CTX_new(TLS_server_method()); Void::Testing::require(ctx != nullptr); Void::Testing::require(SSL_CTX_use_certificate_file( ctx, cert.cert_file.c_str(), SSL_FILETYPE_PEM) == 1); Void::Testing::require(SSL_CTX_use_PrivateKey_file( ctx, cert.key_file.c_str(), SSL_FILETYPE_PEM) == 1); std::thread worker([&listener, ctx] { const int connection = listener.accept_one(); if (connection < 0) { return; } auto greeting = Void::Testing::NetSupport::read_until(connection, "\r\n", 256); if (greeting.starts_with("HELLO")) { (void)Void::Testing::NetSupport::write_all(connection, "READY\r\n"); } SSL* ssl = SSL_new(ctx); SSL_set_fd(ssl, connection); if (SSL_accept(ssl) == 1) { char buffer[64]; const int count = SSL_read(ssl, buffer, sizeof(buffer)); if (count > 0) { (void)SSL_write(ssl, buffer, count); } SSL_shutdown(ssl); } SSL_free(ssl); ::close(connection); }); auto options = make_options(listener.port, cert.cert_file.string()); auto stream = Void::Net::TlsClientStream::connect_plain(options); Void::Testing::require(stream.has_value()); Void::Testing::expect(!stream->secure()); Void::Testing::require( stream->write_all(std::string_view{"HELLO\r\n"}).has_value()); Void::Testing::expect_eq(read_text(*stream, 7), std::string{"READY\r\n"}); auto upgraded = stream->start_tls(); Void::Testing::require(upgraded.has_value()); Void::Testing::expect(stream->secure()); Void::Testing::require( stream->write_all(std::string_view{"secure"}).has_value()); Void::Testing::expect_eq(read_text(*stream, 6), std::string{"secure"}); worker.join(); SSL_CTX_free(ctx); }