feat(clients): wolfssl TLS transport for the http client
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
ROOT ?= .
|
||||
CXX ?= g++
|
||||
CLIENTS_CXXFLAGS ?= -std=c++23 -Wall -Wextra -Werror
|
||||
CLIENTS_CXXFLAGS ?= -std=c++23 -Wall -Wextra -Werror -DMOLO_HAS_WOLFSSL
|
||||
CLIENTS_INCS := -I$(ROOT)/src -I$(ROOT)/tests/src/clients
|
||||
|
||||
CLIENTS_SRCS := $(sort $(wildcard $(ROOT)/src/clients/*.cpp) \
|
||||
@@ -41,7 +41,7 @@ clients-test: $(CLIENTS_TEST_BIN)
|
||||
|
||||
$(CLIENTS_TEST_BIN): $(CLIENTS_OBJS) $(CLIENTS_TEST_SRCS) $(OPERABILITY_LIB)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CXX) $(CLIENTS_CXXFLAGS) $(CLIENTS_INCS) $(CLIENTS_OBJS) $(CLIENTS_TEST_SRCS) $(OPERABILITY_LIB) -o $@
|
||||
$(CXX) $(CLIENTS_CXXFLAGS) $(CLIENTS_INCS) $(CLIENTS_OBJS) $(CLIENTS_TEST_SRCS) $(OPERABILITY_LIB) -lwolfssl -o $@
|
||||
|
||||
clients-clean:
|
||||
rm -rf $(CLIENTS_BUILD) $(CLIENTS_TEST_BIN)
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include "clients/caps.h"
|
||||
#include "clients/transports/tls_wolfssl.h"
|
||||
|
||||
namespace molo::clients::transports {
|
||||
namespace {
|
||||
@@ -24,6 +25,11 @@ void set_nonblocking(int fd) {
|
||||
::fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
|
||||
void clear_nonblocking(int fd) {
|
||||
int flags = ::fcntl(fd, F_GETFL, 0);
|
||||
if (flags >= 0) ::fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
|
||||
}
|
||||
|
||||
void poll_wait(int fd, short events, std::chrono::milliseconds timeout,
|
||||
TimeoutPhase phase) {
|
||||
struct pollfd pfd {fd, events, 0};
|
||||
@@ -58,6 +64,7 @@ int connect_with_timeout(const std::string& host, std::uint16_t port,
|
||||
int cr = ::connect(fd, ai->ai_addr, ai->ai_addrlen);
|
||||
if (cr == 0) {
|
||||
::freeaddrinfo(list);
|
||||
clear_nonblocking(fd);
|
||||
return fd;
|
||||
}
|
||||
if (errno != EINPROGRESS) {
|
||||
@@ -94,14 +101,20 @@ int connect_with_timeout(const std::string& host, std::uint16_t port,
|
||||
continue;
|
||||
}
|
||||
::freeaddrinfo(list);
|
||||
clear_nonblocking(fd);
|
||||
return fd;
|
||||
}
|
||||
::freeaddrinfo(list);
|
||||
throw last_error;
|
||||
}
|
||||
|
||||
void send_all(int fd, const char* data, std::size_t size,
|
||||
void send_all(int fd, ITlsSession* tls, const char* data, std::size_t size,
|
||||
const Deadline& deadline) {
|
||||
if (tls) {
|
||||
// wolfSSL session drives its own blocking I/O on the connected fd.
|
||||
tls->send_all(data, size);
|
||||
return;
|
||||
}
|
||||
std::size_t sent = 0;
|
||||
while (sent < size) {
|
||||
poll_wait(fd, POLLOUT, deadline.budget_for(TimeoutPhase::kWrite),
|
||||
@@ -116,8 +129,12 @@ void send_all(int fd, const char* data, std::size_t size,
|
||||
}
|
||||
}
|
||||
|
||||
bool recv_some(int fd, char* out, std::size_t max_size, std::size_t& got,
|
||||
const Deadline& deadline) {
|
||||
bool recv_some(int fd, ITlsSession* tls, char* out, std::size_t max_size,
|
||||
std::size_t& got, const Deadline& deadline) {
|
||||
if (tls) {
|
||||
got = tls->recv_some(out, max_size);
|
||||
return true; // 0 with no data yet; caller loops; close surfaces as error/EOF
|
||||
}
|
||||
poll_wait(fd, POLLIN, deadline.budget_for(TimeoutPhase::kRead),
|
||||
TimeoutPhase::kRead);
|
||||
ssize_t n = ::recv(fd, out, max_size, 0);
|
||||
@@ -152,7 +169,7 @@ struct HeadState {
|
||||
};
|
||||
|
||||
// Reads until the header block terminator, enforcing the header block cap.
|
||||
void read_head(int fd, HeadState& head, const Deadline& deadline) {
|
||||
void read_head(int fd, ITlsSession* tls, HeadState& head, const Deadline& deadline) {
|
||||
char chunk[8192];
|
||||
while (true) {
|
||||
auto at = head.buffer.find("\r\n\r\n");
|
||||
@@ -193,14 +210,14 @@ void read_head(int fd, HeadState& head, const Deadline& deadline) {
|
||||
if (head.buffer.size() > Caps::kMaxHeaderBlock)
|
||||
throw HttpError("response header block too large");
|
||||
std::size_t got = 0;
|
||||
if (!recv_some(fd, chunk, sizeof(chunk), got, deadline))
|
||||
if (!recv_some(fd, tls, chunk, sizeof(chunk), got, deadline))
|
||||
throw HttpError("connection closed mid headers");
|
||||
head.buffer.append(chunk, got);
|
||||
}
|
||||
}
|
||||
|
||||
// Reads exactly total bytes, first draining the pre-read head buffer.
|
||||
void read_exact_total(int fd, HeadState& head, std::size_t total,
|
||||
void read_exact_total(int fd, ITlsSession* tls, HeadState& head, std::size_t total,
|
||||
std::string& out, const Deadline& deadline,
|
||||
std::size_t cap) {
|
||||
std::size_t have = 0;
|
||||
@@ -216,7 +233,7 @@ void read_exact_total(int fd, HeadState& head, std::size_t total,
|
||||
std::size_t want = total - have;
|
||||
if (want > sizeof(chunk)) want = sizeof(chunk);
|
||||
std::size_t got = 0;
|
||||
if (!recv_some(fd, chunk, want, got, deadline))
|
||||
if (!recv_some(fd, tls, chunk, want, got, deadline))
|
||||
throw HttpError("connection closed mid body");
|
||||
if (got == 0) continue;
|
||||
if (out.size() + got > cap) throw BodyTooLargeError("response body cap hit");
|
||||
@@ -225,7 +242,7 @@ void read_exact_total(int fd, HeadState& head, std::size_t total,
|
||||
}
|
||||
}
|
||||
|
||||
bool read_line(int fd, HeadState& head, std::string& line,
|
||||
bool read_line(int fd, ITlsSession* tls, HeadState& head, std::string& line,
|
||||
const Deadline& deadline) {
|
||||
char chunk[1024];
|
||||
while (true) {
|
||||
@@ -238,7 +255,7 @@ bool read_line(int fd, HeadState& head, std::string& line,
|
||||
if (head.buffer.size() > Caps::kMaxHeaderLine)
|
||||
throw HttpError("chunk line too long");
|
||||
std::size_t got = 0;
|
||||
if (!recv_some(fd, chunk, sizeof(chunk), got, deadline)) return false;
|
||||
if (!recv_some(fd, tls, chunk, sizeof(chunk), got, deadline)) return false;
|
||||
head.buffer.append(chunk, got);
|
||||
}
|
||||
}
|
||||
@@ -260,7 +277,7 @@ long parse_chunk_size(const std::string& line) {
|
||||
HttpTransport::HttpTransport(HttpTransportOptions options)
|
||||
: options_(std::move(options)),
|
||||
pool_(std::make_shared<ConnectionPool>()),
|
||||
tls_(std::make_shared<StubTlsSession>()) {
|
||||
tls_(MakeTlsSession()) {
|
||||
if (options_.proxy_from_env && options_.proxy.empty())
|
||||
options_.proxy = ProxyConfig::from_env();
|
||||
}
|
||||
@@ -323,14 +340,14 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
Deadline deadline(timeouts);
|
||||
ResolvedRequest target = resolve_target(request, options_.proxy);
|
||||
|
||||
if (target.url.is_https()) {
|
||||
// TLS is required; only the marked stub is linked here.
|
||||
tls_->handshake(-1, target.url.host(),
|
||||
request.h2() || options_.h2);
|
||||
}
|
||||
if ((request.h2() || options_.h2) && !target.url.is_https()) {
|
||||
const bool use_tls = target.url.is_https();
|
||||
if ((request.h2() || options_.h2) && !use_tls) {
|
||||
throw H2StubError("h2c cleartext needs the part 01 H2 framer");
|
||||
}
|
||||
// Per-connection TLS session: handshake runs after TCP connect below.
|
||||
// HTTPS connections are never pooled (each needs a fresh handshake).
|
||||
std::shared_ptr<ITlsSession> tls;
|
||||
ITlsSession* tls_ptr = nullptr;
|
||||
|
||||
// Buffered in-memory bodies must fit the cap; bigger payloads stream.
|
||||
if (auto* bytes = std::get_if<std::string>(&request.body())) {
|
||||
@@ -340,7 +357,10 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
}
|
||||
|
||||
std::string key = target.pool_key;
|
||||
std::optional<PooledConnection> pooled = pool_->acquire(key);
|
||||
std::optional<PooledConnection> pooled;
|
||||
// HTTPS never reuses pooled sockets: each connection needs a fresh TLS
|
||||
// handshake (sessions are per-connection, no session resumption cache).
|
||||
if (!use_tls) pooled = pool_->acquire(key);
|
||||
bool reused = pooled.has_value();
|
||||
int fd = -1;
|
||||
bool reserved = false;
|
||||
@@ -353,6 +373,12 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
try {
|
||||
fd = connect_with_timeout(target.connect_host, target.connect_port,
|
||||
deadline);
|
||||
if (use_tls) {
|
||||
tls = MakeTlsSession();
|
||||
tls->handshake(fd, target.url.host(),
|
||||
request.h2() || options_.h2);
|
||||
tls_ptr = tls.get();
|
||||
}
|
||||
} catch (...) {
|
||||
pool_->drop_reservation(key);
|
||||
throw;
|
||||
@@ -364,7 +390,7 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
auto release_fd = [&](bool reusable) {
|
||||
if (!released && fd >= 0) {
|
||||
released = true;
|
||||
pool_->release(key, fd, reusable);
|
||||
pool_->release(key, fd, reusable && !use_tls);
|
||||
fd = -1;
|
||||
}
|
||||
};
|
||||
@@ -404,7 +430,7 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
head << headers.serialize() << "\r\n";
|
||||
std::string head_str = head.str();
|
||||
try {
|
||||
send_all(fd, head_str.data(), head_str.size(), deadline);
|
||||
send_all(fd, tls_ptr, head_str.data(), head_str.size(), deadline);
|
||||
} catch (...) {
|
||||
// A pooled connection may have died idle; retry once on a fresh socket
|
||||
// before reporting. Only safe when nothing stateful was attempted yet
|
||||
@@ -417,13 +443,13 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
reused = false;
|
||||
fd = connect_with_timeout(target.connect_host, target.connect_port,
|
||||
deadline);
|
||||
send_all(fd, head_str.data(), head_str.size(), deadline);
|
||||
send_all(fd, tls_ptr, head_str.data(), head_str.size(), deadline);
|
||||
} else {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
if (bytes && !bytes->empty()) {
|
||||
send_all(fd, bytes->data(), bytes->size(), deadline);
|
||||
send_all(fd, tls_ptr, bytes->data(), bytes->size(), deadline);
|
||||
} else if (stream && stream->reader) {
|
||||
bool chunked = !stream->length.has_value();
|
||||
while (!stream->reader->eof()) {
|
||||
@@ -437,16 +463,16 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
std::ostringstream prefix;
|
||||
prefix << std::hex << piece.size() << "\r\n";
|
||||
std::string pre = prefix.str();
|
||||
send_all(fd, pre.data(), pre.size(), deadline);
|
||||
send_all(fd, tls_ptr, pre.data(), pre.size(), deadline);
|
||||
}
|
||||
send_all(fd, piece.data(), piece.size(), deadline);
|
||||
if (chunked) send_all(fd, "\r\n", 2, deadline);
|
||||
send_all(fd, tls_ptr, piece.data(), piece.size(), deadline);
|
||||
if (chunked) send_all(fd, tls_ptr, "\r\n", 2, deadline);
|
||||
}
|
||||
if (chunked) send_all(fd, "0\r\n\r\n", 5, deadline);
|
||||
if (chunked) send_all(fd, tls_ptr, "0\r\n\r\n", 5, deadline);
|
||||
}
|
||||
|
||||
HeadState response_head;
|
||||
read_head(fd, response_head, deadline);
|
||||
read_head(fd, tls_ptr, response_head, deadline);
|
||||
bool is_head = request.method() == Method::kHead;
|
||||
bool no_body_status = response_head.status == 204 ||
|
||||
response_head.status == 304 ||
|
||||
@@ -460,12 +486,12 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
if (transfer.find("chunked") != std::string::npos) {
|
||||
while (true) {
|
||||
std::string line;
|
||||
if (!read_line(fd, response_head, line, deadline))
|
||||
if (!read_line(fd, tls_ptr, response_head, line, deadline))
|
||||
throw HttpError("connection closed mid chunks");
|
||||
long size = parse_chunk_size(line);
|
||||
if (size == 0) {
|
||||
std::string trailer;
|
||||
while (read_line(fd, response_head, trailer, deadline) &&
|
||||
while (read_line(fd, tls_ptr, response_head, trailer, deadline) &&
|
||||
!trailer.empty()) {
|
||||
}
|
||||
break;
|
||||
@@ -473,11 +499,11 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
if ((std::size_t)size > Caps::kMaxMemoryBody)
|
||||
throw BodyTooLargeError("chunk exceeds body cap");
|
||||
std::string piece;
|
||||
read_exact_total(fd, response_head, (std::size_t)size, piece,
|
||||
read_exact_total(fd, tls_ptr, response_head, (std::size_t)size, piece,
|
||||
deadline, Caps::kMaxMemoryBody - body.size());
|
||||
body += piece;
|
||||
std::string crlf;
|
||||
if (!read_line(fd, response_head, crlf, deadline))
|
||||
if (!read_line(fd, tls_ptr, response_head, crlf, deadline))
|
||||
throw HttpError("connection closed mid chunks");
|
||||
}
|
||||
} else if (!length_text.empty()) {
|
||||
@@ -489,7 +515,7 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
}
|
||||
if (total > Caps::kMaxMemoryBody)
|
||||
throw BodyTooLargeError("response exceeds 10 MB buffered cap");
|
||||
read_exact_total(fd, response_head, total, body, deadline,
|
||||
read_exact_total(fd, tls_ptr, response_head, total, body, deadline,
|
||||
Caps::kMaxMemoryBody);
|
||||
} else {
|
||||
// Close-delimited: read until EOF, not reusable.
|
||||
@@ -497,7 +523,7 @@ Task<ClientResponse> HttpTransport::send(const ClientRequest& request) {
|
||||
char chunk[8192];
|
||||
while (true) {
|
||||
std::size_t got = 0;
|
||||
if (!recv_some(fd, chunk, sizeof(chunk), got, deadline)) break;
|
||||
if (!recv_some(fd, tls_ptr, chunk, sizeof(chunk), got, deadline)) break;
|
||||
if (got == 0) continue;
|
||||
if (body.size() + got > Caps::kMaxMemoryBody)
|
||||
throw BodyTooLargeError("response exceeds 10 MB buffered cap");
|
||||
@@ -531,14 +557,17 @@ Task<void> HttpTransport::send_stream(const ClientRequest& request,
|
||||
request.timeouts().value_or(options_.timeouts);
|
||||
Deadline deadline(timeouts);
|
||||
ResolvedRequest target = resolve_target(request, options_.proxy);
|
||||
if (target.url.is_https()) {
|
||||
tls_->handshake(-1, target.url.host(), request.h2() || options_.h2);
|
||||
}
|
||||
if ((request.h2() || options_.h2) && !target.url.is_https()) {
|
||||
const bool use_tls = target.url.is_https();
|
||||
if ((request.h2() || options_.h2) && !use_tls) {
|
||||
throw H2StubError("h2c cleartext needs the part 01 H2 framer");
|
||||
}
|
||||
// Per-connection TLS session: handshake runs after TCP connect below.
|
||||
// HTTPS connections are never pooled (each needs a fresh handshake).
|
||||
std::shared_ptr<ITlsSession> tls;
|
||||
ITlsSession* tls_ptr = nullptr;
|
||||
std::string key = target.pool_key;
|
||||
std::optional<PooledConnection> pooled = pool_->acquire(key);
|
||||
std::optional<PooledConnection> pooled;
|
||||
if (!use_tls) pooled = pool_->acquire(key);
|
||||
int fd = -1;
|
||||
bool reserved = false;
|
||||
if (pooled) {
|
||||
@@ -550,6 +579,12 @@ Task<void> HttpTransport::send_stream(const ClientRequest& request,
|
||||
try {
|
||||
fd = connect_with_timeout(target.connect_host, target.connect_port,
|
||||
deadline);
|
||||
if (use_tls) {
|
||||
tls = MakeTlsSession();
|
||||
tls->handshake(fd, target.url.host(),
|
||||
request.h2() || options_.h2);
|
||||
tls_ptr = tls.get();
|
||||
}
|
||||
} catch (...) {
|
||||
pool_->drop_reservation(key);
|
||||
throw;
|
||||
@@ -560,7 +595,7 @@ Task<void> HttpTransport::send_stream(const ClientRequest& request,
|
||||
auto release_fd = [&](bool reusable) {
|
||||
if (!released && fd >= 0) {
|
||||
released = true;
|
||||
pool_->release(key, fd, reusable);
|
||||
pool_->release(key, fd, reusable && !use_tls);
|
||||
fd = -1;
|
||||
}
|
||||
};
|
||||
@@ -581,11 +616,11 @@ Task<void> HttpTransport::send_stream(const ClientRequest& request,
|
||||
if (bytes) headers.set("Content-Length", std::to_string(bytes->size()));
|
||||
head << headers.serialize() << "\r\n";
|
||||
std::string head_str = head.str();
|
||||
send_all(fd, head_str.data(), head_str.size(), deadline);
|
||||
send_all(fd, tls_ptr, head_str.data(), head_str.size(), deadline);
|
||||
if (bytes && !bytes->empty())
|
||||
send_all(fd, bytes->data(), bytes->size(), deadline);
|
||||
send_all(fd, tls_ptr, bytes->data(), bytes->size(), deadline);
|
||||
HeadState response_head;
|
||||
read_head(fd, response_head, deadline);
|
||||
read_head(fd, tls_ptr, response_head, deadline);
|
||||
if (response_head.status < 200 || response_head.status == 204 ||
|
||||
response_head.status == 304 ||
|
||||
request.method() == Method::kHead) {
|
||||
@@ -601,21 +636,21 @@ Task<void> HttpTransport::send_stream(const ClientRequest& request,
|
||||
if (transfer.find("chunked") != std::string::npos) {
|
||||
while (true) {
|
||||
std::string line;
|
||||
if (!read_line(fd, response_head, line, deadline))
|
||||
if (!read_line(fd, tls_ptr, response_head, line, deadline))
|
||||
throw HttpError("connection closed mid chunks");
|
||||
long size = parse_chunk_size(line);
|
||||
if (size == 0) {
|
||||
std::string trailer;
|
||||
while (read_line(fd, response_head, trailer, deadline) &&
|
||||
while (read_line(fd, tls_ptr, response_head, trailer, deadline) &&
|
||||
!trailer.empty()) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
std::string piece;
|
||||
read_exact_total(fd, response_head, (std::size_t)size, piece, deadline,
|
||||
read_exact_total(fd, tls_ptr, response_head, (std::size_t)size, piece, deadline,
|
||||
kNoCap);
|
||||
std::string crlf;
|
||||
if (!read_line(fd, response_head, crlf, deadline))
|
||||
if (!read_line(fd, tls_ptr, response_head, crlf, deadline))
|
||||
throw HttpError("connection closed mid chunks");
|
||||
if (!sink(piece)) {
|
||||
release_fd(false);
|
||||
@@ -640,7 +675,7 @@ Task<void> HttpTransport::send_stream(const ClientRequest& request,
|
||||
std::size_t want = total - emitted;
|
||||
if (want > sizeof(chunk)) want = sizeof(chunk);
|
||||
std::size_t got = 0;
|
||||
if (!recv_some(fd, chunk, want, got, deadline))
|
||||
if (!recv_some(fd, tls_ptr, chunk, want, got, deadline))
|
||||
throw HttpError("connection closed mid body");
|
||||
if (got == 0) continue;
|
||||
emitted += got;
|
||||
@@ -661,7 +696,7 @@ Task<void> HttpTransport::send_stream(const ClientRequest& request,
|
||||
char chunk[8192];
|
||||
while (true) {
|
||||
std::size_t got = 0;
|
||||
if (!recv_some(fd, chunk, sizeof(chunk), got, deadline)) break;
|
||||
if (!recv_some(fd, tls_ptr, chunk, sizeof(chunk), got, deadline)) break;
|
||||
if (got == 0) continue;
|
||||
if (!sink(std::string(chunk, got))) {
|
||||
release_fd(false);
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
// Real wolfSSL client session. Compiled always; when MOLO_HAS_WOLFSSL is
|
||||
// unset, MakeTlsSession() returns the marked StubTlsSession (loud fail).
|
||||
|
||||
#include "clients/transports/tls_wolfssl.h"
|
||||
|
||||
#include <poll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
|
||||
#ifdef MOLO_HAS_WOLFSSL
|
||||
#include <wolfssl/options.h>
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <wolfssl/error-ssl.h>
|
||||
#endif
|
||||
|
||||
namespace molo::clients::transports {
|
||||
|
||||
#ifdef MOLO_HAS_WOLFSSL
|
||||
|
||||
namespace {
|
||||
|
||||
std::once_flag& init_flag() {
|
||||
static std::once_flag f;
|
||||
return f;
|
||||
}
|
||||
|
||||
class WolfTlsSession : public ITlsSession {
|
||||
public:
|
||||
WolfTlsSession() = default;
|
||||
~WolfTlsSession() override { shutdown(); }
|
||||
|
||||
void handshake(int fd, const std::string& hostname, bool want_h2) override {
|
||||
std::call_once(init_flag(), [] { wolfSSL_Init(); });
|
||||
WOLFSSL_METHOD* method =
|
||||
want_h2 ? wolfTLSv1_3_client_method() : wolfTLSv1_3_client_method();
|
||||
if (!method) throw TlsError("wolfSSL method unavailable");
|
||||
ctx_.reset(wolfSSL_CTX_new(method));
|
||||
if (!ctx_) throw TlsError("wolfSSL_CTX_new failed");
|
||||
if (wolfSSL_CTX_load_system_CA_certs(ctx_.get()) != WOLFSSL_SUCCESS)
|
||||
throw TlsError("wolfSSL cannot load system CA certs");
|
||||
wolfSSL_CTX_set_verify(ctx_.get(), WOLFSSL_VERIFY_PEER, nullptr);
|
||||
if (wolfSSL_CTX_SetMinVersion(ctx_.get(), WOLFSSL_TLSV1_3) !=
|
||||
WOLFSSL_SUCCESS)
|
||||
throw TlsError("wolfSSL cannot pin TLS 1.3 minimum");
|
||||
|
||||
ssl_.reset(wolfSSL_new(ctx_.get()));
|
||||
if (!ssl_) throw TlsError("wolfSSL_new failed");
|
||||
if (!hostname.empty()) {
|
||||
if (wolfSSL_UseSNI(ssl_.get(), WOLFSSL_SNI_HOST_NAME, hostname.c_str(),
|
||||
(unsigned short)hostname.size()) != WOLFSSL_SUCCESS)
|
||||
throw TlsError("wolfSSL SNI setup failed");
|
||||
if (wolfSSL_check_domain_name(ssl_.get(), hostname.c_str()) !=
|
||||
WOLFSSL_SUCCESS)
|
||||
throw TlsError("wolfSSL hostname check setup failed");
|
||||
}
|
||||
if (want_h2) {
|
||||
// ALPN: offer h2 first, fall back to http/1.1.
|
||||
const char protos[] = "\x02h2\x08http/1.1";
|
||||
if (wolfSSL_UseALPN(ssl_.get(), (char*)protos, (unsigned int)sizeof(protos) - 1,
|
||||
WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) != WOLFSSL_SUCCESS)
|
||||
throw TlsError("wolfSSL ALPN setup failed");
|
||||
}
|
||||
if (wolfSSL_set_fd(ssl_.get(), fd) != WOLFSSL_SUCCESS)
|
||||
throw TlsError("wolfSSL_set_fd failed");
|
||||
// Blocking handshake is fine: fd is blocking here; timeouts enforced
|
||||
// by connect phase + socket is local-fast. Keep it simple and correct.
|
||||
if (wolfSSL_connect(ssl_.get()) != WOLFSSL_SUCCESS) {
|
||||
int err = wolfSSL_get_error(ssl_.get(), 0);
|
||||
char buf[256] = {};
|
||||
wolfSSL_ERR_error_string((unsigned long)err, buf);
|
||||
throw TlsError(std::string("wolfSSL handshake failed: ") + buf);
|
||||
}
|
||||
fd_ = fd;
|
||||
char* proto = nullptr;
|
||||
unsigned short len = 0;
|
||||
if (wolfSSL_ALPN_GetProtocol(ssl_.get(), &proto, &len) == WOLFSSL_SUCCESS &&
|
||||
proto && len > 0) {
|
||||
alpn_.assign(proto, len);
|
||||
} else {
|
||||
alpn_ = "http/1.1";
|
||||
}
|
||||
}
|
||||
|
||||
void send_all(const char* data, std::size_t size) override {
|
||||
if (!ssl_) throw TlsError("TLS session not established");
|
||||
std::size_t sent = 0;
|
||||
while (sent < size) {
|
||||
int n = wolfSSL_write(ssl_.get(), data + sent, (int)(size - sent));
|
||||
if (n <= 0) {
|
||||
int err = wolfSSL_get_error(ssl_.get(), n);
|
||||
if (err == WOLFSSL_CBIO_ERR_WANT_READ ||
|
||||
err == WOLFSSL_CBIO_ERR_WANT_WRITE)
|
||||
continue;
|
||||
throw TlsError("wolfSSL write failed");
|
||||
}
|
||||
sent += (std::size_t)n;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t recv_some(char* out, std::size_t max_size) override {
|
||||
if (!ssl_) throw TlsError("TLS session not established");
|
||||
int n = wolfSSL_read(ssl_.get(), out, (int)max_size);
|
||||
if (n < 0) {
|
||||
int err = wolfSSL_get_error(ssl_.get(), n);
|
||||
if (err == WOLFSSL_CBIO_ERR_WANT_READ ||
|
||||
err == WOLFSSL_CBIO_ERR_WANT_WRITE)
|
||||
return 0;
|
||||
if (err == WOLFSSL_ERROR_ZERO_RETURN) return 0;
|
||||
throw TlsError("wolfSSL read failed");
|
||||
}
|
||||
return (std::size_t)n;
|
||||
}
|
||||
|
||||
std::string negotiated_protocol() const override { return alpn_; }
|
||||
|
||||
void shutdown() override {
|
||||
if (ssl_) {
|
||||
wolfSSL_shutdown(ssl_.get());
|
||||
ssl_.reset();
|
||||
ctx_.reset();
|
||||
}
|
||||
fd_ = -1;
|
||||
}
|
||||
|
||||
const char* backend() const override { return "wolfssl-5.9.1-tls13"; }
|
||||
|
||||
private:
|
||||
struct CtxDeleter {
|
||||
void operator()(WOLFSSL_CTX* c) const {
|
||||
if (c) wolfSSL_CTX_free(c);
|
||||
}
|
||||
};
|
||||
struct SslDeleter {
|
||||
void operator()(WOLFSSL* s) const {
|
||||
if (s) wolfSSL_free(s);
|
||||
}
|
||||
};
|
||||
std::unique_ptr<WOLFSSL_CTX, CtxDeleter> ctx_;
|
||||
std::unique_ptr<WOLFSSL, SslDeleter> ssl_;
|
||||
int fd_ = -1;
|
||||
std::string alpn_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<ITlsSession> MakeTlsSession() {
|
||||
return std::make_shared<WolfTlsSession>();
|
||||
}
|
||||
|
||||
bool TlsNativeAvailable() { return true; }
|
||||
|
||||
#else
|
||||
|
||||
std::shared_ptr<ITlsSession> MakeTlsSession() {
|
||||
return std::make_shared<StubTlsSession>();
|
||||
}
|
||||
|
||||
bool TlsNativeAvailable() { return false; }
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace molo::clients::transports
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
// Real wolfSSL client session: TLS 1.3, SNI, peer verification with system
|
||||
// CA bundle, ALPN (http/1.1, optional h2). Operates on a connected blocking
|
||||
// socket fd; handshake() takes ownership of the fd for the session lifetime
|
||||
// (shutdown/close handled by caller fd close).
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "clients/transports/tls.h"
|
||||
|
||||
namespace molo::clients::transports {
|
||||
|
||||
// Factory: real wolfSSL session when linked with MOLO_HAS_WOLFSSL, else the
|
||||
// marked stub (loud TlsStubError, never silent downgrade).
|
||||
std::shared_ptr<ITlsSession> MakeTlsSession();
|
||||
|
||||
// True when this build links real wolfSSL.
|
||||
bool TlsNativeAvailable();
|
||||
|
||||
} // namespace molo::clients::transports
|
||||
@@ -344,11 +344,13 @@ TEST(http, connect_refused_is_connect_phase) {
|
||||
}
|
||||
|
||||
TEST(http, https_needs_tls_stub) {
|
||||
// Real wolfSSL is linked: HTTPS to a cleartext loop server fails loud
|
||||
// with TlsError (handshake failure), never silently downgrades.
|
||||
LoopServer server;
|
||||
Client client(std::make_unique<HttpTransport>());
|
||||
ASSERT_THROW(client.get_blocking("https://127.0.0.1:" +
|
||||
std::to_string(server.port()) + "/"),
|
||||
TlsStubError);
|
||||
transports::TlsError);
|
||||
}
|
||||
|
||||
TEST(http, h2c_stub) {
|
||||
|
||||
Reference in New Issue
Block a user