-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ed25519 key type in ca_util #27093
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package pki | ||
|
||
import ( | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/ed25519" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"testing" | ||
|
||
"github.com/hashicorp/vault/sdk/helper/certutil" | ||
) | ||
|
||
func TestGetKeyTypeAndBitsFromPublicKeyForRole(t *testing.T) { | ||
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
t.Fatalf("error generating rsa key: %s", err) | ||
} | ||
|
||
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) | ||
if err != nil { | ||
t.Fatalf("error generating ecdsa key: %s", err) | ||
} | ||
|
||
publicKey, _, err := ed25519.GenerateKey(rand.Reader) | ||
if err != nil { | ||
t.Fatalf("error generating ed25519 key: %s", err) | ||
} | ||
|
||
testCases := map[string]struct { | ||
publicKey crypto.PublicKey | ||
expectedKeyType certutil.PrivateKeyType | ||
expectedKeyBits int | ||
expectError bool | ||
}{ | ||
"rsa": { | ||
publicKey: rsaKey.Public(), | ||
expectedKeyType: certutil.RSAPrivateKey, | ||
expectedKeyBits: 2048, | ||
}, | ||
"ecdsa": { | ||
publicKey: ecdsaKey.Public(), | ||
expectedKeyType: certutil.ECPrivateKey, | ||
expectedKeyBits: 0, | ||
}, | ||
"ed25519": { | ||
publicKey: publicKey, | ||
expectedKeyType: certutil.Ed25519PrivateKey, | ||
expectedKeyBits: 0, | ||
}, | ||
"bad key type": { | ||
publicKey: []byte{}, | ||
expectedKeyType: certutil.UnknownPrivateKey, | ||
expectedKeyBits: 0, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for name, tt := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
keyType, keyBits, err := getKeyTypeAndBitsFromPublicKeyForRole(tt.publicKey) | ||
if err != nil && !tt.expectError { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
if err == nil && tt.expectError { | ||
t.Fatal("expected error, got nil") | ||
} | ||
|
||
if keyType != tt.expectedKeyType { | ||
t.Fatalf("key type mismatch: expected %s, got %s", tt.expectedKeyType, keyType) | ||
} | ||
|
||
if keyBits != tt.expectedKeyBits { | ||
t.Fatalf("key bits mismatch: expected %d, got %d", tt.expectedKeyBits, keyBits) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:bug | ||
pki: Fix error in cross-signing using ed25519 keys | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package certutil | ||
|
||
import ( | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/ed25519" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"testing" | ||
) | ||
|
||
func TestGetPrivateKeyTypeFromPublicKey(t *testing.T) { | ||
rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
t.Fatalf("error generating rsa key: %s", err) | ||
} | ||
|
||
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) | ||
if err != nil { | ||
t.Fatalf("error generating ecdsa key: %s", err) | ||
} | ||
|
||
publicKey, _, err := ed25519.GenerateKey(rand.Reader) | ||
if err != nil { | ||
t.Fatalf("error generating ed25519 key: %s", err) | ||
} | ||
|
||
testCases := map[string]struct { | ||
publicKey crypto.PublicKey | ||
expectedKeyType PrivateKeyType | ||
}{ | ||
"rsa": { | ||
publicKey: rsaKey.Public(), | ||
expectedKeyType: RSAPrivateKey, | ||
}, | ||
"ecdsa": { | ||
publicKey: ecdsaKey.Public(), | ||
expectedKeyType: ECPrivateKey, | ||
}, | ||
"ed25519": { | ||
publicKey: publicKey, | ||
expectedKeyType: Ed25519PrivateKey, | ||
}, | ||
"bad key type": { | ||
publicKey: []byte{}, | ||
expectedKeyType: UnknownPrivateKey, | ||
}, | ||
} | ||
|
||
for name, tt := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
keyType := GetPrivateKeyTypeFromPublicKey(tt.publicKey) | ||
|
||
if keyType != tt.expectedKeyType { | ||
t.Fatalf("key type mismatch: expected %s, got %s", tt.expectedKeyType, keyType) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.