Files
mtusi2026lab7-fork/trust.go
T

131 lines
3.2 KiB
Go

package main
import (
"crypto/tls"
"crypto/x509"
"errors"
"net"
"net/http"
"os"
"slices"
"strings"
"time"
"github.com/redis/go-redis/v9"
)
const tlsDir = "/run/lab-tls"
const revocations = "/run/lab-revocation/serials"
func trustRoots() (*x509.CertPool, error) {
pem, err := os.ReadFile(tlsDir + "/ca.crt")
roots := x509.NewCertPool()
if err != nil || !roots.AppendCertsFromPEM(pem) {
return nil, errors.New("cannot load TLS CA")
}
return roots, nil
}
func leaf(dir string) (tls.Certificate, error) {
pair, err := tls.LoadX509KeyPair(dir+"/tls.crt", dir+"/tls.key")
if err != nil {
return pair, errors.New("cannot load TLS leaf")
}
return pair, nil
}
func clientTLS(mutual bool) (*tls.Config, error) {
roots, err := trustRoots()
if err != nil {
return nil, err
}
cfg := &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: roots}
if mutual {
pair, err := leaf(tlsDir)
if err != nil {
return nil, err
}
cfg.Certificates = []tls.Certificate{pair}
}
return cfg, nil
}
func trustedPeer(cert *x509.Certificate) error {
data, err := os.ReadFile(revocations)
if err != nil {
return errors.New("revocation data unavailable")
}
for _, serial := range strings.Fields(string(data)) {
if strings.EqualFold(cert.SerialNumber.Text(16), serial) {
return errors.New("client leaf revoked")
}
}
if !time.Now().Before(cert.NotAfter) {
return errors.New("client leaf expired")
}
return nil
}
func clientIdentity(cert *x509.Certificate) string {
if len(cert.DNSNames) != 1 {
return ""
}
return cert.DNSNames[0]
}
func serverTLS(allowed []string) (*tls.Config, error) {
pair, err := leaf(env("LAB_SERVER_TLS_DIR", tlsDir))
if err != nil {
return nil, err
}
cfg := &tls.Config{MinVersion: tls.VersionTLS12, Certificates: []tls.Certificate{pair}, SessionTicketsDisabled: true}
if len(allowed) == 0 {
return cfg, nil
}
cfg.ClientCAs, err = trustRoots()
if err != nil {
return nil, err
}
cfg.ClientAuth = tls.RequireAndVerifyClientCert
cfg.VerifyConnection = func(state tls.ConnectionState) error {
cert := state.PeerCertificates[0]
if !slices.Contains(allowed, clientIdentity(cert)) {
return errors.New("client identity denied")
}
return trustedPeer(cert)
}
return cfg, nil
}
func labHTTPTransport(mutual bool) (*http.Transport, error) {
cfg, err := clientTLS(mutual)
if err != nil {
return nil, err
}
return &http.Transport{
TLSClientConfig: cfg, DialContext: (&net.Dialer{Timeout: 2 * time.Second}).DialContext,
MaxIdleConns: 16, MaxIdleConnsPerHost: 8, MaxConnsPerHost: 16, IdleConnTimeout: 30 * time.Second,
ResponseHeaderTimeout: 5 * time.Second, TLSHandshakeTimeout: 3 * time.Second,
DisableCompression: true,
}, nil
}
func redisTLS(rawURL string, mutual bool) (*redis.Options, error) {
opts, err := redis.ParseURL(rawURL)
if err != nil || opts.TLSConfig == nil || opts.Username == "" || opts.Password == "" {
return nil, errors.New("invalid TLS Valkey configuration")
}
host, _, err := net.SplitHostPort(opts.Addr)
if err != nil {
return nil, errors.New("invalid Valkey address")
}
opts.TLSConfig, err = clientTLS(mutual)
if err != nil {
return nil, err
}
opts.TLSConfig.ServerName = host
opts.Protocol = 2
opts.DisableIdentity = true
return opts, nil
}