From 1c0a496394aa3d3e272881ae0cf807a5109bbba6 Mon Sep 17 00:00:00 2001 From: Teo Calin Date: Sun, 8 Nov 2026 13:15:54 -0800 Subject: [PATCH 1/2] Bind trust only to keys the registry confirmed; no key does not pass a bound record IsTrustedWithKey(id, "") answered by node ID alone, so a caller with no key in scope passed a record bound to a key. It now reports false for a bound record. An unbound record is still trusted by node ID, which is all it grants. Callers that mean the node ID alone have IsTrustedByNodeID (IsTrusted is the same, under the name daemonapi.HandshakeService uses). Automatic trust needed the registry's confirmation on some rules and not others: - Direct requests: mutual, same-network and trusted-agent required registryBound; trust-auto-approve did not, so with the registry lookup failing, any sender could claim any node ID with its own key pair and be trusted under it. It now requires registryBound; an unconfirmed request waits in the pending queue. - Relayed requests: no rule checked a key. Each now needs the registry to list a key for the sender (looked up before the rules run) and binds the record to it. With no key, the request waits for the operator. The allowlist is asked with that key, so a pinned entry can match over the relay, where it was asked with "" and never could. A key a peer merely presents no longer binds or drops a record: - IsTrustedWithKey no longer adopts the first key presented into an unbound record. It starts a registry lookup (backfillPeerKey) instead, at most one per node per 30 s, so a per-packet check cannot flood the registry. - A request or accept whose key the registry did not confirm leaves an existing record alone: an unbound one is bound by a registry lookup; a record bound to another key is kept and the message ignored. Before, a request under another key during a registry outage dropped the record, and one for an unbound record bound it to the sender's key. - A new record from an unconfirmed accept is unbound until the registry binds it. - Pending requests record whether the registry confirmed their key (KeyConfirmed, persisted as key_confirmed). Approving one binds its key only then, and otherwise the registry's key. An unconfirmed request no longer replaces a confirmed pending one under another key. Behaviour changes: a trust-auto-approve or relayed request that the registry cannot confirm (registry down, or a node with no key, of which the live registry lists none) now waits for the operator instead of being trusted. Snapshots stay compatible both ways: key_confirmed is optional, and older pending entries load as unconfirmed, so approving them binds the registry's key. --- handshake.go | 532 ++++++++++++++-------- runtime.go | 8 +- zz_ceiling_branches_test.go | 16 +- zz_findings_hardening_test.go | 20 +- zz_handshake_accept_request_test.go | 31 +- zz_handshake_connection_parse_test.go | 24 +- zz_handshake_registry_integration_test.go | 1 + zz_handshake_relayed_trust_test.go | 25 +- zz_outgoing_lifetime_test.go | 2 +- zz_pending_full_auto_accept_test.go | 9 +- zz_ppa001_accept_revoke_identity_test.go | 12 +- zz_ppa008_replay_key_timestamp_test.go | 7 +- zz_registry_confirmed_trust_test.go | 485 ++++++++++++++++++++ zz_relayed_more_test.go | 3 +- zz_signed_msg_helper_test.go | 30 ++ zz_trust_key_binding_test.go | 52 ++- 16 files changed, 964 insertions(+), 293 deletions(-) create mode 100644 zz_registry_confirmed_trust_test.go create mode 100644 zz_signed_msg_helper_test.go diff --git a/handshake.go b/handshake.go index a5ebf67..19641cb 100644 --- a/handshake.go +++ b/handshake.go @@ -51,10 +51,16 @@ type HandshakeMsg struct { // node ID is unchanged. Checks that have the peer's current key in // scope go through IsTrustedWithKey, which compares the two. // +// A record is only ever bound to a key the registry confirmed for the +// node: the key of a request or accept whose registry lookup matched it, +// or the key a registry lookup returned. A key a peer merely presents +// never binds a record, and never drops one either. +// // An empty PublicKey means "unbound": records restored from a snapshot -// written before keys were recorded, and relay-established records -// whose backfillPeerKey has not landed yet, both look like this. Those -// stay trusted on node ID alone and adopt the first key that turns up. +// written before keys were recorded, records made over the registry +// relay or from a request the registry could not confirm, before their +// registry lookup has landed. Those stay trusted on node ID alone until +// backfillPeerKey binds them to the registry's key. type TrustRecord struct { NodeID uint32 PublicKey string // base64 Ed25519 pubkey @@ -64,9 +70,16 @@ type TrustRecord struct { } // PendingHandshake is an unapproved incoming request. +// +// PublicKey is the key the request was signed with ("" for a request +// that came over the registry relay). KeyConfirmed records whether the +// registry listed that same key for NodeID when the request arrived; +// approving the request binds the trust record to PublicKey only then, +// and otherwise to the key a registry lookup returns. type PendingHandshake struct { NodeID uint32 PublicKey string + KeyConfirmed bool Justification string ReceivedAt time.Time Source uint32 @@ -98,6 +111,10 @@ const ( // operators ample time to decide without letting the queue grow // unboundedly under sustained inbound handshake pressure. pendingHandshakeTTL = 30 * 24 * time.Hour + // bindRetryInterval spaces the registry lookups that bind an unbound + // trust record to its node's key (scheduleBind). Trust checks run per + // packet, so without it every packet from such a peer would start one. + bindRetryInterval = 30 * time.Second // outgoingRequestTTL is how long a request this node sent stays // answerable: the peer keeps it pending for pendingHandshakeTTL, and an // acceptance is honoured only while the matching outgoing entry exists. @@ -153,6 +170,12 @@ type Manager struct { // so that markTrustedLocked can fire notifications while holding hm.mu. trustWaitersMu sync.Mutex trustWaiters map[uint32][]chan struct{} + + // Registry binds of unbound trust records (see scheduleBind): when one + // was last started for each node. bindMu is a leaf lock, taken after + // hm.mu when both are held. + bindMu sync.Mutex + bindTried map[uint32]time.Time } // NewManager constructs a Manager bound to the given Runtime. Loads @@ -169,6 +192,7 @@ func NewManager(rt Runtime) *Manager { replaySet: make(map[[32]byte]replayEntry), replayPeer: make(map[uint32]int), trustWaiters: make(map[uint32][]chan struct{}), + bindTried: make(map[uint32]time.Time), dirty: make(chan struct{}, 1), done: make(chan struct{}), drainDone: make(chan struct{}), @@ -411,6 +435,7 @@ type trustSnapshotEntry struct { type pendingSnapshotEntry struct { NodeID uint32 `json:"node_id"` PublicKey string `json:"public_key,omitempty"` + KeyConfirmed bool `json:"key_confirmed,omitempty"` Justification string `json:"justification,omitempty"` ReceivedAt string `json:"received_at"` Source uint32 `json:"source,omitempty"` @@ -477,6 +502,7 @@ func (hm *Manager) saveTrustLocked() { snap.Pending = append(snap.Pending, pendingSnapshotEntry{ NodeID: p.NodeID, PublicKey: p.PublicKey, + KeyConfirmed: p.KeyConfirmed, Justification: p.Justification, ReceivedAt: p.ReceivedAt.Format(time.RFC3339), Source: p.Source, @@ -586,6 +612,7 @@ func (hm *Manager) loadTrust() { hm.pending[e.NodeID] = &PendingHandshake{ NodeID: e.NodeID, PublicKey: e.PublicKey, + KeyConfirmed: e.KeyConfirmed, Justification: e.Justification, ReceivedAt: received, Source: e.Source, @@ -652,6 +679,7 @@ func (hm *Manager) Start() error { hm.reapReplay() hm.reapOutgoingAndRevoked() hm.reapStalePending() + hm.reapBindAttempts() case <-hm.reapStop: return } @@ -740,8 +768,9 @@ func (hm *Manager) handleConnection(stream coreapi.Stream) { // processMessage handles an incoming handshake message. // // On a successful (claimed pubkey == registry-registered pubkey) match -// we set registryBound = true and pass it to handleRequest. The -// trusted-agents auto-accept path requires that flag — without it, +// we set registryBound = true and pass it to handleRequest and +// handleAccept. Every automatic trust rule, and every binding or +// rebinding of a trust record, requires that flag — without it, // trusting a peer by node_id alone is unsafe (registry down or no // record means we'd be relying on signature verification against a // claimed pubkey that nothing has tied to the claimed node ID). @@ -841,7 +870,7 @@ func (hm *Manager) processMessage(stream coreapi.Stream, msg *HandshakeMsg) { case HandshakeRequest: hm.handleRequest(stream, msg, registryBound) case HandshakeAccept: - hm.handleAccept(msg) + hm.handleAccept(msg, registryBound) case HandshakeReject: hm.handleRejectMsg(msg) case HandshakeRevoke: @@ -1047,9 +1076,10 @@ func sanitizeJustification(s string) string { // // registryBound is true iff processMessage confirmed the claimed // (node_id, public_key) binding via a registry lookup that returned a -// non-empty pubkey matching the claimed one. It gates the trusted- -// agents auto-accept path; signature-only authentication is not -// enough to safely key on node_id. +// non-empty pubkey matching the claimed one. It gates every auto-accept +// rule (mutual, same network, trusted agent, trust-auto-approve) and +// any change to an existing record's binding; signature-only +// authentication is not enough to safely key on node_id. func (hm *Manager) handleRequest(stream coreapi.Stream, msg *HandshakeMsg, registryBound bool) { peerNodeID := msg.NodeID var source uint32 @@ -1072,11 +1102,27 @@ func (hm *Manager) handleRequest(stream coreapi.Stream, msg *HandshakeMsg, regis hm.mu.Lock() defer hm.mu.Unlock() - // Already trusted? Only if the record still binds the key this - // request carries — otherwise it is dropped here and the request - // falls through to the normal approval paths below. - if _, ok := hm.trusted[peerNodeID]; ok { - if hm.reconcileTrustBindingLocked(peerNodeID, msg.PublicKey) { + // Already trusted? A key the registry confirmed settles the binding: + // an unbound record takes it, and a record bound to another key is + // dropped here, so the request falls through to the approval paths + // below. A key the registry did not confirm changes nothing: it is + // answered only if the record is unbound or bound to that same key. + if rec, ok := hm.trusted[peerNodeID]; ok { + if registryBound { + if hm.reconcileTrustBindingLocked(peerNodeID, msg.PublicKey) { + slog.Debug("node already trusted", "peer_node_id", peerNodeID) + hm.sendAcceptLocked(peerNodeID) + return + } + } else { + switch { + case rec.PublicKey == "": + hm.scheduleBindLocked(peerNodeID) + case subtle.ConstantTimeCompare([]byte(rec.PublicKey), []byte(msg.PublicKey)) != 1: + slog.Warn("handshake request from a trusted node ID under another key the registry could not confirm, ignoring", + "peer_node_id", peerNodeID) + return + } slog.Debug("node already trusted", "peer_node_id", peerNodeID) hm.sendAcceptLocked(peerNodeID) return @@ -1115,7 +1161,7 @@ func (hm *Manager) handleRequest(stream coreapi.Stream, msg *HandshakeMsg, regis // Check if peers are on the same network (network trust) // SEC-038: same-network auto-trust is gated on registryBound so a // peer cannot claim any NodeID with their own keypair and slip into trust. - if hm.sameNetwork(peerNodeID) && registryBound { + if registryBound && hm.sameNetwork(peerNodeID) { hm.markTrustedLocked(peerNodeID, &TrustRecord{ NodeID: peerNodeID, PublicKey: msg.PublicKey, @@ -1174,8 +1220,13 @@ func (hm *Manager) handleRequest(stream coreapi.Stream, msg *HandshakeMsg, regis } } - // Auto-approve all requests when the daemon is configured to do so. - if hm.rt.TrustAutoApprove() { + // Auto-approve all requests when the daemon is configured to do so, + // as long as the registry confirmed the requester's key, like every + // rule above. Without that, any sender could claim any node ID with + // its own key pair and be trusted under it. An unconfirmed request + // waits in the pending queue instead, for the operator or for a later + // request the registry does confirm. + if registryBound && hm.rt.TrustAutoApprove() { hm.markTrustedLocked(peerNodeID, &TrustRecord{ NodeID: peerNodeID, PublicKey: msg.PublicKey, @@ -1198,7 +1249,16 @@ func (hm *Manager) handleRequest(stream coreapi.Stream, msg *HandshakeMsg, regis return } - // Store as pending (cap to prevent unbounded growth from spam) + // Store as pending (cap to prevent unbounded growth from spam). A + // request the registry could not confirm does not replace one it did + // confirm under another key, so it cannot swap in its own key and + // justification for the operator to approve. + if p, exists := hm.pending[peerNodeID]; exists && p.KeyConfirmed && !registryBound && + subtle.ConstantTimeCompare([]byte(p.PublicKey), []byte(msg.PublicKey)) != 1 { + slog.Warn("unconfirmed handshake request under another key, keeping the confirmed pending one", + "peer_node_id", peerNodeID) + return + } if _, exists := hm.pending[peerNodeID]; !exists { if len(hm.pending) >= maxPendingHandshakes { slog.Warn("pending handshake queue full, rejecting", "peer_node_id", peerNodeID) @@ -1212,6 +1272,7 @@ func (hm *Manager) handleRequest(stream coreapi.Stream, msg *HandshakeMsg, regis hm.setPendingLocked(peerNodeID, &PendingHandshake{ NodeID: peerNodeID, PublicKey: msg.PublicKey, + KeyConfirmed: registryBound, Justification: justification, ReceivedAt: time.Now(), Source: source, @@ -1229,7 +1290,13 @@ func (hm *Manager) handleRequest(stream coreapi.Stream, msg *HandshakeMsg, regis // matched against, and consumes, the hm.outgoing entry created by // SendRequest. Accepts with no matching entry are dropped, mirroring // processRelayedApproval's precondition on the relay path. -func (hm *Manager) handleAccept(msg *HandshakeMsg) { +// +// registryBound is processMessage's verdict: the registry lists +// msg.PublicKey for the sender. Only then does the record take that key +// or drop a binding to another one. Otherwise the new record is left +// unbound and bound by a registry lookup (scheduleBindLocked), and an +// existing record bound to another key is kept and the accept ignored. +func (hm *Manager) handleAccept(msg *HandshakeMsg, registryBound bool) { peerNodeID := msg.NodeID slog.Info("handshake accepted by peer", "peer_node_id", peerNodeID) @@ -1253,30 +1320,48 @@ func (hm *Manager) handleAccept(msg *HandshakeMsg) { return } + // The key the new record is bound to: the accept's own, only when the + // registry confirmed it. + key := "" + if registryBound { + key = msg.PublicKey + } + // An accept that carries a different key than the record we hold - // comes from a different key holder behind the same node ID. Drop - // the record instead of rebinding it — re-trusting is an explicit - // handshake, not a side effect of an inbound accept. + // comes from a different key holder behind the same node ID. With + // the registry's confirmation, drop the record instead of rebinding + // it — re-trusting is an explicit handshake, not a side effect of an + // inbound accept. Without it, the accept cannot undo the binding, so + // it is ignored. if existing, ok := hm.trusted[peerNodeID]; ok { bound := existing.PublicKey - if !hm.reconcileTrustBindingLocked(peerNodeID, msg.PublicKey) { - hm.dropOutgoingLocked(peerNodeID) + if registryBound { + if !hm.reconcileTrustBindingLocked(peerNodeID, msg.PublicKey) { + hm.dropOutgoingLocked(peerNodeID) + return + } + } else if bound != "" && subtle.ConstantTimeCompare([]byte(bound), []byte(msg.PublicKey)) != 1 { + slog.Warn("ignoring handshake acceptance under another key the registry could not confirm", "peer_node_id", peerNodeID) return } - // Preserve an already-bound key when the accept omits one. - if msg.PublicKey == "" { - msg.PublicKey = bound + // Preserve an already-bound key when the accept brings none the + // registry confirmed. + if key == "" { + key = bound } } hm.dropOutgoingLocked(peerNodeID) hm.markTrustedLocked(peerNodeID, &TrustRecord{ NodeID: peerNodeID, - PublicKey: msg.PublicKey, + PublicKey: key, ApprovedAt: time.Now(), Mutual: true, }) hm.markDirty() + if key == "" { + hm.scheduleBindLocked(peerNodeID) + } // Report trust to registry if reg := hm.rt.Registry(); reg != nil { @@ -1361,9 +1446,20 @@ func (hm *Manager) ProcessRelayedRequest(fromNodeID uint32, justification string } // processRelayedRequest handles a handshake request received via registry relay. +// +// The relay vouches for the sender's node ID (only authenticated nodes +// can relay) but carries no key. Every rule that grants trust on its own +// (mutual, same network, trusted agent, trust-auto-approve) needs the +// registry to confirm the requester's key first, as on the direct path, +// so the key is looked up before anything else and the record is bound +// to it. When the registry gives no key, nothing is granted +// automatically and the request waits for the operator. func (hm *Manager) processRelayedRequest(fromNodeID uint32, justification string) { justification = sanitizeJustification(justification) + // A registry RPC, so before taking hm.mu. "" means not confirmed. + regKey, _ := hm.registryKey(fromNodeID) + // As in handleRequest: the pending caps are enforced where the request is // queued, below, so a full queue does not stop a peer the auto-accept // rules would take without queueing it. @@ -1371,118 +1467,35 @@ func (hm *Manager) processRelayedRequest(fromNodeID uint32, justification string hm.mu.Lock() defer hm.mu.Unlock() - // Already trusted? - if _, ok := hm.trusted[fromNodeID]; ok { - slog.Debug("relayed request from already-trusted node", "peer_node_id", fromNodeID) - // Respond via registry that we accept - if reg := hm.rt.Registry(); reg != nil { - nodeID, peerID := hm.rt.NodeID(), fromNodeID - sig := hm.signHandshakeChallenge(fmt.Sprintf("respond:%d:%d", nodeID, peerID)) - hm.goRPCLocked(func() { reg.RespondHandshake(nodeID, peerID, true, sig) }) - } - return - } - - // Check if we have an outgoing request to this peer (mutual handshake) - if _, ok := hm.outgoing[fromNodeID]; ok { - hm.dropOutgoingLocked(fromNodeID) - hm.markTrustedLocked(fromNodeID, &TrustRecord{ - NodeID: fromNodeID, - ApprovedAt: time.Now(), - Mutual: true, - }) - slog.Info("mutual relayed handshake auto-approved", "peer_node_id", fromNodeID) - hm.markDirty() - // Respond via registry and backfill public key - if reg := hm.rt.Registry(); reg != nil { - nodeID, peerID := hm.rt.NodeID(), fromNodeID - sig := hm.signHandshakeChallenge(fmt.Sprintf("respond:%d:%d", nodeID, peerID)) - hm.goRPCLocked(func() { - reg.RespondHandshake(nodeID, peerID, true, sig) - hm.backfillPeerKey(peerID) - }) - } - return - } - - // Check if peers are on the same network (network trust) - if hm.sameNetwork(fromNodeID) { - hm.markTrustedLocked(fromNodeID, &TrustRecord{ - NodeID: fromNodeID, - ApprovedAt: time.Now(), - Network: hm.sharedNetwork(fromNodeID), - }) - slog.Info("same network relayed handshake auto-approved", "peer_node_id", fromNodeID) - hm.markDirty() - if reg := hm.rt.Registry(); reg != nil { - nodeID, peerID := hm.rt.NodeID(), fromNodeID - sig := hm.signHandshakeChallenge(fmt.Sprintf("respond:%d:%d", nodeID, peerID)) - hm.goRPCLocked(func() { - reg.RespondHandshake(nodeID, peerID, true, sig) - reg.ReportTrust(nodeID, peerID) - hm.backfillPeerKey(peerID) - }) - } - return - } - - // Auto-approve when the sender is in the embedded trusted-agents list. - // The registry relay vouches for the sender's node_id (only authenticated - // nodes can relay), so no separate registryBound check is needed here. - // - // The relay carries no peer public key, so the empty key is passed: - // allowlist entries that carry a pin do not match on this path. - if name, ok := hm.trustedAgentName(fromNodeID, ""); ok { - hm.markTrustedLocked(fromNodeID, &TrustRecord{ - NodeID: fromNodeID, - ApprovedAt: time.Now(), - }) - slog.Info("relayed handshake auto-approved (trusted agent)", - "peer_node_id", fromNodeID, "agent", name) - hm.rt.PublishEvent("handshake.auto_approved", map[string]interface{}{ - "peer_node_id": fromNodeID, "reason": "trusted_agent", "agent": name, - }) - hm.rt.PublishEvent("trust.changed", map[string]interface{}{ - "peer_node_id": fromNodeID, "state": "granted", "reason": "trusted_agent_relayed", "agent": name, - }) - hm.markDirty() - if reg := hm.rt.Registry(); reg != nil { - nodeID, peerID := hm.rt.NodeID(), fromNodeID - sig := hm.signHandshakeChallenge(fmt.Sprintf("respond:%d:%d", nodeID, peerID)) - hm.goRPCLocked(func() { - reg.RespondHandshake(nodeID, peerID, true, sig) - reg.ReportTrust(nodeID, peerID) - hm.backfillPeerKey(peerID) - }) + // Already trusted? The registry's key settles the binding as on the + // direct path: an unbound record takes it, and a record bound to + // another key is dropped and the request handled as new below. + if rec, ok := hm.trusted[fromNodeID]; ok { + stands := true + if regKey != "" { + stands = hm.reconcileTrustBindingLocked(fromNodeID, regKey) + } else if rec.PublicKey == "" { + hm.scheduleBindLocked(fromNodeID) + } + if stands { + slog.Debug("relayed request from already-trusted node", "peer_node_id", fromNodeID) + // Respond via registry that we accept + if reg := hm.rt.Registry(); reg != nil { + nodeID, peerID := hm.rt.NodeID(), fromNodeID + sig := hm.signHandshakeChallenge(fmt.Sprintf("respond:%d:%d", nodeID, peerID)) + hm.goRPCLocked(func() { reg.RespondHandshake(nodeID, peerID, true, sig) }) + } + return } - return } - // Auto-approve all requests when the daemon is configured to do so. - if hm.rt.TrustAutoApprove() { - hm.markTrustedLocked(fromNodeID, &TrustRecord{ - NodeID: fromNodeID, - ApprovedAt: time.Now(), - Mutual: false, - }) - slog.Info("relayed handshake auto-approved (trust-auto-approve enabled)", "peer_node_id", fromNodeID) - hm.rt.PublishEvent("handshake.auto_approved", map[string]interface{}{ - "peer_node_id": fromNodeID, "reason": "auto_approve", - }) - hm.rt.PublishEvent("trust.changed", map[string]interface{}{ - "peer_node_id": fromNodeID, "state": "granted", "reason": "auto_approve_relayed", - }) - hm.markDirty() - if reg := hm.rt.Registry(); reg != nil { - nodeID, peerID := hm.rt.NodeID(), fromNodeID - sig := hm.signHandshakeChallenge(fmt.Sprintf("respond:%d:%d", nodeID, peerID)) - hm.goRPCLocked(func() { - reg.RespondHandshake(nodeID, peerID, true, sig) - reg.ReportTrust(nodeID, peerID) - hm.backfillPeerKey(peerID) - }) + if regKey != "" { + if hm.autoApproveRelayedLocked(fromNodeID, regKey) { + return } - return + } else { + slog.Info("relayed handshake request: the registry did not confirm the sender's key, so no rule approves it automatically", + "peer_node_id", fromNodeID) } // Store as pending (for manual approval via pilotctl approve) @@ -1506,6 +1519,82 @@ func (hm *Manager) processRelayedRequest(fromNodeID uint32, justification string slog.Info("relayed handshake request pending approval", "from_node_id", fromNodeID, "justification", justification) } +// autoApproveRelayedLocked applies the automatic trust rules to a relayed +// request whose sender the registry lists with regKey, and reports +// whether one of them granted trust. The record is bound to regKey. +// Caller MUST hold hm.mu. +func (hm *Manager) autoApproveRelayedLocked(fromNodeID uint32, regKey string) bool { + rec := &TrustRecord{NodeID: fromNodeID, PublicKey: regKey, ApprovedAt: time.Now()} + var reason, agent string + switch { + case hm.hasOutgoingLocked(fromNodeID): + // We asked them too: a mutual handshake. + rec.Mutual = true + reason = "mutual" + case hm.sameNetwork(fromNodeID): + rec.Network = hm.sharedNetwork(fromNodeID) + reason = "same_network" + default: + // The registry's key is the key the sender authenticated to the + // relay with, so an allowlist pin is checked against it. + if name, ok := hm.trustedAgentName(fromNodeID, regKey); ok { + reason, agent = "trusted_agent", name + } else if hm.rt.TrustAutoApprove() { + reason = "auto_approve" + } else { + return false + } + } + + hm.dropOutgoingLocked(fromNodeID) + hm.markTrustedLocked(fromNodeID, rec) + hm.markDirty() + switch reason { + case "mutual": + slog.Info("mutual relayed handshake auto-approved", "peer_node_id", fromNodeID) + case "same_network": + slog.Info("same network relayed handshake auto-approved", "peer_node_id", fromNodeID) + case "trusted_agent": + slog.Info("relayed handshake auto-approved (trusted agent)", "peer_node_id", fromNodeID, "agent", agent) + hm.rt.PublishEvent("handshake.auto_approved", map[string]interface{}{ + "peer_node_id": fromNodeID, "reason": "trusted_agent", "agent": agent, + }) + hm.rt.PublishEvent("trust.changed", map[string]interface{}{ + "peer_node_id": fromNodeID, "state": "granted", "reason": "trusted_agent_relayed", "agent": agent, + }) + case "auto_approve": + slog.Info("relayed handshake auto-approved (trust-auto-approve enabled)", "peer_node_id", fromNodeID) + hm.rt.PublishEvent("handshake.auto_approved", map[string]interface{}{ + "peer_node_id": fromNodeID, "reason": "auto_approve", + }) + hm.rt.PublishEvent("trust.changed", map[string]interface{}{ + "peer_node_id": fromNodeID, "state": "granted", "reason": "auto_approve_relayed", + }) + } + + if reg := hm.rt.Registry(); reg != nil { + nodeID, peerID := hm.rt.NodeID(), fromNodeID + sig := hm.signHandshakeChallenge(fmt.Sprintf("respond:%d:%d", nodeID, peerID)) + mutual := rec.Mutual + hm.goRPCLocked(func() { + reg.RespondHandshake(nodeID, peerID, true, sig) + // The relayed mutual path has never reported the pair; + // kept as it was. + if !mutual { + reg.ReportTrust(nodeID, peerID) + } + }) + } + return true +} + +// hasOutgoingLocked reports whether this node has an unanswered request +// out to nodeID. Caller MUST hold hm.mu. +func (hm *Manager) hasOutgoingLocked(nodeID uint32) bool { + _, ok := hm.outgoing[nodeID] + return ok +} + // ProcessRelayedApproval handles a handshake approval received via registry relay. // Exported for daemon-side polling glue. func (hm *Manager) ProcessRelayedApproval(fromNodeID uint32) { @@ -1566,16 +1655,11 @@ func (hm *Manager) processRelayedApproval(fromNodeID uint32) { // other key is dropped, since the node ID now resolves to a different // identity than the one trust was granted to. func (hm *Manager) backfillPeerKey(peerNodeID uint32) { - reg := hm.rt.Registry() - if reg == nil { - return - } - resp, err := reg.Lookup(peerNodeID) + pubKeyB64, err := hm.registryKey(peerNodeID) if err != nil { slog.Debug("backfill peer key lookup failed", "peer_node_id", peerNodeID, "error", err) return } - pubKeyB64, _ := resp["public_key"].(string) if pubKeyB64 == "" { return } @@ -1607,6 +1691,71 @@ func (hm *Manager) backfillPeerKey(peerNodeID uint32) { }) } +// registryKey returns the base64 Ed25519 key the registry lists for +// nodeID: "" with a nil error when the registry has no key for it (or no +// registry is wired), and the lookup's error when it failed. It is an +// RPC, so the caller MUST NOT hold hm.mu. +func (hm *Manager) registryKey(nodeID uint32) (string, error) { + reg := hm.rt.Registry() + if reg == nil { + return "", nil + } + resp, err := reg.Lookup(nodeID) + if err != nil { + return "", err + } + key, _ := resp["public_key"].(string) + return key, nil +} + +// scheduleBind starts a registry lookup that binds nodeID's unbound +// trust record to the key the registry lists (backfillPeerKey), unless +// one was started for it within bindRetryInterval. This is how unbound +// records get their key now that a key a caller presents is no longer +// adopted. Caller MUST NOT hold hm.mu. +func (hm *Manager) scheduleBind(nodeID uint32) { + if hm.claimBind(nodeID) { + hm.goRPC(func() { hm.backfillPeerKey(nodeID) }) + } +} + +// scheduleBindLocked is scheduleBind for a caller holding hm.mu +// write-locked. +func (hm *Manager) scheduleBindLocked(nodeID uint32) { + if hm.claimBind(nodeID) { + hm.goRPCLocked(func() { hm.backfillPeerKey(nodeID) }) + } +} + +// claimBind reports whether a registry bind may start for nodeID now, +// and if so records the attempt. +func (hm *Manager) claimBind(nodeID uint32) bool { + if hm.rt.Registry() == nil { + return false + } + now := time.Now() + hm.bindMu.Lock() + defer hm.bindMu.Unlock() + if last, ok := hm.bindTried[nodeID]; ok && now.Sub(last) < bindRetryInterval { + return false + } + hm.bindTried[nodeID] = now + return true +} + +// reapBindAttempts forgets bind attempts older than bindRetryInterval, +// which no longer hold anything back. +func (hm *Manager) reapBindAttempts() { + cutoff := time.Now().Add(-bindRetryInterval) + hm.bindMu.Lock() + defer hm.bindMu.Unlock() + for id, at := range hm.bindTried { + if at.Before(cutoff) { + delete(hm.bindTried, id) + } + } +} + // ProcessRelayedRejection handles a handshake rejection received via registry relay. // Exported for daemon-side polling glue. func (hm *Manager) ProcessRelayedRejection(fromNodeID uint32) { @@ -1636,9 +1785,17 @@ func (hm *Manager) ApproveHandshake(peerNodeID uint32) error { hm.mu.Unlock() return nil } + // The request's key binds the record only if the registry confirmed + // it when the request came in. Otherwise (a relayed request, which + // has no key, or one the registry could not confirm) the record is + // bound to the registry's key by backfillPeerKey below. + key := "" + if req.KeyConfirmed { + key = req.PublicKey + } hm.markTrustedLocked(peerNodeID, &TrustRecord{ NodeID: peerNodeID, - PublicKey: req.PublicKey, + PublicKey: key, ApprovedAt: time.Now(), }) hm.markDirty() @@ -1666,13 +1823,13 @@ func (hm *Manager) ApproveHandshake(peerNodeID uint32) error { } nodeID := hm.rt.NodeID() sig := hm.signHandshakeChallenge(fmt.Sprintf("respond:%d:%d", nodeID, peerNodeID)) - relayedRequest := req.PublicKey == "" + unbound := key == "" hm.goRPC(func() { if err := hm.retryRPC(func() error { _, err := reg.ReportTrust(nodeID, peerNodeID); return err }); err != nil { slog.Warn("handshake approved, but the registry did not take the trust pair", "peer_node_id", peerNodeID, "error", err) } - if relayedRequest { - hm.backfillPeerKey(peerNodeID) // a relayed request carries no key: take the registry's + if unbound { + hm.backfillPeerKey(peerNodeID) // no confirmed key on the request: take the registry's } relayErr := hm.retryRPC(func() error { _, err := reg.RespondHandshake(nodeID, peerNodeID, true, sig); return err }) if err := hm.sendAccept(peerNodeID); err != nil && relayErr != nil { @@ -1845,13 +2002,22 @@ func (hm *Manager) trustedAgentName(peerNodeID uint32, pubKeyB64 string) (string return hm.rt.IsTrusted(peerNodeID) } -// IsTrusted returns whether a peer has been approved. -// -// This answers the node-ID-only question, for call sites that have no -// peer key in scope (outbound dials, for instance). Callers that do -// know the key the peer is currently presenting should use -// IsTrustedWithKey instead so the record's binding is enforced. +// IsTrusted returns whether a peer has been approved, by node ID alone. +// It is IsTrustedByNodeID under the name the daemon's HandshakeService +// interface (common/daemonapi) uses. func (hm *Manager) IsTrusted(nodeID uint32) bool { + return hm.IsTrustedByNodeID(nodeID) +} + +// IsTrustedByNodeID reports whether a trust record exists for nodeID, +// whatever key it is bound to. It answers for the node ID, not for +// whoever presents it: use it only where no key the peer proved is in +// scope and the node ID is all the question is about (an outbound dial, +// a list of trusted peers, a gate before a key exchange that is itself +// checked against the registry). Where the caller knows the key the +// peer authenticated with, IsTrustedWithKey checks the record's binding. +// It has no side effects. +func (hm *Manager) IsTrustedByNodeID(nodeID uint32) bool { hm.mu.RLock() defer hm.mu.RUnlock() _, ok := hm.trusted[nodeID] @@ -1860,22 +2026,25 @@ func (hm *Manager) IsTrusted(nodeID uint32) bool { // IsTrustedWithKey returns whether a peer has been approved, given the // base64 Ed25519 key that peer currently presents (same encoding as -// TrustRecord.PublicKey — crypto.EncodePublicKey). -// -// A record bound to a different key does not describe the peer behind -// this node ID any more, so it is dropped and the answer is false; the -// peer has to handshake again under its new key. -// -// Both unknowns degrade to IsTrusted rather than to a denial, so -// upgrades and older peers keep their trust: +// TrustRecord.PublicKey — crypto.EncodePublicKey). Callers pass a key +// the peer proved, such as the key that authenticated its tunnel +// session. // -// - currentKeyB64 empty — no key resolved at the call site. -// - record unbound — pre-binding snapshot or a not-yet-backfilled -// relay record. It adopts currentKeyB64 here, so the binding takes -// effect from the first check that resolves a key. +// - Bound record, same key: trusted. +// - Bound record, different key: the record does not describe the +// peer behind this node ID any more, so it is dropped and the answer +// is false; the peer has to handshake again under its new key. +// - Bound record, empty key: false. With no key to check the binding +// against, the record cannot vouch for whoever is asking; callers +// that mean the node ID alone ask IsTrustedByNodeID. +// - Unbound record (a pre-binding snapshot, or a record made over the +// relay or from a request the registry could not confirm): trusted +// by node ID, which is all such a record grants. The presented key +// is not adopted: a registry lookup binds the record instead +// (scheduleBind), so the first key to turn up cannot claim it. // // The common case (bound key, matching) is served entirely under the -// read lock; only adopting and dropping take the write lock. +// read lock; only dropping takes the write lock. func (hm *Manager) IsTrustedWithKey(nodeID uint32, currentKeyB64 string) bool { hm.mu.RLock() rec, ok := hm.trusted[nodeID] @@ -1888,13 +2057,13 @@ func (hm *Manager) IsTrustedWithKey(nodeID uint32, currentKeyB64 string) bool { if !ok { return false } - if currentKeyB64 == "" { - return true - } if bound == "" { - hm.adoptPeerKey(nodeID, currentKeyB64) + hm.scheduleBind(nodeID) return true } + if currentKeyB64 == "" { + return false + } if subtle.ConstantTimeCompare([]byte(bound), []byte(currentKeyB64)) == 1 { return true } @@ -1902,21 +2071,6 @@ func (hm *Manager) IsTrustedWithKey(nodeID uint32, currentKeyB64 string) bool { return false } -// adoptPeerKey binds keyB64 to an existing, still-unbound trust record. -// No-op when the record has since gone away or been bound elsewhere. -func (hm *Manager) adoptPeerKey(nodeID uint32, keyB64 string) { - hm.mu.Lock() - rec, ok := hm.trusted[nodeID] - if !ok || rec.PublicKey != "" { - hm.mu.Unlock() - return - } - rec.PublicKey = keyB64 - hm.markDirty() - hm.mu.Unlock() - slog.Debug("bound peer public key to trust record", "peer_node_id", nodeID) -} - // dropRebound removes a trust record whose bound key no longer matches // the key the peer presents. staleKey is re-checked under the write lock // so a record re-established in the meantime is left alone. @@ -1938,10 +2092,12 @@ func (hm *Manager) dropRebound(nodeID uint32, staleKey string) { } // reconcileTrustBindingLocked resolves an existing trust record against -// the key a peer is presenting on an inbound handshake message, and -// reports whether the record still stands. Same rules as -// IsTrustedWithKey; separate because these call sites already hold -// hm.mu write-locked. Caller MUST hold hm.mu. +// a key the registry confirmed for nodeID, and reports whether the +// record still stands: an unbound record is bound to the key, a record +// bound to it stands, and a record bound to another key is dropped. +// Callers MUST pass only a key the registry confirmed (registryBound, or +// a registry lookup's answer); a key that is merely presented neither +// binds nor drops a record. Caller MUST hold hm.mu. func (hm *Manager) reconcileTrustBindingLocked(nodeID uint32, currentKeyB64 string) bool { rec, ok := hm.trusted[nodeID] if !ok { diff --git a/runtime.go b/runtime.go index 9a07709..471f78a 100644 --- a/runtime.go +++ b/runtime.go @@ -94,9 +94,11 @@ type Runtime interface { // for it and falls back to IsTrusted when the adapter does not provide // it, so existing Runtime implementations keep compiling unchanged. // -// pubKeyB64 is the base64 Ed25519 key the peer proved possession of on -// this handshake, or "" when no authenticated key is in scope (the -// registry-relayed path). Implementations that carry a pinned key for +// pubKeyB64 is the base64 Ed25519 key the registry confirmed for the +// peer: the key it signed a direct request with, or, for a request that +// came over the registry relay, the key a registry lookup returned. It +// is never empty: without a confirmed key no auto-accept rule runs. +// Implementations that carry a pinned key for // the node MUST require an exact match and MUST treat "" as a miss; // entries with no pin answer the node-ID-only question, matching // IsTrusted. diff --git a/zz_ceiling_branches_test.go b/zz_ceiling_branches_test.go index e0881d3..2ac7439 100644 --- a/zz_ceiling_branches_test.go +++ b/zz_ceiling_branches_test.go @@ -143,7 +143,7 @@ func TestProcessRelayedRequest_AutoApproveWithRegistryFiresRespond(t *testing.T) rt.mu.Unlock() rt.registry.setLookup(1, map[string]interface{}{"networks": []interface{}{}}) - rt.registry.setLookup(5555, map[string]interface{}{"networks": []interface{}{}}) + rt.registry.setLookup(5555, map[string]interface{}{"networks": []interface{}{}, "public_key": bindKeyA}) hm.processRelayedRequest(5555, "") @@ -173,7 +173,7 @@ func TestProcessRelayedRequest_MutualWithRegistryFiresRespond(t *testing.T) { rt.mu.Unlock() hm.outgoing[6666] = time.Now() - rt.registry.setLookup(6666, map[string]interface{}{"networks": []interface{}{}}) + rt.registry.setLookup(6666, map[string]interface{}{"networks": []interface{}{}, "public_key": bindKeyA}) hm.processRelayedRequest(6666, "") @@ -276,7 +276,7 @@ func TestHandleAccept_RecentlyRevokedDrops(t *testing.T) { Type: HandshakeAccept, NodeID: 60, Timestamp: time.Now().Unix(), - }) + }, true) hm.mu.RLock() _, trusted := hm.trusted[60] @@ -310,7 +310,7 @@ func TestHandleAccept_ExpiredRevokeAllowsTrustAndFiresReport(t *testing.T) { NodeID: 70, PublicKey: "pk70", Timestamp: time.Now().Unix(), - }) + }, true) hm.mu.RLock() _, trusted := hm.trusted[70] @@ -549,12 +549,8 @@ func TestProcessMessage_ReplaySetFullStillDispatches(t *testing.T) { } hm.replayMu.Unlock() - msg := &HandshakeMsg{ - Type: HandshakeReject, - NodeID: 77, - Reason: "test", - Timestamp: time.Now().Unix(), - } + msg := signedMsg(deterministicIdentity(77), HandshakeReject, 77, hm.rt.NodeID()) + msg.Reason = "test" // Seed an outgoing entry so the dispatch is observable. hm.outgoing[77] = time.Now() hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 77}}, msg) diff --git a/zz_findings_hardening_test.go b/zz_findings_hardening_test.go index 38d6d81..57ce53b 100644 --- a/zz_findings_hardening_test.go +++ b/zz_findings_hardening_test.go @@ -25,7 +25,7 @@ func TestHandleAccept_WithoutOutgoingRequestDropped(t *testing.T) { NodeID: 4242, PublicKey: "pk4242", Timestamp: time.Now().Unix(), - }) + }, true) hm.mu.RLock() _, trusted := hm.trusted[4242] @@ -74,7 +74,7 @@ func TestHandleAccept_ConsumesOutgoingEntryOnce(t *testing.T) { PublicKey: "pk4444", Timestamp: time.Now().Unix(), } - hm.handleAccept(msg) + hm.handleAccept(msg, true) hm.mu.RLock() _, trusted := hm.trusted[peer] @@ -91,7 +91,7 @@ func TestHandleAccept_ConsumesOutgoingEntryOnce(t *testing.T) { hm.mu.Lock() delete(hm.trusted, peer) hm.mu.Unlock() - hm.handleAccept(msg) + hm.handleAccept(msg, true) hm.mu.RLock() _, reTrusted := hm.trusted[peer] @@ -165,11 +165,8 @@ func TestProcessMessage_RejectOverMatchingStreamCancels(t *testing.T) { hm.outgoing[peer] = time.Now() hm.mu.Unlock() - hm.processMessage(&addrStream{addr: coreapi.Addr{Node: peer}}, &HandshakeMsg{ - Type: HandshakeReject, - NodeID: peer, - Timestamp: time.Now().Unix(), - }) + hm.processMessage(&addrStream{addr: coreapi.Addr{Node: peer}}, + signedMsg(deterministicIdentity(72), HandshakeReject, peer, hm.rt.NodeID())) hm.mu.RLock() _, stillOutgoing := hm.outgoing[peer] @@ -212,11 +209,8 @@ func TestProcessMessage_UnverifiedMessageDoesNotConsumeReplayCapacity(t *testing hm.mu.Lock() hm.outgoing[901] = time.Now() hm.mu.Unlock() - hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 901}}, &HandshakeMsg{ - Type: HandshakeReject, - NodeID: 901, - Timestamp: time.Now().Unix(), - }) + hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 901}}, + signedMsg(deterministicIdentity(91), HandshakeReject, 901, hm.rt.NodeID())) hm.replayMu.Lock() size = len(hm.replaySet) diff --git a/zz_handshake_accept_request_test.go b/zz_handshake_accept_request_test.go index f7937ed..128fc4b 100644 --- a/zz_handshake_accept_request_test.go +++ b/zz_handshake_accept_request_test.go @@ -34,7 +34,7 @@ func TestHandleAcceptAddsPeerToTrustedAndRemovesOutgoing(t *testing.T) { PublicKey: "some-pubkey", Timestamp: time.Now().Unix(), } - hm.handleAccept(msg) + hm.handleAccept(msg, true) if _, ok := hm.outgoing[77]; ok { t.Fatal("outgoing[77] should have been cleared after accept") @@ -93,8 +93,9 @@ func TestHandleRequestAlreadyTrustedSendsAcceptWithoutDuplicating(t *testing.T) } // A request for an already-trusted node ID that carries a different key -// is a different key holder behind a recycled node ID. The record is -// dropped and the request falls through to the normal approval flow. +// the registry confirmed is a different key holder behind a recycled +// node ID. The record is dropped and the request falls through to the +// normal approval flow. func TestHandleRequestTrustedNodeWithDifferentKeyDropsRecord(t *testing.T) { t.Parallel() hm, _ := hsTestManager(t, false) @@ -112,7 +113,7 @@ func TestHandleRequestTrustedNodeWithDifferentKeyDropsRecord(t *testing.T) { PublicKey: "reclaimed-key", Timestamp: time.Now().Unix(), } - hm.handleRequest(nil, msg, false) + hm.handleRequest(nil, msg, true) // the registry lists reclaimed-key for 42 hm.mu.RLock() _, stillTrusted := hm.trusted[42] @@ -128,9 +129,9 @@ func TestHandleRequestTrustedNodeWithDifferentKeyDropsRecord(t *testing.T) { } // A record persisted before keys were bound (or one whose registry -// backfill never landed) keeps working, and adopts the first key it -// sees. -func TestHandleRequestUnboundLegacyRecordAdoptsKey(t *testing.T) { +// backfill never landed) keeps working, and takes the key of a request +// the registry confirmed. +func TestHandleRequestUnboundLegacyRecordAdoptsConfirmedKey(t *testing.T) { t.Parallel() hm, _ := hsTestManager(t, false) defer waitForGoRPCDrain() @@ -146,7 +147,7 @@ func TestHandleRequestUnboundLegacyRecordAdoptsKey(t *testing.T) { PublicKey: "first-seen-key", Timestamp: time.Now().Unix(), } - hm.handleRequest(nil, msg, false) + hm.handleRequest(nil, msg, true) hm.mu.RLock() rec := hm.trusted[42] @@ -208,7 +209,7 @@ func TestHandleRequestAutoApproveWhenTrustAutoApproveEnabled(t *testing.T) { PublicKey: "key-55", Timestamp: time.Now().Unix(), } - hm.handleRequest(nil, msg, false) + hm.handleRequest(nil, msg, true) // the registry confirmed key-55 hm.mu.RLock() rec, ok := hm.trusted[55] @@ -371,18 +372,14 @@ func TestProcessMessageDispatchesHandshakeAccept(t *testing.T) { hm := newTestHM(t, "") t.Cleanup(hm.Stop) - // Omit PublicKey so processMessage skips the signature-required check - // and dispatches through the Type switch to handleAccept. The accept - // completes an outgoing request, so one has to be in flight. + // A signed accept dispatches through the Type switch to + // handleAccept. The accept completes an outgoing request, so one has + // to be in flight. hm.mu.Lock() hm.outgoing[44] = time.Now() hm.mu.Unlock() - msg := &HandshakeMsg{ - Type: HandshakeAccept, - NodeID: 44, - Timestamp: time.Now().Unix(), - } + msg := signedMsg(deterministicIdentity(44), HandshakeAccept, 44, hm.rt.NodeID()) hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 44}}, msg) hm.mu.RLock() diff --git a/zz_handshake_connection_parse_test.go b/zz_handshake_connection_parse_test.go index 94e53cf..20b782e 100644 --- a/zz_handshake_connection_parse_test.go +++ b/zz_handshake_connection_parse_test.go @@ -107,13 +107,9 @@ func TestHandleConnectionValidMsgDispatchesToProcessMessage(t *testing.T) { t.Cleanup(hm.Stop) hm.outgoing[321] = time.Now() - msg := HandshakeMsg{ - Type: HandshakeReject, - NodeID: 321, - Reason: "nope", - Timestamp: time.Now().Unix(), - } - raw, _ := json.Marshal(&msg) + msg := signedMsg(deterministicIdentity(32), HandshakeReject, 321, hm.rt.NodeID()) + msg.Reason = "nope" + raw, _ := json.Marshal(msg) stream := newMockStreamFrom(321, raw) @@ -190,12 +186,8 @@ func TestProcessMessageReplayCaughtOnSecondDispatch(t *testing.T) { t.Cleanup(hm.Stop) hm.outgoing[33] = time.Now() - msg := &HandshakeMsg{ - Type: HandshakeReject, - NodeID: 33, - Reason: "replay-test", - Timestamp: time.Now().Unix(), - } + msg := signedMsg(deterministicIdentity(33), HandshakeReject, 33, hm.rt.NodeID()) + msg.Reason = "replay-test" stream := &addrStream{addr: coreapi.Addr{Node: 33}} @@ -331,11 +323,7 @@ func TestProcessMessageRevokeDispatchRemovesTrusted(t *testing.T) { t.Cleanup(hm.Stop) hm.trusted[99] = &TrustRecord{NodeID: 99} - msg := &HandshakeMsg{ - Type: HandshakeRevoke, - NodeID: 99, - Timestamp: time.Now().Unix(), - } + msg := signedMsg(deterministicIdentity(99), HandshakeRevoke, 99, hm.rt.NodeID()) hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 99}}, msg) hm.mu.RLock() diff --git a/zz_handshake_registry_integration_test.go b/zz_handshake_registry_integration_test.go index 30b3080..10a88fa 100644 --- a/zz_handshake_registry_integration_test.go +++ b/zz_handshake_registry_integration_test.go @@ -117,6 +117,7 @@ func TestApproveHandshakeMovesPendingToTrustedAndEmitsWebhook(t *testing.T) { hm.pending[77] = &PendingHandshake{ NodeID: 77, PublicKey: "pending-pubkey", + KeyConfirmed: true, // the registry listed this key when the request came in Justification: "please", ReceivedAt: time.Now(), } diff --git a/zz_handshake_relayed_trust_test.go b/zz_handshake_relayed_trust_test.go index 819f408..01a6685 100644 --- a/zz_handshake_relayed_trust_test.go +++ b/zz_handshake_relayed_trust_test.go @@ -45,9 +45,11 @@ func TestProcessRelayedRequestAlreadyTrustedShortCircuits(t *testing.T) { func TestProcessRelayedRequestMutualAutoApproves(t *testing.T) { t.Parallel() - hm := newTestHM(t, "") - t.Cleanup(hm.Stop) + hm, rt := newFakeHM(t) + rt.registry.setLookup(99, map[string]interface{}{"public_key": bindKeyA}) + hm.mu.Lock() hm.outgoing[99] = time.Now() + hm.mu.Unlock() hm.processRelayedRequest(99, "") @@ -64,9 +66,9 @@ func TestProcessRelayedRequestMutualAutoApproves(t *testing.T) { if stillOutgoing { t.Fatal("outgoing[99] should have been cleared") } - // PublicKey is NOT populated by the relay path (comes from backfillPeerKey goRPC). - if rec.PublicKey != "" { - t.Fatalf("PublicKey should be empty on relay mutual path (got %q)", rec.PublicKey) + // The relay carries no key: the record is bound to the registry's. + if rec.PublicKey != bindKeyA { + t.Fatalf("PublicKey = %q, want the registry's key", rec.PublicKey) } } @@ -74,10 +76,9 @@ func TestProcessRelayedRequestMutualAutoApproves(t *testing.T) { func TestProcessRelayedRequestAutoApproveMarksNonMutual(t *testing.T) { t.Parallel() - rt := newTestRuntime() + hm, rt := newFakeHM(t) rt.trustAutoApprove = true - hm := NewManager(rt) - t.Cleanup(hm.Stop) + rt.registry.setLookup(55, map[string]interface{}{"public_key": bindKeyA}) hm.processRelayedRequest(55, "from-relay") @@ -91,6 +92,9 @@ func TestProcessRelayedRequestAutoApproveMarksNonMutual(t *testing.T) { if rec.Mutual { t.Fatal("rec.Mutual should be false (auto_approve, not mutual)") } + if rec.PublicKey != bindKeyA { + t.Fatalf("PublicKey = %q, want the registry's key", rec.PublicKey) + } if isPending { t.Fatal("pending[55] should NOT be set when auto-approved") } @@ -131,10 +135,9 @@ func TestProcessRelayedRequestTrustedAgentAutoApproves(t *testing.T) { // NOT parallel — setTestTrustedAgents mutates a package-level map. setTestTrustedAgents(t, []testAgent{{NodeID: 77, Hostname: "crtsh.example"}}) - rt := newTestRuntime() + hm, rt := newFakeHM(t) rt.trustChecker = testTrustChecker{} - hm := NewManager(rt) - t.Cleanup(hm.Stop) + rt.registry.setLookup(77, map[string]interface{}{"public_key": bindKeyA}) hm.processRelayedRequest(77, "service-agent reply handshake") diff --git a/zz_outgoing_lifetime_test.go b/zz_outgoing_lifetime_test.go index edd1080..4af470c 100644 --- a/zz_outgoing_lifetime_test.go +++ b/zz_outgoing_lifetime_test.go @@ -20,7 +20,7 @@ func TestOutgoing_AcceptanceHoursLaterIsHonoured(t *testing.T) { hm.mu.Unlock() hm.reapOutgoingAndRevoked() // the reaper must not drop it - hm.handleAccept(&HandshakeMsg{Type: HandshakeAccept, NodeID: 77, PublicKey: "k", Timestamp: time.Now().Unix()}) + hm.handleAccept(&HandshakeMsg{Type: HandshakeAccept, NodeID: 77, PublicKey: "k", Timestamp: time.Now().Unix()}, true) hm.mu.RLock() defer hm.mu.RUnlock() diff --git a/zz_pending_full_auto_accept_test.go b/zz_pending_full_auto_accept_test.go index 76962e7..2d0e576 100644 --- a/zz_pending_full_auto_accept_test.go +++ b/zz_pending_full_auto_accept_test.go @@ -23,7 +23,7 @@ func TestFullPendingQueueDoesNotBlockAutoAccept(t *testing.T) { t.Run("trust-auto-approve, direct", func(t *testing.T) { m, _ := hsTestManager(t, true) fillPending(m) - m.handleRequest(nil, &HandshakeMsg{NodeID: 5000, PublicKey: "k"}, false) + m.handleRequest(nil, &HandshakeMsg{NodeID: 5000, PublicKey: "k"}, true) m.mu.RLock() defer m.mu.RUnlock() if _, ok := m.trusted[5000]; !ok { @@ -35,7 +35,9 @@ func TestFullPendingQueueDoesNotBlockAutoAccept(t *testing.T) { }) t.Run("trust-auto-approve, relayed", func(t *testing.T) { - m, _ := hsTestManager(t, true) + m, rt := newFakeHM(t) + rt.trustAutoApprove = true + rt.registry.setLookup(6000, map[string]interface{}{"public_key": bindKeyA}) fillPending(m) m.processRelayedRequest(6000, "join") m.mu.RLock() @@ -46,7 +48,8 @@ func TestFullPendingQueueDoesNotBlockAutoAccept(t *testing.T) { }) t.Run("mutual request, relayed", func(t *testing.T) { - m, _ := hsTestManager(t, false) + m, rt := newFakeHM(t) + rt.registry.setLookup(7000, map[string]interface{}{"public_key": bindKeyA}) fillPending(m) m.mu.Lock() m.outgoing[7000] = time.Now() diff --git a/zz_ppa001_accept_revoke_identity_test.go b/zz_ppa001_accept_revoke_identity_test.go index 805face..8241040 100644 --- a/zz_ppa001_accept_revoke_identity_test.go +++ b/zz_ppa001_accept_revoke_identity_test.go @@ -68,11 +68,7 @@ func TestPPA001_HonestAcceptOverMatchingStreamTrusts(t *testing.T) { hm.outgoing[peer] = time.Now() hm.mu.Unlock() - msg := &HandshakeMsg{ - Type: HandshakeAccept, - NodeID: peer, - Timestamp: time.Now().Unix(), - } + msg := signedMsg(deterministicIdentity(50), HandshakeAccept, peer, 1) hm.processMessage(&addrStream{addr: coreapi.Addr{Node: peer}}, msg) hm.mu.RLock() @@ -127,11 +123,7 @@ func TestPPA001_HonestRevokeOverMatchingStreamRevokes(t *testing.T) { hm.trusted[peer] = &TrustRecord{NodeID: peer, ApprovedAt: time.Now()} hm.mu.Unlock() - msg := &HandshakeMsg{ - Type: HandshakeRevoke, - NodeID: peer, - Timestamp: time.Now().Unix(), - } + msg := signedMsg(deterministicIdentity(80), HandshakeRevoke, peer, 1) hm.processMessage(&addrStream{addr: coreapi.Addr{Node: peer}}, msg) hm.mu.RLock() diff --git a/zz_ppa008_replay_key_timestamp_test.go b/zz_ppa008_replay_key_timestamp_test.go index ea69d24..af9d653 100644 --- a/zz_ppa008_replay_key_timestamp_test.go +++ b/zz_ppa008_replay_key_timestamp_test.go @@ -81,8 +81,11 @@ func TestPPA008_DistinctSendersGetDistinctReplayKeys(t *testing.T) { hm.mu.Unlock() ts := time.Now().Unix() - hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 11}}, &HandshakeMsg{Type: HandshakeReject, NodeID: 11, Timestamp: ts}) - hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 22}}, &HandshakeMsg{Type: HandshakeReject, NodeID: 22, Timestamp: ts}) + from11 := signedMsg(deterministicIdentity(11), HandshakeReject, 11, hm.rt.NodeID()) + from22 := signedMsg(deterministicIdentity(22), HandshakeReject, 22, hm.rt.NodeID()) + from11.Timestamp, from22.Timestamp = ts, ts + hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 11}}, from11) + hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 22}}, from22) hm.mu.RLock() _, out11 := hm.outgoing[11] diff --git a/zz_registry_confirmed_trust_test.go b/zz_registry_confirmed_trust_test.go new file mode 100644 index 0000000..c3837ff --- /dev/null +++ b/zz_registry_confirmed_trust_test.go @@ -0,0 +1,485 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +package handshake + +import ( + "errors" + "path/filepath" + "testing" + "time" + + "github.com/pilot-protocol/common/coreapi" +) + +// Regression tests for trust that is granted or bound only against a key +// the registry confirmed, and for IsTrustedWithKey with no key. + +var errRegistryDown = errors.New("registry down") + +// waitFor polls cond until it holds or two seconds pass. +func waitFor(t *testing.T, what string, cond func() bool) { + t.Helper() + deadline := time.Now().Add(2 * time.Second) + for !cond() { + if time.Now().After(deadline) { + t.Fatalf("timed out waiting for %s", what) + } + time.Sleep(5 * time.Millisecond) + } +} + +// boundKeyOf reads a record's key under the lock: background binds +// write it in place. +func boundKeyOf(hm *Manager, nodeID uint32) (key string, ok bool) { + hm.mu.RLock() + defer hm.mu.RUnlock() + rec, ok := hm.trusted[nodeID] + if !ok { + return "", false + } + return rec.PublicKey, true +} + +func pendingOf(hm *Manager, nodeID uint32) *PendingHandshake { + hm.mu.RLock() + defer hm.mu.RUnlock() + return hm.pending[nodeID] +} + +func dialsTo(rt *fakeRuntime, nodeID uint32) int { + rt.mu.RLock() + defer rt.mu.RUnlock() + n := 0 + for _, c := range rt.dialCalls { + if c.NodeID == nodeID { + n++ + } + } + return n +} + +func lookupsOf(rt *fakeRuntime, nodeID uint32) int { + rt.registry.mu.Lock() + defer rt.registry.mu.Unlock() + n := 0 + for _, id := range rt.registry.lookupCalls { + if id == nodeID { + n++ + } + } + return n +} + +// keyedFakeRuntime is a fakeRuntime whose allowlist pins keys +// (KeyedTrustChecker), recording the key it was asked about. +type keyedFakeRuntime struct { + *fakeRuntime + pinned map[uint32]string + asked chan string +} + +func (r *keyedFakeRuntime) IsTrustedWithKey(nodeID uint32, pubKeyB64 string) (string, bool) { + select { + case r.asked <- pubKeyB64: + default: + } + if want, ok := r.pinned[nodeID]; ok && want == pubKeyB64 { + return "pinned-agent", true + } + return "", false +} + +// --- item 1: IsTrustedWithKey --- + +// An unbound record does not take the key a caller presents; a registry +// lookup binds it to the registry's key, which then decides. +func TestIsTrustedWithKey_UnboundRecordBindsToRegistryKeyNotPresentedKey(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.registry.setLookup(42, map[string]interface{}{"public_key": bindKeyB}) + seedTrusted(hm, 42, "") + + if !hm.IsTrustedWithKey(42, bindKeyA) { + t.Fatal("an unbound record is node-ID trust until the registry binds it") + } + waitFor(t, "the registry bind", func() bool { + key, _ := boundKeyOf(hm, 42) + return key != "" + }) + if key, _ := boundKeyOf(hm, 42); key != bindKeyB { + t.Fatalf("record bound to %q, want the registry's key, not the presented one", key) + } + if hm.IsTrustedWithKey(42, bindKeyA) { + t.Fatal("once bound to the registry's key, another key is not trusted") + } +} + +// Trust checks run per packet: an unbound record starts at most one +// registry lookup per bindRetryInterval, also while the lookup fails. +func TestIsTrustedWithKey_RegistryBindIsRateLimited(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.registry.setLookupErr(42, errRegistryDown) + seedTrusted(hm, 42, "") + + for i := 0; i < 50; i++ { + hm.IsTrustedWithKey(42, bindKeyA) + } + waitFor(t, "the first lookup", func() bool { return lookupsOf(rt, 42) >= 1 }) + time.Sleep(50 * time.Millisecond) + if n := lookupsOf(rt, 42); n != 1 { + t.Fatalf("%d registry lookups for one unbound record, want 1 per retry interval", n) + } + if key, ok := boundKeyOf(hm, 42); !ok || key != "" { + t.Fatalf("a failed lookup must leave the record as it was: key %q, present %v", key, ok) + } +} + +// --- item 2: direct requests --- + +// The TrustAutoApprove rule needs the registry to confirm the requester's +// key, like the other rules. A request whose lookup fails waits for the +// operator instead of being trusted under whatever node ID it claims. +func TestProcessMessage_AutoApproveWithoutRegistryConfirmationIsPending(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.nodeID = 1 + rt.trustAutoApprove = true + rt.registry.setLookupErr(500, errRegistryDown) + + attacker := deterministicIdentity(66) + hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 666}}, + signedMsg(attacker, HandshakeRequest, 500, 1)) + + if trustRecordOf(hm, 500) != nil { + t.Fatal("auto-approve trusted a node ID whose key the registry did not confirm") + } + p := pendingOf(hm, 500) + if p == nil { + t.Fatal("the unconfirmed request should wait for the operator") + } + if p.KeyConfirmed { + t.Fatal("pending entry marked confirmed although the registry did not answer") + } +} + +// The same request, confirmed by the registry, is auto-approved and bound +// to the confirmed key. +func TestProcessMessage_AutoApproveWithRegistryConfirmationTrusts(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.nodeID = 1 + rt.trustAutoApprove = true + peer := deterministicIdentity(67) + rt.registry.setLookup(500, map[string]interface{}{"public_key": keyOf(peer)}) + + hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 500}}, + signedMsg(peer, HandshakeRequest, 500, 1)) + + rec := trustRecordOf(hm, 500) + if rec == nil { + t.Fatal("a confirmed request must be auto-approved") + } + if rec.PublicKey != keyOf(peer) { + t.Fatalf("record bound to %q, want the confirmed key", rec.PublicKey) + } +} + +// A request under another key the registry could not confirm neither +// drops a bound record nor rebinds it, and is not answered. +func TestHandleRequest_UnconfirmedKeyKeepsBoundRecord(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.nodeID = 1 + seedTrusted(hm, 42, bindKeyA) + + hm.handleRequest(nil, &HandshakeMsg{Type: HandshakeRequest, NodeID: 42, PublicKey: bindKeyB}, false) + hm.Stop() // drain any accept it started + + if rec := trustRecordOf(hm, 42); rec == nil || rec.PublicKey != bindKeyA { + t.Fatalf("an unconfirmed key changed the bound record: %+v", rec) + } + if pendingOf(hm, 42) != nil { + t.Fatal("an unconfirmed request for a trusted node must not be queued") + } + if n := dialsTo(rt, 42); n != 0 { + t.Fatalf("an unconfirmed request under another key was answered (%d dials)", n) + } +} + +// An unbound record does not take the key of an unconfirmed request; the +// registry binds it. +func TestHandleRequest_UnconfirmedKeyDoesNotBindUnboundRecord(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.nodeID = 1 + rt.registry.setLookup(42, map[string]interface{}{"public_key": bindKeyB}) + seedTrusted(hm, 42, "") + + hm.handleRequest(nil, &HandshakeMsg{Type: HandshakeRequest, NodeID: 42, PublicKey: bindKeyA}, false) + waitFor(t, "the registry bind", func() bool { + key, _ := boundKeyOf(hm, 42) + return key != "" + }) + if key, _ := boundKeyOf(hm, 42); key != bindKeyB { + t.Fatalf("record bound to %q, want the registry's key", key) + } +} + +// An unconfirmed request does not replace a confirmed pending one under +// another key, so the operator approves what the registry confirmed. +func TestHandleRequest_UnconfirmedRequestDoesNotReplaceConfirmedPending(t *testing.T) { + t.Parallel() + hm, _ := newFakeHM(t) + + hm.handleRequest(nil, &HandshakeMsg{Type: HandshakeRequest, NodeID: 42, PublicKey: bindKeyA, Justification: "real"}, true) + hm.handleRequest(nil, &HandshakeMsg{Type: HandshakeRequest, NodeID: 42, PublicKey: bindKeyB, Justification: "forged"}, false) + + p := pendingOf(hm, 42) + if p == nil || p.PublicKey != bindKeyA || !p.KeyConfirmed || p.Justification != "real" { + t.Fatalf("confirmed pending request was replaced: %+v", p) + } +} + +// Approving a request the registry did not confirm binds the record to +// the registry's key, never to the key the request carried. +func TestApproveHandshake_UnconfirmedPendingBindsRegistryKey(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.nodeID = 1 + rt.registry.setLookup(77, map[string]interface{}{"public_key": bindKeyB}) + hm.mu.Lock() + hm.pending[77] = &PendingHandshake{NodeID: 77, PublicKey: bindKeyA, ReceivedAt: time.Now()} + hm.mu.Unlock() + + if err := hm.ApproveHandshake(77); err != nil { + t.Fatalf("ApproveHandshake: %v", err) + } + if key, ok := boundKeyOf(hm, 77); !ok || key == bindKeyA { + t.Fatalf("the unconfirmed key bound the record at approval: key %q, present %v", key, ok) + } + hm.Stop() // waits for the backfill + if rec := trustRecordOf(hm, 77); rec == nil || rec.PublicKey != bindKeyB { + t.Fatalf("record bound to %+v, want the registry's key", rec) + } +} + +// Whether a pending request's key was confirmed survives a restart. +func TestPendingKeyConfirmedPersists(t *testing.T) { + t.Parallel() + path := filepath.Join(t.TempDir(), "identity.json") + hm := newTestHM(t, path) + t.Cleanup(hm.Stop) + hm.mu.Lock() + hm.pending[1] = &PendingHandshake{NodeID: 1, PublicKey: bindKeyA, KeyConfirmed: true, ReceivedAt: time.Now()} + hm.pending[2] = &PendingHandshake{NodeID: 2, PublicKey: bindKeyB, ReceivedAt: time.Now()} + hm.mu.Unlock() + hm.saveTrust() + + hm2 := newTestHM(t, path) + t.Cleanup(hm2.Stop) + if p := pendingOf(hm2, 1); p == nil || !p.KeyConfirmed { + t.Fatalf("confirmed flag lost: %+v", p) + } + if p := pendingOf(hm2, 2); p == nil || p.KeyConfirmed { + t.Fatalf("unconfirmed entry loaded as confirmed: %+v", p) + } +} + +// An accept the registry did not confirm completes the request but does +// not bind its key; the registry's key is bound instead. +func TestHandleAccept_UnconfirmedKeyBindsRegistryKey(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.registry.setLookup(42, map[string]interface{}{"public_key": bindKeyB}) + hm.mu.Lock() + hm.outgoing[42] = time.Now() + hm.mu.Unlock() + + hm.handleAccept(&HandshakeMsg{Type: HandshakeAccept, NodeID: 42, PublicKey: bindKeyA}, false) + + if trustRecordOf(hm, 42) == nil { + t.Fatal("a solicited accept must still complete the handshake") + } + waitFor(t, "the registry bind", func() bool { + key, _ := boundKeyOf(hm, 42) + return key != "" + }) + if key, _ := boundKeyOf(hm, 42); key != bindKeyB { + t.Fatalf("record bound to %q, want the registry's key", key) + } +} + +// An unconfirmed accept under another key leaves a bound record and the +// outgoing request alone. +func TestHandleAccept_UnconfirmedDifferentKeyIgnored(t *testing.T) { + t.Parallel() + hm, _ := newFakeHM(t) + seedTrusted(hm, 42, bindKeyA) + hm.mu.Lock() + hm.outgoing[42] = time.Now() + hm.mu.Unlock() + + hm.handleAccept(&HandshakeMsg{Type: HandshakeAccept, NodeID: 42, PublicKey: bindKeyB}, false) + + if rec := trustRecordOf(hm, 42); rec == nil || rec.PublicKey != bindKeyA { + t.Fatalf("an unconfirmed accept changed the bound record: %+v", rec) + } + hm.mu.RLock() + _, out := hm.outgoing[42] + hm.mu.RUnlock() + if !out { + t.Fatal("an ignored accept must not consume the outgoing request") + } +} + +// --- item 2: relayed requests --- + +// No automatic rule trusts a relayed request when the registry cannot +// confirm the sender's key; each waits for the operator. +func TestProcessRelayedRequest_NoRegistryKeyMeansNoAutoTrust(t *testing.T) { + setTestTrustedAgents(t, []testAgent{{NodeID: 303, Hostname: "agent"}}) + + cases := []struct { + name string + setup func(hm *Manager, rt *fakeRuntime, peer uint32) + }{ + {"auto-approve", func(hm *Manager, rt *fakeRuntime, peer uint32) { rt.trustAutoApprove = true }}, + {"mutual", func(hm *Manager, rt *fakeRuntime, peer uint32) { + hm.mu.Lock() + hm.outgoing[peer] = time.Now() + hm.mu.Unlock() + }}, + {"same network", func(hm *Manager, rt *fakeRuntime, peer uint32) { + rt.registry.setLookup(1, map[string]interface{}{"networks": []interface{}{float64(9)}}) + }}, + {"trusted agent", func(hm *Manager, rt *fakeRuntime, peer uint32) { rt.trustChecker = testTrustChecker{} }}, + } + for _, tc := range cases { + for _, lookup := range []struct { + name string + resp map[string]interface{} + err error + }{ + {"lookup fails", nil, errRegistryDown}, + {"no key listed", map[string]interface{}{"networks": []interface{}{float64(9)}, "public_key": ""}, nil}, + } { + t.Run(tc.name+", "+lookup.name, func(t *testing.T) { + const peer = uint32(303) + hm, rt := newFakeHM(t) + rt.nodeID = 1 + if lookup.err != nil { + rt.registry.setLookupErr(peer, lookup.err) + } else { + rt.registry.setLookup(peer, lookup.resp) + } + tc.setup(hm, rt, peer) + + hm.processRelayedRequest(peer, "relayed") + + if rec := trustRecordOf(hm, peer); rec != nil { + t.Fatalf("trusted without a registry-confirmed key: %+v", rec) + } + if pendingOf(hm, peer) == nil { + t.Fatal("the request should wait for the operator") + } + }) + } + } +} + +// With the registry's key, each rule trusts the relayed sender, bound to +// that key from the start. +func TestProcessRelayedRequest_RulesBindRegistryKey(t *testing.T) { + setTestTrustedAgents(t, []testAgent{{NodeID: 304, Hostname: "agent"}}) + + cases := []struct { + name string + setup func(hm *Manager, rt *fakeRuntime, peer uint32) + }{ + {"auto-approve", func(hm *Manager, rt *fakeRuntime, peer uint32) { rt.trustAutoApprove = true }}, + {"mutual", func(hm *Manager, rt *fakeRuntime, peer uint32) { + hm.mu.Lock() + hm.outgoing[peer] = time.Now() + hm.mu.Unlock() + }}, + {"same network", func(hm *Manager, rt *fakeRuntime, peer uint32) { + rt.registry.setLookup(1, map[string]interface{}{"networks": []interface{}{float64(9)}}) + }}, + {"trusted agent", func(hm *Manager, rt *fakeRuntime, peer uint32) { rt.trustChecker = testTrustChecker{} }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + const peer = uint32(304) + hm, rt := newFakeHM(t) + rt.nodeID = 1 + rt.registry.setLookup(peer, map[string]interface{}{"networks": []interface{}{float64(9)}, "public_key": bindKeyA}) + tc.setup(hm, rt, peer) + + hm.processRelayedRequest(peer, "relayed") + + rec := trustRecordOf(hm, peer) + if rec == nil { + t.Fatal("a relayed request the registry confirmed should be trusted by this rule") + } + if rec.PublicKey != bindKeyA { + t.Fatalf("record bound to %q, want the registry's key", rec.PublicKey) + } + }) + } +} + +// The registry's key also reaches a pinned allowlist entry on the relay +// path, which used to be asked with no key and could never match. +func TestProcessRelayedRequest_PinnedAgentCheckedAgainstRegistryKey(t *testing.T) { + t.Parallel() + const peer = uint32(305) + for _, tc := range []struct { + name string + regKey string + trusted bool + }{ + {"registry lists the pinned key", bindKeyA, true}, + {"registry lists another key", bindKeyB, false}, + } { + t.Run(tc.name, func(t *testing.T) { + rt := &keyedFakeRuntime{fakeRuntime: newFakeRuntime(), pinned: map[uint32]string{peer: bindKeyA}, asked: make(chan string, 1)} + rt.registry.setLookup(peer, map[string]interface{}{"public_key": tc.regKey}) + hm := NewManager(rt) + t.Cleanup(hm.Stop) + + hm.processRelayedRequest(peer, "relayed") + + select { + case got := <-rt.asked: + if got != tc.regKey { + t.Fatalf("allowlist asked with %q, want the registry's key", got) + } + default: + t.Fatal("the allowlist was not asked") + } + if (trustRecordOf(hm, peer) != nil) != tc.trusted { + t.Fatalf("trusted = %v, want %v", trustRecordOf(hm, peer) != nil, tc.trusted) + } + }) + } +} + +// A relayed request for a trusted node ID whose registry key is no longer +// the bound one drops the stale record and is handled as a new request. +func TestProcessRelayedRequest_RebindingDropsStaleRecord(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.nodeID = 1 + rt.registry.setLookup(42, map[string]interface{}{"public_key": bindKeyB}) + seedTrusted(hm, 42, bindKeyA) + + hm.processRelayedRequest(42, "new holder") + + if rec := trustRecordOf(hm, 42); rec != nil { + t.Fatalf("stale record survived: %+v", rec) + } + if pendingOf(hm, 42) == nil { + t.Fatal("the new key holder's request should wait for the operator") + } +} diff --git a/zz_relayed_more_test.go b/zz_relayed_more_test.go index 3145647..a1be233 100644 --- a/zz_relayed_more_test.go +++ b/zz_relayed_more_test.go @@ -31,7 +31,8 @@ func TestProcessRelayedRequest_AlreadyTrustedIsAccept(t *testing.T) { // outgoing → mutual path. func TestProcessRelayedRequest_MutualHandshakeAutoApproves(t *testing.T) { t.Parallel() - hm := newTestHM(t, "") + hm, rt := newFakeHM(t) + rt.registry.setLookup(99, map[string]interface{}{"public_key": bindKeyA}) // Seed an outgoing request to peer 99. hm.mu.Lock() hm.outgoing[99] = time.Now() diff --git a/zz_signed_msg_helper_test.go b/zz_signed_msg_helper_test.go new file mode 100644 index 0000000..7bfbb6f --- /dev/null +++ b/zz_signed_msg_helper_test.go @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +package handshake + +import ( + "encoding/base64" + "fmt" + "time" + + "github.com/pilot-protocol/common/crypto" +) + +// signedMsg builds a handshake message of any type the way sendMessage +// does: from's key in PublicKey, and the signature over +// "handshake::" made with id. +func signedMsg(id *crypto.Identity, typ string, from, to uint32) *HandshakeMsg { + challenge := fmt.Sprintf("handshake:%d:%d", from, to) + return &HandshakeMsg{ + Type: typ, + NodeID: from, + PublicKey: base64.StdEncoding.EncodeToString(id.PublicKey), + Signature: base64.StdEncoding.EncodeToString(id.Sign([]byte(challenge))), + Timestamp: time.Now().Unix(), + } +} + +// keyOf returns id's public key as TrustRecord.PublicKey encodes it. +func keyOf(id *crypto.Identity) string { + return base64.StdEncoding.EncodeToString(id.PublicKey) +} diff --git a/zz_trust_key_binding_test.go b/zz_trust_key_binding_test.go index 22e884f..45417a4 100644 --- a/zz_trust_key_binding_test.go +++ b/zz_trust_key_binding_test.go @@ -66,41 +66,61 @@ func TestIsTrustedWithKeyDifferentKeyNotTrusted(t *testing.T) { } // Legacy records — persisted before keys were bound — keep working, so -// an upgrade does not untrust anyone. +// an upgrade does not untrust anyone. The presented key is not adopted: +// only the registry binds an unbound record. func TestIsTrustedWithKeyLegacyUnboundRecordStaysTrusted(t *testing.T) { t.Parallel() - hm, _ := newFakeHM(t) + hm, rt := newFakeHM(t) + rt.clearRegistry() // nothing to bind against seedTrusted(hm, 42, "") if !hm.IsTrustedWithKey(42, bindKeyA) { t.Fatal("legacy unbound record must remain trusted") } - if rec := trustRecordOf(hm, 42); rec == nil || rec.PublicKey != bindKeyA { - t.Fatalf("legacy record should have adopted the presented key: %+v", rec) + if rec := trustRecordOf(hm, 42); rec == nil || rec.PublicKey != "" { + t.Fatalf("an unbound record must not adopt a presented key: %+v", rec) } - // Having adopted key A, it now rejects a different key. - if hm.IsTrustedWithKey(42, bindKeyB) { - t.Fatal("record should be bound after adoption") + // So the first key to turn up does not lock out the next one. + if !hm.IsTrustedWithKey(42, bindKeyB) { + t.Fatal("unbound record is node-ID trust until the registry binds it") } } -// A call site with no key in scope degrades to the node-ID-only answer -// rather than denying. -func TestIsTrustedWithKeyEmptyKeyFallsBackToNodeID(t *testing.T) { +// A record bound to a key does not vouch for a caller that has no key to +// check the binding against. +func TestIsTrustedWithKeyEmptyKeyOnBoundRecordIsNotTrusted(t *testing.T) { t.Parallel() hm, _ := newFakeHM(t) seedTrusted(hm, 42, bindKeyA) - if !hm.IsTrustedWithKey(42, "") { - t.Fatal("empty presented key should fall back to node-ID-only trust") + if hm.IsTrustedWithKey(42, "") { + t.Fatal("an empty key must not pass a key-bound record") } if hm.IsTrustedWithKey(43, "") { t.Fatal("untrusted node must stay untrusted regardless of key") } - if rec := trustRecordOf(hm, 42); rec.PublicKey != bindKeyA { - t.Fatalf("binding must not be cleared by a keyless check, got %q", rec.PublicKey) + if rec := trustRecordOf(hm, 42); rec == nil || rec.PublicKey != bindKeyA { + t.Fatalf("a keyless check must neither drop nor clear the binding: %+v", rec) + } + // The node-ID question is still answered, under its own name. + if !hm.IsTrustedByNodeID(42) || !hm.IsTrusted(42) { + t.Fatal("IsTrustedByNodeID answers by node ID alone") + } +} + +// An unbound record checked with no key is node-ID trust, which is all +// it grants. +func TestIsTrustedWithKeyEmptyKeyOnUnboundRecordIsTrusted(t *testing.T) { + t.Parallel() + hm, rt := newFakeHM(t) + rt.clearRegistry() + + seedTrusted(hm, 42, "") + + if !hm.IsTrustedWithKey(42, "") { + t.Fatal("unbound record should answer by node ID") } } @@ -171,7 +191,7 @@ func TestHandleAcceptDifferentKeyDropsRecord(t *testing.T) { NodeID: 42, PublicKey: bindKeyB, Timestamp: time.Now().Unix(), - }) + }, true) if rec := trustRecordOf(hm, 42); rec != nil { t.Fatalf("accept from a different key holder must not keep or rebind the record: %+v", rec) @@ -190,7 +210,7 @@ func TestHandleAcceptWithoutKeyPreservesBinding(t *testing.T) { Type: HandshakeAccept, NodeID: 42, Timestamp: time.Now().Unix(), - }) + }, true) rec := trustRecordOf(hm, 42) if rec == nil { From 770af17bf5f1e20791c0d7348d65c657927e083d Mon Sep 17 00:00:00 2001 From: Teo Calin Date: Sun, 8 Nov 2026 13:17:38 -0800 Subject: [PATCH 2/2] Require a signature on accept, reject and revoke processMessage checked a signature only when a message carried PublicKey, and refused a keyless message only for requests. An accept, reject or revoke without a key went through on the transport's source node ID alone, which nothing proves in plaintext mode. Every daemon release sends both: from v1.1.0 (the first tag), through the in-tree pkg/daemon and plugins/handshake code and every handshake module release (v0.1.0 to v0.3.3), sendAccept, RejectHandshake and RevokeTrust set PublicKey to the daemon's key and sendMessage signs every message, and every daemon generates its identity at start. libpilot embeds the same plugin. So a message of any type without a key or a signature is now refused. Without the registry's answer, a signature checked against the key the message brings only shows the sender holds some key. For an accept, reject or revoke from a peer whose trust record is bound to a key, that key now stands in for the registry: the message must carry and be signed by it. --- handshake.go | 113 ++++++++++++++++--------- zz_signed_accept_reject_revoke_test.go | 94 ++++++++++++++++++++ 2 files changed, 165 insertions(+), 42 deletions(-) create mode 100644 zz_signed_accept_reject_revoke_test.go diff --git a/handshake.go b/handshake.go index 19641cb..5c01691 100644 --- a/handshake.go +++ b/handshake.go @@ -767,13 +767,14 @@ func (hm *Manager) handleConnection(stream coreapi.Stream) { // processMessage handles an incoming handshake message. // -// On a successful (claimed pubkey == registry-registered pubkey) match -// we set registryBound = true and pass it to handleRequest and -// handleAccept. Every automatic trust rule, and every binding or -// rebinding of a trust record, requires that flag — without it, -// trusting a peer by node_id alone is unsafe (registry down or no -// record means we'd be relying on signature verification against a -// claimed pubkey that nothing has tied to the claimed node ID). +// Every message must carry a key and a signature over +// "handshake::". On a successful (claimed pubkey == +// registry-registered pubkey) match we set registryBound = true and pass +// it to handleRequest and handleAccept. Every automatic trust rule, and +// every binding or rebinding of a trust record, requires that flag — +// without it, trusting a peer by node_id alone is unsafe (registry down +// or no record means we'd be relying on signature verification against +// a claimed pubkey that nothing has tied to the claimed node ID). func (hm *Manager) processMessage(stream coreapi.Stream, msg *HandshakeMsg) { // Timestamp validation now := time.Now() @@ -797,52 +798,80 @@ func (hm *Manager) processMessage(stream coreapi.Stream, msg *HandshakeMsg) { return } - // M12 fix: verify P2P signature if the sender provides a public key - registryBound := false - if msg.PublicKey == "" && msg.Type == HandshakeRequest { - slog.Warn("handshake: request missing public key, rejecting", "peer_node_id", msg.NodeID) + // Every message must carry the sender's key and a signature, whatever + // its type. Every daemon release (v1.1.0 on) puts both on every + // message it sends here: sendMessage signs with the identity each + // daemon generates at start, and SendRequest, sendAccept, + // RejectHandshake and RevokeTrust all set PublicKey. Accept, reject + // and revoke used to be let through unsigned when they left the key + // out, with only the transport's source node ID standing for the + // sender, which proves nothing in plaintext mode. + if msg.PublicKey == "" { + slog.Warn("handshake: message missing public key, rejecting", "peer_node_id", msg.NodeID, "type", msg.Type) + return + } + if msg.Signature == "" { + slog.Warn("handshake: message missing signature, rejecting", "peer_node_id", msg.NodeID, "type", msg.Type) return } - if msg.PublicKey != "" { - if msg.Signature == "" { - slog.Warn("handshake: missing signature from authenticated node", "peer_node_id", msg.NodeID) - return - } - // M3 fix: verify claimed pubkey against registry-registered key - verifyKey := msg.PublicKey - if reg := hm.rt.Registry(); reg != nil { - resp, err := reg.Lookup(msg.NodeID) - if err == nil { - if regPubKey, ok := resp["public_key"].(string); ok && regPubKey != "" { - if subtle.ConstantTimeCompare([]byte(regPubKey), []byte(msg.PublicKey)) != 1 { - slog.Warn("handshake: pubkey mismatch with registry", - "peer_node_id", msg.NodeID) - return - } - verifyKey = regPubKey - registryBound = true + // registryBound: the registry lists msg.PublicKey for msg.NodeID. Only + // then may the key bind a trust record or replace a bound one. + registryBound := false + verifyKey := msg.PublicKey + if reg := hm.rt.Registry(); reg != nil { + resp, err := reg.Lookup(msg.NodeID) + if err == nil { + if regPubKey, ok := resp["public_key"].(string); ok && regPubKey != "" { + if subtle.ConstantTimeCompare([]byte(regPubKey), []byte(msg.PublicKey)) != 1 { + slog.Warn("handshake: pubkey mismatch with registry", + "peer_node_id", msg.NodeID) + return } + verifyKey = regPubKey + registryBound = true } } + } - challenge := fmt.Sprintf("handshake:%d:%d", msg.NodeID, hm.rt.NodeID()) - pubKeyBytes, err := base64.StdEncoding.DecodeString(verifyKey) - if err != nil { - slog.Warn("handshake: invalid public key encoding", "peer_node_id", msg.NodeID, "err", err) - return - } - sigBytes, err := base64.StdEncoding.DecodeString(msg.Signature) - if err != nil { - slog.Warn("handshake: invalid signature encoding", "peer_node_id", msg.NodeID, "err", err) - return + // Without the registry, a signature checked against the key the + // message brings proves only that the sender holds some key. For the + // messages that act on trust already granted, the key the record is + // bound to stands in for the registry: an accept, reject or revoke + // from a trusted peer must be signed by the key it was trusted under. + if !registryBound && msg.Type != HandshakeRequest { + hm.mu.RLock() + var bound string + if rec, ok := hm.trusted[msg.NodeID]; ok { + bound = rec.PublicKey } - if !crypto.Verify(pubKeyBytes, []byte(challenge), sigBytes) { - slog.Warn("handshake: P2P signature verification failed", "peer_node_id", msg.NodeID) - return + hm.mu.RUnlock() + if bound != "" { + if subtle.ConstantTimeCompare([]byte(bound), []byte(msg.PublicKey)) != 1 { + slog.Warn("handshake: key differs from the trusted key and the registry could not confirm it, rejecting", + "peer_node_id", msg.NodeID, "type", msg.Type) + return + } + verifyKey = bound } } + challenge := fmt.Sprintf("handshake:%d:%d", msg.NodeID, hm.rt.NodeID()) + pubKeyBytes, err := base64.StdEncoding.DecodeString(verifyKey) + if err != nil { + slog.Warn("handshake: invalid public key encoding", "peer_node_id", msg.NodeID, "err", err) + return + } + sigBytes, err := base64.StdEncoding.DecodeString(msg.Signature) + if err != nil { + slog.Warn("handshake: invalid signature encoding", "peer_node_id", msg.NodeID, "err", err) + return + } + if !crypto.Verify(pubKeyBytes, []byte(challenge), sigBytes) { + slog.Warn("handshake: P2P signature verification failed", "peer_node_id", msg.NodeID) + return + } + // accept / reject / revoke all act on existing local state (they grant // trust, cancel an outgoing request, or drop trust), so each must // arrive on a transport whose authenticated node ID matches the diff --git a/zz_signed_accept_reject_revoke_test.go b/zz_signed_accept_reject_revoke_test.go new file mode 100644 index 0000000..6da88b5 --- /dev/null +++ b/zz_signed_accept_reject_revoke_test.go @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +package handshake + +import ( + "testing" + "time" + + "github.com/pilot-protocol/common/coreapi" +) + +// Regression tests: accept, reject and revoke must carry a key and a +// valid signature, and without the registry, the key a trusted peer's +// record is bound to. + +// A message with no key and no signature is refused for every type, +// including over a stream from the claimed sender. +func TestProcessMessage_UnsignedAcceptRejectRevokeRefused(t *testing.T) { + t.Parallel() + const peer = uint32(810) + stream := &addrStream{addr: coreapi.Addr{Node: peer}} + + t.Run("accept", func(t *testing.T) { + hm := newTestHM(t, "") + t.Cleanup(hm.Stop) + hm.mu.Lock() + hm.outgoing[peer] = time.Now() + hm.mu.Unlock() + hm.processMessage(stream, &HandshakeMsg{Type: HandshakeAccept, NodeID: peer, Timestamp: time.Now().Unix()}) + if trustRecordOf(hm, peer) != nil { + t.Fatal("an unsigned accept established trust") + } + }) + t.Run("reject", func(t *testing.T) { + hm := newTestHM(t, "") + t.Cleanup(hm.Stop) + hm.mu.Lock() + hm.outgoing[peer] = time.Now() + hm.mu.Unlock() + hm.processMessage(stream, &HandshakeMsg{Type: HandshakeReject, NodeID: peer, Timestamp: time.Now().Unix()}) + hm.mu.RLock() + _, out := hm.outgoing[peer] + hm.mu.RUnlock() + if !out { + t.Fatal("an unsigned reject cancelled the outgoing request") + } + }) + t.Run("revoke", func(t *testing.T) { + hm := newTestHM(t, "") + t.Cleanup(hm.Stop) + seedTrusted(hm, peer, "") + hm.processMessage(stream, &HandshakeMsg{Type: HandshakeRevoke, NodeID: peer, Timestamp: time.Now().Unix()}) + if trustRecordOf(hm, peer) == nil { + t.Fatal("an unsigned revoke dropped trust") + } + }) + t.Run("key without signature", func(t *testing.T) { + hm := newTestHM(t, "") + t.Cleanup(hm.Stop) + seedTrusted(hm, peer, "") + msg := signedMsg(deterministicIdentity(81), HandshakeRevoke, peer, hm.rt.NodeID()) + msg.Signature = "" + hm.processMessage(stream, msg) + if trustRecordOf(hm, peer) == nil { + t.Fatal("a revoke without a signature dropped trust") + } + }) +} + +// With the registry unreachable, a revoke for a key-bound record must be +// signed by the bound key: a signature by whatever key the message brings +// proves nothing. +func TestProcessMessage_RevokeWithoutRegistryNeedsBoundKey(t *testing.T) { + t.Parallel() + const peer = uint32(820) + trustedID := deterministicIdentity(82) + otherID := deterministicIdentity(83) + stream := &addrStream{addr: coreapi.Addr{Node: peer}} + + hm, rt := newFakeHM(t) + rt.nodeID = 1 + rt.registry.setLookupErr(peer, errRegistryDown) + seedTrusted(hm, peer, keyOf(trustedID)) + + hm.processMessage(stream, signedMsg(otherID, HandshakeRevoke, peer, 1)) + if trustRecordOf(hm, peer) == nil { + t.Fatal("a revoke signed by a key other than the bound one dropped trust") + } + + hm.processMessage(stream, signedMsg(trustedID, HandshakeRevoke, peer, 1)) + if trustRecordOf(hm, peer) != nil { + t.Fatal("a revoke signed by the bound key must still revoke") + } +}