Skip to content

Fix loading of unencrypted PKCS#8 DER private keys on pre-3.x#190

Draft
toddr-bot wants to merge 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-pkcs8-der-unencrypted
Draft

Fix loading of unencrypted PKCS#8 DER private keys on pre-3.x#190
toddr-bot wants to merge 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-pkcs8-der-unencrypted

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

@toddr-bot toddr-bot commented May 22, 2026

What

Fix new_private_key() to accept unencrypted PKCS#8 DER private keys on pre-3.x OpenSSL (including LibreSSL).

Why

On pre-3.x, _new_private_key_der without a passphrase only tried d2i_RSAPrivateKey_bio, which parses PKCS#1 (traditional RSA) DER format. Unencrypted PKCS#8 DER (PrivateKeyInfo structure, as produced by openssl pkey -outform DER) would fail because the ASN.1 structure differs from PKCS#1 RSAPrivateKey.

Encrypted PKCS#8 DER already worked (when a passphrase was provided), but the unencrypted variant was silently broken on pre-3.x. On 3.x, OSSL_DECODER handles both formats transparently.

How

Added _load_pkcs8_unenc_der_key() helper (placed before the EVP_PKEY→RSA compatibility macros) that uses d2i_PKCS8_PRIV_KEY_INFO_bio() + EVP_PKCS82PKEY() to parse unencrypted PKCS#8 DER and extract the RSA key. The no-passphrase path now tries PKCS#1 first and falls back to unencrypted PKCS#8 on failure.

Testing

  • Added 5 tests to t/der.t: unencrypted PKCS#8 DER round-trip (load, is_private, public key export, sign/verify)
  • Full test suite passes (all test files green)

🤖 Generated with Claude Code


Quality Report

Changes: 2 files changed, 57 insertions(+), 1 deletion(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

On pre-3.x (including LibreSSL), _new_private_key_der without a
passphrase only tried d2i_RSAPrivateKey_bio, which handles PKCS#1
(traditional RSA) format but fails on PKCS#8 PrivateKeyInfo.

Add _load_pkcs8_unenc_der_key helper that uses
d2i_PKCS8_PRIV_KEY_INFO_bio + EVP_PKCS82PKEY as a fallback when
PKCS#1 parsing fails.  On 3.x this path is unused — OSSL_DECODER
already handles both formats.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
toddr-bot added a commit to toddr-bot/Crypt-OpenSSL-RSA that referenced this pull request May 24, 2026
On pre-3.x, new_private_key() without a passphrase calls
d2i_RSAPrivateKey_bio() which only handles PKCS#1 DER format.
Unencrypted PKCS#8 DER import is not supported until OpenSSL 3.x
(or with the fix in PR cpan-authors#190). Without this skip, the test dies on
Debian bullseye (OpenSSL 1.1.1), killing the remaining 6 tests.

The encrypted PKCS#8 DER tests still run on pre-3.x because the
passphrase code path uses d2i_PKCS8PrivateKey_bio() which handles
PKCS#8 correctly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant