Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
AdnanKhan-ak47:feat/libressl-ssl-lifecycle
Nov 18, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
AdnanKhan-ak47:feat/libressl-ssl-lifecycle

Conversation

@AdnanKhan-ak47

@AdnanKhan-ak47 AdnanKhan-ak47 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

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 #12729 (comment). 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

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Progress report (259949a → 44b9833)

📈 System libraries: 42.37% (+0.01%, +7 implemented functions)

✅ 7 implemented
Library Name
libSceLibreSsl DTLSv1_client_method_nid_postfix
libSceLibreSsl DTLSv1_server_method_nid_postfix
libSceLibreSsl SSL_get_fd_nid_postfix
libSceLibreSsl SSL_set_fd_nid_postfix
libSceLibreSsl SSLv23_client_method_nid_postfix
libSceLibreSsl SSLv23_server_method_nid_postfix
libSceLibreSsl TLS_client_method_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libSceLibreSsl 302/473 (63.85%) 309/473 (65.33%) +1.48%

@AdnanKhan-ak47

Copy link
Copy Markdown
Contributor Author

Two follow-ups, both already merged into the branch:

  • main 2960e9a6e7 merged in. The only conflict was the pending-export count in docs/dev/debt/libSceLibreSsl.md, now 267 to match Export.cpp. The needs rebase label dates from before that merge, and the conflict-label workflow clears it on its next run.
  • f7e5b79806 gives the 38 APS5_VABI method-slot and socket-BIO callbacks in SslMethod.cpp and SslBio.cpp the _nid_no_patch suffix, as docs/dev/PROGRESS.md asks for internal helpers. The first progress report had counted them as library functions (29 new declared, AlwaysOk and TlsDefaultTimeout among the implemented ones). Nothing else changed: with the suffix stripped and whitespace normalised, both files are identical to the previous head. With the helpers excluded, tools/progress.py --module libSceLibreSsl on this head reports no new declared names and no new behaviourally empty ones. SSL_CTX_free, SSL_new and SSL_free stay pending in the count because each reaches NotImplemented_nid_no_patch on a path the debt entry lists (sessions, certificates and keys, live cipher contexts). That is accurate until those paths exist.

Re-run on Arch Linux x86-64 at this head: ctest -R libressl 37/37, public_headers 65/65, guest_libressl_ssl_lifecycle 20/20.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 11146 core/libs/prx/libSceLibreSsl/Export.cpp L233-252
PR 11176 core/libs/prx/libSceLibreSsl/Export.cpp L346-360
PR 13285 core/libs/prx/libSceLibreSsl/src/X509Store.cpp L1
PR 15278 core/libs/prx/libSceLibreSsl/Export.cpp L498-507
PR 21701 core/libs/prx/libSceLibreSsl/src/X509Store.cpp L1

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 11466 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 15279 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md
PR 15280 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
docs/dev/debt/libSceLibreSsl.md

@JJenkx

JJenkx commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Windows run of this head (f7e5b79806), merged onto main edf6804cf5 (clean merge, local only). Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, no Vulkan driver visible.

  • This PR: all 5 ctest entries of this PR's test executables (guest_libressl_ssl, guest_libressl_ssl_ctx, guest_libressl_ssl_ctx_fields, guest_libressl_ssl_lifecycle, guest_libressl_ssl_settings and 0 more) pass 3 of 3 (ctest --repeat until-fail:3).
  • Negative control: not possible. With this PR's source files (core/libs/prx/libSceLibreSsl/Export.cpp, core/libs/prx/libSceLibreSsl/include/Api.hpp, core/libs/prx/libSceLibreSsl/include/SslInternal.hpp, core/libs/prx/libSceLibreSsl/src/ExData.cpp, core/libs/prx/libSceLibreSsl/src/Ssl.cpp, core/libs/prx/libSceLibreSsl/src/SslBio.cpp and 4 more) put back to main's version, and the ones it adds removed, the build fails against the rest of the PR, so there is no source-only control.

AI-assisted: yes (Claude Code).

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@AdnanKhan-ak47 Three changes on f7e5b79:

  • Drop the assertions that valid unimplemented method slots and SSL_new on a DTLS context must throw (GuestLibreSslSslLifecycle.cpp:208-217,373-381). Keep the positive lifecycle/layout/refcount/fd cases and malformed-input checks.
  • Preserve the complete original LibreSSL/OpenSSL/SSLeay copyright and licence notices at the top of the files adapted from ssl_lib.c, the method files and bss_sock.c; identify the exact source versions used.
  • Make LibreSslFreeExData teardown allocation-safe. Its bad_alloc catch currently returns before callbacks and freeing the stack, but SSL_free/SSL_CTX_free then free the owner. Preserve cleanup under allocation failure and add a regression for it.

