99 lines
2.9 KiB
Go
99 lines
2.9 KiB
Go
// Package security implements the lab's explicit trust boundaries.
|
|
package security
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func roots() (*x509.CertPool, error) {
|
|
b, e := os.ReadFile(os.Getenv("TLS_CA_FILE"))
|
|
if e != nil {
|
|
return nil, fmt.Errorf("TLS CA unavailable")
|
|
}
|
|
p := x509.NewCertPool()
|
|
if !p.AppendCertsFromPEM(b) {
|
|
return nil, fmt.Errorf("invalid TLS CA")
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// Recheck revocation on EVERY HTTP request, including an already-open TLS
|
|
// connection. An in-flight request remains bounded by the HTTP deadlines.
|
|
func AuthorizeHTTP(c *tls.Config, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if c.VerifyConnection != nil && (r.TLS == nil || c.VerifyConnection(*r.TLS) != nil) {
|
|
w.Header().Set("Connection", "close")
|
|
http.Error(w, "service identity forbidden", 403)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
func pair() (*tls.Certificate, error) {
|
|
c, e := tls.LoadX509KeyPair(os.Getenv("TLS_CERT_FILE"), os.Getenv("TLS_KEY_FILE"))
|
|
if e != nil {
|
|
return nil, fmt.Errorf("TLS identity unavailable")
|
|
}
|
|
return &c, nil
|
|
}
|
|
func ClientTLS() (*tls.Config, error) {
|
|
p, e := roots()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
return &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: p, GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) { return pair() }}, nil
|
|
}
|
|
|
|
// VerifyConnection runs even on resumed handshakes. Current CA-validated chain,
|
|
// exact DNS SAN and serial denylist all matter, not the certificate CN alone.
|
|
func ServerTLS(identities []string) (*tls.Config, error) {
|
|
p, e := roots()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
c := &tls.Config{MinVersion: tls.VersionTLS12, ClientCAs: p, GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return pair() }, SessionTicketsDisabled: true}
|
|
if len(identities) == 0 {
|
|
return c, nil
|
|
}
|
|
c.ClientAuth = tls.RequireAndVerifyClientCert
|
|
c.VerifyConnection = func(s tls.ConnectionState) error {
|
|
if len(s.VerifiedChains) == 0 || len(s.PeerCertificates) == 0 {
|
|
return fmt.Errorf("unverified peer")
|
|
}
|
|
leaf := s.PeerCertificates[0]
|
|
now := time.Now()
|
|
if now.Before(leaf.NotBefore) || !now.Before(leaf.NotAfter) {
|
|
return fmt.Errorf("identity expired")
|
|
}
|
|
b, e := os.ReadFile(os.Getenv("TLS_REVOCATIONS_FILE"))
|
|
if e != nil {
|
|
return fmt.Errorf("revocation state unavailable")
|
|
}
|
|
var revoked []string
|
|
if json.Unmarshal(b, &revoked) != nil {
|
|
return fmt.Errorf("invalid revocation state")
|
|
}
|
|
for _, serial := range revoked {
|
|
if strings.EqualFold(serial, leaf.SerialNumber.Text(16)) {
|
|
return fmt.Errorf("identity revoked")
|
|
}
|
|
}
|
|
for _, allowed := range identities {
|
|
for _, name := range leaf.DNSNames {
|
|
if name == allowed+".client.lab" {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return fmt.Errorf("identity not allowed")
|
|
}
|
|
return c, nil
|
|
}
|