feat(libSceLibreSsl3): implement d2i_ASN1_SEQUENCE_ANY #18284

Merged
getuliobr merged 3 commits from upstream-pr/18284 into main 2026-11-26 03:05:44 +00:00
getuliobr commented 2026-11-11 03:09:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by getuliobr on 2026-11-11T03:09:40Z.


What

Implements d2i_ASN1_SEQUENCE_ANY (NID vR0po3nBkBs) in libSceLibreSsl3, which decodes a DER/BER SEQUENCE OF ANY into a stack of ASN1_TYPE values. It follows the LibreSSL 4.0.0 template decoder (tasn_dec.c, asn1_lib.c, asn1_types.c, a_object.c, a_int.c, a_enum.c, a_bitstr.c, a_string.c, a_time_tm.c), the revision the rest of this library follows:

  • tag and length parsing, including long-form tags and lengths, indefinite lengths and end-of-contents;
  • universal types converted into ASN1_STRING, ASN1_OBJECT, booleans and NULL; SEQUENCE, SET and non-universal classes kept in encoded form as upstream does (V_ASN1_OTHER for the latter);
  • constructed strings collected with the five-level nesting limit;
  • integer and enumerated sign handling, bit-string unused bits, OID arc validation and UTCTime/GeneralizedTime validation;
  • the same error-queue reasons and order, including the doubled ERR_R_MALLOC_FAILURE from item-created strings.

The stub is removed from AdditionalExports.cpp, the declaration gets the real signature, and the gaps are recorded in docs/dev/debt/libSceLibreSsl3.md.

Tested

  • Linux x86-64 (Ubuntu 24.04 container, GCC 13, Release): all 57 guest_libressl3_* tests pass, including the new guest_libressl3_asn1_sequence_any, which covers decoded values, indefinite lengths, 36 rejected encodings with their error codes, argument handling and every allocation failure.
  • Differential check: LibreSSL 4.0.0's own d2i_ASN1_SEQUENCE_ANY, built on macOS (x86-64 through Rosetta) from commit ce488b78 of the 3rdparty/libressl-openbsd repository, and this implementation returned identical values, consumed lengths and error-queue codes for 1,200,000 generated encodings (valid, malformed, truncated and mutated). Allocation failures were not part of that comparison.
  • Not tested on a console or with a title.

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: #17068, #17124, #17147, #17246 (shared lines only: the stub count and NID table in docs/dev/debt/libSceLibreSsl3.md and adjacent CMakeLists.txt lines; this PR rebases after them)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5253067566 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18284). Originally posted by [getuliobr](https://github.com/getuliobr) on 2026-11-11T03:09:40Z. --- ### What Implements `d2i_ASN1_SEQUENCE_ANY` (NID `vR0po3nBkBs`) in libSceLibreSsl3, which decodes a DER/BER `SEQUENCE OF ANY` into a stack of `ASN1_TYPE` values. It follows the [LibreSSL 4.0.0](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1) template decoder (`tasn_dec.c`, `asn1_lib.c`, `asn1_types.c`, `a_object.c`, `a_int.c`, `a_enum.c`, `a_bitstr.c`, `a_string.c`, `a_time_tm.c`), the revision the rest of this library follows: - tag and length parsing, including long-form tags and lengths, indefinite lengths and end-of-contents; - universal types converted into `ASN1_STRING`, `ASN1_OBJECT`, booleans and NULL; SEQUENCE, SET and non-universal classes kept in encoded form as upstream does (`V_ASN1_OTHER` for the latter); - constructed strings collected with the five-level nesting limit; - integer and enumerated sign handling, bit-string unused bits, OID arc validation and UTCTime/GeneralizedTime validation; - the same error-queue reasons and order, including the doubled `ERR_R_MALLOC_FAILURE` from item-created strings. The stub is removed from `AdditionalExports.cpp`, the declaration gets the real signature, and the gaps are recorded in `docs/dev/debt/libSceLibreSsl3.md`. ### Tested - Linux x86-64 (Ubuntu 24.04 container, GCC 13, Release): all 57 `guest_libressl3_*` tests pass, including the new `guest_libressl3_asn1_sequence_any`, which covers decoded values, indefinite lengths, 36 rejected encodings with their error codes, argument handling and every allocation failure. - Differential check: LibreSSL 4.0.0's own `d2i_ASN1_SEQUENCE_ANY`, built on macOS (x86-64 through Rosetta) from commit `ce488b78` of the `3rdparty/libressl-openbsd` repository, and this implementation returned identical values, consumed lengths and error-queue codes for 1,200,000 generated encodings (valid, malformed, truncated and mutated). Allocation failures were not part of that comparison. - Not tested on a console or with a title. ### 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: #17068, #17124, #17147, #17246 (shared lines only: the stub count and NID table in `docs/dev/debt/libSceLibreSsl3.md` and adjacent `CMakeLists.txt` lines; this PR rebases after them) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-11 16:27:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-11T16:27:52Z.

