aboutsummaryrefslogtreecommitdiff
path: root/ssh/keys.go
diff options
context:
space:
mode:
authorNicola Murino <nicola.murino@gmail.com>2023-10-31 18:02:46 +0100
committerGopher Robot <gobot@golang.org>2023-11-08 18:01:48 +0000
commit42c83fffffc70640068263e765db9c9b09cd2ba2 (patch)
tree9db625162399f9d7aa822b112fb1cf0cba480bdd /ssh/keys.go
parente668aa9b451cd0866ba1c81c26309815c020c61f (diff)
downloadgo-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.go')
-rw-r--r--ssh/keys.go19
1 files changed, 15 insertions, 4 deletions
diff --git a/ssh/keys.go b/ssh/keys.go
index ef1bad7..df4ebda 100644
--- a/ssh/keys.go
+++ b/ssh/keys.go
@@ -1232,16 +1232,27 @@ func ParseRawPrivateKeyWithPassphrase(pemBytes, passphrase []byte) (interface{},
return nil, fmt.Errorf("ssh: cannot decode encrypted private keys: %v", err)
}
+ var result interface{}
+
switch block.Type {
case "RSA PRIVATE KEY":
- return x509.ParsePKCS1PrivateKey(buf)
+ result, err = x509.ParsePKCS1PrivateKey(buf)
case "EC PRIVATE KEY":
- return x509.ParseECPrivateKey(buf)
+ result, err = x509.ParseECPrivateKey(buf)
case "DSA PRIVATE KEY":
- return ParseDSAPrivateKey(buf)
+ result, err = ParseDSAPrivateKey(buf)
default:
- return nil, fmt.Errorf("ssh: unsupported key type %q", block.Type)
+ err = fmt.Errorf("ssh: unsupported key type %q", block.Type)
}
+ // 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.
+ if _, ok := err.(asn1.StructuralError); ok {
+ return nil, x509.IncorrectPasswordError
+ }
+
+ return result, err
}
// ParseDSAPrivateKey returns a DSA private key from its ASN.1 DER encoding, as