diff options
| author | Nicola Murino <nicola.murino@gmail.com> | 2023-10-31 18:02:46 +0100 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-11-08 18:01:48 +0000 |
| commit | 42c83fffffc70640068263e765db9c9b09cd2ba2 (patch) | |
| tree | 9db625162399f9d7aa822b112fb1cf0cba480bdd /ssh/keys_test.go | |
| parent | e668aa9b451cd0866ba1c81c26309815c020c61f (diff) | |
| download | go-x-crypto-42c83fffffc70640068263e765db9c9b09cd2ba2.tar.xz | |
ssh: try harder to detect incorrect passwords for legacy PEM encryption
Because of deficiencies in the format, DecryptPEMBlock does not always
detect an incorrect password. In these cases decrypted DER bytes is
random noise. If the parsing of the key returns an asn1.StructuralError
we return x509.IncorrectPasswordError.
Fixes golang/go#62265
Change-Id: Ib8b845f2bd01662c1f1421d35859a32ac5b78da7
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/538835
Reviewed-by: Heschi Kreinick <heschi@google.com>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'ssh/keys_test.go')
| -rw-r--r-- | ssh/keys_test.go | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/ssh/keys_test.go b/ssh/keys_test.go index 76d2338..3e18488 100644 --- a/ssh/keys_test.go +++ b/ssh/keys_test.go @@ -16,6 +16,7 @@ import ( "encoding/base64" "encoding/hex" "encoding/pem" + "errors" "fmt" "io" "reflect" @@ -221,6 +222,16 @@ func TestParseEncryptedPrivateKeysWithPassphrase(t *testing.T) { } } +func TestParseEncryptedPrivateKeysWithIncorrectPassphrase(t *testing.T) { + pem := testdata.PEMEncryptedKeys[0].PEMBytes + for i := 0; i < 4096; i++ { + _, err := ParseRawPrivateKeyWithPassphrase(pem, []byte(fmt.Sprintf("%d", i))) + if !errors.Is(err, x509.IncorrectPasswordError) { + t.Fatalf("expected error: %v, got: %v", x509.IncorrectPasswordError, err) + } + } +} + func TestParseDSA(t *testing.T) { // We actually exercise the ParsePrivateKey codepath here, as opposed to // using the ParseRawPrivateKey+NewSignerFromKey path that testdata_test.go |
