Add device keys, revocation and libp2p gossip with sync
- identity: 12-word recovery phrase, device certificates, revocations, device key storage; the identity key is never stored on a node - listing: signed by a device with an embedded certificate, domain-separated signatures, ErrTimeWindow for clock-related failures, region helpers - store: revocations purge a device's listings; keyset-paged Since for sync - p2p: gossipsub topic per geohash region plus a global revocation topic, validators run before relaying, /allmende/1/sync catch-up protocol, bootstrap peers and mDNS - docs: key model, replication and conflict rules Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var testNow = time.Unix(1_800_000_000, 0)
|
||||
|
||||
func TestPhraseRoundTrip(t *testing.T) {
|
||||
root, phrase, err := NewRoot()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n := len(strings.Fields(phrase)); n != 12 {
|
||||
t.Fatalf("phrase has %d words, want 12", n)
|
||||
}
|
||||
again, err := FromPhrase(" " + strings.ToUpper(phrase) + "\n")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if again.PublicKeyHex() != root.PublicKeyHex() {
|
||||
t.Fatal("phrase did not recreate the same identity")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadPhraseRejected(t *testing.T) {
|
||||
_, phrase, _ := NewRoot()
|
||||
words := strings.Fields(phrase)
|
||||
// Swapping two different words almost always breaks the checksum;
|
||||
// changing the last word to another one is deterministic enough to try several.
|
||||
bad := 0
|
||||
for _, w := range []string{"abandon", "zoo", "legal", "winner"} {
|
||||
if w == words[11] {
|
||||
continue
|
||||
}
|
||||
cp := append([]string{}, words...)
|
||||
cp[11] = w
|
||||
if _, err := FromPhrase(strings.Join(cp, " ")); err != nil {
|
||||
bad++
|
||||
}
|
||||
}
|
||||
if bad == 0 {
|
||||
t.Fatal("checksum never rejected a modified phrase")
|
||||
}
|
||||
if _, err := FromPhrase("not a real phrase"); err == nil {
|
||||
t.Fatal("garbage phrase accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCert(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
dev, err := Enroll(root, testNow, 30*24*time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := dev.Cert.Check(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !dev.Cert.Covers(testNow.Unix()) || dev.Cert.Covers(testNow.Add(31*24*time.Hour).Unix()) ||
|
||||
dev.Cert.Covers(testNow.Add(-time.Second).Unix()) {
|
||||
t.Fatal("Covers returned wrong validity window")
|
||||
}
|
||||
|
||||
forged := *dev.Cert
|
||||
forged.Device = strings.Repeat("a", 64)
|
||||
if forged.Check() == nil {
|
||||
t.Fatal("tampered cert accepted")
|
||||
}
|
||||
other, _, _ := NewRoot()
|
||||
swapped := *dev.Cert
|
||||
swapped.Author = other.PublicKeyHex()
|
||||
if swapped.Check() == nil {
|
||||
t.Fatal("cert with swapped author accepted")
|
||||
}
|
||||
if _, err := Enroll(root, testNow, 3*366*24*time.Hour); err == nil {
|
||||
t.Fatal("over-long cert lifetime accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevocation(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
dev, _ := Enroll(root, testNow, 24*time.Hour)
|
||||
r, err := Revoke(root, dev.Cert, testNow, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.Check(10); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.Check(40) == nil {
|
||||
t.Fatal("insufficient proof-of-work accepted")
|
||||
}
|
||||
t2 := *r
|
||||
t2.Device = strings.Repeat("b", 64)
|
||||
if t2.Check(0) == nil {
|
||||
t.Fatal("tampered revocation accepted")
|
||||
}
|
||||
other, _, _ := NewRoot()
|
||||
if _, err := Revoke(other, dev.Cert, testNow, 0); err == nil {
|
||||
t.Fatal("foreign identity revoked someone else's device")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceSaveLoad(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
dev, _ := Enroll(root, testNow, 24*time.Hour)
|
||||
dir := t.TempDir()
|
||||
if err := dev.Save(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := LoadDevice(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Key.PublicKeyHex() != dev.Key.PublicKeyHex() || got.Cert.Sig != dev.Cert.Sig {
|
||||
t.Fatal("device differs after reload")
|
||||
}
|
||||
if err := dev.Save(dir); err == nil {
|
||||
t.Fatal("Save must not overwrite an existing device key")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user