-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Address PKI to properly support managed keys #15256
Merged
stevendpclark
merged 3 commits into
pki-pod-rotation
from
stevendpclark/managed-key-fixes
May 2, 2022
Merged
Changes from all commits
Commits
Show all changes
3 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
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
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,126 @@ | ||
package pki | ||
|
||
import ( | ||
"context" | ||
"crypto" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/hashicorp/vault/sdk/helper/certutil" | ||
"github.com/hashicorp/vault/sdk/helper/errutil" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
) | ||
|
||
type managedKeyContext struct { | ||
ctx context.Context | ||
b *backend | ||
mountPoint string | ||
} | ||
|
||
func newManagedKeyContext(ctx context.Context, b *backend, mountPoint string) managedKeyContext { | ||
return managedKeyContext{ | ||
ctx: ctx, | ||
b: b, | ||
mountPoint: mountPoint, | ||
} | ||
} | ||
|
||
func comparePublicKey(ctx managedKeyContext, key *keyEntry, publicKey crypto.PublicKey) (bool, error) { | ||
publicKeyForKeyEntry, err := getPublicKey(ctx, key) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return certutil.ComparePublicKeysAndType(publicKeyForKeyEntry, publicKey) | ||
} | ||
|
||
func getPublicKey(mkc managedKeyContext, key *keyEntry) (crypto.PublicKey, error) { | ||
if key.PrivateKeyType == certutil.ManagedPrivateKey { | ||
keyId, err := extractManagedKeyId([]byte(key.PrivateKey)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return getManagedKeyPublicKey(mkc, keyId) | ||
} | ||
|
||
signer, _, _, err := getSignerFromKeyEntryBytes(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return signer.Public(), nil | ||
} | ||
|
||
func getSignerFromKeyEntryBytes(key *keyEntry) (crypto.Signer, certutil.BlockType, *pem.Block, error) { | ||
if key.PrivateKeyType == certutil.UnknownPrivateKey { | ||
return nil, certutil.UnknownBlock, nil, errutil.InternalError{Err: fmt.Sprintf("unsupported unknown private key type for key: %s (%s)", key.ID, key.Name)} | ||
} | ||
|
||
if key.PrivateKeyType == certutil.ManagedPrivateKey { | ||
return nil, certutil.UnknownBlock, nil, errutil.InternalError{Err: fmt.Sprintf("can not get a signer from a managed key: %s (%s)", key.ID, key.Name)} | ||
} | ||
|
||
bytes, blockType, blk, err := getSignerFromBytes([]byte(key.PrivateKey)) | ||
if err != nil { | ||
return nil, certutil.UnknownBlock, nil, errutil.InternalError{Err: fmt.Sprintf("failed parsing key entry bytes for key id: %s (%s): %s", key.ID, key.Name, err.Error())} | ||
} | ||
|
||
return bytes, blockType, blk, nil | ||
} | ||
|
||
func getSignerFromBytes(keyBytes []byte) (crypto.Signer, certutil.BlockType, *pem.Block, error) { | ||
pemBlock, _ := pem.Decode(keyBytes) | ||
if pemBlock == nil { | ||
return nil, certutil.UnknownBlock, pemBlock, errutil.InternalError{Err: "no data found in PEM block"} | ||
} | ||
|
||
signer, blk, err := certutil.ParseDERKey(pemBlock.Bytes) | ||
if err != nil { | ||
return nil, certutil.UnknownBlock, pemBlock, errutil.InternalError{Err: fmt.Sprintf("failed to parse PEM block: %s", err.Error())} | ||
} | ||
return signer, blk, pemBlock, nil | ||
} | ||
|
||
func getManagedKeyPublicKey(mkc managedKeyContext, keyId managedKeyId) (crypto.PublicKey, error) { | ||
// Determine key type and key bits from the managed public key | ||
var pubKey crypto.PublicKey | ||
err := withManagedPKIKey(mkc.ctx, mkc.b, keyId, mkc.mountPoint, func(ctx context.Context, key logical.ManagedSigningKey) error { | ||
var myErr error | ||
pubKey, myErr = key.GetPublicKey(ctx) | ||
if myErr != nil { | ||
return myErr | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, errors.New("failed to lookup public key from managed key: " + err.Error()) | ||
} | ||
return pubKey, nil | ||
} | ||
|
||
func getPublicKeyFromBytes(keyBytes []byte) (crypto.PublicKey, error) { | ||
signer, _, _, err := getSignerFromBytes(keyBytes) | ||
if err != nil { | ||
return nil, errutil.InternalError{Err: fmt.Sprintf("failed parsing key bytes: %s", err.Error())} | ||
} | ||
|
||
return signer.Public(), nil | ||
} | ||
|
||
func importKeyFromBytes(mkc managedKeyContext, s logical.Storage, keyValue string, keyName string) (*keyEntry, bool, error) { | ||
signer, _, _, err := getSignerFromBytes([]byte(keyValue)) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
privateKeyType := certutil.GetPrivateKeyTypeFromSigner(signer) | ||
if privateKeyType == certutil.UnknownPrivateKey { | ||
return nil, false, errors.New("unsupported private key type within pem bundle") | ||
} | ||
|
||
key, existed, err := importKey(mkc, s, keyValue, keyName, privateKeyType) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
return key, existed, nil | ||
} |
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.
Maybe it makes sense to add a "fast path" and compare on bytes? Are there any cases where two bytes are equal but their public keys would be unequal?
That would avoid having to (unnecessarily) parse string->PEM->ASN.1->GoStruct blobs repeatedly. Just a thought.
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.
Yeah as you pointed out below not sure if it's worth it at least for this round to add in a fast path.
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.
Nah that's fair.