diff --git a/PROTOCOL.md b/PROTOCOL.md index 20bf77a..6bbf379 100644 --- a/PROTOCOL.md +++ b/PROTOCOL.md @@ -273,3 +273,11 @@ There are no acknowledgements or retries. Missing fragments cause loss of that datagram, never delivery of a partial message. Complete duplicate datagrams may still be delivered; the application owns replay handling. This framing does not change the bootstrap or WireGuard protocol. + +Authorization uses one immutable map snapshot per published generation. Blinded +ClientHello tags enumerate that index; finishes and installed-session validity +checks use expected O(1) known-PeerID lookup in the same index. Snapshot +replacement applies key changes and revocation to both paths. This is map +complexity, not constant-time cryptographic comparison. Candidate testing, +retrospective linkage given a candidate identity, and visible network metadata +remain unchanged. diff --git a/cmd/qcat/main.go b/cmd/qcat/main.go index d38dc91..b901752 100644 --- a/cmd/qcat/main.go +++ b/cmd/qcat/main.go @@ -305,17 +305,11 @@ func serve(ctx context.Context, s config.Store, args []string) error { for _, p := range paired { authorized[protocol.ID(p.PublicKey)] = p.PublicKey } - // Blinded client peer tags make the protocol enumerate authorized identities - // rather than index them, so hand it a snapshot iterator over the reloaded - // map instead of a lookup callback. - server := transport.Server(i, pub, meta.Node, func(yield func(protocol.PeerID, []byte) bool) { + snapshot := protocol.NewAuthorizations(authorized) + server := transport.Server(i, pub, meta.Node, func() *protocol.AuthorizationSnapshot { mu.RLock() defer mu.RUnlock() - for id, key := range authorized { - if !yield(id, key) { - return - } - } + return snapshot }) if *tunMode { tun, err := tunnel.Open() @@ -402,6 +396,7 @@ func serve(ctx context.Context, s config.Store, args []string) error { next := reloadAuthorizations(s, os.Stderr, &reloadState, authorized) mu.Lock() authorized = next + snapshot = protocol.NewAuthorizations(next) mu.Unlock() } } diff --git a/cmd/qcat/mosh_test.go b/cmd/qcat/mosh_test.go index 7e7a805..0ac5518 100644 --- a/cmd/qcat/mosh_test.go +++ b/cmd/qcat/mosh_test.go @@ -222,7 +222,7 @@ func TestMoshSessionWithMockExecutables(t *testing.T) { } si, _ := software.Generate() sp, _ := si.PublicKey(ctx) - server := transport.Server(si, sp, key.NewNode(), func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) }) + server := transport.Server(si, sp, key.NewNode(), protocol.EnumeratedAuthorizations(func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) })) server.Region = &tailcfg.DERPRegion{RegionID: 1, RegionCode: "test", Nodes: []*tailcfg.DERPNode{{Name: "test", RegionID: 1, HostName: "127.0.0.1", IPv4: "127.0.0.1", IPv6: "none", DERPPort: httpRelay.Listener.Addr().(*net.TCPAddr).Port, STUNPort: -1, InsecureForTests: true}}} remotePort := echo.LocalAddr().(*net.UDPAddr).Port server.OnUDP, err = proxy.UDPServices(ctx, map[uint16]string{uint16(remotePort): echo.LocalAddr().String()}) diff --git a/internal/crypto/provider/se/se_test.go b/internal/crypto/provider/se/se_test.go index fcc5c72..5a74a00 100644 --- a/internal/crypto/provider/se/se_test.go +++ b/internal/crypto/provider/se/se_test.go @@ -55,7 +55,7 @@ func TestSecureEnclaveStandardLibraryInteroperability(t *testing.T) { rand.Read(local.Disco[:]) rand.Read(remote.WG[:]) rand.Read(remote.Disco[:]) - server := &protocol.Server{Identity: si, Public: sp, Keys: remote, Authorized: func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(pub), pub) }} + server := &protocol.Server{Identity: si, Public: sp, Keys: remote, Authorized: protocol.EnumeratedAuthorizations(func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(pub), pub) })} defer server.Close() c, hello, err := protocol.NewClient(ctx, h, se.NewKEM, sp, local, remote, time.Now()) if err != nil { @@ -83,7 +83,7 @@ func TestSecureEnclaveStandardLibraryInteroperability(t *testing.T) { rand.Read(clientKeys.Disco[:]) rand.Read(serverKeys.WG[:]) rand.Read(serverKeys.Disco[:]) - enclaveServer := &protocol.Server{Identity: h, Public: pub, Keys: serverKeys, Authorized: func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(sp), sp) }} + enclaveServer := &protocol.Server{Identity: h, Public: pub, Keys: serverKeys, Authorized: protocol.EnumeratedAuthorizations(func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(sp), sp) })} defer enclaveServer.Close() softwareClient, clientHello, err := protocol.NewClient(ctx, si, software.NewKEM, pub, clientKeys, serverKeys, time.Now()) if err != nil { diff --git a/internal/protocol/blinding_test.go b/internal/protocol/blinding_test.go index 44767e3..2b6beeb 100644 --- a/internal/protocol/blinding_test.go +++ b/internal/protocol/blinding_test.go @@ -134,12 +134,12 @@ func TestResolutionDistinguishesTwoRealIdentities(t *testing.T) { if err != nil { t.Fatal(err) } - s.Authorized = func(yield func(PeerID, []byte) bool) { + s.Authorized = EnumeratedAuthorizations(func(yield func(PeerID, []byte) bool) { if !yield(ID(firstPub), firstPub) { return } yield(ID(otherPub), otherPub) - } + }) otherSrc := [32]byte{7} otherClient, otherHello, err := NewClient(ctx, other, software.NewKEM, first.serverPublic, Keys{WG: otherSrc, Disco: [32]byte{2}}, first.keys, now) if err != nil { @@ -231,13 +231,13 @@ func TestServerResolvesTagAmongManyAuthorizedPeers(t *testing.T) { } set := decoys(t, 16) set[ID(pub)] = pub - s.Authorized = func(yield func(PeerID, []byte) bool) { + s.Authorized = EnumeratedAuthorizations(func(yield func(PeerID, []byte) bool) { for id, key := range set { if !yield(id, key) { return } } - } + }) reply, session := s.Handle(ctx, src, hello, now) if len(reply) == 0 || session != nil { t.Fatal("server failed to resolve a blinded tag against its authorized set") @@ -396,13 +396,13 @@ func TestRevokedIdentityTagNoLongerResolves(t *testing.T) { } spent := s.peerLimits[[32]byte(ID(pub))] set := decoys(t, 4) - s.Authorized = func(yield func(PeerID, []byte) bool) { + s.Authorized = EnumeratedAuthorizations(func(yield func(PeerID, []byte) bool) { for id, key := range set { if !yield(id, key) { return } } - } + }) next, renewed, err := NewClient(ctx, c.identity, software.NewKEM, c.serverPublic, Keys{WG: src, Disco: [32]byte{1}}, c.keys, now.Add(time.Second)) if err != nil { t.Fatal(err) @@ -471,3 +471,41 @@ func TestRenewalChangesTagButNotOwner(t *testing.T) { } renewed.Installed(true) } + +func TestSnapshotLookupAndEnumerationShareRevocation(t *testing.T) { + id := PeerID{1} + input := map[PeerID][]byte{id: {7}} + snapshot := NewAuthorizations(input) + input[id][0] = 9 + authority := func() *AuthorizationSnapshot { return snapshot } + if Lookup(authority, id)[0] != 7 { + t.Fatal("input mutated snapshot") + } + key := Lookup(authority, id) + key[0] = 8 + for got, pub := range snapshot.All() { + if got != id || pub[0] != 7 { + t.Fatal("lookup and enumeration differ") + } + pub[0] = 3 + } + if Lookup(authority, id)[0] != 7 { + t.Fatal("output mutated snapshot") + } + snapshot = NewAuthorizations(map[PeerID][]byte{id: {2}}) + if Lookup(authority, id)[0] != 2 { + t.Fatal("lookup missed key change") + } + for _, pub := range snapshot.All() { + if pub[0] != 2 { + t.Fatal("enumeration missed key change") + } + } + snapshot = NewAuthorizations(nil) + if Lookup(authority, id) != nil { + t.Fatal("lookup retained revoked identity") + } + for range snapshot.All() { + t.Fatal("enumeration retained revoked identity") + } +} diff --git a/internal/protocol/handshake.go b/internal/protocol/handshake.go index cf31c51..1524746 100644 --- a/internal/protocol/handshake.go +++ b/internal/protocol/handshake.go @@ -10,7 +10,6 @@ import ( "crypto/sha512" "encoding/binary" "errors" - "iter" "sync" "time" @@ -159,11 +158,10 @@ type Server struct { Identity provider.Identity Public []byte Keys Keys - // Authorized enumerates each authorized peer's full PeerID and ML-DSA public - // key. It must be served from memory and must omit revoked peers: handshake - // processing performs no filesystem enumeration or parsing. Protocol code - // only reads the set; it never retains or mutates it. - Authorized iter.Seq2[PeerID, []byte] + // Authorized returns the current immutable in-memory authority snapshot. + // Tag enumeration and known-identity lookup use the same snapshot index. + // Publishing a replacement revokes both access paths together. + Authorized Authorizations pending map[[hashSize]byte]*pending replay map[[hashSize]byte]time.Time limits map[[32]byte]bucket @@ -416,7 +414,7 @@ func (s *Server) resolve(tag, nonce []byte) (PeerID, []byte) { if s.Authorized == nil { return PeerID{}, nil } - for id, pub := range s.Authorized { + for id, pub := range s.Authorized().All() { if candidate := peerTag(id, nonce); hmac.Equal(candidate[:], tag) { return id, pub } diff --git a/internal/protocol/handshake_test.go b/internal/protocol/handshake_test.go index e4a69f5..e3b36e6 100644 --- a/internal/protocol/handshake_test.go +++ b/internal/protocol/handshake_test.go @@ -33,7 +33,7 @@ func fixture(t *testing.T) (*Server, *Client, []byte, [32]byte, time.Time) { rand.Read(remote.WG[:]) rand.Read(remote.Disco[:]) now := time.Now().Truncate(time.Second) - s := &Server{Identity: si, Public: sp, Keys: remote, Authorized: func(yield func(PeerID, []byte) bool) { yield(ID(cp), cp) }} + s := &Server{Identity: si, Public: sp, Keys: remote, Authorized: EnumeratedAuthorizations(func(yield func(PeerID, []byte) bool) { yield(ID(cp), cp) })} c, hello, err := NewClient(ctx, ci, software.NewKEM, sp, local, remote, now) if err != nil { t.Fatal(err) @@ -151,7 +151,7 @@ func TestFreshnessSourceRevocationAndExpiry(t *testing.T) { case "source": src[0] ^= 1 case "unknown": - s.Authorized = func(func(PeerID, []byte) bool) {} + s.Authorized = EnumeratedAuthorizations(func(func(PeerID, []byte) bool) {}) } reply, session := s.Handle(ctx, src, hello, now) if test == "stale" || test == "future" || test == "source" || test == "unknown" { @@ -166,7 +166,7 @@ func TestFreshnessSourceRevocationAndExpiry(t *testing.T) { } switch test { case "revoked": - s.Authorized = func(func(PeerID, []byte) bool) {} + s.Authorized = EnumeratedAuthorizations(func(func(PeerID, []byte) bool) {}) case "expired": now = now.Add(31 * time.Second) case "bad-finish": diff --git a/internal/protocol/review_test.go b/internal/protocol/review_test.go index a9a15ff..49380fa 100644 --- a/internal/protocol/review_test.go +++ b/internal/protocol/review_test.go @@ -89,12 +89,12 @@ func TestPendingQuotaSharedByIdentityAcrossSources(t *testing.T) { defer other.Destroy() pub, _ := other.PublicKey(ctx) authorized := s.Authorized - s.Authorized = func(yield func(PeerID, []byte) bool) { + s.Authorized = EnumeratedAuthorizations(func(yield func(PeerID, []byte) bool) { if !yield(ID(pub), pub) { return } - authorized(yield) - } + authorized().All()(yield) + }) c, hello, err := NewClient(ctx, other, software.NewKEM, client.serverPublic, Keys{WG: [32]byte{99}, Disco: [32]byte{1}}, client.keys, now.Add(10*time.Second)) if err != nil { t.Fatal(err) diff --git a/internal/protocol/wire.go b/internal/protocol/wire.go index 085634c..46f7167 100644 --- a/internal/protocol/wire.go +++ b/internal/protocol/wire.go @@ -66,20 +66,51 @@ func peerTag(id PeerID, nonce []byte) [32]byte { return [32]byte(m.Sum(nil)[:32]) } -// Lookup returns the ML-DSA public key authorized for id, or nil when id is -// unknown or revoked. Blinded tags already require the server to enumerate its -// authorized peers, so enumeration is the single authorization interface and no -// separate lookup callback can drift from it. -func Lookup(peers iter.Seq2[PeerID, []byte], id PeerID) []byte { +// AuthorizationSnapshot owns one immutable index for both access paths. Keys +// are copied on construction and return so callers cannot mutate its authority. +type AuthorizationSnapshot struct{ peers map[PeerID][]byte } +type Authorizations func() *AuthorizationSnapshot + +func NewAuthorizations(peers map[PeerID][]byte) *AuthorizationSnapshot { + s := &AuthorizationSnapshot{peers: make(map[PeerID][]byte, len(peers))} + for id, pub := range peers { + s.peers[id] = append([]byte(nil), pub...) + } + return s +} +func (s *AuthorizationSnapshot) All() iter.Seq2[PeerID, []byte] { + return func(yield func(PeerID, []byte) bool) { + if s == nil { + return + } + for id, pub := range s.peers { + if !yield(id, append([]byte(nil), pub...)) { + return + } + } + } +} +func Lookup(peers Authorizations, id PeerID) []byte { if peers == nil { return nil } - for candidate, pub := range peers { - if candidate == id { - return pub + s := peers() + if s == nil { + return nil + } + return append([]byte(nil), s.peers[id]...) +} + +// EnumeratedAuthorizations adapts dynamic enumerations, primarily for tests. +// Production callers should publish a prebuilt snapshot to keep lookup O(1). +func EnumeratedAuthorizations(peers iter.Seq2[PeerID, []byte]) Authorizations { + return func() *AuthorizationSnapshot { + m := map[PeerID][]byte{} + for id, pub := range peers { + m[id] = pub } + return NewAuthorizations(m) } - return nil } type Keys struct{ WG, Disco [32]byte } diff --git a/internal/transport/renew_test.go b/internal/transport/renew_test.go index c3446b9..9020641 100644 --- a/internal/transport/renew_test.go +++ b/internal/transport/renew_test.go @@ -105,11 +105,11 @@ func TestTCPStreamSurvivesRenewalAndLostAcknowledgement(t *testing.T) { sp, _ := si.PublicKey(ctx) cp, _ := ci.PublicKey(ctx) var revoked, wrongOwner atomic.Bool - server := serverWithLifetime(si, sp, key.NewNode(), func(yield func(protocol.PeerID, []byte) bool) { + server := serverWithLifetime(si, sp, key.NewNode(), protocol.EnumeratedAuthorizations(func(yield func(protocol.PeerID, []byte) bool) { if !revoked.Load() { yield(protocol.ID(cp), cp) } - }, lease) + }), lease) server.Region = &tailcfg.DERPRegion{RegionID: 1, RegionCode: "test", Nodes: []*tailcfg.DERPNode{{ Name: "test", RegionID: 1, HostName: "127.0.0.1", IPv4: "127.0.0.1", IPv6: "none", DERPPort: httpRelay.Listener.Addr().(*net.TCPAddr).Port, STUNPort: stun.Port, STUNTestIP: "127.0.0.1", InsecureForTests: true, diff --git a/internal/transport/transport.go b/internal/transport/transport.go index 42da541..903eacd 100644 --- a/internal/transport/transport.go +++ b/internal/transport/transport.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "errors" - "iter" "time" "github.com/ekarulf/quantumcat/internal/crypto/provider" @@ -24,11 +23,11 @@ func Keys(node key.NodePrivate) protocol.Keys { disco := tailcat.DiscoPublicForNode(node).Raw32() return protocol.Keys{WG: wg, Disco: disco} } -func Server(identity provider.Identity, pub []byte, node key.NodePrivate, authorized iter.Seq2[protocol.PeerID, []byte]) *tailcat.Server { +func Server(identity provider.Identity, pub []byte, node key.NodePrivate, authorized protocol.Authorizations) *tailcat.Server { return serverWithLifetime(identity, pub, node, authorized, SessionLifetime) } -func serverWithLifetime(identity provider.Identity, pub []byte, node key.NodePrivate, authorized iter.Seq2[protocol.PeerID, []byte], lifetime time.Duration) *tailcat.Server { +func serverWithLifetime(identity provider.Identity, pub []byte, node key.NodePrivate, authorized protocol.Authorizations, lifetime time.Duration) *tailcat.Server { p := &protocol.Server{Identity: identity, Public: pub, Keys: Keys(node), Authorized: authorized} return &tailcat.Server{ BootstrapCleanup: func() { p.Expire(time.Now()) }, diff --git a/internal/transport/transport_test.go b/internal/transport/transport_test.go index 38f18fb..6f8e4be 100644 --- a/internal/transport/transport_test.go +++ b/internal/transport/transport_test.go @@ -48,7 +48,7 @@ func TestInstallationFailureNeverAcknowledged(t *testing.T) { ci, _ := software.Generate() sp, _ := si.PublicKey(ctx) cp, _ := ci.PublicKey(ctx) - server := Server(si, sp, key.NewNode(), func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) }) + server := Server(si, sp, key.NewNode(), protocol.EnumeratedAuthorizations(func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) })) server.Region = &tailcfg.DERPRegion{RegionID: 1, RegionCode: "test", Nodes: []*tailcfg.DERPNode{{ Name: "test", RegionID: 1, HostName: "127.0.0.1", IPv4: "127.0.0.1", IPv6: "none", DERPPort: httpRelay.Listener.Addr().(*net.TCPAddr).Port, STUNPort: stun.Port, STUNTestIP: "127.0.0.1", InsecureForTests: true, @@ -102,11 +102,11 @@ func TestTunnelAuthenticationLossAndRevocation(t *testing.T) { sp, _ := si.PublicKey(ctx) cp, _ := ci.PublicKey(ctx) var revoked atomic.Bool - server := Server(si, sp, key.NewNode(), func(yield func(protocol.PeerID, []byte) bool) { + server := Server(si, sp, key.NewNode(), protocol.EnumeratedAuthorizations(func(yield func(protocol.PeerID, []byte) bool) { if !revoked.Load() { yield(protocol.ID(cp), cp) } - }) + })) server.Region = region server.Logf = t.Logf server.OnTCP = func(p uint16) func(net.Conn) { @@ -223,7 +223,7 @@ func TestDisconnectAuthenticatedClient(t *testing.T) { ci, _ := software.Generate() sp, _ := si.PublicKey(ctx) cp, _ := ci.PublicKey(ctx) - server := Server(si, sp, key.NewNode(), func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) }) + server := Server(si, sp, key.NewNode(), protocol.EnumeratedAuthorizations(func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) })) server.Region = &tailcfg.DERPRegion{RegionID: 1, RegionCode: "test", Nodes: []*tailcfg.DERPNode{{ Name: "test", RegionID: 1, HostName: "127.0.0.1", IPv4: "127.0.0.1", IPv6: "none", DERPPort: httpRelay.Listener.Addr().(*net.TCPAddr).Port, STUNPort: stun.Port, STUNTestIP: "127.0.0.1", InsecureForTests: true, @@ -337,7 +337,7 @@ func TestIPModeHostPackets(t *testing.T) { ci, _ := software.Generate() sp, _ := si.PublicKey(ctx) cp, _ := ci.PublicKey(ctx) - server := Server(si, sp, key.NewNode(), func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) }) + server := Server(si, sp, key.NewNode(), protocol.EnumeratedAuthorizations(func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) })) server.Region = &tailcfg.DERPRegion{RegionID: 1, RegionCode: "test", Nodes: []*tailcfg.DERPNode{{Name: "test", RegionID: 1, HostName: "127.0.0.1", IPv4: "127.0.0.1", IPv6: "none", DERPPort: httpRelay.Listener.Addr().(*net.TCPAddr).Port, STUNPort: stun.Port, STUNTestIP: "127.0.0.1", InsecureForTests: true}}} st := newMemoryTUN() server.TUN = st diff --git a/internal/transport/udp_test.go b/internal/transport/udp_test.go index 4d841fa..addbf05 100644 --- a/internal/transport/udp_test.go +++ b/internal/transport/udp_test.go @@ -64,7 +64,7 @@ func TestPublishedUDPForwarding(t *testing.T) { ci, _ := software.Generate() sp, _ := si.PublicKey(ctx) cp, _ := ci.PublicKey(ctx) - server := Server(si, sp, key.NewNode(), func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) }) + server := Server(si, sp, key.NewNode(), protocol.EnumeratedAuthorizations(func(yield func(protocol.PeerID, []byte) bool) { yield(protocol.ID(cp), cp) })) server.Region = &tailcfg.DERPRegion{RegionID: 1, RegionCode: "test", Nodes: []*tailcfg.DERPNode{{Name: "test", RegionID: 1, HostName: "127.0.0.1", IPv4: "127.0.0.1", IPv6: "none", DERPPort: httpRelay.Listener.Addr().(*net.TCPAddr).Port, STUNPort: stun.Port, STUNTestIP: "127.0.0.1", InsecureForTests: true}}} server.Logf = t.Logf server.OnUDP, err = proxy.UDPServices(ctx, map[uint16]string{60001: echo.LocalAddr().String()})