Skip to content

fix(libSceLibreSsl3): decode constructed strings in X509V3_EXT_d2i - #24677

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
kessler-drift:fix/libscelibressl3-ext-d2i-constructed
Dec 4, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
kessler-drift:fix/libscelibressl3-ext-d2i-constructed

Conversation

@kessler-drift

@kessler-drift kessler-drift commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What

X509V3_EXT_d2i (libSceLibreSsl3) failed every constructed (BER) extension value with TYPE_NOT_PRIMITIVE (231). LibreSSL refuses a constructed encoding only for its primitive-only types. Constructed BIT STRING, OCTET STRING, IA5String and GeneralizedTime values are now joined fragment by fragment, as LibreSSL 4.0.0 asn1_collect does (tasn_dec.c L180-L231, L452-L520). The joined content then goes through the existing decoders. This covers definite and indefinite lengths, nested fragments up to 5 levels (deeper gives NESTED_ASN1_STRING), fragments of any tag or class, empty fragments, UNEXPECTED_EOC and MISSING_EOC. ENUMERATED and OBJECT stay primitive-only (231), as in asn1_types.c.

Examples (extension value → result):

  • nid 82 24 06 04 01 41 04 01 42 → OCTET STRING "AB" (was NULL, 0x0dfff0e7)
  • nid 82 24 80 04 01 41 00 00 → "A"
  • nid 82 24 00 → empty OCTET STRING
  • nid 72 36 05 16 03 61 62 63 → IA5String "abc"

Compared with LibreSSL 3.4.1 from the submodule, run through X509V3_EXT_d2i on an extension made by X509_EXTENSION_create_by_NID, on 72 inputs: 49 give the same result. Of the other 23, 21 follow 4.0.0 and 2 are still rejected because of the 8 length-octet limit, a gap already on main. LibreSSL 3.7.3 (announced by firmware 11.60), 4.0.0 (announced by 13.40) and the LibreSSL main branch have the same tasn_dec.c on this path; all three were read, none was run. Where 4.0.0 differs from 3.4.1:

  • A definite length does not bound the fragments asn1_collect reads. 24 03 04 01 41 04 01 42 gives "AB" where 3.4.1 gives "A", and an end-of-contents after a definite fragment gives UNEXPECTED_EOC.
  • A fragment header error queues one reason fewer.
  • Joined GeneralizedTime content is validated.
  • ENUMERATED, OBJECT and INTEGER values stay primitive-only.
  • More than 8 length octets are still rejected. This gap was already there and is recorded in the debt fragment.

The existing test that decodes the constructed BIT STRING 23 03 00 00 00 now expects UNEXPECTED_EOC (159) instead of 231. Both the measured 3.4.1 and the 4.0.0 source give 159.

Overlap: #22890, which is open, changes the same file, but only its includes and the exported function. The two diffs do not touch the same lines, and its patch applies cleanly on this commit.

Tested

Ubuntu 26.04 x86-64 container under Rosetta 2 on Apple silicon (Colima VM, kernel 7.0), GCC 15.2. Not tested on Windows or on native x86-64.

  • guest_libressl3_x509v3_ext_d2i passes. It adds 41 checks in TestConstructedStrings and TestConstructedLibreSsl4Rules. Every check fails on main except two guards (ENUMERATED and OBJECT give 231 on main too), and the test as a whole fails on main.
  • 13 mutations were tried: depth limit ±1, EOC handling, MISSING_EOC at the outer level only, which types may be constructed, depth increment, a bounding outer length and no tag limit on fragments. The test catches all 13.
  • All 95 libSceLibreSsl3 tests pass, as do the public_headers, registration and progress control tests. tools/check_conventions.py --base upstream/main passes.
  • Nothing was compared with 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 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

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • X.509 extension decoding now supports string values split across nested fragments, including definite- and indefinite-length encodings.
  • Bug Fixes
    • Invalid constructed strings are rejected more consistently, with improved handling of malformed lengths, nesting, and end markers.
  • Documentation
    • Added notes describing differences in X.509 extension decoding behavior.

Constructed BIT STRING, OCTET STRING, IA5String and GeneralizedTime
extension values are joined as LibreSSL 4.0.0 asn1_collect does instead
of failing with TYPE_NOT_PRIMITIVE, which stays for ENUMERATED and
OBJECT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

