Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions PROTOCOL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 4 additions & 9 deletions cmd/qcat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/qcat/mosh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand Down
4 changes: 2 additions & 2 deletions internal/crypto/provider/se/se_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
50 changes: 44 additions & 6 deletions internal/protocol/blinding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
}
12 changes: 5 additions & 7 deletions internal/protocol/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"crypto/sha512"
"encoding/binary"
"errors"
"iter"
"sync"
"time"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions internal/protocol/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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" {
Expand All @@ -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":
Expand Down
6 changes: 3 additions & 3 deletions internal/protocol/review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 40 additions & 9 deletions internal/protocol/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions internal/transport/renew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions internal/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"errors"
"iter"
"time"

"github.com/ekarulf/quantumcat/internal/crypto/provider"
Expand All @@ -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()) },
Expand Down
10 changes: 5 additions & 5 deletions internal/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/transport/udp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand Down
Loading