// 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 #include #include #include #include #include #ifdef MOLO_HAS_WOLFSSL #include #include #include #endif namespace molo::clients::transports { #ifdef MOLO_HAS_WOLFSSL namespace { std::once_flag& init_flag() { static std::once_flag f; return f; } // Socket send for wolfSSL: the default callback calls send() without // MSG_NOSIGNAL, so a vanished peer raises SIGPIPE and kills the process. int SendNoSignal(WOLFSSL*, char* buf, int sz, void* ctx) { int fd = *static_cast(ctx); for (;;) { ssize_t n = ::send(fd, buf, (size_t)sz, MSG_NOSIGNAL); if (n >= 0) return (int)n; switch (errno) { case EINTR: continue; case EAGAIN: #if EWOULDBLOCK != EAGAIN case EWOULDBLOCK: #endif return WOLFSSL_CBIO_ERR_WANT_WRITE; case ECONNRESET: return WOLFSSL_CBIO_ERR_CONN_RST; case EPIPE: return WOLFSSL_CBIO_ERR_CONN_CLOSE; default: return WOLFSSL_CBIO_ERR_GENERAL; } } } 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"); } // ALPN: this transport speaks HTTP/1.1 only, so that is all it offers // (h2 is never negotiated even when requested). wolfSSL takes a // comma-separated list; a wire-format list is sent as one garbage // protocol name and servers such as Google answer with a fatal alert. { char protos[] = "http/1.1"; if (wolfSSL_UseALPN(ssl_.get(), protos, (unsigned int)strlen(protos), WOLFSSL_ALPN_FAILED_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"); wolfSSL_SSLSetIOSend(ssl_.get(), SendNoSignal); // keeps the fd context set_fd installed // 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) { // The fd is blocking with SO_SNDTIMEO: WANT_READ / WANT_WRITE here is // the socket timeout expiring. Retrying would spin without a bound. int err = wolfSSL_get_error(ssl_.get(), n); if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) throw TlsTimeoutError("wolfSSL write timed out"); throw TlsError("wolfSSL write failed"); } sent += (std::size_t)n; } } // Socket read timeout: throws TlsTimeoutError. Closed connection (TLS // close_notify, or a TCP drop without one): returns 0 and // last_read_status() is kClosed. Measured against a TLS 1.3 server: a // timeout is n=-1 with WOLFSSL_ERROR_WANT_READ, a drop is n=0 then // WOLFSSL_ERROR_SYSCALL, a close_notify is n=0 then WOLFSSL_ERROR_ZERO_RETURN. 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) { status_ = TlsReadStatus::kData; return (std::size_t)n; } int err = wolfSSL_get_error(ssl_.get(), n); if (err == WOLFSSL_ERROR_WANT_READ || err == WOLFSSL_ERROR_WANT_WRITE) throw TlsTimeoutError("wolfSSL read timed out"); if (n == 0 || err == WOLFSSL_ERROR_ZERO_RETURN || err == WOLFSSL_ERROR_SYSCALL) { status_ = TlsReadStatus::kClosed; return 0; } throw TlsError("wolfSSL read failed"); } TlsReadStatus last_read_status() const override { return status_; } 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 ctx_; std::unique_ptr ssl_; int fd_ = -1; std::string alpn_; TlsReadStatus status_ = TlsReadStatus::kUnknown; }; } // namespace std::shared_ptr MakeTlsSession() { return std::make_shared(); } bool TlsNativeAvailable() { return true; } #else std::shared_ptr MakeTlsSession() { return std::make_shared(); } bool TlsNativeAvailable() { return false; } #endif } // namespace molo::clients::transports