Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation. Co-Authored-By: Craft Agent <agents-noreply@craft.do>
68 lines
2.4 KiB
Go
68 lines
2.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/pem"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLeaseBoundsAndCanonical(t *testing.T) {
|
|
now := time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC)
|
|
far := now.AddDate(10, 0, 0)
|
|
if got := boundedLease(now, far); !got.Equal(now.Add(14 * 24 * time.Hour)) {
|
|
t.Fatal("far-future entitlement gave permanent offline lease")
|
|
}
|
|
short := now.Add(time.Hour)
|
|
if !boundedLease(now, short).Equal(short) {
|
|
t.Fatal("lease exceeded entitlement")
|
|
}
|
|
p := leasePayload{"lease-v1", "00000000-0000-4000-8000-000000000001", "active", "2026-09-30T12:00:00Z", "2036-09-30T12:00:00Z", "2026-10-14T12:00:00Z"}
|
|
expected := "version=lease-v1\ninstance_id=00000000-0000-4000-8000-000000000001\nstate=active\nserver_time=2026-09-30T12:00:00Z\nentitlement_expires_at=2036-09-30T12:00:00Z\nlease_expires_at=2026-10-14T12:00:00Z"
|
|
if canonicalLease(p) != expected {
|
|
t.Fatal("canonical lease changed")
|
|
}
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
digest := sha256.Sum256([]byte(expected))
|
|
signature, err := ecdsa.SignASN1(rand.Reader, key, digest[:])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
der, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !ecdsa.VerifyASN1(&key.PublicKey, digest[:], signature) {
|
|
t.Fatal("signature failed")
|
|
}
|
|
_ = der
|
|
block, _ := pem.Decode([]byte("-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEz39VZCPp0fPgbDhKOM0jUHqzJAe3\n7GSZpcSe1TO9UGqv8BO3PBI8Th3Z9Fy5EdPaVggh8V/pjW/dcsUg9QbzWA==\n-----END PUBLIC KEY-----"))
|
|
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fixedSig, err := base64.RawStdEncoding.DecodeString("MEQCIFHuseC8F3iv6Jz9W3yywkacJ5qBOcDRJSNFLdFWS2w9AiBRZEmPGOzti3IJ0VOPccUOmmtVfWs6ZxtjcSDKJLH8BQ")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hex.EncodeToString(digest[:]) != "777150ed1107b51f17dd1ffaefac303310a32ec415e3d83a4b16cb05b9aac379" || !ecdsa.VerifyASN1(pub.(*ecdsa.PublicKey), digest[:], fixedSig) {
|
|
t.Fatal("published vector invalid")
|
|
}
|
|
changed := sha256.Sum256([]byte(expected + "\n"))
|
|
if ecdsa.VerifyASN1(pub.(*ecdsa.PublicKey), changed[:], fixedSig) {
|
|
t.Fatal("tampered vector accepted")
|
|
}
|
|
if leaseState("activated", "frozen", "revoked", "active", true, now.Add(-time.Hour), far, now) != "revoked" {
|
|
t.Fatal("revocation must dominate freeze")
|
|
}
|
|
}
|