@adriadam10 we'll merge this once the requests above are addressed, #11138/#11143 below it have landed and exact-head CI is green, unless you'd rather merge it yourself.

@AdnanKhan-ak47

Copy link
Copy Markdown
Contributor Author

@oneandonlydean all three are in, head ed9ae73cf6:

  • Throw assertions (1382db3e56): removed the valid-but-unimplemented method-slot block and the DTLS SSL_new refusal test. The positive lifecycle, layout, refcount and fd cases and the malformed-input checks stay.

  • Notices (29510c46f1): each adapted file starts with the complete original notices, verbatim from the libressl-2.5.4.tar.gz release (sha256 107a5b52…336efa, the same release as openbsd mirror commit 2e2c612066), with each source's $OpenBSD id and what was taken from it.

    • SslLifecycle.cpp: ssl_lib.c, ssl_cert.c
    • SslMethod.cpp: t1_clnt.c, t1_srvr.c, d1_clnt.c, d1_srvr.c, t1_lib.c, d1_lib.c, s3_lib.c, ssl_err.c
    • SslBio.cpp: bss_sock.c, bio_lib.c

    The notices kept are SSLeay, OpenSSL and Sun ECC, plus Nokia where ssl_lib.c/s3_lib.c carry it; identical blocks appear once.

  • ex_data teardown (ed9ae73cf6): LibreSslFreeExData no longer copies the registered-function vector. It reads the count and fetches each entry under the lock, as LibreSSL's CRYPTO_free_ex_data falls back to per index, so allocation failure can't skip the callbacks or the stack free. TestExDataTeardownUnderHostAllocationFailure makes host operator new throw through SSL_free, SSL_CTX_free and a direct call. With the old ExData.cpp it aborts because the free callbacks never ran; with the change it passes.

Tested on macOS 27.2 (Apple M4), x86-64 under Rosetta, with libSceLibreSsl built on the macOS PR set from #1: the lifecycle and ex_data entries pass, 0.15 s and 0.13 s cold. The Linux box I normally use is offline, so this head has no Linux run of mine yet; @JJenkx's Windows run was on the previous head. check_conventions.py: 0 errors.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 30, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 30, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 31, 2026

@CnRJay CnRJay left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 8, 2026
@AdnanKhan-ak47
AdnanKhan-ak47 force-pushed the feat/libressl-ssl-lifecycle branch from 8a5dd67 to 731ce9e Compare November 10, 2026 04:26
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 10, 2026
@AdnanKhan-ak47
AdnanKhan-ak47 force-pushed the feat/libressl-ssl-lifecycle branch from 731ce9e to 230a460 Compare November 11, 2026 16:45
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 11, 2026
@AdnanKhan-ak47
AdnanKhan-ak47 force-pushed the feat/libressl-ssl-lifecycle branch from 230a460 to ace8525 Compare November 12, 2026 03:59
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 12, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 13, 2026
@AdnanKhan-ak47
AdnanKhan-ak47 force-pushed the feat/libressl-ssl-lifecycle branch from ace8525 to 784499a Compare November 16, 2026 05:59
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 16, 2026
…SL 2.5.4 method objects

Adds SSL_CTX_new, SSL_CTX_free, SSL_new, SSL_free, SSL_set_fd and SSL_get_fd and the five
method getters the PS5 module exports (SSLv23_client_method, SSLv23_server_method,
TLS_client_method, DTLSv1_client_method, DTLSv1_server_method). The static SSL_METHOD objects
follow t1_meth.c, t1_clnt.c, t1_srvr.c and d1_*.c; SSL_new inherits the SSL_CTX defaults as
ssl_lib.c does; SSL_set_fd creates a socket BIO object as bss_sock.c does.
@AdnanKhan-ak47
AdnanKhan-ak47 force-pushed the feat/libressl-ssl-lifecycle branch from 784499a to 1bea86b Compare November 17, 2026 08:05
@adriadam10 adriadam10 removed their assignment Nov 17, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 17, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 17, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 18, 2026
@oneandonlydean
oneandonlydean merged commit e724cf2 into boykopovar:main Nov 18, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants