Skip to content
Draft
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
10 changes: 8 additions & 2 deletions 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/X509PubkeyParam.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/X509PubkeyParam.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/NumericObject.cpp)
target_include_directories(libSceLibreSsl3 PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(libSceLibreSsl3 PRIVATE libSceLibreSsl libc)
configure_windows_unwind(libSceLibreSsl3)
Expand Down Expand Up @@ -104,14 +104,20 @@ if(BUILD_TESTING)
configure_windows_unwind(guest_libressl3_key_reference_tests)
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_object_parser_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/ObjectParser.cpp)
target_include_directories(guest_libressl3_object_parser_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libressl3_object_parser_tests PRIVATE libSceLibreSsl3 libSceLibreSsl libc)
configure_windows_unwind(guest_libressl3_object_parser_tests)
add_test(NAME guest_libressl3_object_parser COMMAND guest_libressl3_object_parser_tests)
set_tests_properties(guest_libressl3_object_parser PROPERTIES TIMEOUT 20)
add_test_executable(guest_libressl3_version_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/OpenSslVersion.cpp)
target_include_directories(guest_libressl3_version_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libressl3_version_tests PRIVATE libSceLibreSsl3 libc)
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)
if(WIN32)
set_tests_properties(guest_crypto_memcmp guest_libressl3_digest guest_libressl3_asn1 guest_libressl3_key_reference guest_libressl3_version guest_libressl3_x509_pubkey_param PROPERTIES ENVIRONMENT_MODIFICATION
set_tests_properties(guest_crypto_memcmp guest_libressl3_digest guest_libressl3_asn1 guest_libressl3_key_reference guest_libressl3_object_parser guest_libressl3_version guest_libressl3_x509_pubkey_param PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl3>")
endif()
endif()
5 changes: 0 additions & 5 deletions core/libs/prx/libSceLibreSsl3/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ int APS5_VABI OBJ_cmp_nid_postfix(void) {
return 0;
}

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

int APS5_VABI OCSP_CERTID_free_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
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 @@ -74,7 +74,7 @@ int APS5_VABI EVP_aes_256_wrap_nid_postfix(void);
int APS5_VABI EVP_sha1_nid_postfix(void);
int APS5_VABI EVP_sha224_nid_postfix(void);
int APS5_VABI OBJ_cmp_nid_postfix(void);
int APS5_VABI OBJ_txt2obj_nid_postfix(void);
LibreSslAsn1Object* APS5_VABI OBJ_txt2obj_nid_postfix(const char* text, int noName);
int APS5_VABI OCSP_CERTID_free_nid_postfix(void);
int APS5_VABI OCSP_cert_to_id_nid_postfix(void);
int APS5_VABI OCSP_resp_find_status_nid_postfix(void);
Expand Down
147 changes: 147 additions & 0 deletions core/libs/prx/libSceLibreSsl3/src/NumericObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp"
#include "prx/libSceLibreSsl/include/Error.hpp"
#include "prx/libc/include/Api.hpp"
#include "prx/libc/include/General.hpp"

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <new>
#include <stdexcept>
#include <vector>

namespace {

constexpr int invalidNumber = 187;
constexpr int invalidSeparator = 131;
constexpr int invalidDigit = 130;
constexpr int missingSecondNumber = 138;
constexpr int firstNumberTooLarge = 122;
constexpr int secondNumberTooLarge = 147;
constexpr int tooLong = 155;
constexpr int allocationFailure = 65;

int readDecimalArc(const char*& cursor, std::uint64_t& value, int characterError) {
const char* begin = cursor;
value = 0;
while (*cursor != '\0' && *cursor != '.' && *cursor != ' ') {
const char character = *cursor;
if (character < '0' || character > '9') {
return characterError;
}
if (cursor != begin && value == 0 && character == '0') {
return invalidNumber;
}
if (value > std::numeric_limits<std::uint64_t>::max() / 10) {
return tooLong;
}
value = value * 10 + static_cast<std::uint64_t>(character - '0');
++cursor;
}
return cursor == begin ? invalidNumber : 0;
}

int appendArc(std::vector<unsigned char>& output, std::uint64_t value) {
unsigned char digits[10];
std::size_t count = 0;
do {
digits[count++] = static_cast<unsigned char>(value & 0x7f);
value >>= 7;
} while (value != 0);
if (count > static_cast<std::size_t>(std::numeric_limits<int>::max()) - output.size()) {
return tooLong;
}
while (count != 0) {
--count;
output.push_back(static_cast<unsigned char>(digits[count] | (count == 0 ? 0 : 0x80)));
}
return 0;
}

int parseNumericObject(const char* text, std::vector<unsigned char>& output) {
const char* cursor = text;
std::uint64_t first = 0;
int error = readDecimalArc(cursor, first, invalidSeparator);
if (error != 0) {
return error;
}
if (*cursor == '\0') {
return missingSecondNumber;
}
const char separator = *cursor++;
std::uint64_t second = 0;
error = readDecimalArc(cursor, second, invalidDigit);
if (error != 0) {
return error;
}
if (first > 2) {
return firstNumberTooLarge;
}
const std::uint64_t secondLimit = first == 2 ? std::numeric_limits<std::uint64_t>::max() - 80 : 39;
if (second > secondLimit) {
return secondNumberTooLarge;
}
error = appendArc(output, first * 40 + second);
if (error != 0) {
return error;
}
while (*cursor != '\0') {
if (*cursor != separator) {
return invalidSeparator;
}
++cursor;
std::uint64_t arc = 0;
error = readDecimalArc(cursor, arc, invalidDigit);
if (error != 0) {
return error;
}
error = appendArc(output, arc);
if (error != 0) {
return error;
}
}
return 0;
}

LibreSslAsn1Object* rejectNumericObject(int reason) {
LibreSslPutError_nid_no_patch(13, 0xfff, reason);
return nullptr;
}

}

extern "C" LibreSslAsn1Object* APS5_VABI OBJ_txt2obj_nid_postfix(const char* text, int noName) {
if (text == nullptr) {
throw std::invalid_argument("OBJ_txt2obj requires a non-null text pointer");
}
if (noName == 0 && (text[0] < '0' || text[0] > '9')) {
NotImplemented_nid_no_patch(__func__);
}
try {
std::vector<unsigned char> encoded;
const int error = parseNumericObject(text, encoded);
if (error != 0) {
return rejectNumericObject(error);
}
auto* data = static_cast<unsigned char*>(malloc_nid_postfix(encoded.size()));
if (data == nullptr) {
return nullptr;
}
auto* object = static_cast<LibreSslAsn1Object*>(calloc_nid_postfix(1, sizeof(LibreSslAsn1Object)));
if (object == nullptr) {
free_nid_postfix(data);
return rejectNumericObject(allocationFailure);
}
std::memcpy(data, encoded.data(), encoded.size());
object->shortName = nullptr;
object->longName = nullptr;
object->nid = 0;
object->length = static_cast<int>(encoded.size());
object->data = data;
object->flags = 9;
return object;
} catch (const std::bad_alloc&) {
return nullptr;
}
}
182 changes: 182 additions & 0 deletions core/libs/prx/libSceLibreSsl3/tests/ObjectParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp"
#include "prx/libc/include/Api.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <initializer_list>
#include <source_location>
#include <stdexcept>
#include <string>
#include <unordered_map>

static_assert(sizeof(LibreSslAsn1Object) == 40);
static_assert(offsetof(LibreSslAsn1Object, shortName) == 0);
static_assert(offsetof(LibreSslAsn1Object, longName) == 8);
static_assert(offsetof(LibreSslAsn1Object, nid) == 16);
static_assert(offsetof(LibreSslAsn1Object, length) == 20);
static_assert(offsetof(LibreSslAsn1Object, data) == 24);
static_assert(offsetof(LibreSslAsn1Object, flags) == 32);

namespace {

std::unordered_map<void*, std::size_t> allocations;
int failAfter = -1;
int attempts = 0;

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

void* APS5_VABI Allocate_nid_no_patch(std::size_t size) {
if (attempts++ == failAfter) return nullptr;
void* pointer = std::malloc(size);
Require(pointer != nullptr);
allocations.emplace(pointer, size);
return pointer;
}

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

void* APS5_VABI AllocateZeroed_nid_no_patch(std::size_t count, std::size_t size) {
Require(count == 1 && size == sizeof(LibreSslAsn1Object));
void* pointer = Allocate_nid_no_patch(size);
if (pointer) std::memset(pointer, 0, size);
return pointer;
}
void* APS5_VABI UnexpectedReallocate_nid_no_patch(void*, std::size_t) { throw std::runtime_error("unexpected realloc"); }
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"); }

void Release(LibreSslAsn1Object* object) {
Require(object && object->flags == 9 && object->nid == 0);
free_nid_postfix(const_cast<unsigned char*>(object->data));
free_nid_postfix(object);
}

void Check(const char* text, std::initializer_list<unsigned char> expected) {
for (const int noName : {0, 1, -1}) {
auto* object = OBJ_txt2obj_nid_postfix(text, noName);
Require(object && !object->shortName && !object->longName);
Require(object->length == static_cast<int>(expected.size()));
Require(std::equal(expected.begin(), expected.end(), object->data));
Require(allocations.size() == 2);
Release(object);
Require(allocations.empty());
}
}

void Numeric() {
Check("0.0", {0});
Check("1.39", {79});
Check("2.0", {80});
Check("2.999.3", {0x88, 0x37, 3});
Check("1.2.840.113549.1.1.1", {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 1, 1, 1});
Check("1 2 840 113549 1 1 1", {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 1, 1, 1});
Check("01.02.03", {42, 3});
Check("2.5.4.3", {85, 4, 3});
Check("1.3.101.112", {43, 101, 112});
Check("2.16.0.127.128.16383.16384", {96, 0, 127, 0x81, 0, 0xff, 0x7f, 0x81, 0x80, 0});
Check("2.18446744073709551535", {0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f});
Check("2.16.18446744073709551615", {96, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f});
Check("2.16.18446744073709551616", {96, 0});
Check("2.18446744073709551616", {80});
Check("1.2.184467440737095516161", {42, 1});
Check("1.2\0.3", {42});
for (const char* text : {"", "0", "1.", "1..2", "1 2", "1.2.", "1 2 ", "1 2.3", "1.2 3", ".1.2", " 1.2", "1\t2",
"3.0", "0.40", "1.40", "00.2", "1.00", "1.2.00", "-1.2", "1.-2", "1.+2", "1.a", "1.2x", "2.18446744073709551536",
"2.16.184467440737095516159", "CN", "commonName", "unknown-name"}) {
Require(OBJ_txt2obj_nid_postfix(text, 1) == nullptr);
Require(allocations.empty());
}
std::string text = "1.2";
for (int index = 0; index < 1024; ++index) text += ".1";
auto* object = OBJ_txt2obj_nid_postfix(text.c_str(), 1);
Require(object && object->length == 1025 && object->data[0] == 42);
Require(std::all_of(object->data + 1, object->data + object->length, [](unsigned char value) { return value == 1; }));
Release(object);
}

void NameLookupUnavailable() {
for (const char* text : {"CN", "commonName", "Ed25519", "NULL", "not-a-known-object", ""}) {
ERR_clear_error_nid_postfix();
Require(OBJ_txt2obj_nid_postfix("1", 1) == nullptr);
const int before = attempts;
bool threw = false;
try { OBJ_txt2obj_nid_postfix(text, 0); }
catch (const std::runtime_error& error) {
threw = std::string(error.what()).find("not implemented") != std::string::npos;
}
Require(threw && attempts == before && allocations.empty());
Require(ERR_get_error_nid_postfix() == 0x0dfff08au);
Require(ERR_get_error_nid_postfix() == 0);
}
for (int noName : {0, 1, -1}) {
bool threw = false;
try { OBJ_txt2obj_nid_postfix(nullptr, noName); }
catch (const std::invalid_argument&) { threw = true; }
Require(threw);
}
}

void Errors() {
struct Rejection { const char* text; int reason; };
for (const Rejection rejection : {Rejection{"", 187}, {"1", 138}, {"1.", 187}, {"1.2a", 130},
{"a.2", 131}, {"1 2.3", 131}, {"3.1", 122}, {"1.40", 147},
{"2.18446744073709551536", 147}, {"2.1.184467440737095516159", 155},
{"3.a", 130}, {"3.", 187}, {"3.00", 187}, {"1.40a", 130}, {"1.40.00", 147},
{"1.2.184467440737095516160", 187}}) {
ERR_clear_error_nid_postfix();
Require(OBJ_txt2obj_nid_postfix(rejection.text, 1) == nullptr);
Require(ERR_get_error_nid_postfix() == (0x0dfff000u | static_cast<unsigned>(rejection.reason)));
Require(ERR_get_error_nid_postfix() == 0);
}
Require(OBJ_txt2obj_nid_postfix("1", 1) == nullptr);
auto* object = OBJ_txt2obj_nid_postfix("1.2.3", 1);
Require(object != nullptr);
Require(ERR_get_error_nid_postfix() == 0x0dfff08au);
Release(object);
}

void AllocationFailures() {
for (const int allocation : {0, 1}) {
failAfter = allocation;
attempts = 0;
ERR_clear_error_nid_postfix();
Require(OBJ_txt2obj_nid_postfix("1.2.3", 1) == nullptr);
Require(ERR_get_error_nid_postfix() == (allocation == 1 ? 0x0dfff041u : 0u));
Require(attempts == allocation + 1 && allocations.empty());
}
failAfter = -1;
auto* first = OBJ_txt2obj_nid_postfix("2.5.4.3", 1);
auto* second = OBJ_txt2obj_nid_postfix("2.5.4.3", 1);
Require(first != second && first->data != second->data && allocations.size() == 4);
Release(second);
Release(first);
Require(allocations.empty());
}

}

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*>(AllocateZeroed_nid_no_patch),
reinterpret_cast<void*>(UnexpectedReallocate_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());
Numeric();
NameLookupUnavailable();
Errors();
AllocationFailures();
std::puts("OBJ_txt2obj tests passed");
}
Loading
Loading