From 4d6e6cc17ff7e42d0ad3e514f6160a17214dbf99 Mon Sep 17 00:00:00 2001 From: Teo Calin Date: Sun, 8 Nov 2026 13:41:22 -0800 Subject: [PATCH] runtime: pass key-bound trust checks through to the handshake service handshakeServiceAdapter had no IsTrustedWithKey, so the daemon's type-assertion for a key-bound handshake service failed and every key check fell back to node id alone. HandshakeRuntime had no IsTrustedWithKey (handshake.KeyedTrustChecker), so trusted-agents key pins were never checked either. - handshakeServiceAdapter.IsTrustedWithKey forwards to the manager. - HandshakeRuntime.IsTrustedWithKey asks the trust checker with the decoded key when it supports pins, by node id when it doesn't, and refuses a key that isn't valid base64. - Compile-time guards for both optional interfaces, so a missing method can't silently weaken a check again. Test: a pinned trusted agent passes only with its key; a pinless checker still answers by node id. --- handshake.go | 41 +++++++++++++++++++++++++++ zz_keyed_trust_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 zz_keyed_trust_test.go diff --git a/handshake.go b/handshake.go index 01570a5..c7effa7 100644 --- a/handshake.go +++ b/handshake.go @@ -4,6 +4,7 @@ package runtime import ( "crypto/ed25519" + "encoding/base64" "fmt" "time" @@ -57,6 +58,30 @@ func (r HandshakeRuntime) IsTrusted(nodeID uint32) (string, bool) { return tc.IsTrusted(nodeID) } +// IsTrustedWithKey is handshake.KeyedTrustChecker: the trusted-agents list +// with an entry's key pin checked against the key the peer presented. The +// handshake manager asks this when it implements it, and otherwise falls +// back to IsTrusted, which never checked pins. A checker without pin +// support answers by node id as before; a key that isn't valid base64 is +// not trusted. +func (r HandshakeRuntime) IsTrustedWithKey(nodeID uint32, pubKeyB64 string) (string, bool) { + tc := r.d.GetTrustChecker() + if tc == nil { + return "", false + } + kc, ok := tc.(interface { + IsTrustedWithKey(nodeID uint32, pubKey []byte) (string, bool) + }) + if !ok { + return tc.IsTrusted(nodeID) + } + key, err := base64.StdEncoding.DecodeString(pubKeyB64) + if err != nil { + return "", false + } + return kc.IsTrustedWithKey(nodeID, key) +} + func (r HandshakeRuntime) PublishEvent(topic string, payload map[string]any) { r.d.PublishEvent(topic, payload) } @@ -106,6 +131,22 @@ func NewHandshakeServiceAdapter(svc *handshake.Service) daemonapi.HandshakeServi func (a handshakeServiceAdapter) IsTrusted(nodeID uint32) bool { return a.m.IsTrusted(nodeID) } +// IsTrustedWithKey passes the daemon's key-bound trust check through to the +// handshake manager. The daemon type-asserts its handshake service for this +// method; without it the daemon's key checks fell back to node id alone. +func (a handshakeServiceAdapter) IsTrustedWithKey(nodeID uint32, pubKeyB64 string) bool { + return a.m.IsTrustedWithKey(nodeID, pubKeyB64) +} + +// The daemon's and the handshake manager's optional, key-bound interfaces: +// a missing method here silently weakens their checks to node id. +var ( + _ interface { + IsTrustedWithKey(nodeID uint32, pubKeyB64 string) bool + } = handshakeServiceAdapter{} + _ handshake.KeyedTrustChecker = HandshakeRuntime{} +) + func (a handshakeServiceAdapter) TrustedPeers() []daemonapi.HandshakeTrustRecord { src := a.m.TrustedPeers() out := make([]daemonapi.HandshakeTrustRecord, len(src)) diff --git a/zz_keyed_trust_test.go b/zz_keyed_trust_test.go new file mode 100644 index 0000000..c125b96 --- /dev/null +++ b/zz_keyed_trust_test.go @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: AGPL-3.0-or-later + +package runtime_test + +import ( + "bytes" + "encoding/base64" + "testing" + + "github.com/pilot-protocol/common/daemonapi" + "github.com/pilot-protocol/handshake" + "github.com/pilot-protocol/runtime" +) + +// pinnedChecker is a trusted-agents list with one entry pinned to a key. +type pinnedChecker struct { + id uint32 + key []byte +} + +func (c pinnedChecker) IsTrusted(nodeID uint32) (string, bool) { return "agent", nodeID == c.id } +func (c pinnedChecker) IsTrustedWithKey(nodeID uint32, key []byte) (string, bool) { + return "agent", nodeID == c.id && bytes.Equal(key, c.key) +} + +// trustDaemon is a daemon whose only working method is GetTrustChecker, +// all IsTrustedWithKey needs. +type trustDaemon struct { + daemonapi.Daemon + tc daemonapi.TrustChecker +} + +func (d trustDaemon) GetTrustChecker() daemonapi.TrustChecker { return d.tc } + +// nodeOnlyChecker has no key pins. +type nodeOnlyChecker struct{ id uint32 } + +func (c nodeOnlyChecker) IsTrusted(nodeID uint32) (string, bool) { return "agent", nodeID == c.id } + +// The handshake manager asks KeyedTrustChecker when its runtime has it; the +// runtime used not to, so a trusted-agents key pin was never checked. +func TestHandshakeRuntimeChecksTrustedAgentKeyPins(t *testing.T) { + t.Parallel() + key := bytes.Repeat([]byte{7}, 32) + kc, ok := runtime.NewHandshakeRuntime(trustDaemon{tc: pinnedChecker{id: 42, key: key}}).(handshake.KeyedTrustChecker) + if !ok { + t.Fatal("HandshakeRuntime is not a handshake.KeyedTrustChecker") + } + if _, ok := kc.IsTrustedWithKey(42, base64.StdEncoding.EncodeToString(key)); !ok { + t.Error("the pinned key was refused") + } + if _, ok := kc.IsTrustedWithKey(42, base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{8}, 32))); ok { + t.Error("another key passed the pin") + } + if _, ok := kc.IsTrustedWithKey(42, "not base64!"); ok { + t.Error("an undecodable key passed") + } + + // A checker without pins answers by node id, as IsTrusted did. + if _, ok := runtime.NewHandshakeRuntime(trustDaemon{tc: nodeOnlyChecker{id: 42}}).(handshake.KeyedTrustChecker).IsTrustedWithKey(42, "anything"); !ok { + t.Error("a checker without pins stopped trusting its entry") + } +}