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
11 changes: 11 additions & 0 deletions core/libs/prx/libSceLibreSsl3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/PkeyGette
target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/LibTls.cpp)
target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/Time.cpp)
target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/DigestNames.cpp)
target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/BioLifetime.cpp)

if(BUILD_TESTING)
add_test_executable(guest_libressl3_libtls_settings_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/LibTlsSettings.cpp)
Expand Down Expand Up @@ -203,6 +204,16 @@ if(BUILD_TESTING)
configure_windows_unwind(guest_libressl3_version_tests)
add_test(NAME guest_libressl3_version COMMAND guest_libressl3_version_tests)
set_tests_properties(guest_libressl3_version PROPERTIES TIMEOUT 30)
add_test_executable(guest_libressl3_bio_lifetime_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/BioLifetime.cpp)
target_include_directories(guest_libressl3_bio_lifetime_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libressl3_bio_lifetime_tests PRIVATE libSceLibreSsl3 libSceLibreSsl libc)
configure_windows_unwind(guest_libressl3_bio_lifetime_tests)
add_test(NAME guest_libressl3_bio_lifetime COMMAND guest_libressl3_bio_lifetime_tests)
set_tests_properties(guest_libressl3_bio_lifetime PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_libressl3_bio_lifetime PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl3>")
endif()
if(WIN32)
set_tests_properties(guest_crypto_memcmp guest_libressl3_digest guest_libressl3_asn1 guest_libressl3_key_reference guest_libressl3_evp guest_libressl3_version guest_libressl3_x509_pubkey_param PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl3>")
Expand Down
5 changes: 0 additions & 5 deletions core/libs/prx/libSceLibreSsl3/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
extern "C" {

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

int APS5_VABI CRYPTO_memcmp_nid_postfix(const void* first, const void* second, std::size_t length) {
const auto* firstBytes = static_cast<const volatile unsigned char*>(first);
const auto* secondBytes = static_cast<const volatile unsigned char*>(second);
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 @@ -119,7 +119,7 @@ void APS5_VABI ASN1_INTEGER_free_nid_postfix(Asn1String* integer);
void APS5_VABI ASN1_OBJECT_free_nid_postfix(Asn1Object_nid_no_patch* object);
const unsigned char* APS5_VABI ASN1_STRING_get0_data_nid_postfix(const Asn1String* string);
int APS5_VABI BIO_up_ref_nid_postfix(LibreSsl3Bio* bio);
int APS5_VABI BIO_vfree_nid_postfix(void);
void APS5_VABI BIO_vfree_nid_postfix(LibreSsl::Bio* bio);
int APS5_VABI CRYPTO_memcmp_nid_postfix(const void* first, const void* second, std::size_t length);
void APS5_VABI DH_get0_key_nid_postfix(const LibreSsl3Dh* dh, const void** pubKey, const void** privKey);
void APS5_VABI DH_get0_pqg_nid_postfix(const LibreSsl3Dh* dh, const void** p, const void** q, const void** g);
Expand Down
69 changes: 69 additions & 0 deletions core/libs/prx/libSceLibreSsl3/src/BioLifetime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Derived from bio/bio_lib.c@ce488b78e0bd994780569df718596b9776ff681a */
/* $OpenBSD: bio_lib.c,v 1.54 2024/07/09 06:14:59 beck Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/

#include "prx/libSceLibreSsl3/include/Api.hpp"
#include "prx/libSceLibreSsl/include/Api.hpp"

extern "C" {

void APS5_VABI BIO_vfree_nid_postfix(LibreSsl::Bio* bio) {
BIO_free_nid_postfix(bio);
}

}
215 changes: 215 additions & 0 deletions core/libs/prx/libSceLibreSsl3/tests/BioLifetime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp"
#include "prx/libSceLibreSsl/libSceLibreSsl.hpp"
#include "prx/libc/libc.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include <array>
#include <atomic>
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <source_location>
#include <stdexcept>
#include <unordered_map>

namespace {

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

std::unordered_map<void*, std::size_t> allocations;
std::size_t freeCalls = 0;
std::size_t callocCalls = 0;

void* APS5_VABI Allocate_nid_no_patch(std::size_t size) {
void* result = std::malloc(size);
Require(result != nullptr);
allocations.emplace(result, size);
return result;
}

void APS5_VABI Free_nid_no_patch(void* pointer) {
if (!pointer) return;
Require(allocations.erase(pointer) == 1);
++freeCalls;
std::free(pointer);
}

void* APS5_VABI Calloc_nid_no_patch(std::size_t count, std::size_t size) {
void* result = std::calloc(count, size);
if (!result) return nullptr;
allocations.emplace(result, count * size);
++callocCalls;
return result;
}

void* APS5_VABI Reallocate_nid_no_patch(void* pointer, std::size_t size) {
if (pointer) Require(allocations.erase(pointer) == 1);
void* result = std::realloc(pointer, size);
Require(result != nullptr);
allocations.emplace(result, size);
return result;
}

void* APS5_VABI UnexpectedAlign_nid_no_patch(std::size_t, std::size_t) { throw std::runtime_error("unexpected aligned allocation"); }
void* APS5_VABI UnexpectedRealign_nid_no_patch(void*, std::size_t, std::size_t) { throw std::runtime_error("unexpected aligned reallocation"); }
int APS5_VABI UnexpectedPosixAlign_nid_no_patch(void**, std::size_t, std::size_t) { throw std::runtime_error("unexpected posix_memalign"); }

struct CustomState {
int createCalls = 0;
int destroyCalls = 0;
int callbackCalls = 0;
int lastOperation = 0;
int callbackVeto = 0;
};

int APS5_VABI CustomCreate_nid_no_patch(LibreSsl::Bio* bio) {
if (!bio) return 0;
bio->initialized = 1;
bio->shutdown = 1;
auto* state = static_cast<CustomState*>(bio->data);
if (state) ++state->createCalls;
return 1;
}

int APS5_VABI CustomDestroy_nid_no_patch(LibreSsl::Bio* bio) {
if (!bio) return 0;
auto* state = static_cast<CustomState*>(bio->data);
if (state) ++state->destroyCalls;
return 1;
}

std::int64_t APS5_VABI CustomCallback_nid_no_patch(LibreSsl::Bio* bio, int operation, const char* argp, int argi, std::int64_t argl, std::int64_t result) {
(void)argp;
(void)argi;
(void)argl;
auto* state = static_cast<CustomState*>(bio->data);
if (state) {
++state->callbackCalls;
state->lastOperation = operation;
if (state->callbackVeto) return -7;
}
return result;
}

LibreSsl::BioMethod customMethod{
0x480,
"test_custom",
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
CustomCreate_nid_no_patch,
CustomDestroy_nid_no_patch,
nullptr
};

void TestNull() {
errno = 123;
BIO_vfree_nid_postfix(nullptr);
Require(errno == 123);
}

void TestMemoryBio() {
errno = 456;
auto* bio = BIO_new_nid_postfix(BIO_s_mem_nid_postfix());
Require(bio != nullptr);
Require(bio->references == 1);
Require(allocations.size() == 2);
BIO_vfree_nid_postfix(bio);
Require(errno == 456);
Require(allocations.empty());
}

void TestReferenceCount() {
CustomState state{};
auto* bio = BIO_new_nid_postfix(&customMethod);
Require(bio != nullptr);
bio->data = &state;
bio->callback = CustomCallback_nid_no_patch;
std::atomic_ref(bio->references).store(3);

BIO_vfree_nid_postfix(bio);
Require(bio->references == 2);
Require(state.callbackCalls == 0);
Require(state.destroyCalls == 0);

BIO_vfree_nid_postfix(bio);
Require(bio->references == 1);
Require(state.callbackCalls == 0);
Require(state.destroyCalls == 0);

BIO_vfree_nid_postfix(bio);
Require(state.callbackCalls == 1);
Require(state.lastOperation == 1);
Require(state.destroyCalls == 1);
Require(allocations.empty());
}

void TestCallbackVeto() {
CustomState state{};
state.callbackVeto = 1;
auto* bio = BIO_new_nid_postfix(&customMethod);
Require(bio != nullptr);
bio->data = &state;
bio->callback = CustomCallback_nid_no_patch;

BIO_vfree_nid_postfix(bio);
Require(state.callbackCalls == 1);
Require(state.lastOperation == 1);
Require(state.destroyCalls == 0);
Require(bio->references == 0);
Require(!allocations.empty());

state.callbackVeto = 0;
bio->callback = nullptr;
bio->references = 1;
BIO_vfree_nid_postfix(bio);
Require(state.destroyCalls == 1);
Require(allocations.empty());
}

void TestChainedOwnership() {
CustomState state1{};
CustomState state2{};
auto* bio1 = BIO_new_nid_postfix(&customMethod);
auto* bio2 = BIO_new_nid_postfix(&customMethod);
Require(bio1 != nullptr && bio2 != nullptr);
bio1->data = &state1;
bio2->data = &state2;
bio1->next = bio2;
bio2->previous = bio1;

BIO_vfree_nid_postfix(bio1);
Require(state1.destroyCalls == 1);
Require(state2.destroyCalls == 0);
Require(bio2->references == 1);

BIO_vfree_nid_postfix(bio2);
Require(state2.destroyCalls == 1);
Require(allocations.empty());
}

} // namespace

int main() {
const std::array<void*, 10> api{
reinterpret_cast<void*>(Allocate_nid_no_patch), reinterpret_cast<void*>(Free_nid_no_patch), reinterpret_cast<void*>(Calloc_nid_no_patch),
reinterpret_cast<void*>(Reallocate_nid_no_patch), reinterpret_cast<void*>(UnexpectedAlign_nid_no_patch), reinterpret_cast<void*>(UnexpectedRealign_nid_no_patch),
reinterpret_cast<void*>(UnexpectedPosixAlign_nid_no_patch)};
ApplicationHeapRegister_nid_no_patch(api.data());

TestNull();
TestMemoryBio();
TestReferenceCount();
TestCallbackVeto();
TestChainedOwnership();

std::puts("LibreSSL3 BIO_vfree tests passed");
}
4 changes: 3 additions & 1 deletion docs/dev/debt/libSceLibreSsl3.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- [libtls CA memory and verification settings](../../../core/libs/prx/libSceLibreSsl3/src/LibTls.cpp) - Four additional exports copy owned CA bytes, assign a protocol bitmask, and set client-certificate and hostname-verification flags. They follow [OpenBSD libtls `tls_config.c`](https://github.com/openbsd/src/blob/master/lib/libtls/tls_config.c). TLS transport, certificate parsing and cipher-list validation remain unimplemented; `tls_config_set_cert_mem` and `tls_config_set_ciphers` still throw; `tls_config_parse_protocols` is implemented in `TlsProtocols.cpp`. Native PS5 ABI and option interoperability are unverified.

- [BIO_vfree](../../../core/libs/prx/libSceLibreSsl3/src/BioLifetime.cpp) (libSceLibreSsl3) - console ABI and BIO layout are unmeasured; the wrapper uses existing parent libSceLibreSsl BIO semantics and inherits known parent limitations. Source is adapted from pinned LibreSSL (ce488b78e0bd994780569df718596b9776ff681a) `bio/bio_lib.c` and delegates directly to `BIO_free`. Closes when: compared with console execution.

- [OpenSSL_version](../../../core/libs/prx/libSceLibreSsl3/Export.cpp) - other firmware revisions are unverified. Selectors 0 through 5 follow the public firmware 13.40 measurements in [discussion #8981](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214); unknown selectors follow LibreSSL 4.0.0 and return `not available`. No independent console test was run.

- [ERR_load_crypto_strings and ERR_peek_last_error](../../../core/libs/prx/libSceLibreSsl3/Export.cpp) have no console execution verification. [Firmware 13.42 modules run on a PC](https://github.com/boykopovar/AnyPS5/pull/9535#issuecomment-6407060914) retained numeric-only output after loaders and matched queue order/overflow; whether the two firmware modules share a queue was not tested. This implementation reads libSceLibreSsl's calling-thread queue. `ERR_print_errors_cb` remains throwing because guest thread identifiers and diagnostic metadata are unverified.
Expand All @@ -16,7 +18,7 @@

- [EVP_PKEY_new, EVP_PKEY_free, EVP_PKEY_set1_RSA, EVP_PKEY_copy_parameters](../../../core/libs/prx/libSceLibreSsl3/src/PkeyLifecycle.cpp) - not checked on a console. Native allocation and release follow the 32-byte [LibreSSL 4.0.0 layout and p_lib.c](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/evp). Allocation failure returns null without its allocation error. Final release of a nonnull method or payload throws and restores the reference instead of calling the method; no native payload destruction is implemented. Empty objects are cleared before release. set1_RSA and copy_parameters throw locally instead of forwarding to the incompatible 56-byte legacy layout. The native RSA producers and get1_RSA remain local throwing stubs. Objects from the two modules must not be mixed.
- [Native PKEY forwarding gaps](../../../core/libs/prx/libSceLibreSsl3/ParentExports.cpp) - `X509_get_pubkey` and `PEM_read_bio_PrivateKey` still forward to legacy throwing stubs. `SSL_get_privatekey` forwards to the parent library and returns its legacy PKEY. The consumers `EVP_PKEY_get1_EC_KEY`, `EVP_DigestVerifyInit`, `SSL_CTX_use_PrivateKey`, `SSL_CTX_use_PrivateKey_ASN1`, `SSL_CTX_use_PrivateKey_file` and `SSL_use_PrivateKey_ASN1` do too. Each needs a native wrapper before its legacy implementation can run, to prevent mixing the incompatible PKEY layouts.
- [libSceLibreSsl3 pending exports](../../../core/libs/prx/libSceLibreSsl3/Export.cpp) - 45 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`, `DSA_get0_key` and `BIO_up_ref`, 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) - 44 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`, `DSA_get0_key`, `BIO_up_ref` and `BIO_vfree`, 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`.

Check warning on line 21 in docs/dev/debt/libSceLibreSsl3.md

View workflow job for this annotation

GitHub Actions / conventions

debt-length

Technical debt entry over 1000 characters: state the gap first (what differs from the console, what is unverified, what throws) and keep only the behaviour it needs: 1406 characters

Check warning on line 21 in docs/dev/debt/libSceLibreSsl3.md

View workflow job for this annotation

GitHub Actions / conventions

debt-length

Technical debt entry over 1000 characters: state the gap first (what differs from the console, what is unverified, what throws) and keep only the behaviour it needs: 1406 characters

### Functional

Expand Down
Loading