From 5d6e6c84a6b5f381e9736617c4a98c53de09d639 Mon Sep 17 00:00:00 2001 From: joshvanl Date: Thu, 7 May 2026 14:56:09 -0300 Subject: [PATCH] [1.16] Fix Ed key parsing Signed-off-by: joshvanl --- crypto/pem/pem.go | 2 +- crypto/pem/pem_test.go | 133 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 crypto/pem/pem_test.go diff --git a/crypto/pem/pem.go b/crypto/pem/pem.go index 0b82f7f..5be6f56 100644 --- a/crypto/pem/pem.go +++ b/crypto/pem/pem.go @@ -115,7 +115,7 @@ func EncodePrivateKey(key any) ([]byte, error) { ) switch key := key.(type) { - case *ecdsa.PrivateKey, *ed25519.PrivateKey: + case *ecdsa.PrivateKey, ed25519.PrivateKey, *rsa.PrivateKey: keyBytes, err = x509.MarshalPKCS8PrivateKey(key) if err != nil { return nil, err diff --git a/crypto/pem/pem_test.go b/crypto/pem/pem_test.go new file mode 100644 index 0000000..49102c4 --- /dev/null +++ b/crypto/pem/pem_test.go @@ -0,0 +1,133 @@ +/* +Copyright 2023 The Dapr Authors +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pem + +import ( + "crypto" + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "crypto/rsa" + "strings" + "testing" +) + +func TestEncodePrivateKey(t *testing.T) { + ecKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatalf("failed to generate ECDSA key: %v", err) + } + + rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatalf("failed to generate RSA key: %v", err) + } + + _, ed25519Key, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatalf("failed to generate Ed25519 key: %v", err) + } + + tests := []struct { + name string + key any + wantErr bool + errSubstr string + }{ + { + name: "ECDSA P-256", + key: ecKey, + }, + { + name: "RSA 2048", + key: rsaKey, + }, + { + name: "Ed25519", + key: ed25519Key, + }, + { + name: "unsupported type", + key: "not a key", + wantErr: true, + errSubstr: "unsupported key type", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + encoded, err := EncodePrivateKey(tt.key) + if tt.wantErr { + if err == nil { + t.Fatal("expected error, got nil") + } + if tt.errSubstr != "" && !strings.Contains(err.Error(), tt.errSubstr) { + t.Fatalf("expected error containing %q, got %q", tt.errSubstr, err.Error()) + } + return + } + + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if len(encoded) == 0 { + t.Fatal("encoded output is empty") + } + + decoded, err := DecodePEMPrivateKey(encoded) + if err != nil { + t.Fatalf("roundtrip decode failed: %v", err) + } + + if !keysEqual(t, tt.key, decoded) { + t.Fatal("roundtrip key does not match original") + } + }) + } +} + +// keysEqual compares the original key with the decoded signer using +// each key type's Equal method. +func keysEqual(t *testing.T, original any, decoded crypto.Signer) bool { + t.Helper() + + switch orig := original.(type) { + case *ecdsa.PrivateKey: + d, ok := decoded.(*ecdsa.PrivateKey) + if !ok { + t.Errorf("decoded key type %T, want *ecdsa.PrivateKey", decoded) + return false + } + return orig.Equal(d) + case *rsa.PrivateKey: + d, ok := decoded.(*rsa.PrivateKey) + if !ok { + t.Errorf("decoded key type %T, want *rsa.PrivateKey", decoded) + return false + } + return orig.Equal(d) + case ed25519.PrivateKey: + d, ok := decoded.(ed25519.PrivateKey) + if !ok { + t.Errorf("decoded key type %T, want ed25519.PrivateKey", decoded) + return false + } + return orig.Equal(d) + default: + t.Errorf("unknown key type %T", original) + return false + } +}