feat(libSceLibreSsl): report ASN1_STRING_to_UTF8 errors through the ASN.1 error queue #11050

Merged
harisibrar1 merged 3 commits from upstream-pr/11050 into main 2026-10-27 07:38:38 +00:00
harisibrar1 commented 2026-10-25 15:21:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-25T15:21:40Z.


What

ASN1_STRING_to_UTF8 throws on malformed input and on allocation failure. The debt entry says so: "Malformed encodings and allocation failure throw until ASN.1 error-queue reporting is implemented". Main now has that error queue (LibreSslPutError_nid_no_patch in src/Error.cpp, read by ERR_get_error), so this reports those cases the way LibreSSL 2.5.4 does. Discussion #8981 places 2.5.4 on firmware 13.40.

In 2.5.4, ASN1_STRING_to_UTF8 (a_strex.c:624-646) calls ASN1_mbstring_copy with a stack ASN1_STRING and only writes *out on success. ASN1_mbstring_ncopy (a_mbstr.c:95-256) then does the following, each failure being ASN1error(reason) (ERR_PUT_error(13, 0xfff, reason)) and -1:

  • BMPString with an odd length: INVALID_BMPSTRING_LENGTH (129). UniversalString with a length that isn't a multiple of 4: INVALID_UNIVERSALSTRING_LENGTH (133). Both are checked before the data is read.
  • UTF8String that UTF8_getc rejects (bad lead or continuation byte, overlong form, surrogate, above U+10FFFF, truncated): INVALID_UTF8STRING (134).
  • A surrogate in a BMPString, or a surrogate or value above U+10FFFF in a UniversalString (traverse_string): ILLEGAL_CHARACTERS (124).
  • Allocation failure: ERR_R_MALLOC_FAILURE (65). For a UTF8String the copy goes through ASN1_STRING_set, which reports it too, so it's queued twice.

ASN1_STRING_to_UTF8 now returns -1 and queues those codes through LibreSslPutError_nid_no_patch, leaving the output untouched. The cases where 2.5.4 has undefined behaviour still throw, now as std::invalid_argument/std::length_error like Digest.cpp's guards:

  • null data with a nonzero or negative length
  • a length that overflows int
  • an int overflow of the converted size

The conversion runs in two passes, as @thepoppfish suggested in review: the first decodes the whole string, reporting any malformed character, and counts the UTF-8 size; then the one guest allocation is made and the second pass writes into it. No host buffer is allocated, so no std::bad_alloc can reach the guest.

A null output now throws only once a conversion has succeeded, since 2.5.4 dereferences it only there; a malformed string with a null output returns -1 like the source. The export no longer reaches NotImplemented_nid_no_patch. The debt sentence is rewritten to match.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main 3a2c51afd2 with this change: no failed steps.

  • tests/Asn1.cpp gains MalformedStrings, which checks the return value, the untouched output and the exact queued code for each case:
    • odd BMP length (also with null data, showing the length is checked first): 129
    • BMP surrogate: 124
    • UniversalString length 5 (also with null data): 133
    • UniversalString value above U+10FFFF and surrogate: 124
    • UTF8String overlong, truncated, surrogate, 0xF5 lead and stray continuation byte: 134
    • a malformed string with a null output: -1 with the error
  • The allocator mode now also fails the allocation: a converted string queues 65 once, and a UTF8String copy queues it twice. It also converts a UniversalString holding 1-, 2-, 3- and 4-byte UTF-8 characters with exactly one allocation of the exact size, and a malformed string with no allocation.
  • All ten guest_libressl* tests pass, 3 runs on the two-pass head, including guest_libressl_asn1 and guest_libressl_asn1_allocator.
  • Negative control: with main's Asn1.cpp, both ASN.1 tests abort with "ASN1_STRING_to_UTF8_nid_postfix not implemented" (2 runs).
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py lists only open PRs that conflict with main on the debt file; none touches Asn1.cpp.
  • Not tested: Linux and macOS, or a console; the behaviour follows the 2.5.4 sources.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5006001250 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11050). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-25T15:21:40Z. --- ### What `ASN1_STRING_to_UTF8` throws on malformed input and on allocation failure. The debt entry says so: "Malformed encodings and allocation failure throw until ASN.1 error-queue reporting is implemented". Main now has that error queue (`LibreSslPutError_nid_no_patch` in `src/Error.cpp`, read by `ERR_get_error`), so this reports those cases the way [LibreSSL 2.5.4](https://github.com/libressl/openbsd/tree/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/asn1) does. Discussion #8981 places 2.5.4 on firmware 13.40. In 2.5.4, `ASN1_STRING_to_UTF8` (`a_strex.c:624-646`) calls `ASN1_mbstring_copy` with a stack `ASN1_STRING` and only writes `*out` on success. `ASN1_mbstring_ncopy` (`a_mbstr.c:95-256`) then does the following, each failure being `ASN1error(reason)` (`ERR_PUT_error(13, 0xfff, reason)`) and -1: - BMPString with an odd length: `INVALID_BMPSTRING_LENGTH` (129). UniversalString with a length that isn't a multiple of 4: `INVALID_UNIVERSALSTRING_LENGTH` (133). Both are checked before the data is read. - UTF8String that `UTF8_getc` rejects (bad lead or continuation byte, overlong form, surrogate, above U+10FFFF, truncated): `INVALID_UTF8STRING` (134). - A surrogate in a BMPString, or a surrogate or value above U+10FFFF in a UniversalString (`traverse_string`): `ILLEGAL_CHARACTERS` (124). - Allocation failure: `ERR_R_MALLOC_FAILURE` (65). For a UTF8String the copy goes through `ASN1_STRING_set`, which reports it too, so it's queued twice. `ASN1_STRING_to_UTF8` now returns -1 and queues those codes through `LibreSslPutError_nid_no_patch`, leaving the output untouched. The cases where 2.5.4 has undefined behaviour still throw, now as `std::invalid_argument`/`std::length_error` like `Digest.cpp`'s guards: - null data with a nonzero or negative length - a length that overflows `int` - an `int` overflow of the converted size The conversion runs in two passes, as @thepoppfish suggested in review: the first decodes the whole string, reporting any malformed character, and counts the UTF-8 size; then the one guest allocation is made and the second pass writes into it. No host buffer is allocated, so no `std::bad_alloc` can reach the guest. A null output now throws only once a conversion has succeeded, since 2.5.4 dereferences it only there; a malformed string with a null output returns -1 like the source. The export no longer reaches `NotImplemented_nid_no_patch`. The debt sentence is rewritten to match. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, full Release build on main 3a2c51afd2 with this change: no failed steps. - `tests/Asn1.cpp` gains `MalformedStrings`, which checks the return value, the untouched output and the exact queued code for each case: - odd BMP length (also with null data, showing the length is checked first): 129 - BMP surrogate: 124 - UniversalString length 5 (also with null data): 133 - UniversalString value above U+10FFFF and surrogate: 124 - UTF8String overlong, truncated, surrogate, 0xF5 lead and stray continuation byte: 134 - a malformed string with a null output: -1 with the error - The allocator mode now also fails the allocation: a converted string queues 65 once, and a UTF8String copy queues it twice. It also converts a UniversalString holding 1-, 2-, 3- and 4-byte UTF-8 characters with exactly one allocation of the exact size, and a malformed string with no allocation. - All ten `guest_libressl*` tests pass, 3 runs on the two-pass head, including `guest_libressl_asn1` and `guest_libressl_asn1_allocator`. - Negative control: with main's `Asn1.cpp`, both ASN.1 tests abort with "ASN1_STRING_to_UTF8_nid_postfix not implemented" (2 runs). - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py` lists only open PRs that conflict with main on the debt file; none touches `Asn1.cpp`. - Not tested: Linux and macOS, or a console; the behaviour follows the 2.5.4 sources. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 15:26:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:26:59Z.

Review: DISMISSED


