Skip to content

feat(libSceLibreSsl3): decrypt DES and Triple DES PEM private keys - #24953

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

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

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

What

Stacked on #24943 (the first commit here is that PR; only the second one is new).

#24943 made the PEM readers of libSceLibreSsl3 decrypt AES records and left every other cipher throwing. This PR adds the DES family, which is what older tools write by default (openssl rsa -des3, openssl pkcs8 -topk8 with its v1 default, PKCS#12 exports). It follows LibreSSL 4.0.0 (ce488b78).

Format Now read
DEK-Info records DES-CBC (alias DES), DES-EDE-CBC, DES-EDE3-CBC (alias DES3), with the lengths of e_des.c and e_des3.c
PBES2 the DES-CBC and DES-EDE3-CBC OIDs, 8-byte IV, same parameter rules as AES
PBES1 PBE-MD5-DES and PBE-SHA1-DES, PKCS5_PBE_keyivgen: the digest of password and salt, iterated, first 8 bytes as key and bytes 8 to 16 as IV
PKCS#12 schemes PBE-SHA1-3DES and PBE-SHA1-2DES, PKCS12_PBE_keyivgen with PKCS12_key_gen_uni and the big-endian two-byte password of OPENSSL_asc2uni

The cipher. No DES EVP_CIPHER exists in this port, so the blocks are decrypted with mbedtls (mbedtls_des_crypt_cbc, mbedtls_des3_crypt_cbc; the parent library already uses mbedtls DES for DES_ecb_encrypt). The checks that EVP_DecryptUpdate and EVP_DecryptFinal_ex make are redone on the result, with their errors: a length that is 0 or not a multiple of 8 is EVP_R_WRONG_FINAL_BLOCK_LENGTH; a last byte of 0 or above 8, or padding bytes that differ, is EVP_R_BAD_DECRYPT. The AES path still goes through the parent's EVP functions and is not changed.

The two MD2 schemes (PBE-MD2-DES, PBE-MD2-RC2-64) threw NotImplemented in #24943. Their result does not need a cipher: 4.0.0 has no MD2 (evp_names.c has no such digest), so EVP_PBE_CipherInit finds the cipher, fails on the digest and queues EVP_R_UNKNOWN_DIGEST without EVP_R_KEYGEN_FAILURE. They now do that.

Still throwing: the ECB, CFB and OFB DES modes (in DEK-Info, ECB has no IV and the source then salts the key with uninitialised bytes), DESX, RC2, RC4, Camellia and the rest of the 4.0.0 list, the six RC2 and RC4 PBE schemes, and DES in the writer (no DES cipher object is exported, so the writer cannot be asked for it). The debt fragment lists them.

Tested

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

Vectors. 14 records made with OpenSSL 3.2.4 (default and legacy providers) from the EC and RSA keys the test already had; each one decrypts back to its key with openssl:

openssl ec    -in ec_p8.pem  -des3 | -des-ede-cbc | -des-cbc | -camellia128
openssl rsa   -in rsa_p8.pem -traditional -des3
openssl pkcs8 -topk8 -in ec_p8.pem  -v2 des3 | -v2 des-cbc | -v2 camellia-128-cbc
openssl pkcs8 -topk8 -in ec_p8.pem  -v1 PBE-MD5-DES | PBE-SHA1-DES | PBE-SHA1-3DES | PBE-SHA1-2DES | PBE-SHA1-RC4-128
openssl pkcs8 -topk8 -in rsa_p8.pem -v1 PBE-SHA1-3DES

Three of them (-des3 traditional, -v2 des3, PBE-SHA1-3DES) were already in #24943's test as records that had to throw; they now have to decrypt. The Camellia and RC4 records take their place as the ones that still throw.

