Skip to content

fix: support RSA and fix ed25519 key encoding in EncodePrivateKey#148

Merged
JoshVanL merged 2 commits intodapr:mainfrom
nelson-parente:fix/pem-encode-rsa-key
Mar 18, 2026
Merged

fix: support RSA and fix ed25519 key encoding in EncodePrivateKey#148
JoshVanL merged 2 commits intodapr:mainfrom
nelson-parente:fix/pem-encode-rsa-key

Conversation

@nelson-parente
Copy link
Contributor

Summary

EncodePrivateKey in crypto/pem only handled *ecdsa.PrivateKey and *ed25519.PrivateKey, rejecting RSA keys with "unsupported key type". Since all three key types use x509.MarshalPKCS8PrivateKey with the same PKCS#8 block type, this adds *rsa.PrivateKey to the existing case list.

Also fixes a pre-existing bug: *ed25519.PrivateKey (pointer) should be ed25519.PrivateKey (value type, since ed25519.PrivateKey is []byte and ed25519.GenerateKey returns a value). The pointer form never matched at runtime.

Changes

File Change
crypto/pem/pem.go Add *rsa.PrivateKey to type switch, fix ed25519.PrivateKey to value type
crypto/pem/pem_test.go New table-driven tests for all key types + roundtrip verification

Test plan

  • RSA-2048 key encodes and roundtrips through DecodePEMPrivateKey
  • RSA-4096 key encodes and roundtrips
  • ECDSA P-256 key still works (regression)
  • Ed25519 key now works (was silently broken due to pointer vs value type)
  • Unsupported type still returns error

Signed-off-by: Nelson Parente nelson_parente@live.com.pt

@nelson-parente nelson-parente requested review from a team as code owners March 18, 2026 15:31
EncodePrivateKey only handled *ecdsa.PrivateKey and ed25519.PrivateKey,
rejecting RSA keys with "unsupported key type". Since RSA keys also use
x509.MarshalPKCS8PrivateKey with the same PKCS#8 block type, add
*rsa.PrivateKey to the existing case list.

Adds table-driven tests covering RSA-2048, RSA-4096, ECDSA P-256,
Ed25519, and unsupported type with roundtrip verification through
DecodePEMPrivateKey.

Signed-off-by: Nelson Parente <nelson_parente@live.com.pt>
@nelson-parente nelson-parente force-pushed the fix/pem-encode-rsa-key branch from 3f6ea43 to bf1cd3c Compare March 18, 2026 16:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the crypto/pem helper to support encoding RSA private keys to PEM (PKCS#8), and adds unit tests to validate encode/decode roundtrips across supported key types.

Changes:

  • Add *rsa.PrivateKey support to EncodePrivateKey by marshaling RSA keys as PKCS#8.
  • Add pem_test.go covering ECDSA, RSA, Ed25519 encode/decode roundtrips and an unsupported-type error case.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
crypto/pem/pem.go Expands EncodePrivateKey to accept *rsa.PrivateKey and marshal it as PKCS#8 PEM.
crypto/pem/pem_test.go Adds tests for supported key types and error behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

- Drop RSA-4096 test case to avoid slow CI (2048 is sufficient coverage)
- Replace custom containsSubstr/searchSubstr with strings.Contains
- Remove unused helper functions

Signed-off-by: Nelson Parente <nelson_parente@live.com.pt>
@JoshVanL JoshVanL merged commit fd4e386 into dapr:main Mar 18, 2026
6 checks passed
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.

3 participants