feat(libSceLibreSsl3): implement ASN1_get_object #17068

Closed
fishingpvalues wants to merge 13 commits from upstream-pr/17068 into main
fishingpvalues commented 2026-11-08 09:22:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fishingpvalues on 2026-11-08T09:22:44Z.


What

Implements ASN1_get_object (mXrusLhI3CI), which libSceLibreSsl3 exported only as an import-resolution stub, following LibreSSL 4.0.0 asn1_lib.c:

  • parses the one-byte and high-tag identifiers, the short, long and indefinite length forms, and returns the constructed and indefinite bits;
  • sets the 0x80 too-long flag and queues ASN1_R_TOO_LONG when the declared length exceeds the remaining input;
  • returns 0x80 with ASN1_R_HEADER_TOO_LONG queued and the input cursor unchanged for a malformed header, a truncated high tag or length, an indefinite primitive and a length above LONG_MAX;
  • skips leading zero bytes in long lengths and leaves *pp unchanged on every error path.

A null output pointer or a negative input length throws where LibreSSL would dereference or read out of bounds. The PS5 ABI and console behaviour are unverified.

src/Asn1ObjectHeader.cpp holds the export, tests/Asn1ObjectHeader.cpp is registered as guest_libressl3_asn1_object_header, and the stub leaves AdditionalExports.cpp while the declaration gains its real signature in include/Api.hpp. docs/dev/debt/libSceLibreSsl3.md loses the row (125 to 124 callable exports, 116 to 115 named) and gains one Functional entry.

Open PR #14465 is an alternate independent implementation of the same export; it is currently conflicting with main.

Tested

Linux x86-64 (CachyOS, GCC 16.2.1), Release, BUILD_TESTING=ON, base main 828e44310e:

  • guest_libressl3_asn1_object_header covers universal, application and private classes, constructed and indefinite forms, high tags, long lengths with leading zero bytes, the too-long flag, truncated headers and lengths, indefinite primitive rejection, high-tag overflow, the error queue codes and the null and negative-argument throws.
  • ctest --test-dir build -R '^guest_libressl3_asn1_object_header$' --output-on-failure: 1/1 passed.
  • python3 tools/check_conventions.py --base origin/main: passed; tools/check_test_registration.py build: passed.
  • Not run: Windows, a console, or a title.

