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
13 changes: 12 additions & 1 deletion core/libs/prx/libSceLibreSsl3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_library(libSceLibreSsl3 SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Digest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/IntegerDecoder.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Key.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/EvpDigest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/X509PubkeyParam.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/ObjCmp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/X509Extensions.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Asn1.cpp)
add_library(libSceLibreSsl3 SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Digest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/IntegerDecoder.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Key.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/EvpDigest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/X509PubkeyParam.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/ObjCmp.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/X509Extensions.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Asn1.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/X509Validity.cpp)
target_include_directories(libSceLibreSsl3 PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(libSceLibreSsl3 PRIVATE libSceLibreSsl libc)
configure_windows_unwind(libSceLibreSsl3)
Expand Down Expand Up @@ -167,6 +167,17 @@ if(BUILD_TESTING)
set_tests_properties(guest_libressl3_x509_extensions PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl3>")
endif()

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

if(BUILD_TESTING AND CMAKE_SYSTEM_NAME STREQUAL "Linux" AND EXISTS "${LIBS_INCLUDE_DIR}/prx/libSceLibreSsl/src/Evp.cpp")
Expand Down
25 changes: 0 additions & 25 deletions core/libs/prx/libSceLibreSsl3/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,36 +271,11 @@ int APS5_VABI X509_STORE_up_ref_nid_postfix(void) {
return 0;
}

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

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

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

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

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

int APS5_VABI X509_getm_notBefore_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;
Expand Down
31 changes: 26 additions & 5 deletions core/libs/prx/libSceLibreSsl3/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ static_assert(sizeof(LibreSsl3X509Algor) == 16);
static_assert(sizeof(LibreSsl3X509Pubkey) == 24);
static_assert(offsetof(LibreSsl3X509Pubkey, publicKey) == 8);

struct LibreSsl3X509Validity {
void* notBefore;
void* notAfter;
};

struct LibreSsl3ValidityCertInfo {
void* version;
void* serialNumber;
void* signature;
void* issuer;
LibreSsl3X509Validity* validity;
unsigned char rest[64];
};

struct LibreSsl3ValidityX509 {
LibreSsl3ValidityCertInfo* certInfo;
const void* sigAlg;
const void* signature;
unsigned char rest[192];
};

extern "C" {

void APS5_VABI ASN1_INTEGER_free_nid_postfix(Asn1String* integer);
Expand Down Expand Up @@ -119,14 +140,14 @@ int APS5_VABI X509_STORE_CTX_init_nid_postfix(void);
int APS5_VABI X509_STORE_CTX_new_nid_postfix(void);
int APS5_VABI X509_STORE_up_ref_nid_postfix(void);
const LibreSsl3Stack* APS5_VABI X509_get0_extensions_nid_postfix(const LibreSsl3ExtensionsX509* certificate);
int APS5_VABI X509_get0_notAfter_nid_postfix(void);
int APS5_VABI X509_get0_notBefore_nid_postfix(void);
int APS5_VABI X509_get0_signature_nid_postfix(void);
const void* APS5_VABI X509_get0_notAfter_nid_postfix(const LibreSsl3ValidityX509* certificate);
const void* APS5_VABI X509_get0_notBefore_nid_postfix(const LibreSsl3ValidityX509* certificate);
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);
int APS5_VABI X509_getm_notAfter_nid_postfix(void);
int APS5_VABI X509_getm_notBefore_nid_postfix(void);
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);
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);
Expand Down
49 changes: 49 additions & 0 deletions core/libs/prx/libSceLibreSsl3/src/X509Validity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "prx/libSceLibreSsl3/include/Api.hpp"
#include <cstddef>
#include <stdexcept>
#include <string>
#include "prx/libc/include/General.hpp"

static_assert(sizeof(LibreSsl3X509Validity) == 16);
static_assert(sizeof(LibreSsl3ValidityCertInfo) == 104);
static_assert(offsetof(LibreSsl3ValidityCertInfo, validity) == 32);
static_assert(sizeof(LibreSsl3ValidityX509) == 216);
static_assert(offsetof(LibreSsl3ValidityX509, sigAlg) == 8);
static_assert(offsetof(LibreSsl3ValidityX509, signature) == 16);

namespace {

const LibreSsl3X509Validity* Validity(const LibreSsl3ValidityX509* certificate) {
if (!certificate || !certificate->certInfo) return nullptr;
return certificate->certInfo->validity;
}

}

extern "C" {

void* APS5_VABI X509_getm_notBefore_nid_postfix(const LibreSsl3ValidityX509* certificate) {
const LibreSsl3X509Validity* validity = Validity(certificate);
return validity ? validity->notBefore : nullptr;
}

void* APS5_VABI X509_getm_notAfter_nid_postfix(const LibreSsl3ValidityX509* certificate) {
const LibreSsl3X509Validity* validity = Validity(certificate);
return validity ? validity->notAfter : nullptr;
}

const void* APS5_VABI X509_get0_notBefore_nid_postfix(const LibreSsl3ValidityX509* certificate) {
return X509_getm_notBefore_nid_postfix(certificate);
}

const void* APS5_VABI X509_get0_notAfter_nid_postfix(const LibreSsl3ValidityX509* certificate) {
return X509_getm_notAfter_nid_postfix(certificate);
}

void APS5_VABI X509_get0_signature_nid_postfix(const void** signature, const void** algorithm, const LibreSsl3ValidityX509* certificate) {
if (!certificate && (signature || algorithm)) throw std::invalid_argument(std::string(__func__) + ": null certificate");
if (signature) *signature = certificate->signature;
if (algorithm) *algorithm = certificate->sigAlg;
}

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

static int Before;
static int After;

static void Require(bool condition, std::source_location location = std::source_location::current()) {
if (condition) return;
std::fprintf(stderr, "LibreSSL X509 validity 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 TestGetmNotBefore() {
LibreSsl3X509Validity validity {};
LibreSsl3ValidityCertInfo info {};
LibreSsl3ValidityX509 certificate {};
Require(X509_getm_notBefore_nid_postfix(nullptr) == nullptr);
Require(X509_getm_notBefore_nid_postfix(&certificate) == nullptr);
certificate.certInfo = &info;
Require(X509_getm_notBefore_nid_postfix(&certificate) == nullptr);
info.validity = &validity;
Require(X509_getm_notBefore_nid_postfix(&certificate) == nullptr);
validity.notBefore = &Before;
validity.notAfter = &After;
Require(X509_getm_notBefore_nid_postfix(&certificate) == &Before);
validity.notBefore = nullptr;
Require(X509_getm_notBefore_nid_postfix(&certificate) == nullptr);
}

static void TestGetmNotAfter() {
LibreSsl3X509Validity validity {};
LibreSsl3ValidityCertInfo info {};
LibreSsl3ValidityX509 certificate {};
Require(X509_getm_notAfter_nid_postfix(nullptr) == nullptr);
Require(X509_getm_notAfter_nid_postfix(&certificate) == nullptr);
certificate.certInfo = &info;
Require(X509_getm_notAfter_nid_postfix(&certificate) == nullptr);
info.validity = &validity;
Require(X509_getm_notAfter_nid_postfix(&certificate) == nullptr);
validity.notBefore = &Before;
validity.notAfter = &After;
Require(X509_getm_notAfter_nid_postfix(&certificate) == &After);
validity.notAfter = nullptr;
Require(X509_getm_notAfter_nid_postfix(&certificate) == nullptr);
}

static void TestGet0NotBefore() {
LibreSsl3X509Validity validity {};
LibreSsl3ValidityCertInfo info {};
LibreSsl3ValidityX509 certificate {};
Require(X509_get0_notBefore_nid_postfix(nullptr) == nullptr);
Require(X509_get0_notBefore_nid_postfix(&certificate) == nullptr);
certificate.certInfo = &info;
Require(X509_get0_notBefore_nid_postfix(&certificate) == nullptr);
info.validity = &validity;
Require(X509_get0_notBefore_nid_postfix(&certificate) == nullptr);
validity.notBefore = &Before;
validity.notAfter = &After;
Require(X509_get0_notBefore_nid_postfix(&certificate) == &Before);
validity.notBefore = nullptr;
Require(X509_get0_notBefore_nid_postfix(&certificate) == nullptr);
}

static void TestGet0NotAfter() {
LibreSsl3X509Validity validity {};
LibreSsl3ValidityCertInfo info {};
LibreSsl3ValidityX509 certificate {};
Require(X509_get0_notAfter_nid_postfix(nullptr) == nullptr);
Require(X509_get0_notAfter_nid_postfix(&certificate) == nullptr);
certificate.certInfo = &info;
Require(X509_get0_notAfter_nid_postfix(&certificate) == nullptr);
info.validity = &validity;
Require(X509_get0_notAfter_nid_postfix(&certificate) == nullptr);
validity.notBefore = &Before;
validity.notAfter = &After;
Require(X509_get0_notAfter_nid_postfix(&certificate) == &After);
validity.notAfter = nullptr;
Require(X509_get0_notAfter_nid_postfix(&certificate) == nullptr);
}

static void TestGet0Signature() {
int signatureBits = 0;
int algorithm = 0;
LibreSsl3ValidityX509 certificate {};
const void* signature = &Before;
const void* signatureAlgorithm = &Before;
X509_get0_signature_nid_postfix(&signature, &signatureAlgorithm, &certificate);
Require(signature == nullptr && signatureAlgorithm == nullptr);
certificate.signature = &signatureBits;
certificate.sigAlg = &algorithm;
X509_get0_signature_nid_postfix(&signature, &signatureAlgorithm, &certificate);
Require(signature == &signatureBits && signatureAlgorithm == &algorithm);
signature = signatureAlgorithm = &After;
X509_get0_signature_nid_postfix(nullptr, &signatureAlgorithm, &certificate);
Require(signature == &After && signatureAlgorithm == &algorithm);
X509_get0_signature_nid_postfix(&signature, nullptr, &certificate);
Require(signature == &signatureBits);
X509_get0_signature_nid_postfix(nullptr, nullptr, &certificate);
X509_get0_signature_nid_postfix(nullptr, nullptr, nullptr);
RequireThrows([&] { X509_get0_signature_nid_postfix(&signature, &signatureAlgorithm, nullptr); });
RequireThrows([&] { X509_get0_signature_nid_postfix(&signature, nullptr, nullptr); });
RequireThrows([&] { X509_get0_signature_nid_postfix(nullptr, &signatureAlgorithm, nullptr); });
}

int main() {
TestGetmNotBefore();
TestGetmNotAfter();
TestGet0NotBefore();
TestGet0NotAfter();
TestGet0Signature();
return 0;
}
3 changes: 2 additions & 1 deletion docs/dev/debt/libSceLibreSsl3.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

### Unknown function info

- [libSceLibreSsl3 pending exports](../../../core/libs/prx/libSceLibreSsl3/Export.cpp) - 61 named callable exports, excluding `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` and `EVP_PKEY_size`, 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) - 56 named callable exports, excluding `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` and `X509_get0_signature`, 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 13 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: 1101 characters

### Functional

Expand All @@ -31,6 +31,7 @@
- [sk_new, sk_push and sk_free](../../../core/libs/prx/libSceLibreSsl3/src/Stack.cpp) (libSceLibreSsl3) - follow [LibreSSL 4.0.0](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/stack/stack.c) `stack.c` with libSceLibreSsl's 32-byte `_STACK` layout, which `stack_local.h` keeps there. Stacks and element arrays come from the guest heap, so the forwarded `sk_pop_free` can release them. Nothing is checked on the console. Where LibreSSL faults or overflows, they throw: `sk_push` on a null stack, on a negative element count, on a null element array it does not regrow, or when the element array cannot double without overflowing `int`
- [ASN1_STRING_get0_data, ASN1_INTEGER_free](../../../core/libs/prx/libSceLibreSsl3/src/Asn1.cpp) (libSceLibreSsl3) - nothing is checked on the console. The 24-byte `ASN1_STRING` layout and the results follow [LibreSSL 4.0.0](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1) `asn1.h`, `a_string.c`, `a_int.c` and `tasn_fre.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 string yet, so only strings the caller builds reach them. `ASN1_INTEGER_free` zeroes and frees the data unless `ASN1_STRING_FLAG_NDEF` is set, then frees the string, through the guest `free` as the module would through libc. A null string in `ASN1_STRING_get0_data`, which LibreSSL dereferences, and a negative length with data in `ASN1_INTEGER_free`, which `freezero` would take as a huge size, throw
- [X509_get0_extensions, X509_get_ext_count, X509_get_ext](../../../core/libs/prx/libSceLibreSsl3/src/X509Extensions.cpp) (libSceLibreSsl3) - nothing is checked on the console. The `X509`, `X509_CINF` and stack layouts and the results follow [LibreSSL 4.0.0](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto) `x509_local.h`, `stack_local.h`, `x509_set.c`, `x509_ext.c`, `x509_v3.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 certificate yet, so only certificates the caller builds reach them. A null certificate or certificate info, and an extension stack with a positive count and a null entry array, which LibreSSL dereferences, throw
- [X509_getm_notBefore, X509_getm_notAfter, X509_get0_notBefore, X509_get0_notAfter, X509_get0_signature](../../../core/libs/prx/libSceLibreSsl3/src/X509Validity.cpp) (libSceLibreSsl3) - nothing is checked on the console. The `X509`, `X509_CINF` and `X509_VAL` layouts and the results follow [LibreSSL 4.0.0](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto) `x509_local.h`, `x509_set.c` and `x_x509.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 certificate yet, so only certificates the caller builds reach them. A null certificate in `X509_get0_signature` throws when an output is requested, where LibreSSL dereferences it; with both outputs null the call returns without reading the certificate, as in LibreSSL

- [ASN1_TIME_diff](../../../core/libs/prx/libSceLibreSsl3/src/Time.cpp) has no console ABI or behavior verification. Its signature and date rules follow LibreSSL 4.0.0 [a_time.c](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/a_time.c), [a_time_tm.c](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/a_time_tm.c) and [a_time_posix.c](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/a_time_posix.c), using the existing 24-byte string layout. Null inputs use the host wall clock; the console clock relationship is unverified. Null output pointers throw after valid input parsing, where the reference dereferences them. Closes when: measured on a PS5.

Expand Down
Loading