Skip to content

fix(libSceLibreSsl3): follow rsa_pub_decode for RSA keys in SubjectPublicKeyInfo - #25081

Closed
ToRaNek wants to merge 3 commits into
boykopovar:mainfrom
ToRaNek:fix/libressl3-rsa-spki-parameter-and-trailing
Closed

ToRaNek wants to merge 3 commits into
boykopovar:mainfrom
ToRaNek:fix/libressl3-rsa-spki-parameter-and-trailing

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

What

Stacked on #25008: only the last commit is new. It is the public-key counterpart of that PR.

For an RSA key in a SubjectPublicKeyInfo, d2i_PUBKEY and d2i_RSA_PUBKEY in libSceLibreSsl3 were stricter than LibreSSL 4.0.0 in two ways. The source is rsa_pub_decode:

4.0.0 this port before
AlgorithmIdentifier parameter that is not NULL ignored: rsa_param_decode returns 1 at once unless the OID is RSA-PSS key refused
bytes after the RSAPublicKey inside the BIT STRING left unread: d2i_RSAPublicKey(NULL, &p, pklen) stops at the end of the sequence and nothing checks the cursor key refused with ASN1_R_SEQUENCE_LENGTH_MISMATCH

PEM_read_bio_PUBKEY goes through d2i_PUBKEY and gets the same change.

The parameter. It is an ANY in the source, decoded before the key is looked at, so a malformed one still fails. The RSA branch of ParseAlgorithm now accepts:

  • no parameter, or NULL with empty content, as before;
  • an OID, validated as c2i_ASN1_OBJECT_cbs does (not empty, no arc starting with 0x80, last arc complete), else ASN1_R_INVALID_OBJECT_ENCODING. The validation copies oid_parse_arc, including its 64-bit accumulator: an arc that wraps to zero and is followed by 0x80 is refused, as in the source;
  • a SEQUENCE, whose content is kept unparsed in the source.

Not changed:

Still refused where 4.0.0 accepts (listed in the debt fragment): a parameter of another ANY type (integer, strings, set, ...), a BIT STRING with 1 to 7 unused bits, and the RSA-PSS OID. The first is the same subset the PKCS#8 decoder has after #24999.

Tested

Windows 11, MinGW GCC 15.2. Not tested on a console.

One new test function in guest_libressl3_key_der, built from the RSA and P-256 SubjectPublicKeyInfo vectors the file already has:

  • 9 parameters (none, NULL, four OIDs, three SEQUENCEs) times 4 endings (none, 1, 2 and 3 extra bytes), through d2i_PUBKEY and d2i_RSA_PUBKEY: key returned, cursor at the end of the structure, queue empty, same n and e as the reference key;
  • 6 RSAPublicKey values that do not decode, under each parameter: null, cursor untouched, queue = ASN.1 errors, then ERR_R_RSA_LIB, then X509_R_PUBLIC_KEY_DECODE_ERROR;
  • 9 parameters that are refused: five bad OID encodings (first error ASN1_R_INVALID_OBJECT_ENCODING), two parameters, a truncated one, NULL with content, a primitive SEQUENCE tag;
  • EC unchanged: P-256 with a NULL parameter, with none, and with a byte after the point are still refused.

With KeyDer.cpp from #25008 the test fails at the first accepted case that the old code refused; with the change it passes, and guest_libressl3_pem_keys still passes.

Negative controls. 13 mutations of KeyDer.cpp, each rebuilt and run on its own, all make the test fail: trailing bytes refused again, OID or SEQUENCE refused, any tag accepted, the OID not validated, NULL with content accepted, another error code for a bad OID, and six on the OID validation itself (empty content, leading 0x80, 0x80 refused everywhere, truncated arc, no wrap, a 32-bit accumulator).

python tools/check_conventions.py --base origin/main and python tools/debt_fold.py --check pass.

Not verified. The expected queues come from reading the source, not from running a LibreSSL 4.0.0 binary.

Overlap (python tools/pr_overlap.py --branch fix/libressl3-rsa-spki-parameter-and-trailing):

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: fix(libSceLibreSsl3): follow rsa_priv_decode and eckey_priv_decode in EVP_PKCS82PKEY #25008
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

ToRaNek and others added 3 commits November 21, 2026 15:44
… EVP_PKCS82PKEY

EVP_PKCS82PKEY differed from LibreSSL 4.0.0 for RSA and EC keys:

- it refused an rsaEncryption AlgorithmIdentifier whose parameter is
  not NULL, where rsa_param_decode ignores the parameter unless the key
  is RSA-PSS;
- it refused bytes after the RSAPrivateKey or the ECPrivateKey inside
  the OCTET STRING, where d2i_RSAPrivateKey and d2i_ECPrivateKey leave
  them unread;
- on an RSA key that does not decode it queued ERR_R_RSA_LIB before
  EVP_R_PRIVATE_KEY_DECODE_ERROR. Only old_rsa_priv_decode, the
  traditional path, raises that one.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Pkcs8WithKeySuffix added DerHeaderLength(der, offset, length) and length
in one expression. The call writes length and the other operand reads
it, and the order of the two is unspecified.

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

d2i_PUBKEY and d2i_RSA_PUBKEY were stricter than LibreSSL 4.0.0 for an
rsaEncryption key:

- they refused an AlgorithmIdentifier parameter that is not NULL, where
  rsa_param_decode ignores the parameter unless the key is RSA-PSS;
- they refused bytes after the RSAPublicKey inside the BIT STRING,
  where d2i_RSAPublicKey leaves them unread.

An OID or SEQUENCE parameter is now accepted, an OID being validated as
c2i_ASN1_OBJECT_cbs does. EC keys are unchanged.

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 2fc2aa3098:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_key_der_tests built cleanly.
  • CTest (guest_libressl3_key_der):
    1/1 Test #4730: guest_libressl3_key_der ..........   Passed    0.23 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4730: guest_libressl3_key_der ..........Subprocess aborted***Exception:   0.70 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified ValidObjectEncoding in core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp:758-772 correctly implements ASN.1 OID arc parsing, rejects empty content, leading 0x80 arc bytes, and unclosed variable-length arc octets
    • Verified ParseAlgorithm in core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp:781-805 validates parameter TLVs for RSA keys, properly allowing absent parameters, NULL (0x05) with empty payload, valid OID (0x06), and SEQUENCE (0x30), setting ErrLibAsn1 errors appropriately
    • Verified DecodeSpki in core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp:868 passes allowTrailing=true to DecodeRsaPkey matching upstream LibreSSL rsa_pub_decode behavior
    • Verified technical debt fragment docs/dev/debt/changes/fix-libressl3-rsa-spki-parameter-and-trailing.md accurately documents remaining divergences and conforms to AGENTS.md debt guidelines
    • Verified comprehensive unit test coverage in core/libs/prx/libSceLibreSsl3/tests/KeyDer.cpp:506-621 asserting valid parameters, trailing bytes handling, broken RSA key payloads, and invalid parameter encodings with appropriate error stack checks

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 — looks good

Nothing blocks this change; the inspected implementation and tests support merging.

What I ran

Reviewed head 2fc2aa3098e536a1a1b2668327d777face2f1464 against base main. I read the prepared description, commits, diff, manifest, and all changed lines in KeyDer.cpp, tests/KeyDer.cpp, and the debt fragment. I inspected the touched DER reader and RSA decoder context with repo_read.py. No commands or tests were run during this review.

Results

Check Result Note
Diff and changed lines passed All changed files read
Targeted implementation context passed RSA SPKI parsing and decode path
Tests and prechecks not run Author-reported only

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author reports Windows 11/MinGW tests, convention and debt checks, and mutation controls; these were not run independently. Console behavior and equivalence against a running LibreSSL 4.0.0 binary were not verified. No files were excluded from the review bundle.

To get this to a good state

Nothing is required.

@github-actions

Copy link
Copy Markdown

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

@ToRaNek

ToRaNek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Resubmitted as #26415. This pull request was closed when my fork left the fork network (I switched it to private by mistake), and GitHub does not let it be reopened. #26415 has the same head commit, title and description.

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.

3 participants