Skip to content
Merged
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
41 changes: 41 additions & 0 deletions handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package runtime

import (
"crypto/ed25519"
"encoding/base64"
"fmt"
"time"

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
Expand Down
63 changes: 63 additions & 0 deletions zz_keyed_trust_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading