diff --git a/cmd/tailscale/cli/ssh_test.go b/cmd/tailscale/cli/ssh_test.go index 5ac218e1d..4332c9593 100644 --- a/cmd/tailscale/cli/ssh_test.go +++ b/cmd/tailscale/cli/ssh_test.go @@ -5,12 +5,14 @@ package cli import ( "crypto/ed25519" + "crypto/mldsa" "crypto/rand" "net" "net/netip" "os" "path/filepath" "runtime" + "slices" "strings" "testing" @@ -130,6 +132,64 @@ func TestGenKnownHosts(t *testing.T) { } } +// newMLDSA44TestKey returns a new ML-DSA-44 SSH public key and its +// authorized_keys form, without the trailing newline. +func newMLDSA44TestKey(t *testing.T) (ssh.PublicKey, string) { + t.Helper() + priv, err := mldsa.GenerateKey(mldsa.MLDSA44()) + if err != nil { + t.Fatal(err) + } + sshPub, err := ssh.NewPublicKey(priv.PublicKey()) + if err != nil { + t.Fatal(err) + } + return sshPub, strings.TrimSpace(string(ssh.MarshalAuthorizedKey(sshPub))) +} + +func TestGenKnownHostsPQ(t *testing.T) { + if !slices.Contains(ssh.SupportedAlgorithms().HostKeys, ssh.KeyAlgoMLDSA44) { + t.Skip("ML-DSA host keys are not supported in this build") + } + edKey, edStr := newSSHTestKey(t) + pqKey, pqStr := newMLDSA44TestKey(t) + otherPQKey, _ := newMLDSA44TestKey(t) + + foo := &ipnstate.PeerStatus{ + DNSName: "foo.example.ts.net.", + TailscaleIPs: []netip.Addr{netip.MustParseAddr("100.64.0.1")}, + SSH_HostKeys: []string{edStr, pqStr}, + } + st := sshTestStatus(foo) + got := genKnownHosts(st) + + khFile := filepath.Join(t.TempDir(), "known_hosts") + if err := os.WriteFile(khFile, got, 0600); err != nil { + t.Fatal(err) + } + check, err := knownhosts.New(khFile) + if err != nil { + t.Fatalf("knownhosts.New: %v", err) + } + fakeAddr := &net.TCPAddr{IP: net.IPv4(100, 64, 0, 1), Port: 22} + for _, tt := range []struct { + name string + host string + key ssh.PublicKey + wantErr bool + }{ + {"dns_ed25519", "foo.example.ts.net.", edKey, false}, + {"dns_mldsa44", "foo.example.ts.net.", pqKey, false}, + {"ip_mldsa44", "100.64.0.1", pqKey, false}, + {"wrong_mldsa44", "foo.example.ts.net.", otherPQKey, true}, + } { + err := check(net.JoinHostPort(tt.host, "22"), fakeAddr, tt.key) + if (err != nil) != tt.wantErr { + t.Errorf("%s: known_hosts check err=%v, wantErr=%v", tt.name, err, tt.wantErr) + } + } +} + func TestWriteKnownHosts(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("relies on XDG_CONFIG_HOME controlling os.UserConfigDir")