2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-07-21 22:45:43 +01:00
|
|
|
|
|
|
|
package tka
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
2024-05-09 07:23:03 +01:00
|
|
|
"reflect"
|
2022-07-21 22:45:43 +01:00
|
|
|
"testing"
|
2022-08-12 21:13:38 +01:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2022-08-23 21:13:46 +01:00
|
|
|
"tailscale.com/types/key"
|
2022-07-21 22:45:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSigDirect(t *testing.T) {
|
2022-08-23 21:13:46 +01:00
|
|
|
node := key.NewNode()
|
|
|
|
nodeKeyPub, _ := node.Public().MarshalBinary()
|
2022-07-21 22:45:43 +01:00
|
|
|
|
|
|
|
// Verification key (the key used to sign)
|
|
|
|
pub, priv := testingKey25519(t, 1)
|
2022-08-23 21:13:46 +01:00
|
|
|
k := Key{Kind: Key25519, Public: pub, Votes: 2}
|
2022-07-21 22:45:43 +01:00
|
|
|
|
|
|
|
sig := NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
2023-01-03 17:39:55 +00:00
|
|
|
KeyID: k.MustID(),
|
2022-07-21 22:45:43 +01:00
|
|
|
Pubkey: nodeKeyPub,
|
|
|
|
}
|
2022-08-11 18:43:09 +01:00
|
|
|
sigHash := sig.SigHash()
|
2022-07-21 22:45:43 +01:00
|
|
|
sig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
|
2022-08-11 18:43:09 +01:00
|
|
|
if sig.SigHash() != sigHash {
|
|
|
|
t.Errorf("sigHash changed after signing: %x != %x", sig.SigHash(), sigHash)
|
2022-07-21 22:45:43 +01:00
|
|
|
}
|
|
|
|
|
2022-08-23 21:13:46 +01:00
|
|
|
if err := sig.verifySignature(node.Public(), k); err != nil {
|
2022-07-21 22:45:43 +01:00
|
|
|
t.Fatalf("verifySignature() failed: %v", err)
|
|
|
|
}
|
2022-08-23 21:13:46 +01:00
|
|
|
|
|
|
|
// Test verification fails when verifying for a different node
|
|
|
|
if err := sig.verifySignature(key.NewNode().Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature() did not error for different nodekey")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test verification fails if the wrong verification key is provided
|
|
|
|
copy(k.Public, []byte{1, 2, 3, 4})
|
|
|
|
if err := sig.verifySignature(node.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature() did not error for wrong verification key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSigNested(t *testing.T) {
|
|
|
|
// Network-lock key (the key used to sign the nested sig)
|
|
|
|
pub, priv := testingKey25519(t, 1)
|
|
|
|
k := Key{Kind: Key25519, Public: pub, Votes: 2}
|
|
|
|
// Rotation key (the key used to sign the outer sig)
|
|
|
|
rPub, rPriv := testingKey25519(t, 2)
|
|
|
|
// The old node key which is being rotated out
|
|
|
|
oldNode := key.NewNode()
|
|
|
|
oldPub, _ := oldNode.Public().MarshalBinary()
|
|
|
|
// The new node key that is being rotated in
|
|
|
|
node := key.NewNode()
|
|
|
|
nodeKeyPub, _ := node.Public().MarshalBinary()
|
|
|
|
|
|
|
|
// The original signature for the old node key, signed by
|
|
|
|
// the network-lock key.
|
|
|
|
nestedSig := NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
2023-01-03 17:39:55 +00:00
|
|
|
KeyID: k.MustID(),
|
2022-08-23 21:13:46 +01:00
|
|
|
Pubkey: oldPub,
|
2022-08-29 21:53:33 +01:00
|
|
|
WrappingPubkey: rPub,
|
2022-08-23 21:13:46 +01:00
|
|
|
}
|
|
|
|
sigHash := nestedSig.SigHash()
|
|
|
|
nestedSig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
if err := nestedSig.verifySignature(oldNode.Public(), k); err != nil {
|
|
|
|
t.Fatalf("verifySignature(oldNode) failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The signature authorizing the rotation, signed by the
|
|
|
|
// rotation key & embedding the original signature.
|
|
|
|
sig := NodeKeySignature{
|
|
|
|
SigKind: SigRotation,
|
|
|
|
Pubkey: nodeKeyPub,
|
|
|
|
Nested: &nestedSig,
|
|
|
|
}
|
|
|
|
sigHash = sig.SigHash()
|
|
|
|
sig.Signature = ed25519.Sign(rPriv, sigHash[:])
|
|
|
|
|
|
|
|
if err := sig.verifySignature(node.Public(), k); err != nil {
|
|
|
|
t.Fatalf("verifySignature(node) failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test verification fails if the wrong verification key is provided
|
|
|
|
kBad := Key{Kind: Key25519, Public: []byte{1, 2, 3, 4}, Votes: 2}
|
|
|
|
if err := sig.verifySignature(node.Public(), kBad); err == nil {
|
|
|
|
t.Error("verifySignature() did not error for wrong verification key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test verification fails if the inner signature is invalid
|
|
|
|
tmp := make([]byte, ed25519.SignatureSize)
|
|
|
|
copy(tmp, nestedSig.Signature)
|
|
|
|
copy(nestedSig.Signature, []byte{1, 2, 3, 4})
|
|
|
|
if err := sig.verifySignature(node.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature(node) succeeded with bad inner signature")
|
|
|
|
}
|
|
|
|
copy(nestedSig.Signature, tmp)
|
|
|
|
|
|
|
|
// Test verification fails if the outer signature is invalid
|
|
|
|
copy(sig.Signature, []byte{1, 2, 3, 4})
|
|
|
|
if err := sig.verifySignature(node.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature(node) succeeded with bad outer signature")
|
|
|
|
}
|
2022-08-29 21:53:33 +01:00
|
|
|
|
|
|
|
// Test verification fails if the outer signature is signed with a
|
|
|
|
// different public key to whats specified in WrappingPubkey
|
|
|
|
sig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
if err := sig.verifySignature(node.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature(node) succeeded with different signature")
|
|
|
|
}
|
2022-08-23 21:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSigNested_DeepNesting(t *testing.T) {
|
|
|
|
// Network-lock key (the key used to sign the nested sig)
|
|
|
|
pub, priv := testingKey25519(t, 1)
|
|
|
|
k := Key{Kind: Key25519, Public: pub, Votes: 2}
|
|
|
|
// Rotation key (the key used to sign the outer sig)
|
|
|
|
rPub, rPriv := testingKey25519(t, 2)
|
|
|
|
// The old node key which is being rotated out
|
|
|
|
oldNode := key.NewNode()
|
|
|
|
oldPub, _ := oldNode.Public().MarshalBinary()
|
|
|
|
|
|
|
|
// The original signature for the old node key, signed by
|
|
|
|
// the network-lock key.
|
|
|
|
nestedSig := NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
2023-01-03 17:39:55 +00:00
|
|
|
KeyID: k.MustID(),
|
2022-08-23 21:13:46 +01:00
|
|
|
Pubkey: oldPub,
|
2022-08-29 21:53:33 +01:00
|
|
|
WrappingPubkey: rPub,
|
2022-08-23 21:13:46 +01:00
|
|
|
}
|
|
|
|
sigHash := nestedSig.SigHash()
|
|
|
|
nestedSig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
if err := nestedSig.verifySignature(oldNode.Public(), k); err != nil {
|
|
|
|
t.Fatalf("verifySignature(oldNode) failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
outer := nestedSig
|
|
|
|
var lastNodeKey key.NodePrivate
|
2024-04-16 21:15:13 +01:00
|
|
|
for range 15 { // 15 = max nesting level for CBOR
|
2022-08-23 21:13:46 +01:00
|
|
|
lastNodeKey = key.NewNode()
|
|
|
|
nodeKeyPub, _ := lastNodeKey.Public().MarshalBinary()
|
|
|
|
|
|
|
|
tmp := outer
|
|
|
|
sig := NodeKeySignature{
|
|
|
|
SigKind: SigRotation,
|
|
|
|
Pubkey: nodeKeyPub,
|
|
|
|
Nested: &tmp,
|
|
|
|
}
|
|
|
|
sigHash = sig.SigHash()
|
|
|
|
sig.Signature = ed25519.Sign(rPriv, sigHash[:])
|
|
|
|
|
|
|
|
outer = sig
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := outer.verifySignature(lastNodeKey.Public(), k); err != nil {
|
|
|
|
t.Fatalf("verifySignature(lastNodeKey) failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-10-14 20:34:04 +01:00
|
|
|
// Test this works with our public API
|
|
|
|
a, _ := Open(newTestchain(t, "G1\nG1.template = genesis",
|
|
|
|
optTemplate("genesis", AUM{MessageKind: AUMCheckpoint, State: &State{
|
|
|
|
Keys: []Key{k},
|
|
|
|
DisablementSecrets: [][]byte{DisablementKDF([]byte{1, 2, 3})},
|
|
|
|
}})).Chonk())
|
|
|
|
if err := a.NodeKeyAuthorized(lastNodeKey.Public(), outer.Serialize()); err != nil {
|
|
|
|
t.Errorf("NodeKeyAuthorized(lastNodeKey) failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-08-23 21:13:46 +01:00
|
|
|
// Test verification fails if the inner signature is invalid
|
|
|
|
tmp := make([]byte, ed25519.SignatureSize)
|
|
|
|
copy(tmp, nestedSig.Signature)
|
|
|
|
copy(nestedSig.Signature, []byte{1, 2, 3, 4})
|
|
|
|
if err := outer.verifySignature(lastNodeKey.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature(lastNodeKey) succeeded with bad inner signature")
|
|
|
|
}
|
|
|
|
copy(nestedSig.Signature, tmp)
|
|
|
|
|
|
|
|
// Test verification fails if an intermediate signature is invalid
|
|
|
|
copy(outer.Nested.Nested.Signature, []byte{1, 2, 3, 4})
|
|
|
|
if err := outer.verifySignature(lastNodeKey.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature(lastNodeKey) succeeded with bad outer signature")
|
|
|
|
}
|
2022-07-21 22:45:43 +01:00
|
|
|
}
|
2022-08-12 21:13:38 +01:00
|
|
|
|
2022-08-29 21:53:33 +01:00
|
|
|
func TestSigCredential(t *testing.T) {
|
|
|
|
// Network-lock key (the key used to sign the nested sig)
|
|
|
|
pub, priv := testingKey25519(t, 1)
|
|
|
|
k := Key{Kind: Key25519, Public: pub, Votes: 2}
|
|
|
|
// 'credential' key (the one being delegated to)
|
|
|
|
cPub, cPriv := testingKey25519(t, 2)
|
|
|
|
// The node key being certified
|
|
|
|
node := key.NewNode()
|
|
|
|
nodeKeyPub, _ := node.Public().MarshalBinary()
|
|
|
|
|
|
|
|
// The signature certifying delegated trust to another
|
|
|
|
// public key.
|
|
|
|
nestedSig := NodeKeySignature{
|
|
|
|
SigKind: SigCredential,
|
2023-01-03 17:39:55 +00:00
|
|
|
KeyID: k.MustID(),
|
2022-08-29 21:53:33 +01:00
|
|
|
WrappingPubkey: cPub,
|
|
|
|
}
|
|
|
|
sigHash := nestedSig.SigHash()
|
|
|
|
nestedSig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
|
|
|
|
// The signature authorizing the node key, signed by the
|
|
|
|
// delegated key & embedding the original signature.
|
|
|
|
sig := NodeKeySignature{
|
|
|
|
SigKind: SigRotation,
|
|
|
|
Pubkey: nodeKeyPub,
|
|
|
|
Nested: &nestedSig,
|
|
|
|
}
|
|
|
|
sigHash = sig.SigHash()
|
|
|
|
sig.Signature = ed25519.Sign(cPriv, sigHash[:])
|
|
|
|
if err := sig.verifySignature(node.Public(), k); err != nil {
|
|
|
|
t.Fatalf("verifySignature(node) failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test verification fails if the wrong verification key is provided
|
|
|
|
kBad := Key{Kind: Key25519, Public: []byte{1, 2, 3, 4}, Votes: 2}
|
|
|
|
if err := sig.verifySignature(node.Public(), kBad); err == nil {
|
|
|
|
t.Error("verifySignature() did not error for wrong verification key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test someone can't misuse our public API for verifying node-keys
|
|
|
|
a, _ := Open(newTestchain(t, "G1\nG1.template = genesis",
|
|
|
|
optTemplate("genesis", AUM{MessageKind: AUMCheckpoint, State: &State{
|
|
|
|
Keys: []Key{k},
|
2022-09-07 00:34:16 +01:00
|
|
|
DisablementSecrets: [][]byte{DisablementKDF([]byte{1, 2, 3})},
|
2022-08-29 21:53:33 +01:00
|
|
|
}})).Chonk())
|
|
|
|
if err := a.NodeKeyAuthorized(node.Public(), nestedSig.Serialize()); err == nil {
|
|
|
|
t.Error("NodeKeyAuthorized(SigCredential, node) did not fail")
|
|
|
|
}
|
|
|
|
// but that they can use it properly (nested in a SigRotation)
|
|
|
|
if err := a.NodeKeyAuthorized(node.Public(), sig.Serialize()); err != nil {
|
|
|
|
t.Errorf("NodeKeyAuthorized(SigRotation{SigCredential}, node) failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test verification fails if the inner signature is invalid
|
|
|
|
tmp := make([]byte, ed25519.SignatureSize)
|
|
|
|
copy(tmp, nestedSig.Signature)
|
|
|
|
copy(nestedSig.Signature, []byte{1, 2, 3, 4})
|
|
|
|
if err := sig.verifySignature(node.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature(node) succeeded with bad inner signature")
|
|
|
|
}
|
|
|
|
copy(nestedSig.Signature, tmp)
|
|
|
|
|
|
|
|
// Test verification fails if the outer signature is invalid
|
|
|
|
copy(tmp, sig.Signature)
|
|
|
|
copy(sig.Signature, []byte{1, 2, 3, 4})
|
|
|
|
if err := sig.verifySignature(node.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature(node) succeeded with bad outer signature")
|
|
|
|
}
|
|
|
|
copy(sig.Signature, tmp)
|
|
|
|
|
|
|
|
// Test verification fails if we attempt to check a different node-key
|
|
|
|
otherNode := key.NewNode()
|
|
|
|
if err := sig.verifySignature(otherNode.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature(otherNode) succeeded with different principal")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test verification fails if the outer signature is signed with a
|
|
|
|
// different public key to whats specified in WrappingPubkey
|
|
|
|
sig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
if err := sig.verifySignature(node.Public(), k); err == nil {
|
|
|
|
t.Error("verifySignature(node) succeeded with different signature")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 21:13:38 +01:00
|
|
|
func TestSigSerializeUnserialize(t *testing.T) {
|
|
|
|
nodeKeyPub := []byte{1, 2, 3, 4}
|
|
|
|
pub, priv := testingKey25519(t, 1)
|
|
|
|
key := Key{Kind: Key25519, Public: pub, Votes: 2}
|
|
|
|
sig := NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
2023-01-03 17:39:55 +00:00
|
|
|
KeyID: key.MustID(),
|
2022-08-12 21:13:38 +01:00
|
|
|
Pubkey: nodeKeyPub,
|
2022-08-23 21:13:46 +01:00
|
|
|
Nested: &NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
2023-01-03 17:39:55 +00:00
|
|
|
KeyID: key.MustID(),
|
2022-08-23 21:13:46 +01:00
|
|
|
Pubkey: nodeKeyPub,
|
|
|
|
},
|
2022-08-12 21:13:38 +01:00
|
|
|
}
|
2022-08-11 18:43:09 +01:00
|
|
|
sigHash := sig.SigHash()
|
2022-08-12 21:13:38 +01:00
|
|
|
sig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
|
|
|
|
var decoded NodeKeySignature
|
|
|
|
if err := decoded.Unserialize(sig.Serialize()); err != nil {
|
|
|
|
t.Fatalf("Unserialize() failed: %v", err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(sig, decoded); diff != "" {
|
|
|
|
t.Errorf("unmarshalled version differs (-want, +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
2024-05-09 07:23:03 +01:00
|
|
|
|
|
|
|
func TestNodeKeySignatureRotationDetails(t *testing.T) {
|
|
|
|
// Trusted network lock key
|
|
|
|
pub, priv := testingKey25519(t, 1)
|
|
|
|
k := Key{Kind: Key25519, Public: pub, Votes: 2}
|
|
|
|
|
|
|
|
// 'credential' key (the one being delegated to)
|
|
|
|
cPub, cPriv := testingKey25519(t, 2)
|
|
|
|
|
|
|
|
n1, n2, n3 := key.NewNode(), key.NewNode(), key.NewNode()
|
|
|
|
n1pub, _ := n1.Public().MarshalBinary()
|
|
|
|
n2pub, _ := n2.Public().MarshalBinary()
|
|
|
|
n3pub, _ := n3.Public().MarshalBinary()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
nodeKey key.NodePublic
|
|
|
|
sigFn func() NodeKeySignature
|
|
|
|
want *RotationDetails
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "SigDirect",
|
|
|
|
nodeKey: n1.Public(),
|
|
|
|
sigFn: func() NodeKeySignature {
|
|
|
|
s := NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
|
|
|
KeyID: pub,
|
|
|
|
Pubkey: n1pub,
|
|
|
|
}
|
|
|
|
sigHash := s.SigHash()
|
|
|
|
s.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
return s
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SigWrappedCredential",
|
|
|
|
nodeKey: n1.Public(),
|
|
|
|
sigFn: func() NodeKeySignature {
|
|
|
|
nestedSig := NodeKeySignature{
|
|
|
|
SigKind: SigCredential,
|
|
|
|
KeyID: pub,
|
|
|
|
WrappingPubkey: cPub,
|
|
|
|
}
|
|
|
|
sigHash := nestedSig.SigHash()
|
|
|
|
nestedSig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
|
|
|
|
sig := NodeKeySignature{
|
|
|
|
SigKind: SigRotation,
|
|
|
|
Pubkey: n1pub,
|
|
|
|
Nested: &nestedSig,
|
|
|
|
}
|
|
|
|
sigHash = sig.SigHash()
|
|
|
|
sig.Signature = ed25519.Sign(cPriv, sigHash[:])
|
|
|
|
return sig
|
|
|
|
},
|
|
|
|
want: &RotationDetails{
|
2024-06-27 12:54:17 +01:00
|
|
|
InitialSig: &NodeKeySignature{
|
|
|
|
SigKind: SigCredential,
|
|
|
|
KeyID: pub,
|
|
|
|
WrappingPubkey: cPub,
|
|
|
|
},
|
2024-05-09 07:23:03 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SigRotation",
|
|
|
|
nodeKey: n2.Public(),
|
|
|
|
sigFn: func() NodeKeySignature {
|
|
|
|
nestedSig := NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
|
|
|
Pubkey: n1pub,
|
|
|
|
KeyID: pub,
|
|
|
|
WrappingPubkey: cPub,
|
|
|
|
}
|
|
|
|
sigHash := nestedSig.SigHash()
|
|
|
|
nestedSig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
|
|
|
|
sig := NodeKeySignature{
|
|
|
|
SigKind: SigRotation,
|
|
|
|
Pubkey: n2pub,
|
|
|
|
Nested: &nestedSig,
|
|
|
|
}
|
|
|
|
sigHash = sig.SigHash()
|
|
|
|
sig.Signature = ed25519.Sign(cPriv, sigHash[:])
|
|
|
|
return sig
|
|
|
|
},
|
|
|
|
want: &RotationDetails{
|
2024-06-27 12:54:17 +01:00
|
|
|
InitialSig: &NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
|
|
|
Pubkey: n1pub,
|
|
|
|
KeyID: pub,
|
|
|
|
WrappingPubkey: cPub,
|
|
|
|
},
|
|
|
|
PrevNodeKeys: []key.NodePublic{n1.Public()},
|
2024-05-09 07:23:03 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SigRotationNestedTwice",
|
|
|
|
nodeKey: n3.Public(),
|
|
|
|
sigFn: func() NodeKeySignature {
|
|
|
|
initialSig := NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
|
|
|
Pubkey: n1pub,
|
|
|
|
KeyID: pub,
|
|
|
|
WrappingPubkey: cPub,
|
|
|
|
}
|
|
|
|
sigHash := initialSig.SigHash()
|
|
|
|
initialSig.Signature = ed25519.Sign(priv, sigHash[:])
|
|
|
|
|
|
|
|
prevRotation := NodeKeySignature{
|
|
|
|
SigKind: SigRotation,
|
|
|
|
Pubkey: n2pub,
|
|
|
|
Nested: &initialSig,
|
|
|
|
}
|
|
|
|
sigHash = prevRotation.SigHash()
|
|
|
|
prevRotation.Signature = ed25519.Sign(cPriv, sigHash[:])
|
|
|
|
|
|
|
|
sig := NodeKeySignature{
|
|
|
|
SigKind: SigRotation,
|
|
|
|
Pubkey: n3pub,
|
|
|
|
Nested: &prevRotation,
|
|
|
|
}
|
|
|
|
sigHash = sig.SigHash()
|
|
|
|
sig.Signature = ed25519.Sign(cPriv, sigHash[:])
|
|
|
|
|
|
|
|
return sig
|
|
|
|
},
|
|
|
|
want: &RotationDetails{
|
2024-06-27 12:54:17 +01:00
|
|
|
InitialSig: &NodeKeySignature{
|
|
|
|
SigKind: SigDirect,
|
|
|
|
Pubkey: n1pub,
|
|
|
|
KeyID: pub,
|
|
|
|
WrappingPubkey: cPub,
|
|
|
|
},
|
|
|
|
PrevNodeKeys: []key.NodePublic{n2.Public(), n1.Public()},
|
2024-05-09 07:23:03 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-06-27 12:54:17 +01:00
|
|
|
if tt.want != nil {
|
|
|
|
initialHash := tt.want.InitialSig.SigHash()
|
|
|
|
tt.want.InitialSig.Signature = ed25519.Sign(priv, initialHash[:])
|
|
|
|
}
|
|
|
|
|
2024-05-09 07:23:03 +01:00
|
|
|
sig := tt.sigFn()
|
|
|
|
if err := sig.verifySignature(tt.nodeKey, k); err != nil {
|
|
|
|
t.Fatalf("verifySignature(node) failed: %v", err)
|
|
|
|
}
|
|
|
|
got, err := sig.rotationDetails()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("rotationDetails() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-27 12:02:15 +01:00
|
|
|
|
|
|
|
func TestDecodeWrappedAuthkey(t *testing.T) {
|
|
|
|
k, isWrapped, sig, priv := DecodeWrappedAuthkey("tskey-32mjsdkdsffds9o87dsfkjlh", nil)
|
|
|
|
if want := "tskey-32mjsdkdsffds9o87dsfkjlh"; k != want {
|
|
|
|
t.Errorf("decodeWrappedAuthkey(<unwrapped-key>).key = %q, want %q", k, want)
|
|
|
|
}
|
|
|
|
if isWrapped {
|
|
|
|
t.Error("decodeWrappedAuthkey(<unwrapped-key>).isWrapped = true, want false")
|
|
|
|
}
|
|
|
|
if sig != nil {
|
|
|
|
t.Errorf("decodeWrappedAuthkey(<unwrapped-key>).sig = %v, want nil", sig)
|
|
|
|
}
|
|
|
|
if priv != nil {
|
|
|
|
t.Errorf("decodeWrappedAuthkey(<unwrapped-key>).priv = %v, want nil", priv)
|
|
|
|
}
|
|
|
|
|
|
|
|
k, isWrapped, sig, priv = DecodeWrappedAuthkey("tskey-auth-k7UagY1CNTRL-ZZZZZ--TLpAEDA1ggnXuw4/fWnNWUwcoOjLemhOvml1juMl5lhLmY5sBUsj8EWEAfL2gdeD9g8VDw5tgcxCiHGlEb67BgU2DlFzZApi4LheLJraA+pYjTGChVhpZz1iyiBPD+U2qxDQAbM3+WFY0EBlggxmVqG53Hu0Rg+KmHJFMlUhfgzo+AQP6+Kk9GzvJJOs4-k36RdoSFqaoARfQo0UncHAV0t3YTqrkD5r/z2jTrE43GZWobnce7RGD4qYckUyVSF+DOj4BA/r4qT0bO8kk6zg", nil)
|
|
|
|
if want := "tskey-auth-k7UagY1CNTRL-ZZZZZ"; k != want {
|
|
|
|
t.Errorf("decodeWrappedAuthkey(<wrapped-key>).key = %q, want %q", k, want)
|
|
|
|
}
|
|
|
|
if !isWrapped {
|
|
|
|
t.Error("decodeWrappedAuthkey(<wrapped-key>).isWrapped = false, want true")
|
|
|
|
}
|
|
|
|
|
|
|
|
if sig == nil {
|
|
|
|
t.Fatal("decodeWrappedAuthkey(<wrapped-key>).sig = nil, want non-nil signature")
|
|
|
|
}
|
|
|
|
sigHash := sig.SigHash()
|
|
|
|
if !ed25519.Verify(sig.KeyID, sigHash[:], sig.Signature) {
|
|
|
|
t.Error("signature failed to verify")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the private is correct by using it.
|
|
|
|
someSig := ed25519.Sign(priv, []byte{1, 2, 3, 4})
|
|
|
|
if !ed25519.Verify(sig.WrappingPubkey, []byte{1, 2, 3, 4}, someSig) {
|
|
|
|
t.Error("failed to use priv")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|