feat(clients): wolfssl TLS transport for the http client
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user