feat(libSceLibreSsl): implement i2d_X509_PUBKEY #13546

Open
JaceTheBlu wants to merge 46 commits from upstream-pr/13546 into main
JaceTheBlu commented 2026-10-30 18:00:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JaceTheBlu on 2026-10-30T18:00:45Z.


What

Implements i2d_X509_PUBKEY in libSceLibreSsl, on top of the X509 decoder from #13266. This is stacked on #13266, and through it #11359 and #11129. Merge those first; this PR's own diff is then only the encoder, its tests and its debt entry.

src/X509.cpp ports the LibreSSL 2.5.4 template encoder (tasn_enc.c) for the items an X509_PUBKEY holds. It reuses the item tables of the decoder.

  • Sequences, primitives and ASN1_TYPE parameters of every type are encoded, including high tag numbers, and SEQUENCE, SET and OTHER values written as they are.
  • i2c_ASN1_INTEGER handles padding and negative values, as LibreSSL does.
  • i2c_ASN1_BIT_STRING counts unused bits when ASN1_STRING_FLAG_BITS_LEFT is not set, and clears them otherwise.
  • A BOOLEAN parameter of -1 is omitted, and null fields are left out.
  • The three output forms work as in LibreSSL:
    • a null out returns the length;
    • a null *out gets a buffer from the guest heap;
    • otherwise the encoding is written at *out and the pointer advances.

PutHeader, which the canonical name encoding already used, now also takes a tag class.

There is a new entry in docs/dev/debt/libSceLibreSsl.md, and the pending count goes from 286 to 285.

These cases throw instead of being encoded, because nothing reachable from an X509_PUBKEY needs them:

  • names;
  • SET OF and SEQUENCE OF fields;
  • cached encodings.

These cases throw where LibreSSL reads before the data or faults:

  • an all-zero BIT STRING without BITS_LEFT, and a negative zero INTEGER;
  • a null object or string inside an ASN1_TYPE;
  • a negative length, or null data behind a positive length.

Tested

Debian container, GCC 14.2, full Release build of head 16388cabad: ctest -R 'libressl|crypto_memcmp' passes 38 of 38. tools/check_test_registration.py and python3 tools/check_conventions.py --base upstream/main pass.

  • guest_libressl_x509 checks that a decoded key encodes back to its DER in all three output forms, and that nothing is written outside the encoding. It also checks:

    • null key, algorithm, object, parameter and public key;
    • unused-bit handling with and without BITS_LEFT;
    • BOOLEAN, positive and negative INTEGER and ENUMERATED, SEQUENCE, OTHER, high-tag and OBJECT parameters;
    • the throwing cases.
  • guest_libressl_oracle (ANYPS5_LIBRESSL_ORACLE) now also decodes random certificates with both implementations and encodes their public keys. In 60% of runs it first applies the same change to both keys:

    • a different parameter type, or a BOOLEAN or NULL parameter;
    • an integer sign flip;
    • a removed field;
    • other bit-string flags.

    It compares the return value, the bytes, the pointer advance and the error queue for each output form. The 11,646 cases show no difference. The only differences the oracle reports are the 6 AES_set_*_key cases already described in the debt file.

Not tested on Windows, on a title or on a console.

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: #13266, #11359, #11129
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5088836599 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13546). Originally posted by [JaceTheBlu](https://github.com/JaceTheBlu) on 2026-10-30T18:00:45Z. --- ### What Implements `i2d_X509_PUBKEY` in libSceLibreSsl, on top of the `X509` decoder from #13266. This is stacked on #13266, and through it #11359 and #11129. Merge those first; this PR's own diff is then only the encoder, its tests and its debt entry. `src/X509.cpp` ports the LibreSSL 2.5.4 template encoder (`tasn_enc.c`) for the items an `X509_PUBKEY` holds. It reuses the item tables of the decoder. - Sequences, primitives and `ASN1_TYPE` parameters of every type are encoded, including high tag numbers, and SEQUENCE, SET and OTHER values written as they are. - `i2c_ASN1_INTEGER` handles padding and negative values, as LibreSSL does. - `i2c_ASN1_BIT_STRING` counts unused bits when `ASN1_STRING_FLAG_BITS_LEFT` is not set, and clears them otherwise. - A BOOLEAN parameter of -1 is omitted, and null fields are left out. - The three output forms work as in LibreSSL: - a null `out` returns the length; - a null `*out` gets a buffer from the guest heap; - otherwise the encoding is written at `*out` and the pointer advances. `PutHeader`, which the canonical name encoding already used, now also takes a tag class. There is a new entry in `docs/dev/debt/libSceLibreSsl.md`, and the pending count goes from 286 to 285. These cases throw instead of being encoded, because nothing reachable from an `X509_PUBKEY` needs them: - names; - SET OF and SEQUENCE OF fields; - cached encodings. These cases throw where LibreSSL reads before the data or faults: - an all-zero BIT STRING without `BITS_LEFT`, and a negative zero INTEGER; - a null object or string inside an `ASN1_TYPE`; - a negative length, or null data behind a positive length. ### Tested Debian container, GCC 14.2, full Release build of head `16388cabad`: `ctest -R 'libressl|crypto_memcmp'` passes 38 of 38. `tools/check_test_registration.py` and `python3 tools/check_conventions.py --base upstream/main` pass. - **`guest_libressl_x509`** checks that a decoded key encodes back to its DER in all three output forms, and that nothing is written outside the encoding. It also checks: - null key, algorithm, object, parameter and public key; - unused-bit handling with and without `BITS_LEFT`; - BOOLEAN, positive and negative INTEGER and ENUMERATED, SEQUENCE, OTHER, high-tag and OBJECT parameters; - the throwing cases. - **`guest_libressl_oracle`** (`ANYPS5_LIBRESSL_ORACLE`) now also decodes random certificates with both implementations and encodes their public keys. In 60% of runs it first applies the same change to both keys: - a different parameter type, or a BOOLEAN or NULL parameter; - an integer sign flip; - a removed field; - other bit-string flags. It compares the return value, the bytes, the pointer advance and the error queue for each output form. The 11,646 cases show no difference. The only differences the oracle reports are the 6 `AES_set_*_key` cases already described in the debt file. Not tested on Windows, on a title or on a console. ### 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: #13266, #11359, #11129 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
CnRJay commented 2026-11-01 19:50:18 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Correction (2026-11-01): this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows Unblocks: none; a title is only required when the PR fixes a title. The technical points below are unaffected.

Two process points; I have not reviewed the encoder itself.

  1. Stacked on open PRs. This branch carries about 50 commits and 11 files because it sits on #13266 (still open), through it #11359 and #11129. The "own diff is then only the encoder" statement cannot be checked from the PR as submitted: GitHub shows +3915 lines, including the merges of pr10477. Please retarget or rebase once the base lands, otherwise this cannot be reviewed or merged independently, and CI results here do not isolate this change. #13547 and #13877 carry the same 50-odd commits.

  2. No title reaches it. "Unblocks: none" and "Not tested on Windows, on a title or on a console." i2d_X509_PUBKEY is a leaf of the X509 decoder that itself has no consumer on main (PEM_X509_INFO_read_bio still throws, per the sibling PR). Maintainers close export PRs that do not name a title ID and a log line showing the title reaching the function on main. Which title calls i2d_X509_PUBKEY, and which earlier export in that title's trace currently throws first?

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729925080 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13546#pullrequestreview-5729925080). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:50:18Z. **Review:** COMMENTED --- **Correction (2026-11-01):** this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows `Unblocks: none`; a title is only required when the PR fixes a title. The technical points below are unaffected. Two process points; I have not reviewed the encoder itself. 1. Stacked on open PRs. This branch carries about 50 commits and 11 files because it sits on #13266 (still open), through it #11359 and #11129. The "own diff is then only the encoder" statement cannot be checked from the PR as submitted: GitHub shows +3915 lines, including the merges of `pr10477`. Please retarget or rebase once the base lands, otherwise this cannot be reviewed or merged independently, and CI results here do not isolate this change. #13547 and #13877 carry the same 50-odd commits. 2. No title reaches it. "Unblocks: none" and "Not tested on Windows, on a title or on a console." `i2d_X509_PUBKEY` is a leaf of the X509 decoder that itself has no consumer on main (`PEM_X509_INFO_read_bio` still throws, per the sibling PR). Maintainers close export PRs that do not name a title ID and a log line showing the title reaching the function on main. Which title calls `i2d_X509_PUBKEY`, and which earlier export in that title's trace currently throws first?
MarcZX10 commented 2026-11-20 06:45:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T06:45:23Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head 8057ade57bd671539090517c8d17fb765098b812 using the prepared diff and the pinned repository reader. No commands or tests were run; the build, test, and oracle results in the description are author-reported.

Read all changed lines in CMakeLists.txt, Api.hpp, src/X509.cpp, tests/Oracle.cpp, tests/OracleReference.cpp, tests/X509.cpp, and docs/dev/debt/libSceLibreSsl.md, plus the relevant Export.cpp diff. All changed files were included; no files were excluded.

Results

Check Result Note
Diff review passed Prepared patch inspected
Tests not run Results reported by author
Windows and console behavior not run Author reports not tested

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full build, registered tests, oracle comparisons, Windows behavior, title behavior, and console behavior were not independently verified. The description reports a Debian/GCC 14.2 build and passing tests at a different head (16388cabad); those are author-reported results, not runs on this reviewed head.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5988591969 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13546#pullrequestreview-5988591969). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T06:45:23Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `8057ade57bd671539090517c8d17fb765098b812` using the prepared diff and the pinned repository reader. No commands or tests were run; the build, test, and oracle results in the description are author-reported. Read all changed lines in `CMakeLists.txt`, `Api.hpp`, `src/X509.cpp`, `tests/Oracle.cpp`, `tests/OracleReference.cpp`, `tests/X509.cpp`, and `docs/dev/debt/libSceLibreSsl.md`, plus the relevant `Export.cpp` diff. All changed files were included; no files were excluded. ## Results | Check | Result | Note | |---|---|---| | Diff review | passed | Prepared patch inspected | | Tests | not run | Results reported by author | | Windows and console behavior | not run | Author reports not tested | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full build, registered tests, oracle comparisons, Windows behavior, title behavior, and console behavior were not independently verified. The description reports a Debian/GCC 14.2 build and passing tests at a different head (`16388cabad`); those are author-reported results, not runs on this reviewed head. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-21 14:01:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T14:01:22Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change based on the source inspected; the reported tests were not run in this review.

What I ran

Reviewed head b97fded6b7be929b146fe207b402938ae96db3d7 using repo_read.py to inspect the prepared diff, manifest, and changed source and test files. No build or tests were run.

Results

Check Result Note
Source review passed Encoder and related API changes inspected
Build and tests not run Description reports host tests; not independently verified

Findings

Blocking

None found in the inspected code.

Non-blocking

None.

Not verified

The full text of core/libs/prx/libSceLibreSsl/tests/Oracle.cpp and core/libs/prx/libSceLibreSsl/tests/OracleReference.cpp was not read; those files were included in the review bundle but excluded from detailed inspection. Build, automated tests, Windows behavior, title behavior, and console behavior were not verified. The description reports a Debian Release build and 38 passing tests, plus 11,646 oracle cases; these are author-reported results.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6005749341 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13546#pullrequestreview-6005749341). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T14:01:22Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change based on the source inspected; the reported tests were not run in this review. ## What I ran Reviewed head `b97fded6b7be929b146fe207b402938ae96db3d7` using `repo_read.py` to inspect the prepared diff, manifest, and changed source and test files. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Encoder and related API changes inspected | | Build and tests | not run | Description reports host tests; not independently verified | ## Findings ### Blocking None found in the inspected code. ### Non-blocking None. ## Not verified The full text of `core/libs/prx/libSceLibreSsl/tests/Oracle.cpp` and `core/libs/prx/libSceLibreSsl/tests/OracleReference.cpp` was not read; those files were included in the review bundle but excluded from detailed inspection. Build, automated tests, Windows behavior, title behavior, and console behavior were not verified. The description reports a Debian Release build and 38 passing tests, plus 11,646 oracle cases; these are author-reported results. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-22 13:08:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T13:08:22Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

Changes are required before merge: fix d2i_X509 so decode failures do not leave a freed certificate pointer in the caller’s output.

What I ran

Reviewed head 08100a4b9d7ad08f135b037f1e393a38b01f70c5 using the prepared diff and pinned reader. No build or tests were run. The author reports a Debian/GCC 14.2 build, 38 passing tests, and 11,646 oracle cases; those results were not independently verified.

Results

Check Result Note
Source review passed All eight changed files read
Build and tests not run Author-reported only
Console behavior not run Not verified on console

Findings

Blocking

  • core/libs/prx/libSceLibreSsl/src/X509.cpp:2051: ItemDecode frees the reused certificate on failure (SequenceDecode’s failure path calls ItemFree), but this wrapper returns null without clearing *certificate. The existing reuse test expects the output pointer to be null after a failed decode, but its call passes the now-freed pointer back in and does not check the pointer’s value. Clear the caller’s output pointer when decoding fails, and ensure the failed decode path does not leave a dangling pointer.

Non-blocking

None.

Not verified

No commands, build, tests, or console execution were performed. All changed files were included and read: core/libs/prx/libSceLibreSsl/CMakeLists.txt, core/libs/prx/libSceLibreSsl/Export.cpp, core/libs/prx/libSceLibreSsl/include/Api.hpp, core/libs/prx/libSceLibreSsl/src/X509.cpp, core/libs/prx/libSceLibreSsl/tests/Oracle.cpp, core/libs/prx/libSceLibreSsl/tests/OracleReference.cpp, core/libs/prx/libSceLibreSsl/tests/X509.cpp, and docs/dev/debt/libSceLibreSsl.md.

To get this to a good state

Fix the dangling output pointer on decode failure, then rerun the X.509 tests and allocator test on the new head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6012418380 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13546#pullrequestreview-6012418380). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T13:08:22Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested Changes are required before merge: fix `d2i_X509` so decode failures do not leave a freed certificate pointer in the caller’s output. ## What I ran Reviewed head `08100a4b9d7ad08f135b037f1e393a38b01f70c5` using the prepared diff and pinned reader. No build or tests were run. The author reports a Debian/GCC 14.2 build, 38 passing tests, and 11,646 oracle cases; those results were not independently verified. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All eight changed files read | | Build and tests | not run | Author-reported only | | Console behavior | not run | Not verified on console | ## Findings ### Blocking - `core/libs/prx/libSceLibreSsl/src/X509.cpp:2051`: `ItemDecode` frees the reused certificate on failure (`SequenceDecode`’s failure path calls `ItemFree`), but this wrapper returns null without clearing `*certificate`. The existing reuse test expects the output pointer to be null after a failed decode, but its call passes the now-freed pointer back in and does not check the pointer’s value. Clear the caller’s output pointer when decoding fails, and ensure the failed decode path does not leave a dangling pointer. ### Non-blocking None. ## Not verified No commands, build, tests, or console execution were performed. All changed files were included and read: `core/libs/prx/libSceLibreSsl/CMakeLists.txt`, `core/libs/prx/libSceLibreSsl/Export.cpp`, `core/libs/prx/libSceLibreSsl/include/Api.hpp`, `core/libs/prx/libSceLibreSsl/src/X509.cpp`, `core/libs/prx/libSceLibreSsl/tests/Oracle.cpp`, `core/libs/prx/libSceLibreSsl/tests/OracleReference.cpp`, `core/libs/prx/libSceLibreSsl/tests/X509.cpp`, and `docs/dev/debt/libSceLibreSsl.md`. ## To get this to a good state Fix the dangling output pointer on decode failure, then rerun the X.509 tests and allocator test on the new head.
ligmaSec commented 2026-12-03 16:00:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T16:00:55Z.

Review: COMMENTED


On Linux, CTest passed guest_libressl_x509_der and guest_libressl_x509_der_allocator with this revision integrated. The default cases cover DER algorithm identifiers, omitted fields, and bit-string padding. The allocator variant passed separately.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6173430685 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13546#pullrequestreview-6173430685). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T16:00:55Z. **Review:** COMMENTED --- On Linux, CTest passed `guest_libressl_x509_der` and `guest_libressl_x509_der_allocator` with this revision integrated. The default cases cover DER algorithm identifiers, omitted fields, and bit-string padding. The allocator variant passed separately. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/13546:upstream-pr/13546'
git switch 'upstream-pr/13546'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/13546'
git switch 'upstream-pr/13546'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/13546'
git switch 'upstream-pr/13546'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/13546'
git switch 'main'
git merge --squash 'upstream-pr/13546'
git switch 'main'
git merge --ff-only 'upstream-pr/13546'
git switch 'main'
git merge 'upstream-pr/13546'
git push origin 'main'
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!13546
No description provided.