Add runnable node, HTTP API, all-devices revocation and reconnect sync
- cmd/node: init (recovery phrase onboarding), run, recover - api: public reads of canonical signed records, token-protected writes - identity: RevokeAll voids every device certified before a cutoff, so recovery works with only the phrase; store purges by certificate age - p2p: redial bootstrap peers, sync on every new connection (in-flight guard only), validate bootstrap addresses up front - docs: API, recovery semantics, peer-ID change on recovery Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -121,3 +121,36 @@ func TestDeviceSaveLoad(t *testing.T) {
|
||||
t.Fatal("Save must not overwrite an existing device key")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevokeAll(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
oldDev, _ := Enroll(root, testNow.Add(-time.Hour), 24*time.Hour)
|
||||
newDev, _ := Enroll(root, testNow, 24*time.Hour) // enrolled at the cutoff
|
||||
other, _, _ := NewRoot()
|
||||
foreign, _ := Enroll(other, testNow.Add(-time.Hour), 24*time.Hour)
|
||||
|
||||
r, err := RevokeAll(root, testNow, 8)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.Check(8); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !r.AllDevices() {
|
||||
t.Fatal("expected an all-devices revocation")
|
||||
}
|
||||
if !r.Covers(oldDev.Cert) {
|
||||
t.Fatal("older device must be covered")
|
||||
}
|
||||
if r.Covers(newDev.Cert) {
|
||||
t.Fatal("device enrolled at the cutoff must stay valid")
|
||||
}
|
||||
if r.Covers(foreign.Cert) {
|
||||
t.Fatal("another identity's device must not be covered")
|
||||
}
|
||||
|
||||
// It must outlive every certificate it covers.
|
||||
if r.CertExpiresAt < oldDev.Cert.ExpiresAt {
|
||||
t.Fatal("revocation would be dropped before the covered certificates expire")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,12 @@ const (
|
||||
)
|
||||
|
||||
// Revocation voids a device: every signature made by Device is rejected from
|
||||
// now on. Nodes keep it until the device's certificate would have expired
|
||||
// anyway (CertExpiresAt). It carries proof-of-work so nobody can flood the
|
||||
// network with revocations for throwaway identities.
|
||||
// now on. With Device empty it voids every device of Author whose certificate
|
||||
// was issued before RevokedAt, which is what recovery on a new machine needs:
|
||||
// the owner only has the phrase, not the old device's key. Nodes keep it
|
||||
// until the certificates it covers would have expired anyway
|
||||
// (CertExpiresAt). It carries proof-of-work so nobody can flood the network
|
||||
// with revocations for throwaway identities.
|
||||
type Revocation struct {
|
||||
Version int `json:"v"`
|
||||
Author string `json:"author"`
|
||||
@@ -44,13 +47,27 @@ func Revoke(root *Identity, cert *Cert, at time.Time, difficulty int) (*Revocati
|
||||
if root.PublicKeyHex() != cert.Author {
|
||||
return nil, errors.New("identity: cert does not belong to this identity")
|
||||
}
|
||||
r := &Revocation{
|
||||
return mineRevocation(root, &Revocation{
|
||||
Version: RevocationVersion,
|
||||
Author: cert.Author,
|
||||
Device: cert.Device,
|
||||
RevokedAt: at.Unix(),
|
||||
CertExpiresAt: cert.ExpiresAt,
|
||||
}
|
||||
}, difficulty)
|
||||
}
|
||||
|
||||
// RevokeAll voids every device certified before at. Enroll the replacement
|
||||
// device with the same timestamp: certificates issued at or after at stay valid.
|
||||
func RevokeAll(root *Identity, at time.Time, difficulty int) (*Revocation, error) {
|
||||
return mineRevocation(root, &Revocation{
|
||||
Version: RevocationVersion,
|
||||
Author: root.PublicKeyHex(),
|
||||
RevokedAt: at.Unix(),
|
||||
CertExpiresAt: at.Add(MaxCertLifetime).Unix(), // older certs all expire before this
|
||||
}, difficulty)
|
||||
}
|
||||
|
||||
func mineRevocation(root *Identity, r *Revocation, difficulty int) (*Revocation, error) {
|
||||
for ; ; r.PoW++ {
|
||||
b, err := r.signingBytes()
|
||||
if err != nil {
|
||||
@@ -63,12 +80,26 @@ func Revoke(root *Identity, cert *Cert, at time.Time, difficulty int) (*Revocati
|
||||
}
|
||||
}
|
||||
|
||||
// AllDevices reports whether r covers every earlier device of the author.
|
||||
func (r *Revocation) AllDevices() bool { return r.Device == "" }
|
||||
|
||||
// Covers reports whether r voids the device certified by c.
|
||||
func (r *Revocation) Covers(c *Cert) bool {
|
||||
if r.Author != c.Author {
|
||||
return false
|
||||
}
|
||||
if r.AllDevices() {
|
||||
return c.IssuedAt < r.RevokedAt
|
||||
}
|
||||
return r.Device == c.Device
|
||||
}
|
||||
|
||||
// Check verifies structure, signature and proof-of-work.
|
||||
func (r *Revocation) Check(difficulty int) error {
|
||||
if r.Version != RevocationVersion {
|
||||
return errors.New("identity: unsupported revocation version")
|
||||
}
|
||||
if !isHex(r.Author, 64) || !isHex(r.Device, 64) {
|
||||
if !isHex(r.Author, 64) || (r.Device != "" && !isHex(r.Device, 64)) {
|
||||
return errors.New("identity: revocation keys must be 64 hex characters")
|
||||
}
|
||||
if r.PoW > 1<<53-1 {
|
||||
|
||||
Reference in New Issue
Block a user