feat(libSceSsl): apply the verify options, run the verify callback, add ALPN, non-blocking sockets and sceSslClose #7626
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7626"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-18T17:48:40Z.
What
The follow-up oneandonlydean asked for on #7319 and loyalburrito reviewed on loyalburrito/AnyPS5#3, on top of #6828's
Sessiondesign, with that review's four points taken:MBEDTLS_SSL_VERIFY_OPTIONALandsceSslConnectreturns the first failure the connection's verify options select, with SharpProspero'sSceSslVerifyOptionbits and the codes KytyPS5's errno list gives:SSL_ERROR_UNKNOWN_CA0x8095F00C,CN_CHECK0x8095F00B,NOT_AFTER_CHECK0x8095F00D,NOT_BEFORE_CHECK0x8095F00E,INVALID_CERT0x8095F00A for any other X.509 fault. The initial options are all five checks (0x3D), so a title that never callssceSslEnableVerifyOptionfails closed: with no CA loaded a default connection returnsSSL_ERROR_UNKNOWN_CAinstead of throwing. The result is final for the connection: a failure sends close_notify, a repeatedsceSslConnectreturns the same code,sceSslRead/sceSslWritethrow; a repeatedsceSslConnectafter success returns 0.sceSslGetSubjectNameand the other readers on the connection's context accept, the chain length and the user argument. A negative return fails the connect withSSL_ERROR_APPLICATION_VERIFICATION_CALLBACK_CHECK(0x8095F0FD); any other return accepts the connection. The callback can use the connection (sceSslGetAlpnSelected,sceSslClose,sceSslDeleteConnection); asceSslConnectfrom inside it, or from another thread while it runs, throws.sceSslSetAlpn's string is split on commas and offered;sceSslGetAlpnSelectedreturns the server's choice, null before the handshake or when nothing was selected. Every acceptedSceSslVersionfloors the handshake at TLS 1.2, all this build speaks.MBEDTLS_ERR_SSL_WANT_READ/WANT_WRITEreturnSSL_ERROR_WANT_POLLIN/WANT_POLLOUT(0x8095F013/14) from connect, read and write; the nextsceSslConnectresumes the handshake; a write that blocks after sending part of the buffer returns the part sent.Send/ReceivemapECONNRESET/EPIPEandWSAECONNRESET/WSAECONNABORTED/WSAENETRESETtoMBEDTLS_ERR_NET_CONN_RESET, which with a stream that ends without close_notify returnsSSL_ERROR_SOCKET_CLOSED(0x8095170D). A blocking socket's expired receive timeout (EAGAINon a socket withoutO_NONBLOCK,WSAETIMEDOUTon Windows) is told apart from a non-blocking would-block and returnsSSL_ERROR_READ_TIMEOUT(0x80951710) from connect and read; the call can be repeated. An expired send timeout throws, as no write timeout code is known. A fatal alert returnsSSL_ERROR_FATAL_ALERT(0x8095F010). When one of these ends the handshake the session is dropped and the nextsceSslConnectstarts over.sceSslClose. Sends close_notify once if the handshake completed, marks the connection closed (later connect, read and write throw) and leaves the socket to the title; returns 0 on a connection that never connected, already failed verification or was already closed.ssl_flags0xA7); the situation-to-code choices are listed as inferred from KytyPS5's names:WANT_POLLIN/WANT_POLLOUTrather thanSSL_ERROR_EAGAIN,SOCKET_CLOSEDrather thanSSL_ERROR_EOFfor a stream cut short,READ_TIMEOUT,FATAL_ALERT,INVALID_CERTas the catch-all, a repeatedsceSslConnectreturning the first result; and the callback's unknowns (which id, code or failed bits, element type, when it runs, whether a non-negative return overrides). The two title IDs the verify option entry named are removed; the entry text is otherwise rewritten around what the code now does.Unchanged from #6828: the
Sessionstruct and its locks, the pool allocator,KernelLookupSocketNative_nid_no_patch,sceSslDeleteConnection(no close_notify, socket stays open), the throws for unknown flags, null buffers, descriptors that are not sockets and other handshake failures.Tested
Windows 11 26200, WinLibs MinGW-w64 GCC 15.2.0 posix-seh ucrt r7, Release,
BUILD_TESTING=ON, onf03a2da5e+ this commit:guest_ssl_transport_kernelandguest_ssl_transport_netrewritten around the new behaviour, 5 runs each, all passing: a connection on a context with no CA and untouched options returnsSSL_ERROR_UNKNOWN_CAtwice, read throws,sceSslClosereturns 0; withSERVER_VERIFYdisabled the same connection returns 0 and reads to the server's close_notify; a verify callback on that context receives the connection id,SSL_ERROR_UNKNOWN_CA, one peer handle whose subject readsCN=localhostthroughsceSslGetSubjectName/sceSslGetNameEntryInfo, returns 0 and the connection becomes readable, and the peer handle throws once the connection is deleted; a certificate for another host returnsSSL_ERROR_CN_CHECKtwice, 0 withCN_CHECKdisabled; a callback returning -1 on a passing chain gets code 0 and the connect returnsSSL_ERROR_APPLICATION_VERIFICATION_CALLBACK_CHECKtwice;SetMinSslVersion(TLS 1.2)+SetAlpn("h2,http/1.1")handshakes,GetAlpnSelectedishttp/1.1after and null before, a second connect returns 0,sceSslClosereturns 0 after the exchange, read and connect throw after it, a second close returns 0, pool memory returns and the socket stays open; a guest socket made non-blocking (SO_NBIOon libSceNet, the libkernel equivalent) handshakes throughWANT_POLLIN/WANT_POLLOUTrounds, read with nothing pending returnsWANT_POLLIN, then ping/pong and the close_notify arrive; a server that closes without close_notify ends the read with 0; a server that resets (linger 0) returnsSSL_ERROR_SOCKET_CLOSED; on the libSceNet table a 200 msSO_RCVTIMEOreturnsSSL_ERROR_READ_TIMEOUTfrom a read with nothing pending and the connection then exchanges ping/pong normally;sceSslCloseon a connection that never connected returns 0; four parallel connections unchanged.guest_ssl,guest_ssl_load,guest_memory_pool_statspass.python3 tools/check_conventions.py --base mainpasses.HostReset/HostTimedOutand the test'sclose/fcntlpaths are compiled only there), a send timeout, and a title.Checklist
main; no other open PR implements the same functions (#6511 addssceSslGetPemin the same file and does not touch these calls)NotImplemented_nid_no_patch); silent stubs are listed in the technical debt file.mdfiles or images added to the repositorysceSslConnect)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T17:49:24Z.
Progress report (
376c040→51d0cb9)📈 System libraries: 90.28% (+0.03%, +1 implemented functions)
✅ 1 implemented
sceSslCloseImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T18:01:20Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
docs/dev/debt/libSceSsl.mdImported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T18:23:16Z.
Linux CI (run 39151593422) fails
guest_ssl_transport_kernelandguest_ssl_transport_netat "handshake before the server resets the connection". Windows passes both. It looks like a race in the newServe::Resetcase rather than a problem inExport.cpp.The server resets the socket (
SO_LINGER {1, 0}thenclose) as soon as its ownmbedtls_ssl_handshakereturns. At that moment the client can still be finishing its side of the handshake, reading the server's last flight. On Linux, an incoming RST discards data the client hasn't read yet, so the client's nextrecvreturnsECONNRESET. Your new mapping then correctly turns that into SOCKET_CLOSED, sosceSslConnect(reset)doesn't return 0. On Windows the timing usually lets the client finish first.Could the server wait until the client has finished its handshake before resetting? For example: after
sceSslConnectsucceeds, the client writes one byte; the server reads it and only then callsAbort. ThesceSslRead(...) == ErrorSocketClosedcheck after that is still the behaviour you want to pin. If you'd like the handshake-time reset covered too, a separate case could expectsceSslConnectto return SOCKET_CLOSED there.Imported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-18T18:38:36Z.
@oneandonlydean that reading is right, thanks. The server reset as soon as its own handshake returned, while the client could still be reading the last flight, and the RST discarding that unread data is a Linux behaviour the Windows run did not show.
51d0cb962does what you describe: aftersceSslConnectreturns 0 the client writes one byte, the server reads it and only then closes with linger 0, and thesceSslRead(...) == SSL_ERROR_SOCKET_CLOSEDcheck follows. I left the handshake-time reset out: with the server resetting at its own pace there is no point in the client's handshake it can reliably hit, so a case for it would be as timing-dependent as the one it replaces; the mapping it would pin (a reset duringsceSslConnectreturns SOCKET_CLOSED and drops the session) is the sameSession::Codepath the read case exercises.On the 4090 box (Windows 11, WinLibs GCC 15.2.0, Release):
guest_ssl_transport_kernelandguest_ssl_transport_netpass 5 runs each with the change,guest_ssl,guest_ssl_loadandguest_memory_pool_statspass. Linux still untested here, so the manual run is the check that counts.AI-assisted: yes (Claude Code).