Skip to content

Add DER export functions for public and private keys#186

Draft
toddr-bot wants to merge 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/add-der-export
Draft

Add DER export functions for public and private keys#186
toddr-bot wants to merge 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/add-der-export

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

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

Summary

  • Add four new methods for exporting keys in binary DER format
  • Complete the API symmetry: the module already imported DER but couldn't export it

Why

Users who needed DER-encoded keys had to manually strip PEM headers and base64-decode — error-prone and surprising for a module that already handles DER import transparently.

How

Each function follows the pattern of its PEM counterpart:

  • PKCS#1 public (get_public_key_der_string): i2d_RSAPublicKey_bio on pre-3.x, OSSL_ENCODER with DER/PKCS1 on 3.x
  • X.509 public (get_public_key_x509_der_string): i2d_RSA_PUBKEY_bio / i2d_PUBKEY_bio
  • PKCS#1 private (get_private_key_der_string): i2d_RSAPrivateKey_bio / i2d_PrivateKey_bio
  • PKCS#8 private (get_private_key_pkcs8_der_string): i2d_PKCS8PrivateKey_bio (with pre-3.x EVP_PKEY wrapper)

PKCS#8 DER supports optional passphrase/cipher for encrypted export, matching the PEM version.

Testing

  • 19 new tests in t/der.t covering:
    • DER output matches pem_to_der(PEM export) for each format
    • Round-trip: export DER → import DER → sign/verify
    • Encrypted PKCS#8 DER with passphrase
    • Wrong passphrase rejection
    • Public-only key error handling
  • Full test suite passes (686 tests)

🤖 Generated with Claude Code


Quality Report

Changes: 3 files changed, 254 insertions(+), 1 deletion(-)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

The module supported DER import via new_public_key() and
new_private_key() but had no DER export counterparts, forcing
users to manually base64-decode PEM output.

New methods:
- get_public_key_der_string / get_public_key_pkcs1_der_string
- get_public_key_x509_der_string
- get_private_key_der_string
- get_private_key_pkcs8_der_string (with optional passphrase/cipher)

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