Skip to content

fix(libSceLibreSsl3): accept BER long-form lengths in the key decoders - #25390

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
kessler-drift:fix/libscelibressl3-key-ber-lengths
Nov 24, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
kessler-drift:fix/libscelibressl3-key-ber-lengths

Conversation

@kessler-drift

Copy link
Copy Markdown
Contributor

What

The key decoders of libSceLibreSsl3 refused, with reason 102, every long-form length that is not minimal: a first length octet of zero, or a value below 128. LibreSSL 3.4.1 (the 3rdparty/libressl-openbsd submodule) decodes these, so a key such as a PKCS#8 whose outer SEQUENCE is written 30 82 00 xx, or an EC SubjectPublicKeyInfo starting 30 81 59, was rejected by d2i_PKCS8_PRIV_KEY_INFO, EVP_PKCS82PKEY (for the inner key), d2i_PUBKEY, d2i_RSA_PUBKEY, d2i_PKCS8_PRIV_KEY_INFO_bio, d2i_PrivateKey_bio and d2i_RSAPublicKey_bio.

ReadTlv now reads a length as asn1_get_length of 3.4.1 does: leading zero octets are skipped, more than eight significant octets or a value above INT64_MAX fail, any other long form is accepted. The BIO reader, which those three BIO functions share, accepts up to six length octets and fails with ASN1_R_HEADER_TOO_LONG beyond, which is what asn1_d2i_read_bio of 3.4.1 does with its eight-octet header read.

Indefinite lengths are still refused, where 3.4.1 accepts them on constructed values; the new debt entry says so. The pending debt entry of d2i_RSAPublicKey_bio, which named the non-minimal refusal, is reworded. A first length octet of 0xff (127 length octets) is accepted as in 3.4.1; LibreSSL 4.0.0 refuses it, and the entry names that too.

Tested

Ubuntu 26.04 x86-64 container under Rosetta on macOS 27.2, GCC 15.2. A harness takes 13 PKCS#8 keys written by LibreSSL 3.4.1 (RSA 512 to 2048 bits, EC P-256, P-384, P-521, secp256k1, P-224, keys without a public point, explicit parameters), applies one seeded mutation (non-minimal lengths on one, a few or every header, bit flips, truncation, deleted, duplicated or swapped elements, changed tags, trailing bytes, indefinite lengths), and calls d2i_PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO, EVP_PKCS82PKEY, EVP_PKEY2PKCS8, d2i_PKCS8_PRIV_KEY_INFO_bio and d2i_PrivateKey_bio, each case in its own process. The same calls run against LibreSSL 3.4.1 built from the submodule.

Inputs that 3.4.1 decodes and that are refused before this change and decoded after it:

seeds 20261121 to 20264120 (3000 cases) seeds 7 to 3006 (3000 cases)
d2i_PKCS8_PRIV_KEY_INFO 701 743
EVP_PKCS82PKEY returning a key 733 716
d2i_PKCS8_PRIV_KEY_INFO_bio 701 743
d2i_PrivateKey_bio 733 716

No other decision changes in either range: no input goes from decoded to refused, none that 3.4.1 refuses becomes accepted, and no output that matched 3.4.1 before differs after. No signal and no exception.

A second run rewrites the outer header of each key with 1 to 10, 126 and 127 length octets. For the 12 keys other than the explicit-parameters one, which is refused before and after for its parameters, every width that can hold the length now decodes in memory, as in 3.4.1; through a BIO, widths up to 6 decode and 7 or more fail with reason 123, as in 3.4.1.

guest_libressl3_key_der gains TestNonMinimalLengths: an RSA PKCS#8 with 3, 4, 8, 9, 126 and 127 length octets must decode to the same key and re-encode to the canonical bytes, and through the two BIO functions must decode up to 6 octets and fail with ASN1_R_HEADER_TOO_LONG beyond; an EC public key with 81 59, 82 00 59 and 127 octets through d2i_PUBKEY; an RSA public key with 5 octets through d2i_RSA_PUBKEY; an EC PKCS#8 whose OCTET STRING and inner key carry non-minimal lengths; the NULL parameter written 05 81 00 decodes and 05 80 is refused; nine significant octets, a value above INT64_MAX and a length of 2^32 are refused. Six mutations of the change each make the test fail: either minimality check put back in ReadTlv (lines 890 and 940), the BIO limit raised to 8 (line 912), the leading-zero refusal put back in the BIO reader (line 909), the eight-octet limit removed (line 983), the INT64_MAX check removed (line 995).

d2i_RSAPublicKey_bio reads through the same BIO reader, and guest_libressl3_rsa_public_key_bio asserted the old refusal of 30 81 05 …. That assertion now uses a seven-octet length, which 3.4.1 also refuses with reason 123, and TestBerHeaders requires 30 81 06, 30 82 00 06 and 30 86 00 00 00 00 00 06 to decode and seven and eight octets to fail. A second harness prints the result and the error queue of d2i_RSAPublicKey_bio on 18 fixed inputs: 7 non-minimal forms that 3.4.1 decodes are refused before and decoded after, and no decision differs from 3.4.1 after.

d2i_PUBKEY and d2i_RSA_PUBKEY are covered by the unit test only, not by the harness. The PEM readers reach the same decoders and were not measured. Still different from 3.4.1 and not changed here: indefinite lengths, the curves other than P-256 and P-384, explicit EC parameters, and the error codes left on the queue after a refusal. A PKCS#8 key whose attributes are malformed inside, or whose algorithm OID is badly encoded, is now decoded when another of its headers is non-minimal, because main already accepts the same key in DER: attributes are checked one level deep and kept as read, and the OID is not checked. After a refused header, the BIO readers leave the bytes after the length byte unread, where 3.4.1 has consumed its eight-byte header read. d2i_RSAPublicKey_bio still refuses a first length octet 0xff inside the key, as LibreSSL 4.0.0 does.

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

python3 tools/check_conventions.py --base upstream/main passes. python3 tools/pr_overlap.py --names for these functions lists #22277, which changes how the d2i_PUBKEY and d2i_PKCS8_PRIV_KEY_INFO exports read and advance their input pointers, #24999, which changes other parts of KeyDer.cpp for DSA keys, #11466, which changes the parent library only, and the refactors #24322, #24910 and #24917, which split libraries; --branch also reports #11146, which it cannot compare because that PR conflicts with main and which changes how DecodeEcPrivateKey and EncodeEcPrivateKey in KeyDer.cpp set and write the EC private key; none changes the two readers changed here, ReadTlv and ReadSingleDerObjectFromBio. #24999, #24943 and #24953 also edit the lines next to the pending debt entry corrected here, in the same fragment file, so whichever merges later has to rebase that file; #24943 and #24953 change no code that this change touches.

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

d2i_PUBKEY, d2i_RSA_PUBKEY, d2i_PKCS8_PRIV_KEY_INFO, EVP_PKCS82PKEY and
the BIO readers d2i_PKCS8_PRIV_KEY_INFO_bio, d2i_PrivateKey_bio and
d2i_RSAPublicKey_bio refused a long-form length below 128 or with a
leading zero octet with reason 102, so a key that the pinned 078b3d1d
LibreSSL decodes, such as a PKCS#8 whose outer SEQUENCE is written
30 82 00 xx, was not decoded. The in-memory reader now reads lengths as
asn1_get_length of that version does: leading zeros are skipped before
the eight-octet limit and any long form is accepted. The BIO reader
accepts up to six length octets and fails with ASN1_R_HEADER_TOO_LONG
beyond, as asn1_d2i_read_bio does with its eight-octet header read.
Indefinite lengths are still refused; the new debt entry says so.

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

@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 bc4f4fc850:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_key_der_tests, guest_libressl3_rsa_public_key_bio_tests built cleanly.
  • CTest (guest_libressl3_key_der, guest_libressl3_rsa_public_key_bio):
    1/2 Test #4759: guest_libressl3_rsa_public_key_bio ...   Passed    0.02 sec
    2/2 Test #4758: guest_libressl3_key_der ..............   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #4759: guest_libressl3_rsa_public_key_bio ...Subprocess aborted***Exception:   0.14 sec
    2/2 Test #4758: guest_libressl3_key_der ..............Subprocess aborted***Exception:   0.17 sec
    0% tests passed, 2 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified bounds safety and offset calculations in ReadTlv (core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp): checked that input.size() - offset < count prevents underflow and that the while-loop skipping leading zero octets preserves invariant offset + count <= input.size().
    • Verified integer overflow safety in ReadTlv: confirmed that non-zero length octet count is strictly bounded by sizeof(std::size_t) and values exceeding INT64_MAX return Asn1RBadObjectHeader (102), matching LibreSSL 3.4.1 asn1_get_length.
    • Verified ReadSingleDerObjectFromBio (core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp): confirmed lengths up to 6 octets are read without rejecting leading zeros or values < 128, and counts > 6 octets fail with Asn1RHeaderTooLong (123), matching LibreSSL 3.4.1 asn1_d2i_read_bio.
    • Verified LP64/LLP64 portability: confirmed all size and offset manipulations use std::size_t and fixed-width std::int64_t limits without 32-bit narrowing or platform-dependent type assumptions.
    • Verified AGENTS.md compliance: confirmed no code comments were introduced, the PR title follows Conventional Commits, and technical debt entries in docs/dev/debt/changes/ properly record gaps for indefinite length refusal.
    • Verified test coverage in KeyDer.cpp and RsaPublicKeyBio.cpp: verified coverage for non-minimal lengths across various byte counts (1, 2, 3, 4, 5, 6, 8, 9, 126, 127), leading zero padding, BIO limits, and boundary error conditions.

Looks Good To Me

@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

The change appears consistent with the stated BER length handling; I did not run the tests or convention check, so the review does not establish that those checks pass.

What I ran

Reviewed head bc4f4fc850d65484061dbb9a0a0a4b801f54815b; no commands or tests were run. The description reports author-run harness and convention checks, which I did not independently verify.

Results

Check Result Note
Changed code review passed Reviewed all changed files
Tests and convention check not run Author reports these passed

Findings

Blocking

None.

Non-blocking

None.

Sources

Reviewed the implementation at KeyDer.cpp and its tests.

Not verified

Tests, convention check, console behavior, Windows and macOS behavior were not run or confirmed. The review folder listed all five changed files as included; I read each: core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp, core/libs/prx/libSceLibreSsl3/tests/KeyDer.cpp, core/libs/prx/libSceLibreSsl3/tests/RsaPublicKeyBio.cpp, docs/dev/debt/changes/feat-libressl3-rsa-free-rsapublickey-bio.md, and docs/dev/debt/changes/fix-libscelibressl3-key-ber-lengths.md. No changed files were excluded.

To get this to a good state

Nothing is required.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 26409 docs/dev/debt/changes/feat-libressl3-rsa-free-rsapublickey-bio.md L6
PR 26410 docs/dev/debt/changes/feat-libressl3-rsa-free-rsapublickey-bio.md L6
PR 26414 docs/dev/debt/changes/feat-libressl3-rsa-free-rsapublickey-bio.md L6
PR 26425 docs/dev/debt/changes/feat-libressl3-rsa-free-rsapublickey-bio.md L6

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 11146 core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp
core/libs/prx/libSceLibreSsl3/tests/KeyDer.cpp

@Helldiverr

Copy link
Copy Markdown
Contributor

On Windows the merge builds, and guest_libressl3_key_der, guest_libressl3_rsa_public_key_bio, guest_libressl3_pem_keys and guest_libressl3_x509_sign pass 3/3. I found nothing blocking. The two readers now follow LibreSSL 3.4.1 asn1_get_length and asn1_d2i_read_bio, line for line. The new test catches every one of my six mutations, including the 0xff rule.

Existing runs:

  • The author, kessler-drift, ran the harness and the tests in an Ubuntu 26.04 container under Rosetta on macOS (GCC 15.2). The description says it was "Not tested on a console, on Windows or on macOS".
  • PrivacyOnion approved on Linux (Ubuntu 24.04, GCC 13.3) at this head, bc4f4fc850.
  • MarcZX10 commented at this head after reading the code. That review ran no build or tests.
  • The CI windows job passed at this head.

The PR is based on main. It edits the pending fragment feat-libressl3-rsa-free-rsapublickey-bio.md, which is already on main (from 365ab09411), so it does not depend on another PR.

What this run adds.

  • A Windows build of the head merged onto main, with three runs. These include guest_libressl3_pem_keys and guest_libressl3_x509_sign, which reach the same decoders.
  • The negative control.
  • Six mutations.
  • A check of both readers against the pinned 3.4.1 and 4.0.0 sources.

