Repository navigation
fix(libSceLibreSsl3): accept BER long-form lengths in the key decoders - #25390
oneandonlydean merged 1 commit into
Conversation
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
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against bc4f4fc850:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_libressl3_key_der_tests,guest_libressl3_rsa_public_key_bio_testsbuilt 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.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.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 - 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
left a comment
There was a problem hiding this comment.
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.
|
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.
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.
|
|
On Windows the merge builds, and Existing runs:
The PR is based on What this run adds.
Code.
Negative control and mutations. Each change was made on the merge, rebuilt, and run with
The test's BIO cases in Tests.
On the merge, Host:
AI-assisted: yes |
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-openbsdsubmodule) decodes these, so a key such as a PKCS#8 whose outer SEQUENCE is written30 82 00 xx, or an ECSubjectPublicKeyInfostarting30 81 59, was rejected byd2i_PKCS8_PRIV_KEY_INFO,EVP_PKCS82PKEY(for the inner key),d2i_PUBKEY,d2i_RSA_PUBKEY,d2i_PKCS8_PRIV_KEY_INFO_bio,d2i_PrivateKey_bioandd2i_RSAPublicKey_bio.ReadTlvnow reads a length asasn1_get_lengthof 3.4.1 does: leading zero octets are skipped, more than eight significant octets or a value aboveINT64_MAXfail, any other long form is accepted. The BIO reader, which those three BIO functions share, accepts up to six length octets and fails withASN1_R_HEADER_TOO_LONGbeyond, which is whatasn1_d2i_read_bioof 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_bioandd2i_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:
d2i_PKCS8_PRIV_KEY_INFOEVP_PKCS82PKEYreturning a keyd2i_PKCS8_PRIV_KEY_INFO_biod2i_PrivateKey_bioNo 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_dergainsTestNonMinimalLengths: 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 withASN1_R_HEADER_TOO_LONGbeyond; an EC public key with81 59,82 00 59and 127 octets throughd2i_PUBKEY; an RSA public key with 5 octets throughd2i_RSA_PUBKEY; an EC PKCS#8 whose OCTET STRING and inner key carry non-minimal lengths; the NULL parameter written05 81 00decodes and05 80is refused; nine significant octets, a value aboveINT64_MAXand a length of 2^32 are refused. Six mutations of the change each make the test fail: either minimality check put back inReadTlv(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), theINT64_MAXcheck removed (line 995).d2i_RSAPublicKey_bioreads through the same BIO reader, andguest_libressl3_rsa_public_key_bioasserted the old refusal of30 81 05 …. That assertion now uses a seven-octet length, which 3.4.1 also refuses with reason 123, andTestBerHeadersrequires30 81 06,30 82 00 06and30 86 00 00 00 00 00 06to decode and seven and eight octets to fail. A second harness prints the result and the error queue ofd2i_RSAPublicKey_bioon 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_PUBKEYandd2i_RSA_PUBKEYare 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, becausemainalready 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_biostill 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/mainpasses.python3 tools/pr_overlap.py --namesfor these functions lists #22277, which changes how thed2i_PUBKEYandd2i_PKCS8_PRIV_KEY_INFOexports read and advance their input pointers, #24999, which changes other parts ofKeyDer.cppfor DSA keys, #11466, which changes the parent library only, and the refactors #24322, #24910 and #24917, which split libraries;--branchalso reports #11146, which it cannot compare because that PR conflicts withmainand which changes howDecodeEcPrivateKeyandEncodeEcPrivateKeyinKeyDer.cppset and write the EC private key; none changes the two readers changed here,ReadTlvandReadSingleDerObjectFromBio. #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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code