parent:
7eeb8a0214368f4491f26f4cf37bd7bf6dc67cd3
nmyk <nick@nmyk.io>
2026-11-14T19:17:15-05:00
add repeal checking
diff --git a/DESIGN.md b/DESIGN.md
index f924b897218a69fd31c456ce5967afc103a3ff7c..acb20611de1719bca7cfb24cf92ac763789d9d91 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -333,7 +333,7 @@ | `government verify prog.gov` | Ratify and decode only; print the certificate of ratification; exit 0 if the program is lawful |
| `government tree prog.gov` | Print the policy tree, with `GOV` abbreviated, payloads decoded and notes shown |
| `government decompile prog.gov` | Print equivalent Lisp source, notes restored as comments |
| `government compile prog.gov.lisp -o prog.gov` | Run the transpiler (`-key`, `-author`, `-seed`, `-effective`, `-expires`, `-name`, `-source`) |
-| `government repeal prog.gov -depth i -o repeal.crl` | Write a CRL repealing depth i (`-key`, `-reason`, `-at`, `-next-update`) |
+| `government repeal prog.gov -depth i -o repeal.crl` | Write a CRL repealing depth i (`-key` or `-seed`, `-reason`, `-at`, `-issued`, `-next-update`; `-next-update` defaults to 9999-12-31, a permanent repeal) |
Shared flags: `-at <RFC 3339 time>`, `-anchor <cert or public key PEM>`, `-crl <file>` (repeatable), `-max-nodes <n>`.
@@ -416,7 +416,7 @@
- Revoking cert *i* repeals depth *i*, and with it the whole program.
- Per RFC 5280, a cert's revocation is published by its issuer. So depth *i* is repealed by a CRL signed by cert *i−1* (the anchor for depth 1). Every CA cert therefore gets `cRLSign` as well as `keyCertSign`.
- `government repeal prog.gov -depth 3 -reason superseded -o repeal.crl` writes that CRL. It needs the program's signing key (see Authorship). The reason is any RFC 5280 CRLReason, e.g. `superseded` or `cessationOfOperation`.
-- `government run -crl repeal.crl prog.gov` loads one or more CRL PEM blocks (the flag repeats). For each CRL it checks the issuer, its signature (`RevocationList.CheckSignatureFrom`), and that the check time lies between `thisUpdate` and `nextUpdate`.
+- `government run -crl repeal.crl prog.gov` loads one or more CRL PEM blocks (the flag repeats; `verify`, `tree` and `decompile` take it too). For each CRL it checks that its issuer is a cert of this program (else `E-FOREIGN`), its signature (`RevocationList.CheckSignatureFrom`, else `E-TAMPERED`), and that the check time is not past `nextUpdate`. A check time before `thisUpdate` is allowed: each entry's revocation time already says when the repeal applies, so a newer list can still answer whether the program was in force at an earlier `-at`.
- A path cert listed with a revocation time at or before the check time fails: `E-REPEALED: depth 3 repealed 2027-03-01 (superseded)`.
- A CRL past its `nextUpdate` is `E-STALE-REPEALS`. This is a deliberate GOVERNMENT rule: an outdated repeal list fails closed.
- A future revocation time gives a scheduled repeal. RFC 5280 treats that field as when revocation occurred, so honoring a future time is a GOVERNMENT-specific reading, noted as such.
diff --git a/cmd/government/inspect.go b/cmd/government/inspect.go
index 26571950732b0eb9aa50cb97aa5254b714c9b140..161f240cfb8471ea932dbe50c3fd198ba914684e 100644
--- a/cmd/government/inspect.go
+++ b/cmd/government/inspect.go
@@ -19,7 +19,7 @@ fs := flag.NewFlagSet("tree", flag.ContinueOnError)
fs.SetOutput(stderr)
lf := addLoadFlags(fs)
fs.Usage = func() {
- fmt.Fprintln(stderr, "usage: government tree prog.gov [-at time] [-anchor file] [-max-nodes n]")
+ fmt.Fprintln(stderr, "usage: government tree prog.gov [-at time] [-anchor file] [-crl file]... [-max-nodes n]")
fs.PrintDefaults()
}
path, data, ok := oneFile(fs, args, stderr)
@@ -27,7 +27,7 @@ if !ok {
return exitInvalid
}
opts := lf.programOptions()
- certs, err := ratify.Ratify(data, ratify.Options{At: opts.At, Pin: opts.Pin})
+ certs, err := ratify.Ratify(data, ratify.Options{At: opts.At, Pin: opts.Pin, CRLs: opts.CRLs})
if err != nil {
fmt.Fprintf(stderr, "%s: %v\n", path, err)
return exitInvalid
@@ -46,7 +46,7 @@ fs := flag.NewFlagSet("decompile", flag.ContinueOnError)
fs.SetOutput(stderr)
lf := addLoadFlags(fs)
fs.Usage = func() {
- fmt.Fprintln(stderr, "usage: government decompile prog.gov [-at time] [-anchor file] [-max-nodes n]")
+ fmt.Fprintln(stderr, "usage: government decompile prog.gov [-at time] [-anchor file] [-crl file]... [-max-nodes n]")
fs.PrintDefaults()
}
path, data, ok := oneFile(fs, args, stderr)
diff --git a/cmd/government/load.go b/cmd/government/load.go
index 1bef4e13ee9adeffa4a09ed6c6b662188e477234..5b9290a7e1741ec8c9ab4028d479a9a3ca1cd1ac 100644
--- a/cmd/government/load.go
+++ b/cmd/government/load.go
@@ -5,6 +5,7 @@ "flag"
"fmt"
"io"
"os"
+ "strings"
"government/policy"
"government/program"
@@ -14,19 +15,47 @@ // loadFlags are the flags every command that reads a .gov file shares.
type loadFlags struct {
at *atFlag
anchor *anchorFlag
+ crls *crlFlag
maxNodes *int
}
func addLoadFlags(fs *flag.FlagSet) loadFlags {
- return loadFlags{
+ lf := loadFlags{
at: addAtFlag(fs),
anchor: addAnchorFlag(fs),
+ crls: &crlFlag{},
maxNodes: fs.Int("max-nodes", policy.DefaultMaxNodes, "refuse programs whose policy graph needs more than `n` nodes"),
}
+ fs.Var(lf.crls, "crl", "apply the repeal list in `file` (repeatable)")
+ return lf
+}
+
+// crlFlag is -crl, which may be given several times: each is a file of
+// repeal lists to check the program against.
+type crlFlag struct {
+ paths []string
+ data [][]byte
+}
+
+func (c *crlFlag) String() string {
+ if c == nil {
+ return ""
+ }
+ return strings.Join(c.paths, ", ")
+}
+
+func (c *crlFlag) Set(path string) error {
+ data, err := os.ReadFile(path)
+ if err != nil {
+ return err
+ }
+ c.paths = append(c.paths, path)
+ c.data = append(c.data, data)
+ return nil
}
func (lf loadFlags) programOptions() program.Options {
- return program.Options{At: lf.at.Time(), Pin: lf.anchor.Pin(), MaxNodes: *lf.maxNodes}
+ return program.Options{At: lf.at.Time(), Pin: lf.anchor.Pin(), CRLs: lf.crls.data, MaxNodes: *lf.maxNodes}
}
// oneFile parses a command's arguments, which must name exactly one .gov
diff --git a/cmd/government/repeal.go b/cmd/government/repeal.go
index 154a2d92d9aac363d98b3c2d9655c5460f86b0a9..46a72ace76e9414c0ddeb8feded1e811de6e31bb 100644
--- a/cmd/government/repeal.go
+++ b/cmd/government/repeal.go
@@ -21,7 +21,7 @@ depth := fs.Int("depth", 0, "repeal the cert at depth `i` (1 to the program's depth)")
out := fs.String("o", "", "write the CRL to `file` instead of standard output")
keyFile := fs.String("key", "", "the Ed25519 PKCS#8 private key `file` the program was compiled with")
seedHex := fs.String("seed", "", "the `hex` seed the program was compiled with, instead of a key file")
- reason := fs.String("reason", "unspecified", "the CRLReason: `name` is one of "+strings.Join(lisp2gov.ReasonNames(), ", "))
+ reason := fs.String("reason", "unspecified", "the CRLReason: `name` is one of "+strings.Join(ratify.ReasonNames(), ", "))
var at, issued, nextUpdate dateFlag
fs.Var(&at, "at", "the repeal takes effect at this `time` (default now); a future time schedules it")
fs.Var(&issued, "issued", "the CRL's issue `time` (thisUpdate; default now)")
@@ -35,9 +35,9 @@ path, data, ok := oneFile(fs, args, stderr)
if !ok {
return exitInvalid
}
- code, known := lisp2gov.Reasons[*reason]
+ code, known := ratify.Reasons[*reason]
if !known {
- fmt.Fprintf(stderr, "unknown reason %q; want one of %s\n", *reason, strings.Join(lisp2gov.ReasonNames(), ", "))
+ fmt.Fprintf(stderr, "unknown reason %q; want one of %s\n", *reason, strings.Join(ratify.ReasonNames(), ", "))
return exitInvalid
}
seed, err := signingSeed(*keyFile, *seedHex)
diff --git a/cmd/government/repeal_test.go b/cmd/government/repeal_test.go
index 2e1c5afe3e0c2ff53a220bfd13fcad9ce25489e1..3579ec13e08ad969932c26d6a77f72d5ca4ce4ef 100644
--- a/cmd/government/repeal_test.go
+++ b/cmd/government/repeal_test.go
@@ -78,3 +78,33 @@ certs = append(certs, c)
}
return certs
}
+
+// TestRepealedProgramIsRefused is the whole round: compile as an author,
+// repeal a depth, and watch every loading command refuse the program.
+func TestRepealedProgramIsRefused(t *testing.T) {
+ dir := t.TempDir()
+ key := writeKey(t, dir, "ada", 1)
+ src := writeSource(t, "law", "(+ 1 (* 2 3))")
+ gov := filepath.Join(dir, "law.gov")
+ runCmd("compile", src, "-o", gov, "-key", key)
+ crl := filepath.Join(dir, "repeal.crl")
+ if code, _, stderr := runCmd("repeal", gov, "-depth", "1", "-key", key, "-reason", "superseded",
+ "-at", "2027-03-01", "-issued", "2027-01-01", "-o", crl); code != exitOK {
+ t.Fatal(stderr)
+ }
+
+ before := "2027-02-01T00:00:00Z"
+ after := "2027-04-01T00:00:00Z"
+ if code, stdout, stderr := runCmd("run", gov, "-crl", crl, "-at", before); code != exitOK || stdout != "7\n" {
+ t.Errorf("before the repeal: exit %d, %q, %s", code, stdout, stderr)
+ }
+ for _, cmd := range []string{"run", "verify", "tree", "decompile"} {
+ code, stdout, stderr := runCmd(cmd, gov, "-crl", crl, "-at", after)
+ if code != exitInvalid || stdout != "" || !strings.Contains(stderr, "E-REPEALED: depth 1 repealed 2027-03-01 (superseded)") {
+ t.Errorf("%s after the repeal: exit %d, stdout %q, stderr %q", cmd, code, stdout, stderr)
+ }
+ }
+ if code, _, stderr := runCmd("run", gov, "-crl", filepath.Join(dir, "missing.crl")); code != exitInvalid || !strings.Contains(stderr, "no such file") {
+ t.Errorf("missing CRL file: exit %d, %s", code, stderr)
+ }
+}
diff --git a/cmd/government/run.go b/cmd/government/run.go
index 4750a5dbc1c7612519f09f6b54c894bcf42a99ff..59a27e9bf77c0ecdb5343e73893b87cb9acacac4 100644
--- a/cmd/government/run.go
+++ b/cmd/government/run.go
@@ -13,7 +13,7 @@ fs := flag.NewFlagSet("run", flag.ContinueOnError)
fs.SetOutput(stderr)
lf := addLoadFlags(fs)
fs.Usage = func() {
- fmt.Fprintln(stderr, "usage: government run prog.gov [-at time] [-anchor file] [-max-nodes n]")
+ fmt.Fprintln(stderr, "usage: government run prog.gov [-at time] [-anchor file] [-crl file]... [-max-nodes n]")
fs.PrintDefaults()
}
path, data, ok := oneFile(fs, args, stderr)
diff --git a/cmd/government/verify.go b/cmd/government/verify.go
index e59e4d6036ff6591436359c8d101b5fb58fbf5d1..f125e7a02aa07ba445b35cad0cc40a3f662dea3c 100644
--- a/cmd/government/verify.go
+++ b/cmd/government/verify.go
@@ -22,7 +22,7 @@ fs := flag.NewFlagSet("verify", flag.ContinueOnError)
fs.SetOutput(stderr)
lf := addLoadFlags(fs)
fs.Usage = func() {
- fmt.Fprintln(stderr, "usage: government verify prog.gov [-at time] [-anchor file] [-max-nodes n]")
+ fmt.Fprintln(stderr, "usage: government verify prog.gov [-at time] [-anchor file] [-crl file]... [-max-nodes n]")
fs.PrintDefaults()
}
path, data, ok := oneFile(fs, args, stderr)
diff --git a/lisp2gov/repeal.go b/lisp2gov/repeal.go
index 6d340b04b09f069b5bc179ee4c760949c19ba3a5..50c7700658406cf6b403ac5f51453ddb6ed4af84 100644
--- a/lisp2gov/repeal.go
+++ b/lisp2gov/repeal.go
@@ -7,42 +7,16 @@ "crypto/x509"
"encoding/pem"
"fmt"
"math/big"
- "slices"
"time"
)
-// Reasons are RFC 5280's CRLReason codes (section 5.3.1), by name. Code 7
-// is unused in the RFC.
-var Reasons = map[string]int{
- "unspecified": 0,
- "keyCompromise": 1,
- "cACompromise": 2,
- "affiliationChanged": 3,
- "superseded": 4,
- "cessationOfOperation": 5,
- "certificateHold": 6,
- "removeFromCRL": 8,
- "privilegeWithdrawn": 9,
- "aACompromise": 10,
-}
-
-// ReasonNames lists the reason names in code order, for messages.
-func ReasonNames() []string {
- names := make([]string, 0, len(Reasons))
- for n := range Reasons {
- names = append(names, n)
- }
- slices.SortFunc(names, func(a, b string) int { return Reasons[a] - Reasons[b] })
- return names
-}
-
type RepealOptions struct {
// Seed is the key the program was compiled with; nil means
// DefaultSeed. Repealing needs it because the cert being repealed is
// revoked by its issuer, the cert one depth up, whose key is derived
// from this seed.
Seed *[32]byte
- // Reason is a CRLReason code.
+ // Reason is a CRLReason code (see ratify.Reasons).
Reason int
// At is when the repeal takes effect: the revocation time. A time in
// the future schedules the repeal.
diff --git a/lisp2gov/repeal_test.go b/lisp2gov/repeal_test.go
index 437e9b652e4e447c4a6f1f77a01730648de03ce9..6bde2ea0bb62c3ddd6920e301aa24081c105b621 100644
--- a/lisp2gov/repeal_test.go
+++ b/lisp2gov/repeal_test.go
@@ -33,7 +33,7 @@ t.Fatal(err)
}
certs := parsePEM(t, out)
for depth := 1; depth < len(certs); depth++ {
- data, err := Repeal(certs, depth, RepealOptions{Reason: Reasons["superseded"], At: issued, Issued: issued, NextUpdate: never})
+ data, err := Repeal(certs, depth, RepealOptions{Reason: 4, At: issued, Issued: issued, NextUpdate: never})
if err != nil {
t.Fatalf("depth %d: %v", depth, err)
}
@@ -94,10 +94,3 @@ t.Errorf("%s: %v, want %q", tt.name, err, tt.want)
}
}
}
-
-func TestReasonNames(t *testing.T) {
- names := ReasonNames()
- if names[0] != "unspecified" || names[len(names)-1] != "aACompromise" || len(names) != 10 {
- t.Errorf("ReasonNames = %v", names)
- }
-}
diff --git a/program/program.go b/program/program.go
index 8ff29e8925b79e0191ba1f12e9b2ad8b3c61ad9f..c430330d50bffc30920bedbbf47f32680848a8a5 100644
--- a/program/program.go
+++ b/program/program.go
@@ -34,6 +34,7 @@
type Options struct {
At time.Time
Pin ratify.Pin
+ CRLs [][]byte
MaxNodes int
}
@@ -59,7 +60,7 @@
// Load runs every check on a .gov file and decodes it. Any error it returns
// means the program may not run: a ratify, policy or decode error.
func Load(data []byte, opts Options) (*Program, error) {
- certs, err := ratify.Ratify(data, ratify.Options{At: opts.At, Pin: opts.Pin})
+ certs, err := ratify.Ratify(data, ratify.Options{At: opts.At, Pin: opts.Pin, CRLs: opts.CRLs})
if err != nil {
return nil, err
}
diff --git a/ratify/error.go b/ratify/error.go
index 0d3d03fc98d6031cd7f9f5ecaf683837af118e0d..b6aa80be06d5d5021499f1c672be2661a19c91ea 100644
--- a/ratify/error.go
+++ b/ratify/error.go
@@ -16,8 +16,13 @@ ErrNotInForce = "E-NOT-IN-FORCE"
// ErrLapsed: the check time is after the program expired.
ErrLapsed = "E-LAPSED"
// ErrForeign: the program is ratified by a key other than the pinned
- // one.
+ // one, or a repeal list comes from another program.
ErrForeign = "E-FOREIGN"
+ // ErrRepealed: a CRL from the program's own chain repeals a depth.
+ ErrRepealed = "E-REPEALED"
+ // ErrStaleRepeals: a repeal list is past its nextUpdate, so it can no
+ // longer be trusted to be complete.
+ ErrStaleRepeals = "E-STALE-REPEALS"
)
type Error struct {
diff --git a/ratify/ratify.go b/ratify/ratify.go
index 23b066a7edf8c5bdc45598621636cc653627764f..17a6c316843539db6a444963b772d9a2ee8e0b9f 100644
--- a/ratify/ratify.go
+++ b/ratify/ratify.go
@@ -12,6 +12,8 @@ // Pin, if set, is the only key the program's anchor may have. Without
// one, the anchor inside the file is trusted, so ratification checks
// that the program is internally consistent, not who wrote it.
Pin Pin
+ // CRLs are repeal lists to apply, as each file's bytes.
+ CRLs [][]byte
}
// Ratify loads a .gov file and runs every ratification check that comes
@@ -34,6 +36,17 @@ if err := CheckChain(certs); err != nil {
return nil, err
}
if err := CheckValidity(certs, opts.At); err != nil {
+ return nil, err
+ }
+ var crls []*x509.RevocationList
+ for _, data := range opts.CRLs {
+ more, err := LoadCRLs(data)
+ if err != nil {
+ return nil, err
+ }
+ crls = append(crls, more...)
+ }
+ if err := CheckRepeals(certs, crls, opts.At); err != nil {
return nil, err
}
return certs, nil
diff --git a/ratify/repeal.go b/ratify/repeal.go
new file mode 100644
index 0000000000000000000000000000000000000000..2ec3368c1c80d73d63281e40a1db191577299da7
--- /dev/null
+++ b/ratify/repeal.go
@@ -0,0 +1,114 @@
+package ratify
+
+import (
+ "bytes"
+ "crypto/x509"
+ "encoding/pem"
+ "fmt"
+ "slices"
+ "time"
+)
+
+// Reasons are RFC 5280's CRLReason codes (section 5.3.1), by name. Code 7
+// is unused in the RFC.
+var Reasons = map[string]int{
+ "unspecified": 0,
+ "keyCompromise": 1,
+ "cACompromise": 2,
+ "affiliationChanged": 3,
+ "superseded": 4,
+ "cessationOfOperation": 5,
+ "certificateHold": 6,
+ "removeFromCRL": 8,
+ "privilegeWithdrawn": 9,
+ "aACompromise": 10,
+}
+
+// ReasonNames lists the reason names in code order.
+func ReasonNames() []string {
+ names := make([]string, 0, len(Reasons))
+ for n := range Reasons {
+ names = append(names, n)
+ }
+ slices.SortFunc(names, func(a, b string) int { return Reasons[a] - Reasons[b] })
+ return names
+}
+
+func reasonName(code int) string {
+ for name, c := range Reasons {
+ if c == code {
+ return name
+ }
+ }
+ return fmt.Sprintf("reason %d", code)
+}
+
+// LoadCRLs reads the "X509 CRL" PEM blocks in data, one file's worth.
+func LoadCRLs(data []byte) ([]*x509.RevocationList, error) {
+ var crls []*x509.RevocationList
+ rest := data
+ for {
+ var block *pem.Block
+ block, rest = pem.Decode(rest)
+ if block == nil {
+ break
+ }
+ if block.Type != "X509 CRL" {
+ return nil, errorf(ErrMalformed, "a repeal list holds X509 CRL blocks, not %s", block.Type)
+ }
+ crl, err := x509.ParseRevocationList(block.Bytes)
+ if err != nil {
+ return nil, errorf(ErrMalformed, "repeal list: %v", err)
+ }
+ crls = append(crls, crl)
+ }
+ if len(crls) == 0 {
+ return nil, errorf(ErrMalformed, "no X509 CRL block found")
+ }
+ return crls, nil
+}
+
+// CheckRepeals applies CRLs to a chain in depth order, at time at.
+//
+// Checking revocation is a trust decision of its own: the chain says the
+// program was ratified, and a CRL can say it has since been struck down.
+// GOVERNMENT fails closed at every step. A CRL that is not for this
+// program, is not signed by its issuer, or has gone stale stops the
+// program rather than being skipped, since a list that cannot be trusted
+// cannot be trusted to say "nothing is repealed" either.
+func CheckRepeals(certs []*x509.Certificate, crls []*x509.RevocationList, at time.Time) error {
+ for _, crl := range crls {
+ depth := slices.IndexFunc(certs, func(c *x509.Certificate) bool {
+ return bytes.Equal(c.RawSubject, crl.RawIssuer)
+ })
+ if depth < 0 {
+ return errorf(ErrForeign, "a repeal list from %s, which is not part of this program", crl.Issuer)
+ }
+ // CheckSignatureFrom also requires the issuer to have cRLSign, so
+ // the last cert, which issues nothing, cannot publish repeals.
+ if err := crl.CheckSignatureFrom(certs[depth]); err != nil {
+ return errorf(ErrTampered, "the repeal list from depth %d fails its signature check: %v", depth, err)
+ }
+ if at.After(crl.NextUpdate) {
+ return errorf(ErrStaleRepeals, "the repeal list from depth %d went stale %s", depth, stamp(crl.NextUpdate))
+ }
+ if depth+1 >= len(certs) {
+ continue
+ }
+ // A serial number is unique only among one issuer's certs, so the
+ // only cert this CRL can name is the one its issuer issued.
+ child := certs[depth+1]
+ for _, e := range crl.RevokedCertificateEntries {
+ if e.SerialNumber.Cmp(child.SerialNumber) != 0 {
+ continue
+ }
+ // A revocation time later than the check time is a scheduled
+ // repeal. RFC 5280 reads the field as when the revocation
+ // happened; honouring a future one is GOVERNMENT's own reading.
+ if !e.RevocationTime.After(at) {
+ return errorf(ErrRepealed, "depth %d repealed %s (%s)", depth+1, e.RevocationTime.UTC().Format(time.DateOnly), reasonName(e.ReasonCode))
+ }
+ }
+ }
+ return nil
+}
diff --git a/ratify/repeal_test.go b/ratify/repeal_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..1f4f3a58599c15c1d5b7ec83c1339bbeafa52c57
--- /dev/null
+++ b/ratify/repeal_test.go
@@ -0,0 +1,104 @@
+package ratify
+
+import (
+ "crypto/ed25519"
+ "crypto/rand"
+ "crypto/x509"
+ "encoding/pem"
+ "math/big"
+ "testing"
+ "time"
+
+ "government/lisp2gov"
+)
+
+var (
+ feb = time.Date(2027, 2, 1, 0, 0, 0, 0, time.UTC)
+ mar = time.Date(2027, 3, 1, 0, 0, 0, 0, time.UTC)
+ apr = time.Date(2027, 4, 1, 0, 0, 0, 0, time.UTC)
+ jun = time.Date(2027, 6, 1, 0, 0, 0, 0, time.UTC)
+ jul = time.Date(2027, 7, 1, 0, 0, 0, 0, time.UTC)
+ ever = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
+)
+
+func program(t *testing.T, name string, seed *[32]byte) ([]byte, []*x509.Certificate) {
+ t.Helper()
+ data, err := lisp2gov.Compile("(+ 1 (* 2 3))", lisp2gov.Options{Name: name, Seed: seed})
+ if err != nil {
+ t.Fatal(err)
+ }
+ certs, err := Load(data)
+ if err != nil {
+ t.Fatal(err)
+ }
+ certs, err = Order(certs)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return data, certs
+}
+
+func repealOf(t *testing.T, certs []*x509.Certificate, depth int, seed *[32]byte, at, next time.Time) []byte {
+ t.Helper()
+ crl, err := lisp2gov.Repeal(certs, depth, lisp2gov.RepealOptions{Seed: seed, Reason: Reasons["superseded"], At: at, Issued: feb, NextUpdate: next})
+ if err != nil {
+ t.Fatal(err)
+ }
+ return crl
+}
+
+func TestRepeals(t *testing.T) {
+ data, certs := program(t, "p", nil)
+ other := [32]byte{8}
+ _, impostor := program(t, "p", &other) // same names, another key
+ _, foreign := program(t, "q", &other) // another program altogether
+
+ // A CRL from the right issuer that names no cert of this program.
+ anchorKey := ed25519.NewKeyFromSeed(lisp2gov.DefaultSeed[:])
+ der, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{
+ Number: big.NewInt(1), ThisUpdate: feb, NextUpdate: ever,
+ RevokedCertificateEntries: []x509.RevocationListEntry{{SerialNumber: big.NewInt(99), RevocationTime: feb}},
+ }, certs[0], anchorKey)
+ if err != nil {
+ t.Fatal(err)
+ }
+ unrelated := pem.EncodeToMemory(&pem.Block{Type: "X509 CRL", Bytes: der})
+
+ tests := []struct {
+ name string
+ crls [][]byte
+ at time.Time
+ code string
+ msg string
+ }{
+ {"no repeals", nil, apr, "", ""},
+ {"repeal depth 1", [][]byte{repealOf(t, certs, 1, nil, mar, ever)}, apr, ErrRepealed, "depth 1 repealed 2027-03-01 (superseded)"},
+ {"repeal depth 2", [][]byte{repealOf(t, certs, 2, nil, mar, ever)}, apr, ErrRepealed, "depth 2 repealed"},
+ {"repeal depth 3", [][]byte{repealOf(t, certs, 3, nil, mar, ever)}, apr, ErrRepealed, "depth 3 repealed"},
+ {"scheduled, before its date", [][]byte{repealOf(t, certs, 2, nil, mar, ever)}, feb, "", ""},
+ {"scheduled, on its date", [][]byte{repealOf(t, certs, 2, nil, mar, ever)}, mar, ErrRepealed, "depth 2 repealed"},
+ {"scheduled, after its date", [][]byte{repealOf(t, certs, 2, nil, mar, ever)}, apr, ErrRepealed, "depth 2 repealed"},
+ {"stale", [][]byte{repealOf(t, certs, 2, nil, ever, jun)}, jul, ErrStaleRepeals, "depth 1 went stale 2027-06-01T00:00:00Z"},
+ {"fresh until nextUpdate", [][]byte{repealOf(t, certs, 2, nil, ever, jun)}, jun, "", ""},
+ {"signed by the wrong key", [][]byte{repealOf(t, impostor, 2, &other, mar, ever)}, apr, ErrTampered, "repeal list from depth 1 fails"},
+ {"from another program", [][]byte{repealOf(t, foreign, 2, &other, mar, ever)}, apr, ErrForeign, "not part of this program"},
+ {"names no cert of ours", [][]byte{unrelated}, apr, "", ""},
+ {"one of several repeals", [][]byte{unrelated, repealOf(t, certs, 3, nil, mar, ever)}, apr, ErrRepealed, "depth 3 repealed"},
+ {"not a CRL", [][]byte{data}, apr, ErrMalformed, "not CERTIFICATE"},
+ {"empty file", [][]byte{nil}, apr, ErrMalformed, "no X509 CRL"},
+ }
+ for _, tt := range tests {
+ _, err := Ratify(data, Options{At: tt.at, CRLs: tt.crls})
+ checkCode(t, tt.name, err, tt.code, tt.msg)
+ }
+}
+
+func TestReasonNames(t *testing.T) {
+ names := ReasonNames()
+ if names[0] != "unspecified" || names[len(names)-1] != "aACompromise" || len(names) != 10 {
+ t.Errorf("ReasonNames = %v", names)
+ }
+ if reasonName(4) != "superseded" || reasonName(7) != "reason 7" {
+ t.Errorf("reasonName(4) = %s, reasonName(7) = %s", reasonName(4), reasonName(7))
+ }
+}