Skip to content

Commit bac4c82

Browse files
committed
ssh: return an error for malformed ed25519 public keys rather than panic
An attacker can craft an ssh-ed25519 or [email protected] public key, such that the library will panic when trying to verify a signature with it. Clients can deliver such a public key and signature to any golang.org/x/crypto/ssh server with a PublicKeyCallback, and servers can deliver them to any golang.org/x/crypto/ssh client. This issue was discovered and reported by Alex Gaynor, Fish in a Barrel, and is tracked as CVE-2020-9283. Change-Id: Ie25b78a0b0181fbbc8cc7de4f4e27d908777529c Reviewed-on: https://go-review.googlesource.com/c/crypto/+/220357 Run-TryBot: Filippo Valsorda <[email protected]> Reviewed-by: Katie Hockman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 1ad67e1 commit bac4c82

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

ssh/keys.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,11 @@ func parseED25519(in []byte) (out PublicKey, rest []byte, err error) {
562562
return nil, nil, err
563563
}
564564

565-
key := ed25519.PublicKey(w.KeyBytes)
565+
if l := len(w.KeyBytes); l != ed25519.PublicKeySize {
566+
return nil, nil, fmt.Errorf("invalid size %d for Ed25519 public key", l)
567+
}
566568

567-
return (ed25519PublicKey)(key), w.Rest, nil
569+
return ed25519PublicKey(w.KeyBytes), w.Rest, nil
568570
}
569571

570572
func (k ed25519PublicKey) Marshal() []byte {
@@ -582,9 +584,11 @@ func (k ed25519PublicKey) Verify(b []byte, sig *Signature) error {
582584
if sig.Format != k.Type() {
583585
return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, k.Type())
584586
}
587+
if l := len(k); l != ed25519.PublicKeySize {
588+
return fmt.Errorf("ssh: invalid size %d for Ed25519 public key", l)
589+
}
585590

586-
edKey := (ed25519.PublicKey)(k)
587-
if ok := ed25519.Verify(edKey, b, sig.Blob); !ok {
591+
if ok := ed25519.Verify(ed25519.PublicKey(k), b, sig.Blob); !ok {
588592
return errors.New("ssh: signature did not verify")
589593
}
590594

@@ -838,6 +842,10 @@ func parseSKEd25519(in []byte) (out PublicKey, rest []byte, err error) {
838842
return nil, nil, err
839843
}
840844

845+
if l := len(w.KeyBytes); l != ed25519.PublicKeySize {
846+
return nil, nil, fmt.Errorf("invalid size %d for Ed25519 public key", l)
847+
}
848+
841849
key := new(skEd25519PublicKey)
842850
key.application = w.Application
843851
key.PublicKey = ed25519.PublicKey(w.KeyBytes)
@@ -862,6 +870,9 @@ func (k *skEd25519PublicKey) Verify(data []byte, sig *Signature) error {
862870
if sig.Format != k.Type() {
863871
return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, k.Type())
864872
}
873+
if l := len(k.PublicKey); l != ed25519.PublicKeySize {
874+
return fmt.Errorf("invalid size %d for Ed25519 public key", l)
875+
}
865876

866877
h := sha256.New()
867878
h.Write([]byte(k.application))
@@ -898,8 +909,7 @@ func (k *skEd25519PublicKey) Verify(data []byte, sig *Signature) error {
898909

899910
original := Marshal(blob)
900911

901-
edKey := (ed25519.PublicKey)(k.PublicKey)
902-
if ok := ed25519.Verify(edKey, original, edSig.Signature); !ok {
912+
if ok := ed25519.Verify(k.PublicKey, original, edSig.Signature); !ok {
903913
return errors.New("ssh: signature did not verify")
904914
}
905915

@@ -1051,7 +1061,10 @@ func NewPublicKey(key interface{}) (PublicKey, error) {
10511061
case *dsa.PublicKey:
10521062
return (*dsaPublicKey)(key), nil
10531063
case ed25519.PublicKey:
1054-
return (ed25519PublicKey)(key), nil
1064+
if l := len(key); l != ed25519.PublicKeySize {
1065+
return nil, fmt.Errorf("ssh: invalid size %d for Ed25519 public key", l)
1066+
}
1067+
return ed25519PublicKey(key), nil
10551068
default:
10561069
return nil, fmt.Errorf("ssh: unsupported key type %T", key)
10571070
}
@@ -1304,7 +1317,6 @@ func parseOpenSSHPrivateKey(key []byte, decrypt openSSHDecryptFunc) (crypto.Priv
13041317
return nil, errors.New("ssh: malformed OpenSSH key")
13051318
}
13061319

1307-
// we only handle ed25519 and rsa keys currently
13081320
switch pk1.Keytype {
13091321
case KeyAlgoRSA:
13101322
// https://github.com/openssh/openssh-portable/blob/master/sshkey.c#L2760-L2773

0 commit comments

Comments
 (0)