Skip to content

Add passphrase support for DER-encoded private keys#180

Merged
timlegge merged 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/der-passphrase-support
Apr 23, 2026
Merged

Add passphrase support for DER-encoded private keys#180
timlegge merged 1 commit intocpan-authors:mainfrom
toddr-bot:koan.toddr.bot/der-passphrase-support

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

@toddr-bot toddr-bot commented Apr 23, 2026

What

new_private_key() now accepts a passphrase for encrypted PKCS#8 DER keys, matching the existing PEM passphrase support.

Why

PR #176 croaked when a passphrase was provided with DER keys. timlegge's review pointed out that implementing passphrase support is better than rejecting it — encrypted PKCS#8 DER (EncryptedPrivateKeyInfo) is a valid format that OpenSSL supports on all versions.

How

  • OpenSSL 3.x: Sets OSSL_DECODER_CTX_set_passphrase() on the existing decoder context before calling OSSL_DECODER_from_bio(). The decoder chain auto-detects both encrypted and unencrypted PKCS#8 DER.
  • Pre-3.x: Adds _load_pkcs8_der_key() helper (placed before EVP_PKEY→RSA compatibility macros) that calls d2i_PKCS8PrivateKey_bio() and extracts the RSA* via EVP_PKEY_get1_RSA(). Used when a passphrase is provided; otherwise falls back to d2i_RSAPrivateKey_bio() for traditional PKCS#1 DER.

Testing

  • 6 new tests in t/der.t: encrypted PKCS#8 DER round-trip, key validation, sign/verify, wrong-passphrase rejection
  • Full test suite passes (all existing tests unaffected)

Supersedes #176 — adds the feature instead of rejecting the use case.

🤖 Generated with Claude Code


Quality Report

Changes: 3 files changed, 72 insertions(+), 7 deletions(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

Copy link
Copy Markdown
Member

@timlegge timlegge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fin2 @toddr-bot we can fix the @rest to $passphrase later

@timlegge timlegge marked this pull request as ready for review April 23, 2026 14:55
@timlegge
Copy link
Copy Markdown
Member

@toddr-bot please rebase

Copy link
Copy Markdown
Member

@timlegge timlegge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebase

_new_private_key_der() now accepts an optional passphrase to decrypt
encrypted PKCS#8 DER (EncryptedPrivateKeyInfo) private keys. On
OpenSSL 3.x, sets OSSL_DECODER_CTX_set_passphrase() on the existing
decoder context. On pre-3.x, uses d2i_PKCS8PrivateKey_bio() via a
helper placed before the EVP_PKEY->RSA compatibility macros.

Addresses review feedback on PR cpan-authors#176 — rather than croaking when a
passphrase is provided for DER keys, we now support the use case.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@toddr-bot toddr-bot force-pushed the koan.toddr.bot/der-passphrase-support branch from 0698084 to 2283c75 Compare April 23, 2026 15:42
@timlegge timlegge merged commit afc8b6a into cpan-authors:main Apr 23, 2026
28 checks passed
@toddr-bot toddr-bot deleted the koan.toddr.bot/der-passphrase-support branch April 24, 2026 07:22
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.

2 participants