From f9d0a39815d5b09567dc0ad26c4c2b48f24f5548 Mon Sep 17 00:00:00 2001 From: Erik Karulf Date: Fri, 13 Nov 2026 01:14:07 -0600 Subject: [PATCH] Address issue #17 --- README.md | 7 ++++ cmd/qcat/client_trust.go | 57 +++++++++++++++++++++++++++++ cmd/qcat/client_trust_test.go | 69 +++++++++++++++++++++++++++++++++++ cmd/qcat/main.go | 1 + cmd/qcat/mosh.go | 1 + 5 files changed, 135 insertions(+) create mode 100644 cmd/qcat/client_trust.go create mode 100644 cmd/qcat/client_trust_test.go diff --git a/README.md b/README.md index 05af003..b5b7d09 100644 --- a/README.md +++ b/README.md @@ -379,3 +379,10 @@ Contributions, testing on real networks, protocol review, and security analysis ## License See [LICENSE](LICENSE). + +Running clients (`connect`, `forward`, `socks`, `up`, and `mosh`) recheck the +originally pinned server identity every second and before bootstrap sends and +after bootstrap completion. Removal, replacement, or an unreadable descriptor +cancels the connection within one polling interval plus local scheduling and +filesystem latency. Replacement requires a new connection. This is local +distrust of a server; to revoke a client remotely, remove it on the server. diff --git a/cmd/qcat/client_trust.go b/cmd/qcat/client_trust.go new file mode 100644 index 0000000..9bc9d7f --- /dev/null +++ b/cmd/qcat/client_trust.go @@ -0,0 +1,57 @@ +package main + +import ( + "bytes" + "context" + "errors" + "time" + + "github.com/ekarulf/quantumcat/internal/config" + "github.com/tailscale/tailcat" +) + +// guardClientTrust is shared by connect, forward, socks, up and mosh. Keep the +// startup identity pinned: replacing a name never adopts a new trust anchor. +func guardClientTrust(ctx context.Context, cancel context.CancelFunc, c *tailcat.Client, store config.Store, name string, pinned []byte) { + check := func() error { + peer, err := store.Peer(name) + if err != nil || !bytes.Equal(peer.PublicKey, pinned) { + cancel() + return errors.New("paired server removed, replaced, or unreadable") + } + return nil + } + bootstrap := c.Bootstrap + c.Bootstrap = func(ctx context.Context, send func([]byte) error, recv <-chan []byte) (tailcat.PresharedKey, error) { + if err := check(); err != nil { + return tailcat.PresharedKey{}, err + } + psk, err := bootstrap(ctx, func(b []byte) error { + if err := check(); err != nil { + return err + } + return send(b) + }, recv) + if trustErr := check(); trustErr != nil { + clear(psk[:]) + return tailcat.PresharedKey{}, trustErr + } + return psk, err + } + go func() { + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + if check() != nil { + return + } + } + } + }() + // Also closes the device for modes whose application listener outlives ctx. + context.AfterFunc(ctx, func() { c.Close() }) +} diff --git a/cmd/qcat/client_trust_test.go b/cmd/qcat/client_trust_test.go new file mode 100644 index 0000000..3c09a00 --- /dev/null +++ b/cmd/qcat/client_trust_test.go @@ -0,0 +1,69 @@ +package main + +import ( + "context" + "os" + "path/filepath" + "testing" + "time" + + "github.com/ekarulf/quantumcat/internal/config" + "github.com/ekarulf/quantumcat/internal/crypto/provider/software" + "github.com/ekarulf/quantumcat/internal/protocol" + "github.com/tailscale/tailcat" +) + +func TestClientTrustRemovalAndReplacement(t *testing.T) { + for _, mutation := range []string{"remove", "replace"} { + t.Run(mutation, func(t *testing.T) { + store := config.Store{Dir: t.TempDir()} + identity, err := software.Generate() + if err != nil { + t.Fatal(err) + } + defer identity.Destroy() + pub, _ := identity.PublicKey(context.Background()) + path := filepath.Join(store.Dir, "peers", "server.qpeer") + peer := config.Peer{Version: config.PeerVersion, Name: "server", PublicKey: pub, PeerID: protocol.ID(pub).String()} + if err := config.WriteNew(path, peer); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + entered, release := make(chan struct{}), make(chan struct{}) + c := &tailcat.Client{Bootstrap: func(context.Context, func([]byte) error, <-chan []byte) (tailcat.PresharedKey, error) { + close(entered) + <-release + return tailcat.PresharedKey{1}, nil + }} + guardClientTrust(ctx, cancel, c, store, "server", pub) + done := make(chan tailcat.PresharedKey, 1) + go func() { psk, _ := c.Bootstrap(ctx, nil, nil); done <- psk }() + <-entered + if err := os.Remove(path); err != nil { + t.Fatal(err) + } + if mutation == "replace" { + other, _ := software.Generate() + defer other.Destroy() + peer.PublicKey, _ = other.PublicKey(ctx) + peer.PeerID = protocol.ID(peer.PublicKey).String() + if err := config.WriteNew(path, peer); err != nil { + t.Fatal(err) + } + } + select { + case <-ctx.Done(): + case <-time.After(3 * time.Second): + t.Fatal("active client not cancelled") + } + close(release) + if psk := <-done; !psk.IsZero() { + t.Fatal("in-flight renewal returned revoked PSK") + } + if psk, err := c.Bootstrap(ctx, nil, nil); err == nil || !psk.IsZero() { + t.Fatal("revoked client renewed") + } + }) + } +} diff --git a/cmd/qcat/main.go b/cmd/qcat/main.go index d38dc91..d270458 100644 --- a/cmd/qcat/main.go +++ b/cmd/qcat/main.go @@ -456,6 +456,7 @@ func client(ctx context.Context, s config.Store, command string, args []string) return err } defer c.Close() + guardClientTrust(ctx, cancel, c, s, args[0], append([]byte(nil), p.PublicKey...)) c.Logf = transportLogf(*verbose) var tun *tunnel.Tunnel if command == "up" { diff --git a/cmd/qcat/mosh.go b/cmd/qcat/mosh.go index 74723d2..f648660 100644 --- a/cmd/qcat/mosh.go +++ b/cmd/qcat/mosh.go @@ -211,6 +211,7 @@ func mosh(parent context.Context, s config.Store, args []string) error { return err } defer c.Close() + guardClientTrust(ctx, cancel, c, s, peerName, append([]byte(nil), p.PublicKey...)) c.Logf = transportLogf(*verbose) if _, err = c.Ping(ctx); err != nil { return err