@ 2dbe47969d09d4856796bbf662b4e11c74a7f4fe | history
package ratify
import (
"crypto/x509"
"encoding/pem"
"testing"
"time"
"government/lisp2gov"
)
func pemOf(typ string, der []byte) []byte {
return pem.EncodeToMemory(&pem.Block{Type: typ, Bytes: der})
}
func encodeChain(certs []*x509.Certificate) []byte {
var out []byte
for _, c := range certs {
out = append(out, pemOf("CERTIFICATE", c.Raw)...)
}
return out
}
func mustPin(t *testing.T, data []byte) Pin {
t.Helper()
pin, err := LoadPin(data)
if err != nil {
t.Fatal(err)
}
return pin
}
func TestPinSurvivesReratification(t *testing.T) {
// Same keys, different dates and a different program: a new anchor
// cert, but the same anchor key.
v1 := testChain(t, 2, nil)
v2 := testChain(t, 3, func(d int, tmpl *x509.Certificate) {
tmpl.NotBefore = time.Date(2030, 1, 1, 0, 0, 0, 0, time.UTC)
tmpl.Subject.Organization = []string{"amended"}
})
if string(v1[0].Raw) == string(v2[0].Raw) {
t.Fatal("the two anchors are the same cert")
}
for name, pin := range map[string]Pin{
"anchor cert": mustPin(t, pemOf("CERTIFICATE", v1[0].Raw)),
"public key": mustPin(t, pemOf("PUBLIC KEY", v1[0].RawSubjectPublicKeyInfo)),
"whole .gov": mustPin(t, encodeChain(v1)),
"shuffled .gov": mustPin(t, encodeChain([]*x509.Certificate{v1[2], v1[0], v1[1]})),
} {
if err := CheckPin(v2[0], pin); err != nil {
t.Errorf("pin from %s: %v", name, err)
}
}
}
func TestPinRejectsAnotherSeed(t *testing.T) {
seed := [32]byte{42}
mine, err := lisp2gov.Compile("(+ 1 2)", lisp2gov.Options{})
if err != nil {
t.Fatal(err)
}
theirs, err := lisp2gov.Compile("(+ 1 2)", lisp2gov.Options{Seed: &seed})
if err != nil {
t.Fatal(err)
}
pin := mustPin(t, mine)
certs, err := Load(theirs)
if err != nil {
t.Fatal(err)
}
checkCode(t, "another seed", CheckPin(certs[0], pin), ErrForeign, "not the pinned key "+Fingerprint(pin))
}
func TestLoadPinRejects(t *testing.T) {
for name, data := range map[string][]byte{
"empty": nil,
"not PEM": []byte("hello"),
"private key": pemOf("PRIVATE KEY", []byte{1, 2, 3}),
"bad key": pemOf("PUBLIC KEY", []byte{1, 2, 3}),
"bad cert": pemOf("CERTIFICATE", []byte{1, 2, 3}),
} {
if _, err := LoadPin(data); err == nil {
t.Errorf("%s: accepted as a pin", name)
}
}
}
// TestRatify is Phase 4's milestone: a good program is accepted, and a
// tampered, expired or foreign one is refused.
func TestRatify(t *testing.T) {
good, err := lisp2gov.Compile("(define sq (lambda (x) (* x x))) (sq 12)", lisp2gov.Options{})
if err != nil {
t.Fatal(err)
}
now := time.Date(2026, 11, 14, 0, 0, 0, 0, time.UTC)
pin := mustPin(t, good)
certs, err := Ratify(good, Options{At: now, Pin: pin})
if err != nil {
t.Fatalf("good program: %v", err)
}
if len(certs) != 5 {
t.Errorf("%d certs, want 5", len(certs))
}
loaded, _ := Load(good)
tampered := append([]*x509.Certificate(nil), loaded...)
tampered[3] = flipByte(t, loaded[3], loaded[3].Signature)
seed := [32]byte{42}
foreign, err := lisp2gov.Compile("(define sq (lambda (x) (* x x))) (sq 12)", lisp2gov.Options{Seed: &seed})
if err != nil {
t.Fatal(err)
}
checkCode(t, "tampered", ratifyErr(encodeChain(tampered), Options{At: now}), ErrTampered, "depth 3")
checkCode(t, "expired", ratifyErr(good, Options{At: time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC)}), ErrLapsed, "expired 9999-12-31T23:59:59Z")
checkCode(t, "not yet in force", ratifyErr(good, Options{At: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC)}), ErrNotInForce, "takes effect 2000-01-01")
checkCode(t, "foreign", ratifyErr(foreign, Options{At: now, Pin: pin}), ErrForeign, "not the pinned key")
checkCode(t, "foreign without a pin", ratifyErr(foreign, Options{At: now}), "", "")
}
func ratifyErr(data []byte, opts Options) error {
_, err := Ratify(data, opts)
return err
}