parent:
53308ffa01ed91d41c6657b16c77ee7a97ad8d3b
nmyk <nick@nmyk.io>
2026-11-14T17:08:42-05:00
apply policy mappings
diff --git a/policy/graph.go b/policy/graph.go
index 6c1765a2883a81ef87804e8e20447f81692b8189..564abb968281f6a9bde6c4408ac1a91969c78d14 100644
--- a/policy/graph.go
+++ b/policy/graph.go
@@ -61,9 +61,17 @@ // three counters never reach zero and are left out.
func Process(certs []*x509.Certificate) (*Graph, error) {
root := &Node{Policy: AnyPolicy, Expected: []x509.OID{AnyPolicy}}
g := &Graph{Levels: [][]*Node{{root}}}
- for i := 1; i < len(certs); i++ {
+ n := len(certs) - 1
+ for i := 1; i <= n; i++ {
if err := g.addCert(i, certs[i]); err != nil {
return nil, err
+ }
+ // 6.1.4 prepares for the next cert, so it runs for certs 1..n-1
+ // only; mappings in cert n would never be read.
+ if i < n {
+ if err := g.applyMappings(i, certs[i]); err != nil {
+ return nil, err
+ }
}
}
return g, nil
@@ -173,3 +181,60 @@ func key(oid x509.OID) string {
b, _ := oid.MarshalBinary()
return string(b)
}
+
+// applyMappings is RFC 5280 6.1.4 (a)-(b) for cert i, with mappings
+// allowed. A mapping says "my policy P is equivalent to your policy Q":
+// the next cert may then assert Q where this one asserted P. So each node
+// at depth i whose policy is an issuerDomainPolicy now expects the mapped
+// subjectDomainPolicy values instead of itself. For GOVERNMENT a mapping is
+// a parent->child edge, and the expected set is a list node's elements.
+func (g *Graph) applyMappings(i int, c *x509.Certificate) error {
+ if len(c.PolicyMappings) == 0 {
+ return nil
+ }
+ mapped := map[string][]x509.OID{}
+ var issuers []x509.OID
+ for _, m := range c.PolicyMappings {
+ // (a): anyPolicy may not appear on either side of a mapping.
+ if m.IssuerDomainPolicy.Equal(AnyPolicy) || m.SubjectDomainPolicy.Equal(AnyPolicy) {
+ return errorf(ErrPolicy, "cert %d maps anyPolicy, which RFC 5280 forbids", i)
+ }
+ k := key(m.IssuerDomainPolicy)
+ if _, seen := mapped[k]; !seen {
+ issuers = append(issuers, m.IssuerDomainPolicy)
+ }
+ if !slices.ContainsFunc(mapped[k], m.SubjectDomainPolicy.Equal) {
+ mapped[k] = append(mapped[k], m.SubjectDomainPolicy)
+ }
+ }
+
+ level := g.Levels[i]
+ for _, p := range issuers {
+ found := false
+ for _, n := range level {
+ if n.Policy.Equal(p) {
+ n.Expected = mapped[key(p)]
+ found = true
+ }
+ }
+ if found {
+ continue
+ }
+ // (b)(1), second half: with no node for P but an anyPolicy node at
+ // this depth, P is taken to hold under it, as a new node with the
+ // anyPolicy node's parents. GOVERNMENT never asserts anyPolicy in
+ // cert 1, so this only matters for chains built by other tools.
+ for _, anyNode := range level {
+ if !anyNode.Policy.Equal(AnyPolicy) {
+ continue
+ }
+ n := &Node{Policy: p, Depth: i, Parents: slices.Clone(anyNode.Parents), Expected: mapped[key(p)]}
+ for _, parent := range n.Parents {
+ parent.Children = append(parent.Children, n)
+ }
+ g.Levels[i] = append(g.Levels[i], n)
+ break
+ }
+ }
+ return nil
+}
diff --git a/policy/mapping_test.go b/policy/mapping_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..8e82af6b22f8d16823db3c6e7740e3d8ffe4171d
--- /dev/null
+++ b/policy/mapping_test.go
@@ -0,0 +1,140 @@
+package policy
+
+import (
+ "crypto/x509"
+ "strconv"
+ "strings"
+ "testing"
+
+ "government/ast"
+ "government/lisp2gov"
+ "government/ratify"
+)
+
+// mapping adds policyMappings to a fake cert, as "issuer>subject" pairs.
+func mapping(c *x509.Certificate, pairs ...string) *x509.Certificate {
+ for _, pair := range pairs {
+ from, to, _ := strings.Cut(pair, ">")
+ if from == "any" {
+ from = "2.5.29.32.0"
+ }
+ if to == "any" {
+ to = "2.5.29.32.0"
+ }
+ c.PolicyMappings = append(c.PolicyMappings, x509.PolicyMapping{IssuerDomainPolicy: mustOID(from), SubjectDomainPolicy: mustOID(to)})
+ }
+ return c
+}
+
+// label names a GOVERNMENT node the way the design's worked example does:
+// "+#2", "1#3", "(...)#4".
+func label(t *testing.T, n *Node) string {
+ t.Helper()
+ _, id, text, err := ast.DecodeOID(n.Policy)
+ if err != nil {
+ t.Fatalf("%s: %v", n.Policy, err)
+ }
+ return text + "#" + strconv.FormatUint(id, 10)
+}
+
+func compiledGraph(t *testing.T, src string) *Graph {
+ t.Helper()
+ out, err := lisp2gov.Compile(src, lisp2gov.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ certs, err := ratify.Ratify(out, ratify.Options{At: ratifyTime})
+ if err != nil {
+ t.Fatal(err)
+ }
+ return mustProcess(t, certs)
+}
+
+// TestWorkedExample checks the design's worked example. The ′ marks in the
+// design are the validator's copies: +#2 and 1#3 appear again at depth 3,
+// each the only child of itself at depth 2.
+func TestWorkedExample(t *testing.T) {
+ g := compiledGraph(t, "(+ 1 (* 2 3))")
+ want := []string{
+ "(...)#1",
+ "+#2 <- (...)#1 | 1#3 <- (...)#1 | (...)#4 <- (...)#1",
+ "+#2 <- +#2 | 1#3 <- 1#3 | *#5 <- (...)#4 | 2#6 <- (...)#4 | 3#7 <- (...)#4",
+ }
+ if g.Depth() != 3 {
+ t.Fatalf("depth %d, want 3", g.Depth())
+ }
+ for d := 1; d <= 3; d++ {
+ var nodes []string
+ for _, n := range g.Levels[d] {
+ s := label(t, n)
+ if d > 1 {
+ var parents []string
+ for _, p := range n.Parents {
+ parents = append(parents, label(t, p))
+ }
+ s += " <- " + strings.Join(parents, ", ")
+ }
+ nodes = append(nodes, s)
+ }
+ if got := strings.Join(nodes, " | "); got != want[d-1] {
+ t.Errorf("depth %d:\n got %s\n want %s", d, got, want[d-1])
+ }
+ }
+ // Nothing is pruned: every branch reaches depth 3.
+ if n := len(g.Levels[1][0].Children); n != 3 {
+ t.Errorf("(...)#1 has %d children, want 3", n)
+ }
+}
+
+func TestChildWithTwoParents(t *testing.T) {
+ // Both 1.2.3 and 1.2.4 map to 1.2.5. RFC 5280's tree would copy 1.2.5
+ // under each; RFC 9618's graph makes one node with two parents, which
+ // for GOVERNMENT is a shared subexpression.
+ g := mustProcess(t, path(
+ mapping(fakeCert("1.2.3", "1.2.4"), "1.2.3>1.2.5", "1.2.4>1.2.5"),
+ fakeCert("any"),
+ ))
+ if got, want := levels(g), "any | 1.2.3 1.2.4 | 1.2.5"; got != want {
+ t.Fatalf("levels = %s, want %s", got, want)
+ }
+ child := g.Levels[2][0]
+ if len(child.Parents) != 2 || child.Parents[0] != g.Levels[1][0] || child.Parents[1] != g.Levels[1][1] {
+ t.Errorf("1.2.5 has %d parents, want both depth-1 nodes", len(child.Parents))
+ }
+}
+
+func TestMappingReplacesExpectedSet(t *testing.T) {
+ // Once 1.2.3 maps to 1.2.4, the next cert may no longer continue
+ // 1.2.3 itself: anyPolicy creates 1.2.4, not a copy of 1.2.3.
+ g := mustProcess(t, path(mapping(fakeCert("1.2.3"), "1.2.3>1.2.4"), fakeCert("any")))
+ if got, want := levels(g), "any | 1.2.3 | 1.2.4"; got != want {
+ t.Errorf("levels = %s, want %s", got, want)
+ }
+ wantPolicyError(t, "asserting the issuer's own policy after mapping it",
+ path(mapping(fakeCert("1.2.3"), "1.2.3>1.2.4"), fakeCert("1.2.3")), "no policy holds at depth 2")
+}
+
+func TestMappingsIgnoredWhereRFCIgnoresThem(t *testing.T) {
+ // Mappings in the last cert are never processed (step 29 will reject
+ // them), and a mapping from a policy with no node does nothing.
+ g := mustProcess(t, path(mapping(fakeCert("1.2.3"), "1.2.9>1.2.4"), mapping(fakeCert("any"), "1.2.3>1.2.4")))
+ if got, want := levels(g), "any | 1.2.3 | 1.2.3"; got != want {
+ t.Errorf("levels = %s, want %s", got, want)
+ }
+}
+
+func TestMappingUnderAnyPolicy(t *testing.T) {
+ // Cert 1 asserts only anyPolicy and maps 1.2.3 to 1.2.4. RFC 5280
+ // 6.1.4 (b)(1) then creates 1.2.3 under the root as if cert 1 had named
+ // it, expecting 1.2.4; the unused anyPolicy node is pruned.
+ g := mustProcess(t, path(mapping(fakeCert("any"), "1.2.3>1.2.4"), fakeCert("1.2.4")))
+ if got, want := levels(g), "any | 1.2.3 | 1.2.4"; got != want {
+ t.Errorf("levels = %s, want %s", got, want)
+ }
+}
+
+func TestAnyPolicyCannotBeMapped(t *testing.T) {
+ for _, pair := range []string{"any>1.2.4", "1.2.3>any"} {
+ wantPolicyError(t, pair, path(mapping(fakeCert("1.2.3"), pair), fakeCert("any")), "maps anyPolicy")
+ }
+}