2018-04-24 20:40:04 +00:00
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2018-11-27 16:42:04 +00:00
|
|
|
"crypto/x509"
|
2018-07-06 08:30:03 +00:00
|
|
|
"net"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-04-24 20:40:04 +00:00
|
|
|
|
2018-07-06 08:30:03 +00:00
|
|
|
"github.com/patrickmn/go-cache"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/safe"
|
2018-04-24 20:40:04 +00:00
|
|
|
)
|
|
|
|
|
2020-07-21 16:06:04 +00:00
|
|
|
// CertificateStore store for dynamic certificates.
|
2018-04-24 20:40:04 +00:00
|
|
|
type CertificateStore struct {
|
2018-07-06 08:30:03 +00:00
|
|
|
DynamicCerts *safe.Safe
|
|
|
|
DefaultCertificate *tls.Certificate
|
|
|
|
CertCache *cache.Cache
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:06:04 +00:00
|
|
|
// NewCertificateStore create a store for dynamic certificates.
|
2018-07-06 08:30:03 +00:00
|
|
|
func NewCertificateStore() *CertificateStore {
|
2022-09-13 18:34:08 +00:00
|
|
|
s := &safe.Safe{}
|
|
|
|
s.Set(make(map[string]*tls.Certificate))
|
|
|
|
|
2018-07-06 08:30:03 +00:00
|
|
|
return &CertificateStore{
|
2022-09-13 18:34:08 +00:00
|
|
|
DynamicCerts: s,
|
2018-07-06 08:30:03 +00:00
|
|
|
CertCache: cache.New(1*time.Hour, 10*time.Minute),
|
|
|
|
}
|
2018-04-24 20:40:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 16:42:04 +00:00
|
|
|
func (c CertificateStore) getDefaultCertificateDomains() []string {
|
2018-04-24 20:40:04 +00:00
|
|
|
var allCerts []string
|
|
|
|
|
2018-11-27 16:42:04 +00:00
|
|
|
if c.DefaultCertificate == nil {
|
|
|
|
return allCerts
|
|
|
|
}
|
|
|
|
|
|
|
|
x509Cert, err := x509.ParseCertificate(c.DefaultCertificate.Certificate[0])
|
|
|
|
if err != nil {
|
2020-07-21 16:06:04 +00:00
|
|
|
log.WithoutContext().Errorf("Could not parse default certificate: %v", err)
|
2018-11-27 16:42:04 +00:00
|
|
|
return allCerts
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(x509Cert.Subject.CommonName) > 0 {
|
|
|
|
allCerts = append(allCerts, x509Cert.Subject.CommonName)
|
|
|
|
}
|
|
|
|
|
|
|
|
allCerts = append(allCerts, x509Cert.DNSNames...)
|
|
|
|
|
|
|
|
for _, ipSan := range x509Cert.IPAddresses {
|
|
|
|
allCerts = append(allCerts, ipSan.String())
|
2018-04-24 20:40:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 16:42:04 +00:00
|
|
|
return allCerts
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// GetAllDomains return a slice with all the certificate domain.
|
2018-11-27 16:42:04 +00:00
|
|
|
func (c CertificateStore) GetAllDomains() []string {
|
2020-12-18 17:44:03 +00:00
|
|
|
allDomains := c.getDefaultCertificateDomains()
|
2018-11-27 16:42:04 +00:00
|
|
|
|
2018-04-24 20:40:04 +00:00
|
|
|
// Get dynamic certificates
|
|
|
|
if c.DynamicCerts != nil && c.DynamicCerts.Get() != nil {
|
2021-08-23 08:13:31 +00:00
|
|
|
for domain := range c.DynamicCerts.Get().(map[string]*Certificate) {
|
2020-12-18 17:44:03 +00:00
|
|
|
allDomains = append(allDomains, domain)
|
2018-04-24 20:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-18 17:44:03 +00:00
|
|
|
|
|
|
|
return allDomains
|
2018-04-24 20:40:04 +00:00
|
|
|
}
|
2018-07-06 08:30:03 +00:00
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// GetBestCertificate returns the best match certificate, and caches the response.
|
2021-08-23 08:13:31 +00:00
|
|
|
func (c *CertificateStore) GetBestCertificate(clientHello *tls.ClientHelloInfo) *Certificate {
|
2021-06-14 08:06:05 +00:00
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-12 14:16:08 +00:00
|
|
|
serverName := strings.ToLower(strings.TrimSpace(clientHello.ServerName))
|
|
|
|
if len(serverName) == 0 {
|
2018-07-06 08:30:03 +00:00
|
|
|
// If no ServerName is provided, Check for local IP address matches
|
|
|
|
host, _, err := net.SplitHostPort(clientHello.Conn.LocalAddr().String())
|
|
|
|
if err != nil {
|
2022-07-12 14:16:08 +00:00
|
|
|
log.WithoutContext().Debugf("Could not split host/port: %v", err)
|
2018-07-06 08:30:03 +00:00
|
|
|
}
|
2022-07-12 14:16:08 +00:00
|
|
|
serverName = strings.TrimSpace(host)
|
2018-07-06 08:30:03 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:16:08 +00:00
|
|
|
if cert, ok := c.CertCache.Get(serverName); ok {
|
2021-08-23 08:13:31 +00:00
|
|
|
return cert.(*Certificate)
|
2018-07-06 08:30:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 08:13:31 +00:00
|
|
|
matchedCerts := map[string]*Certificate{}
|
2018-07-06 08:30:03 +00:00
|
|
|
if c.DynamicCerts != nil && c.DynamicCerts.Get() != nil {
|
2021-08-23 08:13:31 +00:00
|
|
|
for domains, cert := range c.DynamicCerts.Get().(map[string]*Certificate) {
|
2018-07-06 08:30:03 +00:00
|
|
|
for _, certDomain := range strings.Split(domains, ",") {
|
2022-07-12 14:16:08 +00:00
|
|
|
if matchDomain(serverName, certDomain) {
|
2018-07-06 08:30:03 +00:00
|
|
|
matchedCerts[certDomain] = cert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matchedCerts) > 0 {
|
|
|
|
// sort map by keys
|
|
|
|
keys := make([]string, 0, len(matchedCerts))
|
|
|
|
for k := range matchedCerts {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
// cache best match
|
2022-07-12 14:16:08 +00:00
|
|
|
c.CertCache.SetDefault(serverName, matchedCerts[keys[len(keys)-1]])
|
2018-07-06 08:30:03 +00:00
|
|
|
return matchedCerts[keys[len(keys)-1]]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 18:34:08 +00:00
|
|
|
// GetCertificate returns the first certificate matching all the given domains.
|
|
|
|
func (c *CertificateStore) GetCertificate(domains []string) *tls.Certificate {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(domains)
|
|
|
|
domainsKey := strings.Join(domains, ",")
|
|
|
|
|
|
|
|
if cert, ok := c.CertCache.Get(domainsKey); ok {
|
|
|
|
return cert.(*tls.Certificate)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DynamicCerts != nil && c.DynamicCerts.Get() != nil {
|
|
|
|
for certDomains, cert := range c.DynamicCerts.Get().(map[string]*tls.Certificate) {
|
|
|
|
if domainsKey == certDomains {
|
|
|
|
c.CertCache.SetDefault(domainsKey, cert)
|
|
|
|
return cert
|
|
|
|
}
|
|
|
|
|
|
|
|
var matchedDomains []string
|
|
|
|
for _, certDomain := range strings.Split(certDomains, ",") {
|
|
|
|
for _, checkDomain := range domains {
|
|
|
|
if certDomain == checkDomain {
|
|
|
|
matchedDomains = append(matchedDomains, certDomain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matchedDomains) == len(domains) {
|
|
|
|
c.CertCache.SetDefault(domainsKey, cert)
|
|
|
|
return cert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// ResetCache clears the cache in the store.
|
2018-07-06 08:30:03 +00:00
|
|
|
func (c CertificateStore) ResetCache() {
|
|
|
|
if c.CertCache != nil {
|
|
|
|
c.CertCache.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 14:16:08 +00:00
|
|
|
// matchDomain returns whether the server name matches the cert domain.
|
|
|
|
// The server name, from TLS SNI, must not have trailing dots (https://datatracker.ietf.org/doc/html/rfc6066#section-3).
|
|
|
|
// This is enforced by https://github.com/golang/go/blob/d3d7998756c33f69706488cade1cd2b9b10a4c7f/src/crypto/tls/handshake_messages.go#L423-L427.
|
|
|
|
func matchDomain(serverName, certDomain string) bool {
|
|
|
|
// TODO: assert equality after removing the trailing dots?
|
|
|
|
if serverName == certDomain {
|
2018-07-06 08:30:03 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for len(certDomain) > 0 && certDomain[len(certDomain)-1] == '.' {
|
|
|
|
certDomain = certDomain[:len(certDomain)-1]
|
|
|
|
}
|
|
|
|
|
2022-07-12 14:16:08 +00:00
|
|
|
labels := strings.Split(serverName, ".")
|
2018-07-06 08:30:03 +00:00
|
|
|
for i := range labels {
|
|
|
|
labels[i] = "*"
|
|
|
|
candidate := strings.Join(labels, ".")
|
|
|
|
if certDomain == candidate {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|