Skip to content

Commit 4dfc5bc

Browse files
committed
fix: fail for unknown private key encryption methods
1 parent d02f28d commit 4dfc5bc

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

privatekey.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ var (
1616

1717
// ErrNotAPrivateKey indicates that a given PEM block did not contain a known private key format.
1818
ErrNotAPrivateKey = errors.New("invalid Key: PEM block must be a PKCS #1, PKCS #8 or SEC 1 private key")
19+
20+
// ErrUnknownEncryption indicates that the given PEM block was not encrypted in a known format,
21+
// or not encrypted in the first place.
22+
ErrUnknownEncryption = errors.New("invalid encryption: PEM block is encrypted in a unknown format, or not encrypted at all")
1923
)
2024

2125
// LoadPrivateKey tries to load a private key from a given path.
@@ -68,8 +72,9 @@ func ParsePrivateKeyFromEncryptedPEMBytes(pemBytes []byte, password []byte) (cry
6872
} else if pkcs8Decryption, err := tryPKCS8Decryption(block, password); err == nil {
6973
blockDecrypted = pkcs8Decryption
7074
} else {
71-
// Either its not a password secured block, or
72-
blockDecrypted = block.Bytes
75+
// Either its not a password secured block, or is encrypted in a format
76+
// we don't know.
77+
return nil, ErrUnknownEncryption
7378
}
7479

7580
return ParsePrivateKeyFromDERBytes(blockDecrypted)

privatekey_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func TestParsePrivateKeyFromEncryptedPEMBytes(t *testing.T) {
132132
{name: "RSA PKCS8 RFC1423 wrong password", args: args{pemBytes: encryptedRsaPKCS8WrongPassword}, want: nil, wantErr: x509.IncorrectPasswordError},
133133

134134
{name: "RSA PKCS8 2.0", args: args{pemBytes: rsaPKCS8Encrypted}, want: rsaKey, wantErr: nil},
135-
{name: "RSA PKCS8 2.0 wrong password", args: args{pemBytes: rsaPKCS8EncryptedWrongPassword}, want: nil, wantErr: keybox.ErrNotAPrivateKey},
135+
{name: "RSA PKCS8 2.0 wrong password", args: args{pemBytes: rsaPKCS8EncryptedWrongPassword}, want: nil, wantErr: keybox.ErrUnknownEncryption},
136136

137137
{name: "ecdsa PKCS8 RFC1423", args: args{pemBytes: encryptedEcdsaPKCS8}, want: ecdsaKey, wantErr: nil},
138138
{name: "ecdsa SEC1 RFC1423", args: args{pemBytes: encryptedEcdsaSEC1}, want: ecdsaKey, wantErr: nil},

0 commit comments

Comments
 (0)