Checklist

  • Based on current main; #14465 is an alternate implementation of the same export, not a dependency
  • 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)
  • No notes, investigation .md files or images added to the repository
  • 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:5210174659 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17068). Originally posted by [fishingpvalues](https://github.com/fishingpvalues) on 2026-11-08T09:22:44Z. --- ### What Implements `ASN1_get_object` (`mXrusLhI3CI`), which `libSceLibreSsl3` exported only as an import-resolution stub, following LibreSSL 4.0.0 [`asn1_lib.c`](https://github.com/libressl/openbsd/blob/078b3d1df8ca2011c1605cd40418c58fa0f5b143/src/lib/libcrypto/asn1/asn1_lib.c): - parses the one-byte and high-tag identifiers, the short, long and indefinite length forms, and returns the constructed and indefinite bits; - sets the `0x80` too-long flag and queues `ASN1_R_TOO_LONG` when the declared length exceeds the remaining input; - returns `0x80` with `ASN1_R_HEADER_TOO_LONG` queued and the input cursor unchanged for a malformed header, a truncated high tag or length, an indefinite primitive and a length above `LONG_MAX`; - skips leading zero bytes in long lengths and leaves `*pp` unchanged on every error path. A null output pointer or a negative input length throws where LibreSSL would dereference or read out of bounds. The PS5 ABI and console behaviour are unverified. `src/Asn1ObjectHeader.cpp` holds the export, `tests/Asn1ObjectHeader.cpp` is registered as `guest_libressl3_asn1_object_header`, and the stub leaves `AdditionalExports.cpp` while the declaration gains its real signature in `include/Api.hpp`. `docs/dev/debt/libSceLibreSsl3.md` loses the row (125 to 124 callable exports, 116 to 115 named) and gains one Functional entry. Open PR #14465 is an alternate independent implementation of the same export; it is currently conflicting with `main`. ### Tested Linux x86-64 (CachyOS, GCC 16.2.1), Release, `BUILD_TESTING=ON`, base `main` `828e44310e`: - `guest_libressl3_asn1_object_header` covers universal, application and private classes, constructed and indefinite forms, high tags, long lengths with leading zero bytes, the too-long flag, truncated headers and lengths, indefinite primitive rejection, high-tag overflow, the error queue codes and the null and negative-argument throws. - `ctest --test-dir build -R '^guest_libressl3_asn1_object_header$' --output-on-failure`: 1/1 passed. - `python3 tools/check_conventions.py --base origin/main`: passed; `tools/check_test_registration.py build`: passed. - Not run: Windows, a console, or a title. ### Checklist - [x] Based on current `main`; #14465 is an alternate implementation of the same export, not a dependency - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`) - [x] No notes, investigation `.md` files or images added to the repository - [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
gabichuuuuu commented 2026-11-08 12:57:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T12:57:30Z.

Review: COMMENTED


Windows run of head 44d5b8fc33, merged onto main 57c0f6a48d. GitHub shows this branch as conflicting, and the local merge stops in docs/dev/debt/libSceLibreSsl3.md, so for the run I took main's version of that file.

Setup: Windows 11, WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release, BUILD_TESTING=ON.

long is 32 bits on the Windows host. The export is declared and implemented with host long: long* plength, long omax, sizeof(long) and LONG_MAX. The guest's long is 64 bits. On Linux and macOS the two agree, but MinGW is LLP64, so long there is 4 bytes, and then:

  • only the low 4 bytes of the guest's 8-byte *plength are written;
  • omax is read as 32 bits;
  • count > sizeof(long) rejects a length of 5 to 8 bytes that LP64 LibreSSL accepts;
  • LONG_MAX caps lengths at 2^31 - 1.

guest_libressl3_asn1_object_header passes here, as in the earlier Windows run, because the test uses host long on both sides.

To check it the way a guest calls it, I added a local, unpushed differential test. The oracle is ASN1_get_object and asn1_get_length copied from 3rdparty/libressl-openbsd/src/lib/libcrypto/asn1/asn1_lib.c, with long written as std::int64_t and unsigned long as std::uint64_t. The export was called with a pointer to an 8-byte length set to 0x5A5A5A5A5A5A5A5A. Over 200,000 random headers of 1 to 20 bytes:

  • 97,611 results match except for the upper 32 bits of the length. A length of 73 comes back to the guest as 0x5A5A5A5A00000049.
  • 7,227 also differ in the return value, the cursor, the tag or the class. For example, 04 85 01 00 00 00 00 (length 2^32) returns 0x80 from both, but LP64 LibreSSL advances *pp and stores 4294967296 with the too-long flag. This PR takes the header-error path and leaves the length unwritten.

The fix is the one include/Api.hpp already uses for d2i_ASN1_INTEGER_nid_postfix(..., std::int64_t length):

  • std::int64_t* plength and std::int64_t omax in the declaration and the definition;
  • std::uint64_t for the accumulator;
  • 8 and INT64_MAX in place of sizeof(long) and LONG_MAX.

A test row with a 5-byte length, and one that reads the length through an 8-byte slot, would catch this on Windows.

#14465, the other implementation of this export, is still open and also conflicts with main.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5817971987 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17068#pullrequestreview-5817971987). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T12:57:30Z. **Review:** COMMENTED --- Windows run of head `44d5b8fc33`, merged onto `main` `57c0f6a48d`. GitHub shows this branch as conflicting, and the local merge stops in `docs/dev/debt/libSceLibreSsl3.md`, so for the run I took `main`'s version of that file. Setup: Windows 11, WinLibs GCC 15.2 (`x86_64-ucrt-posix-seh`), Release, `BUILD_TESTING=ON`. **`long` is 32 bits on the Windows host.** The export is declared and implemented with host `long`: `long* plength`, `long omax`, `sizeof(long)` and `LONG_MAX`. The guest's `long` is 64 bits. On Linux and macOS the two agree, but MinGW is LLP64, so `long` there is 4 bytes, and then: - only the low 4 bytes of the guest's 8-byte `*plength` are written; - `omax` is read as 32 bits; - `count > sizeof(long)` rejects a length of 5 to 8 bytes that LP64 LibreSSL accepts; - `LONG_MAX` caps lengths at 2^31 - 1. `guest_libressl3_asn1_object_header` passes here, as in the earlier Windows run, because the test uses host `long` on both sides. To check it the way a guest calls it, I added a local, unpushed differential test. The oracle is `ASN1_get_object` and `asn1_get_length` copied from `3rdparty/libressl-openbsd/src/lib/libcrypto/asn1/asn1_lib.c`, with `long` written as `std::int64_t` and `unsigned long` as `std::uint64_t`. The export was called with a pointer to an 8-byte length set to `0x5A5A5A5A5A5A5A5A`. Over 200,000 random headers of 1 to 20 bytes: - 97,611 results match except for the upper 32 bits of the length. A length of 73 comes back to the guest as `0x5A5A5A5A00000049`. - 7,227 also differ in the return value, the cursor, the tag or the class. For example, `04 85 01 00 00 00 00` (length 2^32) returns `0x80` from both, but LP64 LibreSSL advances `*pp` and stores 4294967296 with the too-long flag. This PR takes the header-error path and leaves the length unwritten. The fix is the one `include/Api.hpp` already uses for `d2i_ASN1_INTEGER_nid_postfix(..., std::int64_t length)`: - `std::int64_t* plength` and `std::int64_t omax` in the declaration and the definition; - `std::uint64_t` for the accumulator; - `8` and `INT64_MAX` in place of `sizeof(long)` and `LONG_MAX`. A test row with a 5-byte length, and one that reads the length through an 8-byte slot, would catch this on Windows. #14465, the other implementation of this export, is still open and also conflicts with `main`.

Pull request closed

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!17068
No description provided.