government

commit de62e8f8076f6010f59527991dbc61d7625a603e

tree

parent:
badf8bb5a62b278affaa0a72163e965aeecae8b0

nmyk <nick@nmyk.io>

2026-11-14T16:10:22-05:00

add policyMappings extension

diff --git a/lisp2gov/cert.go b/lisp2gov/cert.go
index 82e7c610af77fd5766b62b1977716052bd52cdad..8515e4ab5f5eb256da551621e729cb4fb007f4a3 100644
--- a/lisp2gov/cert.go
+++ b/lisp2gov/cert.go
@@ -51,7 +51,7 @@
 // newCert makes the cert at depth i, issued by parent. Every cert but the
 // last is a CA, because each one signs the next; the last one signs
 // nothing.
-func newCert(seed [32]byte, program string, i int, last bool, policies []x509.OID, parent signer) (signer, error) {
+func newCert(seed [32]byte, program string, i int, last bool, policies []x509.OID, mappings []x509.PolicyMapping, parent signer) (signer, error) {
 	key := keyForDepth(seed, i)
 	pub := key.Public().(ed25519.PublicKey)
 	tmpl := &x509.Certificate{
@@ -67,6 +67,18 @@ 		Policies:              policies,
 	}
 	if last {
 		tmpl.KeyUsage = x509.KeyUsageDigitalSignature
+	}
+	if last && len(mappings) > 0 {
+		// RFC 5280 never processes mappings in the last cert (6.1.4 runs
+		// only for certs 1..n-1), so any here would be silently dead.
+		return signer{}, fmt.Errorf("cert %d is the last one and cannot carry mappings", i)
+	}
+	if len(mappings) > 0 {
+		ext, err := policyMappingsExtension(mappings)
+		if err != nil {
+			return signer{}, err
+		}
+		tmpl.ExtraExtensions = append(tmpl.ExtraExtensions, ext)
 	}
 	return create(tmpl, parent.cert, pub, parent.key, key)
 }
@@ -138,7 +150,7 @@ 	anchor, err := newAnchor(seed, name)
 	if err != nil {
 		return nil, err
 	}
-	cert1, err := newCert(seed, name, 1, true, policies, anchor)
+	cert1, err := newCert(seed, name, 1, true, policies, nil, anchor)
 	if err != nil {
 		return nil, err
 	}
diff --git a/lisp2gov/extensions.go b/lisp2gov/extensions.go
new file mode 100644
index 0000000000000000000000000000000000000000..68a568c8ec8db99e06453805b41e695d59cd8b1f
--- /dev/null
+++ b/lisp2gov/extensions.go
@@ -0,0 +1,54 @@
+package lisp2gov
+
+import (
+	"crypto/x509"
+	"crypto/x509/pkix"
+	"encoding/asn1"
+	"errors"
+
+	"government/ast"
+	"government/internal/der"
+)
+
+var oidPolicyMappings = asn1.ObjectIdentifier{2, 5, 29, 33}
+
+// policyMappingsExtension encodes
+//
+//	PolicyMappings ::= SEQUENCE SIZE (1..MAX) OF SEQUENCE {
+//	     issuerDomainPolicy      CertPolicyId,
+//	     subjectDomainPolicy     CertPolicyId }
+//
+// Go parses this extension but CreateCertificate does not write it, so it
+// goes in through ExtraExtensions. RFC 5280 says conforming CAs should
+// mark it critical. For GOVERNMENT that matters more than usual: a
+// verifier that ignored the mappings would see a program with no structure
+// below depth 1, so it must refuse the cert instead.
+func policyMappingsExtension(mappings []x509.PolicyMapping) (pkix.Extension, error) {
+	if len(mappings) == 0 {
+		return pkix.Extension{}, errors.New("policyMappings needs at least one mapping")
+	}
+	entries := make([][]byte, len(mappings))
+	for i, m := range mappings {
+		entries[i] = der.Sequence(der.OID(m.IssuerDomainPolicy), der.OID(m.SubjectDomainPolicy))
+	}
+	return pkix.Extension{Id: oidPolicyMappings, Critical: true, Value: der.Sequence(entries...)}, nil
+}
+
+// mappingsFor turns a layer's edges into mappings: the parent's OID is the
+// policy at this depth, and the child's is what the next cert's anyPolicy
+// will create beneath it.
+func mappingsFor(edges []ast.Edge) ([]x509.PolicyMapping, error) {
+	mappings := make([]x509.PolicyMapping, len(edges))
+	for i, e := range edges {
+		parent, err := e.Parent.OID()
+		if err != nil {
+			return nil, err
+		}
+		child, err := e.Child.OID()
+		if err != nil {
+			return nil, err
+		}
+		mappings[i] = x509.PolicyMapping{IssuerDomainPolicy: parent, SubjectDomainPolicy: child}
+	}
+	return mappings, nil
+}
diff --git a/lisp2gov/extensions_test.go b/lisp2gov/extensions_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..b90aa7ed2a74f52da725f0be9849b376845b637b
--- /dev/null
+++ b/lisp2gov/extensions_test.go
@@ -0,0 +1,153 @@
+package lisp2gov
+
+import (
+	"bytes"
+	"crypto/x509"
+	"encoding/asn1"
+	"testing"
+
+	"government/ast"
+	"government/sexpr"
+)
+
+func TestPolicyMappingsEncoding(t *testing.T) {
+	type mapping struct{ Issuer, Subject asn1.ObjectIdentifier }
+	want, err := asn1.Marshal([]mapping{
+		{asn1.ObjectIdentifier{1, 2, 3}, asn1.ObjectIdentifier{1, 2, 4}},
+		{asn1.ObjectIdentifier{1, 2, 3}, asn1.ObjectIdentifier{2, 999, 1}},
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
+	oid := func(s string) x509.OID {
+		o, err := x509.ParseOID(s)
+		if err != nil {
+			t.Fatal(err)
+		}
+		return o
+	}
+	ext, err := policyMappingsExtension([]x509.PolicyMapping{
+		{IssuerDomainPolicy: oid("1.2.3"), SubjectDomainPolicy: oid("1.2.4")},
+		{IssuerDomainPolicy: oid("1.2.3"), SubjectDomainPolicy: oid("2.999.1")},
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
+	if !bytes.Equal(ext.Value, want) {
+		t.Errorf("% x\nwant % x", ext.Value, want)
+	}
+	if !ext.Id.Equal(asn1.ObjectIdentifier{2, 5, 29, 33}) || !ext.Critical {
+		t.Errorf("extension %v critical=%v", ext.Id, ext.Critical)
+	}
+}
+
+func TestPolicyMappingsRejectsEmpty(t *testing.T) {
+	if _, err := policyMappingsExtension(nil); err == nil {
+		t.Error("empty policyMappings accepted; RFC 5280 requires at least one")
+	}
+}
+
+// buildChain makes the full chain for src by hand, ahead of Compile in
+// step 22: cert 1 lists the top-level nodes, the rest assert anyPolicy,
+// and every cert but the last carries its layer's mappings.
+func buildChain(t *testing.T, src string) ([]*x509.Certificate, ast.Layers) {
+	t.Helper()
+	forms, err := sexpr.ReadAll(src)
+	if err != nil {
+		t.Fatal(err)
+	}
+	tree, err := ast.ToTree(forms)
+	if err != nil {
+		t.Fatal(err)
+	}
+	layers, err := ast.Layer(tree)
+	if err != nil {
+		t.Fatal(err)
+	}
+	anyPolicy, _ := x509.ParseOID("2.5.29.32.0")
+
+	prev, err := newAnchor(DefaultSeed, "mappings")
+	if err != nil {
+		t.Fatal(err)
+	}
+	certs := []*x509.Certificate{prev.cert}
+	n := layers.Depth()
+	for i := 1; i <= n; i++ {
+		policies := []x509.OID{anyPolicy}
+		if i == 1 {
+			policies = nil
+			for _, node := range layers.Nodes[0] {
+				oid, err := node.OID()
+				if err != nil {
+					t.Fatal(err)
+				}
+				policies = append(policies, oid)
+			}
+		}
+		mappings, err := mappingsFor(layers.Edges[i-1])
+		if err != nil {
+			t.Fatal(err)
+		}
+		next, err := newCert(DefaultSeed, "mappings", i, i == n, policies, mappings, prev)
+		if err != nil {
+			t.Fatal(err)
+		}
+		certs = append(certs, next.cert)
+		prev = next
+	}
+	return certs, layers
+}
+
+func TestMappingsSurviveParsing(t *testing.T) {
+	certs, layers := buildChain(t, "(+ 1 (* 2 3))")
+	if len(certs) != 4 {
+		t.Fatalf("%d certs, want anchor + 3", len(certs))
+	}
+	for i, c := range certs[1:] {
+		// Re-parse from the raw bytes, as the interpreter will.
+		parsed, err := x509.ParseCertificate(c.Raw)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if len(parsed.UnhandledCriticalExtensions) != 0 {
+			t.Errorf("cert %d: unhandled critical extensions %v", i+1, parsed.UnhandledCriticalExtensions)
+		}
+		want, err := mappingsFor(layers.Edges[i])
+		if err != nil {
+			t.Fatal(err)
+		}
+		if len(parsed.PolicyMappings) != len(want) {
+			t.Errorf("cert %d: %d mappings, want %d", i+1, len(parsed.PolicyMappings), len(want))
+			continue
+		}
+		for j, m := range parsed.PolicyMappings {
+			if !m.IssuerDomainPolicy.Equal(want[j].IssuerDomainPolicy) || !m.SubjectDomainPolicy.Equal(want[j].SubjectDomainPolicy) {
+				t.Errorf("cert %d mapping %d: %s -> %s, want %s -> %s", i+1, j,
+					m.IssuerDomainPolicy, m.SubjectDomainPolicy, want[j].IssuerDomainPolicy, want[j].SubjectDomainPolicy)
+			}
+		}
+		if err := parsed.CheckSignatureFrom(certs[i]); err != nil {
+			t.Errorf("cert %d: %v", i+1, err)
+		}
+	}
+	// The design's worked example: cert 1 maps the outer list to its three
+	// elements, cert 2 maps the inner list to its three.
+	if n := len(certs[1].PolicyMappings); n != 3 {
+		t.Errorf("cert 1 has %d mappings, want 3", n)
+	}
+	if n := len(certs[2].PolicyMappings); n != 3 {
+		t.Errorf("cert 2 has %d mappings, want 3", n)
+	}
+}
+
+func TestMappingsRejectedOnLastCert(t *testing.T) {
+	anchor, err := newAnchor(DefaultSeed, "x")
+	if err != nil {
+		t.Fatal(err)
+	}
+	a, _ := x509.ParseOID("1.2.3")
+	_, err = newCert(DefaultSeed, "x", 1, true, nil, []x509.PolicyMapping{{IssuerDomainPolicy: a, SubjectDomainPolicy: a}}, anchor)
+	if err == nil {
+		t.Error("last cert accepted mappings")
+	}
+}