Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 24b19888ce:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_libressl3_pem_keys_testsbuilt 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.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/1 Test #4730: guest_libressl3_pem_keys .........Subprocess aborted***Exception: 0.40 sec 0% tests passed, 1 tests failed out of 1 - 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
left a comment
There was a problem hiding this comment.
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.
|
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.
|
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, aDSA PRIVATE KEYrecord threwNotImplemented, and a DSA key in aPRIVATE KEY(PKCS#8) record was an unsupported algorithm. The structureLibreSsl3Dsaand 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:DSAPrivateKeysequence (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;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 theAlgorithmIdentifier, the private key as anINTEGER,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_PrivateKeywrites the key back as PKCS#8.The object is the 112-byte
dsa_stofdsa_local.h:flags1 (DSA_FLAG_CACHE_MONT_P, asdsa_initsets it) andreferences1; the method pointer stays null, because the address of the module's method is not known.EVP_PKEY_freereleases 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:
PemEncryption.cppmoves, unchanged, tosrc/Asn1Reader.hpp, so that the DSA decoder uses the same one;KeyDer.cpp: the PKCS#8AlgorithmIdentifierdecoder now keeps aSEQUENCEparameter (DSA needs it). It used to refuse any parameter that was notNULLor an OID.Left out: DSA public keys (
PUBLIC KEYwith the DSA OID still fails as an unsupported algorithm),d2i_PrivateKey_bioon a bare DSA key, the exportedDSA_new/DSA_freeand 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 -textprints. 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:0x7fffffffis refused, as theLONGtemplate does;dsa_check_keyfrom both sides, with its error;NULLor OID parameter, bad parameter sequences, bad private-key encodings, q of 223, 224, 225, 255, 256 and 257 bits, g equal to p;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_keyand 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
libSceLibreSsl3test 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/mainpasses.python tools/debt_fold.py --checkpasses once the branch has the fragment fix thatmaingot 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):EVP_PKEY_freeand the other lifecycle helpers, and rewrites the lines where this PR adds its three-line DSA branch (PkeyLifecycle.cppL338-340). Different topics, same lines: whichever merges second re-applies the branch on the other's code.mainand shareCMakeLists.txt,KeyDer.cpportests/PemKeys.cpp: the 14 listed on feat(libSceLibreSsl3): read and write encrypted PEM private keys #24943 and feat(libSceLibreSsl3): decrypt DES and Triple DES PEM private keys #24953, the three refactors that split the large libraries (refactor: split large emulator libraries into small-size libraries #24322, refactor: split Icu and libc sources into small-size libraries #24910, refactor: split more prx libraries (Pad, NetCtl, Ult, NpManager, SystemService, others) into small-size libraries #24917), and feat(libSceLibreSsl): implement EC_KEY_get0_private_key and EC_KEY_set_private_key #11146 again, whoseKeyDer.cppchanges are inDecodeEcPrivateKey, which this PR does not touch.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)