forked from golang/crypto
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ssh: add server side support for Diffie Hellman Group Exchange
We add this support for the following reasons: - We are planning to expose recommended (secure) vs. supported (works, not necessarily recommended) algorithms. The DHGEX kex is currently only exposed as a client-side kex. To simplify the calling convention for this follow-on, we expose the server side too. - Some clients are quite inflexible with reference to kex algorithms choice, for example they offer: diffie-hellman-group-exchange-sha256, diffie-hellman-group-exchange-sha1, diffie-hellman-group14-sha1, diffie-hellman-group1-sha1 therefore DHGEX helps interoperability. We do not recommend the DHGEX kex as a whole: - the negotiation requires an extra round trip - the server must generate parameters (slow) or hardcode them, which defeats the security benefit over traditional DH. In this implementation we hardcode sending Oakley Group 14 or Oakley Group 16. Users that are concerned with security of classical DH kex should migrate to kex based on EC or Ed25519. Fixes golang/go#54743 Change-Id: I127822e90efc36821af4aca679931f40a2023021
- Loading branch information
Showing
5 changed files
with
121 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,23 +34,29 @@ func sshClient(t *testing.T) string { | |
return sshCLI | ||
} | ||
|
||
// setupSSHCLIKeys writes the provided key files to a temporary directory and | ||
// returns the path to the private key. | ||
func setupSSHCLIKeys(t *testing.T, keyFiles map[string][]byte, privKeyName string) string { | ||
tmpDir := t.TempDir() | ||
for fn, content := range keyFiles { | ||
if err := os.WriteFile(filepath.Join(tmpDir, fn), content, 0600); err != nil { | ||
t.Fatalf("WriteFile(%q): %v", fn, err) | ||
} | ||
} | ||
return filepath.Join(tmpDir, privKeyName) | ||
} | ||
|
||
func TestSSHCLIAuth(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skipf("always fails on Windows, see #64403") | ||
} | ||
sshCLI := sshClient(t) | ||
dir := t.TempDir() | ||
keyPrivPath := filepath.Join(dir, "rsa") | ||
|
||
for fn, content := range map[string][]byte{ | ||
keyPrivPath: testdata.PEMBytes["rsa"], | ||
keyPrivPath + ".pub": ssh.MarshalAuthorizedKey(testPublicKeys["rsa"]), | ||
filepath.Join(dir, "rsa-cert.pub"): testdata.SSHCertificates["rsa-user-testcertificate"], | ||
} { | ||
if err := os.WriteFile(fn, content, 0600); err != nil { | ||
t.Fatalf("WriteFile(%q): %v", fn, err) | ||
} | ||
keyFiles := map[string][]byte{ | ||
"rsa": testdata.PEMBytes["rsa"], | ||
"rsa.pub": ssh.MarshalAuthorizedKey(testPublicKeys["rsa"]), | ||
"rsa-cert.pub": testdata.SSHCertificates["rsa-user-testcertificate"], | ||
} | ||
keyPrivPath := setupSSHCLIKeys(t, keyFiles, "rsa") | ||
|
||
certChecker := ssh.CertChecker{ | ||
IsUserAuthority: func(k ssh.PublicKey) bool { | ||
|
@@ -98,3 +104,52 @@ func TestSSHCLIAuth(t *testing.T) { | |
t.Fatalf("user certificate authentication failed, error: %v, command output %q", err, string(out)) | ||
} | ||
} | ||
|
||
func TestSSHCLIKeyExchanges(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skipf("always fails on Windows, see #64403") | ||
} | ||
sshCLI := sshClient(t) | ||
keyFiles := map[string][]byte{ | ||
"rsa": testdata.PEMBytes["rsa"], | ||
"rsa.pub": ssh.MarshalAuthorizedKey(testPublicKeys["rsa"]), | ||
} | ||
keyPrivPath := setupSSHCLIKeys(t, keyFiles, "rsa") | ||
|
||
keyExchanges := append(ssh.SupportedAlgorithms().KeyExchanges, ssh.InsecureAlgorithms().KeyExchanges...) | ||
for _, kex := range keyExchanges { | ||
t.Run(kex, func(t *testing.T) { | ||
config := &ssh.ServerConfig{ | ||
Config: ssh.Config{ | ||
KeyExchanges: []string{kex}, | ||
}, | ||
PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { | ||
if conn.User() == "testpubkey" && bytes.Equal(key.Marshal(), testPublicKeys["rsa"].Marshal()) { | ||
return nil, nil | ||
} | ||
|
||
return nil, fmt.Errorf("pubkey for %q not acceptable", conn.User()) | ||
}, | ||
} | ||
config.AddHostKey(testSigners["rsa"]) | ||
|
||
server, err := newTestServer(config) | ||
if err != nil { | ||
t.Fatalf("unable to start test server: %v", err) | ||
} | ||
defer server.Close() | ||
|
||
port, err := server.port() | ||
if err != nil { | ||
t.Fatalf("unable to get server port: %v", err) | ||
} | ||
|
||
cmd := testenv.Command(t, sshCLI, "-vvv", "-i", keyPrivPath, "-o", "StrictHostKeyChecking=no", | ||
"-o", fmt.Sprintf("KexAlgorithms=%s", kex), "-p", port, "[email protected]", "true") | ||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("%s failed, error: %v, command output %q", kex, err, string(out)) | ||
} | ||
}) | ||
} | ||
} |