Files
archie/internal/infrastructure/egress/ca.go
T

178 lines
5.3 KiB
Go

package egress
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"io/fs"
"math/big"
"net"
"os"
"path/filepath"
"sync"
"time"
)
const (
caCertFile = "ca.pem"
caKeyFile = "ca-key.pem"
caLifetime = 10 * 365 * 24 * time.Hour
leafLifetime = 7 * 24 * time.Hour
)
// CA is the daemon's egress certificate authority. Sandbox containers trust
// it so the proxy can terminate their TLS; nothing outside a sandbox does.
// It persists across restarts because every running container was started
// trusting this exact certificate.
type CA struct {
cert *x509.Certificate
certPEM []byte
key *ecdsa.PrivateKey
mu sync.Mutex
leaves map[string]*tls.Certificate
}
// CACertPath is where LoadOrCreateCA keeps the CA certificate in dir, the
// file a sandbox container mounts to trust the proxy.
func CACertPath(dir string) string { return filepath.Join(dir, caCertFile) }
// LoadOrCreateCA loads the CA from dir, creating it on first use. A CA that
// exists but will not load is an error: minting a replacement silently
// would break trust in every running container.
func LoadOrCreateCA(dir string) (*CA, error) {
certPath, keyPath := filepath.Join(dir, caCertFile), filepath.Join(dir, caKeyFile)
certPEM, certErr := os.ReadFile(certPath)
keyPEM, keyErr := os.ReadFile(keyPath)
switch {
case certErr == nil && keyErr == nil:
return parseCA(certPEM, keyPEM)
case errors.Is(certErr, fs.ErrNotExist) && errors.Is(keyErr, fs.ErrNotExist):
return createCA(dir, certPath, keyPath)
case certErr != nil && !errors.Is(certErr, fs.ErrNotExist):
return nil, fmt.Errorf("read egress CA certificate: %w", certErr)
case keyErr != nil && !errors.Is(keyErr, fs.ErrNotExist):
return nil, fmt.Errorf("read egress CA key: %w", keyErr)
default:
return nil, fmt.Errorf("egress CA in %s is incomplete: one of %s and %s is missing", dir, caCertFile, caKeyFile)
}
}
func createCA(dir, certPath, keyPath string) (*CA, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
now := time.Now()
template := &x509.Certificate{
SerialNumber: serial(),
Subject: pkix.Name{CommonName: "archie egress CA"},
NotBefore: now.Add(-time.Hour),
NotAfter: now.Add(caLifetime),
IsCA: true,
BasicConstraintsValid: true,
MaxPathLenZero: true,
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
}
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
return nil, err
}
keyDER, err := x509.MarshalECPrivateKey(key)
if err != nil {
return nil, err
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER})
if err := os.MkdirAll(dir, 0o700); err != nil {
return nil, err
}
if err := os.WriteFile(keyPath, keyPEM, 0o600); err != nil {
return nil, err
}
if err := os.WriteFile(certPath, certPEM, 0o644); err != nil {
return nil, err
}
return parseCA(certPEM, keyPEM)
}
func parseCA(certPEM, keyPEM []byte) (*CA, error) {
certBlock, _ := pem.Decode(certPEM)
if certBlock == nil {
return nil, errors.New("egress CA certificate is not PEM")
}
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("parse egress CA certificate: %w", err)
}
keyBlock, _ := pem.Decode(keyPEM)
if keyBlock == nil {
return nil, errors.New("egress CA key is not PEM")
}
key, err := x509.ParseECPrivateKey(keyBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("parse egress CA key: %w", err)
}
if !key.PublicKey.Equal(cert.PublicKey) {
return nil, errors.New("egress CA key does not match its certificate")
}
return &CA{cert: cert, certPEM: certPEM, key: key, leaves: map[string]*tls.Certificate{}}, nil
}
// CertPEM is the certificate a sandbox container installs as trusted.
func (c *CA) CertPEM() []byte { return c.certPEM }
// leaf returns a certificate for host signed by the CA, issuing it once and
// reusing it until it nears expiry.
func (c *CA) leaf(host string) (*tls.Certificate, error) {
c.mu.Lock()
defer c.mu.Unlock()
if cached, ok := c.leaves[host]; ok && time.Until(cached.Leaf.NotAfter) > leafLifetime/2 {
return cached, nil
}
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
now := time.Now()
template := &x509.Certificate{
SerialNumber: serial(),
Subject: pkix.Name{CommonName: host},
NotBefore: now.Add(-time.Hour),
NotAfter: now.Add(leafLifetime),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
if ip := net.ParseIP(host); ip != nil {
template.IPAddresses = []net.IP{ip}
} else {
template.DNSNames = []string{host}
}
der, err := x509.CreateCertificate(rand.Reader, template, c.cert, &key.PublicKey, c.key)
if err != nil {
return nil, err
}
parsed, err := x509.ParseCertificate(der)
if err != nil {
return nil, err
}
issued := &tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key, Leaf: parsed}
c.leaves[host] = issued
return issued, nil
}
func serial() *big.Int {
n, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 127))
if err != nil {
panic(err)
}
return n
}