feat(libSceLibreSsl3): implement RSA_free and d2i_RSAPublicKey_bio #22804

Merged
Callimann8 merged 1 commit from upstream-pr/22804 into main 2026-11-18 11:04:29 +00:00
Callimann8 commented 2026-11-18 10:10:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Callimann8 on 2026-11-18T10:10:11Z.


Requested by Carlos · project thread

What

Implements RSA_free and d2i_RSAPublicKey_bio natively in libSceLibreSsl3, following LibreSSL 4.0.0 at ce488b78e0bd (rsa_lib.c, x_all.c, tasn_dec.c, asn1_lib.c, a_int.c, x_bignum.c).

  • RSA_free was forwarded to libSceLibreSsl, so every key made by this module's own RSA_new or handed out by EVP_PKEY_get1_RSA reached the 2.5.4 free and threw on its null method. It is now exported from libSceLibreSsl3. A key whose method pointer is set still goes to libSceLibreSsl's RSA_free, because only that library's RSA_new sets one; keys from this module, which leave it null, are freed by the existing native FreeRsa. The method and reference count are at the same offsets in both layouts, and the test checks that with static_assert.
  • The Windows parent alias for RSA_free is removed so the NID is exported once; libressl3_import_binding now also checks that RSA_free binds to libSceLibreSsl3.
  • d2i_RSAPublicKey_bio reads one object with the reader d2i_PrivateKey_bio already uses (now shared through ReadDerObjectFromBio_nid_no_patch) and decodes RSAPublicKey as ASN1_item_d2i does: an existing *rsa is freed once an object has been read and is left null on a decode failure, BER identifiers and lengths inside the SEQUENCE are accepted, integers follow c2i_ASN1_INTEGER_cbs (negative values, minimal-encoding check), and the error reasons are queued in upstream order.
  • Debt fragment: new entries for both functions; removes the d2i_RSA_PUBKEY/d2i_RSAPublicKey_bio and PEM_read_RSAPrivateKey entries whose closing condition was native RSA_free, and drops the same reason from the PEM keys entry. The inventory fragment's pending-export count goes from 11 to 10 and the parent alias count from 386 to 385.
  • d2i_RSA_PUBKEY and PEM_read_RSAPrivateKey stay stubs; they are separate follow-ups.

Tested

Linux (Ubuntu 24.04, GCC 13.3, Release), all libraries built. The new guest_libressl3_rsa_public_key_bio test tracks every heap block and checks:

  • native free with all eight components, reference counting, null, and the existing PSS throw;
  • a libSceLibreSsl key freed through the new export (routed to the parent, components freed);
  • EVP_PKEY_get1_RSA followed by RSA_free and EVP_PKEY_free, the case that used to throw;
  • decoding a real 2048-bit PKCS#1 public key and comparing n and e with d2i_PUBKEY of the same key, plus a 512-bit OpenSSL key read from the same BIO;
  • output replacement for native and parent keys, *rsa cleared on decode failure, and left untouched when the BIO read fails;
  • 13 integer cases (zero, negatives, padding rules, empty), 4 BER header forms and 18 malformed structures with their exact error queues, all without leaks.

Making RSA_free always free natively, or changing one queued reason, makes the test fail. All 179 LibreSSL tests (ctest -R libressl) pass on current main. tools/check_conventions.py --base origin/main reports no errors (one existing length warning on the inventory entry). Not tested on a console.

Overlap: no open PR implements either function. #17299 and #17303 rename the RSA_free alias line in ParentExports.cpp that this PR deletes, so whichever merges second resolves a one-line conflict. The OCSP_cert_to_id PR changes the same pending-export count line in libressl3-20171-inventory.md; whichever merges second lowers it by one more.

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:5373945898 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22804). Originally posted by [Callimann8](https://github.com/Callimann8) on 2026-11-18T10:10:11Z. --- <!-- claude-projects-attribution: {"github_login":"Callimann8"} --> _Requested by **Carlos** · [project thread](https://claude.ai/code/project/chan_01Qqp8R1FFuG8DvmaaKJbYPa?thread=cmsg_01Qqp8R1FFuG8DvmaaKJbYPa57LFYdEjJGN2HUNo9odJq5)_ ### What Implements `RSA_free` and `d2i_RSAPublicKey_bio` natively in libSceLibreSsl3, following LibreSSL 4.0.0 at `ce488b78e0bd` (`rsa_lib.c`, `x_all.c`, `tasn_dec.c`, `asn1_lib.c`, `a_int.c`, `x_bignum.c`). - `RSA_free` was forwarded to libSceLibreSsl, so every key made by this module's own `RSA_new` or handed out by `EVP_PKEY_get1_RSA` reached the 2.5.4 free and threw on its null method. It is now exported from libSceLibreSsl3. A key whose method pointer is set still goes to libSceLibreSsl's `RSA_free`, because only that library's `RSA_new` sets one; keys from this module, which leave it null, are freed by the existing native `FreeRsa`. The method and reference count are at the same offsets in both layouts, and the test checks that with `static_assert`. - The Windows parent alias for `RSA_free` is removed so the NID is exported once; `libressl3_import_binding` now also checks that `RSA_free` binds to libSceLibreSsl3. - `d2i_RSAPublicKey_bio` reads one object with the reader `d2i_PrivateKey_bio` already uses (now shared through `ReadDerObjectFromBio_nid_no_patch`) and decodes `RSAPublicKey` as `ASN1_item_d2i` does: an existing `*rsa` is freed once an object has been read and is left null on a decode failure, BER identifiers and lengths inside the SEQUENCE are accepted, integers follow `c2i_ASN1_INTEGER_cbs` (negative values, minimal-encoding check), and the error reasons are queued in upstream order. - Debt fragment: new entries for both functions; removes the `d2i_RSA_PUBKEY`/`d2i_RSAPublicKey_bio` and `PEM_read_RSAPrivateKey` entries whose closing condition was native `RSA_free`, and drops the same reason from the PEM keys entry. The inventory fragment's pending-export count goes from 11 to 10 and the parent alias count from 386 to 385. - `d2i_RSA_PUBKEY` and `PEM_read_RSAPrivateKey` stay stubs; they are separate follow-ups. ### Tested Linux (Ubuntu 24.04, GCC 13.3, Release), all libraries built. The new `guest_libressl3_rsa_public_key_bio` test tracks every heap block and checks: - native free with all eight components, reference counting, null, and the existing PSS throw; - a libSceLibreSsl key freed through the new export (routed to the parent, components freed); - `EVP_PKEY_get1_RSA` followed by `RSA_free` and `EVP_PKEY_free`, the case that used to throw; - decoding a real 2048-bit PKCS#1 public key and comparing n and e with `d2i_PUBKEY` of the same key, plus a 512-bit OpenSSL key read from the same BIO; - output replacement for native and parent keys, `*rsa` cleared on decode failure, and left untouched when the BIO read fails; - 13 integer cases (zero, negatives, padding rules, empty), 4 BER header forms and 18 malformed structures with their exact error queues, all without leaks. Making `RSA_free` always free natively, or changing one queued reason, makes the test fail. All 179 LibreSSL tests (`ctest -R libressl`) pass on current `main`. `tools/check_conventions.py --base origin/main` reports no errors (one existing length warning on the inventory entry). Not tested on a console. Overlap: no open PR implements either function. #17299 and #17303 rename the `RSA_free` alias line in `ParentExports.cpp` that this PR deletes, so whichever merges second resolves a one-line conflict. The `OCSP_cert_to_id` PR changes the same pending-export count line in `libressl3-20171-inventory.md`; whichever merges second lowers it by one more. ### 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 10:22:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-18T10:22:07Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 365ab09411:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_rsa_public_key_bio_tests, libressl3_import_binding_tests built cleanly.
  • CTest (libressl3_import_binding, guest_libressl3_rsa_public_key_bio):
    1/2 Test #4425: libressl3_import_binding .............   Passed    0.03 sec
    2/2 Test #4443: guest_libressl3_rsa_public_key_bio ...   Passed    0.04 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/Export.cpp, core/libs/prx/libSceLibreSsl3/ParentExports.cpp, core/libs/prx/libSceLibreSsl3/include/Api.hpp, core/libs/prx/libSceLibreSsl3/include/PkeyState.hpp, core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp, core/libs/prx/libSceLibreSsl3/src/RsaCrypto.cpp, core/libs/prx/libSceLibreSsl3/src/RsaPublicKeyBio.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified RSA_free_nid_disambig1_nid_postfix correctly checks for rsa->method to delegate to parent libSceLibreSsl while natively calling FreeRsa_nid_no_patch for null-method keys with reference decrementing.
    • Verified d2i_RSAPublicKey_bio_nid_postfix in RsaPublicKeyBio.cpp properly handles null output, frees existing *output, and avoids leaks on early decode failure.
    • Verified DER/BER length and tag parsing in CheckTag, ReadIdentifier, and ReadLength for integer overflow, buffer bounds with std::span, and indefinite length checks.
    • Verified IntegerContentToBignum handles two's-complement negative bignums, leading zero/0xff padding rules, and memory cleanup in DecodeRsaPublicKey on decode failures.
    • Verified AGENTS.md rules compliance: technical debt fragments added/modified in docs/dev/debt/changes/, no banned comments, and correct test coverage under tests/RsaPublicKeyBio.cpp.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5954702215 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22804#pullrequestreview-5954702215). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-18T10:22:07Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `365ab09411`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_libressl3_rsa_public_key_bio_tests`, `libressl3_import_binding_tests` built cleanly. - **CTest** (`libressl3_import_binding`, `guest_libressl3_rsa_public_key_bio`): ``` 1/2 Test #4425: libressl3_import_binding ............. Passed 0.03 sec 2/2 Test #4443: guest_libressl3_rsa_public_key_bio ... Passed 0.04 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/libs/prx/libSceLibreSsl3/Export.cpp`, `core/libs/prx/libSceLibreSsl3/ParentExports.cpp`, `core/libs/prx/libSceLibreSsl3/include/Api.hpp`, `core/libs/prx/libSceLibreSsl3/include/PkeyState.hpp`, `core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp`, `core/libs/prx/libSceLibreSsl3/src/RsaCrypto.cpp`, `core/libs/prx/libSceLibreSsl3/src/RsaPublicKeyBio.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified RSA_free_nid_disambig1_nid_postfix correctly checks for rsa->method to delegate to parent libSceLibreSsl while natively calling FreeRsa_nid_no_patch for null-method keys with reference decrementing. - Verified d2i_RSAPublicKey_bio_nid_postfix in RsaPublicKeyBio.cpp properly handles null output, frees existing *output, and avoids leaks on early decode failure. - Verified DER/BER length and tag parsing in CheckTag, ReadIdentifier, and ReadLength for integer overflow, buffer bounds with std::span, and indefinite length checks. - Verified IntegerContentToBignum handles two's-complement negative bignums, leading zero/0xff padding rules, and memory cleanup in DecodeRsaPublicKey on decode failures. - Verified AGENTS.md rules compliance: technical debt fragments added/modified in docs/dev/debt/changes/, no banned comments, and correct test coverage under tests/RsaPublicKeyBio.cpp. Looks Good To Me
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!22804
No description provided.