diff --git a/core/libs/prx/libSceLibreSsl3/CMakeLists.txt b/core/libs/prx/libSceLibreSsl3/CMakeLists.txt index 45cb8215a0f5..70218575276e 100644 --- a/core/libs/prx/libSceLibreSsl3/CMakeLists.txt +++ b/core/libs/prx/libSceLibreSsl3/CMakeLists.txt @@ -2,6 +2,7 @@ add_library(libSceLibreSsl3 SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/ target_include_directories(libSceLibreSsl3 PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(libSceLibreSsl3 PRIVATE libSceLibreSsl libc) configure_windows_unwind(libSceLibreSsl3) +target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/Ssl.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/X509.cpp) target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/AdditionalExports.cpp) target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/Stack.cpp) @@ -140,6 +141,16 @@ if(BUILD_TESTING) configure_windows_unwind(guest_libressl3_key_reference_tests) add_test(NAME guest_libressl3_key_reference COMMAND guest_libressl3_key_reference_tests) set_tests_properties(guest_libressl3_key_reference PROPERTIES TIMEOUT 20) + add_test_executable(guest_libressl3_ssl_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSsl3Ssl.cpp) + target_include_directories(guest_libressl3_ssl_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_libressl3_ssl_tests PRIVATE libSceLibreSsl3 libSceLibreSsl) + configure_windows_unwind(guest_libressl3_ssl_tests) + add_test(NAME guest_libressl3_ssl COMMAND guest_libressl3_ssl_tests) + set_tests_properties(guest_libressl3_ssl PROPERTIES TIMEOUT 20) + if(WIN32) + set_tests_properties(guest_libressl3_ssl PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(guest_libressl3_evp_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSsl3Evp.cpp) target_include_directories(guest_libressl3_evp_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(guest_libressl3_evp_tests PRIVATE libSceLibreSsl3 libc) diff --git a/core/libs/prx/libSceLibreSsl3/Export.cpp b/core/libs/prx/libSceLibreSsl3/Export.cpp index 60dd69efa331..42fe16a1f5e6 100644 --- a/core/libs/prx/libSceLibreSsl3/Export.cpp +++ b/core/libs/prx/libSceLibreSsl3/Export.cpp @@ -206,36 +206,6 @@ int APS5_VABI SSL_CTX_clear_chain_certs_nid_postfix(void) { return 0; } -int APS5_VABI SSL_CTX_set_max_proto_version_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI SSL_CTX_set_min_proto_version_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI SSL_CTX_up_ref_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI SSL_SESSION_up_ref_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI SSL_get_client_random_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI SSL_up_ref_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI X509_STORE_CTX_free_nid_postfix(void) { NotImplemented_nid_no_patch(__func__); return 0; @@ -256,16 +226,6 @@ int APS5_VABI X509_STORE_up_ref_nid_postfix(void) { return 0; } -int APS5_VABI X509_get_version_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - -int APS5_VABI X509_up_ref_nid_postfix(void) { - NotImplemented_nid_no_patch(__func__); - return 0; -} - int APS5_VABI d2i_ECParameters_nid_postfix(void) { NotImplemented_nid_no_patch(__func__); return 0; diff --git a/core/libs/prx/libSceLibreSsl3/include/Api.hpp b/core/libs/prx/libSceLibreSsl3/include/Api.hpp index bcce1ccbf246..148dca1eeab2 100644 --- a/core/libs/prx/libSceLibreSsl3/include/Api.hpp +++ b/core/libs/prx/libSceLibreSsl3/include/Api.hpp @@ -4,6 +4,8 @@ #include "SceTypes.hpp" #include "EvpDigest.hpp" #include "prx/libSceLibreSsl3/include/Key.hpp" +#include "prx/libSceLibreSsl3/include/Ssl.hpp" +#include "prx/libSceLibreSsl3/include/X509.hpp" #include "prx/libSceLibreSsl/include/Api.hpp" #include "prx/libSceLibreSsl3/include/Cipher.hpp" @@ -172,12 +174,12 @@ int APS5_VABI RSA_padding_add_PKCS1_PSS_mgf1_nid_postfix(void); int APS5_VABI RSA_up_ref_nid_postfix(LibreSsl3Rsa* rsa); int APS5_VABI SSL_CTX_add0_chain_cert_nid_postfix(void); int APS5_VABI SSL_CTX_clear_chain_certs_nid_postfix(void); -int APS5_VABI SSL_CTX_set_max_proto_version_nid_postfix(void); -int APS5_VABI SSL_CTX_set_min_proto_version_nid_postfix(void); -int APS5_VABI SSL_CTX_up_ref_nid_postfix(void); -int APS5_VABI SSL_SESSION_up_ref_nid_postfix(void); -int APS5_VABI SSL_get_client_random_nid_postfix(void); -int APS5_VABI SSL_up_ref_nid_postfix(void); +int APS5_VABI SSL_CTX_set_max_proto_version_nid_postfix(LibreSsl3SslCtx* context, std::uint16_t version); +int APS5_VABI SSL_CTX_set_min_proto_version_nid_postfix(LibreSsl3SslCtx* context, std::uint16_t version); +int APS5_VABI SSL_CTX_up_ref_nid_postfix(LibreSsl3SslCtx* ctx); +int APS5_VABI SSL_SESSION_up_ref_nid_postfix(LibreSsl3SslSession* session); +std::size_t APS5_VABI SSL_get_client_random_nid_postfix(const LibreSsl3Ssl* ssl, unsigned char* out, std::size_t maxOut); +int APS5_VABI SSL_up_ref_nid_postfix(LibreSsl3Ssl* ssl); int APS5_VABI X509_PUBKEY_get0_param_nid_postfix(void** publicKeyAlgorithm, const unsigned char** publicKey, int* publicKeyLength, void** algorithm, const void* pub); int APS5_VABI X509_STORE_CTX_free_nid_postfix(void); int APS5_VABI X509_STORE_CTX_init_nid_postfix(void); @@ -189,10 +191,10 @@ const void* APS5_VABI X509_get0_notBefore_nid_postfix(const LibreSsl3ValidityX50 void APS5_VABI X509_get0_signature_nid_postfix(const void** signature, const void** algorithm, const LibreSsl3ValidityX509* certificate); void* APS5_VABI X509_get_ext_nid_postfix(const LibreSsl3ExtensionsX509* certificate, int loc); int APS5_VABI X509_get_ext_count_nid_postfix(const LibreSsl3ExtensionsX509* certificate); -int APS5_VABI X509_get_version_nid_postfix(void); +std::int64_t APS5_VABI X509_get_version_nid_postfix(const LibreSsl3X509* certificate); void* APS5_VABI X509_getm_notAfter_nid_postfix(const LibreSsl3ValidityX509* certificate); void* APS5_VABI X509_getm_notBefore_nid_postfix(const LibreSsl3ValidityX509* certificate); -int APS5_VABI X509_up_ref_nid_postfix(void); +int APS5_VABI X509_up_ref_nid_postfix(LibreSsl3X509* certificate); void* APS5_VABI d2i_ASN1_INTEGER_nid_postfix(void** output, const unsigned char** input, std::int64_t length); int APS5_VABI d2i_ECParameters_nid_postfix(void); int APS5_VABI d2i_PKCS7_nid_postfix(void); diff --git a/core/libs/prx/libSceLibreSsl3/include/Key.hpp b/core/libs/prx/libSceLibreSsl3/include/Key.hpp index 3886d399d2d0..afdc1812a90d 100644 --- a/core/libs/prx/libSceLibreSsl3/include/Key.hpp +++ b/core/libs/prx/libSceLibreSsl3/include/Key.hpp @@ -58,12 +58,38 @@ struct LibreSsl3Rsa { void* mtBlinding; }; +struct LibreSsl3SslCtx { + const void* method; + const void* quicMethod; + void* cipherList; + void* certStore; + std::int64_t sessionTimeout; + std::int32_t references; + void* extraCerts; + std::int32_t verifyMode; + std::uint64_t sidCtxLength; + unsigned char sidCtx[32]; + void* param; + void* defaultPasswdCallback; + void* defaultPasswdCallbackUserdata; + std::uint16_t minTlsVersion; + std::uint16_t maxTlsVersion; + std::uint16_t minProtoVersion; + std::uint16_t maxProtoVersion; +}; + static_assert(sizeof(LibreSsl3EvpPkey) == 32); static_assert(offsetof(LibreSsl3EvpPkey, references) == 4); static_assert(sizeof(LibreSsl3EcKey) == 64); static_assert(offsetof(LibreSsl3EcKey, references) == 48); static_assert(sizeof(LibreSsl3Rsa) == 152); static_assert(offsetof(LibreSsl3Rsa, references) == 104); +static_assert(sizeof(LibreSsl3SslCtx) == 136); +static_assert(offsetof(LibreSsl3SslCtx, references) == 40); +static_assert(offsetof(LibreSsl3SslCtx, minTlsVersion) == 128); +static_assert(offsetof(LibreSsl3SslCtx, maxTlsVersion) == 130); +static_assert(offsetof(LibreSsl3SslCtx, minProtoVersion) == 132); +static_assert(offsetof(LibreSsl3SslCtx, maxProtoVersion) == 134); static_assert(offsetof(LibreSsl3Rsa, p) == 48); static_assert(offsetof(LibreSsl3Rsa, q) == 56); static_assert(offsetof(LibreSsl3Rsa, dmp1) == 64); diff --git a/core/libs/prx/libSceLibreSsl3/include/Reference.hpp b/core/libs/prx/libSceLibreSsl3/include/Reference.hpp new file mode 100644 index 000000000000..e417a654dc77 --- /dev/null +++ b/core/libs/prx/libSceLibreSsl3/include/Reference.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include +#include + +static_assert(std::atomic_ref::required_alignment <= alignof(std::int32_t)); + +inline int Retain_nid_no_patch(std::int32_t& references) { + std::atomic_ref count(references); + auto previous = count.load(); + for (;;) { + if (previous == std::numeric_limits::max()) + throw std::overflow_error("LibreSSL3 reference count overflow"); + if (count.compare_exchange_weak(previous, previous + 1)) + return previous > 0; + } +} diff --git a/core/libs/prx/libSceLibreSsl3/include/Ssl.hpp b/core/libs/prx/libSceLibreSsl3/include/Ssl.hpp new file mode 100644 index 000000000000..5452d211b3b5 --- /dev/null +++ b/core/libs/prx/libSceLibreSsl3/include/Ssl.hpp @@ -0,0 +1,70 @@ +#pragma once + +#include +#include +#include "prx/libSceLibreSsl3/include/Key.hpp" + +struct LibreSsl3SslMethod { + std::int32_t dtls; + std::int32_t server; + std::int32_t version; + std::uint16_t minTlsVersion; + std::uint16_t maxTlsVersion; +}; + +struct LibreSsl3Ssl3State { + std::int64_t flags; + unsigned char serverRandom[32]; + unsigned char clientRandom[32]; +}; + +struct LibreSsl3Ssl { + std::int32_t version; + const LibreSsl3SslMethod* method; + const void* quicMethod; + void* rbio; + void* wbio; + void* bbio; + std::int32_t server; + LibreSsl3Ssl3State* s3; + void* d1; + void* param; + void* cipherList; + void* cert; + std::uint64_t sidCtxLength; + unsigned char sidCtx[32]; + void* session; + std::int32_t verifyMode; + std::int32_t error; + std::int32_t errorCode; + LibreSsl3SslCtx* ctx; + std::int64_t verifyResult; + std::int32_t references; +}; + +struct LibreSsl3SslSession { + std::int32_t sslVersion; + std::uint64_t masterKeyLength; + unsigned char masterKey[48]; + std::uint64_t sessionIdLength; + unsigned char sessionId[32]; + std::uint64_t sidCtxLength; + unsigned char sidCtx[32]; + void* peerCert; + std::int32_t peerCertType; + std::int64_t verifyResult; + std::int64_t timeout; + std::int64_t time; + std::int32_t references; +}; + +static_assert(sizeof(LibreSsl3SslMethod) == 16); +static_assert(offsetof(LibreSsl3SslMethod, minTlsVersion) == 12); +static_assert(offsetof(LibreSsl3SslMethod, maxTlsVersion) == 14); +static_assert(sizeof(LibreSsl3Ssl3State) == 72); +static_assert(offsetof(LibreSsl3Ssl3State, clientRandom) == 40); +static_assert(sizeof(LibreSsl3Ssl) == 184); +static_assert(offsetof(LibreSsl3Ssl, s3) == 56); +static_assert(offsetof(LibreSsl3Ssl, references) == 176); +static_assert(sizeof(LibreSsl3SslSession) == 192); +static_assert(offsetof(LibreSsl3SslSession, references) == 184); diff --git a/core/libs/prx/libSceLibreSsl3/include/X509.hpp b/core/libs/prx/libSceLibreSsl3/include/X509.hpp new file mode 100644 index 000000000000..3e2df2c042d5 --- /dev/null +++ b/core/libs/prx/libSceLibreSsl3/include/X509.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include "prx/libSceLibreSsl/include/Api.hpp" + +struct LibreSsl3X509CertInfo { + Asn1String* version; +}; + +struct LibreSsl3X509 { + LibreSsl3X509CertInfo* certInfo; + void* sigAlg; + void* signature; + std::int32_t valid; + std::int32_t references; +}; + +static_assert(sizeof(LibreSsl3X509CertInfo) == 8); +static_assert(sizeof(LibreSsl3X509) == 32); +static_assert(offsetof(LibreSsl3X509, references) == 28); diff --git a/core/libs/prx/libSceLibreSsl3/src/Key.cpp b/core/libs/prx/libSceLibreSsl3/src/Key.cpp index 6e5877b5dd00..b8ff53165521 100644 --- a/core/libs/prx/libSceLibreSsl3/src/Key.cpp +++ b/core/libs/prx/libSceLibreSsl3/src/Key.cpp @@ -57,28 +57,10 @@ */ #include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp" +#include "prx/libSceLibreSsl3/include/Reference.hpp" -#include -#include #include -namespace { - -static_assert(std::atomic_ref::required_alignment <= alignof(std::int32_t)); - -int Retain_nid_no_patch(std::int32_t& references) { - std::atomic_ref count(references); - auto previous = count.load(); - for (;;) { - if (previous == std::numeric_limits::max()) - throw std::overflow_error("LibreSSL3 key reference count overflow"); - if (count.compare_exchange_weak(previous, previous + 1)) - return previous > 0; - } -} - -} // namespace - extern "C" { int APS5_VABI EVP_PKEY_up_ref_nid_postfix(LibreSsl3EvpPkey* key) { @@ -99,6 +81,12 @@ int APS5_VABI RSA_up_ref_nid_postfix(LibreSsl3Rsa* rsa) { return Retain_nid_no_patch(rsa->references); } +int APS5_VABI SSL_CTX_up_ref_nid_postfix(LibreSsl3SslCtx* ctx) { + if (!ctx) + throw std::invalid_argument("SSL_CTX_up_ref requires a context"); + return Retain_nid_no_patch(ctx->references); +} + void APS5_VABI RSA_get0_factors_nid_postfix(const LibreSsl3Rsa* rsa, const void** p, const void** q) { if (!rsa && (p || q)) throw std::invalid_argument("RSA_get0_factors requires a key when outputs are requested"); diff --git a/core/libs/prx/libSceLibreSsl3/src/Ssl.cpp b/core/libs/prx/libSceLibreSsl3/src/Ssl.cpp new file mode 100644 index 000000000000..6fbf355ad8de --- /dev/null +++ b/core/libs/prx/libSceLibreSsl3/src/Ssl.cpp @@ -0,0 +1,94 @@ +#include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp" +#include "prx/libSceLibreSsl3/include/Reference.hpp" + +#include +#include +#include +#include +#include + +namespace { + +constexpr std::uint16_t Tls11Version = 0x0302; +constexpr std::uint16_t Tls12Version = 0x0303; +constexpr std::uint16_t Dtls1Version = 0xfeff; +constexpr std::uint16_t Dtls12Version = 0xfefd; + +std::uint16_t TlsVersion(std::uint16_t dtlsVersion) { + if (dtlsVersion == Dtls1Version) return Tls11Version; + if (dtlsVersion == Dtls12Version) return Tls12Version; + return 0; +} + +std::uint16_t DtlsVersion(std::uint16_t tlsVersion) { + if (tlsVersion == Tls11Version) return Dtls1Version; + if (tlsVersion == Tls12Version) return Dtls12Version; + return 0; +} + +int SetVersionBound(const LibreSsl3SslMethod& method, std::uint16_t version, bool minimum, std::uint16_t otherBound, + std::uint16_t& tlsVersion, std::uint16_t& protoVersion) { + if (version == 0) { + tlsVersion = minimum ? method.minTlsVersion : method.maxTlsVersion; + protoVersion = 0; + return 1; + } + const bool dtls = method.dtls != 0; + const auto requested = dtls ? TlsVersion(version) : version; + if (requested == 0) return 0; + const auto low = minimum ? requested : otherBound; + const auto high = minimum ? otherBound : requested; + if (method.minTlsVersion > method.maxTlsVersion || low > high) return 0; + if (low > method.maxTlsVersion || high < method.minTlsVersion) return 0; + const auto bound = minimum ? std::max(low, method.minTlsVersion) : std::min(high, method.maxTlsVersion); + const auto proto = dtls ? DtlsVersion(bound) : bound; + if (dtls && proto == 0) return 0; + tlsVersion = bound; + protoVersion = proto; + return 1; +} + +} // namespace + +extern "C" { + +int APS5_VABI SSL_up_ref_nid_postfix(LibreSsl3Ssl* ssl) { + if (!ssl) + throw std::invalid_argument("SSL_up_ref requires an SSL"); + return Retain_nid_no_patch(ssl->references); +} + +int APS5_VABI SSL_SESSION_up_ref_nid_postfix(LibreSsl3SslSession* session) { + if (!session) + throw std::invalid_argument("SSL_SESSION_up_ref requires a session"); + return Retain_nid_no_patch(session->references); +} + +int APS5_VABI SSL_CTX_set_min_proto_version_nid_postfix(LibreSsl3SslCtx* context, std::uint16_t version) { + if (!context || !context->method) + throw std::invalid_argument("SSL_CTX_set_min_proto_version requires a context with a method"); + return SetVersionBound(*static_cast(context->method), version, true, + context->maxTlsVersion, context->minTlsVersion, context->minProtoVersion); +} + +int APS5_VABI SSL_CTX_set_max_proto_version_nid_postfix(LibreSsl3SslCtx* context, std::uint16_t version) { + if (!context || !context->method) + throw std::invalid_argument("SSL_CTX_set_max_proto_version requires a context with a method"); + return SetVersionBound(*static_cast(context->method), version, false, + context->minTlsVersion, context->maxTlsVersion, context->maxProtoVersion); +} + +std::size_t APS5_VABI SSL_get_client_random_nid_postfix(const LibreSsl3Ssl* ssl, unsigned char* out, std::size_t maxOut) { + constexpr std::size_t randomSize = sizeof(LibreSsl3Ssl3State::clientRandom); + if (!out) return randomSize; + if (!ssl) + throw std::invalid_argument("SSL_get_client_random requires an SSL"); + const auto length = std::min(randomSize, maxOut); + if (length == 0) return 0; + if (!ssl->s3) + throw std::invalid_argument("SSL_get_client_random requires SSL3 state"); + std::memcpy(out, ssl->s3->clientRandom, length); + return length; +} + +} diff --git a/core/libs/prx/libSceLibreSsl3/src/X509.cpp b/core/libs/prx/libSceLibreSsl3/src/X509.cpp new file mode 100644 index 000000000000..8353bfd7ac96 --- /dev/null +++ b/core/libs/prx/libSceLibreSsl3/src/X509.cpp @@ -0,0 +1,60 @@ +#include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp" +#include "prx/libSceLibreSsl3/include/Reference.hpp" +#include "prx/libSceLibreSsl/include/Error.hpp" + +#include +#include +#include + +namespace { + +constexpr int Asn1Library = 13; +constexpr int AnyFunction = 0xfff; +constexpr int TooLarge = 223; +constexpr int TooSmall = 224; +constexpr int WrongIntegerType = 225; +constexpr std::int32_t IntegerType = 2; +constexpr std::int32_t NegativeIntegerType = 0x102; + +std::int64_t Asn1Error(int reason) { + LibreSslPutError_nid_no_patch(Asn1Library, AnyFunction, reason); + return -1; +} + +std::int64_t IntegerValue(const Asn1String* integer) { + if (!integer) return 0; + if (integer->length < 0) return -1; + if (integer->type != IntegerType && integer->type != NegativeIntegerType) return Asn1Error(WrongIntegerType); + if (integer->length > 0 && !integer->data) + throw std::invalid_argument("X509_get_version requires integer data"); + std::uint64_t magnitude = 0; + for (std::int32_t index = 0; index < integer->length; ++index) { + if (magnitude > std::numeric_limits::max() >> 8) return Asn1Error(TooLarge); + magnitude = (magnitude << 8) | integer->data[index]; + } + const auto largest = static_cast(std::numeric_limits::max()); + if (integer->type == NegativeIntegerType) { + if (magnitude > largest + 1) return Asn1Error(TooSmall); + return static_cast(0 - magnitude); + } + if (magnitude > largest) return Asn1Error(TooLarge); + return static_cast(magnitude); +} + +} // namespace + +extern "C" { + +int APS5_VABI X509_up_ref_nid_postfix(LibreSsl3X509* certificate) { + if (!certificate) + throw std::invalid_argument("X509_up_ref requires a certificate"); + return Retain_nid_no_patch(certificate->references); +} + +std::int64_t APS5_VABI X509_get_version_nid_postfix(const LibreSsl3X509* certificate) { + if (!certificate || !certificate->certInfo) + throw std::invalid_argument("X509_get_version requires certificate info"); + return IntegerValue(certificate->certInfo->version); +} + +} diff --git a/core/libs/tests/GuestLibreSsl3KeyReference.cpp b/core/libs/tests/GuestLibreSsl3KeyReference.cpp index 5857f916056c..7e8d416896df 100644 --- a/core/libs/tests/GuestLibreSsl3KeyReference.cpp +++ b/core/libs/tests/GuestLibreSsl3KeyReference.cpp @@ -138,5 +138,7 @@ int main(int argc, char** argv) { } if (argc == 1 || std::strcmp(argv[1], "rsa") == 0) CheckKey(RSA_up_ref_nid_postfix, 104); + if (argc == 1 || std::strcmp(argv[1], "ssl_ctx") == 0) + CheckKey(SSL_CTX_up_ref_nid_postfix, 40); return 0; } diff --git a/core/libs/tests/GuestLibreSsl3Ssl.cpp b/core/libs/tests/GuestLibreSsl3Ssl.cpp new file mode 100644 index 000000000000..f5b55052e971 --- /dev/null +++ b/core/libs/tests/GuestLibreSsl3Ssl.cpp @@ -0,0 +1,269 @@ +#include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +constexpr std::uint64_t TooLarge = 0x0DFFF0DF; +constexpr std::uint64_t TooSmall = 0x0DFFF0E0; +constexpr std::uint64_t WrongIntegerType = 0x0DFFF0E1; + +void Require(bool condition, const char* message, std::source_location location = std::source_location::current()) { + if (condition) return; + std::fprintf(stderr, "line %u: %s\n", location.line(), message); + std::abort(); +} + +template +struct Guarded { + std::array before; + TObject object; + std::array after; +}; + +template +std::array)> Bytes(const Guarded& guarded) { + std::array)> bytes; + std::memcpy(bytes.data(), &guarded, sizeof(guarded)); + return bytes; +} + +template +bool Same(const Guarded& guarded, const std::array)>& expected) { + return std::memcmp(&guarded, expected.data(), sizeof(guarded)) == 0; +} + +template +void CheckRetain(TRetain retain, std::size_t referenceOffset) { + const std::size_t offset = offsetof(Guarded, object) + referenceOffset; + Guarded guarded; + for (std::int32_t initial : {-2, 0, 1, 37, std::numeric_limits::max() - 1}) { + std::memset(&guarded, 0xA5, sizeof(guarded)); + guarded.object.references = initial; + auto expected = Bytes(guarded); + const std::int32_t next = initial + 1; + std::memcpy(expected.data() + offset, &next, sizeof(next)); + Require(retain(&guarded.object) == (next > 1), "retain return value"); + Require(Same(guarded, expected), "retain changed bytes outside the reference count"); + } + std::memset(&guarded, 0x5A, sizeof(guarded)); + guarded.object.references = 1; + auto expected = Bytes(guarded); + std::atomic start = false; + std::array threads; + for (auto& thread : threads) { + thread = std::thread([&] { + while (!start.load()) + std::this_thread::yield(); + for (int index = 0; index < 1000; ++index) + Require(retain(&guarded.object) == 1, "concurrent retain"); + }); + } + start = true; + for (auto& thread : threads) + thread.join(); + const std::int32_t finalCount = 4001; + std::memcpy(expected.data() + offset, &finalCount, sizeof(finalCount)); + Require(Same(guarded, expected), "concurrent count or adjacent bytes changed"); +} + +struct Versions { + std::uint16_t minTls; + std::uint16_t maxTls; + std::uint16_t minProto; + std::uint16_t maxProto; +}; + +template +void CheckBound(TSet set, const LibreSsl3SslMethod& method, Versions initial, std::uint16_t version, int result, + Versions final, const char* message, std::source_location location = std::source_location::current()) { + Guarded guarded; + std::memset(&guarded, 0xA5, sizeof(guarded)); + guarded.object.method = &method; + guarded.object.minTlsVersion = initial.minTls; + guarded.object.maxTlsVersion = initial.maxTls; + guarded.object.minProtoVersion = initial.minProto; + guarded.object.maxProtoVersion = initial.maxProto; + auto expected = Bytes(guarded); + const std::array fields = {final.minTls, final.maxTls, final.minProto, final.maxProto}; + std::memcpy(expected.data() + offsetof(Guarded, object) + 128, fields.data(), sizeof(fields)); + Require(set(&guarded.object, version) == result, message, location); + Require(Same(guarded, expected), message, location); +} + +void CheckVersionBounds() { + const auto setMin = SSL_CTX_set_min_proto_version_nid_postfix; + const auto setMax = SSL_CTX_set_max_proto_version_nid_postfix; + const LibreSsl3SslMethod tls{0, 0, 0x0304, 0x0301, 0x0304}; + const Versions full{0x0301, 0x0304, 0, 0}; + CheckBound(setMin, tls, {0x0302, 0x0304, 0x0302, 0}, 0, 1, full, "TLS minimum 0 resets to the method minimum"); + CheckBound(setMax, tls, {0x0301, 0x0302, 0, 0x0302}, 0, 1, full, "TLS maximum 0 resets to the method maximum"); + CheckBound(setMin, tls, full, 0x0303, 1, {0x0303, 0x0304, 0x0303, 0}, "TLS minimum 1.2"); + CheckBound(setMin, tls, full, 0x0304, 1, {0x0304, 0x0304, 0x0304, 0}, "TLS minimum equal to the maximum"); + CheckBound(setMin, tls, full, 0x0300, 1, {0x0301, 0x0304, 0x0301, 0}, "TLS minimum raised to the method minimum"); + CheckBound(setMin, tls, full, 0x0305, 0, full, "TLS minimum above the maximum"); + CheckBound(setMax, tls, full, 0x0303, 1, {0x0301, 0x0303, 0, 0x0303}, "TLS maximum 1.2"); + CheckBound(setMax, tls, full, 0x0301, 1, {0x0301, 0x0301, 0, 0x0301}, "TLS maximum equal to the minimum"); + CheckBound(setMax, tls, full, 0x0305, 1, {0x0301, 0x0304, 0, 0x0304}, "TLS maximum lowered to the method maximum"); + CheckBound(setMax, tls, full, 0x0300, 0, full, "TLS maximum below the minimum"); + for (std::uint16_t version : {0xFEFF, 0xFEFD}) { + CheckBound(setMin, tls, full, version, 0, full, "DTLS minimum on a TLS method"); + CheckBound(setMax, tls, full, version, 1, {0x0301, 0x0304, 0, 0x0304}, "DTLS maximum on a TLS method"); + } + + const LibreSsl3SslMethod narrow{0, 0, 0x0303, 0x0301, 0x0303}; + CheckBound(setMin, narrow, {0x0301, 0x0304, 0, 0}, 0x0302, 1, {0x0302, 0x0304, 0x0302, 0}, + "clamping the stored maximum leaves it unchanged"); + const LibreSsl3SslMethod empty{0, 0, 0, 0, 0}; + CheckBound(setMax, empty, {0, 0, 0x0301, 0x0301}, 0x0303, 1, {0, 0, 0x0301, 0}, "a TLS bound of 0 is stored"); + + const LibreSsl3SslMethod inverted{0, 0, 0x0303, 0x0303, 0x0302}; + CheckBound(setMin, inverted, {0x0302, 0x0303, 0, 0}, 0x0302, 0, {0x0302, 0x0303, 0, 0}, "inverted method range"); + CheckBound(setMax, inverted, {0x0302, 0x0303, 0, 0}, 0x0303, 0, {0x0302, 0x0303, 0, 0}, "inverted method range"); + CheckBound(setMin, tls, {0x0301, 0x0302, 0, 0x0304}, 0x0303, 0, {0x0301, 0x0302, 0, 0x0304}, + "minimum above the stored TLS maximum"); + CheckBound(setMax, tls, {0x0303, 0x0304, 0x0301, 0}, 0x0302, 0, {0x0303, 0x0304, 0x0301, 0}, + "maximum below the stored TLS minimum"); + CheckBound(setMin, narrow, {0x0301, 0x0304, 0, 0}, 0x0304, 0, {0x0301, 0x0304, 0, 0}, "range above the method"); + const LibreSsl3SslMethod upper{0, 0, 0x0304, 0x0302, 0x0304}; + CheckBound(setMax, upper, {0x0301, 0x0304, 0, 0}, 0x0301, 0, {0x0301, 0x0304, 0, 0}, "range below the method"); + + const LibreSsl3SslMethod dtls{1, 0, 0xFEFD, 0x0302, 0x0303}; + const Versions dtlsFull{0x0302, 0x0303, 0, 0}; + CheckBound(setMin, dtls, dtlsFull, 0xFEFF, 1, {0x0302, 0x0303, 0xFEFF, 0}, "DTLS minimum 1.0"); + CheckBound(setMin, dtls, dtlsFull, 0xFEFD, 1, {0x0303, 0x0303, 0xFEFD, 0}, "DTLS minimum 1.2"); + CheckBound(setMax, dtls, dtlsFull, 0xFEFD, 1, {0x0302, 0x0303, 0, 0xFEFD}, "DTLS maximum 1.2"); + CheckBound(setMax, dtls, dtlsFull, 0xFEFF, 1, {0x0302, 0x0302, 0, 0xFEFF}, "DTLS maximum 1.0"); + CheckBound(setMin, dtls, {0x0303, 0x0303, 0xFEFD, 0}, 0, 1, dtlsFull, "DTLS minimum 0"); + CheckBound(setMax, dtls, {0x0302, 0x0302, 0, 0xFEFF}, 0, 1, dtlsFull, "DTLS maximum 0"); + for (std::uint16_t version : {0x0302, 0x0303, 0xFEFE, 0xFEFC, 0x0100}) { + CheckBound(setMin, dtls, dtlsFull, version, 0, dtlsFull, "DTLS minimum outside the DTLS versions"); + CheckBound(setMax, dtls, dtlsFull, version, 0, dtlsFull, "DTLS maximum outside the DTLS versions"); + } + CheckBound(setMin, dtls, {0x0302, 0x0302, 0, 0}, 0xFEFD, 0, {0x0302, 0x0302, 0, 0}, + "DTLS minimum above the maximum"); + const LibreSsl3SslMethod anyDtls{-1, 0, 0xFEFD, 0x0302, 0x0303}; + CheckBound(setMin, anyDtls, dtlsFull, 0xFEFF, 1, {0x0302, 0x0303, 0xFEFF, 0}, "nonzero dtls flag"); + CheckBound(setMin, anyDtls, dtlsFull, 0x0302, 0, dtlsFull, "nonzero dtls flag rejects TLS versions"); + const LibreSsl3SslMethod dtlsTls10{1, 0, 0, 0x0301, 0x0301}; + CheckBound(setMax, dtlsTls10, {0x0301, 0x0301, 0, 0}, 0xFEFF, 0, {0x0301, 0x0301, 0, 0}, + "clamped maximum without a DTLS form"); + CheckBound(setMin, dtlsTls10, {0x0302, 0x0301, 0xFEFF, 0}, 0, 1, {0x0301, 0x0301, 0, 0}, + "DTLS minimum 0 is not converted"); + const LibreSsl3SslMethod dtlsTls13{1, 0, 0, 0x0304, 0x0304}; + CheckBound(setMin, dtlsTls13, {0x0304, 0x0304, 0, 0}, 0xFEFF, 0, {0x0304, 0x0304, 0, 0}, + "clamped minimum without a DTLS form"); +} + +void CheckClientRandom() { + LibreSsl3Ssl3State state; + std::array stateBytes; + for (std::size_t index = 0; index < stateBytes.size(); ++index) + stateBytes[index] = static_cast(index * 7 + 3); + std::memcpy(&state, stateBytes.data(), sizeof(state)); + LibreSsl3Ssl ssl; + std::memset(&ssl, 0x5A, sizeof(ssl)); + ssl.s3 = &state; + const std::size_t limits[] = {0, 1, 10, 31, 32, 33, 64, std::numeric_limits::max()}; + for (std::size_t limit : limits) { + Require(SSL_get_client_random_nid_postfix(nullptr, nullptr, limit) == 32, "null output with a null SSL"); + Require(SSL_get_client_random_nid_postfix(&ssl, nullptr, limit) == 32, "null output"); + std::array out; + out.fill(0xEE); + const auto copied = std::min(limit, 32); + Require(SSL_get_client_random_nid_postfix(&ssl, out.data(), limit) == copied, "copied length"); + for (std::size_t index = 0; index < out.size(); ++index) + Require(out[index] == (index < copied ? stateBytes[40 + index] : 0xEE), "client random bytes or untouched tail"); + } + Require(std::memcmp(&state, stateBytes.data(), sizeof(state)) == 0, "SSL3 state unchanged"); + ssl.s3 = nullptr; + std::array out{}; + Require(SSL_get_client_random_nid_postfix(&ssl, out.data(), 0) == 0, "an empty copy does not read SSL3 state"); +} + +struct VersionResult { + std::int64_t value; + std::uint64_t error; + std::uint64_t following; +}; + +VersionResult ReadVersion(Asn1String* version) { + LibreSsl3X509CertInfo info{version}; + LibreSsl3X509 certificate; + std::memset(&certificate, 0xA5, sizeof(certificate)); + certificate.certInfo = &info; + ERR_clear_error_nid_postfix(); + VersionResult result; + result.value = X509_get_version_nid_postfix(&certificate); + result.error = ERR_get_error_nid_postfix(); + result.following = ERR_get_error_nid_postfix(); + return result; +} + +void CheckVersion(std::int32_t type, std::vector bytes, std::int64_t value, std::uint64_t error, + const char* message, std::source_location location = std::source_location::current()) { + Asn1String integer{static_cast(bytes.size()), type, bytes.data(), 0}; + const auto result = ReadVersion(&integer); + Require(result.value == value && result.error == error && result.following == 0, message, location); +} + +void CheckX509Version() { + const auto absent = ReadVersion(nullptr); + Require(absent.value == 0 && absent.error == 0, "absent version"); + unsigned char byte = 2; + for (unsigned char* data : {&byte, static_cast(nullptr)}) { + Asn1String empty{0, 2, data, 0}; + const auto result = ReadVersion(&empty); + Require(result.value == 0 && result.error == 0, "empty integer"); + } + for (std::int32_t type : {2, 4}) { + Asn1String negativeLength{-1, type, &byte, 0}; + const auto result = ReadVersion(&negativeLength); + Require(result.value == -1 && result.error == 0, "negative length"); + } + const auto maximum = std::numeric_limits::max(); + const auto minimum = std::numeric_limits::min(); + CheckVersion(2, {0x02}, 2, 0, "encoded version 2"); + CheckVersion(2, {0x00, 0x02}, 2, 0, "leading zero"); + CheckVersion(2, {0x01, 0x00}, 256, 0, "big-endian magnitude"); + CheckVersion(2, {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0}, 0x123456789ABCDEF0, 0, "eight bytes"); + CheckVersion(2, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, maximum, 0, "largest value"); + CheckVersion(2, {0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, maximum, 0, "nine bytes with a leading zero"); + std::vector padded(20, 0x00); + padded.push_back(0x05); + CheckVersion(2, padded, 5, 0, "many leading zeros"); + CheckVersion(0x102, {0x01}, -1, 0, "negative one"); + CheckVersion(0x102, {0x00}, 0, 0, "negative zero"); + CheckVersion(0x102, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, minimum, 0, "smallest value"); + CheckVersion(4, {0x02}, -1, WrongIntegerType, "octet string type"); + CheckVersion(0x100, {0x02}, -1, WrongIntegerType, "negative flag alone"); + CheckVersion(2, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, -1, TooLarge, "positive value above the maximum"); + CheckVersion(2, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, -1, TooLarge, "64-bit positive value"); + CheckVersion(2, {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, -1, TooLarge, "nine significant bytes"); + CheckVersion(0x102, {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, -1, TooLarge, + "nine significant negative bytes"); + CheckVersion(0x102, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, -1, TooSmall, "negative value below the minimum"); +} + +} // namespace + +int main() { + CheckRetain(SSL_up_ref_nid_postfix, 176); + CheckRetain(SSL_SESSION_up_ref_nid_postfix, 184); + CheckVersionBounds(); + CheckClientRandom(); + CheckRetain(X509_up_ref_nid_postfix, 28); + CheckX509Version(); + return 0; +} diff --git a/docs/dev/debt/libSceLibreSsl3.md b/docs/dev/debt/libSceLibreSsl3.md index 1244db4196fe..42209b638cd6 100644 --- a/docs/dev/debt/libSceLibreSsl3.md +++ b/docs/dev/debt/libSceLibreSsl3.md @@ -10,7 +10,7 @@ ### Unknown function info -- [libSceLibreSsl3 pending exports](../../../core/libs/prx/libSceLibreSsl3/Export.cpp) - 54 named callable exports, excluding `ASN1_OBJECT_free`, `CRYPTO_memcmp`, `MD5`, `SHA1`, `d2i_ASN1_INTEGER`, `EVP_PKEY_up_ref`, `EC_KEY_up_ref`, `RSA_up_ref`, `EVP_PKEY_get0_RSA`, `EVP_PKEY_get0_DSA`, `EVP_PKEY_get0_DH`, `EVP_MD_CTX_new`, `EVP_MD_CTX_free`, `EVP_DigestInit`, `EVP_sha1`, `EVP_sha224`, `ERR_load_crypto_strings`, `ERR_peek_last_error`, `OpenSSL_version`, `X509_PUBKEY_get0_param`, `OBJ_cmp`, `ASN1_STRING_get0_data`, `ASN1_INTEGER_free`, `X509_get0_extensions`, `X509_get_ext_count`, `X509_get_ext`, `EVP_PKEY_size`, `X509_getm_notBefore`, `X509_getm_notAfter`, `X509_get0_notBefore`, `X509_get0_notAfter`, `X509_get0_signature`, `RSA_get0_key`, `DH_get0_pqg`, `DH_get0_key`, `DSA_get0_pqg` and `DSA_get0_key`, remain unimplemented from the [PS5 3.20 export table](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceLibreSsl3.c), cross-checked against the same module in PS5 firmware 13.60. Their signatures and behaviour are unverified; the declarations use the no-parameter unknown-signature convention and throw through `NotImplemented_nid_no_patch`. +- [libSceLibreSsl3 pending exports](../../../core/libs/prx/libSceLibreSsl3/Export.cpp) - 46 named callable exports, excluding `ASN1_OBJECT_free`, `CRYPTO_memcmp`, `MD5`, `SHA1`, `d2i_ASN1_INTEGER`, `EVP_PKEY_up_ref`, `EC_KEY_up_ref`, `RSA_up_ref`, `SSL_CTX_up_ref`, `SSL_up_ref`, `SSL_SESSION_up_ref`, `SSL_CTX_set_min_proto_version`, `SSL_CTX_set_max_proto_version`, `SSL_get_client_random`, `X509_up_ref`, `X509_get_version`, `EVP_PKEY_get0_RSA`, `EVP_PKEY_get0_DSA`, `EVP_PKEY_get0_DH`, `EVP_MD_CTX_new`, `EVP_MD_CTX_free`, `EVP_DigestInit`, `EVP_sha1`, `EVP_sha224`, `ERR_load_crypto_strings`, `ERR_peek_last_error`, `OpenSSL_version`, `X509_PUBKEY_get0_param`, `OBJ_cmp`, `ASN1_STRING_get0_data`, `ASN1_INTEGER_free`, `X509_get0_extensions`, `X509_get_ext_count`, `X509_get_ext`, `EVP_PKEY_size`, `X509_getm_notBefore`, `X509_getm_notAfter`, `X509_get0_notBefore`, `X509_get0_notAfter`, `X509_get0_signature`, `RSA_get0_key`, `DH_get0_pqg`, `DH_get0_key`, `DSA_get0_pqg` and `DSA_get0_key`, remain unimplemented from the [PS5 3.20 export table](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceLibreSsl3.c), cross-checked against the same module in PS5 firmware 13.60. Their signatures and behaviour are unverified; the declarations use the no-parameter unknown-signature convention and throw through `NotImplemented_nid_no_patch`. ### Functional @@ -18,6 +18,9 @@ - [CRYPTO_memcmp](../../../core/libs/prx/libSceLibreSsl3/Export.cpp) follows the public [LibreSSL signature and equality contract](https://man.openbsd.org/CRYPTO_memcmp.3), without PS5 verification. The exact nonzero result for unequal buffers is unspecified by that contract. Content-independent execution of its volatile byte reads still depends on the compiler; inspect optimized code when changing toolchains. Closes when: measured on a PS5 and the supported toolchains' generated code is checked. - [ASN1_OBJECT_free](../../../core/libs/prx/libSceLibreSsl3/src/Asn1Object.cpp) - the structure layout and ownership flags follow [LibreSSL 4.0.0](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/asn1_local.h) and its [free routine](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/a_object.c); nothing is checked on the console. Dynamic names and OID data use the guest heap, and dynamic OID data is erased before it is freed. A negative data length with non-null dynamic data throws rather than passing an oversized length to the erase operation. - [MD5 and SHA1](../../../core/libs/prx/libSceLibreSsl3/src/Digest.cpp) (libSceLibreSsl3) - follow LibreSSL's [md5.c, sha1.c](https://github.com/libressl/openbsd/tree/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto) and hash through libSceLibreSsl's `MD5_*` and `SHA1_*`. A null digest buffer, which LibreSSL 4.0.0 does not check, throws. Nothing is checked on the console +- [SSL_CTX_up_ref](../../../core/libs/prx/libSceLibreSsl3/src/Key.cpp) - console layout and retain/release interoperability are unverified. The SSL_CTX layout, with references counter at offset 40, follows [LibreSSL 4.0.0](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_local.h#L665-L678). Atomic retain replaces internal locking; future releases must synchronize with the same counter. With no context constructor or destructor in this module, tests use caller-owned objects without validating allocator or destruction compatibility. Null context and signed-counter overflow, undefined upstream, throw without mutation. Closes when: console layout and lifetime interoperability are measured. +- [SSL and SSL_SESSION retains, SSL_CTX version bounds and SSL_get_client_random](../../../core/libs/prx/libSceLibreSsl3/src/Ssl.cpp) - the SSL_METHOD, SSL_CTX, SSL, SSL3_STATE and SSL_SESSION offsets follow [LibreSSL 4.0.0 ssl_local.h](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_local.h) and are not measured on the console. No SSL constructor is implemented: libSceLibreSsl3 importers reach libSceLibreSsl's LibreSSL 2.5.4 stubs, by dependency lookup on Linux and macOS and parent export forwarding on Windows. 2.5.4 puts the SSL and SSL_SESSION counters at 168 and 160 and SSL s3 at 48, and the method and SSL_CTX version bounds in internal structs, so every call here would misread a 2.5.4 object. Signed-counter overflow at INT32_MAX and null objects, methods and SSL3 state, undefined upstream, throw without mutation. Closes when: the offsets are measured on the firmware module and SSL constructors use these layouts. +- [X509_up_ref and X509_get_version](../../../core/libs/prx/libSceLibreSsl3/src/X509.cpp) - X509_get_version queues its ASN.1 errors in the libSceLibreSsl queue without file or line, while the firmware module keeps its own queue. libSceLibreSsl3 importers read that queue with ERR_get_error, which resolves to libSceLibreSsl by dependency lookup on Linux and macOS and through parent export forwarding on Windows. The X509 and X509_CINF offsets follow [LibreSSL 4.0.0 x509_local.h](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/x509/x509_local.h), match 2.5.4 and are not measured on the console. Signed-counter overflow at INT32_MAX and null certificates, certificate info and integer data with a positive length, undefined upstream, throw without mutation. Closes when: the offsets and the error queue are measured on the firmware module. - [EVP_PKEY_up_ref, EC_KEY_up_ref and RSA_up_ref](../../../core/libs/prx/libSceLibreSsl3/src/Key.cpp) - console layouts and retain/release interoperability are unverified. The 32-byte EVP, 64-byte EC and 152-byte RSA layouts, with counters at offsets 4, 48 and 104, follow [LibreSSL 4.0.0](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto) `evp/evp_local.h`, `evp/p_lib.c`, `ec/ec_local.h`, `ec/ec_key.c`, `rsa/rsa_local.h` and `rsa/rsa_lib.c`; the RSA layout has no `engine` field, unlike libSceLibreSsl's `LibreSslRsa`. Atomic retains replace its internal mutex; future releases must synchronize with the same counters. Tests use caller-owned objects without validating allocator or destruction compatibility; EC tests also use libSceLibreSsl's keys, which share that layout. Null keys and signed-counter overflow, undefined upstream, throw without mutation. Closes when: console layouts and lifetime interoperability are measured. - [EVP_PKEY_get0_RSA, EVP_PKEY_get0_DSA and EVP_PKEY_get0_DH](../../../core/libs/prx/libSceLibreSsl3/src/PkeyGetters.cpp) (libSceLibreSsl3) - a key of another type throws, where [LibreSSL 4.0.0](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/evp/p_lib.c#L630-L751) queues `EVP_R_EXPECTING_AN_RSA_KEY`, `EVP_R_EXPECTING_A_DSA_KEY` or `EVP_R_EXPECTING_A_DH_KEY` and returns NULL, because the LibreSSL3 error queue is not modelled; a null key throws, where LibreSSL dereferences it. A matching key returns the inner key pointer of the 32-byte layout without taking a reference: `EVP_PKEY_RSA` (6) or `EVP_PKEY_RSA_PSS` (912) for RSA, `EVP_PKEY_DSA` (116) and `EVP_PKEY_DH` (28). None of this is verified on a PS5 - [EVP digest helpers](../../../core/libs/prx/libSceLibreSsl3/src/EvpDigest.cpp) - host fallback differs from [13.42 native-module emulation](https://github.com/boykopovar/AnyPS5/pull/10960#issuecomment-6402692742): independent modules use 48/40-byte contexts and state-size offsets 0x68/0x44. Host ELF lookup instead shares legacy 120/48-byte records with the opaque l3 types. Custom methods require this layout and SysV callbacks; untagged foreign layouts cannot be detected. Our new/init/free helpers follow [4.0 operation order](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/evp/evp_digest.c). Fallback version, error-queue and retry behaviour remain legacy; full 4.0 equivalence is unverified. Windows parent-NID aliases provide legacy forwarders, but mixed Windows execution and native layout/routing interoperability are unverified. macOS execution, physical-console behaviour and other firmware layouts/routing remain unverified.