diff --git a/core/libs/prx/libSceSsl/Export.cpp b/core/libs/prx/libSceSsl/Export.cpp index e90ef07c4dfd..53a302b1d117 100644 --- a/core/libs/prx/libSceSsl/Export.cpp +++ b/core/libs/prx/libSceSsl/Export.cpp @@ -39,7 +39,22 @@ namespace { constexpr int ErrorNotFound = static_cast(0x8095f004); constexpr int ErrorInvalidArg = static_cast(0x8095177a); constexpr int ErrorInvalidId = static_cast(0x8095f006); +constexpr int ErrorInvalidCert = static_cast(0x8095f00a); +constexpr int ErrorCnCheck = static_cast(0x8095f00b); +constexpr int ErrorUnknownCa = static_cast(0x8095f00c); +constexpr int ErrorNotAfterCheck = static_cast(0x8095f00d); +constexpr int ErrorNotBeforeCheck = static_cast(0x8095f00e); +constexpr int ErrorFatalAlert = static_cast(0x8095f010); +constexpr int ErrorWantPollIn = static_cast(0x8095f013); +constexpr int ErrorWantPollOut = static_cast(0x8095f014); +constexpr int ErrorSocketClosed = static_cast(0x8095170d); +constexpr int ErrorReadTimeout = static_cast(0x80951710); constexpr int ReadPartial = 0x1; +constexpr std::uint32_t FlagServerVerify = 0x1; +constexpr std::uint32_t FlagCnCheck = 0x4; +constexpr std::uint32_t FlagNotAfterCheck = 0x8; +constexpr std::uint32_t FlagNotBeforeCheck = 0x10; +constexpr std::uint32_t FlagKnownCaCheck = 0x20; struct Pool { std::size_t size, current = 0, peak = 0; @@ -221,6 +236,13 @@ struct Session { std::uintptr_t socket; std::mutex mutex; int hostError = 0; + std::uint32_t verifyOptions = 0; + std::vector alpnStrings; + std::vector alpnList; + std::string alpnSelected; + bool handshaken = false; + bool verified = false; + int verifyResult = 0; Random random; mbedtls_x509_crt authorities; mbedtls_ssl_config config; @@ -240,18 +262,32 @@ struct Session { mbedtls_x509_crt_free(&authorities); } - void Configure(const std::string& host) { + void Configure(const std::string& host, const std::string& alpn, std::uint32_t options) { + verifyOptions = options; random.Seed(); for (const auto& chain : context->authorities) for (const auto* entry = &chain->root; entry; entry = entry->next) CheckBackend(mbedtls_x509_crt_parse_der(&authorities, entry->raw.p, entry->raw.len), "SSL CA certificate copy failed"); CheckBackend(mbedtls_ssl_config_defaults(&config, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT), "SSL client configuration failed"); - mbedtls_ssl_conf_authmode(&config, MBEDTLS_SSL_VERIFY_REQUIRED); + mbedtls_ssl_conf_authmode(&config, MBEDTLS_SSL_VERIFY_OPTIONAL); + mbedtls_ssl_conf_min_tls_version(&config, MBEDTLS_SSL_VERSION_TLS1_2); mbedtls_ssl_conf_ca_chain(&config, &authorities, nullptr); mbedtls_ssl_conf_rng(&config, mbedtls_ctr_drbg_random, &random.drbg); if (identity) CheckBackend(mbedtls_ssl_conf_own_cert(&config, &identity->chain->root, &identity->key.key), "SSL client certificate setup failed"); + for (std::size_t start = 0; start <= alpn.size();) { + const auto end = alpn.find(',', start); + const auto protocol = alpn.substr(start, end == std::string::npos ? std::string::npos : end - start); + if (!protocol.empty()) alpnStrings.push_back(protocol); + if (end == std::string::npos) break; + start = end + 1; + } + if (!alpnStrings.empty()) { + for (const auto& protocol : alpnStrings) alpnList.push_back(protocol.c_str()); + alpnList.push_back(nullptr); + CheckBackend(mbedtls_ssl_conf_alpn_protocols(&config, alpnList.data()), "SSL ALPN list rejected"); + } CheckBackend(mbedtls_ssl_setup(&ssl, &config), "SSL connection setup failed"); CheckBackend(mbedtls_ssl_set_hostname(&ssl, host.c_str()), "SSL host name setup failed"); mbedtls_ssl_set_bio(&ssl, this, Send, Receive, nullptr); @@ -293,10 +329,20 @@ struct Session { } } + int VerifyCode() const { + const auto flags = mbedtls_ssl_get_verify_result(&ssl); + if (!(verifyOptions & FlagServerVerify)) return 0; + if ((verifyOptions & FlagKnownCaCheck) && (flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)) return ErrorUnknownCa; + if ((verifyOptions & FlagCnCheck) && (flags & MBEDTLS_X509_BADCERT_CN_MISMATCH)) return ErrorCnCheck; + if ((verifyOptions & FlagNotAfterCheck) && (flags & MBEDTLS_X509_BADCERT_EXPIRED)) return ErrorNotAfterCheck; + if ((verifyOptions & FlagNotBeforeCheck) && (flags & MBEDTLS_X509_BADCERT_FUTURE)) return ErrorNotBeforeCheck; + constexpr std::uint32_t optional = + MBEDTLS_X509_BADCERT_NOT_TRUSTED | MBEDTLS_X509_BADCERT_CN_MISMATCH | MBEDTLS_X509_BADCERT_EXPIRED | MBEDTLS_X509_BADCERT_FUTURE; + return (flags & ~optional) ? ErrorInvalidCert : 0; + } + [[noreturn]] void Fail(const char* function, int error) const { if (currentPoolExhausted || error == MBEDTLS_ERR_SSL_ALLOC_FAILED) throw std::bad_alloc(); - if (error == MBEDTLS_ERR_SSL_WANT_READ || error == MBEDTLS_ERR_SSL_WANT_WRITE) - throw std::runtime_error(std::string(function) + ": the socket would block or its timeout expired; the console's error code is unknown"); char text[160]; mbedtls_strerror(error, text, sizeof(text)); auto message = std::string(function) + ": " + text; @@ -309,6 +355,18 @@ struct Session { } }; +int SocketCode(int result) { + switch (result) { + case MBEDTLS_ERR_SSL_WANT_READ: return ErrorWantPollIn; + case MBEDTLS_ERR_SSL_WANT_WRITE: return ErrorWantPollOut; + case MBEDTLS_ERR_SSL_TIMEOUT: return ErrorReadTimeout; + case MBEDTLS_ERR_SSL_CONN_EOF: + case MBEDTLS_ERR_NET_CONN_RESET: return ErrorSocketClosed; + case MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE: return ErrorFatalAlert; + default: return 0; + } +} + using SslVerifyCallback = int (APS5_VABI *)(int, unsigned int, void**, int, void*); constexpr int SslVersionNone = 0; @@ -364,6 +422,11 @@ std::shared_ptr RequireSession(const char* function, int sslConnectionI return connection.session; } +void RequireVerified(const char* function, const Session& session) { + if (!session.verified) throw std::logic_error(std::string(function) + ": the connection is not connected"); + if (session.verifyResult != 0) throw std::logic_error(std::string(function) + ": the connection failed verification"); +} + struct Input { Block bytes; std::size_t size; @@ -655,30 +718,45 @@ int APS5_VABI sceSslConnect(int sslConnectionId) { auto& state = State(); std::lock_guard lock(state.mutex); auto& connection = RequireConnection(__func__, sslConnectionId); - if (connection.session) throw std::logic_error("sceSslConnect: the connection is already connected"); if (connection.verifyCallback) throw std::invalid_argument("sceSslConnect: how the console invokes the verify callback is unknown"); - auto context = state.contexts.at(connection.context); - if (context->authorities.empty()) - throw std::runtime_error("sceSslConnect: no CA certificate is loaded, and the console's root certificates are not shipped"); - std::shared_ptr owner; - std::uintptr_t native = 0; - if (!KernelLookupSocketNative_nid_no_patch(connection.socket, &owner, &native)) - throw std::invalid_argument("sceSslConnect: descriptor " + std::to_string(connection.socket) + " is not a socket"); - PoolScope scope(context->pool); - session = std::make_shared(std::move(context), std::move(owner), native); - session->Configure(connection.host); - connection.session = session; + if (!connection.session) { + auto context = state.contexts.at(connection.context); + std::shared_ptr owner; + std::uintptr_t native = 0; + if (!KernelLookupSocketNative_nid_no_patch(connection.socket, &owner, &native)) + throw std::invalid_argument("sceSslConnect: descriptor " + std::to_string(connection.socket) + " is not a socket"); + PoolScope scope(context->pool); + session = std::make_shared(std::move(context), std::move(owner), native); + session->Configure(connection.host, connection.alpn, connection.verifyOptions); + connection.session = session; + } else { + session = connection.session; + } } std::unique_lock sessionLock(session->mutex); + if (session->verified) return session->verifyResult; PoolScope scope(session->context->pool); - const int result = mbedtls_ssl_handshake(&session->ssl); - if (result == 0) return 0; - { - std::lock_guard lock(State().mutex); - const auto found = State().connections.find(sslConnectionId); - if (found != State().connections.end() && found->second.session == session) found->second.session.reset(); + if (!session->handshaken) { + const int result = mbedtls_ssl_handshake(&session->ssl); + if (result == MBEDTLS_ERR_SSL_WANT_READ || result == MBEDTLS_ERR_SSL_WANT_WRITE) return SocketCode(result); + if (result != 0) { + { + std::lock_guard lock(State().mutex); + const auto found = State().connections.find(sslConnectionId); + if (found != State().connections.end() && found->second.session == session) found->second.session.reset(); + } + if (const int code = SocketCode(result)) return code; + session->Fail(__func__, result); + } + session->handshaken = true; + const char* selected = mbedtls_ssl_get_alpn_protocol(&session->ssl); + session->alpnSelected = selected ? selected : ""; } - session->Fail(__func__, result); + const int code = session->VerifyCode(); + session->verified = true; + session->verifyResult = code; + if (code != 0) mbedtls_ssl_close_notify(&session->ssl); + return code; } int APS5_VABI sceSslRead(int sslConnectionId, void* buf, size_t len, int flags) { @@ -686,10 +764,12 @@ int APS5_VABI sceSslRead(int sslConnectionId, void* buf, size_t len, int flags) if (!buf && len) throw std::invalid_argument("sceSslRead: null buffer"); const auto session = RequireSession(__func__, sslConnectionId); std::lock_guard sessionLock(session->mutex); + RequireVerified(__func__, *session); PoolScope scope(session->context->pool); const int result = mbedtls_ssl_read(&session->ssl, static_cast(buf), std::min(len, INT_MAX)); if (result >= 0) return result; if (result == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) return 0; + if (const int code = SocketCode(result)) return code; session->Fail(__func__, result); } @@ -698,13 +778,18 @@ int APS5_VABI sceSslWrite(int sslConnectionId, void* buf, size_t len, int flags) if (!buf && len) throw std::invalid_argument("sceSslWrite: null buffer"); const auto session = RequireSession(__func__, sslConnectionId); std::lock_guard sessionLock(session->mutex); + RequireVerified(__func__, *session); PoolScope scope(session->context->pool); const auto* bytes = static_cast(buf); const auto total = std::min(len, INT_MAX); std::size_t written = 0; while (written < total) { const int result = mbedtls_ssl_write(&session->ssl, bytes + written, total - written); - if (result < 0) session->Fail(__func__, result); + if (result < 0) { + if (written) break; + if (const int code = SocketCode(result)) return code; + session->Fail(__func__, result); + } written += static_cast(result); } return static_cast(written); @@ -757,9 +842,15 @@ int APS5_VABI sceSslDisableVerifyOption(int sslConnectionId, std::uint32_t optio } const char* APS5_VABI sceSslGetAlpnSelected(int sslConnectionId) { - std::lock_guard lock(State().mutex); - RequireConnection(__func__, sslConnectionId); - return nullptr; + std::shared_ptr session; + { + std::lock_guard lock(State().mutex); + session = RequireConnection(__func__, sslConnectionId).session; + } + if (!session) return nullptr; + std::lock_guard sessionLock(session->mutex); + if (!session->handshaken || session->alpnSelected.empty()) return nullptr; + return session->alpnSelected.c_str(); } int APS5_VABI sceSslSetAlpn(int sslConnectionId, const char* proto) { diff --git a/core/libs/tests/GuestSslTransport.cpp b/core/libs/tests/GuestSslTransport.cpp index ddbbd279aa4b..3985cedf73be 100644 --- a/core/libs/tests/GuestSslTransport.cpp +++ b/core/libs/tests/GuestSslTransport.cpp @@ -37,6 +37,11 @@ int APS5_VABI sceSslSetVerifyCallback(int, int (APS5_VABI *)(int, unsigned int, int APS5_VABI sceSslConnect(int); int APS5_VABI sceSslRead(int, void*, std::size_t, int); int APS5_VABI sceSslWrite(int, void*, std::size_t, int); +int APS5_VABI sceSslEnableVerifyOption(int, std::uint32_t); +int APS5_VABI sceSslDisableVerifyOption(int, std::uint32_t); +int APS5_VABI sceSslSetMinSslVersion(int, int); +int APS5_VABI sceSslSetAlpn(int, const char*); +const char* APS5_VABI sceSslGetAlpnSelected(int); int APS5_VABI socket_nid_postfix(int, int, int); int APS5_VABI connect_nid_postfix(int, const void*, std::uint32_t); int APS5_VABI close_nid_postfix(int); @@ -49,6 +54,14 @@ int APS5_VABI sceNetSocketClose(int); namespace { constexpr int ParallelConnections = 4; +constexpr int ErrorCnCheck = static_cast(0x8095f00b); +constexpr int ErrorUnknownCa = static_cast(0x8095f00c); +constexpr std::uint32_t ServerVerify = 0x1, CnCheck = 0x4, NotAfterCheck = 0x8, NotBeforeCheck = 0x10, KnownCaCheck = 0x20; +constexpr std::uint32_t AllChecks = ServerVerify | CnCheck | NotAfterCheck | NotBeforeCheck | KnownCaCheck; +constexpr int VersionTls12 = 5; +const char* ServerProtocols[] = {"http/1.1", nullptr}; + +enum class Serve { Close, Exchange }; void Require(bool condition, const char* message) { if (condition) return; @@ -100,6 +113,7 @@ struct Server { Require(mbedtls_ssl_conf_own_cert(&config, &certificate, &key) == 0, "set server certificate"); mbedtls_ssl_conf_authmode(&config, MBEDTLS_SSL_VERIFY_REQUIRED); mbedtls_ssl_conf_ca_chain(&config, &authority, nullptr); + Require(mbedtls_ssl_conf_alpn_protocols(&config, ServerProtocols) == 0, "set server protocols"); Require(mbedtls_net_bind(&listener, "127.0.0.1", "0", MBEDTLS_NET_PROTO_TCP) == 0, "bind loopback listener"); sockaddr_in address{}; #ifdef _WIN32 @@ -121,7 +135,8 @@ struct Server { mbedtls_net_free(&listener); } - void Serve(bool handshakeSucceeds) { + void Serve(Serve mode, bool requireClientCertificate = true) { + mbedtls_ssl_conf_authmode(&config, requireClientCertificate ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE); mbedtls_net_context client; mbedtls_net_init(&client); mbedtls_ssl_context ssl; @@ -131,8 +146,9 @@ struct Server { } else { mbedtls_ssl_set_bio(&ssl, &client, mbedtls_net_send, mbedtls_net_recv, nullptr); const int handshake = mbedtls_ssl_handshake(&ssl); - if ((handshake == 0) != handshakeSucceeds) failure = "server handshake result differs, error " + std::to_string(handshake); - else if (handshake == 0) Exchange(ssl); + if (handshake != 0) failure = "server handshake failed, error " + std::to_string(handshake); + else if (mode == Serve::Exchange) Exchange(ssl); + else mbedtls_ssl_close_notify(&ssl); } mbedtls_ssl_free(&ssl); mbedtls_net_free(&client); @@ -193,15 +209,28 @@ int main(int argc, char** argv) { if (sockets.net) Require(sceNetInit_nid_postfix() == 0, "initialize libSceNet"); Server server; std::thread serving([&] { - server.Serve(false); - server.Serve(true); - for (int index = 0; index < ParallelConnections; ++index) server.Serve(true); + server.Serve(Serve::Close, false); + server.Serve(Serve::Close, false); + server.Serve(Serve::Close); + server.Serve(Serve::Close); + server.Serve(Serve::Exchange); + for (int index = 0; index < ParallelConnections; ++index) server.Serve(Serve::Exchange); }); const int empty = sceSslInit_nid_postfix(256 * 1024); - const int withoutAuthority = sceSslCreateConnection(empty, -1, "localhost"); - Require(Throws([&] { sceSslConnect(withoutAuthority); }), "a handshake without a loaded CA was attempted"); - Require(sceSslDeleteConnection(withoutAuthority) == 0 && sceSslTerm_nid_postfix(empty) == 0, "release the context without CA"); + const int unverifiedSocket = sockets.Open(server.port); + const int unverified = sceSslCreateConnection(empty, unverifiedSocket, "localhost"); + Require(sceSslConnect(unverified) == 0, "with the initial verify options nothing is checked, so no loaded CA is no failure"); + Require(sceSslDeleteConnection(unverified) == 0 && sockets.Close(unverifiedSocket) == 0, "release the unverified connection"); + const int untrustedSocket = sockets.Open(server.port); + const int untrusted = sceSslCreateConnection(empty, untrustedSocket, "localhost"); + Require(sceSslEnableVerifyOption(untrusted, ServerVerify | KnownCaCheck) == 0, "enable the CA check"); + Require(sceSslConnect(untrusted) == ErrorUnknownCa, "a server certificate without a loaded CA passed the CA check"); + Require(sceSslConnect(untrusted) == ErrorUnknownCa, "a second connect did not repeat the verification result"); + std::array scratch{}; + Require(Throws([&] { sceSslRead(untrusted, scratch.data(), 1, 0); }), "a connection that failed verification was readable"); + Require(sceSslDeleteConnection(untrusted) == 0 && sockets.Close(untrustedSocket) == 0, "release the untrusted connection"); + Require(sceSslTerm_nid_postfix(empty) == 0, "release the context without CA"); const int context = sceSslInit_nid_postfix(1024 * 1024); std::string root = RootPem; @@ -224,18 +253,32 @@ int main(int argc, char** argv) { const int mismatchSocket = sockets.Open(server.port); const int mismatch = sceSslCreateConnection(context, mismatchSocket, "example.com"); - Require(Throws([&] { sceSslConnect(mismatch); }), "a certificate for another host was accepted"); - Require(Throws([&] { sceSslRead(mismatch, root.data(), 1, 0); }), "a failed handshake left the connection usable"); + Require(sceSslEnableVerifyOption(mismatch, AllChecks) == 0, "enable every check"); + Require(sceSslConnect(mismatch) == ErrorCnCheck, "a certificate for another host passed the host name check"); + Require(sceSslConnect(mismatch) == ErrorCnCheck, "a second connect did not repeat the host name result"); + Require(Throws([&] { sceSslRead(mismatch, root.data(), 1, 0); }), "a failed verification left the connection usable"); Require(sceSslDeleteConnection(mismatch) == 0 && sockets.Close(mismatchSocket) == 0, "release the mismatched connection"); + const int lenientSocket = sockets.Open(server.port); + const int lenient = sceSslCreateConnection(context, lenientSocket, "example.com"); + Require(sceSslEnableVerifyOption(lenient, AllChecks) == 0 && sceSslDisableVerifyOption(lenient, CnCheck) == 0, "disable the host name check"); + Require(sceSslConnect(lenient) == 0, "a disabled host name check still failed the handshake"); + Require(sceSslGetAlpnSelected(lenient) == nullptr, "a protocol was selected without an ALPN list"); + Require(sceSslDeleteConnection(lenient) == 0 && sockets.Close(lenientSocket) == 0, "release the lenient connection"); + const int descriptor = sockets.Open(server.port); const int connection = sceSslCreateConnection(context, descriptor, "localhost"); std::array buffer{}; + Require(sceSslEnableVerifyOption(connection, AllChecks) == 0, "enable every check on the main connection"); + Require(sceSslSetMinSslVersion(connection, VersionTls12) == 0 && sceSslSetAlpn(connection, "h2,http/1.1") == 0, "set version and ALPN"); + Require(sceSslGetAlpnSelected(connection) == nullptr, "a protocol was selected before the handshake"); Require(Throws([&] { sceSslRead(connection, buffer.data(), buffer.size(), 0); }), "read before the handshake"); Require(Throws([&] { sceSslWrite(connection, buffer.data(), buffer.size(), 0); }), "write before the handshake"); Require(sceSslConnect(connection) == 0, "handshake with the loopback server"); Require(InUse(context) > loaded, "the connection is not charged to the context pool"); - Require(Throws([&] { sceSslConnect(connection); }), "a second handshake was accepted"); + Require(sceSslConnect(connection) == 0, "a second connect after success did not return the same result"); + const char* selected = sceSslGetAlpnSelected(connection); + Require(selected && std::strcmp(selected, "http/1.1") == 0, "the negotiated ALPN protocol is not reported"); Require(Throws([&] { sceSslWrite(connection, buffer.data(), 1, 1); }), "unknown write flags were accepted"); Require(Throws([&] { sceSslRead(connection, buffer.data(), 1, 3); }), "peek was accepted"); char request[] = "ping"; @@ -253,6 +296,7 @@ int main(int argc, char** argv) { const int parallel = sceSslCreateConnection(context, parallelSocket, "localhost"); std::array reply{}; char ping[] = "ping"; + Require(sceSslEnableVerifyOption(parallel, AllChecks) == 0, "enable every check on a parallel connection"); Require(sceSslConnect(parallel) == 0 && sceSslWrite(parallel, ping, 4, 0) == 4, "parallel handshake or write"); Require(sceSslRead(parallel, reply.data(), reply.size(), 0) == 4 && std::memcmp(reply.data(), "pong", 4) == 0, "parallel reply"); Require(sceSslRead(parallel, reply.data(), reply.size(), 0) == 0, "parallel end of stream"); diff --git a/docs/dev/debt/libSceSsl.md b/docs/dev/debt/libSceSsl.md index 4e2cd2fe780f..6a4f47639801 100644 --- a/docs/dev/debt/libSceSsl.md +++ b/docs/dev/debt/libSceSsl.md @@ -2,8 +2,8 @@ ### Unknown function info -- [sceSslCreateConnection, sceSslDeleteConnection, sceSslSetVerifyCallback, sceSslConnect, sceSslRead, sceSslWrite](../../../core/libs/prx/libSceSsl/Export.cpp) (libSceSsl) - imported by `ffmpeg.prx`. Signatures follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/60977b29103452b9681ae10c7a9011d44965bf16/src/SharpProspero/Interop/Net/Http.cs#L195-L252), which also says the socket stays open after `sceSslDeleteConnection` and that `sceSslRead` returns 0 at the end of the stream. An unknown context returns `SSL_ERROR_INVALID_ID` (0x8095F006), as `sceSslGetMemoryPoolStats` does on the console and as [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/4865dd92ca6b6bb907748f125883093af038f53b/src/libs/network.cpp#L2688-L2720) and [sharpemu](https://github.com/sharpemu/sharpemu/blob/29021b5a71bbc80f262d887e6ba18b3a72963f49/src/SharpEmu.Libs/Network/SslExports.cs#L41-L54) return for other context calls; for an unknown connection, a connection whose context was terminated and a null host the console's codes are unknown, so these throw. `sceSslConnect` runs a TLS 1.2 client handshake with Mbed TLS 3.6.7 over the host socket that the guest number maps to in the libkernel or libSceNet socket table, and `sceSslRead` and `sceSslWrite` carry application data over it. TLS 1.3 is compiled out: SharpProspero's `SceSslVersion` ends at TLS 1.2, and Mbed TLS 1.3 needs PSA crypto state that is process-wide rather than allocated from a context's pool. Connection state is allocated from the context's pool and returned to it by `sceSslDeleteConnection`; a connection keeps the pool alive after `sceSslTerm`. The server is verified against the CAs loaded with `sceSslLoadCert`: chain, validity dates and the host name the connection was created with, which is also sent as SNI. A client certificate loaded with `sceSslLoadCert` is sent when the server asks for one. With no CA loaded `sceSslConnect` throws, because no root bundle is shipped and the console's built-in roots are unknown. With a verify callback set it throws: SharpProspero describes the arguments as an id, bits saying what failed, the chain, its length and the user argument, with a negative return failing the handshake with that value, but whether the id is the context or the connection, the bit values, the chain element type and when the callback runs are unknown. Handshake, verification and socket failures, a second `sceSslConnect`, a read or write before the handshake, a descriptor that is not a socket, a null buffer, write flags other than 0 and read flags other than 0 and `MsgFlagPartialRead` (1) throw, as their console codes are unknown; `MsgFlagPeek` (3) is not implemented. A non-blocking socket that would block throws, and so does a libSceNet socket whose `SO_RCVTIMEO` or `SO_SNDTIMEO` expires, as libSceNet sets them on the host socket; libkernel keeps them above the host socket, so they do not bound SSL reads and writes on a libkernel socket. `sceSslRead` returns what one Mbed TLS read delivers, up to `len`, with flags 0 and with `MsgFlagPartialRead` alike; SharpProspero describes `sceSslRead` as reading up to `len` but `MsgFlagPartialRead` as returning before the buffer is full, so whether flags 0 waits to fill the buffer is unknown. It returns 0 after the server's close_notify and also when the transport ends without one. `sceSslWrite` sends all of `len`, capped at `INT_MAX`, and returns the count. A connection serializes its reads and writes, so a read blocked in one thread holds back a write on the same connection from another. `sceSslDeleteConnection` sends no close_notify. The socket is not checked when the connection is created, `sceSslTerm` leaves the context's connections to `sceSslDeleteConnection`, and `sceSslSetVerifyCallback` accepts any callback, null included, and returns 0. None of this is verified on the console. `SSL_ERROR_BEFORE_INIT` (0x8095F001) is never returned, as the library keeps no state apart from its contexts +- [sceSslCreateConnection, sceSslDeleteConnection, sceSslSetVerifyCallback, sceSslConnect, sceSslRead, sceSslWrite](../../../core/libs/prx/libSceSsl/Export.cpp) (libSceSsl) - imported by `ffmpeg.prx`. Signatures follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/60977b29103452b9681ae10c7a9011d44965bf16/src/SharpProspero/Interop/Net/Http.cs#L195-L252), which also says the socket stays open after `sceSslDeleteConnection` and that `sceSslRead` returns 0 at the end of the stream. An unknown context returns `SSL_ERROR_INVALID_ID` (0x8095F006), as `sceSslGetMemoryPoolStats` does on the console and as [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/4865dd92ca6b6bb907748f125883093af038f53b/src/libs/network.cpp#L2688-L2720) and [sharpemu](https://github.com/sharpemu/sharpemu/blob/29021b5a71bbc80f262d887e6ba18b3a72963f49/src/SharpEmu.Libs/Network/SslExports.cs#L41-L54) return for other context calls; for an unknown connection, a connection whose context was terminated and a null host the console's codes are unknown, so these throw. `sceSslConnect` runs a TLS 1.2 client handshake with Mbed TLS 3.6.7 over the host socket that the guest number maps to in the libkernel or libSceNet socket table, and `sceSslRead` and `sceSslWrite` carry application data over it. TLS 1.3 is compiled out: SharpProspero's `SceSslVersion` ends at TLS 1.2, and Mbed TLS 1.3 needs PSA crypto state that is process-wide rather than allocated from a context's pool. Connection state is allocated from the context's pool and returned to it by `sceSslDeleteConnection`; a connection keeps the pool alive after `sceSslTerm`. The server's chain is checked against the CAs loaded with `sceSslLoadCert`, its validity dates and the host name the connection was created with, which is also sent as SNI, and `sceSslConnect` returns the first failure the connection's verify options ask for: `SSL_ERROR_UNKNOWN_CA` (0x8095F00C), `SSL_ERROR_CN_CHECK` (0x8095F00B), `SSL_ERROR_NOT_AFTER_CHECK` (0x8095F00D), `SSL_ERROR_NOT_BEFORE_CHECK` (0x8095F00E), or `SSL_ERROR_INVALID_CERT` (0x8095F00A) for any other X.509 fault, the codes [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/4865dd92ca6b6bb907748f125883093af038f53b/src/libs/network.cpp) lists; with `SERVER_VERIFY` off nothing is checked. A failed verification is final: the session sends close_notify, later `sceSslConnect` calls return the same code and `sceSslRead` and `sceSslWrite` throw; a repeated `sceSslConnect` after success returns 0 again. No root bundle is shipped, so with the CA check on only a CA the title loaded passes, and whether the console consults a built-in store is unknown. A client certificate loaded with `sceSslLoadCert` is sent when the server asks for one. With a verify callback set it throws: SharpProspero describes the arguments as an id, bits saying what failed, the chain, its length and the user argument, with a negative return failing the handshake with that value, but whether the id is the context or the connection, the bit values, the chain element type and when the callback runs are unknown. A non-blocking socket that would block returns `SSL_ERROR_WANT_POLLIN` (0x8095F013) or `SSL_ERROR_WANT_POLLOUT` (0x8095F014) from `sceSslConnect`, `sceSslRead` and `sceSslWrite`, and the next `sceSslConnect` resumes the handshake; a libSceNet socket whose `SO_RCVTIMEO` or `SO_SNDTIMEO` expires returns `SSL_ERROR_READ_TIMEOUT` (0x80951710), as libSceNet sets them on the host socket; libkernel keeps them above the host socket, so they do not bound SSL reads and writes on a libkernel socket. A peer that resets or ends the connection without close_notify returns `SSL_ERROR_SOCKET_CLOSED` (0x8095170D), a fatal alert `SSL_ERROR_FATAL_ALERT` (0x8095F010); whether the console returns these for a failed handshake as well as a failed read is unknown. Other handshake and socket failures, a read or write before the handshake, a descriptor that is not a socket, a null buffer, write flags other than 0 and read flags other than 0 and `MsgFlagPartialRead` (1) throw, as their console codes are unknown; `MsgFlagPeek` (3) is not implemented. `sceSslRead` returns what one Mbed TLS read delivers, up to `len`, with flags 0 and with `MsgFlagPartialRead` alike; SharpProspero describes `sceSslRead` as reading up to `len` but `MsgFlagPartialRead` as returning before the buffer is full, so whether flags 0 waits to fill the buffer is unknown. It returns 0 after the server's close_notify and also when the transport ends without one. `sceSslWrite` sends all of `len`, capped at `INT_MAX`, and returns the count, or the part sent before the socket would block. A connection serializes its reads and writes, so a read blocked in one thread holds back a write on the same connection from another. `sceSslDeleteConnection` sends no close_notify. The socket is not checked when the connection is created, `sceSslTerm` leaves the context's connections to `sceSslDeleteConnection`, and `sceSslSetVerifyCallback` accepts any callback, null included, and returns 0. None of this is verified on the console. `SSL_ERROR_BEFORE_INIT` (0x8095F001) is never returned, as the library keeps no state apart from its contexts - [sceSslClose](../../../core/libs/prx/libSceSsl/Export.cpp) (libSceSsl) - unknown signature - [sceSslGetSerialNumber, sceSslGetSubjectName, sceSslGetIssuerName, sceSslGetNameEntryCount, sceSslGetNameEntryInfo, sceSslFreeSslCertName](../../../core/libs/prx/libSceSsl/Export.cpp) (libSceSsl) - signatures and context ownership follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/60977b29103452b9681ae10c7a9011d44965bf16/src/SharpProspero/Interop/Net/Http.cs#L293-L350). Operations take a context handle as their first argument and operate on certificate and name handles scoped to that context. `sceSslGetSerialNumber` copies into a caller-allocated output buffer or queries the required length when the buffer is null or capacity is zero; a buffer smaller than the serial number returns `SSL_ERROR_INVALID_ARG` (0x8095177a). Mbed TLS parses the certificate's serial number and X.509 subject and issuer distinguished names, extracting OID attribute short names (or numeric strings) and value strings. An unknown context, unknown or expired certificate handle or invalid certificate name handle throws; null output pointers or out-of-range entry indices return `SSL_ERROR_INVALID_ARG` (0x8095177a). The error codes, the zero-capacity length query, the NUL-terminated truncation of name entry strings with the full value length in `valueLen`, and the lifetime of name handles (released only by `sceSslFreeSslCertName` or when the context is terminated) have not been measured on a PS5 - [sceSslLoadCert, sceSslGetCaList, sceSslFreeCaList, sceSslGetCaCerts, sceSslFreeCaCerts, sceSslUnloadCert](../../../core/libs/prx/libSceSsl/Export.cpp) (libSceSsl) - signatures and layouts follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/5dfdb4b0b166e83dc9ae275001afb695f12c7174/src/SharpProspero/Interop/Net/Http.cs). PPSA15508/01.002.000 calls `sceSslLoadCert` with one 1,408-byte PEM CA and null client certificate/key arguments; this confirms that call shape, not console error behavior. Mbed TLS 3.6.7 parses DER/PEM chains and checks an optional client certificate/key pair. Imports are retained per context and committed together; a failed import preserves existing credentials. CA queries return independently owned snapshots; unload clears active credentials while outstanding snapshots remain valid. Appending repeated CA imports, snapshot lifetime across unload, memory overhead and error precedence have not been measured on a PS5. Invalid loads, malformed material, exhausted pools and unsupported keys throw rather than inventing console error codes. No root bundle is shipped; an empty context still returns `NOT_FOUND`. The existing null-list error, unknown-context behavior and legacy clearing of foreign raw CA lists remain unchanged. Certificate reader queries reflect active certificates or outstanding snapshots. Loading credentials does not start a TLS connection; `sceSslConnect` verifies the server against the loaded CAs and offers the loaded client certificate -- [sceSslEnableVerifyOption, sceSslDisableVerifyOption, sceSslSetMinSslVersion, sceSslSetAlpn, sceSslGetAlpnSelected](../../../core/libs/prx/libSceSsl/Export.cpp) (libSceSsl) - imported by PPSA01317 and PPSA07411. Signatures follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/60977b29103452b9681ae10c7a9011d44965bf16/src/SharpProspero/Interop/Net/Http.cs), the source of the other connection signatures. The verify options, the minimum version and the ALPN protocol list are recorded on the connection; nothing reads them, and how the console applies them is unknown. The initial verify options are unknown and taken as 0. `sceSslSetMinSslVersion` accepts the `SceSslVersion` values SharpProspero lists, 0 (none) to 5 (TLS 1.2), and throws for others; whether the console accepts a TLS 1.3 value is unknown. `sceSslSetAlpn` stores the string up to its terminator and throws for a null list; whether the console expects a comma-separated list or the TLS wire format is unknown. `sceSslGetAlpnSelected` returns null, as the protocol negotiated by the handshake is not read. An unknown connection and a connection whose context was terminated throw, as for `sceSslSetVerifyCallback` +- [sceSslEnableVerifyOption, sceSslDisableVerifyOption, sceSslSetMinSslVersion, sceSslSetAlpn, sceSslGetAlpnSelected](../../../core/libs/prx/libSceSsl/Export.cpp) (libSceSsl) - imported by PPSA01317 and PPSA07411. Signatures follow [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/60977b29103452b9681ae10c7a9011d44965bf16/src/SharpProspero/Interop/Net/Http.cs), the source of the other connection signatures. The verify options select which verification failures `sceSslConnect` reports (see the connection entry above), with the bit values SharpProspero's `SceSslVerifyOption` gives: `SERVER_VERIFY` 0x1, `CN_CHECK` 0x4, `NOT_AFTER_CHECK` 0x8, `NOT_BEFORE_CHECK` 0x10, `KNOWN_CA_CHECK` 0x20. The initial verify options are unknown and taken as 0, so a title that never enables `SERVER_VERIFY` gets an unverified session; whether the console verifies by default is unknown, and a fail-closed default waits on a root bundle. `sceSslSetMinSslVersion` accepts the `SceSslVersion` values SharpProspero lists, 0 (none) to 5 (TLS 1.2), and throws for others; whether the console accepts a TLS 1.3 value is unknown. Every accepted value gives the same handshake, as Mbed TLS 3 speaks TLS 1.2 and nothing older and TLS 1.3 is compiled out. `sceSslSetAlpn` stores the string up to its terminator and throws for a null list; it is split on commas into the protocols offered in the handshake, as HTTP libraries write ALPN lists, and whether the console expects that or the TLS wire format is unknown. `sceSslGetAlpnSelected` returns the protocol the server selected, or null before the handshake and when none was selected; the pointer is owned by the connection. An unknown connection and a connection whose context was terminated throw, as for `sceSslSetVerifyCallback`