Skip to content

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

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

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

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What

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

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

#26409 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 #26409. 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 #26409'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 #26409, 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 #26409
  • 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>
ToRaNek and others added 3 commits November 23, 2026 23:47
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). Adds DES / Triple-DES PEM private-key decryption on top of the encrypted-PEM base.

This is a middle of the PEM stack (#26409 → #26410 → #26414 → #26425), so merge order matters. I verified the full stack's test on #26425 and this layer's test here; both pass. The shared debt fragment carries the honest caveats forward (LibreSSL 4.0.0 PEM_do_header / EVP_PBE_CipherInit provenance, MD2 schemes queuing EVP_R_UNKNOWN_DIGEST, unsupported ciphers throwing via NotImplemented_nid_no_patch). No made-up success.

@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/tests/PemKeys.cpp
PR 12315 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 16654 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 17622 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 18284 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 19125 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 22991 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 23029 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 23066 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 23573 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
PR 25146 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 25901 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 25903 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 26258 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
PR 26672 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 c04bd10073:

  • 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    2.02 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 #4826: guest_libressl3_pem_keys .........Subprocess aborted***Exception:   0.26 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Validated PemCipherInfoFromDekInfo_nid_no_patch in PemEncryption.cpp: verifies DEK-Info header bounds, cipher name parsing, and IV bounds check against sizeof(info.iv).
    • Verified PemDoHeader_nid_no_patch in PemEncryption.cpp: ensures input length bounds checks against data.size(), checks for INT_MAX overflow before buffer allocation, and guarantees sensitive key and password zeroing via RAII guards.
    • Audited PbeCipherInit and PbeCrypt in PemEncryption.cpp: verified DES and 3DES scheme dispatch for PBES1, PBES2, and PKCS#12, verifying correct error reporting via PutError (Pkcs12Library, EvpLibrary) and explicit NotImplemented_nid_no_patch invocation on unimplemented DES encryption.
    • Checked PemReadPassword_nid_no_patch in PemEncryption.cpp: verified safe memory access via GuestCopyIn_nid_no_patch, length enforcement against LibreSsl3PemBufferSize (1024 bytes), and proper error signaling when callback returns negative or zero length.
    • Verified PemEncryptPkcs8_nid_no_patch and DecryptToPkcs8 in PemEncryption.cpp: verified null checks on cipher and plain buffers, valid d2i_PKCS8_PRIV_KEY_INFO_nid_postfix 64-bit length handling, and correct ASN.1 DER encapsulation.
    • Confirmed strict AGENTS.md compliance: upstream BSD/OpenSSL license headers are preserved verbatim, zero prohibited comments are present in source code, and missing/unsupported primitives throw via NotImplemented_nid_no_patch.

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 — changes requested

Changes are required before merge: reject empty PKCS#12 salts before key derivation to avoid a modulo-by-zero crash on malformed encrypted key input.

What I ran

  • Reviewed prepared diff at head c04bd10073ef37b515e1928cb8ce2ef794fdf35c; no commands or tests were run.
  • Read review/description.md, review/commits.md, review/manifest.json, review/diff.patch, and the implementation ranges in core/libs/prx/libSceLibreSsl3/src/PemEncryption.cpp covering DER parsing, PBE parameter decoding, and DES/PBE key derivation.

Results

Check Result Note
Patch review failed Empty PKCS#12 salt can crash key derivation
Tests not run Only author-reported results are available
Manifest coverage not run Full changed-file inventory was not inspected

Findings

Blocking

  • core/libs/prx/libSceLibreSsl3/src/PemEncryption.cpp:1768: For a PKCS#12 PBE parameter with an empty salt, saltBlocks is zero, but the later loop evaluates salt[index % saltLength] while filling the password blocks. This performs modulo by zero; an attacker-controlled encrypted PEM can therefore crash the process. Reject the empty salt before entering this loop (and add a malformed-input regression test).

Non-blocking

None.

Not verified

  • The implementation’s remaining paths, test changes, CMake integration, and debt-document changes were not fully read.
  • files.json was not inspected, so excluded files and complete reviewed-file coverage are not verified.
  • No build or test was run. Windows/MinGW results and OpenSSL vectors in the description are author-reported; console behavior and LibreSSL error-queue equivalence remain unverified.

To get this to a good state

Reject an empty PKCS#12 salt before key derivation and add a regression test that confirms malformed input fails cleanly rather than crashing. Re-run the relevant PEM test target on the updated head.

@ToRaNek

ToRaNek commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thank you for the read. I checked the blocking point against the code at c04bd10073e and it does not hold: an empty salt never reaches the modulo.

core/libs/prx/libSceLibreSsl3/src/PemEncryption.cpp:

  • :1764 const std::size_t saltBlocks = v * ((saltLength + v - 1) / v); With saltLength == 0 this is v * ((v - 1) / v), and (v - 1) / v is 0 in integer division, so saltBlocks is 0. v is at least 1: a block size of 0 or less returns an error at :1757.
  • :1768 for (std::size_t index = 0; index < saltBlocks; ++index) input[index] = salt[index % saltLength]; The body is the only place that divides by saltLength, and with saltBlocks == 0 it is never entered.
  • The password blocks do not use the salt: :1769-1770 index unicode[index % unicode.size()], and :1765 sets passwordBlocks to 0 when unicode is empty, so that loop cannot divide by zero either.

This is the arithmetic of the pinned LibreSSL (3rdparty/libressl-openbsd at 078b3d1df8), which the function follows: src/lib/libcrypto/pkcs12/p12_key.c:116 computes Slen = v * ((saltlen + v - 1) / v) and :130-131 run for (i = 0; i < Slen; i++) *p++ = salt[i % saltlen];. PKCS12_PBE_keyivgen passes the decoded salt on without a length check (p12_crpt.c:102-103), so LibreSSL derives a key from an empty salt instead of rejecting it. Rejecting it here would make the port refuse an input the library accepts, so I am leaving the code as it is.

No commit follows from this review: there is nothing to change in the code, and I did not add a test for a crash that cannot happen. If you see a path where saltBlocks is non-zero while saltLength is 0, point me at it and I will look again.

…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

The DES-family decryption additions are supported by the inspected implementation and its targeted regression coverage; the review is incomplete because several changed files and most of the large implementation were not read.

What I ran

No build or tests were run. I inspected the prepared diff and selected code ranges with the pinned-head reader.

Results

Check Result Note
Diff and metadata passed Prepared review bundle inspected
Targeted code review not run Partial source inspection only
Build and tests not run No commands executed

Findings

Blocking

None identified in the inspected ranges.

Non-blocking

None.

Not verified

The full implementation in core/libs/prx/libSceLibreSsl3/src/PemEncryption.cpp, including most ASN.1 and PBE handling, was not read. core/libs/prx/libSceLibreSsl3/src/PemKeys.cpp, core/libs/prx/libSceLibreSsl3/tests/PemKeys.cpp, and core/libs/prx/libSceLibreSsl3/CMakeLists.txt were not read. The manifest lists six changed files; the new header and debt fragment were inspected through the diff, and selected portions of the implementation were inspected. The reported Windows test and negative-control results are author-reported, not independently run. Console behavior was not verified.

To get this to a good state

Nothing is required.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 25, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@ToRaNek, I am closing this resubmission for the same reason the original was set to close: it is real, tested code, but no title we know of loads a password-protected private key from guest memory, and it is a large piece of crypto to review for no game effect today. If you find a title that does, please reopen with that title and it goes to review.

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.

5 participants