feat(libSceLibreSsl3): read and write keys in DER and PKCS#8 #19535

Merged
MarrasFederico merged 11 commits from upstream-pr/19535 into main 2026-11-14 13:32:32 +00:00
MarrasFederico commented 2026-11-13 13:52:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarrasFederico on 2026-11-13T13:52:02Z.


What

libSceLibreSsl3 can now read and write RSA and EC keys in DER and PKCS#8. The new exports are:

  • d2i_PUBKEY;
  • d2i_PrivateKey_bio;
  • d2i_PKCS8_PRIV_KEY_INFO and d2i_PKCS8_PRIV_KEY_INFO_bio;
  • i2d_PKCS8_PRIV_KEY_INFO;
  • PKCS8_PRIV_KEY_INFO_free;
  • EVP_PKCS82PKEY and EVP_PKEY2PKCS8.

These keys use the native EVP_PKEY from #17528 and #19487, so the guest can release them with EVP_PKEY_free.

  • Keys.
    • RSA keys are built with RSA_new and RSA_set0_*, and assigned with the native RsaAsn1Method.
    • EC keys use EC_KEY from the parent library. P-256 and P-384 are supported; other curves fail with an error.
    • EVP_PKEY_free releases both kinds.
  • PKCS8_PRIV_KEY_INFO. The type is opaque in LibreSSL 4.0.0, which defines it only in x509_local.h.
    • It uses the existing 32-byte LP64 LibreSsl3Pkcs8PrivKeyInfo layout (version, pkeyalg, pkey, attributes), built from the module's ASN.1 types.
    • Optional [0] attributes are validated and stored as raw bytes rather than as STACK_OF(X509_ATTRIBUTE). i2d writes them back and PKCS8_PRIV_KEY_INFO_free releases them.
  • BIO readers. The _bio functions read exactly one definite-length DER object, guided by its TLV header, as asn1_d2i_read_bio does. The content is read in chunks (16 KiB, doubling up to 1 MiB), so memory grows only with the bytes the BIO delivers. Bytes after that object stay in the BIO.
  • d2i_PrivateKey_bio. It follows d2i_AutoPrivateKey in a_pkey.c:
    • 3 elements → PKCS#8;
    • 4 elements → SEC1 EC, falling back to PKCS#8;
    • anything else → PKCS#1 RSA, falling back to PKCS#8.
  • Errors. The error-queue codes follow LibreSSL 4.0.0. This covers the nested ASN.1 errors from ASN1_item_d2i, the EVP_R_* codes from EVP_PKCS82PKEY, and the X509_R_*/EC_R_* codes on the public key paths.
  • Still stubs, with debt entries:
    • d2i_RSA_PUBKEY and d2i_RSAPublicKey_bio return a bare RSA*. RSA_free in this module still forwards to libSceLibreSsl, which uses a different RSA layout, so the guest could not free a native RSA returned by these functions.
    • PEM_read_RSAPrivateKey, PEM_read_bio_PUBKEY and PEM_write_bio_PrivateKey wait for the PEM layer.
  • Debt counts: Export.cpp has 15 stubs. The additional-export table has 56 rows, 52 named and 4 known only by NID, matching AdditionalExports.cpp.

Source: LibreSSL 4.0.0 at ce488b78e0bd994780569df718596b9776ff681a: asn1/x_pubkey.c, asn1/p8_pkey.c, evp/evp_pkey.c, asn1/a_pkey.c and asn1/asn1_item.c. KeyDer.cpp carries their notices and a "Derived from" line.

Tested

Ubuntu 26.04.1, GCC 15.2.0, Release, BUILD_TESTING=ON. Also built on Windows (MSYS2 UCRT64, GCC 16.2), where ctest -R libressl3 passes.

The new test guest_libressl3_key_der frees every object only through public exports (EVP_PKEY_free, PKCS8_PRIV_KEY_INFO_free, BIO_free, free). It covers:

  • RSA-2048 SPKI and PKCS#8 keys;
  • EC P-256 and P-384 keys in SPKI, SEC1 and PKCS#8;
  • PKCS#8 round trips, comparing the RSA and EC components;
  • [0] attributes kept across a round trip;
  • two consecutive objects in one memory BIO, read one at a time;
  • *pp advancement and *out replacement;
  • truncated DER, an unknown OID, a wrong tag and a wrong SEQUENCE length, each checking the queued error;
  • BIO headers claiming about 2 GiB and indefinite-length headers, each checking the queued error;
  • a PKCS#8 key of about 17 KB read from a BIO across several chunks.

