|
| 1 | +// Copyright 2025 Blindspot Software |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package tlsutil |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/ed25519" |
| 9 | + "crypto/rand" |
| 10 | + "crypto/tls" |
| 11 | + "crypto/x509" |
| 12 | + "crypto/x509/pkix" |
| 13 | + "encoding/pem" |
| 14 | + "fmt" |
| 15 | + "log" |
| 16 | + "math/big" |
| 17 | + "net" |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + "time" |
| 21 | +) |
| 22 | + |
| 23 | +// GenerateSelfSignedCert creates a new self-signed TLS certificate and private key. |
| 24 | +// The certificate is valid for 10 years and includes localhost and system hostname in SANs. |
| 25 | +// Uses Ed25519 for better performance and security compared to RSA. |
| 26 | +func GenerateSelfSignedCert(certPath, keyPath string) error { |
| 27 | + // Generate Ed25519 private key (much faster than RSA) |
| 28 | + publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader) |
| 29 | + if err != nil { |
| 30 | + return fmt.Errorf("failed to generate Ed25519 key: %w", err) |
| 31 | + } |
| 32 | + |
| 33 | + // Generate a random serial number |
| 34 | + serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) |
| 35 | + serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("failed to generate serial number: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + // Get system hostname for SANs |
| 41 | + hostname, err := os.Hostname() |
| 42 | + if err != nil { |
| 43 | + hostname = "localhost" // Fallback if hostname detection fails |
| 44 | + } |
| 45 | + |
| 46 | + // Create certificate template |
| 47 | + template := x509.Certificate{ |
| 48 | + SerialNumber: serialNumber, |
| 49 | + Subject: pkix.Name{ |
| 50 | + Organization: []string{"Blindspot Software"}, |
| 51 | + CommonName: "dutagent", |
| 52 | + }, |
| 53 | + NotBefore: time.Now(), |
| 54 | + NotAfter: time.Now().Add(10 * 365 * 24 * time.Hour), // 10 years |
| 55 | + KeyUsage: x509.KeyUsageDigitalSignature, |
| 56 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 57 | + BasicConstraintsValid: true, |
| 58 | + DNSNames: []string{"localhost", hostname}, |
| 59 | + IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}, |
| 60 | + } |
| 61 | + |
| 62 | + // Create self-signed certificate |
| 63 | + derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey, privateKey) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("failed to create certificate: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + // Write certificate to file with secure permissions |
| 69 | + certOut, err := os.OpenFile(certPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("failed to create cert file: %w", err) |
| 72 | + } |
| 73 | + defer certOut.Close() |
| 74 | + |
| 75 | + if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil { |
| 76 | + return fmt.Errorf("failed to write certificate: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Marshal Ed25519 private key in PKCS8 format |
| 80 | + privBytes, err := x509.MarshalPKCS8PrivateKey(privateKey) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("failed to marshal private key: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + // Write private key to file with restrictive permissions |
| 86 | + keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to create key file: %w", err) |
| 89 | + } |
| 90 | + defer keyOut.Close() |
| 91 | + |
| 92 | + if err := pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil { |
| 93 | + return fmt.Errorf("failed to write private key: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + log.Printf("Generated Ed25519 self-signed TLS certificate: %s", certPath) |
| 97 | + log.Printf("Generated private key: %s", keyPath) |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// LoadOrGenerateCert attempts to load an existing TLS certificate/key pair. |
| 103 | +// If the files don't exist, it generates a new self-signed certificate. |
| 104 | +// If the files exist but cannot be loaded, it returns an error without overwriting them. |
| 105 | +func LoadOrGenerateCert(certPath, keyPath string) (tls.Certificate, error) { |
| 106 | + // Check if certificate and key files exist |
| 107 | + certExists := fileExists(certPath) |
| 108 | + keyExists := fileExists(keyPath) |
| 109 | + |
| 110 | + // If either file exists, we must load them (don't auto-generate) |
| 111 | + if certExists || keyExists { |
| 112 | + cert, err := tls.LoadX509KeyPair(certPath, keyPath) |
| 113 | + if err != nil { |
| 114 | + return tls.Certificate{}, fmt.Errorf("certificate/key files exist but failed to load (cert exists: %v, key exists: %v): %w", |
| 115 | + certExists, keyExists, err) |
| 116 | + } |
| 117 | + log.Printf("Loaded existing TLS certificate from: %s", certPath) |
| 118 | + return cert, nil |
| 119 | + } |
| 120 | + |
| 121 | + // Neither file exists, generate new certificate |
| 122 | + log.Printf("TLS certificate not found, generating new self-signed certificate...") |
| 123 | + |
| 124 | + // Derive directory from cert path |
| 125 | + certDir := filepath.Dir(certPath) |
| 126 | + keyDir := filepath.Dir(keyPath) |
| 127 | + |
| 128 | + // Ensure directories exist |
| 129 | + if err := os.MkdirAll(certDir, 0755); err != nil { |
| 130 | + return tls.Certificate{}, fmt.Errorf("failed to create cert directory: %w", err) |
| 131 | + } |
| 132 | + if certDir != keyDir { |
| 133 | + if err := os.MkdirAll(keyDir, 0755); err != nil { |
| 134 | + return tls.Certificate{}, fmt.Errorf("failed to create key directory: %w", err) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Generate certificate |
| 139 | + if err := GenerateSelfSignedCert(certPath, keyPath); err != nil { |
| 140 | + return tls.Certificate{}, err |
| 141 | + } |
| 142 | + |
| 143 | + // Load the newly generated certificate |
| 144 | + cert, err := tls.LoadX509KeyPair(certPath, keyPath) |
| 145 | + if err != nil { |
| 146 | + return tls.Certificate{}, fmt.Errorf("failed to load generated certificate: %w", err) |
| 147 | + } |
| 148 | + |
| 149 | + return cert, nil |
| 150 | +} |
| 151 | + |
| 152 | +// fileExists checks if a file exists and is not a directory. |
| 153 | +func fileExists(path string) bool { |
| 154 | + info, err := os.Stat(path) |
| 155 | + if err != nil { |
| 156 | + return false |
| 157 | + } |
| 158 | + return !info.IsDir() |
| 159 | +} |
0 commit comments