Code.

  • ReadTlv (core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp:432-468 at the head) matches 3.4.1 asn1_get_length:

    • Leading zero octets are skipped (:446-449, L175-179).
    • More than eight significant octets fail (:450, L180).
    • A value above INT64_MAX fails (:456, L189).
    • 0x80 is still refused (:442).

    3.4.1 compares against sizeof(long) and LONG_MAX. Those are 8 and INT64_MAX on the console, but 4 bytes on a Windows host, so using sizeof(std::size_t) and INT64_MAX keeps both builds at the console values.

  • ReadSingleDerObjectFromBio (:1263-1307) matches 3.4.1 asn1_d2i_read_bio. That reader reads an 8-byte header (HEADER_SIZE, L146), which leaves six length octets after a one-byte tag. With seven or more, ASN1_get_object fails with ASN1_R_HEADER_TOO_LONG (asn1_lib.c L136-137, L151-153), and the reader returns that reason (a_d2i_fp.c L195-200), as :1296-1298 does. The BIO reader reads a one-byte tag, as on main, so the fixed six matches 3.4.1 for every one-byte tag.

  • Non-blocking: after this PR the module applies two rules to a first length octet of 0xff:

    • The key decoders accept it, as in 3.4.1.
    • The module's own ASN1_get_object refuses it (core/libs/prx/libSceLibreSsl3/src/IntegerDecoder.cpp:70-71 on main ca01126e05), as 4.0.0 asn1_get_length_cbs does. The description says the inner key reader of d2i_RSAPublicKey_bio refuses it too; I did not test that.

    The fragment names 4.0.0's refusal and closes on the console, so this is recorded. A one-line pointer in the ASN1_get_object entry would keep the two entries from reading as if they contradict each other.

  • Non-blocking: every refusal in ReadTlv queues reason 102 only (:443, :451, :457). In 3.4.1, ASN1_get_object queues ASN1_R_HEADER_TOO_LONG (123) for every length it refuses (L136-137, L151-153). The description lists "the error codes left on the queue after a refusal" as still different, but the new fragment does not.

Negative control and mutations. Each change was made on the merge, rebuilt, and run with guest_libressl3_key_der and guest_libressl3_rsa_public_key_bio. Test lines are in tests/KeyDer.cpp unless marked RsaPublicKeyBio.cpp.

change runs result
control: src/KeyDer.cpp from main, new tests kept 1 both abort: line 890, and RsaPublicKeyBio.cpp:310
m1: ReadTlv refuses a leading zero octet again (:446-449) 1 key_der aborts at line 890
m2: BIO limit raised to 8 octets (:1295) 1 both abort: line 912, and RsaPublicKeyBio.cpp:309
m3: INT64_MAX check removed (:456-459) 1 key_der aborts at line 1021
m4: eight-octet limit removed (:450-453) 1 key_der aborts at line 1009
m5: ReadTlv refuses 0xff, as 4.0.0 does (:442) 1 key_der aborts at line 890 (the 127-octet case)
m6: BIO reader refuses a long-form value below 128 again (after :1306) 1 rsa_public_key_bio aborts at RsaPublicKeyBio.cpp:361; key_der passes

The test's BIO cases in TestNonMinimalLengths (tests/KeyDer.cpp:876) all carry lengths of 128 or more, so m6 is caught only by the RsaPublicKeyBio.cpp cases.

Tests. ctest --test-dir build -R "^guest_libressl3_(key_der|rsa_public_key_bio|pem_keys|x509_sign)$" -V

test main merge 1 2 3
guest_libressl3_key_der Passed 0.06 s Passed 0.07 s Passed 0.14 s Passed 0.05 s
guest_libressl3_rsa_public_key_bio Passed 0.06 s Passed 0.12 s Passed 0.04 s Passed 0.03 s
guest_libressl3_pem_keys Passed 0.32 s Passed 0.34 s Passed 0.29 s Passed 0.28 s
guest_libressl3_x509_sign Passed 0.15 s Passed 0.08 s Passed 0.05 s Passed 0.03 s

On the merge, tools/check_conventions.py --base ca01126e05 passes, and tools/debt_fold.py --check exits 0. tools/debt_fold.py --show libSceLibreSsl3 lists the new "Key DER long-form lengths" entry and the reworded d2i_RSAPublicKey_bio entry.

Host:

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2.0 (UCRT, POSIX threads, SEH) r7, Release
  • Commits: head bc4f4fc850, merged onto main ca01126e05 as 20656a1c85

AI-assisted: yes

@oneandonlydean
oneandonlydean merged commit d195a92 into boykopovar:main Nov 24, 2026
14 checks passed
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.

5 participants