Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ixwebsocket/IXSocketAppleSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ namespace ix
return -1;
}
}
Socket::setErrno(EIO);
return -1;
}

Expand All @@ -294,8 +295,12 @@ namespace ix

// The connection was reset, inform the caller that this
// Socket should close
if (status == errSSLClosedGraceful || status == errSSLClosedNoNotify ||
status == errSSLClosedAbort)
if (status == errSSLClosedGraceful)
{
Socket::setErrno(0);
return 0;
}
if (status == errSSLClosedNoNotify || status == errSSLClosedAbort)
{
Socket::setErrno(ECONNRESET);
return -1;
Expand All @@ -307,6 +312,7 @@ namespace ix
return -1;
}
}
Socket::setErrno(EIO);
return -1;
}

Expand Down
1 change: 1 addition & 0 deletions ixwebsocket/IXSocketAppleSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace ix
virtual std::ptrdiff_t recv(void* buffer, size_t length) final;

private:
friend struct SocketAppleSSLReadTestAccess;
static std::string getSSLErrorDescription(OSStatus status);
static OSStatus writeToSocket(SSLConnectionRef connection, const void* data, size_t* len);
static OSStatus readFromSocket(SSLConnectionRef connection, void* data, size_t* len);
Expand Down
11 changes: 10 additions & 1 deletion ixwebsocket/IXSocketMbedTLS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,24 @@ namespace ix
}
#endif

if (res == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
{
Socket::setErrno(0);
return 0;
}
if (res == 0)
{
Socket::setErrno(ECONNRESET);
}

if (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE)
else if (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE)
{
Socket::setErrno(EWOULDBLOCK);
}
else
{
Socket::setErrno(EIO);
}
return -1;
}
}
Expand Down
37 changes: 16 additions & 21 deletions ixwebsocket/IXSocketOpenSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,31 +873,26 @@ namespace ix

std::ptrdiff_t SocketOpenSSL::recv(void* buf, size_t nbyte)
{
while (true)
std::lock_guard<std::mutex> lock(_mutex);
if (_ssl_connection == nullptr || _ssl_context == nullptr)
{
std::lock_guard<std::mutex> lock(_mutex);

if (_ssl_connection == nullptr || _ssl_context == nullptr)
{
return 0;
}

ERR_clear_error();
std::ptrdiff_t read_result = SSL_read(_ssl_connection, buf, (int) nbyte);

if (read_result > 0)
{
return read_result;
}
Socket::setErrno(0);
return 0;
}

int reason = SSL_get_error(_ssl_connection, (int) read_result);
ERR_clear_error();
const int result = SSL_read(_ssl_connection, buf, static_cast<int>(nbyte));
if (result > 0) return result;

if (reason == SSL_ERROR_WANT_READ || reason == SSL_ERROR_WANT_WRITE)
{
Socket::setErrno(EWOULDBLOCK);
}
return -1;
const int reason = SSL_get_error(_ssl_connection, result);
if (reason == SSL_ERROR_ZERO_RETURN)
{
Socket::setErrno(0);
return 0;
}
Socket::setErrno(
reason == SSL_ERROR_WANT_READ || reason == SSL_ERROR_WANT_WRITE ? EWOULDBLOCK : EIO);
return -1;
}

} // namespace ix
Expand Down
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ if (USE_ZLIB)
)
endif()

if (USE_TLS AND USE_SECURE_TRANSPORT)
list(APPEND TEST_TARGET_NAMES IXSocketAppleSSLReadTest)
endif()

if (USE_TLS AND USE_MBED_TLS)
list(APPEND TEST_TARGET_NAMES
IXSocketMbedTLSPSATest
Expand Down
42 changes: 42 additions & 0 deletions test/IXSocketAppleSSLReadTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* IXSocketAppleSSLReadTest.cpp
* Copyright (c) 2026 Machine Zone, Inc. All rights reserved.
*/
#include <catch_amalgamated.hpp>
#include <ixwebsocket/IXSocketAppleSSL.h>

namespace ix
{
struct SocketAppleSSLReadTestAccess
{
static OSStatus read(SSLConnectionRef, void*, size_t* size)
{
*size = 0;
return errSecIO;
}
static OSStatus write(SSLConnectionRef, const void*, size_t* size)
{
*size = 0;
return errSecIO;
}
static void install(SocketAppleSSL& socket)
{
socket._sslContext =
SSLCreateContext(kCFAllocatorDefault, kSSLClientSide, kSSLStreamType);
REQUIRE(socket._sslContext != nullptr);
REQUIRE(SSLSetIOFuncs(socket._sslContext, read, write) == noErr);
}
};
} // namespace ix

TEST_CASE("SecureTransport terminal errors clear a previous wait status", "[socket_apple][read]")
{
const bool reading = GENERATE(false, true);
ix::SocketAppleSSL socket(ix::SocketTLSOptions {});
ix::SocketAppleSSLReadTestAccess::install(socket);
ix::Socket::setErrno(EWOULDBLOCK);
char byte = 0;
CHECK((reading ? socket.recv(&byte, 1) : socket.send(&byte, 1)) == -1);
CHECK_FALSE(ix::Socket::isWaitNeeded());
CHECK(ix::Socket::getErrno() != 0);
}
29 changes: 29 additions & 0 deletions test/IXSocketMbedTLSWriteTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace
int writesBeforeBlocking = -1;
int blockedResult = MBEDTLS_ERR_SSL_WANT_WRITE;
bool failWrites = false;
bool failReads = false;

MemoryPeer()
{
Expand Down Expand Up @@ -92,6 +93,7 @@ namespace
static int receive(void* context, unsigned char* data, size_t size)
{
auto& peer = *static_cast<MemoryPeer*>(context);
if (peer.failReads) return MBEDTLS_ERR_NET_RECV_FAILED;
if (peer.incoming.empty()) return MBEDTLS_ERR_SSL_WANT_READ;
const size_t count = (std::min) (size, peer.incoming.size());
for (size_t i = 0; i < count; ++i)
Expand Down Expand Up @@ -278,6 +280,33 @@ TEST_CASE("MbedTLS fatal send errors do not retain the previous wait status", "[
CHECK_FALSE(ix::Socket::isWaitNeeded());
}

TEST_CASE("MbedTLS terminal reads clear a previous wait status", "[socket_mbedtls][read]")
{
const bool clean = GENERATE(false, true);
ix::SocketMbedTLS socket(ix::SocketTLSOptions {});
MemoryConnection connection(socket);
char byte;
REQUIRE(socket.recv(&byte, 1) == -1);
REQUIRE(ix::Socket::isWaitNeeded());
if (clean)
REQUIRE(mbedtls_ssl_close_notify(&connection.server.ssl) == 0);
else
connection.client.failReads = true;
CHECK(socket.recv(&byte, 1) == (clean ? 0 : -1));
CHECK_FALSE(ix::Socket::isWaitNeeded());
}

TEST_CASE("MbedTLS readBytes completes from TLS buffers without polling", "[socket_mbedtls][read]")
{
ix::SocketMbedTLS socket(ix::SocketTLSOptions {});
MemoryConnection connection(socket);
REQUIRE(mbedtls_ssl_write(
&connection.server.ssl, reinterpret_cast<const unsigned char*>("abc"), 3) == 3);
auto result = socket.readBytes(3, nullptr, nullptr, nullptr);
CHECK(result.first);
CHECK(result.second == "abc");
}

#else
TEST_CASE("MbedTLS memory write tests require TLS 1.2 PSK and PSA crypto", "[socket_mbedtls]")
{
Expand Down
33 changes: 33 additions & 0 deletions test/IXSocketOpenSSLWriteTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace
int writesBeforeBlocking = -1;
bool wantRead = false;
bool failWrites = false;
bool failReads = false;
bool eof = false;

~MemoryPeer()
{
Expand Down Expand Up @@ -61,6 +63,8 @@ namespace
{
auto& peer = *static_cast<MemoryPeer*>(BIO_get_data(bio));
BIO_clear_retry_flags(bio);
if (peer.failReads) return -1;
if (peer.eof && peer.incoming.empty()) return 0;
if (peer.incoming.empty())
{
BIO_set_retry_read(bio);
Expand Down Expand Up @@ -250,6 +254,35 @@ TEST_CASE("OpenSSL fatal send errors do not retain the previous wait status", "[
CHECK_FALSE(ix::Socket::isWaitNeeded());
}

TEST_CASE("OpenSSL terminal reads clear a previous wait status", "[socket_openssl][read]")
{
const int terminal = GENERATE(0, 1, 2);
ix::SocketOpenSSL socket(ix::SocketTLSOptions {});
MemoryConnection connection(socket);
char byte;
REQUIRE(socket.recv(&byte, 1) == -1);
REQUIRE(ix::Socket::isWaitNeeded());
if (terminal == 0)
REQUIRE(SSL_shutdown(connection.server.ssl) == 0);
else if (terminal == 1)
connection.client.failReads = true;
else
connection.client.eof = true;
CHECK(socket.recv(&byte, 1) == (terminal == 0 ? 0 : -1));
if (terminal != 0) CHECK(ix::Socket::getErrno() != 0);
CHECK_FALSE(ix::Socket::isWaitNeeded());
}

TEST_CASE("OpenSSL readBytes completes from TLS buffers without polling", "[socket_openssl][read]")
{
ix::SocketOpenSSL socket(ix::SocketTLSOptions {});
MemoryConnection connection(socket);
REQUIRE(SSL_write(connection.server.ssl, "abc", 3) == 3);
auto result = socket.readBytes(3, nullptr, nullptr, nullptr);
CHECK(result.first);
CHECK(result.second == "abc");
}

#else
TEST_CASE("OpenSSL memory write tests require TLS 1.2 PSK and OpenSSL 1.1 APIs", "[socket_openssl]")
{
Expand Down
Loading