Skip to content

Commit 9882dda

Browse files
backport of commit 0b02c5d (#27176)
Co-authored-by: Rachel Culpepper <[email protected]>
1 parent b315777 commit 9882dda

File tree

5 files changed

+150
-2
lines changed

5 files changed

+150
-2
lines changed

builtin/logical/pki/ca_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func getKeyTypeAndBitsFromPublicKeyForRole(pubKey crypto.PublicKey) (certutil.Pr
237237
keyBits = certutil.GetPublicKeySize(pubKey)
238238
case *ecdsa.PublicKey:
239239
keyType = certutil.ECPrivateKey
240-
case *ed25519.PublicKey:
240+
case ed25519.PublicKey:
241241
keyType = certutil.Ed25519PrivateKey
242242
default:
243243
return certutil.UnknownPrivateKey, 0, fmt.Errorf("unsupported public key: %#v", pubKey)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package pki
5+
6+
import (
7+
"crypto"
8+
"crypto/ecdsa"
9+
"crypto/ed25519"
10+
"crypto/elliptic"
11+
"crypto/rand"
12+
"crypto/rsa"
13+
"testing"
14+
15+
"github.com/hashicorp/vault/sdk/helper/certutil"
16+
)
17+
18+
func TestGetKeyTypeAndBitsFromPublicKeyForRole(t *testing.T) {
19+
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
20+
if err != nil {
21+
t.Fatalf("error generating rsa key: %s", err)
22+
}
23+
24+
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
25+
if err != nil {
26+
t.Fatalf("error generating ecdsa key: %s", err)
27+
}
28+
29+
publicKey, _, err := ed25519.GenerateKey(rand.Reader)
30+
if err != nil {
31+
t.Fatalf("error generating ed25519 key: %s", err)
32+
}
33+
34+
testCases := map[string]struct {
35+
publicKey crypto.PublicKey
36+
expectedKeyType certutil.PrivateKeyType
37+
expectedKeyBits int
38+
expectError bool
39+
}{
40+
"rsa": {
41+
publicKey: rsaKey.Public(),
42+
expectedKeyType: certutil.RSAPrivateKey,
43+
expectedKeyBits: 2048,
44+
},
45+
"ecdsa": {
46+
publicKey: ecdsaKey.Public(),
47+
expectedKeyType: certutil.ECPrivateKey,
48+
expectedKeyBits: 0,
49+
},
50+
"ed25519": {
51+
publicKey: publicKey,
52+
expectedKeyType: certutil.Ed25519PrivateKey,
53+
expectedKeyBits: 0,
54+
},
55+
"bad key type": {
56+
publicKey: []byte{},
57+
expectedKeyType: certutil.UnknownPrivateKey,
58+
expectedKeyBits: 0,
59+
expectError: true,
60+
},
61+
}
62+
63+
for name, tt := range testCases {
64+
t.Run(name, func(t *testing.T) {
65+
keyType, keyBits, err := getKeyTypeAndBitsFromPublicKeyForRole(tt.publicKey)
66+
if err != nil && !tt.expectError {
67+
t.Fatalf("unexpected error: %s", err)
68+
}
69+
if err == nil && tt.expectError {
70+
t.Fatal("expected error, got nil")
71+
}
72+
73+
if keyType != tt.expectedKeyType {
74+
t.Fatalf("key type mismatch: expected %s, got %s", tt.expectedKeyType, keyType)
75+
}
76+
77+
if keyBits != tt.expectedKeyBits {
78+
t.Fatalf("key bits mismatch: expected %d, got %d", tt.expectedKeyBits, keyBits)
79+
}
80+
})
81+
}
82+
}

changelog/27093.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
pki: Fix error in cross-signing using ed25519 keys
3+
```

sdk/helper/certutil/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func GetPrivateKeyTypeFromPublicKey(pubKey crypto.PublicKey) PrivateKeyType {
171171
return RSAPrivateKey
172172
case *ecdsa.PublicKey:
173173
return ECPrivateKey
174-
case *ed25519.PublicKey:
174+
case ed25519.PublicKey:
175175
return Ed25519PrivateKey
176176
default:
177177
return UnknownPrivateKey

sdk/helper/certutil/types_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package certutil
5+
6+
import (
7+
"crypto"
8+
"crypto/ecdsa"
9+
"crypto/ed25519"
10+
"crypto/elliptic"
11+
"crypto/rand"
12+
"crypto/rsa"
13+
"testing"
14+
)
15+
16+
func TestGetPrivateKeyTypeFromPublicKey(t *testing.T) {
17+
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
18+
if err != nil {
19+
t.Fatalf("error generating rsa key: %s", err)
20+
}
21+
22+
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
23+
if err != nil {
24+
t.Fatalf("error generating ecdsa key: %s", err)
25+
}
26+
27+
publicKey, _, err := ed25519.GenerateKey(rand.Reader)
28+
if err != nil {
29+
t.Fatalf("error generating ed25519 key: %s", err)
30+
}
31+
32+
testCases := map[string]struct {
33+
publicKey crypto.PublicKey
34+
expectedKeyType PrivateKeyType
35+
}{
36+
"rsa": {
37+
publicKey: rsaKey.Public(),
38+
expectedKeyType: RSAPrivateKey,
39+
},
40+
"ecdsa": {
41+
publicKey: ecdsaKey.Public(),
42+
expectedKeyType: ECPrivateKey,
43+
},
44+
"ed25519": {
45+
publicKey: publicKey,
46+
expectedKeyType: Ed25519PrivateKey,
47+
},
48+
"bad key type": {
49+
publicKey: []byte{},
50+
expectedKeyType: UnknownPrivateKey,
51+
},
52+
}
53+
54+
for name, tt := range testCases {
55+
t.Run(name, func(t *testing.T) {
56+
keyType := GetPrivateKeyTypeFromPublicKey(tt.publicKey)
57+
58+
if keyType != tt.expectedKeyType {
59+
t.Fatalf("key type mismatch: expected %s, got %s", tt.expectedKeyType, keyType)
60+
}
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)