diff --git a/util/git/ssh.go b/util/git/ssh.go index a1cb337a80..36e4ab67a4 100644 --- a/util/git/ssh.go +++ b/util/git/ssh.go @@ -1,13 +1,14 @@ package git import ( + "crypto/fips140" "fmt" gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh" "golang.org/x/crypto/ssh" ) -// List of all currently supported algorithms for SSH key exchange +// SupportedSSHKeyExchangeAlgorithms is a list of all currently supported algorithms for SSH key exchange // Unfortunately, crypto/ssh does not offer public constants or list for // this. var SupportedSSHKeyExchangeAlgorithms = []string{ @@ -21,10 +22,15 @@ var SupportedSSHKeyExchangeAlgorithms = []string{ "diffie-hellman-group14-sha1", } -// List of default key exchange algorithms to use. We use those that are -// available by default, we can become more opinionated later on (when -// we support configuration of algorithms to use). -var DefaultSSHKeyExchangeAlgorithms = SupportedSSHKeyExchangeAlgorithms +// SupportedFIPSCompliantSSHKeyExchangeAlgorithms is a list of all currently supported algorithms for SSH key exchange +// that are FIPS compliant +var SupportedFIPSCompliantSSHKeyExchangeAlgorithms = []string{ + "ecdh-sha2-nistp256", + "ecdh-sha2-nistp384", + "ecdh-sha2-nistp521", + "diffie-hellman-group-exchange-sha256", + "diffie-hellman-group14-sha256", +} // PublicKeysWithOptions is an auth method for go-git's SSH client that // inherits from PublicKeys, but provides the possibility to override @@ -51,9 +57,17 @@ func (a *PublicKeysWithOptions) ClientConfig() (*ssh.ClientConfig, error) { if len(a.KexAlgorithms) > 0 { kexAlgos = a.KexAlgorithms } else { - kexAlgos = DefaultSSHKeyExchangeAlgorithms + kexAlgos = getDefaultSSHKeyExchangeAlgorithms() } config := ssh.Config{KeyExchanges: kexAlgos} opts := &ssh.ClientConfig{Config: config, User: a.User, Auth: []ssh.AuthMethod{ssh.PublicKeys(a.Signer)}} return a.SetHostKeyCallback(opts) } + +// getDefaultSSHKeyExchangeAlgorithms returns the default key exchange algorithms to be used +func getDefaultSSHKeyExchangeAlgorithms() []string { + if fips140.Enabled() { + return SupportedFIPSCompliantSSHKeyExchangeAlgorithms + } + return SupportedSSHKeyExchangeAlgorithms +}