feat(libSceLibreSsl): implement SSL_set_SSL_CTX #23088

Merged
Callimann8 merged 2 commits from upstream-pr/23088 into main 2026-11-26 17:55:37 +00:00
Callimann8 commented 2026-11-18 18:05:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Callimann8 on 2026-11-18T18:05:26Z.


What

Implements SSL_set_SSL_CTX in libSceLibreSsl (libSceLibreSsl3 reaches it through its parent forward WXg9MX8BXPg). The export was a NotImplemented stub. Servers call it from the SNI callback to switch a connection to another context.

  • SSL_set_SSL_CTX(SSL*, SSL_CTX*) sits next to SSL_free in src/SslLifecycle.cpp and follows LibreSSL 2.5.4's SSL_set_SSL_CTX:
    • the same context returns at once with nothing changed;
    • a null context means the connection's initial context;
    • the connection gets a copy of the new context's certificate holder (the existing DuplicateCertificate, the ssl_cert_dup port SSL_new uses), the negotiated digest of each of the 7 key slots is copied over from the old holder, and the old holder is freed;
    • the new context gains a reference, the old one is released through SSL_CTX_free, and the new context is returned.
  • Api.hpp declares the export with its real signature instead of (void).
  • A null connection, or a missing initial context or context certificate holder, throws where LibreSSL faults. Where LibreSSL dereferences a failed ssl_cert_dup, the digest copy is skipped and the connection is left without a certificate holder.
  • Debt fragment docs/dev/debt/changes/feat-libressl-ssl-set-ssl-ctx.md.

Certificate holders that carry a certificate or private key still hit the existing NotImplemented in DuplicateCertificate, as SSL_new does today.

Tested

Linux (Ubuntu, GCC), unit test only; nothing checked on a console or in a title.

TestSetSslCtx in core/libs/tests/GuestLibreSslSslLifecycle.cpp checks the null connection throw, that setting the current context changes nothing, that switching to another context moves the reference counts, keeps initialCtx, installs a fresh certificate holder and carries a negotiated digest over, that a null context goes back to the initial context, and that both contexts are released at the end. The guest-heap tracker's live.empty() check catches leaks and double frees.

ctest -R guest_libressl_ssl_lifecycle
100% tests passed, 0 tests failed out of 1

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5381417265 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23088). Originally posted by [Callimann8](https://github.com/Callimann8) on 2026-11-18T18:05:26Z. --- ### What Implements `SSL_set_SSL_CTX` in libSceLibreSsl (libSceLibreSsl3 reaches it through its parent forward `WXg9MX8BXPg`). The export was a `NotImplemented` stub. Servers call it from the SNI callback to switch a connection to another context. - `SSL_set_SSL_CTX(SSL*, SSL_CTX*)` sits next to `SSL_free` in `src/SslLifecycle.cpp` and follows LibreSSL 2.5.4's [`SSL_set_SSL_CTX`](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libssl/ssl_lib.c#L2751-L2772): - the same context returns at once with nothing changed; - a null context means the connection's initial context; - the connection gets a copy of the new context's certificate holder (the existing `DuplicateCertificate`, the `ssl_cert_dup` port `SSL_new` uses), the negotiated digest of each of the 7 key slots is copied over from the old holder, and the old holder is freed; - the new context gains a reference, the old one is released through `SSL_CTX_free`, and the new context is returned. - `Api.hpp` declares the export with its real signature instead of `(void)`. - A null connection, or a missing initial context or context certificate holder, throws where LibreSSL faults. Where LibreSSL dereferences a failed `ssl_cert_dup`, the digest copy is skipped and the connection is left without a certificate holder. - Debt fragment `docs/dev/debt/changes/feat-libressl-ssl-set-ssl-ctx.md`. Certificate holders that carry a certificate or private key still hit the existing `NotImplemented` in `DuplicateCertificate`, as `SSL_new` does today. ### Tested Linux (Ubuntu, GCC), unit test only; nothing checked on a console or in a title. `TestSetSslCtx` in `core/libs/tests/GuestLibreSslSslLifecycle.cpp` checks the null connection throw, that setting the current context changes nothing, that switching to another context moves the reference counts, keeps `initialCtx`, installs a fresh certificate holder and carries a negotiated digest over, that a null context goes back to the initial context, and that both contexts are released at the end. The guest-heap tracker's `live.empty()` check catches leaks and double frees. ``` ctest -R guest_libressl_ssl_lifecycle 100% tests passed, 0 tests failed out of 1 ``` ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-18 18:09:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-18T18:09:36Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 2d914921ef:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl_ssl_lifecycle_tests built cleanly.
  • CTest (guest_libressl_ssl_lifecycle):
    1/1 Test #4375: guest_libressl_ssl_lifecycle .....   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl/Export.cpp, core/libs/prx/libSceLibreSsl/include/Api.hpp, core/libs/prx/libSceLibreSsl/src/SslLifecycle.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified LibreSSL 2.5.4 ssl_lib.c SSL_set_SSL_CTX semantics: same ctx returns early without side effects, null ctx falls back to initialCtx, digest copy preserved across key slots, and reference counts updated via atomic fetch_add and SSL_CTX_free
    • Checked pointer safety and null checks for ssl, initialCtx, source cert, and previous cert handling in SslLifecycle.cpp
    • Verified memory management: previous certificate freed via FreeCertificate, new certificate duplicated via DuplicateCertificate, context reference transferred properly
    • Verified AGENTS.md compliance: no unauthorized comments in code, debt fragment added under docs/dev/debt/changes/feat-libressl-ssl-set-ssl-ctx.md, clean export signature in Api.hpp
    • Verified unit test coverage in GuestLibreSslSslLifecycle.cpp for null handling, same-context no-op, initialCtx fallback, digest propagation, and reference count lifecycle

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5962397647 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23088#pullrequestreview-5962397647). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-18T18:09:36Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `2d914921ef`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libressl_ssl_lifecycle_tests` built cleanly. - **CTest** (`guest_libressl_ssl_lifecycle`): ``` 1/1 Test #4375: guest_libressl_ssl_lifecycle ..... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibreSsl/Export.cpp`, `core/libs/prx/libSceLibreSsl/include/Api.hpp`, `core/libs/prx/libSceLibreSsl/src/SslLifecycle.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified LibreSSL 2.5.4 ssl_lib.c SSL_set_SSL_CTX semantics: same ctx returns early without side effects, null ctx falls back to initialCtx, digest copy preserved across key slots, and reference counts updated via atomic fetch_add and SSL_CTX_free - Checked pointer safety and null checks for ssl, initialCtx, source cert, and previous cert handling in SslLifecycle.cpp - Verified memory management: previous certificate freed via FreeCertificate, new certificate duplicated via DuplicateCertificate, context reference transferred properly - Verified AGENTS.md compliance: no unauthorized comments in code, debt fragment added under docs/dev/debt/changes/feat-libressl-ssl-set-ssl-ctx.md, clean export signature in Api.hpp - Verified unit test coverage in GuestLibreSslSslLifecycle.cpp for null handling, same-context no-op, initialCtx fallback, digest propagation, and reference count lifecycle Looks Good To Me
MarcZX10 commented 2026-11-21 02:33:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T02:33:27Z.

Review: COMMENTED


Reviewed — no blocking findings

The implementation and its lifecycle tests look consistent with the stated behavior; the reported test run was not independently verified.

What I ran

Reviewed head 2d914921efaae289d1d30976847fa0ddd33a7b60. No commands or tests were run; the test result in the PR description is author-reported.

Results

Check Result Note
Code review passed Relevant implementation and callers inspected
Unit test not run Author reports one passing test
Console behavior not run No console or title check reported

Negative control

Not run; no negative control was available in the prepared review materials.

Findings

Blocking

None found in the inspected implementation.

Non-blocking

None.

Sources

The review materials identify SSL_set_SSL_CTX in core/libs/prx/libSceLibreSsl/src/SslLifecycle.cpp and its test in core/libs/tests/GuestLibreSslSslLifecycle.cpp. The diff and relevant lifecycle helpers support the context reference transfer and certificate duplication behavior described in the PR.

Not verified

The author reports ctest -R guest_libressl_ssl_lifecycle passed, but this was not run during review. The console and title behavior was not checked. Read the prepared description, commit list, manifest, diff, plus relevant ranges of Api.hpp, SslLifecycle.cpp, Ssl.cpp, and the lifecycle test. The manifest lists five reviewed files; Export.cpp and the debt fragment were not read beyond their diff hunks. Full texts were available in the prepared review folder, but not all were inspected.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002274350 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23088#pullrequestreview-6002274350). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T02:33:27Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The implementation and its lifecycle tests look consistent with the stated behavior; the reported test run was not independently verified. ## What I ran Reviewed head `2d914921efaae289d1d30976847fa0ddd33a7b60`. No commands or tests were run; the test result in the PR description is author-reported. ## Results | Check | Result | Note | |---|---|---| | Code review | passed | Relevant implementation and callers inspected | | Unit test | not run | Author reports one passing test | | Console behavior | not run | No console or title check reported | ## Negative control Not run; no negative control was available in the prepared review materials. ## Findings ### Blocking None found in the inspected implementation. ### Non-blocking None. ## Sources The review materials identify `SSL_set_SSL_CTX` in `core/libs/prx/libSceLibreSsl/src/SslLifecycle.cpp` and its test in `core/libs/tests/GuestLibreSslSslLifecycle.cpp`. The diff and relevant lifecycle helpers support the context reference transfer and certificate duplication behavior described in the PR. ## Not verified The author reports `ctest -R guest_libressl_ssl_lifecycle` passed, but this was not run during review. The console and title behavior was not checked. Read the prepared description, commit list, manifest, diff, plus relevant ranges of `Api.hpp`, `SslLifecycle.cpp`, `Ssl.cpp`, and the lifecycle test. The manifest lists five reviewed files; `Export.cpp` and the debt fragment were not read beyond their diff hunks. Full texts were available in the prepared review folder, but not all were inspected. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-21 06:32:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T06:32:41Z.

Review: DISMISSED


Reviewed — changes requested

Changes are required before merge: preserve the SSL object’s certificate state when a context switch fails.

What I ran

  • Reviewed head 6869c8a3ef6194af723eef040af2fd6495ac6947; the prepared diff identifies base main.
  • Read the prepared description, commits, manifest, diff, and all five changed files listed in the manifest. Read related certificate duplication, SSL teardown, and context release code through repo_read.py.
  • No commands or tests were run; the test result in the description is author-reported.

Results

Check Result Note
Diff and changed files passed All five manifest entries read
Tests not run Author reports one passing unit test
Console behavior not run No console or title check reported

Findings

Blocking

  • core/libs/prx/libSceLibreSsl/src/SslLifecycle.cpp:612-613: DuplicateCertificate can throw for unsupported certificate contents, but this function assigns ssl->cert only after the call returns. If duplication throws, the old certificate remains attached while the context switch exits exceptionally. More seriously, when duplication returns nullptr on allocation failure, the function overwrites ssl->cert and frees the previous certificate, then continues switching contexts. Return without changing state on allocation failure (or otherwise handle failure before replacing/freeing the existing certificate), and add coverage for both failure paths.

Non-blocking

None.

Not verified

  • No tests were executed, including the reported guest_libressl_ssl_lifecycle test.
  • Console/title behavior was not checked.
  • files.json was not available through the reader; the manifest lists five changed files, all marked included, and each was read.

To get this to a good state

Handle certificate duplication failure before changing ssl->cert or releasing its existing certificate, and cover the failure behavior with tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6003533447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23088#pullrequestreview-6003533447). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T06:32:41Z. **Review:** DISMISSED --- # Reviewed — changes requested Changes are required before merge: preserve the SSL object’s certificate state when a context switch fails. ## What I ran - Reviewed head `6869c8a3ef6194af723eef040af2fd6495ac6947`; the prepared diff identifies base `main`. - Read the prepared description, commits, manifest, diff, and all five changed files listed in the manifest. Read related certificate duplication, SSL teardown, and context release code through `repo_read.py`. - No commands or tests were run; the test result in the description is author-reported. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | passed | All five manifest entries read | | Tests | not run | Author reports one passing unit test | | Console behavior | not run | No console or title check reported | ## Findings ### Blocking - `core/libs/prx/libSceLibreSsl/src/SslLifecycle.cpp:612-613`: `DuplicateCertificate` can throw for unsupported certificate contents, but this function assigns `ssl->cert` only after the call returns. If duplication throws, the old certificate remains attached while the context switch exits exceptionally. More seriously, when duplication returns `nullptr` on allocation failure, the function overwrites `ssl->cert` and frees the previous certificate, then continues switching contexts. Return without changing state on allocation failure (or otherwise handle failure before replacing/freeing the existing certificate), and add coverage for both failure paths. ### Non-blocking None. ## Not verified - No tests were executed, including the reported `guest_libressl_ssl_lifecycle` test. - Console/title behavior was not checked. - `files.json` was not available through the reader; the manifest lists five changed files, all marked included, and each was read. ## To get this to a good state Handle certificate duplication failure before changing `ssl->cert` or releasing its existing certificate, and cover the failure behavior with tests.
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!23088
No description provided.