government / lisp2gov / extensions.go

@ bdd32d2bb7ca2a1b5b8c192fbaab691d8f1ed255 | history


package lisp2gov

import (
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/asn1"
	"errors"

	"government/ast"
	"government/internal/der"
)

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
//
//	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
}