ssh: permit empty but non-nil HostKeyAlgorithms, KeyExchanges, Ciphers, MACs The documentation of ClientConfig.HostKeyAlgorithms says: If empty, a reasonable default is used. However, if HostKeyAlgorithms is empty but non-nil, the reasonable default won't be applied, and the SSH handshake will fail. Fix this by changing the nil check to a length check. While here, do the same for KeyExchanges, Ciphers, MACs in Config. Change-Id: I1b3d2d2159f3c7d57a9a690ad04f1f6818b39a5c Reviewed-on: https://go-review.googlesource.com/c/crypto/+/800080 Reviewed-by: Nicola Murino <nicola.murino@gmail.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Filippo Valsorda <filippo@golang.org> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Nicola Murino <nicola.murino@gmail.com>
diff --git a/ssh/common.go b/ssh/common.go index 4024712..aed0fd9 100644 --- a/ssh/common.go +++ b/ssh/common.go
@@ -544,7 +544,7 @@ if c.Rand == nil { c.Rand = rand.Reader } - if c.Ciphers == nil { + if len(c.Ciphers) == 0 { c.Ciphers = defaultCiphers } var ciphers []string @@ -556,7 +556,7 @@ } c.Ciphers = ciphers - if c.KeyExchanges == nil { + if len(c.KeyExchanges) == 0 { c.KeyExchanges = defaultKexAlgos } var kexs []string @@ -571,7 +571,7 @@ } c.KeyExchanges = kexs - if c.MACs == nil { + if len(c.MACs) == 0 { c.MACs = defaultMACs } var macs []string
diff --git a/ssh/handshake.go b/ssh/handshake.go index 4be3cbb..711a7f7 100644 --- a/ssh/handshake.go +++ b/ssh/handshake.go
@@ -162,7 +162,7 @@ t.remoteAddr = addr t.hostKeyCallback = config.HostKeyCallback t.bannerCallback = config.BannerCallback - if config.HostKeyAlgorithms != nil { + if len(config.HostKeyAlgorithms) > 0 { t.hostKeyAlgorithms = config.HostKeyAlgorithms } else { t.hostKeyAlgorithms = defaultHostKeyAlgos