Skip to content

feat(libSceLibreSsl3): implement the RSA, DH and DSA get0 key and parameter accessors - #10490

Merged
oneandonlydean merged 19 commits into
boykopovar:mainfrom
alvaroelpob:feat/libscelibressl3-key-components
Nov 6, 2026
Merged

oneandonlydean merged 19 commits into
boykopovar:mainfrom
alvaroelpob:feat/libscelibressl3-key-components

Conversation

@alvaroelpob

@alvaroelpob alvaroelpob commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

What

Implements RSA_get0_key, DH_get0_pqg, DH_get0_key, DSA_get0_pqg and DSA_get0_key in libSceLibreSsl3, which threw and were declared without parameters.

The module is LibreSSL 4.0.0 (firmware 13.40 measurements in #8981). Signatures, layouts and results follow that release's rsa_local.h, rsa_lib.c, dh_local.h, dh_lib.c, dsa_local.h and dsa_lib.c. Each writes the requested fields through the non-null output pointers and leaves the others alone:

Export Fields
void RSA_get0_key(const RSA*, const BIGNUM** n, const BIGNUM** e, const BIGNUM** d) n (24), e (32), d (40)
void DH_get0_pqg(const DH*, const BIGNUM** p, const BIGNUM** q, const BIGNUM** g) p (8), q (64), g (16)
void DH_get0_key(const DH*, const BIGNUM** pub_key, const BIGNUM** priv_key) pub_key (32), priv_key (40)
void DSA_get0_pqg(const DSA*, const BIGNUM** p, const BIGNUM** q, const BIGNUM** g) p (16), q (24), g (32)
void DSA_get0_key(const DSA*, const BIGNUM** pub_key, const BIGNUM** priv_key) pub_key (40), priv_key (48)

The structs are 152, 128 and 112 bytes. The offsets were checked with static_asserts and against offsetof from the 4.0.0 headers compiled on x86-64; the 4.0.0 RSA has no engine field, unlike the 2.5.4 one in libSceLibreSsl. LibreSSL reads the key only for the outputs requested: with every output null the call returns without reading it, and a null key throws only when an output is requested, where LibreSSL would dereference it. This library doesn't list the module's own RSA_new, d2i_RSA_PUBKEY and the other key-creating exports yet, so on Linux a title's import of them binds to libSceLibreSsl, whose RSA and DSA use the 2.5.4 layout; the debt entry records it. On firmware 13.42 the module makes its own keys with these 4.0.0 offsets.

The debt entry records the source and what is unverified. The pending-export count drops from 84 to 79, and the five names join the exclusion list.

Tested

Ubuntu 26.04 (WSL2, kernel 6.18), GCC 15.2.0, Release, full build with BUILD_TESTING=ON on main 379b1dc. There is one commit per function, and each adds that function with its test block and debt update. Each was built and its guest_libressl3_key_components test and check_conventions.py run on its own.

Checklist

@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Progress report (f53ddbc → aef79c6)

📈 System libraries: 27.91% (+0.01%, +5 implemented, +4 declared functions)

✅ 5 implemented
Library Name
libSceLibreSsl3 DH_get0_key_nid_postfix
libSceLibreSsl3 DH_get0_pqg_nid_postfix
libSceLibreSsl3 DSA_get0_key_nid_postfix
libSceLibreSsl3 DSA_get0_pqg_nid_postfix
libSceLibreSsl3 RSA_get0_key_nid_postfix
🆕 4 declared as stubs
Library Name
libSceLibreSsl3 EVP_PKEY_get1_RSA_nid_disambig1_nid_postfix
libSceLibreSsl3 PEM_read_RSAPrivateKey_nid_disambig1_nid_postfix
libSceLibreSsl3 RSA_new_nid_disambig1_nid_postfix
libSceLibreSsl3 d2i_RSA_PUBKEY_nid_disambig1_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libSceLibreSsl3 41/276 (14.86%) 46/280 (16.43%) +1.57%

@alvaroelpob
alvaroelpob marked this pull request as ready for review October 24, 2026 03:18
Copilot AI balanced review requested due to automatic review settings October 24, 2026 03:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@MarcZX10 MarcZX10 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.

Reviewed head f05c46e39d10a6d3a5cfe5eb481313ea460b49c3.

I checked the five accessors against the pinned LibreSSL 4.0.0 structures and getter behavior. The asserted sizes/offsets for RSA, DH, and DSA match their field order, and each accessor writes only non-null output pointers from the specified fields. The null-key exception path and output-selection tests are consistent with the stated compatibility approach. I found no blocking correctness issue in this diff.

The PR reports focused accessor tests and a 2005/2006 full CTest run, with the remaining failure also present on main; I did not reproduce these runs locally. Current convention, labels, eligibility, and report checks pass; Linux, Windows, register-context, relinker, write-watch, and ASan jobs are skipped. The overlap report with #10557 lists shared build/export/debt files but no same exports; coordinate those file edits when composing the changes.

@MarcZX10 MarcZX10 mentioned this pull request Oct 24, 2026
12 tasks done
@github-actions

github-actions Bot commented Oct 25, 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 11128 EVP_PKEY_get1_RSA
RSA_new
d2i_RSA_PUBKEY
PR 11466 RSA_new
d2i_RSA_PUBKEY
PR 14409 docs/dev/debt/libSceLibreSsl3.md L27-28
PR 15449 docs/dev/debt/libSceLibreSsl3.md L28

Exports with a name or library/NID already present in main:

  • EVP_PKEY_get1_RSA (also exported by another library in main: core/libs/prx/libSceLibreSsl; may be intentional)
  • PEM_read_RSAPrivateKey (also exported by another library in main: core/libs/prx/libSceLibreSsl; may be intentional)
  • RSA_new (also exported by another library in main: core/libs/prx/libSceLibreSsl; may be intentional)
  • d2i_RSA_PUBKEY (also exported by another library in main: core/libs/prx/libSceLibreSsl; may be intentional)

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 12315 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/Export.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 12754 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/Export.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 12945 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/Export.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 13486 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 14212 docs/dev/debt/libSceLibreSsl3.md
PR 14465 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 14483 core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 14499 core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 14612 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 14992 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15045 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15119 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15163 core/libs/prx/libSceLibreSsl3/Export.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15289 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15303 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15406 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15514 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15544 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/Export.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15591 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15599 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/Export.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15737 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/Export.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15826 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15831 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15854 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16111 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/Export.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16185 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16186 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md

@oneandonlydean oneandonlydean self-assigned this Oct 25, 2026
@alvaroelpob
alvaroelpob force-pushed the feat/libscelibressl3-key-components branch from f05c46e to 476c2c6 Compare October 25, 2026 05:31
@alvaroelpob

Copy link
Copy Markdown
Contributor Author

Rebased on main e7e3e9c, head 476c2c6. The branch still merged cleanly, but after #10706 public_headers fails on the merge: the exports kept their (void) stubs in include/Api.hpp and the test redeclared them. 476c2c6 puts the real signatures and their structs in include/Api.hpp and drops the redeclarations. Full suite 2202/2203, with the same failure on main.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@alvaroelpob Thanks for moving the declarations; the header part is resolved. One behaviour question remains: the PR still rejects a null key unconditionally, and five tests require calls with all outputs null to throw. In pinned LibreSSL 4.0.0 these getters only dereference the key for the outputs that are requested, so a call with no outputs is a no-op. Please either follow that (no-output call returns without touching the key, with tests for it, and the body/debt updated), or cite a console-side source showing the different contract. The TestKey reinterpret fixtures should also use the canonical public objects rather than cast stand-ins.

@ycbuzi

ycbuzi commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Windows run of 476c2c67c6 merged onto main 49d9c1c751 (d434716844, clean). Windows 11 Home 10.0.26200, WinLibs MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

  • Built guest_libressl3_key_components_tests with no errors or warnings.
  • ctest --repeat until-fail:2: guest_libressl3_key_components passes both runs.
  • No negative control: the implementation is a new source file, so main's tree can't build this test without it.

Not run: a title. AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 25, 2026
@alvaroelpob
alvaroelpob force-pushed the feat/libscelibressl3-key-components branch from 476c2c6 to dfb0b57 Compare October 25, 2026 18:51
@alvaroelpob

Copy link
Copy Markdown
Contributor Author

@oneandonlydean Followed LibreSSL 4.0.0: the five getters now read the key only for the outputs requested, so a call with every output null returns without touching it, and a null key throws only when an output is requested (cc8bfe4). The tests use LibreSsl3Rsa, LibreSsl3Dh and LibreSsl3Dsa instead of the TestKey cast and check the no-output calls with a null key (1a7514f); the debt entry says the same (dfb0b57). Rebased on main 6e775f4 first. guest_libressl3_key_components passes; full ctest -j16 2268/2269, agc_driver_multisample_retirement fails the same way on main.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 25, 2026

@ahmadmuj ahmadmuj 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.

Checked head dfb0b57aa4d2 (base 6e775f452ae9; also merges clean onto main ae7d59258ea7). Linux, GCC 13.3, Release.

What I ran:

  • guest_libressl3_key_components on the merge: pass. public_headers: pass. check_conventions.py: pass.
  • Compiled the rsa_st/dh_st/dsa_st from the 4.0.0 *_local.h at ce488b78: 152/128/112 bytes, and every offset you read matches. The get0 bodies match rsa_lib.c, dh_lib.c and dsa_lib.c, and the new null-key handling is right.
  • Mutated the offsets and the null-key paths (9 mutations): all go red.
  • Merged #10915 (head 5576345be456, the 2.5.4 RSA in libSceLibreSsl) on top and ran a probe through both libraries.

So on its own this is right for 4.0.0. The problem is where an l3 title gets its RSA from, same pattern I raised on #10960:

  • In the 3.20 table, libSceLibreSsl3 also exports RSA_new, d2i_RSA_PUBKEY, EVP_PKEY_get1_RSA, PEM_read_RSAPrivateKey etc. under the same NIDs as libSceLibreSsl. It has no RSA_set0_key, and rsa_st is private in 4.0.0, so a title can't build its own RSA. Every key that reaches RSA_get0_key comes from one of those shared calls.
  • Our libSceLibreSsl3 doesn't implement them, so on Linux they bind to libSceLibreSsl. LD_DEBUG=bindings on an exe that only needs libSceLibreSsl3.prx: PFxpQ6lkQP0 (d2i_RSA_PUBKEY) and kADEz2eAE7c (RSA_new) bind to libSceLibreSsl.prx, 9RMSBpPZVdk (RSA_get0_key) binds to libSceLibreSsl3.prx.
  • l1 uses the public 2.5.4 layout, which still has ENGINE *engine at 24, so n is at 32.

Composed run, 2048-bit key: l1 d2i_RSA_PUBKEY -> l3 RSA_get0_key(rsa, &n, &e, &d) gives n = NULL, e = the modulus (top=32, low word 0xd941211635074acf), d = 65537. Each field comes from one slot too early. Both PRs' own tests pass, so it only shows once both land.

DSA has the same shift, read from the headers, not run: 2.5.4 dsa_st has int write_params after version, so p is at 24, not 16. l3 DSA_get0_pqg would hand back write_params (1 after DSA_new) as p. Nothing creates an l1 DSA yet, so that one is latent. DH is fine: the offsets are the same in 2.5.4 and 4.0.0.

What I'd suggest: read RSA and DSA through the 2.5.4 layout (#10915's LibreSslRsa for RSA). Titles can't see the 4.0.0 offsets anyway, and that's the object they actually get. The other way is to implement the l3 NIDs for every call that creates or frees these keys, which is a lot more surface. The debt line "No export of this module creates a key yet, so only keys the caller builds reach them" should change either way: the keys come from libSceLibreSsl.

