Skip to content

feat(libSceLibreSsl3): read and write DSA private keys in PEM - #24999

Closed
ToRaNek wants to merge 3 commits into
boykopovar:mainfrom
ToRaNek:feat/libressl3-dsa-pem-private-keys
Closed

ToRaNek wants to merge 3 commits into
boykopovar:mainfrom
ToRaNek:feat/libressl3-dsa-pem-private-keys

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

What

Stacked on #24953 (which is stacked on #24943): only the last commit is new. The three share one debt fragment.

DSA private keys in the PEM functions of libSceLibreSsl3. Before this, a DSA PRIVATE KEY record threw NotImplemented, and a DSA key in a PRIVATE KEY (PKCS#8) record was an unsupported algorithm. The structure LibreSsl3Dsa and its accessors (DSA_get0_pqg, DSA_get0_key, EVP_PKEY_get0_DSA) existed, but nothing could create a DSA key. This follows LibreSSL 4.0.0 (ce488b78).

Traditional records (DSA PRIVATE KEY, encrypted or not), old_dsa_priv_decode:

  • the DSAPrivateKey sequence (version, p, q, g, public key, private key) is decoded with the template rules;
  • dsa_check_key, in the order and with the errors of the source: p and q odd, 1 < g < p, q positive and of 160, 224 or 256 bits, p between 512 and 10000 bits, 1 < public key < p, 0 < private key < q;
  • then the three checks only this path has: q divides p - 1 (DSA_R_BAD_Q_VALUE), g^q = 1 mod p (DSA_R_INVALID_PARAMETERS), q prime (DSA_R_BAD_Q_VALUE).

PKCS#8, dsa_priv_decode: parameters from the AlgorithmIdentifier, the private key as an INTEGER, dsa_check_key, and the public key computed as g^x mod p. The four alias OIDs (dsaEncryption-old, dsaWithSHA, dsaWithSHA1, dsaWithSHA1-old) give a DSA key as well. As in the source, this path does not test that q is prime or divides p - 1.

Writing, dsa_priv_encode: PEM_write_bio_PrivateKey writes the key back as PKCS#8.

The object is the 112-byte dsa_st of dsa_local.h: flags 1 (DSA_FLAG_CACHE_MONT_P, as dsa_init sets it) and references 1; the method pointer stays null, because the address of the module's method is not known. EVP_PKEY_free releases the key through its reference count; freeing a key that has a method, a Montgomery context or extra data throws.

Arithmetic comes from mbedtls. The primality of q is 64 Miller-Rabin rounds there, Baillie-PSW in the source: a "composite" verdict is a proof in both, every prime passes both, so the answer can only differ on a composite that fools one of the two tests (probability at most 4^-64 on the mbedtls side; no Baillie-PSW counterexample is known).

Changes to existing code:

  • the ASN.1 reader that feat(libSceLibreSsl3): read and write encrypted PEM private keys #24943 added inside PemEncryption.cpp moves, unchanged, to src/Asn1Reader.hpp, so that the DSA decoder uses the same one;
  • KeyDer.cpp: the PKCS#8 AlgorithmIdentifier decoder now keeps a SEQUENCE parameter (DSA needs it). It used to refuse any parameter that was not NULL or an OID.

Left out: DSA public keys (PUBLIC KEY with the DSA OID still fails as an unsupported algorithm), d2i_PrivateKey_bio on a bare DSA key, the exported DSA_new/DSA_free and signing. The debt fragment lists them.

Tested

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

Vectors. Two throwaway test keys generated with OpenSSL 3.2.4 (1024/160 and 2048/256), each as a traditional and a PKCS#8 record, plus four encrypted forms of the first (AES-256 and 3DES traditional, PBES2 AES-128, PBE-SHA1-3DES). The expected p, q, g, public and private values are the ones openssl dsa -text prints. A fifth key with a composite q (the product of two 80-bit primes, with p and g built to match) comes from a script with a fixed seed.

The test (guest_libressl3_pem_keys), nine new functions:

  • both keys in both formats: every number read back through the accessors, the object's fields, and the key written again byte for byte as OpenSSL wrote it;
  • the four encrypted records;
  • versions: the source does not check the version, so 1, -1, an empty integer and the smallest 64-bit value are accepted and stored; only 0x7fffffff is refused, as the LONG template does;
  • every bound of dsa_check_key from both sides, with its error;
  • q that does not divide p - 1, g of another order, composite q: refused in a traditional record, and the composite q accepted in PKCS#8;
  • structure: missing and extra fields, wrong tags, constructed integers, empty and non-minimal integers, truncation;
  • PKCS#8: the alias OIDs, a missing, NULL or OID parameter, bad parameter sequences, bad private-key encodings, q of 223, 224, 225, 255, 256 and 257 bits, g equal to p;
  • key slots, the object's lifetime (references, release, replacement by an RSA key), and the encoder (negative numbers, zero, a null parameter).

Two existing tests expected the DSA label to throw and are updated.

Negative controls. 52 mutations of the new code, each rebuilt and run on its own; 51 make the test fail. They cover each bound of dsa_check_key and its error, the three consistency checks and their errors, the consistency checks wrongly applied to PKCS#8, the public-key computation, the version rules, the error codes of both decoders, the object's flags and reference count, the three conditions under which freeing throws, the integer encoder (padding, two's complement, zero), the parameter order, the sign of decoded numbers, the stored parameter bytes, the four alias OIDs, the EVP error, the traditional branch, the writer's condition and both release paths.

The first version of the test let two of them through (g equal to p, and a 224-bit q); I added those cases. The one that still survives changes the error of the "missing parameter" test in dsa_check_key, which neither decoder can reach: both always supply p, q and g.

Rest of the library. 89 of the 91 libSceLibreSsl3 test targets were rebuilt and pass on the branch; as in #24943, the other two link the graphics driver, which is not built in my tree.

python tools/check_conventions.py --base origin/main passes. python tools/debt_fold.py --check passes once the branch has the fragment fix that main got from #24945 (checked locally with that file).

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

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

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: feat(libSceLibreSsl3): decrypt DES and Triple DES PEM private keys #24953
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

ToRaNek and others added 3 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>

@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 24b19888ce:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_pem_keys_tests built cleanly.
  • CTest (guest_libressl3_pem_keys):
    1/1 Test #4730: guest_libressl3_pem_keys .........   Passed    2.32 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 #4730: guest_libressl3_pem_keys .........Subprocess aborted***Exception:   0.40 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libSceLibreSsl3/src/DsaKeys.cpp: DsaDecodeTraditionalKey_nid_no_patch and DsaDecodePkcs8Key_nid_no_patch ASN.1 parsing, error reporting, and parameter validations against LibreSSL 4.0.0.
    • core/libs/prx/libSceLibreSsl3/src/DsaKeys.cpp: CheckKey and CheckOldKeyConsistency validation of bit lengths (160/224/256 for q, 512-10000 for p), parity, divisibility ((p-1)%q == 0), and order (g^q mod p == 1).
    • core/libs/prx/libSceLibreSsl3/src/DsaKeys.cpp: DsaFree_nid_no_patch memory safety, GuestCopyIn/GuestWritable safety checks, atomic reference counting, and zeroization.
    • core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp: PKCS#8 parameter and key encoding for DSA keys with proper OID handling.
    • core/libs/prx/libSceLibreSsl3/src/Asn1Reader.hpp: Bounds checks, integer decoding, indefinite length handling, and error stack reporting.

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

Nothing blocks this change; the review is incomplete because the pinned reader command and tests could not be run here.

What I ran

Reviewed the prepared diff and targeted pinned-head source for DSA decoding, encoding, and key cleanup. Head: 24b19888ce49963b96499206fbf340fead4e3d37. No commands or tests were run.

Results

Check Result Note
Prepared diff and description passed Inspected
Targeted DSA source passed Decoder, encoder, cleanup
Build and tests not run No shell execution available
Full changed-file review not run Most changed files not inspected

Findings

Blocking

None observed in the inspected DSA paths.

Non-blocking

None.

Not verified

The reader command could not be run in this interface; consequently, most changed files and callers remain unreviewed. Build, automated tests, and console behavior were not verified. The author reports Windows 11/MinGW tests and 89 of 91 library targets passing, but these were not run by this review. Read: review/description.md, review/commits.md, review/manifest.json, review/diff.patch, and targeted ranges of core/libs/prx/libSceLibreSsl3/src/DsaKeys.cpp. The files.json path was unavailable; excluded files were not identified.

To get this to a good state

Nothing is required. Optional: complete the changed-file and test review using the pinned reader and build environment.

@github-actions

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 23438 core/libs/prx/libSceLibreSsl3/src/PkeyLifecycle.cpp L338-340
PR 25390 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/PemKeys.cpp
PR 12315 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 17241 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 17621 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 17622 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 17626 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 19024 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 22991 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 24322 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 24910 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 24917 core/libs/prx/libSceLibreSsl3/CMakeLists.txt

@ToRaNek

ToRaNek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Resubmitted as #26414. 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. #26414 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