Review: APPROVED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_libressl3_asn1_sequence_any_tests built cleanly.
  • CTest (guest_libressl3_asn1_sequence_any):
    1/1 Test #3548: guest_libressl3_asn1_sequence_any ...   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp, core/libs/prx/libSceLibreSsl3/include/Api.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:554 (d2i_ASN1_SEQUENCE_ANY_nid_postfix): Verified argument validation (negative lengths return nullptr without touching input, null input pointers or null data with non-zero length throw std::invalid_argument, null data with zero length returns nullptr and posts BadObjectHeader 102 and NestedAsn1Error 58).
    • core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:507 (DecodeSequence): Verified definite and indefinite sequence decoding, EOC tracking, unexpected EOC detection (UnexpectedEoc 159), missing EOC handling (MissingEoc 137), old stack cleanup via FreeStack, and nulling *output on failure.
    • core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:337 (ConvertInteger, ConvertBitString): Verified ASN.1 two's complement integer decoding to magnitude Asn1String (NegativeFlag 0x100), rejection of non-minimal padding (LengthMismatch 62), bit-string unused bit range validation (InvalidBitStringBitsLeft 220), and trailing bit masking.
    • core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:480 (DecodeContent, Collect): Verified string nesting enforcement (NestedAsn1String 197 when depth > 5), primitive/constructed validation (TypeNotConstructed 156, TypeNotPrimitive 231), and buffer erasure via Cleanse.
    • core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:425 (ConvertString, ValidTime): Verified length alignment for BMPString (BmpStringIsWrongLength 214) and UniversalString (UniversalStringIsWrongLength 215), as well as UTCTime and GeneralizedTime date/leap-year validation (InvalidTimeFormat 132).
    • core/libs/prx/libSceLibreSsl3/tests/Asn1SequenceAny.cpp:284 (AllocationFailures): Verified leak-free resource release and exact error code sequences (including doubled MallocFailure 65) across all 15 simulated allocation failure injection points.
    • Repository rules & conventions (AGENTS.md): Verified zero code comments in implementation and tests (except permitted namespace closings), proper Conventional Commit title, technical debt updates in docs/dev/debt/libSceLibreSsl3.md, and removal of the stub from AdditionalExports.cpp.
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5861982262 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18284#pullrequestreview-5861982262). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T16:27:52Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `b50f3a3e7e`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_libressl3_asn1_sequence_any_tests` built cleanly. - **CTest** (`guest_libressl3_asn1_sequence_any`): ``` 1/1 Test #3548: guest_libressl3_asn1_sequence_any ... Passed 0.03 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp`, `core/libs/prx/libSceLibreSsl3/include/Api.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:554 (d2i_ASN1_SEQUENCE_ANY_nid_postfix): Verified argument validation (negative lengths return nullptr without touching input, null input pointers or null data with non-zero length throw std::invalid_argument, null data with zero length returns nullptr and posts BadObjectHeader 102 and NestedAsn1Error 58). - core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:507 (DecodeSequence): Verified definite and indefinite sequence decoding, EOC tracking, unexpected EOC detection (UnexpectedEoc 159), missing EOC handling (MissingEoc 137), old stack cleanup via FreeStack, and nulling *output on failure. - core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:337 (ConvertInteger, ConvertBitString): Verified ASN.1 two's complement integer decoding to magnitude Asn1String (NegativeFlag 0x100), rejection of non-minimal padding (LengthMismatch 62), bit-string unused bit range validation (InvalidBitStringBitsLeft 220), and trailing bit masking. - core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:480 (DecodeContent, Collect): Verified string nesting enforcement (NestedAsn1String 197 when depth > 5), primitive/constructed validation (TypeNotConstructed 156, TypeNotPrimitive 231), and buffer erasure via Cleanse. - core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:425 (ConvertString, ValidTime): Verified length alignment for BMPString (BmpStringIsWrongLength 214) and UniversalString (UniversalStringIsWrongLength 215), as well as UTCTime and GeneralizedTime date/leap-year validation (InvalidTimeFormat 132). - core/libs/prx/libSceLibreSsl3/tests/Asn1SequenceAny.cpp:284 (AllocationFailures): Verified leak-free resource release and exact error code sequences (including doubled MallocFailure 65) across all 15 simulated allocation failure injection points. - Repository rules & conventions (AGENTS.md): Verified zero code comments in implementation and tests (except permitted namespace closings), proper Conventional Commit title, technical debt updates in docs/dev/debt/libSceLibreSsl3.md, and removal of the stub from AdditionalExports.cpp. - Minor: please drop the `Generated with` footer from the description; the AI-assisted checkbox can stay. Looks Good To Me
MarcZX10 commented 2026-11-18 01:57:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T01:57:16Z.

Review: DISMISSED


Reviewed — changes requested

Changes are required before merge: fix the output stack cleanup so a failed decode does not free the caller’s existing stack.

What I ran

Reviewed head 177c94f91f7681c1fee61ad17ccc239cb75a05ba. No commands or tests were run. The author reports Linux tests and a differential check; those results were not independently run. No merged preview was checked.

Results

Check Result Note
Source review failed Existing output is freed before decoding succeeds
Tests not run Author-reported results only
Console behavior not run No console check reported

Findings

Blocking

  • core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:578 calls FreeStack(value) and clears the caller’s output before validating or decoding the new input. If parsing or allocation then fails, the function returns null with the old stack already freed and *output set to null. This violates the usual d2i replacement behavior: failed decoding must leave the supplied object intact. Decode into a temporary stack and replace/free the old stack only after success.

Non-blocking

None.

Not verified

  • No test suite or build was run; author-reported Linux and differential checks were not independently verified.
  • Console behavior is unverified, as recorded in the debt entry.
  • All changed files were read: AdditionalExports.cpp, CMakeLists.txt, include/Api.hpp, src/Asn1SequenceAny.cpp, tests/Asn1SequenceAny.cpp, and docs/dev/debt/libSceLibreSsl3.md. No changed files were excluded in the manifest.

To get this to a good state

Decode into a temporary stack, preserving the supplied output on failure; rerun the relevant tests on the updated head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5949704533 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18284#pullrequestreview-5949704533). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-18T01:57:16Z. **Review:** DISMISSED --- # Reviewed — changes requested Changes are required before merge: fix the output stack cleanup so a failed decode does not free the caller’s existing stack. ## What I ran Reviewed head `177c94f91f7681c1fee61ad17ccc239cb75a05ba`. No commands or tests were run. The author reports Linux tests and a differential check; those results were not independently run. No merged preview was checked. ## Results | Check | Result | Note | |---|---|---| | Source review | failed | Existing output is freed before decoding succeeds | | Tests | not run | Author-reported results only | | Console behavior | not run | No console check reported | ## Findings ### Blocking - `core/libs/prx/libSceLibreSsl3/src/Asn1SequenceAny.cpp:578` calls `FreeStack(value)` and clears the caller’s output before validating or decoding the new input. If parsing or allocation then fails, the function returns null with the old stack already freed and `*output` set to null. This violates the usual `d2i` replacement behavior: failed decoding must leave the supplied object intact. Decode into a temporary stack and replace/free the old stack only after success. ### Non-blocking None. ## Not verified - No test suite or build was run; author-reported Linux and differential checks were not independently verified. - Console behavior is unverified, as recorded in the debt entry. - All changed files were read: `AdditionalExports.cpp`, `CMakeLists.txt`, `include/Api.hpp`, `src/Asn1SequenceAny.cpp`, `tests/Asn1SequenceAny.cpp`, and `docs/dev/debt/libSceLibreSsl3.md`. No changed files were excluded in the manifest. ## To get this to a good state Decode into a temporary stack, preserving the supplied output on failure; rerun the relevant tests on the updated head.
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!18284
No description provided.