@BunnyFl

BunnyFl commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@alvaroelpob @oneandonlydean @ahmadmuj firmware 13.42 data on both open points. These are runs of the decrypted libSceLibreSsl3.sprx on a PC, not a console. KeyComponents.cpp from dfb0b57aa4 is compiled unchanged with the PR's three structs.

No-output calls. The module handles them the way this head does. I ran its RSA_get0_key, DH_get0_pqg, DH_get0_key, DSA_get0_pqg and DSA_get0_key in Unicorn on all 64 combinations of requested and null outputs, with a marker-filled key or a null key.

  • With a real key, only the requested outputs are written: RSA n/e/d from 0x18/0x20/0x28, DH p/q/g from 0x8/0x40/0x10 and pub/priv from 0x20/0x28, DSA p/q/g from 0x10/0x18/0x20 and pub/priv from 0x28/0x30.
  • With a null key and every output null, all five return normally. With a null key and any output requested, the module faults before writing any output (the harness maps nothing at address 0). Per the disassembly, none of the five checks the key for null.
  • The PR gives the same 64 results, with a throw where the module faults. Three identical runs on each side.
  • Controls: copies of KeyComponents.cpp with one change each are caught. These are throwing on a null key regardless of outputs (the code before cc8bfe4, which differs on the five no-output calls), and reading e from d, DH q from g, DH pub from priv, and DSA priv from pub.

Where an l3 RSA comes from on 13.42. libSceLibreSsl3.sprx exports its own RSA_new, RSA_free, RSA_set0_key, d2i_RSA_PUBKEY, EVP_PKEY_get1_RSA and PEM_read_RSAPrivateKey. As in #10960, it doesn't depend on libSceLibreSsl.prx, and no binary in the dump links both. I ran the module's own code natively:

  • RSA_new allocates a 152-byte object. RSA_set0_key stores n/e/d at 0x18/0x20/0x28, and the PR's RSA_get0_key returns the three pointers that were set.
  • d2i_RSA_PUBKEY on a 2048-bit SubjectPublicKeyInfo, then the PR's RSA_get0_key: n has 2048 bits and all 256 bytes equal the key's modulus, e = 65537, d = NULL. The module's own RSA_get0_key returns the same pointers. Three identical runs.
  • Control: the same run with the 2.5.4 layout (a pointer inserted before n, as with ENGINE *engine) returns 65537 as n and NULL as e.

So on 13.42 the module's own RSA objects use these 4.0.0 offsets. Reading through the 2.5.4 layout would shift those objects by one slot. The l1-created keys in your composed run reach l3 through the emulator binding the missing l3 NIDs to libSceLibreSsl; on 13.42, libSceLibreSsl3 has those NIDs itself.