The implementation and all three changed files were reviewed at head fbc8a1ed0a5d9697a6085942d150338f2db3f44a; no verified blocking defect was found, and the author reports successful tests that I did not run.

What I ran

No commands were run. I read the prepared description, commit list, diff, manifest, and every changed line in the three changed files. Targeted reader searches located the decoder caller and extension parsing code. The author-reported test results are not runs performed in this review.

Results

Check Result Note
Changed code review passed All changed lines were read
Test suite not run Results in description are author-reported
Console comparison not run No console comparison reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run tests, build the project, compare behavior against LibreSSL or a console, or inspect Windows and native x86-64 behavior. Excluded files: none; all three changed files were included and read. Repository context was limited to the decoder caller and extension struct.

To get this to a good state

Nothing is required.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_x509v3_ext_d2i_tests built cleanly.
  • CTest (guest_libressl3_x509v3_ext_d2i):
    1/1 Test #4665: guest_libressl3_x509v3_ext_d2i ...   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/src/X509V3ExtD2i.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4665: guest_libressl3_x509v3_ext_d2i ...Subprocess aborted***Exception:   0.89 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • X509V3ExtD2i.cpp: Collect(): Checked recursion depth limit (depth > MaxStringNesting), EOC handling (00 00), and missing/unexpected EOC error codes.
    • X509V3ExtD2i.cpp: Content(): Checked tag, class, and constructed flags; verified delegation to Collect() when collected vector is provided, and TypeNotPrimitive when omitted for primitive-only decoders (Enumerated, Object).
    • X509V3ExtD2i.cpp: Decode(): Verified BitString, OctetString, Ia5String, and GeneralizedTime decoders pass &collected while Enumerated and Object do not, preserving rejection for non-primitive types.
    • X509V3ExtD2i.cpp: ReadCheckedLength(): Verified bounds checks against input.size() for definite length, tag limit checks against int::max(), and rejection of indefinite length on non-constructed elements.
    • X509V3ExtD2i.cpp: Memory & lifetime: Verified vector collected remains valid for the duration of NewString/NewBitString/NewGeneralizedTime and does not leak or cause use-after-free.
    • AGENTS.md & technical debt: Verified debt fragment docs/dev/debt/changes/fix-libscelibressl3-ext-d2i-constructed.md follows conventions and no prohibited comments were introduced in source files.

Looks Good To Me

@Helldiverr

Copy link
Copy Markdown
Contributor

On Windows the merge passes guest_libressl3_x509v3_ext_d2i and guest_libressl3_x509v3_ext_conf_nid 3/3. With main's decoder the new test fails 3/3. Seven of eight mutations are caught. The one that survives treats any fragment starting with a zero byte as end-of-contents; a pin is suggested below.

Existing runs.

  • The author, kessler-drift, ran Ubuntu 26.04 under Rosetta 2 (GCC 15.2) and reports 13 mutations caught. The PR says it was not tested on Windows.
  • PrivacyOnion approved after a Linux run (Ubuntu 24.04, GCC 13.3) at fbc8a1ed0a, with a negative control: main's X509V3ExtD2i.cpp makes the test abort.
  • MarcZX10 approved at fbc8a1ed0a after reading the code without running it.
  • I found no Windows run.

What this run adds.

  • A Windows run of the merge.
  • A Windows negative control.
  • Eight mutations.
  • A check of Collect against the pinned LibreSSL source.

Merge. main now has 4ce7c920b3 (the 4.0.0 extension table, adding Decoder::Null), which touches the same two files. The merge is automatic and textual only. NewNull keeps its own header parsing and is not affected by this PR.

Code. I compared Collect (core/libs/prx/libSceLibreSsl3/src/X509V3ExtD2i.cpp:194-222) with asn1_collect in tasn_dec.c at the pinned ce488b78e0. They match on every point:

  • Nesting is limited at entry, with depth > 5 (:195, ASN1_MAX_STRING_NEST 5).
  • End-of-contents is a two-byte zero peek (:200). It ends an indefinite level and is UNEXPECTED_EOC in a definite one.
  • An indefinite level that runs out of input gives MISSING_EOC (:220).
  • A constructed fragment recurses over the rest of the input, not over its own length.
  • A fragment header error pushes the header reason followed by ERR_R_NESTED_ASN1_ERROR.
  • The caller passes expected_tag -1, so fragments of any tag and class are accepted. That holds although the comment there says "just check for UNIVERSAL class", and it matches the PR's 24 03 80 01 41 pin (tests/X509V3ExtD2i.cpp:221).
  • In asn1_types.c, BOOLEAN, INTEGER, NULL, OBJECT, REAL, ENUMERATED, RELATIVE-OID and TIME are primitive-only. BIT STRING, OCTET STRING, IA5String and GeneralizedTime are not, which matches which decoders get &collected (:500-522).

Suggestion (non-blocking).

  • Mutation m8 changes :200 to treat a fragment as end-of-contents when only its first byte is zero, and the test still passes.
  • asn1_get_identifier_cbs accepts tag 0, so upstream reads 24 03 00 01 41 as a universal tag-0 fragment and returns "A". The PR does the same.
  • Pinning CheckString(82, {0x24, 0x03, 0x00, 0x01, 0x41}, 4, {0x41}) would catch m8. I added that line locally: it passes on the merge and fails under m8 at the new line.

Negative control and mutations. Each change was made to X509V3ExtD2i.cpp on the merge, rebuilt, and run with guest_libressl3_x509v3_ext_d2i. Line numbers are in the test file at the head.

change runs result
none 3 Passed
control: main's X509V3ExtD2i.cpp, new tests kept 3 3/3 abort, check failed at line 212
m1: a constructed fragment inside a constructed value gives 231 instead of recursing 1 aborts, line 217
m2: a fragment identifier error pushes only 58 1 aborts, line 260
m3: no tag > INT_MAX check in ReadCheckedLength 1 aborts, line 262
m4: GeneralizedTime primitive-only 1 aborts, line 226
m5: IA5String primitive-only 1 aborts, line 223
m6: BIT STRING primitive-only 1 aborts, line 240
m7: end-of-contents of an indefinite level not consumed 1 aborts, line 218
m8: end-of-contents recognised on the first zero byte alone 1 Passed (not caught)
m8 with the suggested pin added 1 aborts at the added pin

Tests. ctest --test-dir build -R "^guest_libressl3_x509v3_ext_(d2i|conf_nid)$" -V

test main merge 1 merge 2 merge 3
guest_libressl3_x509v3_ext_d2i Passed 0.08 s Passed 0.12 s Passed 0.04 s Passed 0.04 s
guest_libressl3_x509v3_ext_conf_nid Passed 0.05 s Passed 0.09 s Passed 0.05 s Passed 0.04 s

On the merge, tools/check_conventions.py --base upstream/main passes with no findings, and tools/debt_fold.py --check exits 0.

Host:

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head fbc8a1ed0a, merged onto main 9111e0f78a as 9f493b7345

AI-assisted: yes

…XT_d2i string

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Constructed X509V3 Extension Strings

Layer / File(s) Summary
Length validation and recursive collection
core/libs/prx/libSceLibreSsl3/src/X509V3ExtD2i.cpp
Added checked length parsing and recursive collection for constructed content. The decoder reports errors for invalid lengths, unexpected or missing end-of-contents markers, and nesting beyond five levels.
Decode integration and regression coverage
core/libs/prx/libSceLibreSsl3/src/X509V3ExtD2i.cpp, core/libs/prx/libSceLibreSsl3/tests/X509V3ExtD2i.cpp, docs/dev/debt/changes/fix-libscelibressl3-ext-d2i-constructed.md
Enabled collection for selected string types. Added tests for constructed encodings, malformed inputs, and LibreSSL 4 rules. Documented comparisons with LibreSSL versions and the console comparison status.

Possibly related PRs

  • boykopovar/AnyPS5#27765: Adds constructed-string collection for subjectAltName and issuerAltName decoding in the same X509V3 extension path.

Suggested reviewers: fishingpvalues

Change: Bug fix

Merge Risk: Minimal · up to 39e8e

This change lets the extension decoder accept constructed string encodings that LibreSSL accepts, and it still rejects malformed input with specific errors. The tests cover accepted and rejected inputs and have been run on multiple platforms. No actionable merge-blocking risk remains. The known deviations from LibreSSL are documented, and behavior was not compared with the console.

Security architecture risk: Low · up to 39e8e

The compatibility change is limited to selected X.509 extension types. The main risk is that, under memory pressure, decoding a constructed value can throw an exception instead of returning a normal decode error. The review did not establish a change to a certificate trust decision.

Retained concerns

  • Low · security · inferred: Constructed values previously failed without allocating a collection buffer. The new buffer can throw on allocation failure, and the public decoder does not translate that exception to the ASN.1 error-and-null return used by existing result allocation paths. Callers that rely on that return contract can lose failure containment under memory pressure.
Security review details

Security Blast Radius

  • observed — The public CRL extension accessor reaches the modified decoder through its existing extension lookup path. The certificate-side accessor forwards to an external parent library; the inspected source does not resolve that target's behavior.

Trust Boundaries and Controls

  • observed — The decoder checks the outer tag and class before collection, bounds fragment reads by the remaining extension input, limits nesting to five, and rejects unexpected or missing end-of-contents markers. The typed decoder still decides whether the collected content is acceptable.

Hardening Proposals

  • proposed — Translate allocation failure from the collection buffer into the existing ASN.1 allocation error and null result. Verify that this path publishes no partial result.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 478620bc-b4a4-4a68-bfa0-8042341f7bb6
Commits

Reviewing files that changed from the base of the PR and between 043a1b6 and 39e8e6e.

Files selected for processing: 3

  • core/libs/prx/libSceLibreSsl3/src/X509V3ExtD2i.cpp
  • core/libs/prx/libSceLibreSsl3/tests/X509V3ExtD2i.cpp
  • docs/dev/debt/changes/fix-libscelibressl3-ext-d2i-constructed.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.

@kessler-drift

Copy link
Copy Markdown
Contributor Author

@Helldiverr Thanks for the mutation run. I added your pin as 39e8e6e88e: CheckString(82, {0x24, 0x03, 0x00, 0x01, 0x41}, 4, {0x41}), right after the 80 01 41 case. With that commit merged into main 8970cd2cfd, the 220 guest_libressl* tests pass, tools/check_conventions.py passes and tools/debt_fold.py --check exits 0 (Ubuntu 26.04 x86-64 container under Rosetta 2, GCC 15.2). I did not re-run your m8 mutation myself.

AI-assisted: yes (Claude Code).

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — no blocking findings

Nothing blocks this change; the code and tests support the described constructed-string decoding behavior, but I did not run the checks needed for a formal approval.

What I ran

Reviewed head 39e8e6e88e30969f97e15d92a75d05f1a31a3210 using the prepared diff and repo_read.py. No build or test commands were run. The author reports passing tests and checks in the PR description; these were not run by me.

Results

Check Result Note
Diff and changed code review passed All three changed files read
Build and tests not run Author reports passing checks
Console comparison not run Author reports no console comparison
Negative control not run Mutation checks not independently run

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

I read the full text of core/libs/prx/libSceLibreSsl3/src/X509V3ExtD2i.cpp, core/libs/prx/libSceLibreSsl3/tests/X509V3ExtD2i.cpp, and docs/dev/debt/changes/fix-libscelibressl3-ext-d2i-constructed.md; all changed files were included and read. No changed files were excluded. Build/test results, mutation checks, and behavior on a physical console were not independently verified.

To get this to a good state

Nothing is required. Optionally, compare behavior with a console as recorded in the debt note.

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built and ran the exact X509V3ExtD2i fixture and changed decode unit against the existing Linux runtime. The fixture passed its constructed-string cases, including flattened output and padding, exact error-queue contents, and freeing allocations on rejection.

AI-assisted: yes.

@oneandonlydean
oneandonlydean merged commit ef9acb0 into boykopovar:main Dec 4, 2026
15 of 17 checks passed
oneandonlydean added a commit to MarrasFederico/AnyPS5 that referenced this pull request Dec 5, 2026


Main gained boykopovar#24677, which declares MissingEoc and NestedAsn1String in the
same anonymous namespace of X509V3ExtD2i.cpp. This branch declared them
too, with the same values, so the merged file stopped compiling. The
branch's two copies are dropped and its code now uses main's. Nothing
else changes; the decoder work is MarrasFederico's.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants