diff --git a/changelog/22753.txt b/changelog/22753.txt new file mode 100644 index 000000000..a297337f9 --- /dev/null +++ b/changelog/22753.txt @@ -0,0 +1,3 @@ +```release-note:bug +secrets/transit: fix panic when providing non-PEM formatted public key for import +``` diff --git a/sdk/helper/keysutil/policy.go b/sdk/helper/keysutil/policy.go index 869733b3e..d3f8bc2e9 100644 --- a/sdk/helper/keysutil/policy.go +++ b/sdk/helper/keysutil/policy.go @@ -1531,9 +1531,13 @@ func (p *Policy) ImportPublicOrPrivate(ctx context.Context, storage logical.Stor } } else { pemBlock, _ := pem.Decode(key) + if pemBlock == nil { + return fmt.Errorf("error parsing public key: not in PEM format") + } + parsedKey, err = x509.ParsePKIXPublicKey(pemBlock.Bytes) if err != nil { - return fmt.Errorf("error parsing public key: %s", err) + return fmt.Errorf("error parsing public key: %w", err) } } @@ -2173,6 +2177,9 @@ func (p *Policy) ImportPrivateKeyForVersion(ctx context.Context, storage logical case *ecdsa.PrivateKey: ecdsaKey := parsedPrivateKey.(*ecdsa.PrivateKey) pemBlock, _ := pem.Decode([]byte(keyEntry.FormattedPublicKey)) + if pemBlock == nil { + return fmt.Errorf("failed to parse key entry public key: invalid PEM blob") + } publicKey, err := x509.ParsePKIXPublicKey(pemBlock.Bytes) if err != nil || publicKey == nil { return fmt.Errorf("failed to parse key entry public key: %v", err)