The test (guest_libressl3_pem_keys), four new functions:

  • every DES record decrypts to the reference key through the callback; four of them are also read with u;
  • DEK-Info variants for the three ciphers: lower-case IV, text after the IV, another separator, the aliases; short IV, bad digit, missing IV; names that 4.0.0 does not know give PEM_R_UNSUPPORTED_ENCRYPTION, names it knows but this port lacks throw;
  • wrong passwords, found by search for each case: bad padding; a last byte between 2 and 8 with wrong padding bytes; a last byte of 0; valid padding followed by a key that does not decode;
  • ciphertext that is empty or not a multiple of 8, for every scheme;
  • PBES2 parameters: IV of 0, 7, 9 and 16 bytes, a NULL instead of the IV, no IV, the right and a wrong key length, 0 iterations;
  • PBES1 and PKCS#12 parameters, each with its error queue (EVP_R_DECODE_ERROR for PBES1, PKCS12_R_DECODE_ERROR for PKCS#12): no parameter, a wrong type, missing and extra fields, wrong tags, end-of-contents octets, a non-minimal integer, 0, negative and 9-byte iteration counts;
  • the two MD2 schemes with and without parameters; the six RC2 and RC4 schemes throw; unknown scheme OIDs give EVP_R_UNKNOWN_PBE_ALGORITHM;
  • the key slot is kept on a wrong password and replaced on the right one.

Negative controls. 41 mutations of the new code, each rebuilt and run on its own. 39 make the test fail. They cover the block-length, padding-value and padding-content checks, the key schedule (two-key against three-key), the IV, the aliases, the cipher OIDs, the PBES2 IV rules, the PBES1 digest input, rounds and IV window, the PKCS#12 diversifier, block filling, carry, rounds and password encoding, the scheme table, the MD2 outcome, the parameter decoder and the error pushes.

The two that survive cannot be seen by a test: giving the two-key cipher a 24-byte key (only its first 16 bytes are used, and both derivations produce the same first 16), and dropping the PKCS12_R_KEY_GEN_ERROR push (it needs SHA-1 itself to fail).

Rest of the library. 89 of the 91 libSceLibreSsl3 test targets were rebuilt and pass on the branch. As in #24943, the other two (guest_libressl3_ecdsa_sig_tests, guest_libressl3_rsa_getters_tests) link the graphics driver, which is not built in my tree, so I did not run them.

python tools/check_conventions.py --base origin/main passes. python tools/debt_fold.py --check fails on current main on another PR's fragment (feat-libwebrtc-rnn-vad.md, fixed by #24945); with that fragment fixed locally it passes on this branch.

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-des-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): read and write encrypted PEM private keys #24943
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

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

@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 6c655a608e:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl3_pem_keys_tests built cleanly.
  • CTest (guest_libressl3_pem_keys):
    1/1 Test #4726: guest_libressl3_pem_keys .........   Passed    5.88 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl3/src/PemKeys.cpp, core/libs/prx/libSceLibreSsl3/src/PemEncryption.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4726: guest_libressl3_pem_keys .........Subprocess aborted***Exception:   0.47 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified DES and 3DES key length and IV length handling in PemEncryption.cpp matches LibreSSL specifications.
    • Checked bounds, integer overflows, and buffer allocations in PbeCrypt and DesCbcDecrypt.
    • Verified ASN.1 DER decoding and validation in ReadEncryptedKey, ReadAlgorithm, and Pbes2Algorithm.
    • Verified password callback invocation and error handling in PemReadPassword_nid_no_patch and PemDoHeader_nid_no_patch.
    • Confirmed AGENTS.md compliance regarding comments, upstream attribution, and error code propagation.

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; the points below are optional.

What I ran

Reviewed head 6c655a608e05f5455c0583c8cc2f5b666cb9975c. I inspected the prepared diff and targeted code through repo_read.py; I did not run build or test commands. The author reports Windows 11/MinGW test results in the PR description; these were not run by me.

Results

Check Result Note
Diff and targeted source review passed Reviewed the DES/PBE paths and PEM integration
Build and tests not run Author-reported results only
Console behavior not run No console verification reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

I read the PR description, commits, diff, manifest, and README, along with targeted ranges of PemEncryption.cpp, PemKeys.cpp, and the PEM test diff. I did not read every added line in the changed files, including all 1,533 added test lines, all 77 added PemKeys.cpp lines, the full 2,136-line PemEncryption.cpp, or the CMake and debt-file additions. Build/test claims and LibreSSL 4.0.0 error-queue equivalence were not independently verified.

To get this to a good state

Nothing is required.

@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 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/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 #26410. 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. #26410 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