parent:
37b1f5914992b1c2410e634b80cb352b72e7f793
nmyk <nick@nmyk.io>
2026-11-14T16:58:17-05:00
ratify: add validity time windows
diff --git a/cmd/government/at.go b/cmd/government/at.go
new file mode 100644
index 0000000000000000000000000000000000000000..e06992b4da254c58e43b969407d81ba717748ba5
--- /dev/null
+++ b/cmd/government/at.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "time"
+)
+
+// atFlag is -at, shared by run, verify and repeal: the moment at which a
+// program's validity windows and repeals are checked. It defaults to now,
+// and setting it is how you ask whether a program was, or will be, in
+// force at some other time.
+type atFlag struct {
+ t time.Time
+}
+
+func addAtFlag(fs *flag.FlagSet) *atFlag {
+ a := &atFlag{t: time.Now().UTC()}
+ fs.Var(a, "at", "check validity and repeals at this RFC 3339 `time` (default now), e.g. 2027-06-01T00:00:00Z")
+ return a
+}
+
+func (a *atFlag) String() string {
+ if a == nil || a.t.IsZero() {
+ return ""
+ }
+ return a.t.Format(time.RFC3339)
+}
+
+func (a *atFlag) Set(s string) error {
+ t, err := time.Parse(time.RFC3339, s)
+ if err != nil {
+ return fmt.Errorf("want an RFC 3339 time such as 2027-06-01T00:00:00Z")
+ }
+ a.t = t.UTC()
+ return nil
+}
+
+func (a *atFlag) Time() time.Time { return a.t }
diff --git a/cmd/government/at_test.go b/cmd/government/at_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..ef6ff60bd1e9c3bf202545412b6072bd1a79b786
--- /dev/null
+++ b/cmd/government/at_test.go
@@ -0,0 +1,38 @@
+package main
+
+import (
+ "flag"
+ "io"
+ "testing"
+ "time"
+)
+
+func TestAtFlag(t *testing.T) {
+ parse := func(args ...string) (*atFlag, error) {
+ fs := flag.NewFlagSet("test", flag.ContinueOnError)
+ fs.SetOutput(io.Discard)
+ at := addAtFlag(fs)
+ return at, fs.Parse(args)
+ }
+
+ at, err := parse()
+ if err != nil || time.Since(at.Time()) > time.Minute {
+ t.Errorf("default: %v, %v; want about now", at.Time(), err)
+ }
+
+ at, err = parse("-at", "2027-06-01T00:00:00Z")
+ if err != nil || !at.Time().Equal(time.Date(2027, 6, 1, 0, 0, 0, 0, time.UTC)) {
+ t.Errorf("-at 2027-06-01T00:00:00Z: %v, %v", at.Time(), err)
+ }
+
+ at, err = parse("-at", "2027-06-01T09:00:00-04:00")
+ if err != nil || !at.Time().Equal(time.Date(2027, 6, 1, 13, 0, 0, 0, time.UTC)) {
+ t.Errorf("offset: %v, %v", at.Time(), err)
+ }
+
+ for _, bad := range []string{"2027-06-01", "June 1", ""} {
+ if _, err := parse("-at", bad); err == nil {
+ t.Errorf("-at %q accepted", bad)
+ }
+ }
+}
diff --git a/ratify/error.go b/ratify/error.go
index 89cc43a4931a749575bf62cf71ad870cfa0ee2a2..df70e89d416afe7a2d143a20a74882a6647ede72 100644
--- a/ratify/error.go
+++ b/ratify/error.go
@@ -11,6 +11,10 @@ // anchor down.
ErrChain = "E-CHAIN"
// ErrTampered: a signature does not match the bytes it covers.
ErrTampered = "E-TAMPERED"
+ // ErrNotInForce: the check time is before the program takes effect.
+ ErrNotInForce = "E-NOT-IN-FORCE"
+ // ErrLapsed: the check time is after the program expired.
+ ErrLapsed = "E-LAPSED"
)
type Error struct {
diff --git a/ratify/validity.go b/ratify/validity.go
new file mode 100644
index 0000000000000000000000000000000000000000..4a2d681381ff9c18bc1a645d0f425be6d925fdbd
--- /dev/null
+++ b/ratify/validity.go
@@ -0,0 +1,42 @@
+package ratify
+
+import (
+ "crypto/x509"
+ "time"
+)
+
+// CheckValidity checks that at falls inside every cert's validity window.
+// Each cert carries its own window, so the program is in force only where
+// all of them overlap: from the latest notBefore to the earliest notAfter.
+// RFC 5280 counts both ends as inside the window.
+func CheckValidity(certs []*x509.Certificate, at time.Time) error {
+ if len(certs) == 0 {
+ return nil
+ }
+ start, end := 0, 0
+ for i, c := range certs {
+ if c.NotBefore.After(certs[start].NotBefore) {
+ start = i
+ }
+ if c.NotAfter.Before(certs[end].NotAfter) {
+ end = i
+ }
+ }
+ takesEffect, expires := certs[start].NotBefore, certs[end].NotAfter
+
+ if takesEffect.After(expires) {
+ return errorf(ErrLapsed, "never in force: depth %d expires %s, before depth %d takes effect %s",
+ end, stamp(expires), start, stamp(takesEffect))
+ }
+ if at.Before(takesEffect) {
+ return errorf(ErrNotInForce, "takes effect %s (depth %d)", stamp(takesEffect), start)
+ }
+ if at.After(expires) {
+ return errorf(ErrLapsed, "expired %s (depth %d)", stamp(expires), end)
+ }
+ return nil
+}
+
+func stamp(t time.Time) string {
+ return t.UTC().Format(time.RFC3339)
+}
diff --git a/ratify/validity_test.go b/ratify/validity_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..44b2efb54441e1255b8580f0be6bf2520ce67846
--- /dev/null
+++ b/ratify/validity_test.go
@@ -0,0 +1,116 @@
+package ratify
+
+import (
+ "bytes"
+ "crypto/x509"
+ "encoding/asn1"
+ "errors"
+ "strings"
+ "testing"
+ "time"
+)
+
+func date(s string) time.Time {
+ t, err := time.Parse(time.RFC3339, s)
+ if err != nil {
+ panic(err)
+ }
+ return t
+}
+
+// windowed builds a chain where every cert is valid for 2027, except the
+// depths in overrides, which get their own window.
+func windowed(t *testing.T, overrides map[int][2]string) []*x509.Certificate {
+ return testChain(t, 3, func(d int, tmpl *x509.Certificate) {
+ tmpl.NotBefore, tmpl.NotAfter = date("2027-01-01T00:00:00Z"), date("2027-12-31T00:00:00Z")
+ if w, ok := overrides[d]; ok {
+ tmpl.NotBefore, tmpl.NotAfter = date(w[0]), date(w[1])
+ }
+ })
+}
+
+func TestValidityWindow(t *testing.T) {
+ chain := windowed(t, nil)
+ tests := []struct {
+ at string
+ code string
+ msg string
+ }{
+ {"2027-06-01T00:00:00Z", "", ""},
+ {"2027-01-01T00:00:00Z", "", ""}, // both ends are inside the window
+ {"2027-12-31T00:00:00Z", "", ""},
+ {"2026-12-31T23:59:59Z", ErrNotInForce, "takes effect 2027-01-01T00:00:00Z"},
+ {"2027-12-31T00:00:01Z", ErrLapsed, "expired 2027-12-31T00:00:00Z"},
+ }
+ for _, tt := range tests {
+ checkCode(t, "at "+tt.at, CheckValidity(chain, date(tt.at)), tt.code, tt.msg)
+ }
+}
+
+func TestValidityIsTheOverlap(t *testing.T) {
+ // Depth 2 starts late and depth 1 ends early, so the program is in
+ // force only from March to September.
+ chain := windowed(t, map[int][2]string{
+ 1: {"2027-01-01T00:00:00Z", "2027-09-30T00:00:00Z"},
+ 2: {"2027-03-01T00:00:00Z", "2027-12-31T00:00:00Z"},
+ })
+ checkCode(t, "February", CheckValidity(chain, date("2027-02-01T00:00:00Z")), ErrNotInForce, "takes effect 2027-03-01T00:00:00Z (depth 2)")
+ checkCode(t, "June", CheckValidity(chain, date("2027-06-01T00:00:00Z")), "", "")
+ checkCode(t, "October", CheckValidity(chain, date("2027-10-01T00:00:00Z")), ErrLapsed, "expired 2027-09-30T00:00:00Z (depth 1)")
+}
+
+func TestValidityNeverInForce(t *testing.T) {
+ chain := windowed(t, map[int][2]string{
+ 1: {"2027-01-01T00:00:00Z", "2027-03-01T00:00:00Z"},
+ 3: {"2027-06-01T00:00:00Z", "2027-12-31T00:00:00Z"},
+ })
+ checkCode(t, "disjoint windows", CheckValidity(chain, date("2027-04-01T00:00:00Z")), ErrLapsed, "never in force: depth 1 expires 2027-03-01T00:00:00Z, before depth 3 takes effect")
+}
+
+// TestTimeEncodings shows RFC 5280's two time formats: UTCTime (two-digit
+// year) up to 2049, GeneralizedTime (four-digit year) from 2050. That is
+// why a 9999-12-31 expiry can be written at all.
+func TestTimeEncodings(t *testing.T) {
+ tests := []struct {
+ notAfter string
+ tag byte
+ }{
+ {"2049-12-31T23:59:59Z", asn1.TagUTCTime},
+ {"2050-01-01T00:00:00Z", asn1.TagGeneralizedTime},
+ {"9999-12-31T23:59:59Z", asn1.TagGeneralizedTime},
+ }
+ for _, tt := range tests {
+ chain := windowed(t, map[int][2]string{1: {"2027-01-01T00:00:00Z", tt.notAfter}})
+ c := chain[1]
+ if !c.NotAfter.Equal(date(tt.notAfter)) {
+ t.Errorf("%s: parsed back as %s", tt.notAfter, c.NotAfter)
+ }
+ // Validity ::= SEQUENCE { notBefore Time, notAfter Time }; find it
+ // by its encoded notBefore, a UTCTime for 2027.
+ notBefore := []byte("270101000000Z")
+ i := bytes.Index(c.RawTBSCertificate, notBefore)
+ if i < 0 {
+ t.Fatal("notBefore not found")
+ }
+ if got := c.RawTBSCertificate[i+len(notBefore)]; got != byte(tt.tag) {
+ t.Errorf("%s: notAfter tag %d, want %d", tt.notAfter, got, tt.tag)
+ }
+ }
+ if err := CheckValidity(windowed(t, map[int][2]string{1: {"2027-01-01T00:00:00Z", "9999-12-31T23:59:59Z"}}), date("2027-06-01T00:00:00Z")); err != nil {
+ t.Error(err)
+ }
+}
+
+func checkCode(t *testing.T, name string, err error, code, msg string) {
+ t.Helper()
+ if code == "" {
+ if err != nil {
+ t.Errorf("%s: unexpected %v", name, err)
+ }
+ return
+ }
+ var e *Error
+ if !errors.As(err, &e) || e.Code != code || !strings.Contains(e.Msg, msg) {
+ t.Errorf("%s: error = %v, want %s containing %q", name, err, code, msg)
+ }
+}