Skip to content

feat(libSceLibreSsl3): read DSA public keys and DSA keys through d2i_PrivateKey_bio - #26425

Closed
ToRaNek wants to merge 14 commits into
boykopovar:mainfrom
ToRaNek:feat/libressl3-dsa-public-keys
Closed

ToRaNek wants to merge 14 commits into
boykopovar:mainfrom
ToRaNek:feat/libressl3-dsa-public-keys

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What

Resubmission of #25445, which was closed when my fork left the fork network (I switched it to private by mistake) and cannot be reopened. Same head commit a9981c5159c; the reviews and the discussion are on #25445.

Stacked on #26409, #26410 and #26414: only the last two commits are new (the change and a merge of main).

Two DSA paths that #26414 left failing and listed in its debt entry. Reference: LibreSSL 4.0.0, libressl/openbsd@ce488b78.

1. A DSA PUBLIC KEY. d2i_PUBKEY and PEM_read_bio_PUBKEY refused a DSA SubjectPublicKeyInfo with X509_R_UNSUPPORTED_ALGORITHM. They now follow dsa_pub_decode:

Parameters of the AlgorithmIdentifier Result
a SEQUENCE (p, q, g) key with its parameters and public number, checked with dsa_check_key
absent, or NULL key that holds only its public number, not checked
any other type DSA_R_PARAMETER_ENCODING_ERROR
	/* We can only check for key consistency if we have parameters. */
	if (ptype == V_ASN1_SEQUENCE) {
		if (!dsa_check_key(dsa))
			goto err;
	}

Bytes after the INTEGER inside the BIT STRING are left alone, as d2i_ASN1_INTEGER leaves them. The five DSA object identifiers give the same key type, as they already did for private keys. A failure ends with X509_R_PUBLIC_KEY_DECODE_ERROR, which X509_PUBKEY_get0 adds.

2. A traditional DSA key through d2i_PrivateKey_bio. d2i_AutoPrivateKey picks the type from the number of elements of the outer SEQUENCE:

	if (sk_ASN1_TYPE_num(inkey) == 6)
		keytype = EVP_PKEY_DSA;
	else if (sk_ASN1_TYPE_num(inkey) == 4)
		keytype = EVP_PKEY_EC;

The port tried six elements as RSA and failed. It now calls the decoder that the DSA PRIVATE KEY label already uses (d2i_PrivateKey for EVP_PKEY_DSA).

Left as it was, in the debt fragment: i2d_PUBKEY is a stub, so a DSA public key is read but not written; an algorithm parameter that is not NULL, an OID or a SEQUENCE is refused by the AlgorithmIdentifier reader before dsa_pub_decode sees it.

