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
4 changes: 0 additions & 4 deletions core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ int APS5_VABI X509_EXTENSION_free_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI X509_STORE_CTX_get0_param_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}

int APS5_VABI X509_STORE_load_locations_nid_postfix() {
NotImplemented_nid_no_patch(__func__);
}
Expand Down
11 changes: 11 additions & 0 deletions core/libs/prx/libSceLibreSsl3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/X509Algor
target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/BioFields.cpp)
target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/BioMethod.cpp)
target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/TimeNew.cpp)
target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/X509StoreContext.cpp)
if(WIN32)
target_sources(libSceLibreSsl3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ParentExports.cpp)
endif()
Expand Down Expand Up @@ -237,6 +238,16 @@ if(BUILD_TESTING)
set_tests_properties(guest_libressl3_hmac_context PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl3>")
endif()
add_test_executable(guest_libressl3_x509_store_context_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/X509StoreContext.cpp)
target_include_directories(guest_libressl3_x509_store_context_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libressl3_x509_store_context_tests PRIVATE libSceLibreSsl3 libSceLibreSsl libc)
configure_windows_unwind(guest_libressl3_x509_store_context_tests)
add_test(NAME guest_libressl3_x509_store_context COMMAND guest_libressl3_x509_store_context_tests)
set_tests_properties(guest_libressl3_x509_store_context PROPERTIES TIMEOUT 30)
if(WIN32)
set_tests_properties(guest_libressl3_x509_store_context PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceLibreSsl3>")
endif()
add_test_executable(guest_libressl3_cipher_tests ${CMAKE_CURRENT_SOURCE_DIR}/tests/Cipher.cpp)
target_include_directories(guest_libressl3_cipher_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_libressl3_cipher_tests PRIVATE libSceLibreSsl3)
Expand Down
15 changes: 0 additions & 15 deletions core/libs/prx/libSceLibreSsl3/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,11 @@ int APS5_VABI SSL_CTX_clear_chain_certs_nid_postfix(void) {
return 0;
}

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

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

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

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

int APS5_VABI d2i_ECParameters_nid_postfix(void) {
NotImplemented_nid_no_patch(__func__);
return 0;
Expand Down
8 changes: 4 additions & 4 deletions core/libs/prx/libSceLibreSsl3/include/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ int APS5_VABI SSL_SESSION_up_ref_nid_postfix(LibreSsl3SslSession* session);
std::size_t APS5_VABI SSL_get_client_random_nid_postfix(const LibreSsl3Ssl* ssl, unsigned char* out, std::size_t maxOut);
int APS5_VABI SSL_up_ref_nid_postfix(LibreSsl3Ssl* ssl);
int APS5_VABI X509_PUBKEY_get0_param_nid_postfix(void** publicKeyAlgorithm, const unsigned char** publicKey, int* publicKeyLength, void** algorithm, const void* pub);
int APS5_VABI X509_STORE_CTX_free_nid_postfix(void);
void APS5_VABI X509_STORE_CTX_free_nid_postfix(LibreSslX509StoreContext* context);
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);
LibreSslX509StoreContext* APS5_VABI X509_STORE_CTX_new_nid_postfix(void);
int APS5_VABI X509_STORE_up_ref_nid_postfix(LibreSslX509Store* store);
const LibreSsl3Stack* APS5_VABI X509_get0_extensions_nid_postfix(const LibreSsl3ExtensionsX509* certificate);
const void* APS5_VABI X509_get0_notAfter_nid_postfix(const LibreSsl3ValidityX509* certificate);
const void* APS5_VABI X509_get0_notBefore_nid_postfix(const LibreSsl3ValidityX509* certificate);
Expand Down Expand Up @@ -307,7 +307,7 @@ int APS5_VABI SSL_set_info_callback_nid_postfix();
int APS5_VABI TLS_method_nid_postfix();
int APS5_VABI TLS_server_method_nid_postfix();
int APS5_VABI X509_EXTENSION_free_nid_postfix();
int APS5_VABI X509_STORE_CTX_get0_param_nid_postfix();
void* APS5_VABI X509_STORE_CTX_get0_param_nid_postfix(LibreSslX509StoreContext* context);
int APS5_VABI X509_STORE_load_locations_nid_postfix();
int APS5_VABI X509_VERIFY_PARAM_set_flags_nid_postfix(LibreSslX509VerifyParam* param, std::uint64_t flags);
int APS5_VABI X509_get0_pubkey_nid_postfix();
Expand Down
48 changes: 48 additions & 0 deletions core/libs/prx/libSceLibreSsl3/src/X509StoreContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp"
#include "prx/libc/include/Api.hpp"
#include "prx/libc/include/General.hpp"

#include <atomic>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <stdexcept>

namespace {

constexpr std::size_t CleanupCallback = 11;

}

extern "C" {

LibreSslX509StoreContext* APS5_VABI X509_STORE_CTX_new_nid_postfix(void) {
return static_cast<LibreSslX509StoreContext*>(calloc_nid_postfix(1, sizeof(LibreSslX509StoreContext)));
}

void APS5_VABI X509_STORE_CTX_free_nid_postfix(LibreSslX509StoreContext* context) {
if (!context) return;
if (context->callbacks[CleanupCallback]) NotImplemented_nid_no_patch("X509_STORE_CTX_free: cleanup callback");
if (context->param && !context->parent) NotImplemented_nid_no_patch("X509_STORE_CTX_free: owned verify parameters");
if (context->tree) NotImplemented_nid_no_patch("X509_STORE_CTX_free: policy tree");
if (context->chain) NotImplemented_nid_no_patch("X509_STORE_CTX_free: certificate chain");
if (context->exData) NotImplemented_nid_no_patch("X509_STORE_CTX_free: extra data");
free_nid_postfix(context);
}

void* APS5_VABI X509_STORE_CTX_get0_param_nid_postfix(LibreSslX509StoreContext* context) {
if (!context) throw std::invalid_argument("X509_STORE_CTX_get0_param: null context");
return context->param;
}

int APS5_VABI X509_STORE_up_ref_nid_postfix(LibreSslX509Store* store) {
if (!store) throw std::invalid_argument("X509_STORE_up_ref: null store");
std::atomic_ref<std::int32_t> count(store->references);
auto previous = count.load();
for (;;) {
if (previous == std::numeric_limits<std::int32_t>::max()) throw std::overflow_error("X509_STORE_up_ref: reference count overflow");
if (count.compare_exchange_weak(previous, previous + 1)) return previous > 0;
}
}

}
194 changes: 194 additions & 0 deletions core/libs/prx/libSceLibreSsl3/tests/X509StoreContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#include "prx/libSceLibreSsl3/libSceLibreSsl3.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"

#include <array>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <thread>
#include <unordered_map>
#include <vector>

static_assert(sizeof(LibreSslX509StoreContext) == 240);
static_assert(offsetof(LibreSslX509StoreContext, param) == 40);
static_assert(offsetof(LibreSslX509StoreContext, callbacks) == 56);
static_assert(offsetof(LibreSslX509StoreContext, chain) == 160);
static_assert(offsetof(LibreSslX509StoreContext, tree) == 168);
static_assert(offsetof(LibreSslX509StoreContext, parent) == 224);
static_assert(offsetof(LibreSslX509StoreContext, exData) == 232);
static_assert(offsetof(LibreSslX509Store, references) == 128);
static_assert(std::atomic_ref<std::int32_t>::required_alignment <= alignof(std::int32_t));

namespace {

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

template <typename Function>
bool Throws(Function function) {
try {
function();
} catch (const std::exception&) {
return true;
}
return false;
}

std::unordered_map<void*, std::size_t> allocations;

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

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

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

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"); }

int Marker = 0;

void TestNewAndFree() {
auto* context = X509_STORE_CTX_new_nid_postfix();
Require(context != nullptr);
std::array<unsigned char, sizeof(LibreSslX509StoreContext)> zero{};
Require(std::memcmp(context, zero.data(), zero.size()) == 0);
X509_STORE_CTX_free_nid_postfix(context);
X509_STORE_CTX_free_nid_postfix(nullptr);
}

void TestFreeBorrowedParam() {
auto* context = X509_STORE_CTX_new_nid_postfix();
Require(context != nullptr);
context->parent = context;
context->param = &Marker;
X509_STORE_CTX_free_nid_postfix(context);
}

template <typename Setter>
void CheckFreeThrows(Setter set) {
auto* context = X509_STORE_CTX_new_nid_postfix();
Require(context != nullptr);
set(*context);
LibreSslX509StoreContext before;
std::memcpy(&before, context, sizeof(before));
Require(Throws([&] { X509_STORE_CTX_free_nid_postfix(context); }));
Require(std::memcmp(&before, context, sizeof(before)) == 0);
std::memset(context, 0, sizeof(*context));
X509_STORE_CTX_free_nid_postfix(context);
}

void TestFreeUnsupported() {
CheckFreeThrows([](LibreSslX509StoreContext& context) { context.param = &Marker; });
CheckFreeThrows([](LibreSslX509StoreContext& context) { context.callbacks[11] = &Marker; });
CheckFreeThrows([](LibreSslX509StoreContext& context) { context.chain = &Marker; });
CheckFreeThrows([](LibreSslX509StoreContext& context) { context.tree = &Marker; });
CheckFreeThrows([](LibreSslX509StoreContext& context) { context.exData = &Marker; });
}

void TestGetParam() {
auto* context = X509_STORE_CTX_new_nid_postfix();
Require(context != nullptr);
Require(X509_STORE_CTX_get0_param_nid_postfix(context) == nullptr);
context->parent = context;
context->param = &Marker;
Require(X509_STORE_CTX_get0_param_nid_postfix(context) == &Marker);
X509_STORE_CTX_free_nid_postfix(context);
Require(Throws([] { X509_STORE_CTX_get0_param_nid_postfix(nullptr); }));
}

void TestUpRef() {
auto* store = X509_STORE_new_nid_postfix();
Require(store != nullptr);
Require(store->references == 1);
Require(X509_STORE_up_ref_nid_postfix(store) == 1);
Require(store->references == 2);
Require(X509_STORE_up_ref_nid_postfix(store) == 1);
Require(store->references == 3);
X509_STORE_free_nid_postfix(store);
X509_STORE_free_nid_postfix(store);
Require(store->references == 1);
X509_STORE_free_nid_postfix(store);

LibreSslX509Store owned{};
owned.references = 0;
Require(X509_STORE_up_ref_nid_postfix(&owned) == 0);
Require(owned.references == 1);
owned.references = -5;
Require(X509_STORE_up_ref_nid_postfix(&owned) == 0);
Require(owned.references == -4);
owned.references = std::numeric_limits<std::int32_t>::max();
Require(Throws([&] { X509_STORE_up_ref_nid_postfix(&owned); }));
Require(owned.references == std::numeric_limits<std::int32_t>::max());
Require(Throws([] { X509_STORE_up_ref_nid_postfix(nullptr); }));
}

void TestUpRefRace() {
constexpr int Threads = 8;
constexpr int Increments = 20000;
LibreSslX509Store shared{};
shared.references = 1;
std::atomic<bool> go{false};
std::vector<std::thread> workers;
for (int index = 0; index < Threads; ++index) {
workers.emplace_back([&] {
while (!go.load()) std::this_thread::yield();
for (int count = 0; count < Increments; ++count) X509_STORE_up_ref_nid_postfix(&shared);
});
}
go.store(true);
for (auto& worker : workers) worker.join();
Require(shared.references == 1 + Threads * Increments);
}

}

int main() {
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());
TestNewAndFree();
Require(allocations.empty());
TestFreeBorrowedParam();
TestFreeUnsupported();
TestGetParam();
TestUpRef();
TestUpRefRace();
Require(allocations.empty());
std::puts("libSceLibreSsl3 X509_STORE_CTX tests passed");
return 0;
}
Loading
Loading