government

commit 844f0e3dd48aea4963c188d8ad2ed9749ed3e7f0

tree

parent:
de62e8f8076f6010f59527991dbc61d7625a603e

nmyk <nick@nmyk.io>

2026-11-14T16:14:27-05:00

add certificatePolicies extension

diff --git a/lisp2gov/cert.go b/lisp2gov/cert.go
index 8515e4ab5f5eb256da551621e729cb4fb007f4a3..747452fcf569e9c16bb4bc1b0af422d993f9cb5c 100644
--- a/lisp2gov/cert.go
+++ b/lisp2gov/cert.go
@@ -63,7 +63,6 @@ 		BasicConstraintsValid: true,
 		IsCA:                  !last,
 		KeyUsage:              x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
 		SubjectKeyId:          keyID(pub),
-		Policies:              policies,
 	}
 	if last {
 		tmpl.KeyUsage = x509.KeyUsageDigitalSignature
@@ -72,6 +71,17 @@ 	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(policies) > 0 {
+		infos := make([]policyInfo, len(policies))
+		for j, p := range policies {
+			infos[j] = policyInfo{id: p}
+		}
+		ext, err := certificatePoliciesExtension(infos)
+		if err != nil {
+			return signer{}, err
+		}
+		tmpl.ExtraExtensions = append(tmpl.ExtraExtensions, ext)
 	}
 	if len(mappings) > 0 {
 		ext, err := policyMappingsExtension(mappings)
diff --git a/lisp2gov/extensions.go b/lisp2gov/extensions.go
index 68a568c8ec8db99e06453805b41e695d59cd8b1f..468560595e4679ab59fef3594015681846da569b 100644
--- a/lisp2gov/extensions.go
+++ b/lisp2gov/extensions.go
@@ -10,7 +10,50 @@ 	"government/ast"
 	"government/internal/der"
 )
 
-var oidPolicyMappings = asn1.ObjectIdentifier{2, 5, 29, 33}
+var (
+	oidCertificatePolicies = asn1.ObjectIdentifier{2, 5, 29, 32}
+	oidPolicyMappings      = asn1.ObjectIdentifier{2, 5, 29, 33}
+)
+
+// anyPolicy is the wildcard policy. Certs 2..n assert only this, which
+// makes the validator create every node its parent cert's mappings name.
+var anyPolicy = mustOID("2.5.29.32.0")
+
+func mustOID(s string) x509.OID {
+	oid, err := x509.ParseOID(s)
+	if err != nil {
+		panic(err)
+	}
+	return oid
+}
+
+// policyInfo is one entry of certificatePolicies. Phase 7 adds user-notice
+// qualifiers here to carry notes, which is why this extension is built by
+// hand: x509.Certificate.Policies holds bare OIDs and has no room for them.
+type policyInfo struct {
+	id x509.OID
+}
+
+// certificatePoliciesExtension encodes
+//
+//	certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
+//	PolicyInformation ::= SEQUENCE {
+//	     policyIdentifier   CertPolicyId,
+//	     policyQualifiers   SEQUENCE SIZE (1..MAX) OF PolicyQualifierInfo OPTIONAL }
+//
+// It is not critical, as Go and most CAs write it: a verifier that does not
+// do policy processing can still check the signatures, and one that does
+// will also need the critical policyMappings.
+func certificatePoliciesExtension(policies []policyInfo) (pkix.Extension, error) {
+	if len(policies) == 0 {
+		return pkix.Extension{}, errors.New("certificatePolicies needs at least one policy")
+	}
+	entries := make([][]byte, len(policies))
+	for i, p := range policies {
+		entries[i] = der.Sequence(der.OID(p.id))
+	}
+	return pkix.Extension{Id: oidCertificatePolicies, Value: der.Sequence(entries...)}, nil
+}
 
 // policyMappingsExtension encodes
 //
diff --git a/lisp2gov/extensions_test.go b/lisp2gov/extensions_test.go
index b90aa7ed2a74f52da725f0be9849b376845b637b..65b9dee083216c5c0efc23b00b425b1330ccba78 100644
--- a/lisp2gov/extensions_test.go
+++ b/lisp2gov/extensions_test.go
@@ -64,8 +64,6 @@ 	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)
@@ -151,3 +149,60 @@ 	if err == nil {
 		t.Error("last cert accepted mappings")
 	}
 }
+
+func TestCertificatePoliciesEncoding(t *testing.T) {
+	type policyInformation struct{ ID asn1.ObjectIdentifier }
+	want, err := asn1.Marshal([]policyInformation{{asn1.ObjectIdentifier{1, 2, 3}}, {asn1.ObjectIdentifier{2, 5, 29, 32, 0}}})
+	if err != nil {
+		t.Fatal(err)
+	}
+	ext, err := certificatePoliciesExtension([]policyInfo{{mustOID("1.2.3")}, {anyPolicy}})
+	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, 32}) || ext.Critical {
+		t.Errorf("extension %v critical=%v", ext.Id, ext.Critical)
+	}
+	if _, err := certificatePoliciesExtension(nil); err == nil {
+		t.Error("empty certificatePolicies accepted; RFC 5280 requires at least one")
+	}
+}
+
+func TestPoliciesSurviveParsing(t *testing.T) {
+	certs, layers := buildChain(t, "(+ 1 (* 2 3)) x")
+	if certs[0].Policies != nil {
+		t.Error("the anchor has policies")
+	}
+
+	parsed, err := x509.ParseCertificate(certs[1].Raw)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if len(parsed.Policies) != len(layers.Nodes[0]) {
+		t.Fatalf("cert 1 has %d policies, want %d", len(parsed.Policies), len(layers.Nodes[0]))
+	}
+	for i, p := range parsed.Policies {
+		want, _ := layers.Nodes[0][i].OID()
+		if !p.Equal(want) {
+			t.Errorf("cert 1 policy %d = %s, want %s", i, p, want)
+		}
+		// The 128-bit GOV arc survives the trip through hand-written DER
+		// and Go's parser.
+		if _, _, _, err := ast.DecodeOID(p); err != nil {
+			t.Errorf("cert 1 policy %d: %v", i, err)
+		}
+	}
+
+	for i := 2; i < len(certs); i++ {
+		parsed, err := x509.ParseCertificate(certs[i].Raw)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if len(parsed.Policies) != 1 || !parsed.Policies[0].Equal(anyPolicy) {
+			t.Errorf("cert %d policies = %v, want only anyPolicy", i, parsed.Policies)
+		}
+	}
+}