CA delegation: name-constrained intermediates, root rotation, ACME node certificates
New roots carry path length 1. `rollcall ca intermediate` signs a CSR (or a generated key) as an intermediate limited to the permitted domains and address ranges; a root with path length 0 refuses and `rollcall ca rotate` replaces it. deploy/tags/acme-cert.yaml gets a node certificate from an ACME server under such an intermediate and renews it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bcbb07721b
commit
77894b7d57
6 files changed
+311
-2
No files matched your search
+183
-2
@@ -40,6 +40,7 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -76,7 +77,7 @@ func newCA(cn string) (*CA, error) {
|
||||
}
|
||||
tmpl := &x509.Certificate{SerialNumber: serial, Subject: pkix.Name{CommonName: cn, Organization: []string{"rollcall"}},
|
||||
NotBefore: time.Now().Add(-backdate), NotAfter: time.Now().Add(caValidity),
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, BasicConstraintsValid: true, IsCA: true, MaxPathLenZero: true}
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, BasicConstraintsValid: true, IsCA: true, MaxPathLen: 1}
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -189,6 +190,58 @@ func (ca *CA) sign(pub crypto.PublicKey, cn string, hosts []string, usage ...x50
|
||||
return pemEncode("CERTIFICATE", der), cert, err
|
||||
}
|
||||
|
||||
// canDelegate: the root may sign intermediate CAs (roots before path length 1 cannot).
|
||||
func (ca *CA) canDelegate() bool {
|
||||
return !(ca.Cert.MaxPathLen == 0 && ca.Cert.MaxPathLenZero)
|
||||
}
|
||||
|
||||
// parsePermits splits "int.example.com,10.213.0.0/16" into DNS and IP constraints.
|
||||
func parsePermits(list string) (dns []string, nets []*net.IPNet, err error) {
|
||||
for _, p := range strings.Split(list, ",") {
|
||||
p = strings.TrimSpace(p)
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
if _, n, e := net.ParseCIDR(p); e == nil {
|
||||
nets = append(nets, n)
|
||||
continue
|
||||
}
|
||||
if net.ParseIP(p) != nil {
|
||||
return nil, nil, fmt.Errorf("%s: give an address range as CIDR", p)
|
||||
}
|
||||
dns = append(dns, strings.TrimPrefix(strings.ToLower(p), "*."))
|
||||
}
|
||||
return dns, nets, nil
|
||||
}
|
||||
|
||||
// intermediate signs a CA certificate for pub that may issue only below the
|
||||
// permitted DNS names and inside the permitted address ranges, one level deep.
|
||||
func (ca *CA) intermediate(pub crypto.PublicKey, cn string, dns []string, nets []*net.IPNet, validity time.Duration) ([]byte, *x509.Certificate, error) {
|
||||
if !ca.canDelegate() {
|
||||
return nil, nil, errors.New("this root was created with path length 0 and cannot sign intermediate CAs; `rollcall ca rotate` replaces it with one that can")
|
||||
}
|
||||
serial, err := randomSerial()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if validity <= 0 || validity > caValidity {
|
||||
validity = 365 * 24 * time.Hour
|
||||
}
|
||||
if na := ca.Cert.NotAfter; time.Now().Add(validity).After(na) {
|
||||
validity = time.Until(na)
|
||||
}
|
||||
tmpl := &x509.Certificate{SerialNumber: serial, Subject: pkix.Name{CommonName: cn, Organization: []string{"rollcall"}},
|
||||
NotBefore: time.Now().Add(-backdate), NotAfter: time.Now().Add(validity),
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature, BasicConstraintsValid: true, IsCA: true, MaxPathLenZero: true,
|
||||
PermittedDNSDomainsCritical: len(dns)+len(nets) > 0, PermittedDNSDomains: dns, PermittedIPRanges: nets}
|
||||
der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.Cert, pub, ca.Key)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cert, err := x509.ParseCertificate(der)
|
||||
return pemEncode("CERTIFICATE", der), cert, err
|
||||
}
|
||||
|
||||
// issue makes a key and a certificate for it.
|
||||
func (ca *CA) issue(cn string, hosts []string, usage ...x509.ExtKeyUsage) (certPEM, keyPEM []byte, err error) {
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
@@ -569,7 +622,12 @@ func caCmd(args []string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if args[0] != "issue" || len(args) < 2 {
|
||||
switch {
|
||||
case args[0] == "intermediate" && len(args) >= 2:
|
||||
return caIntermediateCmd(cfg, args[1], args[2:])
|
||||
case args[0] == "rotate":
|
||||
return caRotateCmd(cfg, args[1:])
|
||||
case args[0] != "issue" || len(args) < 2:
|
||||
usage()
|
||||
}
|
||||
fs := newFlags("ca issue")
|
||||
@@ -604,3 +662,126 @@ func caCmd(args []string) error {
|
||||
fmt.Printf("wrote %s.pem (with the CA) and %s-key.pem in %s, valid until %s\n", args[1], args[1], *out, time.Now().Add(leafValidity).Format("2006-01-02"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// rollcall ca intermediate NAME -permit DOMAIN,CIDR [-csr FILE] [-days N] [-out DIR]
|
||||
func caIntermediateCmd(cfg *Config, name string, args []string) error {
|
||||
fs := newFlags("ca intermediate")
|
||||
permit := fs.String("permit", "", "comma-separated DNS domains and CIDRs it may issue for (required)")
|
||||
csrFile := fs.String("csr", "", "sign this CSR (the key stays where it was made); without it a key is generated")
|
||||
days := fs.Int("days", 365, "validity in days")
|
||||
out := fs.String("out", ".", "directory for NAME.pem (+ NAME-key.pem without -csr)")
|
||||
unconstrained := fs.Bool("unconstrained", false, "allow an intermediate without name constraints")
|
||||
_ = fs.Parse(args)
|
||||
dns, nets, err := parsePermits(*permit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(dns)+len(nets) == 0 && !*unconstrained {
|
||||
return errors.New("an intermediate needs -permit (e.g. -permit int.example.com,10.213.0.0/16); -unconstrained to override")
|
||||
}
|
||||
ca, err := loadOrCreateCA(cfg.CADir, "rollcall CA")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var pub crypto.PublicKey
|
||||
var keyPEM []byte
|
||||
if *csrFile != "" {
|
||||
b, err := os.ReadFile(*csrFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
blk, _ := pem.Decode(b)
|
||||
if blk == nil {
|
||||
return fmt.Errorf("%s: not PEM", *csrFile)
|
||||
}
|
||||
csr, err := x509.ParseCertificateRequest(blk.Bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := csr.CheckSignature(); err != nil {
|
||||
return fmt.Errorf("CSR signature: %w", err)
|
||||
}
|
||||
pub = csr.PublicKey
|
||||
} else {
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pub = &key.PublicKey
|
||||
kder, _ := x509.MarshalECPrivateKey(key)
|
||||
keyPEM = pemEncode("EC PRIVATE KEY", kder)
|
||||
}
|
||||
certPEM, cert, err := ca.intermediate(pub, name, dns, nets, time.Duration(*days)*24*time.Hour)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if keyPEM != nil {
|
||||
if err := os.WriteFile(filepath.Join(*out, name+"-key.pem"), keyPEM, 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(*out, name+".pem"), certPEM, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
var permits []string
|
||||
permits = append(permits, dns...)
|
||||
for _, n := range nets {
|
||||
permits = append(permits, n.String())
|
||||
}
|
||||
fmt.Printf("wrote %s.pem in %s: intermediate CA %q until %s, may issue for %s\nroot: %s (%s)\n", name, *out, name,
|
||||
cert.NotAfter.Format("2006-01-02"), orDash(strings.Join(permits, " ")), filepath.Join(cfg.CADir, "ca.pem"), ca.Fingerprint())
|
||||
return nil
|
||||
}
|
||||
|
||||
// rollcall ca rotate -yes: a new root (the old one is kept as a backup). Racks
|
||||
// must join again and clients trust the new fingerprint.
|
||||
func caRotateCmd(cfg *Config, args []string) error {
|
||||
fs := newFlags("ca rotate")
|
||||
yes := fs.Bool("yes", false, "really replace the root")
|
||||
_ = fs.Parse(args)
|
||||
old, err := loadOrCreateCA(cfg.CADir, "rollcall CA")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !*yes {
|
||||
return fmt.Errorf("this replaces the root %s (delegates: %v); rack controllers then join again and clients trust the new one. Run with -yes", old.Fingerprint(), old.canDelegate())
|
||||
}
|
||||
bak := cfg.CADir + ".bak-" + time.Now().Format("20060102-150405")
|
||||
if err := os.Rename(cfg.CADir, bak); err != nil {
|
||||
return err
|
||||
}
|
||||
ca, err := loadOrCreateCA(cfg.CADir, "rollcall CA")
|
||||
if err != nil {
|
||||
_ = os.Rename(bak, cfg.CADir)
|
||||
return err
|
||||
}
|
||||
if st, err := os.Stat(bak); err == nil {
|
||||
_ = os.Chmod(cfg.CADir, st.Mode().Perm())
|
||||
if own, ok := fileOwner(bak); ok {
|
||||
_ = chownTree(cfg.CADir, own[0], own[1])
|
||||
}
|
||||
}
|
||||
fmt.Printf("new root %s\nold root kept in %s\nnext: restart rollcall; on each rack controller `rollcall rack join` with a new join code; clients trust the new fingerprint\n", ca.Fingerprint(), bak)
|
||||
return nil
|
||||
}
|
||||
|
||||
// fileOwner returns uid, gid of path (unix).
|
||||
func fileOwner(path string) ([2]int, bool) {
|
||||
st, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return [2]int{}, false
|
||||
}
|
||||
if s, ok := st.Sys().(*syscall.Stat_t); ok {
|
||||
return [2]int{int(s.Uid), int(s.Gid)}, true
|
||||
}
|
||||
return [2]int{}, false
|
||||
}
|
||||
|
||||
func chownTree(root string, uid, gid int) error {
|
||||
return filepath.Walk(root, func(p string, _ os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Lchown(p, uid, gid)
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
@@ -101,3 +104,64 @@ func TestSignRackCSR(t *testing.T) {
|
||||
t.Errorf("subject %s, SANs %v %v", cert.Subject.CommonName, cert.IPAddresses, cert.DNSNames)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntermediate(t *testing.T) {
|
||||
root, err := newCA("test root")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !root.canDelegate() {
|
||||
t.Fatal("new roots must be able to delegate one level")
|
||||
}
|
||||
dns, nets, err := parsePermits("int.example.com, 10.213.0.0/16")
|
||||
if err != nil || len(dns) != 1 || len(nets) != 1 {
|
||||
t.Fatalf("permits: %v %v %v", dns, nets, err)
|
||||
}
|
||||
ikey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
_, icert, err := root.intermediate(&ikey.PublicKey, "caddy", dns, nets, 30*24*time.Hour)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !icert.IsCA || !icert.MaxPathLenZero || !icert.PermittedDNSDomainsCritical {
|
||||
t.Fatalf("intermediate shape: %+v", icert)
|
||||
}
|
||||
sub := &CA{Cert: icert, Key: ikey}
|
||||
roots, inters := x509.NewCertPool(), x509.NewCertPool()
|
||||
roots.AddCert(root.Cert)
|
||||
inters.AddCert(icert)
|
||||
verify := func(hosts ...string) error {
|
||||
_, leaf, err := sub.sign(&ikey.PublicKey, hosts[0], hosts, x509.ExtKeyUsageServerAuth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = leaf.Verify(x509.VerifyOptions{Roots: roots, Intermediates: inters, KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}})
|
||||
return err
|
||||
}
|
||||
if err := verify("node001.int.example.com", "10.213.77.50"); err != nil {
|
||||
t.Errorf("leaf inside the constraints: %v", err)
|
||||
}
|
||||
if err := verify("www.google.com"); err == nil {
|
||||
t.Error("leaf outside the DNS constraint verified")
|
||||
}
|
||||
if err := verify("node001.int.example.com", "192.168.1.1"); err == nil {
|
||||
t.Error("leaf outside the IP constraint verified")
|
||||
}
|
||||
// a second level below the intermediate is refused by path length 0
|
||||
k2, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
_, c2, err := sub.intermediate(&k2.PublicKey, "deeper", dns, nil, time.Hour)
|
||||
if err == nil {
|
||||
inters.AddCert(c2)
|
||||
_, leaf, _ := (&CA{Cert: c2, Key: k2}).sign(&k2.PublicKey, "x.int.example.com", []string{"x.int.example.com"}, x509.ExtKeyUsageServerAuth)
|
||||
if _, err := leaf.Verify(x509.VerifyOptions{Roots: roots, Intermediates: inters}); err == nil {
|
||||
t.Error("two levels of intermediates verified")
|
||||
}
|
||||
}
|
||||
// old roots (path length 0) refuse to delegate
|
||||
old := *root
|
||||
oc := *root.Cert
|
||||
oc.MaxPathLen, oc.MaxPathLenZero = 0, true
|
||||
old.Cert = &oc
|
||||
if _, _, err := old.intermediate(&ikey.PublicKey, "x", dns, nil, time.Hour); err == nil {
|
||||
t.Error("path-length-0 root signed an intermediate")
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ func usage() {
|
||||
rollcall rack [-config /etc/rollcall/rack.json] a rack controller next to the machines
|
||||
rollcall rack join CODE -url http://ADDR [-name N] [-listen ADDR:80] enroll a rack controller (writes rack.json + certificate)
|
||||
rollcall ca show | issue NAME -host h1,h2 [-client] [-out DIR] the installation's certificate authority (region host)
|
||||
rollcall ca intermediate NAME -permit DOMAIN,CIDR [-csr FILE] [-days 365] | rotate -yes delegate to an ACME CA / replace the root
|
||||
rollcall version
|
||||
rollcall keygen
|
||||
rollcall list
|
||||
|
||||
Reference in new issue
Block a user