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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ int APS5_VABI EVP_PKEY_assign_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI EVP_PKEY_bits_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI EVP_PKEY_derive_set_peer_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}
Expand Down
6 changes: 6 additions & 0 deletions core/libs/prx/libSceLibreSsl3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ if(BUILD_TESTING)
target_include_directories(guest_libressl3_key_reference_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libressl3_key_reference_tests PRIVATE libSceLibreSsl3 libSceLibreSsl mbedcrypto)
configure_windows_unwind(guest_libressl3_key_reference_tests)
add_test(NAME guest_libressl3_pkey_bits COMMAND guest_libressl3_key_reference_tests bits)
set_tests_properties(guest_libressl3_pkey_bits PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_libressl3_pkey_bits PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl3>")
endif()
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)
Expand Down
2 changes: 1 addition & 1 deletion core/libs/prx/libSceLibreSsl3/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ int APS5_VABI EVP_CIPHER_key_length_nid_postfix(const LibreSsl3EvpCipher* cipher
const void* APS5_VABI EVP_MD_CTX_md_nid_postfix(const void* context);
int APS5_VABI EVP_PKEY_CTX_free_nid_postfix();
int APS5_VABI EVP_PKEY_assign_nid_postfix();
int APS5_VABI EVP_PKEY_bits_nid_postfix();
int APS5_VABI EVP_PKEY_bits_nid_postfix(const LibreSsl3EvpPkey* key);
int APS5_VABI EVP_PKEY_derive_set_peer_nid_postfix();
int APS5_VABI EVP_PKEY_get0_EC_KEY_nid_postfix();
int APS5_VABI EVP_PKEY_verify_nid_postfix();
Expand Down
19 changes: 19 additions & 0 deletions core/libs/prx/libSceLibreSsl3/src/Key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,29 @@
#include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp"
#include "prx/libSceLibreSsl3/include/Reference.hpp"

#include <cstring>
#include <stdexcept>

namespace {

using PkeyBitsFunction = int (APS5_VABI *)(const LibreSsl3EvpPkey*);
constexpr std::size_t PkeyBitsOffset = 104;

static_assert(sizeof(PkeyBitsFunction) == 8);
static_assert(offsetof(LibreSsl3EvpPkey, method) == 8);

} // namespace

extern "C" {

int APS5_VABI EVP_PKEY_bits_nid_postfix(const LibreSsl3EvpPkey* key) {
if (!key || !key->method)
return 0;
PkeyBitsFunction bits;
std::memcpy(&bits, static_cast<const unsigned char*>(key->method) + PkeyBitsOffset, sizeof(bits));
return bits ? bits(key) : 0;
}

int APS5_VABI EVP_PKEY_up_ref_nid_postfix(LibreSsl3EvpPkey* key) {
if (!key)
throw std::invalid_argument("EVP_PKEY_up_ref requires a key");
Expand Down
54 changes: 54 additions & 0 deletions core/libs/tests/GuestLibreSsl3KeyReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,58 @@ static void Require(bool condition, const char* message) {
}
}

using PkeyBitsFunction = int (APS5_VABI *)(const LibreSsl3EvpPkey*);

static const LibreSsl3EvpPkey* expectedBitsKey;
static int bitsCalls;

static int APS5_VABI BitsFromKey(const LibreSsl3EvpPkey* key) {
Require(key == expectedBitsKey, "bits callback received the wrong key");
++bitsCalls;
return key->type;
}

static int APS5_VABI WrongMethod(const LibreSsl3EvpPkey*) {
Require(false, "bits called a neighboring method slot");
return 0;
}

static void CheckBits() {
Require(EVP_PKEY_bits_nid_postfix(nullptr) == 0, "null key bit count");
LibreSsl3EvpPkey key{6, 17, nullptr, reinterpret_cast<void*>(1), 1};
std::array<unsigned char, sizeof(key)> before;
std::memcpy(before.data(), &key, sizeof(key));
Require(EVP_PKEY_bits_nid_postfix(&key) == 0, "key without a method bit count");
Require(std::memcmp(before.data(), &key, sizeof(key)) == 0, "bits changed a key without a method");
alignas(8) std::array<unsigned char, 112> method{};
const PkeyBitsFunction wrong = WrongMethod;
std::memcpy(method.data() + 96, &wrong, sizeof(wrong));
key.method = method.data();
std::memcpy(before.data(), &key, sizeof(key));
auto unchangedMethod = method;
Require(EVP_PKEY_bits_nid_postfix(&key) == 0, "key without a bits callback bit count");
Require(std::memcmp(before.data(), &key, sizeof(key)) == 0, "bits changed a key without a callback");
Require(method == unchangedMethod, "bits changed a method without a callback");
const PkeyBitsFunction callback = BitsFromKey;
std::memcpy(method.data() + 104, &callback, sizeof(callback));
unchangedMethod = method;
expectedBitsKey = &key;
for (int result : {0, 1, 255, 256, 2048, -7, std::numeric_limits<int>::min(), std::numeric_limits<int>::max()}) {
key.type = result;
std::memcpy(before.data(), &key, sizeof(key));
const int calls = bitsCalls;
Require(EVP_PKEY_bits_nid_postfix(&key) == result, "bits did not preserve the callback result");
Require(bitsCalls == calls + 1, "bits did not call the callback exactly once");
Require(std::memcmp(before.data(), &key, sizeof(key)) == 0, "bits changed the key");
Require(method == unchangedMethod, "bits changed the key method");
}
const PkeyBitsFunction missing = nullptr;
std::memcpy(method.data() + 104, &missing, sizeof(missing));
const int calls = bitsCalls;
Require(EVP_PKEY_bits_nid_postfix(&key) == 0, "bits retained a removed callback");
Require(bitsCalls == calls, "bits called a removed callback");
}

template<class TKey, class TRetain>
static void CheckKey(TRetain retain, std::size_t referenceOffset) {
struct GuardedKey {
Expand Down Expand Up @@ -138,6 +190,8 @@ int main(int argc, char** argv) {
}
if (argc == 1 || std::strcmp(argv[1], "rsa") == 0)
CheckKey<LibreSsl3Rsa>(RSA_up_ref_nid_postfix, 104);
if (argc == 1 || std::strcmp(argv[1], "bits") == 0)
CheckBits();
if (argc == 1 || std::strcmp(argv[1], "ssl_ctx") == 0)
CheckKey<LibreSsl3SslCtx>(SSL_CTX_up_ref_nid_postfix, 40);
return 0;
Expand Down
5 changes: 3 additions & 2 deletions docs/dev/debt/libSceLibreSsl3.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
- [EVP_MD_CTX_free and EVP_DigestInit](../../../core/libs/prx/libSceLibreSsl3/src/EvpDigest.cpp) - non-null engine ownership is unknown and throws, including an engine installed by cleanup. Owned non-null `EVP_PKEY_CTX` destruction is unknown and throws after digest cleanup; `KEEP_PKEY_CTX` preserves caller ownership. Digest-state allocation failure and negative initialization sizes throw instead of returning zero with an allocation error. Free accepts a negative-size descriptor when it has no owned data to release; releasing non-null data with a negative size throws after cleanup. Null contexts/types and missing initialization callbacks throw. `EVP_DigestInit` clears the context without freeing previous state, as upstream does; callers must release previous state first to avoid a leak.
- [RSA_get0_factors and RSA_get0_crt_params](../../../core/libs/prx/libSceLibreSsl3/src/Key.cpp) - console RSA layout and behaviour are unverified. They read the existing 152-byte RSA layout at offsets 48, 56, 64, 72 and 80, following [LibreSSL 4.0.0](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/rsa/rsa_lib.c) `rsa_lib.c` and `rsa_local.h`. A null RSA with any requested output, which upstream dereferences, throws without writing outputs. Closes when: compared with the console.

- [EVP_PKEY_bits](../../../core/libs/prx/libSceLibreSsl3/src/Key.cpp) has no console ABI or behavior verification; the console module's LibreSSL revision is unknown. Its `int (const EVP_PKEY*)` signature and dispatch assume [LibreSSL 4.0.0](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/evp/p_lib.c): a null key, method or callback returns 0; otherwise it calls `pkey_bits` with the original key and returns its result. The method pointer at offset 8 uses the existing 32-byte key model. The callback offset 104 assumes that revision's LP64 `EVP_PKEY_ASN1_METHOD` layout in `evp_local.h`; another revision or layout may require a different offset. Tests use caller-owned keys and methods; native layouts and interoperability with real key constructors remain unverified. Closes when: measured on a PS5.

- [Parent export forwarding](../../../core/libs/prx/libSceLibreSsl3/ParentExports.cpp) exposes 387 parent-library exports on Windows through IAT tail jumps without changing their ABI. Non-Windows hosts use their existing dependency lookup. Static COFF inspection verifies names, parent imports and tail jumps; Windows loading and runtime behavior remain unverified. Parent functions retain their existing implementation limitations.
- [EVP_MD_CTX_md](../../../core/libs/prx/libSceLibreSsl3/src/DigestContext.cpp) (libSceLibreSsl3) - nothing is checked on the console. As in [LibreSSL 4.0.0](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/evp/evp_digest.c) `evp_digest.c`, a null context returns null and any other returns its first field, `digest`. That field is first in 4.0.0's `evp_local.h` and in the [2.5.4 layout](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/evp/evp.h) of the contexts the forwarded libSceLibreSsl exports handle; the layouts differ after it, so the context and digest are declared opaque. Closes when: compared with the console.
- [EVP_CIPHER_key_length and EVP_CIPHER_iv_length](../../../core/libs/prx/libSceLibreSsl3/src/Cipher.cpp) (libSceLibreSsl3) - nothing is checked on the console. They return `key_len` and `iv_len` from the 80-byte `EVP_CIPHER` of [LibreSSL 4.0.0](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/evp) `evp_local.h` and `evp_cipher.c`. The fields are at offsets 8 and 12, as in the [2.5.4 layout](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/evp/evp.h) of the ciphers the forwarded libSceLibreSsl exports handle. A null cipher, which LibreSSL dereferences, throws. Closes when: compared with the console.
Expand All @@ -51,7 +53,7 @@

### Additional callable export inventory

- [Additional exports](../../../core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp) declare 165 remaining callable exports: 155 named and 10 known only by NID. Every entry throws. Parameterless `int` declarations are unknown-signature placeholders, not verified ABI contracts. Names/NIDs match the 13.60 export table. No functional implementation or title unblock is claimed.
- [Additional exports](../../../core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp) declare 164 remaining callable exports: 154 named and 10 known only by NID. Every entry throws. Parameterless `int` declarations are unknown-signature placeholders, not verified ABI contracts. Names/NIDs match the 13.60 export table. No functional implementation or title unblock is claimed.

| Name | NID |
| --- | --- |
Expand Down Expand Up @@ -196,7 +198,6 @@
| `ERR_reason_error_string` | `rJZ-aiNIYCA` |
| `X509_add1_ext_i2d` | `s8WMaKNjwvE` |
| `EC_GROUP_cmp` | `sENJFZ7WbLU` |
| `EVP_PKEY_bits` | `ssruelYxprY` |
| `SSL_do_handshake` | `tJsbVTdWjT4` |
| `EVP_PKEY_sign_init` | `tZG2VIefbX0` |
| `EC_POINT_dup` | `uITUdhDDAg8` |
Expand Down
Loading