Results:

  • ctest -R libressl3: 70 of 70 pass at 28e5d37a7d (after merging #19487 3ede9bc1ae).
  • On the base (#19487) the new exports are throwing stubs. With d2i_PUBKEY stubbed again, the test stops with d2i_PUBKEY_nid_postfix not implemented.
  • Negative control: without the native RSA method assignment, the test aborts at its check that a decoded RSA key carries a method. EVP_PKEY_free needs that method to release the key.
  • tools/check_conventions.py --base upstream/main: reports 1 warning ([debt-length] at docs/dev/debt/libSceLibreSsl3.md:28, 1778 characters; 0 errors). python3 core/libs/tests/test_public_headers.py --audit-only: 413 libraries, 34548 exports verified. git diff --check upstream/main...HEAD: clean.

Overlap: no open PR implements these exports. #19508 reads PEM keys in the parent libSceLibreSsl, not here.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: #19487 (this branch contains its commits; #17528 is merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5296661681 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19535). Originally posted by [MarrasFederico](https://github.com/MarrasFederico) on 2026-11-13T13:52:02Z. --- ### What `libSceLibreSsl3` can now read and write RSA and EC keys in DER and PKCS#8. The new exports are: - `d2i_PUBKEY`; - `d2i_PrivateKey_bio`; - `d2i_PKCS8_PRIV_KEY_INFO` and `d2i_PKCS8_PRIV_KEY_INFO_bio`; - `i2d_PKCS8_PRIV_KEY_INFO`; - `PKCS8_PRIV_KEY_INFO_free`; - `EVP_PKCS82PKEY` and `EVP_PKEY2PKCS8`. These keys use the native `EVP_PKEY` from #17528 and #19487, so the guest can release them with `EVP_PKEY_free`. - **Keys.** - RSA keys are built with `RSA_new` and `RSA_set0_*`, and assigned with the native `RsaAsn1Method`. - EC keys use `EC_KEY` from the parent library. P-256 and P-384 are supported; other curves fail with an error. - `EVP_PKEY_free` releases both kinds. - **`PKCS8_PRIV_KEY_INFO`.** The type is opaque in LibreSSL 4.0.0, which defines it only in `x509_local.h`. - It uses the existing 32-byte LP64 `LibreSsl3Pkcs8PrivKeyInfo` layout (`version`, `pkeyalg`, `pkey`, `attributes`), built from the module's ASN.1 types. - Optional `[0]` attributes are validated and stored as raw bytes rather than as `STACK_OF(X509_ATTRIBUTE)`. `i2d` writes them back and `PKCS8_PRIV_KEY_INFO_free` releases them. - **BIO readers.** The `_bio` functions read exactly one definite-length DER object, guided by its TLV header, as `asn1_d2i_read_bio` does. The content is read in chunks (16 KiB, doubling up to 1 MiB), so memory grows only with the bytes the BIO delivers. Bytes after that object stay in the BIO. - **`d2i_PrivateKey_bio`.** It follows `d2i_AutoPrivateKey` in `a_pkey.c`: - 3 elements → PKCS#8; - 4 elements → SEC1 EC, falling back to PKCS#8; - anything else → PKCS#1 RSA, falling back to PKCS#8. - **Errors.** The error-queue codes follow LibreSSL 4.0.0. This covers the nested ASN.1 errors from `ASN1_item_d2i`, the `EVP_R_*` codes from `EVP_PKCS82PKEY`, and the `X509_R_*`/`EC_R_*` codes on the public key paths. - **Still stubs, with debt entries:** - `d2i_RSA_PUBKEY` and `d2i_RSAPublicKey_bio` return a bare `RSA*`. `RSA_free` in this module still forwards to `libSceLibreSsl`, which uses a different RSA layout, so the guest could not free a native RSA returned by these functions. - `PEM_read_RSAPrivateKey`, `PEM_read_bio_PUBKEY` and `PEM_write_bio_PrivateKey` wait for the PEM layer. - **Debt counts:** `Export.cpp` has 15 stubs. The additional-export table has 56 rows, 52 named and 4 known only by NID, matching `AdditionalExports.cpp`. Source: LibreSSL 4.0.0 at [`ce488b78e0bd994780569df718596b9776ff681a`](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto): `asn1/x_pubkey.c`, `asn1/p8_pkey.c`, `evp/evp_pkey.c`, `asn1/a_pkey.c` and `asn1/asn1_item.c`. `KeyDer.cpp` carries their notices and a "Derived from" line. ### Tested Ubuntu 26.04.1, GCC 15.2.0, Release, `BUILD_TESTING=ON`. Also built on Windows (MSYS2 UCRT64, GCC 16.2), where `ctest -R libressl3` passes. The new test `guest_libressl3_key_der` frees every object only through public exports (`EVP_PKEY_free`, `PKCS8_PRIV_KEY_INFO_free`, `BIO_free`, `free`). It covers: - RSA-2048 SPKI and PKCS#8 keys; - EC P-256 and P-384 keys in SPKI, SEC1 and PKCS#8; - PKCS#8 round trips, comparing the RSA and EC components; - `[0]` attributes kept across a round trip; - two consecutive objects in one memory BIO, read one at a time; - `*pp` advancement and `*out` replacement; - truncated DER, an unknown OID, a wrong tag and a wrong SEQUENCE length, each checking the queued error; - BIO headers claiming about 2 GiB and indefinite-length headers, each checking the queued error; - a PKCS#8 key of about 17 KB read from a BIO across several chunks. Results: - `ctest -R libressl3`: 70 of 70 pass at `28e5d37a7d` (after merging `#19487` `3ede9bc1ae`). - On the base (#19487) the new exports are throwing stubs. With `d2i_PUBKEY` stubbed again, the test stops with `d2i_PUBKEY_nid_postfix not implemented`. - Negative control: without the native RSA method assignment, the test aborts at its check that a decoded RSA key carries a method. `EVP_PKEY_free` needs that method to release the key. - `tools/check_conventions.py --base upstream/main`: reports 1 warning (`[debt-length]` at `docs/dev/debt/libSceLibreSsl3.md:28`, 1778 characters; 0 errors). `python3 core/libs/tests/test_public_headers.py --audit-only`: 413 libraries, 34548 exports verified. `git diff --check upstream/main...HEAD`: clean. Overlap: no open PR implements these exports. #19508 reads PEM keys in the parent `libSceLibreSsl`, not here. ### Checklist - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: #19487 (this branch contains its commits; #17528 is merged) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!19535
No description provided.