mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-04 23:52:20 -08:00
cmd/tailscale/cli: test known_hosts generation with ML-DSA host keys
Verify that genKnownHosts and golang.org/x/crypto/ssh/knownhosts accept and validate ssh-mldsa-44 host keys advertised in PeerStatus.SSH_HostKeys alongside classical Ed25519 keys. Updates #22864 Change-Id: I7985492c8a4e495764b783ebed9f553974f2e3a9 Signed-off-by: James Tucker <[email protected]>
This commit is contained in:
1 parent
f8accbe3c4
commit
f0c37d5807
1 file changed
+60
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user