@ 25d85a34ec701b64fde8050571d39eaa9b8d1bbb | 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: a node's OID and, if the
// node has one, its note. x509.Certificate.Policies holds bare OIDs, with
// no room for a note, which is why this extension is built by hand.
type policyInfo struct {
id x509.OID
note string
}
// idQtUnotice is the user-notice qualifier, a policy qualifier holding text
// meant for a person reading the cert.
var idQtUnotice = mustOID("1.3.6.1.5.5.7.2.2")
// maxNoticeText is RFC 5280's limit on a user notice's explicitText, in
// characters. A longer note is split across several user notices, which a
// reader joins back in order.
const maxNoticeText = 200
// certificatePoliciesExtension encodes
//
// certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
// PolicyInformation ::= SEQUENCE {
// policyIdentifier CertPolicyId,
// policyQualifiers SEQUENCE SIZE (1..MAX) OF PolicyQualifierInfo OPTIONAL }
// PolicyQualifierInfo ::= SEQUENCE {
// policyQualifierId id-qt-unotice,
// qualifier UserNotice }
// UserNotice ::= SEQUENCE { explicitText UTF8String }
//
// 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. Qualifiers never affect
// policy processing: RFC 5280 only carries them along, so notes cannot
// change what a program does.
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 {
if p.note == "" {
entries[i] = der.Sequence(der.OID(p.id))
continue
}
var qualifiers [][]byte
for _, chunk := range splitNote(p.note) {
qualifiers = append(qualifiers, der.Sequence(der.OID(idQtUnotice), der.Sequence(der.UTF8String(chunk))))
}
entries[i] = der.Sequence(der.OID(p.id), der.Sequence(qualifiers...))
}
return pkix.Extension{Id: oidCertificatePolicies, Value: der.Sequence(entries...)}, nil
}
// splitNote cuts a note into pieces of at most maxNoticeText characters.
func splitNote(note string) []string {
runes := []rune(note)
var chunks []string
for len(runes) > maxNoticeText {
chunks = append(chunks, string(runes[:maxNoticeText]))
runes = runes[maxNoticeText:]
}
return append(chunks, string(runes))
}
// 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
}