Renew the device certificate from the browser, with the recovery phrase
Within the last 30 days the owner page offers it. The node signs a new certificate for the same device key (peer ID and listings stay), saves it, and signs again own listings that would outlive the old certificate. The phrase is used for one request and not kept; another identity's phrase and no-phrase identities are refused. Device.Current gives the certificate to sign with while a renewal happens. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -154,3 +154,107 @@ func TestRevokeAll(t *testing.T) {
|
||||
t.Fatal("revocation would be dropped before the covered certificates expire")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenewCertifiesTheSameDeviceKeyAgain(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
dev, err := Enroll(root, testNow, 30*24*time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first := dev.Current()
|
||||
if first != dev.Cert {
|
||||
t.Fatal("before a renewal the current certificate is the one the device has")
|
||||
}
|
||||
|
||||
later := testNow.Add(20 * 24 * time.Hour)
|
||||
renewed, err := dev.Renew(root, later, 365*24*time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := renewed.Check(); err != nil {
|
||||
t.Fatalf("the renewed certificate must verify: %v", err)
|
||||
}
|
||||
if renewed.Device != first.Device || renewed.Author != first.Author {
|
||||
t.Fatal("a renewal must keep the device key (and so the peer ID) and the identity")
|
||||
}
|
||||
if renewed.IssuedAt != later.Unix() || renewed.ExpiresAt != later.Add(365*24*time.Hour).Unix() {
|
||||
t.Fatalf("renewed cert runs %d to %d, want %d for a year", renewed.IssuedAt, renewed.ExpiresAt, later.Unix())
|
||||
}
|
||||
if dev.Current() != renewed {
|
||||
t.Fatal("Current must return the renewed certificate")
|
||||
}
|
||||
if dev.Cert != first {
|
||||
t.Fatal("the loaded certificate field must not change: other goroutines read it")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnlyTheIdentityThatCertifiedTheDeviceCanRenewIt(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
stranger, _, _ := NewRoot()
|
||||
dev, _ := Enroll(root, testNow, 30*24*time.Hour)
|
||||
before := dev.Current()
|
||||
|
||||
if _, err := dev.Renew(stranger, testNow, 365*24*time.Hour); err != ErrWrongIdentity {
|
||||
t.Fatalf("a different identity renewing: err = %v, want ErrWrongIdentity", err)
|
||||
}
|
||||
if dev.Current() != before {
|
||||
t.Fatal("a refused renewal must leave the certificate alone")
|
||||
}
|
||||
if _, err := dev.Renew(root, testNow, 400*24*time.Hour); err == nil {
|
||||
t.Fatal("a certificate longer than the maximum must be refused")
|
||||
}
|
||||
if dev.Current() != before {
|
||||
t.Fatal("a refused renewal must leave the certificate alone")
|
||||
}
|
||||
}
|
||||
|
||||
func TestARenewedCertificateIsWhatGetsSavedAndLoaded(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
dev, _ := Enroll(root, testNow, 30*24*time.Hour)
|
||||
dir := t.TempDir()
|
||||
if err := dev.Save(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
renewed, err := dev.Renew(root, testNow.Add(24*time.Hour), 365*24*time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := dev.SaveCert(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := LoadDevice(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded.Cert.ExpiresAt != renewed.ExpiresAt || loaded.Cert.Device != dev.Cert.Device {
|
||||
t.Fatalf("loaded cert expires %d, want %d: the renewal was not saved", loaded.Cert.ExpiresAt, renewed.ExpiresAt)
|
||||
}
|
||||
}
|
||||
|
||||
// Handlers sign while a renewal happens; run with -race.
|
||||
func TestRenewWhileOthersSign(t *testing.T) {
|
||||
root, _, _ := NewRoot()
|
||||
dev, _ := Enroll(root, testNow, 30*24*time.Hour)
|
||||
done := make(chan struct{})
|
||||
for i := 0; i < 4; i++ {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
default:
|
||||
if c := dev.Current(); c.Device != dev.Cert.Device {
|
||||
t.Error("a reader saw a certificate for another device")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
for i := 0; i < 50; i++ {
|
||||
if _, err := dev.Renew(root, testNow.Add(time.Duration(i)*time.Hour), 365*24*time.Hour); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
close(done)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user