Skip to content
Closed
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
13 changes: 12 additions & 1 deletion core/libs/prx/libSceLibreSsl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_library(libSceLibreSsl SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Bignum.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Digest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Runtime.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Aes.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Error.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Ocsp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Stack.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Ui.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Rsa.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/X509StoreContext.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Pkey.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/EvpCipherCtx.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Evp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Ec.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/OcspResponse.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/EvpCipher.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/EvpAes.cpp)
add_library(libSceLibreSsl SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Bignum.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Digest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Runtime.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Aes.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Error.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Ocsp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Stack.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Ui.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Rsa.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/X509StoreContext.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Pkey.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/EvpCipherCtx.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Evp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Ec.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/EvpCipher.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/X509Name.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/OcspResponse.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/EvpAes.cpp)
target_include_directories(libSceLibreSsl PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(libSceLibreSsl PRIVATE mbedcrypto)
configure_windows_unwind(libSceLibreSsl)
Expand Down Expand Up @@ -228,6 +228,17 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
endif()

add_test_executable(guest_libressl_x509_name_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslX509Name.cpp)
target_include_directories(guest_libressl_x509_name_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libressl_x509_name_tests PRIVATE libSceLibreSsl)
configure_windows_unwind(guest_libressl_x509_name_tests)
add_test(NAME guest_libressl_x509_name COMMAND guest_libressl_x509_name_tests)
set_tests_properties(guest_libressl_x509_name PROPERTIES TIMEOUT 30)
if(WIN32)
set_tests_properties(guest_libressl_x509_name PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
endif()

add_test_executable(guest_libressl_ocsp_response_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslOcspResponse.cpp)
target_include_directories(guest_libressl_ocsp_response_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libressl_ocsp_response_tests PRIVATE libSceLibreSsl)
Expand Down
10 changes: 0 additions & 10 deletions core/libs/prx/libSceLibreSsl/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,16 +949,6 @@ int APS5_VABI X509_INFO_free_nid_postfix(void) {
return 0;
}

int APS5_VABI X509_NAME_ENTRY_get_data_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI X509_NAME_get_entry_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
}

int APS5_VABI X509_NAME_get_index_by_NID_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
Expand Down
19 changes: 17 additions & 2 deletions core/libs/prx/libSceLibreSsl/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,21 @@ struct LibreSslOcspSingleResponse {
void* singleExtensions;
};

struct LibreSslX509NameEntry {
void* object;
void* value;
int set;
int size;
};

struct LibreSslX509Name {
LibreSslStack* entries;
int modified;
void* bytes;
unsigned char* canonEnc;
int canonEnclen;
};

extern "C" {

std::int64_t APS5_VABI ASN1_INTEGER_get_nid_postfix(const Asn1String* value);
Expand Down Expand Up @@ -680,8 +695,8 @@ void* APS5_VABI X509_EXTENSION_get_data_nid_postfix(const void* extension);
void* APS5_VABI X509_EXTENSION_get_object_nid_postfix(const void* extension);
int APS5_VABI X509_INFO_free_nid_postfix(void);
LibreSslX509LookupMethod* APS5_VABI X509_LOOKUP_file_nid_postfix(void);
int APS5_VABI X509_NAME_ENTRY_get_data_nid_postfix(void);
int APS5_VABI X509_NAME_get_entry_nid_postfix(void);
void* APS5_VABI X509_NAME_ENTRY_get_data_nid_postfix(const LibreSslX509NameEntry* entry);
LibreSslX509NameEntry* APS5_VABI X509_NAME_get_entry_nid_postfix(const LibreSslX509Name* name, int loc);
int APS5_VABI X509_NAME_get_index_by_NID_nid_postfix(void);
int APS5_VABI X509_NAME_get_text_by_NID_nid_postfix(void);
int APS5_VABI X509_NAME_oneline_nid_postfix(void);
Expand Down
25 changes: 25 additions & 0 deletions core/libs/prx/libSceLibreSsl/src/X509Name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "prx/libSceLibreSsl/include/Api.hpp"
#include <cstddef>
#include <stdexcept>
#include <string>
#include "prx/libc/include/General.hpp"

static_assert(sizeof(LibreSslX509NameEntry) == 24);
static_assert(offsetof(LibreSslX509NameEntry, value) == 8);
static_assert(sizeof(LibreSslX509Name) == 40);
static_assert(offsetof(LibreSslX509Name, entries) == 0);

extern "C" {

LibreSslX509NameEntry* APS5_VABI X509_NAME_get_entry_nid_postfix(const LibreSslX509Name* name, int loc) {
if (!name || loc < 0 || !name->entries || name->entries->num <= loc) return nullptr;
if (!name->entries->data) throw std::invalid_argument(std::string(__func__) + ": null entry array");
return reinterpret_cast<LibreSslX509NameEntry*>(name->entries->data[loc]);
}

void* APS5_VABI X509_NAME_ENTRY_get_data_nid_postfix(const LibreSslX509NameEntry* entry) {
if (!entry) return nullptr;
return entry->value;
}

}
97 changes: 97 additions & 0 deletions core/libs/tests/GuestLibreSslX509Name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "prx/libSceLibreSsl/include/Api.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <climits>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <source_location>
#include <stdexcept>

struct TestStack {
int num;
char** data;
int sorted;
int numAlloc;
void* comp;
};

struct TestNameEntry {
void* object;
void* value;
int set;
int size;
};

struct TestName {
TestStack* entries;
int modified;
void* bytes;
unsigned char* canonEnc;
int canonEnclen;
};

static_assert(sizeof(TestStack) == 32 && offsetof(TestStack, data) == 8);
static_assert(sizeof(TestNameEntry) == 24 && offsetof(TestNameEntry, value) == 8);
static_assert(sizeof(TestName) == 40);

static const LibreSslX509Name* Name(const TestName* name) {
return reinterpret_cast<const LibreSslX509Name*>(name);
}

static const LibreSslX509NameEntry* Entry(const TestNameEntry* entry) {
return reinterpret_cast<const LibreSslX509NameEntry*>(entry);
}

static void Require(bool condition, std::source_location location = std::source_location::current()) {
if (condition) return;
std::fprintf(stderr, "LibreSSL X509_NAME check failed at line %u\n", location.line());
std::abort();
}

template<typename TAction> static void RequireThrows(TAction action, std::source_location location = std::source_location::current()) {
try { action(); } catch (const std::exception&) { return; }
Require(false, location);
}

static void TestGetEntry() {
TestNameEntry first {};
TestNameEntry second {};
char* slots[] {reinterpret_cast<char*>(&first), reinterpret_cast<char*>(&second), nullptr};
TestStack entries {2, slots, 0, 3, nullptr};
TestName name {};
Require(X509_NAME_get_entry_nid_postfix(nullptr, 0) == nullptr);
Require(X509_NAME_get_entry_nid_postfix(Name(&name), 0) == nullptr);
Require(X509_NAME_get_entry_nid_postfix(Name(&name), -1) == nullptr);
name.entries = &entries;
Require(X509_NAME_get_entry_nid_postfix(Name(&name), 0) == Entry(&first));
Require(X509_NAME_get_entry_nid_postfix(Name(&name), 1) == Entry(&second));
Require(X509_NAME_get_entry_nid_postfix(Name(&name), 2) == nullptr);
Require(X509_NAME_get_entry_nid_postfix(Name(&name), -1) == nullptr);
Require(X509_NAME_get_entry_nid_postfix(Name(&name), INT_MIN) == nullptr);
Require(X509_NAME_get_entry_nid_postfix(Name(&name), INT_MAX) == nullptr);
entries.num = 0;
Require(X509_NAME_get_entry_nid_postfix(Name(&name), 0) == nullptr);
entries.num = 3;
Require(X509_NAME_get_entry_nid_postfix(Name(&name), 2) == nullptr);
entries.data = nullptr;
RequireThrows([&] { X509_NAME_get_entry_nid_postfix(Name(&name), 0); });
Require(X509_NAME_get_entry_nid_postfix(Name(&name), 3) == nullptr);
}

static void TestEntryGetData() {
int value = 0;
TestNameEntry entry {};
Require(X509_NAME_ENTRY_get_data_nid_postfix(nullptr) == nullptr);
Require(X509_NAME_ENTRY_get_data_nid_postfix(Entry(&entry)) == nullptr);
entry.value = &value;
Require(X509_NAME_ENTRY_get_data_nid_postfix(Entry(&entry)) == &value);
entry.object = &entry;
entry.set = 7;
Require(X509_NAME_ENTRY_get_data_nid_postfix(Entry(&entry)) == &value);
}

int main() {
TestGetEntry();
TestEntryGetData();
return 0;
}
3 changes: 2 additions & 1 deletion docs/dev/debt/libSceLibreSsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

### Unknown function info

- [libSceLibreSsl pending exports](../../../core/libs/prx/libSceLibreSsl/Export.cpp) - 195 named callable exports are listed in the [PS5 3.20 export table](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceLibreSsl.c) and were 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`.
- [libSceLibreSsl pending exports](../../../core/libs/prx/libSceLibreSsl/Export.cpp) - 193 named callable exports are listed in the [PS5 3.20 export table](https://github.com/DNNDHH/PS5-3.20_Libs/blob/b2a77c09cd71c01f34c790cfbe98c751a13262ad/libSceLibreSsl.c) and were 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`.
- [MD4, MD5, SHA1, SHA224, SHA256, SHA384 and SHA512 Init/Update/Final and SHA256](../../../core/libs/prx/libSceLibreSsl/src/Digest.cpp) (libSceLibreSsl) - signatures and context layouts follow the [LibreSSL headers](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/sha/sha.h), and buffering and `Final` follow its [sources](https://github.com/libressl/openbsd/tree/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/sha); nothing is checked on the console. Null contexts, null input with a nonzero length, null digest buffers in the 32-bit `Final` functions and a buffered byte count that is not below the block size, which LibreSSL does not check, throw. SHA-384 and SHA-512 initialization retains the block buffer, following [LibreSSL 2.5.4 `sha512.c`](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/sha/sha512.c)
- [DES_set_key, DES_ecb_encrypt, DES_set_odd_parity](../../../core/libs/prx/libSceLibreSsl/Export.cpp) (libSceLibreSsl) - the 128-byte `DES_key_schedule` holds the 8-byte key followed by zeros instead of LibreSSL's precomputed subkeys, so a title reading its contents sees different bytes; `DES_ecb_encrypt` derives the subkeys through mbedtls on each call. Not compared with the console. Results follow LibreSSL [des_key.c](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/des/des_key.c) and [des.c](https://github.com/libressl/openbsd/blob/bc9483725c5815efb64b6d9acc52fc5fba139212/src/lib/libcrypto/des/des.c): `DES_set_key` skips the parity and weak-key checks and returns 0, as it does while `DES_check_key` is 0 (that variable is not in the PS5 export inventory), a nonzero `enc` encrypts, and `DES_set_odd_parity` sets each byte's low bit for odd parity. Null pointers throw where LibreSSL faults
- [X509_verify_cert_error_string](../../../core/libs/prx/libSceLibreSsl/Export.cpp) - no console call is verified. The 64-bit argument, low-32-bit lookup, messages and shared numeric fallback follow [LibreSSL 2.5.4](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/x509/x509_txt.c), the version identified in [published firmware 13.40 measurements](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214). Console modifications and other firmware versions remain unverified. Unknown-code results share an overwritten static buffer and are not safe for concurrent use, as in that release; code 1 and codes above 54 use the numeric fallback.
Expand All @@ -32,6 +32,7 @@
- [EVP_PKEY_type](../../../core/libs/prx/libSceLibreSsl/src/Pkey.cpp) (libSceLibreSsl) - follows the method table of [LibreSSL 2.5.4](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/asn1/ameth_lib.c#L83-L111) `ameth_lib.c` built without GOST and with no engine (every `ENGINE_` export throws): RSA (6) and its alias 19 give 6, the DSA types 66, 67, 70, 113 and 116 give 116, DH (28), EC (408), HMAC (855) and CMAC (894) give themselves, and every other type, the GOST types 811, 815, 946 and 947 included, gives 0. These results match [firmware 13.42's module run natively on a PC](https://github.com/boykopovar/AnyPS5/pull/10398#issuecomment-6406072913) for every type from -1000 to 5000 and the `int` extremes; nothing is checked on the console
- [EVP_CIPHER_CTX_init, EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_set_padding](../../../core/libs/prx/libSceLibreSsl/src/EvpCipherCtx.cpp) (libSceLibreSsl) - nothing is checked on the console. The 168-byte `EVP_CIPHER_CTX` layout and the results follow [LibreSSL 2.5.4](https://github.com/libressl/openbsd/tree/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/evp) `evp.h` and `evp_enc.c`, the version in the [firmware 13.40 measurements](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214), which also measured `EVP_CIPHER_CTX_init` clearing 168 bytes. `EVP_CIPHER_CTX_new` allocates through the guest `calloc` as the module would through libc. A null context, which LibreSSL dereferences, throws
- [EVP_CIPHER_block_size](../../../core/libs/prx/libSceLibreSsl/src/EvpCipher.cpp) (libSceLibreSsl) - nothing is checked on the console. The 88-byte `EVP_CIPHER` layout and the result follow [LibreSSL 2.5.4](https://github.com/libressl/openbsd/tree/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/evp) `evp.h` and `evp_lib.c`, the version in the [firmware 13.40 measurements](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214): `block_size` is returned as stored. A null cipher, which LibreSSL dereferences, throws
- [X509_NAME_get_entry, X509_NAME_ENTRY_get_data](../../../core/libs/prx/libSceLibreSsl/src/X509Name.cpp) (libSceLibreSsl) - nothing is checked on the console. Layouts and results follow [LibreSSL 2.5.4](https://github.com/libressl/openbsd/tree/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto) `x509.h`, `x509name.c` and `stack.c`, the version in the [firmware 13.40 measurements](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214). No export of this module creates a name yet, so only names and entries the caller builds reach them. A positive entry count with a null entry array, which LibreSSL dereferences, throws
- [EVP_CIPHER_CTX_cleanup, EVP_CIPHER_CTX_free, EVP_CIPHER_CTX_ctrl](../../../core/libs/prx/libSceLibreSsl/src/EvpCipherCtx.cpp) (libSceLibreSsl) - nothing is checked on the console. A failure returns 0 without queueing an error code. A context holding an `ENGINE` throws in `EVP_CIPHER_CTX_cleanup` instead of releasing it, because no `ENGINE` function is implemented. Results follow [LibreSSL 2.5.4's evp_enc.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/evp/evp_enc.c) with the 88-byte `EVP_CIPHER` of its `evp.h`: the cipher's `cleanup` and `ctrl` are called through the descriptor, a `ctrl` result of -1 becomes 0, and the cipher data and the context are cleared and freed through the guest `free`. A null context and a negative cipher context size, which LibreSSL dereferences or passes to `explicit_bzero`, throw
- [EVP_CipherInit_ex, EVP_EncryptInit_ex, EVP_DecryptInit_ex, EVP_CipherUpdate, EVP_EncryptUpdate, EVP_DecryptUpdate, EVP_CipherFinal_ex, EVP_EncryptFinal_ex, EVP_DecryptFinal_ex](../../../core/libs/prx/libSceLibreSsl/src/EvpCipherCtx.cpp) (libSceLibreSsl) - nothing is checked on the console, and failures queue no error code. A non-null `ENGINE` argument throws, because no `ENGINE` function is implemented; with none registered, the context's `ENGINE` is null as in LibreSSL. Buffering, padding, IV setup and the custom-cipher path follow [LibreSSL 2.5.4's evp_enc.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/evp/evp_enc.c), and the cipher data comes from the guest `malloc`. Inputs LibreSSL dereferences or overruns throw: null contexts, lengths and buffers, a cipher without `init` or `do_cipher`, a CTR IV longer than 16 bytes, and a context whose buffered byte count or block mask no longer matches its cipher's block size
- [EVP_aes_128_cfb8, EVP_aes_192_cfb8, EVP_aes_256_cfb8, EVP_aes_128_ctr, EVP_aes_192_ctr, EVP_aes_256_ctr](../../../core/libs/prx/libSceLibreSsl/src/EvpAes.cpp) (libSceLibreSsl) - nothing is checked on the console. The descriptors hold the values of [LibreSSL 2.5.4's e_aes.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/evp/e_aes.c) (NID, block size 1, 16-byte IV, `EVP_CIPH_FLAG_FIPS` with the mode and the 264-byte `ctx_size` of its LP64 `EVP_AES_KEY`), but `init` and `do_cipher` are host functions, not the module's. The cipher data holds only the `AES_KEY` schedule, laid out as the AES entry describes; the block and stream function pointers LibreSSL keeps after it are not stored. CFB8 follows `CRYPTO_cfb128_8_encrypt` and CTR follows `CRYPTO_ctr128_encrypt` with a 128-bit big-endian counter, checked against the SP 800-38A vectors. A context without cipher data, null buffers and a CTR position outside 0-15 throw
Expand Down
Loading