Files
allmende/internal/identity/revocation.go
T
nilsandClaude Sonnet 5.5 918e16ed37 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>
2026-11-17 18:03:46 +01:00

127 lines
3.9 KiB
Go

package identity
import (
"encoding/hex"
"errors"
"time"
"git.stinnesbeck.com/nils/allmende/internal/jcs"
"git.stinnesbeck.com/nils/allmende/internal/pow"
)
const (
RevocationVersion = 1
revocationContext = "allmende-revocation-v1\n"
)
// Revocation voids a device: every signature made by Device is rejected from
// 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"`
Device string `json:"device"`
RevokedAt int64 `json:"revoked_at"`
CertExpiresAt int64 `json:"cert_expires_at"`
PoW uint64 `json:"pow"`
Sig string `json:"sig,omitempty"`
}
func (r *Revocation) signingBytes() ([]byte, error) {
cp := *r
cp.Sig = ""
b, err := jcs.Marshal(&cp)
if err != nil {
return nil, err
}
return append([]byte(revocationContext), b...), nil
}
// Revoke signs a revocation of the device named in cert, mining the
// proof-of-work to the given difficulty.
func Revoke(root *Identity, cert *Cert, at time.Time, difficulty int) (*Revocation, error) {
if root.PublicKeyHex() != cert.Author {
return nil, errors.New("identity: cert does not belong to this identity")
}
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 {
return nil, err
}
if pow.LeadingZeroBits(b) >= difficulty {
r.Sig = hex.EncodeToString(root.Sign(b))
return r, nil
}
}
}
// 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) || (r.Device != "" && !isHex(r.Device, 64)) {
return errors.New("identity: revocation keys must be 64 hex characters")
}
if r.PoW > 1<<53-1 {
return errors.New("identity: number out of range")
}
sig, err := hex.DecodeString(r.Sig)
if err != nil || len(sig) != 64 {
return errors.New("identity: malformed revocation signature")
}
b, err := r.signingBytes()
if err != nil {
return err
}
if !Verify(r.Author, b, sig) {
return errors.New("identity: bad revocation signature")
}
if pow.LeadingZeroBits(b) < difficulty {
return errors.New("identity: insufficient proof-of-work")
}
return nil
}
// Bytes returns the canonical encoding including the signature.
func (r *Revocation) Bytes() ([]byte, error) { return jcs.Marshal(r) }