2022-03-17 17:42:10 +00:00
|
|
|
package netutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2024-02-12 19:59:41 +00:00
|
|
|
"strconv"
|
2022-04-16 12:07:07 +00:00
|
|
|
"strings"
|
2022-03-17 17:42:10 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
|
|
|
)
|
|
|
|
|
2022-04-16 13:32:17 +00:00
|
|
|
// GetServerTLSConfig returns TLS config for the server.
|
2022-09-26 14:57:59 +00:00
|
|
|
func GetServerTLSConfig(tlsCertFile, tlsKeyFile, tlsMinVersion string, tlsCipherSuites []string) (*tls.Config, error) {
|
2024-04-17 20:10:40 +00:00
|
|
|
minVersion, err := ParseTLSVersion(tlsMinVersion)
|
2022-03-17 17:42:10 +00:00
|
|
|
if err != nil {
|
2024-04-17 20:10:40 +00:00
|
|
|
return nil, fmt.Errorf("cannnot use TLS min version from tlsMinVersion=%q. Supported TLS versions (TLS10, TLS11, TLS12, TLS13): %w", tlsMinVersion, err)
|
2022-03-17 17:42:10 +00:00
|
|
|
}
|
2022-04-16 12:27:21 +00:00
|
|
|
cipherSuites, err := cipherSuitesFromNames(tlsCipherSuites)
|
2022-04-16 12:07:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot use TLS cipher suites from tlsCipherSuites=%q: %w", tlsCipherSuites, err)
|
|
|
|
}
|
2022-03-17 17:42:10 +00:00
|
|
|
cfg := &tls.Config{
|
2022-09-26 14:35:45 +00:00
|
|
|
MinVersion: minVersion,
|
2024-04-17 20:10:40 +00:00
|
|
|
|
2022-09-26 14:35:45 +00:00
|
|
|
// Do not set MaxVersion, since this has no sense from security PoV.
|
|
|
|
// This can only result in lower security level if improperly set.
|
2024-04-17 20:10:40 +00:00
|
|
|
|
2022-04-16 12:07:07 +00:00
|
|
|
CipherSuites: cipherSuites,
|
2022-03-17 17:42:10 +00:00
|
|
|
}
|
2024-04-17 20:10:40 +00:00
|
|
|
|
|
|
|
cfg.GetCertificate = newGetCertificateFunc(tlsCertFile, tlsKeyFile)
|
2022-03-17 17:42:10 +00:00
|
|
|
return cfg, nil
|
|
|
|
}
|
2022-04-16 12:07:07 +00:00
|
|
|
|
2024-04-17 20:10:40 +00:00
|
|
|
func newGetCertificateFunc(tlsCertFile, tlsKeyFile string) func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
var certLock sync.Mutex
|
|
|
|
var certDeadline uint64
|
|
|
|
var cert *tls.Certificate
|
|
|
|
return func(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
certLock.Lock()
|
|
|
|
defer certLock.Unlock()
|
|
|
|
if fasttime.UnixTimestamp() > certDeadline {
|
|
|
|
c, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot load TLS cert from certFile=%q, keyFile=%q: %w", tlsCertFile, tlsKeyFile, err)
|
|
|
|
}
|
|
|
|
certDeadline = fasttime.UnixTimestamp() + 1
|
|
|
|
cert = &c
|
|
|
|
}
|
|
|
|
return cert, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 12:27:21 +00:00
|
|
|
func cipherSuitesFromNames(cipherSuiteNames []string) ([]uint16, error) {
|
|
|
|
if len(cipherSuiteNames) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-02-12 19:59:41 +00:00
|
|
|
|
2022-04-16 12:27:21 +00:00
|
|
|
css := tls.CipherSuites()
|
2024-02-12 19:59:41 +00:00
|
|
|
|
|
|
|
cssByName := make(map[string]uint16, len(css))
|
2022-04-16 12:27:21 +00:00
|
|
|
for _, cs := range css {
|
2024-02-12 19:59:41 +00:00
|
|
|
cssByName[strings.ToLower(cs.Name)] = cs.ID
|
2022-04-16 12:07:07 +00:00
|
|
|
}
|
2024-02-12 19:59:41 +00:00
|
|
|
|
|
|
|
cssByID := make(map[uint16]bool, len(css))
|
|
|
|
for _, cs := range css {
|
|
|
|
cssByID[cs.ID] = true
|
|
|
|
}
|
|
|
|
|
2022-04-16 12:27:21 +00:00
|
|
|
cipherSuites := make([]uint16, 0, len(cipherSuiteNames))
|
|
|
|
for _, name := range cipherSuiteNames {
|
2024-02-12 19:59:41 +00:00
|
|
|
id, ok := cssByName[strings.ToLower(name)]
|
2022-04-16 12:07:07 +00:00
|
|
|
if !ok {
|
2024-02-12 19:59:41 +00:00
|
|
|
// Try searching by ID
|
|
|
|
idKey, err := strconv.ParseUint(name, 0, 16)
|
|
|
|
if err != nil || !cssByID[uint16(idKey)] {
|
|
|
|
return nil, fmt.Errorf("unsupported TLS cipher suite name: %s", name)
|
|
|
|
}
|
|
|
|
id = uint16(idKey)
|
2022-04-16 12:07:07 +00:00
|
|
|
}
|
|
|
|
cipherSuites = append(cipherSuites, id)
|
|
|
|
}
|
|
|
|
return cipherSuites, nil
|
|
|
|
}
|
2022-09-26 14:35:45 +00:00
|
|
|
|
|
|
|
// ParseTLSVersion returns tls version from the given string s.
|
|
|
|
func ParseTLSVersion(s string) (uint16, error) {
|
|
|
|
switch strings.ToUpper(s) {
|
|
|
|
case "":
|
|
|
|
// Special case - use default TLS version provided by tls package.
|
|
|
|
return 0, nil
|
|
|
|
case "TLS13":
|
|
|
|
return tls.VersionTLS13, nil
|
|
|
|
case "TLS12":
|
|
|
|
return tls.VersionTLS12, nil
|
|
|
|
case "TLS11":
|
|
|
|
return tls.VersionTLS11, nil
|
|
|
|
case "TLS10":
|
|
|
|
return tls.VersionTLS10, nil
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("unsupported TLS version %q", s)
|
|
|
|
}
|
|
|
|
}
|