Tested

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

  • guest_libressl3_pem_keys, two new functions. The public halves of the two private test keys (1024 and 2048 bits) were produced by OpenSSL 3.2.4 (openssl pkey -pubout); the test checks that they are byte for byte the DER it builds itself, then reads them through d2i_PUBKEY and PEM_read_bio_PUBKEY and compares p, q, g and the public number. Then: the five object identifiers; absent and NULL parameters; a public number of 1, accepted without parameters and refused with them; bytes after the INTEGER; an OID as parameter, a SEQUENCE of two integers, an OCTET STRING as key, a public number equal to p and an even q, each with its whole error queue. For d2i_PrivateKey_bio: both traditional keys, and two six-element inputs that fail, whose error queue has to be the one the DSA PRIVATE KEY label gives for the same bytes, without the PEM error.
  • With the three source files as they are without this change the test fails; with the change it passes, and guest_libressl3_key_der passes without a change to its file.
  • Negative controls: 20 mutations, each rebuilt and run on its own, all make the test fail: the DSA identifiers not recognised, or only the main one; the public key branch removed; the final X509_R_PUBLIC_KEY_DECODE_ERROR dropped; the input pointer not advanced; parameters required, NULL parameters refused, another parameter type accepted or reported as a decode error; parameters not read, read with the private key layout, or failing without DSA_R_DECODE_ERROR; the key INTEGER failing without its error; the number stored as the private one; the key never checked, or checked without parameters; six elements read as RSA or as EC, and five or nine elements read as DSA.
  • python tools/check_conventions.py --base origin/main and python core/libs/tests/test_public_headers.py --audit-only pass. python tools/debt_fold.py --check fails for the reason it fails on main today (two libkernel_sys fragments, docs(debt): retarget two libkernel_sys fragments at their libkernel entries #25313 and fix(docs): open the libkernel section for two debt fragment entries #25318).

Not verified. The expected results come from reading the LibreSSL source and from the OpenSSL-made vectors, not from running the console module.

Overlap (python tools/pr_overlap.py --branch feat/libressl3-dsa-public-keys):

Checklist

ToRaNek and others added 10 commits November 21, 2026 12:55
PEM_read_bio_PrivateKey, PEM_read_RSAPrivateKey, PEM_read_bio_PUBKEY and
PEM_write_bio_PrivateKey threw NotImplemented for every encrypted record
and for every cipher passed to the writer.

Follow LibreSSL 4.0.0 for both formats:

- Proc-Type and DEK-Info records: PEM_get_EVP_CIPHER_INFO, PEM_do_header
  and EVP_BytesToKey with AES-128/192/256-CBC.
- ENCRYPTED PRIVATE KEY records: the X509_SIG, PBES2 and PBKDF2
  structures with the tasn_dec.c rules, then PKCS12_pbe_crypt,
  EVP_PBE_CipherInit and the PBES2 and PBKDF2 key generators with the
  HMAC digests this port has.
- Writing with a cipher: PKCS8_encrypt with PKCS5_pbe2_set.
- The pass phrase from the callback, or from u when the callback is
  null.

Ciphers, PBE schemes and digests this port has no primitive for, the
terminal prompt and three BER forms still throw; the debt fragment lists
them.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The PEM readers decrypted AES records only. Add the DES family from
LibreSSL 4.0.0:

- DEK-Info records in DES-CBC, DES-EDE-CBC and DES-EDE3-CBC.
- PBES2 with the DES-CBC and DES-EDE3-CBC OIDs.
- PBES1 (PKCS5_PBE_keyivgen): PBE-MD5-DES and PBE-SHA1-DES.
- The PKCS#12 schemes PBE-SHA1-3DES and PBE-SHA1-2DES, with
  PKCS12_key_gen_uni.

The blocks are decrypted with mbedtls and the checks of
EVP_DecryptFinal_ex are redone on the result, with their errors.

The two MD2 schemes now queue EVP_R_UNKNOWN_DIGEST, as 4.0.0 has no MD2,
instead of throwing.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A DSA PRIVATE KEY record threw, and a DSA key in PKCS#8 was an
unsupported algorithm. Follow LibreSSL 4.0.0:

- old_dsa_priv_decode: d2i_DSAPrivateKey, dsa_check_key, then q divides
  p - 1, g has order q and q is prime, each with its error.
- dsa_priv_decode for PKCS#8, with the four alias OIDs; the public key
  is computed from the private one.
- dsa_priv_encode, so PEM_write_bio_PrivateKey writes the key back.
- The DSA object has the dsa_st layout; EVP_PKEY_free releases it.

The ASN.1 reader of PemEncryption.cpp moves to Asn1Reader.hpp so both
files share it. Bignum arithmetic comes from mbedtls.

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

d2i_PUBKEY and PEM_read_bio_PUBKEY refused a DSA SubjectPublicKeyInfo
as an unsupported algorithm. They now follow dsa_pub_decode of LibreSSL
4.0.0: parameters as a SEQUENCE are decoded and the key is checked with
dsa_check_key; absent or NULL parameters give a key that holds only its
public number; any other parameter type is a parameter encoding error.

d2i_PrivateKey_bio read a traditional DSA key, a SEQUENCE of six
elements, as an RSA key and failed. d2i_AutoPrivateKey counts the
elements and takes six as DSA.

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

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
main dropped the file's GuestWritablePreservingErrno wrapper now that
GuestWritable_nid_no_patch keeps errno itself; use it for the slot a
PEM private key read replaces.

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

@catminou4 catminou4 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.

Built libSceLibreSsl3 and ran guest_libressl3_pem_keys_tests on Linux (x86-64, GCC 11): passes (exit 0). Large new surface (RSA_free, d2i_RSAPublicKey_bio, encrypted PEM, DSA keys, PKCS#8 KeyDer), so no meaningful main control.

The debt fragment is exemplary — it pins each behavior to a specific LibreSSL 4.0.0 source file and line, states plainly what is "not measured on a PS5," and enumerates the exact gaps (unsupported ciphers/digests, missing i2d_PUBKEY, Miller-Rabin vs Baillie-PSW, meth-carrying keys throwing on free). Two things worth a maintainer's eye before merge, both already flagged in the note: DSA_new does not exist yet so meth stays null and DSA public-key writes are stubs, and EVP_PKEY_ASN1_METHOD is a static list so writes are always PKCS#8. Nothing here looks like made-up success — the unimplemented paths throw through NotImplemented_nid_no_patch.

@github-actions

github-actions Bot commented Nov 23, 2026 •

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/PemKeys.cpp
PR 12315 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 17241 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 17249 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 17621 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 17626 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 18145 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 18146 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 18147 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 19024 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 19033 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 19044 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 19483 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 20967 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 21684 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 21691 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 21692 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 21702 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 21706 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 23573 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 25901 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 26258 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 26672 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 27521 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp
PR 27715 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 28236 core/libs/prx/libSceLibreSsl3/CMakeLists.txt

@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 610cc26e76:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_pem_keys_tests built cleanly.
  • CTest (guest_libressl3_pem_keys):
    1/1 Test #4826: guest_libressl3_pem_keys .........   Passed    7.11 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp, core/libs/prx/libSceLibreSsl3/src/PemKeys.cpp, core/libs/prx/libSceLibreSsl3/src/PkeyLifecycle.cpp, core/libs/prx/libSceLibreSsl3/src/DsaKeys.cpp, core/libs/prx/libSceLibreSsl3/src/PemEncryption.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4826: guest_libressl3_pem_keys .........Subprocess aborted***Exception:   0.50 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified DsaDecodePublicKey_nid_no_patch against LibreSSL 4.0.0 dsa_pub_decode parameter handling (SEQUENCE, absent/NULL, invalid types) and error codes
    • Checked DsaDecodeTraditionalKey_nid_no_patch and DsaDecodePkcs8Key_nid_no_patch for memory leaks and resource lifetime management
    • Verified ASN.1 DER decoding logic in Asn1Reader.hpp for integer bounds, tag parsing, and negative zero/two's complement edge cases
    • Validated DSA_free / DsaFree_nid_no_patch guest pointer memory access checks (GuestCopyIn_nid_no_patch, GuestWritable_nid_no_patch) and atomic reference counting
    • Confirmed AGENTS.md compliance regarding comments, conventions, and absence of undocumented silent stubs or hardcoded title IDs

Looks Good To Me

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 24, 2026

@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 targeted DSA public key and BIO decoding paths show no verified blocking defect at head 610cc26e764296ee59e2820ca8cfa871f8304f70; the full change was not reviewed.

What I ran

Read the prepared description, commit list, manifest, and relevant diff excerpts. Read DsaKeys.cpp lines 1–220 and KeyDer.cpp lines 1450–1615 through repo_read.py. No commands or tests were run.

Results

Check Result Note
Targeted code review not run Partial inspection only
Tests not run No local execution
Console behavior not run Not verified

Negative control

Not run; the reported mutations were supplied by the author and were not independently executed.

Findings

Blocking

None identified in the inspected DSA decode paths.

Non-blocking

None.

Sources

Reviewed the prepared diff for KeyDer.cpp and DsaKeys.cpp, including the DSA public-key decoder and the d2i_PrivateKey_bio dispatch changes. Those claims are limited to the inspected code; no commit-pinned links were available through the reader.

Not verified

The manifest lists 10 changed files, all included. Full text was read for 2 changed files: DsaKeys.cpp (partial range) and KeyDer.cpp (partial range). The other 8 changed files were not read in full; the diff was inspected only around the targeted DSA paths and test excerpts. Tests, build checks, CI, console behavior, and the author-reported test results were not independently verified.

To get this to a good state

Nothing is required. Optional: complete review of the remaining changed files and run the reported checks before relying on this as a full PR review.

…ted-pem-private-keys

# Conflicts:
#	docs/dev/debt/changes/feat-libressl3-rsa-free-rsapublickey-bio.md
…bressl3-des-pem-private-keys

# Conflicts:
#	docs/dev/debt/changes/feat-libressl3-rsa-free-rsapublickey-bio.md

@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 inspected DSA decode paths and error handling support the stated behavior.

What I ran

  • Reviewed head 5c17a4684c9824a22f045548568bc3f226ae5241; base is main. No commands or tests were run.
  • Read description.md, commits.md, diff.patch, manifest.json, README.md, and the changed source ranges for DsaKeys.cpp, Asn1Reader.hpp, KeyDer.cpp, PemKeys.cpp, and CMakeLists.txt.

Results

Check Result Note
DSA public key decode passed Parameters and optional parameter handling inspected
Traditional DSA auto-detection passed Six-element route inspected
Programmatic tests not run Author-reported results only
Review coverage partial Several changed files were not read

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full text of PemEncryption.cpp, PemKeys.cpp, tests/PemKeys.cpp, and the debt change was available but not read in full; only the noted PemKeys.cpp range was inspected. The full text of DsaKeys.hpp and PemEncryption.hpp was present in the diff but their callers were not audited. No build, test, convention check, or console run was performed. The author reports Windows 11/MinGW test results; these were not independently verified. files.json was not available as a review document; the prepared manifest marks each changed file as included.

To get this to a good state

Nothing is required. The unverified areas are coverage limits, not confirmed defects.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 25, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks for the work. I'm closing this because I know of no title that loads a DSA key, and it builds on the encrypted PEM changes that were closed for the same reason (#26409, #26410), so the whole stack is large to review for nothing a game reaches today.

If a title imports one of these paths, please say so and I'll reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs needs rebase Conflicts with main, rebase needed prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants