feat(libSceLibreSsl3): implement the SSL_CTX, SSL, SSL_SESSION and X509 retains, the SSL_CTX version bounds, SSL_get_client_random and X509_get_version #12754

Merged
Lindamea1 merged 3 commits from upstream-pr/12754 into main 2026-11-06 07:12:55 +00:00
Lindamea1 commented 2026-10-29 07:51:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Lindamea1 on 2026-10-29T07:51:15Z.


What

On main, libSceLibreSsl3 exports these eight LibreSSL calls as stubs that throw (Export.cpp L279-L307, L364-L367, L379-L382). This PR implements them with LibreSSL 4.0.0 behaviour, the version of the firmware module (OpenSSL_version(0) measured in #8981). Names and NIDs are unchanged; the stubs are replaced by src/Key.cpp, src/Ssl.cpp and src/X509.cpp.

SSL_CTX_up_ref, its guest_libressl3_key_reference case and the LibreSsl3SslCtx struct in include/Key.hpp come from #12578. PrivacyOnion closed that pull request unmerged on 31 October, together with 28 other pull requests of theirs and without a comment. No other open PR implements the call and the version bounds need the struct, so this PR keeps that commit under their authorship and no longer depends on #12578. Its code is unchanged by the rebase; only its debt lines were merged with main. This PR's own change is the last commit. It appends the SSL_CTX members the version bounds need to that struct, with their static_assert lines.

Export NID Behaviour
int SSL_up_ref(SSL*) lHGGE+Ubycs adds 1 to the int32 count at offset 176; returns 1 if the new count is above 1, else 0
int SSL_SESSION_up_ref(SSL_SESSION*) MOISxAYBtc0 the same, count at offset 184
int X509_up_ref(X509*) HrCznvA8AQk the same, count at offset 28
int SSL_CTX_up_ref(SSL_CTX*) AmNeqCMs2oU the same, count at offset 40
int SSL_CTX_set_min_proto_version(SSL_CTX*, uint16_t) NG+D00RsX1Y below
int SSL_CTX_set_max_proto_version(SSL_CTX*, uint16_t) mF9+FT4UKSE below
size_t SSL_get_client_random(const SSL*, unsigned char*, size_t) 1sWluYvAfBM below
long X509_get_version(const X509*) gg2cJQtaEns below
  • Retains: the four retains use Retain_nid_no_patch of the merged EVP_PKEY_up_ref (#11165), moved from src/Key.cpp into include/Reference.hpp (inline; the overflow message no longer says "key"). SSL_CTX_up_ref stays in src/Key.cpp, next to the merged EVP_PKEY_up_ref, EC_KEY_up_ref and RSA_up_ref, which use the moved helper too. It adds atomically. In 4.0.0, CRYPTO_add is CRYPTO_add_lock (crypto.h L207-L208), which adds under the library's own pthread mutexes and calls no locking callbacks (crypto_lock.c L24-L96). A null object and a count of INT32_MAX throw without changing memory, as in the merged retains: upstream dereferences the null object, and its signed add at INT32_MAX is undefined behaviour. BunnyFl's native run of the firmware module for #11165 showed that the module wraps EVP_PKEY and EC_KEY counts at INT32_MAX (comment); the SSL and X509 counts were not measured.
  • Version bounds follow ssl_version_set_min and ssl_version_set_max on ctx->method. Version 0 stores the method bound in min_tls_version (offset 128) or max_tls_version (130) and 0 in min_proto_version (132) or max_proto_version (134), and returns 1. For a DTLS method (any nonzero dtls), 0xFEFF and 0xFEFD map to TLS 1.1 and 1.2 and every other nonzero value returns 0. A TLS method takes the value as a plain number, so there a DTLS value, numerically above every TLS version, fails as a minimum and is lowered to the method maximum as a maximum. The call returns 0 when the method range is inverted, when the value and the stored opposite TLS bound are inverted, or when the two ranges do not meet. Otherwise a minimum below the method minimum is raised to it, or a maximum above the method maximum is lowered to it. For a DTLS method the result is converted back to the DTLS form, and the call returns 0 if it has none. Then both fields are stored and the call returns 1. A call that returns 0 writes nothing and queues no error. A null context or method throws; upstream dereferences both.
  • SSL_get_client_random: a null out returns 32 without reading the SSL. Otherwise it copies min(32, max_out) bytes from s->s3->client_random (s3 at offset 56 of SSL, client_random at 40 of SSL3_STATE) and returns the count. A null SSL throws. A null s3 throws only when there is something to copy, since a 0-byte copy reads nothing.
  • X509_get_version returns ASN1_INTEGER_get(x->cert_info->version) with the 4.0.0 rules, as a 64-bit value on every host, including Windows, where long is 32 bits. A null version gives 0. A negative length gives -1 with no error. A type other than 2 or 0x102 gives -1 and queues ERR_PACK(13, 0xfff, 225) (0x0DFFF0E1). Every byte is read, so leading zeros are accepted; a byte that would overflow 64 bits gives -1 with 0x0DFFF0DF (ASN1_R_TOO_LARGE), as does a positive value above INT64_MAX. A negative magnitude above 2^63 gives -1 with 0x0DFFF0E0 (ASN1_R_TOO_SMALL). A null certificate or cert_info, and null data with a positive length, throw.
  • Error queue: X509_get_version is the first libSceLibreSsl3 export that queues errors (the merged d2i_ASN1_INTEGER does not, debt line 9). It queues through LibreSslPutError_nid_no_patch into the libSceLibreSsl queue. On Linux and macOS, the ERR_get_error of a libSceLibreSsl3 importer resolves to libSceLibreSsl by dependency lookup and reads that queue. On Windows, a scoped import searches only the named module and libc (WindowsEntryStubBuilder.cpp L171-L200), and the merged #12369 forwards libSceLibreSsl3's ERR_get_error to libSceLibreSsl (ParentExports.cpp L234-L235), so the importer reads that queue there too. #12369's debt entry lists that forwarding as unverified at runtime on Windows, and I have not run it.

include/Ssl.hpp and include/X509.hpp declare the 4.0.0 prefixes of SSL_METHOD, SSL, SSL3_STATE, SSL_SESSION, X509 and X509_CINF. The SSL_CTX prefix is LibreSsl3SslCtx in include/Key.hpp, extended to the version bounds at 128-135; the setters read its const void* method as SSL_METHOD. The fields are fixed-width, so the offsets also hold with the 32-bit long of MinGW, and every offset used has a static_assert. No constructor of these objects is implemented in either LibreSSL library (SSL_CTX_new, SSL_new and d2i_X509 throw in libSceLibreSsl), so all of them are caller-owned here, as for the merged EVP_PKEY_up_ref.

References, LibreSSL 4.0.0 (libressl-v4.0.0, ce488b78e0bd):

The code is written from the behaviour above; no upstream source is copied.

Gaps, as three new entries in docs/dev/debt/libSceLibreSsl3.md:

  • SSL_CTX_up_ref, #12578's entry: the console layout and retain/release interoperability are unverified. The count at offset 40 follows 4.0.0, and null contexts and signed-counter overflow, undefined upstream, throw without changing memory.
  • The SSL offsets are inferred from 4.0.0 and not measured on the firmware module. A libSceLibreSsl3 importer reaches the 2.5.4 constructor stubs of libSceLibreSsl, by dependency lookup on Linux and macOS and through #12369's forwarding on Windows, and 2.5.4 lays out the SSL_METHOD, SSL_CTX, SSL and SSL_SESSION objects these calls read differently, so every SSL call here would misread a 2.5.4 object once those constructors are implemented. Signed-counter overflow at INT32_MAX and null objects, undefined upstream, throw without changing memory.
  • X509_get_version queues its errors in the libSceLibreSsl queue without file or line, while the firmware module keeps its own queue.

The pending-export count in the same file goes from 80 to 72. python3 tools/progress.py --module libSceLibreSsl3 goes from libraries 12/270 (4.44%) to libraries 20/270 (7.41%).

Firmware evidence: the SSL offsets (SSL_CTX 40 and 128-135, SSL 56 and 176, SSL3_STATE 40, SSL_SESSION 184) would be settled by a native run of the firmware libSceLibreSsl3.sprx on module-created objects, like BunnyFl's for #11165 (comment), for example SSL_CTX_new(TLS_client_method()) followed by these calls. If review wants that first, the X509 pair can go in its own pull request: src/X509.cpp, include/X509.hpp, include/Reference.hpp with the src/Key.cpp change, the X509 cases of the test, the third debt entry, and its own lines in CMakeLists.txt, Api.hpp, Export.cpp and on line 5 of the debt file.

No title is known to import these functions; this is coverage, and it unblocks nothing while the constructors throw. They are TLS client calls of code built against LibreSSL 2.9 or later. LibreSSL 2.7 and 2.8 already export the other seven, but define X509_get_version as a macro over ASN1_INTEGER_get (2.8.0 x509.h L592); 2.9.0 makes it a function (x509_set.c L90-L94).

Overlap, checked at 15:36Z on 31 October with this branch against main 474030e584 and the 1,670 open PR heads fetched then:

  • gh pr list --search finds no other open PR with the eight names or their NIDs, apart from #12945, which carries this branch's commits, and a mention in a comment on #10960.
  • #12945 (BIO_up_ref) is stacked on this PR and names it on Depends on. It still carries this branch's older commits; replayed on this head, its own commit conflicts only in the debt file.
  • tools/pr_overlap.py --branch lists #10395, #10452, #10490, #10491, #10493, #11146, #12315 and #13897, none with a shared export. It could not compare #9970, #10461, #10557, #10631, #10960, #12928 and #13662, which conflict with main; git diff of each against its merge base has no changed line with the eight functions. It also lists this PR's previous head and #12945, which carry the same changes.

Lines this branch shares with those it could compare:

#9571 merges with this branch without a conflict. It gives LibreSslPutError_nid_no_patch an optional file and line, which the three-argument call here still compiles against. It also implements libSceLibreSsl3's ERR_peek_last_error on the libSceLibreSsl queue, so after it that call also sees X509_get_version errors, as ERR_peek_error already does.

Proposed order: this PR before #12945; the others in any order. This PR needs nothing from those, and I rebase it onto each one that lands before it.

Tested

Linux (Gentoo, kernel 7.2.8, AMD Ryzen 7 9800X3D), GCC 16.2.1, Ninja, Release build with BUILD_TESTING=ON.

Current head, 34728c573e, on main 474030e584, with #12578's commit as 3e2103d0f3:

  • This branch conflicted with main in include/Key.hpp, src/Key.cpp, GuestLibreSsl3KeyReference.cpp and the debt file after #13267 (RSA_up_ref), and #12578 was closed, so I rebased both commits onto main. Against the previous head 0ee29b8d1b, the added and removed lines of both commits are unchanged outside docs/. The resolutions keep RSA_up_ref and SSL_CTX_up_ref side by side in those files and recount line 5 of the debt file. The rebase also drops the blank line that #12578's rebase left at the end of the debt file, and SSL_CTX_up_ref's entry now cites ssl_local.h at the 4.0.0 commit its label names. #13659 (EVP_CIPHER_key_length, EVP_CIPHER_iv_length) landed in this library afterwards and rebased without a conflict. Its two exports came from the additional export inventory, so line 5 stays at 72.
  • CI test selection (the steps below): 648 of 3062 tests selected, 648/648 passed, none skipped, with public_headers, guest_crypto_memcmp and the seven guest_libressl3_* tests.
  • python3 tools/check_test_registration.py build: passed (1632 test executables, none built and not run).
  • python3 tools/check_conventions.py --base 474030e584, also with this title and description: passed.
  • NID-patched build/core/libs/libs/libSceLibreSsl3.prx: the eight NIDs are exported at the addresses of their _nid_postfix definitions. The inline helper is not among the library's 397 defined dynamic symbols.
  • MinGW-w64 syntax check (the command below) of Export.cpp, Key.cpp, Ssl.cpp, X509.cpp, GuestLibreSsl3Ssl.cpp and GuestLibreSsl3KeyReference.cpp: no diagnostics. This is not a Windows build or run.
  • The pull request's Windows CI job on this head, merged onto main 474030e584: 2524 tests, 0 failed, 894 skipped. guest_libressl3_ssl, guest_libressl3_key_reference, guest_crypto_memcmp and the other five guest_libressl3_* tests passed.

Previous head, 0ee29b8d1b, on #12578's ced92ec4ed (main f7f03daf5), with the same code and tests:

  • The pull request's Windows CI job passed: it merges the head onto main f91137142e and builds everything, but it runs none of the LibreSSL tests.
  • CI test selection (the steps below): 629 of 2977 tests selected, 629/629 passed, none skipped, with all five libSceLibreSsl3 tests (guest_crypto_memcmp among them). This ran before the last amend, which changed only the debt file.
  • python3 tools/check_test_registration.py build: passed (1579 test executables, none built and not run).
  • python3 tools/check_conventions.py --base upstream/main, also with that head's title and description: passed.
  • NID-patched build/core/libs/libs/libSceLibreSsl3.prx: the seven NIDs, and #12578's SSL_CTX_up_ref, are exported at the addresses of their _nid_postfix definitions. The inline helper is not among the library's 397 defined dynamic symbols.
  • MinGW-w64 syntax check (the command below) of Export.cpp, Key.cpp, Ssl.cpp, X509.cpp, GuestLibreSsl3Ssl.cpp and GuestLibreSsl3KeyReference.cpp: no warnings. This is not a Windows build or run.

Earlier head, 48bc460a1d (#12578's 00b24c75b4 and this commit, on main 319621ba2), with the same code and tests:

  • ctest --test-dir build -R 'guest_libressl3_ssl|guest_libressl3_key_reference|public_headers' --output-on-failure -j8 --timeout 300: 67/67 passed (public_headers and its 64 compile batches, guest_libressl3_key_reference with #12578's SSL_CTX_up_ref case on the extended struct and the moved helper, and guest_libressl3_ssl).
  • python3 tools/check_test_registration.py build: passed (1516 test executables, none built and not run).
  • CI test selection (tools/select_tests.py dump, select with the changed files, ninja -f select.ninja selected, select_tests.py ctest): 491 of 2746 tests selected, 491/491 passed.
  • python3 tools/check_conventions.py --base 319621ba2, also with that head's title and description: passed.
  • Merged into main 3a2b91981 without conflicts and rebuilt: ctest -R 'guest_libressl3|public_headers', 69/69 passed; CI test selection on the merge, 580 of 2858 tests selected, all five libSceLibreSsl3 tests (with guest_crypto_memcmp) among them, 580/580 passed.
  • The new guest_libressl3_ssl test (core/libs/tests/GuestLibreSsl3Ssl.cpp):
    • Each retain on a guarded object with counts -2, 0, 1, 37 and INT32_MAX - 1: the return value, and only the four count bytes at the stated offset change. Then 4 threads × 1000 retains give exactly 4001.
    • 44 version-bound cases on TLS- and DTLS-client-like methods. They cover version 0, raising and lowering to the method bounds, each failing condition of the clamp on its own, DTLS values on a TLS method (both setters, 0xFEFF and 0xFEFD) and TLS values on a DTLS method, a clamped result with no DTLS form, a dtls flag of -1, and the opposite bound read from the TLS field. Each case checks the return value and every byte of the guarded context, so a failing call writes nothing.
    • SSL_get_client_random with a null output (also with a null SSL) and with limits 0, 1, 10, 31, 32, 33, 64 and SIZE_MAX: the count, the bytes taken from offset 40 of the state, and an untouched tail. A 0-byte copy with a null s3 returns 0.
    • X509_get_version on 22 integers (null, empty, negative length, leading zeros, big-endian order, INT64_MAX, INT64_MIN, negative zero, wrong types, each overflow): the value, the exact error code and that nothing else is queued.
    • It has no assertion that only checks for a throw.
  • The test rejects wrong implementations. I compiled it with 33 mutated copies of Ssl.cpp, X509.cpp and Reference.hpp against the built libSceLibreSsl, libc and libkernel, and every one aborted. The mutants dropped each clamp condition, read the opposite bound from the proto field, mapped or rejected DTLS values on a TLS method, used a wrong count field, copied the server random, ignored max_out, added the 2.5.4 eight-byte limit, changed an error reason or the library, and so on. The unmodified sources passed.
  • NID-patched build/core/libs/libs/libSceLibreSsl3.prx: the seven NIDs, and #12578's SSL_CTX_up_ref, are exported at the addresses of their _nid_postfix definitions. The library has 88 defined dynamic symbols, the 87 exports and DummyFunction_nid_no_patch, so the inline helper is not exported.
  • MinGW-w64 syntax check of the changed files only (x86_64-w64-mingw32-g++ 16.2.0, -std=gnu++20 -fsyntax-only -Wall -Wextra -D_WIN32_WINNT=0x0602 -I core/libs -I core/libs/nid/include -I core/libs/prx/libkernel) on Export.cpp, Key.cpp, Ssl.cpp, X509.cpp, GuestLibreSsl3Ssl.cpp and GuestLibreSsl3KeyReference.cpp, and on Key.hpp, Ssl.hpp, X509.hpp, Reference.hpp and Api.hpp each included alone with -Werror: no warnings. This is not a Windows build or run.
  • Windows, by @JJenkx: that head merged onto main edf6804cf5, Windows 11 Enterprise, WinLibs GCC 15.2.0 UCRT, Ninja, Release, BUILD_TESTING=ON. guest_libressl3_key_reference and guest_libressl3_ssl passed 3 of 3 with ctest --repeat until-fail:3; no negative control, since the build fails with this PR's sources put back to main's (comment). The pull request's Windows CI job also passed.
  • Not compared with a console and not run with a title or homebrew.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5064337174 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12754). Originally posted by [Lindamea1](https://github.com/Lindamea1) on 2026-10-29T07:51:15Z. --- ### What On `main`, libSceLibreSsl3 exports these eight LibreSSL calls as stubs that throw ([`Export.cpp` L279-L307, L364-L367, L379-L382](https://github.com/boykopovar/AnyPS5/blob/5de1621ff13712e8b3a6d70a2487b008d9ee80c3/core/libs/prx/libSceLibreSsl3/Export.cpp#L279-L382)). This PR implements them with LibreSSL 4.0.0 behaviour, the version of the firmware module (`OpenSSL_version(0)` measured in [#8981](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214)). Names and NIDs are unchanged; the stubs are replaced by `src/Key.cpp`, `src/Ssl.cpp` and `src/X509.cpp`. `SSL_CTX_up_ref`, its `guest_libressl3_key_reference` case and the `LibreSsl3SslCtx` struct in `include/Key.hpp` come from #12578. PrivacyOnion closed that pull request unmerged on 31 October, together with 28 other pull requests of theirs and without a comment. No other open PR implements the call and the version bounds need the struct, so this PR keeps that commit under their authorship and no longer depends on #12578. Its code is unchanged by the rebase; only its debt lines were merged with `main`. This PR's own change is the last commit. It appends the `SSL_CTX` members the version bounds need to that struct, with their `static_assert` lines. | Export | NID | Behaviour | | --- | --- | --- | | `int SSL_up_ref(SSL*)` | `lHGGE+Ubycs` | adds 1 to the int32 count at offset 176; returns 1 if the new count is above 1, else 0 | | `int SSL_SESSION_up_ref(SSL_SESSION*)` | `MOISxAYBtc0` | the same, count at offset 184 | | `int X509_up_ref(X509*)` | `HrCznvA8AQk` | the same, count at offset 28 | | `int SSL_CTX_up_ref(SSL_CTX*)` | `AmNeqCMs2oU` | the same, count at offset 40 | | `int SSL_CTX_set_min_proto_version(SSL_CTX*, uint16_t)` | `NG+D00RsX1Y` | below | | `int SSL_CTX_set_max_proto_version(SSL_CTX*, uint16_t)` | `mF9+FT4UKSE` | below | | `size_t SSL_get_client_random(const SSL*, unsigned char*, size_t)` | `1sWluYvAfBM` | below | | `long X509_get_version(const X509*)` | `gg2cJQtaEns` | below | - Retains: the four retains use `Retain_nid_no_patch` of the merged `EVP_PKEY_up_ref` (#11165), moved from `src/Key.cpp` into `include/Reference.hpp` (inline; the overflow message no longer says "key"). `SSL_CTX_up_ref` stays in `src/Key.cpp`, next to the merged `EVP_PKEY_up_ref`, `EC_KEY_up_ref` and `RSA_up_ref`, which use the moved helper too. It adds atomically. In 4.0.0, `CRYPTO_add` is `CRYPTO_add_lock` ([`crypto.h` L207-L208](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/crypto.h#L207-L208)), which adds under the library's own pthread mutexes and calls no locking callbacks ([`crypto_lock.c` L24-L96](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/crypto_lock.c#L24-L96)). A null object and a count of `INT32_MAX` throw without changing memory, as in the merged retains: upstream dereferences the null object, and its signed add at `INT32_MAX` is undefined behaviour. BunnyFl's native run of the firmware module for #11165 showed that the module wraps `EVP_PKEY` and `EC_KEY` counts at `INT32_MAX` ([comment](https://github.com/boykopovar/AnyPS5/pull/11165#issuecomment-6405417095)); the SSL and X509 counts were not measured. - Version bounds follow `ssl_version_set_min` and `ssl_version_set_max` on `ctx->method`. Version 0 stores the method bound in `min_tls_version` (offset 128) or `max_tls_version` (130) and 0 in `min_proto_version` (132) or `max_proto_version` (134), and returns 1. For a DTLS method (any nonzero `dtls`), 0xFEFF and 0xFEFD map to TLS 1.1 and 1.2 and every other nonzero value returns 0. A TLS method takes the value as a plain number, so there a DTLS value, numerically above every TLS version, fails as a minimum and is lowered to the method maximum as a maximum. The call returns 0 when the method range is inverted, when the value and the stored opposite TLS bound are inverted, or when the two ranges do not meet. Otherwise a minimum below the method minimum is raised to it, or a maximum above the method maximum is lowered to it. For a DTLS method the result is converted back to the DTLS form, and the call returns 0 if it has none. Then both fields are stored and the call returns 1. A call that returns 0 writes nothing and queues no error. A null context or method throws; upstream dereferences both. - `SSL_get_client_random`: a null `out` returns 32 without reading the `SSL`. Otherwise it copies `min(32, max_out)` bytes from `s->s3->client_random` (`s3` at offset 56 of `SSL`, `client_random` at 40 of `SSL3_STATE`) and returns the count. A null `SSL` throws. A null `s3` throws only when there is something to copy, since a 0-byte copy reads nothing. - `X509_get_version` returns `ASN1_INTEGER_get(x->cert_info->version)` with the 4.0.0 rules, as a 64-bit value on every host, including Windows, where `long` is 32 bits. A null version gives 0. A negative length gives -1 with no error. A type other than 2 or 0x102 gives -1 and queues `ERR_PACK(13, 0xfff, 225)` (0x0DFFF0E1). Every byte is read, so leading zeros are accepted; a byte that would overflow 64 bits gives -1 with 0x0DFFF0DF (`ASN1_R_TOO_LARGE`), as does a positive value above `INT64_MAX`. A negative magnitude above 2^63 gives -1 with 0x0DFFF0E0 (`ASN1_R_TOO_SMALL`). A null certificate or `cert_info`, and null data with a positive length, throw. - Error queue: `X509_get_version` is the first libSceLibreSsl3 export that queues errors (the merged `d2i_ASN1_INTEGER` does not, [debt line 9](https://github.com/boykopovar/AnyPS5/blob/319621ba2e578bd053b0baba2877fe70520e0737/docs/dev/debt/libSceLibreSsl3.md?plain=1#L9)). It queues through `LibreSslPutError_nid_no_patch` into the libSceLibreSsl queue. On Linux and macOS, the `ERR_get_error` of a libSceLibreSsl3 importer resolves to libSceLibreSsl by dependency lookup and reads that queue. On Windows, a scoped import searches only the named module and libc ([`WindowsEntryStubBuilder.cpp` L171-L200](https://github.com/boykopovar/AnyPS5/blob/319621ba2e578bd053b0baba2877fe70520e0737/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp#L171-L200)), and the merged #12369 forwards libSceLibreSsl3's `ERR_get_error` to libSceLibreSsl ([`ParentExports.cpp` L234-L235](https://github.com/boykopovar/AnyPS5/blob/f7f03daf52f11191174b1be089a56e268390a707/core/libs/prx/libSceLibreSsl3/ParentExports.cpp#L234-L235)), so the importer reads that queue there too. #12369's debt entry lists that forwarding as unverified at runtime on Windows, and I have not run it. `include/Ssl.hpp` and `include/X509.hpp` declare the 4.0.0 prefixes of `SSL_METHOD`, `SSL`, `SSL3_STATE`, `SSL_SESSION`, `X509` and `X509_CINF`. The `SSL_CTX` prefix is `LibreSsl3SslCtx` in `include/Key.hpp`, extended to the version bounds at 128-135; the setters read its `const void*` `method` as `SSL_METHOD`. The fields are fixed-width, so the offsets also hold with the 32-bit `long` of MinGW, and every offset used has a `static_assert`. No constructor of these objects is implemented in either LibreSSL library (`SSL_CTX_new`, `SSL_new` and `d2i_X509` throw in libSceLibreSsl), so all of them are caller-owned here, as for the merged `EVP_PKEY_up_ref`. References, LibreSSL 4.0.0 (`libressl-v4.0.0`, [ce488b78e0bd](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a)): - `ssl_lib.c`: [`SSL_CTX_up_ref` L2239-L2243](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_lib.c#L2239-L2243), [`SSL_up_ref` L603-L608](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_lib.c#L603-L608), [`SSL_get_client_random` L3092-L3107](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_lib.c#L3092-L3107), [`SSL_CTX_set_min_proto_version` L3522-L3529](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_lib.c#L3522-L3529), [`SSL_CTX_set_max_proto_version` L3538-L3545](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_lib.c#L3538-L3545); `ssl_sess.c`: [`SSL_SESSION_up_ref` L883-L888](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_sess.c#L883-L888). - [`ssl_versions.c` L20-L125](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_versions.c#L20-L125): the DTLS and TLS version maps, `ssl_clamp_tls_version_range`, `ssl_version_set_min` and `ssl_version_set_max`. - `ssl_local.h` prefixes: [`ssl_method_st` L358-L364](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_local.h#L358-L364), [`ssl_session_st` L410-L437](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_local.h#L410-L437), [`ssl_ctx_st` L665-L709](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_local.h#L665-L709), [`ssl_st` L862-L910](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_local.h#L862-L910), [`ssl3_state_st` L1091-L1095](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_local.h#L1091-L1095). None of these prefixes has a conditional member. - Test fixtures: [`DTLS_client_method_data` L83-L88](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_methods.c#L83-L88) and [`TLS_client_method_data` L286-L291](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl_methods.c#L286-L291) of `ssl_methods.c`, [`SSL3_RANDOM_SIZE` (`ssl3.h` L235)](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/ssl3.h#L235), [`tls1.h` L167-L178](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/tls1.h#L167-L178), [`dtls1.h` L76-L77](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libssl/dtls1.h#L76-L77). - X509: [`X509_up_ref` (`x_all.c` L531-L536)](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/x509/x_all.c#L531-L536), [`X509_get_version` (`x509_set.c` L102-L107)](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/x509/x509_set.c#L102-L107), `x509_local.h` [`x509_cinf_st` L167-L168](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/x509/x509_local.h#L167-L168) and [`x509_st` L181-L186](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/x509/x509_local.h#L181-L186). - ASN.1 integers: `a_int.c` [`asn1_aint_get_uint64` L134-L155](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/a_int.c#L134-L155), [`asn1_aint_get_int64` L202-L225](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/a_int.c#L202-L225), [`ASN1_INTEGER_get_int64` L267-L287](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/a_int.c#L267-L287), [`ASN1_INTEGER_get` L308-L323](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/a_int.c#L308-L323); `asn1.h` [L92](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/asn1.h#L92) (`V_ASN1_NEG` 0x100), [L97-L98](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/asn1.h#L97-L98), [L1095](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/asn1.h#L1095), [L1097](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/asn1.h#L1097), [L1116](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/asn1.h#L1116); `err.h` [L160](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/err/err.h#L160), [L244](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/err/err.h#L244), [L271-L273](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/err/err.h#L271-L273). - LibreSSL 2.5.4 (`libressl-v2.5.4`, 2e2c61206), the version of libSceLibreSsl: its `X509` and `X509_CINF` prefixes are the same ([`x509.h` L238-L240, L268-L274](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/x509/x509.h#L238-L274)), and so is its `SSL3_STATE` prefix, with `client_random` at 40 ([`ssl3.h` L364-L368](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libssl/ssl3.h#L364-L368)), but its other SSL objects are laid out differently. The `SSL` and `SSL_SESSION` counts are at 168 and 160, and `SSL` keeps `s3` at 48 ([`ssl.h` L435-L461, L806-L853](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libssl/ssl.h#L435-L853)). `SSL_METHOD` starts with function pointers ([`ssl.h` L399-L406](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libssl/ssl.h#L399-L406)), and the method and `SSL_CTX` version bounds are in internal structs ([`ssl_locl.h` L377-L381](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libssl/ssl_locl.h#L377-L381), [L434-L436](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libssl/ssl_locl.h#L434-L436)). The code is written from the behaviour above; no upstream source is copied. Gaps, as three new entries in `docs/dev/debt/libSceLibreSsl3.md`: - `SSL_CTX_up_ref`, #12578's entry: the console layout and retain/release interoperability are unverified. The count at offset 40 follows 4.0.0, and null contexts and signed-counter overflow, undefined upstream, throw without changing memory. - The SSL offsets are inferred from 4.0.0 and not measured on the firmware module. A libSceLibreSsl3 importer reaches the 2.5.4 constructor stubs of libSceLibreSsl, by dependency lookup on Linux and macOS and through #12369's forwarding on Windows, and 2.5.4 lays out the `SSL_METHOD`, `SSL_CTX`, `SSL` and `SSL_SESSION` objects these calls read differently, so every SSL call here would misread a 2.5.4 object once those constructors are implemented. Signed-counter overflow at `INT32_MAX` and null objects, undefined upstream, throw without changing memory. - `X509_get_version` queues its errors in the libSceLibreSsl queue without file or line, while the firmware module keeps its own queue. The pending-export count in the same file goes from 80 to 72. `python3 tools/progress.py --module libSceLibreSsl3` goes from `libraries 12/270 (4.44%)` to `libraries 20/270 (7.41%)`. Firmware evidence: the SSL offsets (`SSL_CTX` 40 and 128-135, `SSL` 56 and 176, `SSL3_STATE` 40, `SSL_SESSION` 184) would be settled by a native run of the firmware `libSceLibreSsl3.sprx` on module-created objects, like BunnyFl's for #11165 ([comment](https://github.com/boykopovar/AnyPS5/pull/11165#issuecomment-6405417095)), for example `SSL_CTX_new(TLS_client_method())` followed by these calls. If review wants that first, the X509 pair can go in its own pull request: `src/X509.cpp`, `include/X509.hpp`, `include/Reference.hpp` with the `src/Key.cpp` change, the X509 cases of the test, the third debt entry, and its own lines in `CMakeLists.txt`, `Api.hpp`, `Export.cpp` and on line 5 of the debt file. No title is known to import these functions; this is coverage, and it unblocks nothing while the constructors throw. They are TLS client calls of code built against LibreSSL 2.9 or later. LibreSSL 2.7 and 2.8 already export the other seven, but define `X509_get_version` as a macro over `ASN1_INTEGER_get` ([2.8.0 `x509.h` L592](https://github.com/libressl/openbsd/blob/6b472be450b41d2399d1fd32bf31177c8c24f895/src/lib/libcrypto/x509/x509.h#L592)); 2.9.0 makes it a function ([`x509_set.c` L90-L94](https://github.com/libressl/openbsd/blob/a0045adda702d3bdf41fdaa2e4809700927e0c47/src/lib/libcrypto/x509/x509_set.c#L90-L94)). Overlap, checked at 15:36Z on 31 October with this branch against `main` 474030e584 and the 1,670 open PR heads fetched then: - `gh pr list --search` finds no other open PR with the eight names or their NIDs, apart from #12945, which carries this branch's commits, and a mention in a comment on #10960. - #12945 (`BIO_up_ref`) is stacked on this PR and names it on `Depends on`. It still carries this branch's older commits; replayed on this head, its own commit conflicts only in the debt file. - `tools/pr_overlap.py --branch` lists #10395, #10452, #10490, #10491, #10493, #11146, #12315 and #13897, none with a shared export. It could not compare #9970, #10461, #10557, #10631, #10960, #12928 and #13662, which conflict with `main`; `git diff` of each against its merge base has no changed line with the eight functions. It also lists this PR's previous head and #12945, which carry the same changes. Lines this branch shares with those it could compare: - the pending count on line 5 of the debt file (#10452, #10490, #10491, #10493, #12315, #13897); - the entries at lines 12-13 of that file (#10395, #11146, #12315, #13897); - the top of `src/Key.cpp` (#10395, #11146); - `Export.cpp` L354-L383 and `Api.hpp` L81-L86 (#10491, #10493); - `CMakeLists.txt` L67 and L69 (#12315). #9571 merges with this branch without a conflict. It gives `LibreSslPutError_nid_no_patch` an optional file and line, which the three-argument call here still compiles against. It also implements libSceLibreSsl3's `ERR_peek_last_error` on the libSceLibreSsl queue, so after it that call also sees `X509_get_version` errors, as `ERR_peek_error` already does. Proposed order: this PR before #12945; the others in any order. This PR needs nothing from those, and I rebase it onto each one that lands before it. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux (Gentoo, kernel 7.2.8, AMD Ryzen 7 9800X3D), GCC 16.2.1, Ninja, Release build with `BUILD_TESTING=ON`. Current head, `34728c573e`, on `main` 474030e584, with #12578's commit as `3e2103d0f3`: - This branch conflicted with `main` in `include/Key.hpp`, `src/Key.cpp`, `GuestLibreSsl3KeyReference.cpp` and the debt file after #13267 (`RSA_up_ref`), and #12578 was closed, so I rebased both commits onto `main`. Against the previous head `0ee29b8d1b`, the added and removed lines of both commits are unchanged outside `docs/`. The resolutions keep `RSA_up_ref` and `SSL_CTX_up_ref` side by side in those files and recount line 5 of the debt file. The rebase also drops the blank line that #12578's rebase left at the end of the debt file, and `SSL_CTX_up_ref`'s entry now cites `ssl_local.h` at the 4.0.0 commit its label names. #13659 (`EVP_CIPHER_key_length`, `EVP_CIPHER_iv_length`) landed in this library afterwards and rebased without a conflict. Its two exports came from the additional export inventory, so line 5 stays at 72. - CI test selection (the steps below): 648 of 3062 tests selected, 648/648 passed, none skipped, with `public_headers`, `guest_crypto_memcmp` and the seven `guest_libressl3_*` tests. - `python3 tools/check_test_registration.py build`: passed (1632 test executables, none built and not run). - `python3 tools/check_conventions.py --base 474030e584`, also with this title and description: passed. - NID-patched `build/core/libs/libs/libSceLibreSsl3.prx`: the eight NIDs are exported at the addresses of their `_nid_postfix` definitions. The inline helper is not among the library's 397 defined dynamic symbols. - MinGW-w64 syntax check (the command below) of `Export.cpp`, `Key.cpp`, `Ssl.cpp`, `X509.cpp`, `GuestLibreSsl3Ssl.cpp` and `GuestLibreSsl3KeyReference.cpp`: no diagnostics. This is not a Windows build or run. - The pull request's Windows CI job on this head, merged onto `main` 474030e584: 2524 tests, 0 failed, 894 skipped. `guest_libressl3_ssl`, `guest_libressl3_key_reference`, `guest_crypto_memcmp` and the other five `guest_libressl3_*` tests passed. Previous head, `0ee29b8d1b`, on #12578's `ced92ec4ed` (`main` f7f03daf5), with the same code and tests: - The pull request's Windows CI job passed: it merges the head onto `main` f91137142e and builds everything, but it runs none of the LibreSSL tests. - CI test selection (the steps below): 629 of 2977 tests selected, 629/629 passed, none skipped, with all five libSceLibreSsl3 tests (`guest_crypto_memcmp` among them). This ran before the last amend, which changed only the debt file. - `python3 tools/check_test_registration.py build`: passed (1579 test executables, none built and not run). - `python3 tools/check_conventions.py --base upstream/main`, also with that head's title and description: passed. - NID-patched `build/core/libs/libs/libSceLibreSsl3.prx`: the seven NIDs, and #12578's `SSL_CTX_up_ref`, are exported at the addresses of their `_nid_postfix` definitions. The inline helper is not among the library's 397 defined dynamic symbols. - MinGW-w64 syntax check (the command below) of `Export.cpp`, `Key.cpp`, `Ssl.cpp`, `X509.cpp`, `GuestLibreSsl3Ssl.cpp` and `GuestLibreSsl3KeyReference.cpp`: no warnings. This is not a Windows build or run. Earlier head, `48bc460a1d` (#12578's `00b24c75b4` and this commit, on `main` 319621ba2), with the same code and tests: - `ctest --test-dir build -R 'guest_libressl3_ssl|guest_libressl3_key_reference|public_headers' --output-on-failure -j8 --timeout 300`: 67/67 passed (`public_headers` and its 64 compile batches, `guest_libressl3_key_reference` with #12578's `SSL_CTX_up_ref` case on the extended struct and the moved helper, and `guest_libressl3_ssl`). - `python3 tools/check_test_registration.py build`: passed (1516 test executables, none built and not run). - CI test selection (`tools/select_tests.py dump`, `select` with the changed files, `ninja -f select.ninja selected`, `select_tests.py ctest`): 491 of 2746 tests selected, 491/491 passed. - `python3 tools/check_conventions.py --base 319621ba2`, also with that head's title and description: passed. - Merged into `main` 3a2b91981 without conflicts and rebuilt: `ctest -R 'guest_libressl3|public_headers'`, 69/69 passed; CI test selection on the merge, 580 of 2858 tests selected, all five libSceLibreSsl3 tests (with `guest_crypto_memcmp`) among them, 580/580 passed. - The new `guest_libressl3_ssl` test (`core/libs/tests/GuestLibreSsl3Ssl.cpp`): - Each retain on a guarded object with counts -2, 0, 1, 37 and `INT32_MAX - 1`: the return value, and only the four count bytes at the stated offset change. Then 4 threads × 1000 retains give exactly 4001. - 44 version-bound cases on TLS- and DTLS-client-like methods. They cover version 0, raising and lowering to the method bounds, each failing condition of the clamp on its own, DTLS values on a TLS method (both setters, 0xFEFF and 0xFEFD) and TLS values on a DTLS method, a clamped result with no DTLS form, a `dtls` flag of -1, and the opposite bound read from the TLS field. Each case checks the return value and every byte of the guarded context, so a failing call writes nothing. - `SSL_get_client_random` with a null output (also with a null `SSL`) and with limits 0, 1, 10, 31, 32, 33, 64 and `SIZE_MAX`: the count, the bytes taken from offset 40 of the state, and an untouched tail. A 0-byte copy with a null `s3` returns 0. - `X509_get_version` on 22 integers (null, empty, negative length, leading zeros, big-endian order, `INT64_MAX`, `INT64_MIN`, negative zero, wrong types, each overflow): the value, the exact error code and that nothing else is queued. - It has no assertion that only checks for a throw. - The test rejects wrong implementations. I compiled it with 33 mutated copies of `Ssl.cpp`, `X509.cpp` and `Reference.hpp` against the built libSceLibreSsl, libc and libkernel, and every one aborted. The mutants dropped each clamp condition, read the opposite bound from the proto field, mapped or rejected DTLS values on a TLS method, used a wrong count field, copied the server random, ignored `max_out`, added the 2.5.4 eight-byte limit, changed an error reason or the library, and so on. The unmodified sources passed. - NID-patched `build/core/libs/libs/libSceLibreSsl3.prx`: the seven NIDs, and #12578's `SSL_CTX_up_ref`, are exported at the addresses of their `_nid_postfix` definitions. The library has 88 defined dynamic symbols, the 87 exports and `DummyFunction_nid_no_patch`, so the inline helper is not exported. - MinGW-w64 syntax check of the changed files only (`x86_64-w64-mingw32-g++` 16.2.0, `-std=gnu++20 -fsyntax-only -Wall -Wextra -D_WIN32_WINNT=0x0602 -I core/libs -I core/libs/nid/include -I core/libs/prx/libkernel`) on `Export.cpp`, `Key.cpp`, `Ssl.cpp`, `X509.cpp`, `GuestLibreSsl3Ssl.cpp` and `GuestLibreSsl3KeyReference.cpp`, and on `Key.hpp`, `Ssl.hpp`, `X509.hpp`, `Reference.hpp` and `Api.hpp` each included alone with `-Werror`: no warnings. This is not a Windows build or run. - Windows, by @JJenkx: that head merged onto `main` edf6804cf5, Windows 11 Enterprise, WinLibs GCC 15.2.0 UCRT, Ninja, Release, `BUILD_TESTING=ON`. `guest_libressl3_key_reference` and `guest_libressl3_ssl` passed 3 of 3 with `ctest --repeat until-fail:3`; no negative control, since the build fails with this PR's sources put back to `main`'s ([comment](https://github.com/boykopovar/AnyPS5/pull/12754#issuecomment-6491153089)). The pull request's Windows CI job also passed. - Not compared with a console and not run with a title or homebrew. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12754
No description provided.