government / lisp2gov / extensions.go

@ de62e8f8076f6010f59527991dbc61d7625a603e | history


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
}