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
11 changes: 11 additions & 0 deletions core/libs/prx/libSceLibreSsl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl>")
endif()

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

add_test_executable(guest_libressl_rsa_pss_tests ${LIBS_INCLUDE_DIR}/tests/GuestLibreSslRsaPss.cpp)
target_include_directories(guest_libressl_rsa_pss_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libressl_rsa_pss_tests PRIVATE libSceLibreSsl libc)
Expand Down
16 changes: 13 additions & 3 deletions core/libs/prx/libSceLibreSsl/src/Rsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,17 +1124,27 @@ int APS5_VABI RSA_sign_nid_postfix(int type, const unsigned char* digest, unsign
std::uint64_t contentLength = 0;
std::uint64_t length = Md5Sha1Bytes;
if (type == DigestMd5Sha1) {
if (digestLength != Md5Sha1Bytes) return 0;
if (digestLength != Md5Sha1Bytes) {
LibreSslPutError_nid_no_patch(4, 0xfff, 131);
return 0;
}
} else {
if (type <= 0) return 0;
if (type <= 0) {
if (type < 0) LibreSslAddedObjectName_nid_no_patch(type, false);
LibreSslPutError_nid_no_patch(4, 0xfff, type < 0 ? 117 : 116);
return 0;
}
object = &DigestObjectFor(type, __func__);
if (digestLength > INT_MAX) NotImplemented_nid_no_patch((std::string(__func__) + ": a digest length over INT_MAX").c_str());
algorithmLength = 2 + static_cast<std::uint64_t>(object->length) + 2;
contentLength = 1 + DerLengthBytes(algorithmLength) + algorithmLength + 1 + DerLengthBytes(digestLength) + digestLength;
length = 1 + DerLengthBytes(contentLength) + contentLength;
if (length > INT_MAX) NotImplemented_nid_no_patch((std::string(__func__) + ": a DigestInfo over INT_MAX bytes").c_str());
}
if (static_cast<std::int64_t>(length) > std::int64_t(RSA_size_nid_postfix(rsa)) - 11) return 0;
if (static_cast<std::int64_t>(length) > std::int64_t(RSA_size_nid_postfix(rsa)) - 11) {
LibreSslPutError_nid_no_patch(4, 0xfff, 112);
return 0;
}
std::vector<unsigned char> encoded;
const unsigned char* data = digest;
if (object) {
Expand Down
199 changes: 199 additions & 0 deletions core/libs/tests/GuestLibreSslRsaSignErrors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#include "prx/libSceLibreSsl/include/Api.hpp"
#include "prx/libSceLibreSsl/include/Error.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <source_location>
#include <stdexcept>

namespace {

struct TestBignum {
std::uint64_t* d;
int top;
int dmax;
int neg;
int flags;
};

constexpr unsigned int UntouchedLength = 0xabcdef;
constexpr std::uint64_t Sentinel = (8u << 24) | (0xfffu << 12) | 123u;
int CallbackResult = 0;
int CallbackReason = 0;
int CallbackCalls = 0;

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

std::uint64_t RsaError(int reason) {
return (4u << 24) | (0xfffu << 12) | static_cast<unsigned int>(reason);
}

void SeedError() {
Require(ERR_get_error_nid_postfix() == 0);
LibreSslPutError_nid_no_patch(8, 0xfff, 123);
}

void CheckErrors(int reason, bool objectError = false) {
Require(ERR_get_error_nid_postfix() == Sentinel);
if (objectError) Require(ERR_get_error_nid_postfix() == ((8u << 24) | (0xfffu << 12) | 101u));
const auto actual = ERR_get_error_nid_postfix();
if (actual != (reason ? RsaError(reason) : 0))
std::fprintf(stderr, "RSA_sign error 0x%llx expected RSA/fff/%d\n", static_cast<unsigned long long>(actual), reason);
Require(actual == (reason ? RsaError(reason) : 0));
Require(ERR_get_error_nid_postfix() == 0);
}

int APS5_VABI PrivateEncrypt(int length, const unsigned char* from, unsigned char* to, LibreSslRsa*, int padding) {
++CallbackCalls;
Require(padding == 1);
Require(length > 0 && from != nullptr);
if (length == 51) {
const unsigned char header[] {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
Require(std::memcmp(from, header, sizeof(header)) == 0);
for (int index = sizeof(header); index < length; ++index) Require(from[index] == 0);
} else {
Require(length == 36);
for (int index = 0; index < length; ++index) Require(from[index] == 0);
}
if (CallbackReason) LibreSslPutError_nid_no_patch(4, 0xfff, CallbackReason);
to[0] = 0x42;
return CallbackResult;
}

int APS5_VABI Sign(int type, const unsigned char*, unsigned int length, unsigned char* to, unsigned int* written, const LibreSslRsa*) {
++CallbackCalls;
Require(type == -1 && length == 1);
if (CallbackReason) LibreSslPutError_nid_no_patch(4, 0xfff, CallbackReason);
to[0] = 0x42;
*written = 7;
return CallbackResult;
}

void TestEarlyFailures(LibreSslRsa& rsa, int reason = 0) {
const struct { int type; unsigned int length; int words; int reason; } cases[] {
{114, 0, 8, 131}, {114, 35, 8, 131}, {114, 37, 8, 131},
{-1, 32, 8, 117}, {-100, 32, 8, 117}, {0, 32, 8, 116},
{672, 32, 7, 112}, {674, 64, 8, 112}, {114, 36, 5, 112},
};
for (const auto& scenario : cases) {
if (reason && reason != scenario.reason) continue;
static_cast<TestBignum*>(rsa.n)->top = scenario.words;
std::array<unsigned char, 128> signature;
signature.fill(0xa5);
std::array<unsigned char, 64> digest {};
unsigned int written = UntouchedLength;
CallbackCalls = 0;
SeedError();
Require(RSA_sign_nid_postfix(scenario.type, digest.data(), scenario.length, signature.data(), &written, &rsa) == 0);
Require(written == UntouchedLength);
Require(std::all_of(signature.begin(), signature.end(), [](auto byte) { return byte == 0xa5; }));
Require(CallbackCalls == 0);
CheckErrors(scenario.reason);
}
static_cast<TestBignum*>(rsa.n)->top = 8;
}

void TestObjectTable(LibreSslRsa& rsa) {
Require(OBJ_create_nid_postfix("1.3.6.1.4.1.55555.12", "rsaSignTest", "RSA sign test") >= 956);
for (int type : {-1, 0}) {
unsigned char signature[128];
std::memset(signature, 0xa5, sizeof(signature));
unsigned int written = UntouchedLength;
SeedError();
Require(RSA_sign_nid_postfix(type, nullptr, 0, signature, &written, &rsa) == 0);
Require(written == UntouchedLength);
for (auto byte : signature) Require(byte == 0xa5);
CheckErrors(type < 0 ? 117 : 116, type < 0);
}
OBJ_cleanup_nid_postfix();
}

void TestPrivateEncrypt(LibreSslRsa& rsa) {
for (int type : {114, 672}) {
for (int result : {-1, 0, 64}) {
for (int reason : {0, 104}) {
std::array<unsigned char, 128> signature;
signature.fill(0xa5);
std::array<unsigned char, 36> digest {};
unsigned int written = UntouchedLength;
CallbackResult = result;
CallbackReason = reason;
CallbackCalls = 0;
SeedError();
Require(RSA_sign_nid_postfix(type, digest.data(), type == 114 ? 36 : 32, signature.data(), &written, &rsa) == (result > 0));
Require(written == (result > 0 ? 64 : UntouchedLength));
Require(signature[0] == 0x42);
Require(std::all_of(signature.begin() + 1, signature.end(), [](auto byte) { return byte == 0xa5; }));
Require(CallbackCalls == 1);
CheckErrors(reason);
}
}
}
}

void TestCustomSign(LibreSslRsa& rsa) {
rsa.flags = 0x40;
for (int result : {0, 1, 7}) {
for (int reason : {0, 104}) {
unsigned char signature[2] {0xa5, 0xa5};
unsigned int written = UntouchedLength;
CallbackResult = result;
CallbackReason = reason;
CallbackCalls = 0;
SeedError();
Require(RSA_sign_nid_postfix(-1, nullptr, 1, signature, &written, &rsa) == result);
Require(written == 7 && signature[0] == 0x42 && signature[1] == 0xa5);
Require(CallbackCalls == 1);
CheckErrors(reason);
}
}
rsa.flags = 0;
}

void TestUnsupported(LibreSslRsa& rsa) {
unsigned char signature[128];
std::memset(signature, 0xa5, sizeof(signature));
unsigned int written = UntouchedLength;
SeedError();
bool threw = false;
try {
RSA_sign_nid_postfix(1, nullptr, 0, signature, &written, &rsa);
} catch (const std::exception&) {
threw = true;
}
Require(threw);
Require(written == UntouchedLength);
for (auto byte : signature) Require(byte == 0xa5);
CheckErrors(0);
}

} // namespace

int main(int argc, char** argv) {
std::uint64_t words[8];
std::fill(std::begin(words), std::end(words), UINT64_MAX);
TestBignum modulus {words, 8, 8, 0, 2};
LibreSslRsaMethod method {};
method.operations[2] = reinterpret_cast<void*>(PrivateEncrypt);
method.sign = reinterpret_cast<void*>(Sign);
LibreSslRsa rsa {};
rsa.meth = &method;
rsa.n = &modulus;
if (argc == 2) {
TestEarlyFailures(rsa, std::atoi(argv[1]));
return 0;
}
TestEarlyFailures(rsa);
TestObjectTable(rsa);
TestEarlyFailures(rsa);
TestPrivateEncrypt(rsa);
TestCustomSign(rsa);
TestUnsupported(rsa);
}
2 changes: 2 additions & 0 deletions docs/dev/debt/libSceLibreSsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@

- [X509_EXTENSION_get_critical, X509_EXTENSION_get_data and X509_EXTENSION_get_object](../../../core/libs/prx/libSceLibreSsl/Export.cpp) - signatures, null-input results, borrowed pointers and the signed `critical > 0` rule follow [LibreSSL 2.5.4 x509_v3.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/x509/x509_v3.c). The 24-byte LP64 extension layout (object at 0, 32-bit critical at 8, data at 16) follows [x509.h](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/x509/x509.h#L196-L201). [Published firmware 13.40 measurements](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214) identify libSceLibreSsl as 2.5.4, but these getters and this layout have not been independently checked on a console. The getters do not validate or decode the referenced ASN.1 objects.
- [ASN1_TIME_print](../../../core/libs/prx/libSceLibreSsl/src/Asn1.cpp) (libSceLibreSsl) is unverified on PS5. It follows LibreSSL 2.5.4 `t_x509.c` for UTCTime and GeneralizedTime, including the `BIO_printf` result: a valid time returns 1 even when the write fails, and anything else writes `Bad time value` and returns 0. A null time, or null data with a positive length, which LibreSSL dereferences, throws.

- [RSA_sign](../../../core/libs/prx/libSceLibreSsl/src/Rsa.cpp) error publication is unverified on PS5. Its implemented early failures queue RSA/0xfff reasons 131 (MD5+SHA1 length), 117 (negative algorithm type), 116 (`NID_undef`, whose OID is empty), and 112 (DigestInfo or MD5+SHA1 larger than the key permits), following LibreSSL 2.5.4 [`rsa_sign.c`](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/rsa/rsa_sign.c). A negative type also preserves the OBJ unknown-NID error when the added-object table exists. Custom signing and private-encrypt callbacks retain their own error and output behavior without an extra RSA_sign error. Positive algorithm types outside the implemented digest table still throw; DigestInfo parameters are always ASN.1 NULL. Encoding uses a host vector, so allocation failure still throws instead of queueing upstream malloc failure (65).
Loading