The new error-queue handling covers failure of malloc_nid_postfix, but conversion also allocates through std::vector<unsigned char> converted: AppendCharacter calls push_back once to four times per character, and vector growth can throw std::bad_alloc before the guest allocator is reached. That exception currently escapes this exported C ABI function instead of returning -1 and queuing ERR_R_MALLOC_FAILURE, so allocation failure is not fully handled as the PR body and debt entry claim. The pinned LibreSSL 2.5.4 path reports destination-allocation failures through ASN1error(ERR_R_MALLOC_FAILURE) in ASN1_mbstring_ncopy (including both ASN1_STRING_type_new and the output buffer allocation): github.com/libressl/openbsd@2e2c612066/src/lib/libcrypto/asn1/a_mbstr.c (L1426-L1451) . Please either map failures from the intermediate conversion storage into the documented ASN.1 error/return behavior, or avoid that allocating intermediate, and add focused coverage for that failure path. I could not run the product test locally: full CMake configuration stops because the SDL2 submodule is uninitialized and XRandR development support is unavailable. git diff --check passed; conventions completed with a debt-length warning.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643859827 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11050#pullrequestreview-5643859827). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T15:26:59Z. **Review:** DISMISSED --- The new error-queue handling covers failure of `malloc_nid_postfix`, but conversion also allocates through `std::vector<unsigned char> converted`: `AppendCharacter` calls `push_back` once to four times per character, and vector growth can throw `std::bad_alloc` before the guest allocator is reached. That exception currently escapes this exported C ABI function instead of returning -1 and queuing `ERR_R_MALLOC_FAILURE`, so allocation failure is not fully handled as the PR body and debt entry claim. The pinned LibreSSL 2.5.4 path reports destination-allocation failures through `ASN1error(ERR_R_MALLOC_FAILURE)` in `ASN1_mbstring_ncopy` (including both `ASN1_STRING_type_new` and the output buffer allocation): https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/asn1/a_mbstr.c#L1426-L1451 . Please either map failures from the intermediate conversion storage into the documented ASN.1 error/return behavior, or avoid that allocating intermediate, and add focused coverage for that failure path. I could not run the product test locally: full CMake configuration stops because the SDL2 submodule is uninitialized and XRandR development support is unavailable. `git diff --check` passed; conventions completed with a debt-length warning.
thepoppfish commented 2026-10-26 00:20:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thepoppfish on 2026-10-26T00:20:02Z.

Review: APPROVED


Re-reviewed exact head §d2bb979ffc§. The two-pass implementation resolves the host-scratch-allocation issue from Marc's review and my supporting reproduction. It adopts the approach I suggested; I am not claiming the production file is byte-identical to the earlier scratch patch.

Fresh independent native Windows / GCC 16.2.0-6 UCRT, C++20, §-O1 -g -fno-inline -fno-lto§, static C++ runtime:

  • 8/8 focused isolated CTests pass, 0 skipped, 0.27 s at this published head. This includes the complete committed §tests/Asn1.cpp§ in normal and custom-allocator modes, plus five allocation/error probe modes and the Unicode corpus.
  • 18 allocation/error cases cover UTF8String, T61String, BMPString and UniversalString: valid conversions, armed host-§operator new§ failure, guest malloc failure, malformed data, and malformed data with host failure armed. No allocation exception escapes, and measured host §operator new§ calls stay at zero. Guest allocation failures return -1 and preserve the output sentinel, with exactly two ASN.1 malloc errors for the UTF8String copy and one for the converted forms; malformed inputs allocate nothing and report the expected reason.
  • 1352 independent Python-encoding Unicode cases pass, with zero host §operator new§ calls during conversion, one guest allocation per successful conversion, correct bytes/length/NUL termination and unchanged input. The corpus was regenerated and its SHA-256 matched the original corpus, §9520d8ee95c69480f40caf466e86b63feebd8d9c6dadd3dce7bf1f167047086f§. These runs executed the newly compiled revision; they are not relabeled old results.
  • Clean local merge with main §acb900c194§ also passes 8/8, 0 skipped, 0.31 s, including the same 18 error/allocation cases and 1352 Unicode cases. Local merge §a6f1174dfb8e8831aa1b8c045dd5255bbfe7a6c6§, tree §ce237dc03a6cee583c075080d800ef4dceebf96b§. The complete conversion/error/test source blobs are unchanged by this merge.
  • Both tracked trees are clean and whitespace checks pass. Conventions completes with 0 errors, plus one debt-length warning: the revised ASN.1 paragraph is 1272 characters. Successful focused builds have no warnings.

Non-blocking documentation cleanup: the resolved return/error-code list can be removed from the debt paragraph. The remaining gap and existing source provenance fit in this shorter replacement:

Scope: these executables compile the complete production Asn1.cpp and Error.cpp with real headers/error queue. The explicit heap-registration/malloc test adapter from my earlier replay supplies and counts the guest allocation boundary; this is not the full libc/LibreSSL product build. I did not rerun the author's broader ten-test product suite, hosted CI, Linux/macOS, a console/title, or a canonical overlap scan. The original head's negative controls remain historical evidence for that commit. AI-assisted review/replay: Codex.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5646138422 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11050#pullrequestreview-5646138422). Originally posted by [thepoppfish](https://github.com/thepoppfish) on 2026-10-26T00:20:02Z. **Review:** APPROVED --- Re-reviewed exact head §d2bb979ffcd13da56425d3792cc25e0f629831e8§. The two-pass implementation resolves the host-scratch-allocation issue from [Marc's review](https://github.com/boykopovar/AnyPS5/pull/11050#pullrequestreview-5643859827) and [my supporting reproduction](https://github.com/boykopovar/AnyPS5/pull/11050#issuecomment-6399952656). It adopts the approach I suggested; I am not claiming the production file is byte-identical to the earlier scratch patch. Fresh independent native Windows / GCC 16.2.0-6 UCRT, C++20, §-O1 -g -fno-inline -fno-lto§, static C++ runtime: - **8/8 focused isolated CTests pass, 0 skipped, 0.27 s** at this published head. This includes the complete committed §tests/Asn1.cpp§ in normal and custom-allocator modes, plus five allocation/error probe modes and the Unicode corpus. - **18 allocation/error cases** cover UTF8String, T61String, BMPString and UniversalString: valid conversions, armed host-§operator new§ failure, guest malloc failure, malformed data, and malformed data with host failure armed. No allocation exception escapes, and measured host §operator new§ calls stay at zero. Guest allocation failures return -1 and preserve the output sentinel, with exactly two ASN.1 malloc errors for the UTF8String copy and one for the converted forms; malformed inputs allocate nothing and report the expected reason. - **1352 independent Python-encoding Unicode cases** pass, with zero host §operator new§ calls during conversion, one guest allocation per successful conversion, correct bytes/length/NUL termination and unchanged input. The corpus was regenerated and its SHA-256 matched the original corpus, §9520d8ee95c69480f40caf466e86b63feebd8d9c6dadd3dce7bf1f167047086f§. These runs executed the newly compiled revision; they are not relabeled old results. - Clean local merge with main §acb900c194325b9ee8c23c10768a9a491454f0b2§ also passes **8/8, 0 skipped, 0.31 s**, including the same 18 error/allocation cases and 1352 Unicode cases. Local merge §a6f1174dfb8e8831aa1b8c045dd5255bbfe7a6c6§, tree §ce237dc03a6cee583c075080d800ef4dceebf96b§. The complete conversion/error/test source blobs are unchanged by this merge. - Both tracked trees are clean and whitespace checks pass. Conventions completes with **0 errors**, plus one debt-length warning: the revised ASN.1 paragraph is 1272 characters. Successful focused builds have no warnings. Non-blocking documentation cleanup: the resolved return/error-code list can be removed from the debt paragraph. The remaining gap and existing source provenance fit in this shorter replacement: > - [ASN1_INTEGER_get and ASN1_STRING_data/length/type/to_UTF8](../../../core/libs/prx/libSceLibreSsl/src/Asn1.cpp) are unverified on PS5. Signatures, the 24-byte string layout and conversions follow [LibreSSL 2.5.4](https://github.com/libressl/openbsd/tree/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/asn1), the version reported for firmware 13.40 in [discussion #8981](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214). Null accessor inputs or conversion outputs, missing nonempty data, overflowing lengths and unrepresentable signed integer values throw where the source has undefined behaviour. Scope: these executables compile the complete production `Asn1.cpp` and `Error.cpp` with real headers/error queue. The explicit heap-registration/malloc test adapter from my earlier replay supplies and counts the guest allocation boundary; this is not the full libc/LibreSSL product build. I did not rerun the author's broader ten-test product suite, hosted CI, Linux/macOS, a console/title, or a canonical overlap scan. The original head's negative controls remain historical evidence for that commit. AI-assisted review/replay: Codex.
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!11050
No description provided.