@alvaroelpob

Copy link
Copy Markdown
Contributor Author

@ahmadmuj @BunnyFl thanks, both. BunnyFl's 13.42 run shows that libSceLibreSsl3.sprx makes its own RSA objects through its own RSA_new, RSA_set0_key and d2i_RSA_PUBKEY, with n/e/d at 0x18/0x20/0x28. With the 2.5.4 layout, the control returns 65537 as n. So this PR keeps the 4.0.0 offsets, which match what the module's code reads.

The mismatch in the composed run is real on our side, though. This library doesn't list those key-creating exports yet, so on Linux they bind to libSceLibreSsl and hand over 2.5.4 objects. e2d9aaa replaces the debt entry's "only keys the caller builds" with that gap. The fix is to implement them in libSceLibreSsl3, which is beyond these five accessors.

@ycbuzi

ycbuzi commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

A Windows rerun of the new head e2d9aaa0a8, merged onto main 32b7331c44 with no conflicts: Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, plain ctest --repeat until-fail:2.

  • guest_libressl3_key_components passes, 2 of 2. The build has no warnings. No negative control, since the functions live in the new KeyComponents.cpp.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@alvaroelpob
alvaroelpob force-pushed the feat/libscelibressl3-key-components branch from e2d9aaa to 4d500b4 Compare October 26, 2026 22:37
@alvaroelpob

Copy link
Copy Markdown
Contributor Author

Rebased on main 74acfad (after #10948; head 4d500b4) to resolve the conflicts on the shared CMakeLists.txt and debt-file lines; no change to this PR's functions. Full ctest -j16 2431/2432, agc_driver_multisample_retirement fails the same way on main.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 5, 2026
…RSA and PEM_read_RSAPrivateKey so they no longer bind to libSceLibreSsl
@alvaroelpob
alvaroelpob force-pushed the feat/libscelibressl3-key-components branch from d64e8f1 to 64428a0 Compare November 5, 2026 19:35
@alvaroelpob

Copy link
Copy Markdown
Contributor Author

Rebased on main ce69412 to resolve the new conflicts. The rebase replaces the merge of main that was pushed to the branch. The rebase left the pending-export count wrong, so 64428a0 recounts it from Export.cpp. Head 64428a0. Full Linux suite: 4088/4088. check_conventions.py passes.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 5, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi @alvaroelpob, main moved (#10452 landed), so a merge of main into your branch was pushed as d259e611c3. It resolves the CMake sources and tests and the debt count and exclusion list; the PR's own logic is unchanged and the libressl3 tests pass on the merge.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 5, 2026
@oneandonlydean
oneandonlydean merged commit e322066 into boykopovar:main Nov 6, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs needs rebase Conflicts with main, rebase needed prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants