@ 7eeb8a0214368f4491f26f4cf37bd7bf6dc67cd3 | history
package lisp2gov
import (
"bytes"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"math/big"
"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, for messages.
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
}
type RepealOptions struct {
// Seed is the key the program was compiled with; nil means
// DefaultSeed. Repealing needs it because the cert being repealed is
// revoked by its issuer, the cert one depth up, whose key is derived
// from this seed.
Seed *[32]byte
// Reason is a CRLReason code.
Reason int
// At is when the repeal takes effect: the revocation time. A time in
// the future schedules the repeal.
At time.Time
// Issued is when the CRL is made (thisUpdate), and NextUpdate is when
// it goes stale. A CRL is only trusted until its nextUpdate, so a
// permanent repeal should have a far-off one.
Issued, NextUpdate time.Time
}
// Repeal makes a CRL that revokes the cert at depth of a program, given
// its certs in depth order. Revoking any one depth breaks the chain, so it
// repeals the whole program.
//
// By RFC 5280 a cert is revoked by its own issuer: the CRL is signed by
// the cert at depth-1 (the anchor, for depth 1) and names the revoked cert
// by its serial number, which is unique only among that issuer's certs.
func Repeal(certs []*x509.Certificate, depth int, opts RepealOptions) ([]byte, error) {
n := len(certs) - 1
if depth < 1 || depth > n {
return nil, fmt.Errorf("depth %d: the program has depths 1 to %d", depth, n)
}
seed := DefaultSeed
if opts.Seed != nil {
seed = *opts.Seed
}
issuer, revoked := certs[depth-1], certs[depth]
key := keyForDepth(seed, depth-1)
pub, err := x509.MarshalPKIXPublicKey(key.Public())
if err != nil {
return nil, err
}
if !bytes.Equal(pub, issuer.RawSubjectPublicKeyInfo) {
return nil, fmt.Errorf("this key did not compile the program: depth %d's issuer has a different key", depth)
}
if !opts.NextUpdate.After(opts.Issued) {
return nil, fmt.Errorf("the CRL would go stale (%s) before it is issued (%s)",
opts.NextUpdate.UTC().Format(time.RFC3339), opts.Issued.UTC().Format(time.RFC3339))
}
tmpl := &x509.RevocationList{
// A CRL number should grow with each CRL an issuer publishes; the
// issue time in seconds does, and keeps the output deterministic.
Number: big.NewInt(opts.Issued.Unix()),
ThisUpdate: opts.Issued.UTC(),
NextUpdate: opts.NextUpdate.UTC(),
RevokedCertificateEntries: []x509.RevocationListEntry{{
SerialNumber: revoked.SerialNumber,
RevocationTime: opts.At.UTC(),
ReasonCode: opts.Reason,
}},
}
// Ed25519 signing reads no randomness, so rand is never used.
der, err := x509.CreateRevocationList(rand.Reader, tmpl, issuer, key)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(&pem.Block{Type: "X509 CRL", Bytes: der}), nil
}