government / src / ratify / repeal.go

@ 72c20a953a72bec361bd6d0c25c9d0133e8bf1bd | history


package ratify

import (
	"bytes"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"slices"
	"time"
)

// Reasons are RFC 5280's CRLReason codes (section 5.3.1), by name. Code 7
// is unused in the RFC.
var Reasons = map[string]int{
	"unspecified":          0,
	"keyCompromise":        1,
	"cACompromise":         2,
	"affiliationChanged":   3,
	"superseded":           4,
	"cessationOfOperation": 5,
	"certificateHold":      6,
	"removeFromCRL":        8,
	"privilegeWithdrawn":   9,
	"aACompromise":         10,
}

// ReasonNames lists the reason names in code order.
func ReasonNames() []string {
	names := make([]string, 0, len(Reasons))
	for n := range Reasons {
		names = append(names, n)
	}
	slices.SortFunc(names, func(a, b string) int { return Reasons[a] - Reasons[b] })
	return names
}

func reasonName(code int) string {
	for name, c := range Reasons {
		if c == code {
			return name
		}
	}
	return fmt.Sprintf("reason %d", code)
}

// LoadCRLs reads the "X509 CRL" PEM blocks in data, one file's worth.
func LoadCRLs(data []byte) ([]*x509.RevocationList, error) {
	var crls []*x509.RevocationList
	rest := data
	for {
		var block *pem.Block
		block, rest = pem.Decode(rest)
		if block == nil {
			break
		}
		if block.Type != "X509 CRL" {
			return nil, errorf(ErrMalformed, "a repeal list holds X509 CRL blocks, not %s", block.Type)
		}
		crl, err := x509.ParseRevocationList(block.Bytes)
		if err != nil {
			return nil, errorf(ErrMalformed, "repeal list: %v", err)
		}
		crls = append(crls, crl)
	}
	if len(crls) == 0 {
		return nil, errorf(ErrMalformed, "no X509 CRL block found")
	}
	return crls, nil
}

// CheckRepeals applies CRLs to a chain in depth order, at time at.
//
// Checking revocation is a trust decision of its own: the chain says the
// program was ratified, and a CRL can say it has since been struck down.
// GOVERNMENT fails closed at every step. A CRL that is not for this
// program, is not signed by its issuer, or has gone stale stops the
// program rather than being skipped, since a list that cannot be trusted
// cannot be trusted to say "nothing is repealed" either.
func CheckRepeals(certs []*x509.Certificate, crls []*x509.RevocationList, at time.Time) error {
	for _, crl := range crls {
		depth := slices.IndexFunc(certs, func(c *x509.Certificate) bool {
			return bytes.Equal(c.RawSubject, crl.RawIssuer)
		})
		if depth < 0 {
			return errorf(ErrForeign, "a repeal list from %s, which is not part of this program", crl.Issuer)
		}
		// CheckSignatureFrom also requires the issuer to have cRLSign, so
		// the last cert, which issues nothing, cannot publish repeals.
		if err := crl.CheckSignatureFrom(certs[depth]); err != nil {
			return errorf(ErrTampered, "the repeal list from depth %d fails its signature check: %v", depth, err)
		}
		if at.After(crl.NextUpdate) {
			return errorf(ErrStaleRepeals, "the repeal list from depth %d went stale %s", depth, stamp(crl.NextUpdate))
		}
		if depth+1 >= len(certs) {
			continue
		}
		// A serial number is unique only among one issuer's certs, so the
		// only cert this CRL can name is the one its issuer issued.
		child := certs[depth+1]
		for _, e := range crl.RevokedCertificateEntries {
			if e.SerialNumber.Cmp(child.SerialNumber) != 0 {
				continue
			}
			// A revocation time later than the check time is a scheduled
			// repeal. RFC 5280 reads the field as when the revocation
			// happened; honouring a future one is GOVERNMENT's own reading.
			if !e.RevocationTime.After(at) {
				return errorf(ErrRepealed, "depth %d repealed %s (%s)", depth+1, e.RevocationTime.UTC().Format(time.DateOnly), reasonName(e.ReasonCode))
			}
		}
	}
	return nil
}