feat(libSceLibreSsl): implement the method getters and SSL_CTX and SSL lifecycle #12729

Merged
AdnanKhan-ak47 merged 3 commits from upstream-pr/12729 into main 2026-11-18 09:34:42 +00:00
AdnanKhan-ak47 commented 2026-10-29 05:51:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AdnanKhan-ak47 on 2026-10-29T05:51:56Z.


What

Implements the LibreSSL 2.5.4 method getters and the SSL_CTX/SSL lifecycle in libSceLibreSsl, on top of the SSL_CTX layout from #11138 and the SSL layout from #11143, both now in main. These are the objects SSL_connect/SSL_read/SSL_write need; those follow in a separate PR.

Since #10481, #10483, #11138 and #11143 landed, the branch is one commit, 1bea86bbe7, plus two merges of main: 11 files, +2,319/−70 (581 of them the test). It squashes the original change (a5119c3e7d), the review fixes and the adaptation to main's memory BIO: the SSL struct is LibreSslSsl, as in main, and the socket BIO uses main's LibreSsl::Bio types and is released through its BIO_free.

Exports (11): SSLv23_client_method, SSLv23_server_method, TLS_client_method, DTLSv1_client_method, DTLSv1_server_method, SSL_CTX_new, SSL_CTX_free, SSL_new, SSL_free, SSL_set_fd, SSL_get_fd.

  • Methods (src/SslMethod.cpp): four static guest SSL_METHOD objects in the 2.5.4 public layout (48 bytes) with their internal blocks: TLS client and server (version 0x0303, range 0x0301-0x0303) and DTLS client and server (0xFEFF). SSLv23_client_method returns the same object as TLS_client_method, as ssl/methods.c does. ssl_new, ssl_clear and ssl_free allocate and free the 144-byte ssl3_state and 1040-byte internal block; ssl_accept/ssl_connect on the wrong side point at an ssl_undefined_function equivalent that queues error 66; ssl_renegotiate_check is ssl_ok; get_timeout returns 7200; ssl3_enc points at a 16-byte block with flags 0x17 (TLS) or 0x01 (DTLS). Slots a guest cannot legitimately call (dispatch_alert, the cipher getters, enc, get_message, read_bytes, write_bytes, get_ssl_method) throw on call, and so do connect, accept, read, peek, write, shutdown and pending until the I/O PR wires them.
  • Lifecycle (src/SslLifecycle.cpp): SSL_CTX_new and SSL_new follow ssl_lib.c: defaults, a reference on the context, a fresh X509_VERIFY_PARAM inherited from the context, the ALPN, EC point format and supported group lists copied, ticket keys from host entropy, and the ex_data new callbacks run (src/ExData.cpp gains the hooks). SSL_CTX_free/SSL_free drop references, run the ex_data free callbacks with the values SSL_CTX_set_ex_data/SSL_set_ex_data stored, and release everything the object owns, the ex_data stack included. SSL_new with a DTLS method throws and leaves the context's reference count unchanged, because no DTLS1_STATE is modelled.
  • fd (src/SslBio.cpp): SSL_set_fd creates a socket BIO (104 bytes, static BIO_METHOD type 0x505, init 1, BIO_NOCLOSE, num = fd) and accepts any integer, as bss_sock.c does. SSL_get_fd walks the read BIO and returns -1 when there is none. BIO_new_socket is not exported here because SSL_set_bio, BIO_free and BIO_ctrl are still stubs.
  • Throws instead of guessing, each recorded in docs/dev/debt/libSceLibreSsl.md: inheriting DH/ECDH parameters, a certificate or key, or verify policies/hosts/e-mail/IP in SSL_new; freeing an object that holds a session, the session table, stored certificates or keys, a buffering BIO, an engine, SRTP profiles or live cipher/hash contexts; closing a BIO that owns its descriptor; SSLerror for handshake states other than 0/BEFORE/OK. cipher_list, cipher_list_by_id, the session table and the CERT digests are left null. The internal-block offsets and sizes are from LibreSSL 2.5.4 built for x86-64, not measured on a console.

Tested

The review changes and their test results are in https://github.com/boykopovar/AnyPS5/pull/12729#issuecomment-6493189345. The paragraph below describes the first version.

Head 8a5dd67984 (on main e64c194c43): macOS 27.2 (M4), x86-64 build under Rosetta, with the LibreSSL sources on the macOS integration tree because main alone does not build on macOS. The LibreSSL tests pass 54/57, including guest_libressl_ssl_lifecycle; the other 3 (guest_libressl_protocol_nid, guest_libressl_bio_patched, guest_libressl_cmac_patched) fail identically on main alone there, where the macOS NID patcher rejects _sceNetRecv. Linux not rerun for this update. tools/check_conventions.py --base origin/main passes.

