feat(libSceLibreSsl): read PEM private keys #19508

Merged
LeandroLP23 merged 1 commit from upstream-pr/19508 into main 2026-11-13 16:22:28 +00:00
LeandroLP23 commented 2026-11-13 12:43:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-11-13T12:43:15Z.


What

Reads PEM private keys in libSceLibreSsl, following LibreSSL 2.5.4, the version its debt entries follow.

PEM reader. ReadPemRecord used to stop with NotImplemented right after the -----BEGIN line. It is now a full port of pem_lib.c's PEM_read_bio:

  • header lines, body lines, and the -----END <name>----- check;
  • base64 through encode.c's EVP_DecodeUpdate/Final/Block rules and tables;
  • check_pem with the ANY PRIVATE KEY rules and the old certificate and PKCS#7 aliases, pem_check_suffix, PEM_bytes_read_bio (which skips non-matching records), and PEM_get_EVP_CIPHER_INFO;
  • the existing PemBuffer growth and the 254-byte BIO_gets lines are kept.

Keys:

  • PEM_read_bio_PrivateKey (pem_pkey.c):
    • PRIVATE KEY goes through PKCS#8 PrivateKeyInfo and EVP_PKCS82PKEY to RSA;
    • RSA PRIVATE KEY goes through d2i_PrivateKey → old_rsa_priv_decode → RSAPrivateKey (rsa_asn1.c, d2i_pr.c), with its fallback to PKCS#8 and its reuse of *x.
  • PEM_read_RSAPrivateKey (pem_all.c) reads through a FILE BIO, then pkey_get_rsa, and replaces *rsa.
  • The RSAPrivateKey decoder lives in Rsa.cpp next to DecodeRsaPublicKey, reusing its DER helpers. Like 2.5.4's BIGNUM_it, it ignores the sign of the eight numbers, and the version goes through long_c2i.
  • libSceLibreSsl3 forwards its PEM_read_bio_PrivateKey here with a jmp, so it gets the same behaviour.

Errors, pushed with 2.5.4's library and reason codes:

  • PEM: NO_START_LINE, BAD_END_LINE, BAD_BASE64_DECODE, NOT_PROC_TYPE, NOT_ENCRYPTED, SHORT_HEADER, NOT_DEK_INFO, and ERR_R_ASN1_LIB;
  • EVP: UNSUPPORTED_ALGORITHM, UNSUPPORTED_PRIVATE_KEY_ALGORITHM and PRIVATE_KEY_DECODE_ERROR;
  • RSA: ERR_R_RSA_LIB.

A bad base64 character returns NULL with no error queued, as 2.5.4 does (B64_NOT_BASE64(0xFF) is false, so DecodeBlock fails and bl == 0 takes goto err).

Still throws, recorded in the libSceLibreSsl debt:

  • encrypted records (Proc-Type: 4,ENCRYPTED and ENCRYPTED PRIVATE KEY), since no PEM cipher is modelled;
  • DSA, EC and HMAC keys, traditional or PKCS#8;
  • PKCS#8 attributes;
  • certificates: PEM_read_bio_X509, PEM_read_bio_X509_AUX and PEM_read_X509 now read the whole record and throw at d2i_X509, which #13266 implements.

tasn_dec.c's decode errors and the error-data strings are not queued, as in the existing d2i_RSA_PUBKEY.

Licences. Each ported file carries LibreSSL's notice verbatim and a Derived from LibreSSL 2.5.4 (openbsd 2e2c612066aa993717528a0249c15c51128d61cb) … line naming its sources and revisions:

  • PemReadBioX509.cpp: pem/pem_lib.c 1.44 and evp/encode.c 1.24;
  • PemPrivateKey.cpp: pem/pem_pkey.c 1.22, asn1/d2i_pr.c 1.15 and evp/evp_pkey.c 1.19;
  • PemX509.cpp: pem/pem_all.c 1.17, where PEM_read_RSAPrivateKey lives;
  • Rsa.cpp: rsa/rsa_asn1.c 1.13.

Tested

The RSA test keys and the test certificate were freshly generated for these tests with OpenSSL and belong to nobody.

Windows 11, MinGW GCC 15.2 Release, rebased on main 0294cba8dc.

  • New guest_libressl_pem_private_key, with RSA keys from OpenSSL:
    • PKCS#1 and PKCS#8 keys through a memory BIO and through FILE give the same n, e, d, p, q, dmp1, dmq1 and iqmp as OpenSSL;
    • RSA_sign with SHA-256 reproduces OpenSSL's signature, and RSA_verify accepts it;
    • reuse of EVP_PKEY** x and RSA**;
    • a certificate and a DH PRIVATE KEY record before the key are skipped, and a lowercase label and an empty header are accepted;
    • the PKCS#8 fallback inside RSA PRIVATE KEY works;
    • each error code above is checked;
    • the encrypted, EC and certificate cases throw.
  • GuestLibreSslBio's PEM_read_X509 case now writes a full certificate record. A file holding only a BEGIN line now gives BAD_END_LINE instead of throwing.
  • All 152 libSceLibreSsl and libSceLibreSsl3 tests pass.
  • Negative controls: eleven controls, each breaking one rule, each fail the new test at the line it checks:
    • check_pem accepting every name, the padding rule, the END name, the *x free, the PKCS#8 fallback;
    • dmp1/dmq1 swapped (line 238, which I reran myself);
    • the NOT_PROC_TYPE and EVP 156 errors, the DecodeFinal error;
    • the encrypted and EC throws.
  • tools/check_conventions.py --base origin/main passes.
  • Overlap: tools/pr_overlap.py --branch lists no pull request with the same exports.
    • #17961 and #18396 meet this only on two CMakeLists.txt lines.
    • Of the nine it could not compare, none adds these functions or PEM code. #11466 is EC keys, #16529 is BIO_printf, the others are SSL, X509_STORE and BIO work.

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
  • 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5295688400 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19508). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-11-13T12:43:15Z. --- ### What Reads PEM private keys in `libSceLibreSsl`, following LibreSSL 2.5.4, the version its debt entries follow. **PEM reader.** `ReadPemRecord` used to stop with `NotImplemented` right after the `-----BEGIN` line. It is now a full port of [pem_lib.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/pem/pem_lib.c)'s `PEM_read_bio`: - header lines, body lines, and the `-----END <name>-----` check; - base64 through [encode.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/evp/encode.c)'s `EVP_DecodeUpdate/Final/Block` rules and tables; - `check_pem` with the `ANY PRIVATE KEY` rules and the old certificate and PKCS#7 aliases, `pem_check_suffix`, `PEM_bytes_read_bio` (which skips non-matching records), and `PEM_get_EVP_CIPHER_INFO`; - the existing `PemBuffer` growth and the 254-byte `BIO_gets` lines are kept. **Keys:** - `PEM_read_bio_PrivateKey` ([pem_pkey.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/pem/pem_pkey.c)): - `PRIVATE KEY` goes through PKCS#8 `PrivateKeyInfo` and `EVP_PKCS82PKEY` to RSA; - `RSA PRIVATE KEY` goes through `d2i_PrivateKey` → `old_rsa_priv_decode` → `RSAPrivateKey` ([rsa_asn1.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/rsa/rsa_asn1.c), [d2i_pr.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/asn1/d2i_pr.c)), with its fallback to PKCS#8 and its reuse of `*x`. - `PEM_read_RSAPrivateKey` ([pem_all.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/pem/pem_all.c)) reads through a FILE BIO, then `pkey_get_rsa`, and replaces `*rsa`. - The `RSAPrivateKey` decoder lives in `Rsa.cpp` next to `DecodeRsaPublicKey`, reusing its DER helpers. Like 2.5.4's `BIGNUM_it`, it ignores the sign of the eight numbers, and the version goes through `long_c2i`. - `libSceLibreSsl3` forwards its `PEM_read_bio_PrivateKey` here with a `jmp`, so it gets the same behaviour. **Errors**, pushed with 2.5.4's library and reason codes: - PEM: `NO_START_LINE`, `BAD_END_LINE`, `BAD_BASE64_DECODE`, `NOT_PROC_TYPE`, `NOT_ENCRYPTED`, `SHORT_HEADER`, `NOT_DEK_INFO`, and `ERR_R_ASN1_LIB`; - EVP: `UNSUPPORTED_ALGORITHM`, `UNSUPPORTED_PRIVATE_KEY_ALGORITHM` and `PRIVATE_KEY_DECODE_ERROR`; - RSA: `ERR_R_RSA_LIB`. A bad base64 character returns NULL with no error queued, as 2.5.4 does (`B64_NOT_BASE64(0xFF)` is false, so `DecodeBlock` fails and `bl == 0` takes `goto err`). **Still throws**, recorded in the libSceLibreSsl debt: - encrypted records (`Proc-Type: 4,ENCRYPTED` and `ENCRYPTED PRIVATE KEY`), since no PEM cipher is modelled; - DSA, EC and HMAC keys, traditional or PKCS#8; - PKCS#8 attributes; - certificates: `PEM_read_bio_X509`, `PEM_read_bio_X509_AUX` and `PEM_read_X509` now read the whole record and throw at `d2i_X509`, which #13266 implements. `tasn_dec.c`'s decode errors and the error-data strings are not queued, as in the existing `d2i_RSA_PUBKEY`. **Licences.** Each ported file carries LibreSSL's notice verbatim and a `Derived from LibreSSL 2.5.4 (openbsd 2e2c612066aa993717528a0249c15c51128d61cb) …` line naming its sources and revisions: - `PemReadBioX509.cpp`: `pem/pem_lib.c` 1.44 and `evp/encode.c` 1.24; - `PemPrivateKey.cpp`: `pem/pem_pkey.c` 1.22, `asn1/d2i_pr.c` 1.15 and `evp/evp_pkey.c` 1.19; - `PemX509.cpp`: `pem/pem_all.c` 1.17, where `PEM_read_RSAPrivateKey` lives; - `Rsa.cpp`: `rsa/rsa_asn1.c` 1.13. ### Tested The RSA test keys and the test certificate were freshly generated for these tests with OpenSSL and belong to nobody. Windows 11, MinGW GCC 15.2 Release, rebased on `main` `0294cba8dc`. - New `guest_libressl_pem_private_key`, with RSA keys from OpenSSL: - PKCS#1 and PKCS#8 keys through a memory BIO and through FILE give the same n, e, d, p, q, dmp1, dmq1 and iqmp as OpenSSL; - `RSA_sign` with SHA-256 reproduces OpenSSL's signature, and `RSA_verify` accepts it; - reuse of `EVP_PKEY** x` and `RSA**`; - a certificate and a `DH PRIVATE KEY` record before the key are skipped, and a lowercase label and an empty header are accepted; - the PKCS#8 fallback inside `RSA PRIVATE KEY` works; - each error code above is checked; - the encrypted, EC and certificate cases throw. - `GuestLibreSslBio`'s `PEM_read_X509` case now writes a full certificate record. A file holding only a BEGIN line now gives `BAD_END_LINE` instead of throwing. - All 152 `libSceLibreSsl` and `libSceLibreSsl3` tests pass. - **Negative controls:** eleven controls, each breaking one rule, each fail the new test at the line it checks: - check_pem accepting every name, the padding rule, the END name, the `*x` free, the PKCS#8 fallback; - dmp1/dmq1 swapped (line 238, which I reran myself); - the NOT_PROC_TYPE and EVP 156 errors, the DecodeFinal error; - the encrypted and EC throws. - `tools/check_conventions.py --base origin/main` passes. - **Overlap:** `tools/pr_overlap.py --branch` lists no pull request with the same exports. - #17961 and #18396 meet this only on two `CMakeLists.txt` lines. - Of the nine it could not compare, none adds these functions or PEM code. #11466 is EC keys, #16529 is `BIO_printf`, the others are SSL, X509_STORE and BIO work. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!19508
No description provided.