diff --git a/ixwebsocket/IXSocketAppleSSL.cpp b/ixwebsocket/IXSocketAppleSSL.cpp index 68b7261e..70893d1e 100644 --- a/ixwebsocket/IXSocketAppleSSL.cpp +++ b/ixwebsocket/IXSocketAppleSSL.cpp @@ -277,6 +277,7 @@ namespace ix return -1; } } + Socket::setErrno(EIO); return -1; } @@ -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; @@ -307,6 +312,7 @@ namespace ix return -1; } } + Socket::setErrno(EIO); return -1; } diff --git a/ixwebsocket/IXSocketAppleSSL.h b/ixwebsocket/IXSocketAppleSSL.h index 39ead7b2..91c5da14 100644 --- a/ixwebsocket/IXSocketAppleSSL.h +++ b/ixwebsocket/IXSocketAppleSSL.h @@ -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); diff --git a/ixwebsocket/IXSocketMbedTLS.cpp b/ixwebsocket/IXSocketMbedTLS.cpp index e95f216f..8893babf 100644 --- a/ixwebsocket/IXSocketMbedTLS.cpp +++ b/ixwebsocket/IXSocketMbedTLS.cpp @@ -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; } } diff --git a/ixwebsocket/IXSocketOpenSSL.cpp b/ixwebsocket/IXSocketOpenSSL.cpp index 19a1c357..d1307abe 100644 --- a/ixwebsocket/IXSocketOpenSSL.cpp +++ b/ixwebsocket/IXSocketOpenSSL.cpp @@ -873,31 +873,26 @@ namespace ix std::ptrdiff_t SocketOpenSSL::recv(void* buf, size_t nbyte) { - while (true) + std::lock_guard lock(_mutex); + if (_ssl_connection == nullptr || _ssl_context == nullptr) { - std::lock_guard 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(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 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 653afdd4..96f9427c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 diff --git a/test/IXSocketAppleSSLReadTest.cpp b/test/IXSocketAppleSSLReadTest.cpp new file mode 100644 index 00000000..b8829886 --- /dev/null +++ b/test/IXSocketAppleSSLReadTest.cpp @@ -0,0 +1,42 @@ +/* + * IXSocketAppleSSLReadTest.cpp + * Copyright (c) 2026 Machine Zone, Inc. All rights reserved. + */ +#include +#include + +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); +} diff --git a/test/IXSocketMbedTLSWriteTest.cpp b/test/IXSocketMbedTLSWriteTest.cpp index 325e5c93..22bb5479 100644 --- a/test/IXSocketMbedTLSWriteTest.cpp +++ b/test/IXSocketMbedTLSWriteTest.cpp @@ -58,6 +58,7 @@ namespace int writesBeforeBlocking = -1; int blockedResult = MBEDTLS_ERR_SSL_WANT_WRITE; bool failWrites = false; + bool failReads = false; MemoryPeer() { @@ -92,6 +93,7 @@ namespace static int receive(void* context, unsigned char* data, size_t size) { auto& peer = *static_cast(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) @@ -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("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]") { diff --git a/test/IXSocketOpenSSLWriteTest.cpp b/test/IXSocketOpenSSLWriteTest.cpp index f409da40..3e453b82 100644 --- a/test/IXSocketOpenSSLWriteTest.cpp +++ b/test/IXSocketOpenSSLWriteTest.cpp @@ -32,6 +32,8 @@ namespace int writesBeforeBlocking = -1; bool wantRead = false; bool failWrites = false; + bool failReads = false; + bool eof = false; ~MemoryPeer() { @@ -61,6 +63,8 @@ namespace { auto& peer = *static_cast(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); @@ -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]") {