Current head 44b98337f3 merges main ec7878b6f9 on top of oneandonlydean's merge of main (693c81aee5), which resolved the Export.cpp stubs beside RSA_generate_key_ex (#18396). The only conflict this time was in docs/dev/debt/changes/fix-libc-ld80-inverse-hyperbolic.md, a main file that 693c81aee5 had folded the hypotl fragment into before #22579 did it on main; it now matches main, so this PR's diff is again exactly its own 11 files, line for line as before.

1bea86bbe7 is the same change rebased without conflicts onto main 050cdf95ad, which has the fix for the patched_libraries failure of the previous Windows run (libSceVideoOut.prx and libSceGnmDriver.prx failing to load with error 127; main 0694c4c, the base of that run, failed it the same way, and the revert in #21466 fixed it).

The rebase before it, 784499ab90 onto main 55bfc4aad3: main had implemented SSL_CTX_get_ex_data (#15281) beside this PR's SSL_CTX_free stub, and MarrasFederico's SSL_CTX_set_cipher_list (#17813) now installs real cipher lists into a context. Changes:

  • SSL_CTX_free already releases both lists with the guest allocator their stacks use. New test step TestInstalledCipherListsReleased sets two lists and frees the context under the tracking heap; with the cipher_list_by_id free removed it fails the per-step leak check, restored it passes.
  • SSL_CTX_new still leaves both lists null. Its debt entry now says LibreSSL installs ALL:!aNULL:!eNULL:!SSLv2 there, instead of saying no export builds a cipher table.
  • The ex_data debt line now says only SSL_get_ex_data throws.
  • main had reworded the SSL_get_SSL_CTX and SSL_set_verify debt entries this PR edits; git's merge kept both copies and re-added an old OCSP_RESPONSE_free line without a conflict. I removed the copies, so the debt diff touches the same entries as before.

Earlier rebases: ace8525a57 onto main c959605c79 (only stubs beside SSL_CTX_add_client_CA, #16688, and SSL_get_peer_cert_chain, #16694; no code change).

The previous rebase, 230a4607d6 onto main 016db0480e: main had gained MarrasFederico's SSL_CTX_set_ex_data and SSL_set_ex_data (#16678, #16680) and SSL_CTX_check_private_key (#16682), whose stubs sat beside this PR's; both sides are kept. Since SSL_set_ex_data can now fill the ex_data stack this PR's SSL_free releases, three small changes:

  • SSL_new/SSL_free use main's new LibreSslSslInternal::exData field instead of a raw offset (the same 272).
  • New test step TestStoredExDataReachesRelease: it stores a value with each setter, frees, and requires the free callbacks to get that value; the tracking heap checks the stack is freed. With the free path changed to pass null, the test fails at the callback check; restored, it passes.
  • The ex_data debt line no longer says SSL_set_ex_data throws.

On macOS 27.2 (M4, x86-64 under Rosetta), 44b98337f3 with #12730 on top, on main ec7878b6f9: 177 of the 181 LibreSSL tests pass, guest_libressl_ssl_lifecycle included. guest_libressl_protocol, guest_libressl_protocol_nid, guest_libressl3_ecdsa_sig and guest_libressl3_rsa_getters are not built there, because libcurl.prx does not link on that Mac.

Arch Linux x86-64, GCC 16.2.1, branch head f7e5b79806 (main 2960e9a6e7 merged in): ctest --test-dir build -R libressl 37/37 pass; the new guest_libressl_ssl_lifecycle 20/20 in a loop; ctest -R ^public_headers 65/65. The new test runs against a tracking guest heap and fails on any leak. It covers method identity and version fields, the undefined slots queueing error 66, the throwing slots, context defaults and distinct ticket keys, reference counts (through the ex_data free callbacks), SSL defaults copied at SSL_new, the DTLS refusal and its unwind, the list copies, and the fd round-trip including -1, 0 and INT_MAX. Ten deliberate mutations (wrong reference counts, flags, skipped frees and callbacks, among others) each make it abort. tools/check_conventions.py --base origin/main passes. Not tested on a console or with a title.

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 (#11138 and #11143 are merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5063037141 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12729). Originally posted by [AdnanKhan-ak47](https://github.com/AdnanKhan-ak47) on 2026-10-29T05:51:56Z. --- ### What Implements the LibreSSL 2.5.4 method getters and the `SSL_CTX`/`SSL` lifecycle in libSceLibreSsl, on top of the `SSL_CTX` layout from #11138 and the `SSL` layout from #11143, both now in `main`. These are the objects `SSL_connect`/`SSL_read`/`SSL_write` need; those follow in a separate PR. Since #10481, #10483, #11138 and #11143 landed, the branch is one commit, `1bea86bbe7`, plus two merges of `main`: 11 files, +2,319/−70 (581 of them the test). It squashes the original change (`a5119c3e7d`), the review fixes and the adaptation to `main`'s memory BIO: the `SSL` struct is `LibreSslSsl`, as in `main`, and the socket `BIO` uses `main`'s `LibreSsl::Bio` types and is released through its `BIO_free`. Exports (11): `SSLv23_client_method`, `SSLv23_server_method`, `TLS_client_method`, `DTLSv1_client_method`, `DTLSv1_server_method`, `SSL_CTX_new`, `SSL_CTX_free`, `SSL_new`, `SSL_free`, `SSL_set_fd`, `SSL_get_fd`. - **Methods** (`src/SslMethod.cpp`): four static guest `SSL_METHOD` objects in the 2.5.4 public layout (48 bytes) with their internal blocks: TLS client and server (version 0x0303, range 0x0301-0x0303) and DTLS client and server (0xFEFF). `SSLv23_client_method` returns the same object as `TLS_client_method`, as `ssl/methods.c` does. `ssl_new`, `ssl_clear` and `ssl_free` allocate and free the 144-byte `ssl3_state` and 1040-byte internal block; `ssl_accept`/`ssl_connect` on the wrong side point at an `ssl_undefined_function` equivalent that queues error 66; `ssl_renegotiate_check` is `ssl_ok`; `get_timeout` returns 7200; `ssl3_enc` points at a 16-byte block with flags 0x17 (TLS) or 0x01 (DTLS). Slots a guest cannot legitimately call (`dispatch_alert`, the cipher getters, `enc`, `get_message`, `read_bytes`, `write_bytes`, `get_ssl_method`) throw on call, and so do connect, accept, read, peek, write, shutdown and pending until the I/O PR wires them. - **Lifecycle** (`src/SslLifecycle.cpp`): `SSL_CTX_new` and `SSL_new` follow `ssl_lib.c`: defaults, a reference on the context, a fresh `X509_VERIFY_PARAM` inherited from the context, the ALPN, EC point format and supported group lists copied, ticket keys from host entropy, and the ex_data new callbacks run (`src/ExData.cpp` gains the hooks). `SSL_CTX_free`/`SSL_free` drop references, run the ex_data free callbacks with the values `SSL_CTX_set_ex_data`/`SSL_set_ex_data` stored, and release everything the object owns, the ex_data stack included. `SSL_new` with a DTLS method throws and leaves the context's reference count unchanged, because no `DTLS1_STATE` is modelled. - **fd** (`src/SslBio.cpp`): `SSL_set_fd` creates a socket BIO (104 bytes, static `BIO_METHOD` type 0x505, init 1, `BIO_NOCLOSE`, num = fd) and accepts any integer, as `bss_sock.c` does. `SSL_get_fd` walks the read BIO and returns -1 when there is none. `BIO_new_socket` is not exported here because `SSL_set_bio`, `BIO_free` and `BIO_ctrl` are still stubs. - **Throws instead of guessing**, each recorded in `docs/dev/debt/libSceLibreSsl.md`: inheriting DH/ECDH parameters, a certificate or key, or verify policies/hosts/e-mail/IP in `SSL_new`; freeing an object that holds a session, the session table, stored certificates or keys, a buffering BIO, an engine, SRTP profiles or live cipher/hash contexts; closing a BIO that owns its descriptor; `SSLerror` for handshake states other than 0/BEFORE/OK. `cipher_list`, `cipher_list_by_id`, the session table and the `CERT` digests are left null. The internal-block offsets and sizes are from LibreSSL 2.5.4 built for x86-64, not measured on a console. ### Tested The review changes and their test results are in https://github.com/boykopovar/AnyPS5/pull/12729#issuecomment-6493189345. The paragraph below describes the first version. Head `8a5dd67984` (on `main` `e64c194c43`): macOS 27.2 (M4), x86-64 build under Rosetta, with the LibreSSL sources on the macOS integration tree because `main` alone does not build on macOS. The LibreSSL tests pass 54/57, including `guest_libressl_ssl_lifecycle`; the other 3 (`guest_libressl_protocol_nid`, `guest_libressl_bio_patched`, `guest_libressl_cmac_patched`) fail identically on `main` alone there, where the macOS NID patcher rejects `_sceNetRecv`. Linux not rerun for this update. `tools/check_conventions.py --base origin/main` passes. Current head `44b98337f3` merges `main` `ec7878b6f9` on top of oneandonlydean's merge of `main` (`693c81aee5`), which resolved the `Export.cpp` stubs beside `RSA_generate_key_ex` (#18396). The only conflict this time was in `docs/dev/debt/changes/fix-libc-ld80-inverse-hyperbolic.md`, a `main` file that `693c81aee5` had folded the `hypotl` fragment into before #22579 did it on `main`; it now matches `main`, so this PR's diff is again exactly its own 11 files, line for line as before. `1bea86bbe7` is the same change rebased without conflicts onto `main` `050cdf95ad`, which has the fix for the `patched_libraries` failure of the previous Windows run (`libSceVideoOut.prx` and `libSceGnmDriver.prx` failing to load with error 127; `main` `0694c4c`, the base of that run, failed it the same way, and the revert in #21466 fixed it). The rebase before it, `784499ab90` onto `main` `55bfc4aad3`: `main` had implemented `SSL_CTX_get_ex_data` (#15281) beside this PR's `SSL_CTX_free` stub, and MarrasFederico's `SSL_CTX_set_cipher_list` (#17813) now installs real cipher lists into a context. Changes: - `SSL_CTX_free` already releases both lists with the guest allocator their stacks use. New test step `TestInstalledCipherListsReleased` sets two lists and frees the context under the tracking heap; with the `cipher_list_by_id` free removed it fails the per-step leak check, restored it passes. - `SSL_CTX_new` still leaves both lists null. Its debt entry now says LibreSSL installs `ALL:!aNULL:!eNULL:!SSLv2` there, instead of saying no export builds a cipher table. - The ex_data debt line now says only `SSL_get_ex_data` throws. - `main` had reworded the `SSL_get_SSL_CTX` and `SSL_set_verify` debt entries this PR edits; git's merge kept both copies and re-added an old `OCSP_RESPONSE_free` line without a conflict. I removed the copies, so the debt diff touches the same entries as before. Earlier rebases: `ace8525a57` onto `main` `c959605c79` (only stubs beside `SSL_CTX_add_client_CA`, #16688, and `SSL_get_peer_cert_chain`, #16694; no code change). The previous rebase, `230a4607d6` onto `main` `016db0480e`: `main` had gained MarrasFederico's `SSL_CTX_set_ex_data` and `SSL_set_ex_data` (#16678, #16680) and `SSL_CTX_check_private_key` (#16682), whose stubs sat beside this PR's; both sides are kept. Since `SSL_set_ex_data` can now fill the `ex_data` stack this PR's `SSL_free` releases, three small changes: - `SSL_new`/`SSL_free` use `main`'s new `LibreSslSslInternal::exData` field instead of a raw offset (the same 272). - New test step `TestStoredExDataReachesRelease`: it stores a value with each setter, frees, and requires the free callbacks to get that value; the tracking heap checks the stack is freed. With the free path changed to pass null, the test fails at the callback check; restored, it passes. - The ex_data debt line no longer says `SSL_set_ex_data` throws. On macOS 27.2 (M4, x86-64 under Rosetta), `44b98337f3` with #12730 on top, on `main` `ec7878b6f9`: 177 of the 181 LibreSSL tests pass, `guest_libressl_ssl_lifecycle` included. `guest_libressl_protocol`, `guest_libressl_protocol_nid`, `guest_libressl3_ecdsa_sig` and `guest_libressl3_rsa_getters` are not built there, because `libcurl.prx` does not link on that Mac. Arch Linux x86-64, GCC 16.2.1, branch head `f7e5b79806` (`main` `2960e9a6e7` merged in): `ctest --test-dir build -R libressl` 37/37 pass; the new `guest_libressl_ssl_lifecycle` 20/20 in a loop; `ctest -R ^public_headers` 65/65. The new test runs against a tracking guest heap and fails on any leak. It covers method identity and version fields, the undefined slots queueing error 66, the throwing slots, context defaults and distinct ticket keys, reference counts (through the ex_data free callbacks), SSL defaults copied at `SSL_new`, the DTLS refusal and its unwind, the list copies, and the fd round-trip including -1, 0 and `INT_MAX`. Ten deliberate mutations (wrong reference counts, flags, skipped frees and callbacks, among others) each make it abort. `tools/check_conventions.py --base origin/main` passes. Not tested on a console or with a title. ### 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 (#11138 and #11143 are merged) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
CnRJay commented 2026-11-01 19:54:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:54:01Z.

Review: COMMENTED


It is also +4.2k lines that cannot do anything on its own: there is no handshake or I/O until #12730 (+8.7k, cumulative) lands, so the first point at which a title could benefit is the second PR. Is there a reason these are not one reviewable unit?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729937037 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12729#pullrequestreview-5729937037). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:54:01Z. **Review:** COMMENTED --- It is also +4.2k lines that cannot do anything on its own: there is no handshake or I/O until #12730 (+8.7k, cumulative) lands, so the first point at which a title could benefit is the second PR. Is there a reason these are not one reviewable unit?
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!12729
No description provided.