Skip to content
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

more accurate error message #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkcs8.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func ParsePrivateKey(der []byte, password []byte) (interface{}, KDFParameters, e
// Use the password provided to decrypt the private key
var privKey encryptedPrivateKeyInfo
if _, err := asn1.Unmarshal(der, &privKey); err != nil {
return nil, nil, errors.New("pkcs8: only PKCS #5 v2.0 supported")
return nil, nil, errors.New("pkcs8: failed to parse encrypted private key (make sure you passed the DER as the first parameter)")
}

if !privKey.EncryptionAlgorithm.Algorithm.Equal(oidPBES2) {
Expand Down
58 changes: 58 additions & 0 deletions pkcs8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,64 @@ func TestParsePKCS8PrivateKey(t *testing.T) {
}
}

func TestParsePKCS8PrivateKeyInvalidDER(t *testing.T) {
keyList := []struct {
name string
encrypted string
password string
}{
{
name: "encryptedRSA2048aes",
encrypted: encryptedRSA2048aes,
password: "password",
},
{
name: "encryptedRSA2048des3",
encrypted: encryptedRSA2048des3,
password: "password",
},
{
name: "encryptedRSA2048scrypt",
encrypted: encryptedRSA2048scrypt,
password: "password",
},
{
name: "encryptedEC256aes",
encrypted: encryptedEC256aes,
password: "password",
},
{
name: "encryptedEC256aes128sha1",
encrypted: encryptedEC256aes128sha1,
password: "password",
},
{
name: "encryptedRFCscrypt",
encrypted: encryptedRFCscrypt,
password: "Rabbit",
},
{
name: "encryptedEC128aes",
encrypted: encryptedEC128aes,
password: "password",
},
}
expectedError := "pkcs8: failed to parse encrypted private key (make sure you passed the DER as the first parameter)"

for i, key := range keyList {
t.Run(key.name, func(t *testing.T) {
_, err := pkcs8.ParsePKCS8PrivateKey([]byte(key.encrypted), []byte(key.password))
if err == nil {
t.Errorf("%d: should have failed", i)
} else {
if err.Error() != expectedError {
t.Errorf("Expected error \"%s\", got \"%s\"", expectedError, err.Error())
}
}
})
}
}

func TestConvertPrivateKeyToPKCS8(t *testing.T) {
for i, password := range [][]byte{nil, []byte("password")} {
var args [][]byte
Expand Down