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>
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestActivationCanonicalSignature(t *testing.T) {
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
der, err := x509.MarshalECPrivateKey(key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := readSigningKey(string(pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: der})))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
start := time.Date(2026, 9, 30, 0, 0, 0, 0, time.UTC)
|
|
end := start.AddDate(1, 0, 0)
|
|
canonical := canonicalCertificatePayload("license-v1", "00000000-0000-4000-8000-000000000001", 1, 2, 3, start, end)
|
|
expected := "version=license-v1\ninstance_id=00000000-0000-4000-8000-000000000001\nlicense_id=1\ncustomer_id=2\nproduct_id=3\nstarts_at=2026-09-30T00:00:00Z\nexpires_at=2027-09-30T00:00:00Z"
|
|
if canonical != expected {
|
|
t.Fatal("canonical payload mismatch")
|
|
}
|
|
sum := sha256.Sum256([]byte(canonical))
|
|
sig, err := ecdsa.SignASN1(rand.Reader, loaded, sum[:])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !ecdsa.VerifyASN1(&loaded.PublicKey, sum[:], sig) {
|
|
t.Fatal("signature not valid")
|
|
}
|
|
modified := sha256.Sum256([]byte(canonical + "x"))
|
|
if ecdsa.VerifyASN1(&loaded.PublicKey, modified[:], sig) {
|
|
t.Fatal("modified payload accepted")
|
|
}
|
|
if _, err = readSigningKey(""); err == nil {
|
|
t.Fatal("missing key accepted")
|
|
}
|
|
}
|