2014-11-18 16:03:36 +00:00
|
|
|
package tlsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2019-06-27 20:22:07 +00:00
|
|
|
"io/ioutil"
|
2014-11-18 16:03:36 +00:00
|
|
|
"net"
|
2019-06-27 20:22:07 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-02-19 22:22:31 +00:00
|
|
|
"sort"
|
2015-05-11 23:05:39 +00:00
|
|
|
"strings"
|
2019-02-27 09:14:59 +00:00
|
|
|
"sync"
|
2014-11-18 16:03:36 +00:00
|
|
|
"time"
|
2020-02-19 22:22:31 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/logging"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2014-11-18 16:03:36 +00:00
|
|
|
)
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// ALPNWrapper is a function that is used to wrap a non-TLS connection and
|
|
|
|
// returns an appropriate TLS connection or error. This taks a datacenter and
|
|
|
|
// node name as argument to configure the desired SNI value and the desired
|
|
|
|
// next proto for configuring ALPN.
|
|
|
|
type ALPNWrapper func(dc, nodeName, alpnProto string, conn net.Conn) (net.Conn, error)
|
|
|
|
|
2015-05-11 22:14:56 +00:00
|
|
|
// DCWrapper is a function that is used to wrap a non-TLS connection
|
|
|
|
// and returns an appropriate TLS connection or error. This takes
|
|
|
|
// a datacenter as an argument.
|
|
|
|
type DCWrapper func(dc string, conn net.Conn) (net.Conn, error)
|
|
|
|
|
|
|
|
// Wrapper is a variant of DCWrapper, where the DC is provided as
|
|
|
|
// a constant value. This is usually done by currying DCWrapper.
|
|
|
|
type Wrapper func(conn net.Conn) (net.Conn, error)
|
2015-05-08 22:54:42 +00:00
|
|
|
|
2017-02-01 20:52:04 +00:00
|
|
|
// TLSLookup maps the tls_min_version configuration to the internal value
|
|
|
|
var TLSLookup = map[string]uint16{
|
2019-03-13 09:29:06 +00:00
|
|
|
"": tls.VersionTLS10, // default in golang
|
2017-02-01 20:52:04 +00:00
|
|
|
"tls10": tls.VersionTLS10,
|
|
|
|
"tls11": tls.VersionTLS11,
|
|
|
|
"tls12": tls.VersionTLS12,
|
2020-02-19 22:22:31 +00:00
|
|
|
"tls13": tls.VersionTLS13,
|
2017-02-01 20:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 22:22:31 +00:00
|
|
|
// TLSVersions has all the keys from the map above.
|
|
|
|
var TLSVersions = strings.Join(tlsVersions(), ", ")
|
|
|
|
|
2014-11-18 16:03:36 +00:00
|
|
|
// Config used to create tls.Config
|
|
|
|
type Config struct {
|
2019-03-05 20:35:43 +00:00
|
|
|
// VerifyIncoming is used to verify the authenticity of incoming
|
|
|
|
// connections. This means that TCP requests are forbidden, only
|
|
|
|
// allowing for TLS. TLS connections must match a provided certificate
|
|
|
|
// authority. This can be used to force client auth.
|
2019-02-27 09:14:59 +00:00
|
|
|
VerifyIncoming bool
|
|
|
|
|
2019-03-05 20:35:43 +00:00
|
|
|
// VerifyIncomingRPC is used to verify the authenticity of incoming RPC
|
|
|
|
// connections. This means that TCP requests are forbidden, only
|
|
|
|
// allowing for TLS. TLS connections must match a provided certificate
|
|
|
|
// authority. This can be used to force client auth.
|
2019-02-27 09:14:59 +00:00
|
|
|
VerifyIncomingRPC bool
|
|
|
|
|
2019-03-05 20:35:43 +00:00
|
|
|
// VerifyIncomingHTTPS is used to verify the authenticity of incoming
|
|
|
|
// HTTPS connections. This means that TCP requests are forbidden, only
|
|
|
|
// allowing for TLS. TLS connections must match a provided certificate
|
|
|
|
// authority. This can be used to force client auth.
|
2019-02-26 15:52:07 +00:00
|
|
|
VerifyIncomingHTTPS bool
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2019-03-05 20:35:43 +00:00
|
|
|
// VerifyOutgoing is used to verify the authenticity of outgoing
|
|
|
|
// connections. This means that TLS requests are used, and TCP
|
|
|
|
// requests are not made. TLS connections must match a provided
|
|
|
|
// certificate authority. This is used to verify authenticity of server
|
|
|
|
// nodes.
|
2014-11-18 16:03:36 +00:00
|
|
|
VerifyOutgoing bool
|
|
|
|
|
2019-03-05 20:35:43 +00:00
|
|
|
// VerifyServerHostname is used to enable hostname verification of
|
|
|
|
// servers. This ensures that the certificate presented is valid for
|
|
|
|
// server.<datacenter>.<domain>. This prevents a compromised client
|
|
|
|
// from being restarted as a server, and then intercepting request
|
|
|
|
// traffic as well as being added as a raft peer. This should be
|
|
|
|
// enabled by default with VerifyOutgoing, but for legacy reasons we
|
|
|
|
// cannot break existing clients.
|
2015-05-11 22:14:56 +00:00
|
|
|
VerifyServerHostname bool
|
|
|
|
|
2017-05-10 21:25:48 +00:00
|
|
|
// UseTLS is used to enable outgoing TLS connections to Consul servers.
|
|
|
|
UseTLS bool
|
|
|
|
|
2019-03-05 20:35:43 +00:00
|
|
|
// CAFile is a path to a certificate authority file. This is used with
|
|
|
|
// VerifyIncoming or VerifyOutgoing to verify the TLS connection.
|
2014-11-18 16:03:36 +00:00
|
|
|
CAFile string
|
|
|
|
|
2019-03-05 20:35:43 +00:00
|
|
|
// CAPath is a path to a directory containing certificate authority
|
|
|
|
// files. This is used with VerifyIncoming or VerifyOutgoing to verify
|
|
|
|
// the TLS connection.
|
2017-04-27 08:29:39 +00:00
|
|
|
CAPath string
|
|
|
|
|
2019-03-05 20:35:43 +00:00
|
|
|
// CertFile is used to provide a TLS certificate that is used for
|
|
|
|
// serving TLS connections. Must be provided to serve TLS connections.
|
2014-11-18 16:03:36 +00:00
|
|
|
CertFile string
|
|
|
|
|
2019-03-05 20:35:43 +00:00
|
|
|
// KeyFile is used to provide a TLS key that is used for serving TLS
|
|
|
|
// connections. Must be provided to serve TLS connections.
|
2014-11-18 16:03:36 +00:00
|
|
|
KeyFile string
|
|
|
|
|
|
|
|
// Node name is the name we use to advertise. Defaults to hostname.
|
|
|
|
NodeName string
|
|
|
|
|
|
|
|
// ServerName is used with the TLS certificate to ensure the name we
|
|
|
|
// provide matches the certificate
|
|
|
|
ServerName string
|
2015-05-11 22:14:56 +00:00
|
|
|
|
|
|
|
// Domain is the Consul TLD being used. Defaults to "consul."
|
|
|
|
Domain string
|
2017-02-01 20:52:04 +00:00
|
|
|
|
|
|
|
// TLSMinVersion is the minimum accepted TLS version that can be used.
|
|
|
|
TLSMinVersion string
|
2017-04-27 08:29:39 +00:00
|
|
|
|
|
|
|
// CipherSuites is the list of TLS cipher suites to use.
|
|
|
|
CipherSuites []uint16
|
|
|
|
|
2019-03-05 20:35:43 +00:00
|
|
|
// PreferServerCipherSuites specifies whether to prefer the server's
|
|
|
|
// ciphersuite over the client ciphersuites.
|
2017-04-27 08:29:39 +00:00
|
|
|
PreferServerCipherSuites bool
|
2019-02-26 15:52:07 +00:00
|
|
|
|
2019-02-27 09:14:59 +00:00
|
|
|
// EnableAgentTLSForChecks is used to apply the agent's TLS settings in
|
|
|
|
// order to configure the HTTP client used for health checks. Enabling
|
|
|
|
// this allows HTTP checks to present a client certificate and verify
|
|
|
|
// the server using the same TLS configuration as the agent (CA, cert,
|
|
|
|
// and key).
|
2019-02-26 15:52:07 +00:00
|
|
|
EnableAgentTLSForChecks bool
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
// AutoEncryptTLS opts the agent into provisioning agent
|
|
|
|
// TLS certificates.
|
|
|
|
AutoEncryptTLS bool
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 22:22:31 +00:00
|
|
|
func tlsVersions() []string {
|
|
|
|
versions := []string{}
|
|
|
|
for v := range TLSLookup {
|
|
|
|
if v != "" {
|
|
|
|
versions = append(versions, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(versions)
|
|
|
|
return versions
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:03:36 +00:00
|
|
|
// KeyPair is used to open and parse a certificate and key file
|
|
|
|
func (c *Config) KeyPair() (*tls.Certificate, error) {
|
2019-03-13 09:29:06 +00:00
|
|
|
return loadKeyPair(c.CertFile, c.KeyFile)
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 22:14:56 +00:00
|
|
|
// SpecificDC is used to invoke a static datacenter
|
|
|
|
// and turns a DCWrapper into a Wrapper type.
|
|
|
|
func SpecificDC(dc string, tlsWrap DCWrapper) Wrapper {
|
|
|
|
if tlsWrap == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return func(conn net.Conn) (net.Conn, error) {
|
|
|
|
return tlsWrap(dc, conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
type autoEncrypt struct {
|
|
|
|
manualCAPems []string
|
|
|
|
connectCAPems []string
|
|
|
|
cert *tls.Certificate
|
|
|
|
verifyServerHostname bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *autoEncrypt) caPems() []string {
|
|
|
|
return append(a.manualCAPems, a.connectCAPems...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type manual struct {
|
|
|
|
caPems []string
|
|
|
|
cert *tls.Certificate
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// Configurator holds a Config and is responsible for generating all the
|
|
|
|
// *tls.Config necessary for Consul. Except the one in the api package.
|
|
|
|
type Configurator struct {
|
|
|
|
sync.RWMutex
|
2020-04-30 20:12:17 +00:00
|
|
|
base *Config
|
|
|
|
autoEncrypt *autoEncrypt
|
|
|
|
manual *manual
|
|
|
|
peerDatacenterUseTLS map[string]bool
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
caPool *x509.CertPool
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.Logger
|
2019-03-13 09:29:06 +00:00
|
|
|
version int
|
|
|
|
}
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// NewConfigurator creates a new Configurator and sets the provided
|
|
|
|
// configuration.
|
2020-01-28 23:50:41 +00:00
|
|
|
func NewConfigurator(config Config, logger hclog.Logger) (*Configurator, error) {
|
|
|
|
if logger == nil {
|
|
|
|
logger = hclog.New(&hclog.LoggerOptions{
|
|
|
|
Level: hclog.Debug,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Configurator{
|
2020-04-30 20:12:17 +00:00
|
|
|
logger: logger.Named(logging.TLSUtil),
|
|
|
|
manual: &manual{},
|
|
|
|
autoEncrypt: &autoEncrypt{},
|
|
|
|
peerDatacenterUseTLS: map[string]bool{},
|
2020-01-28 23:50:41 +00:00
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
err := c.Update(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// CAPems returns the currently loaded CAs in PEM format.
|
|
|
|
func (c *Configurator) CAPems() []string {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return append(c.manual.caPems, c.autoEncrypt.caPems()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ManualCAPems returns the currently loaded CAs in PEM format.
|
|
|
|
func (c *Configurator) ManualCAPems() []string {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.manual.caPems
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// Update updates the internal configuration which is used to generate
|
|
|
|
// *tls.Config.
|
|
|
|
// This function acquires a write lock because it writes the new config.
|
|
|
|
func (c *Configurator) Update(config Config) error {
|
2019-06-27 20:22:07 +00:00
|
|
|
c.Lock()
|
|
|
|
// order of defers matters because log acquires a RLock()
|
|
|
|
defer c.log("Update")
|
|
|
|
defer c.Unlock()
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
cert, err := loadKeyPair(config.CertFile, config.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-12-17 17:56:18 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
pems, err := loadCAs(config.CAFile, config.CAPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pool, err := pool(append(pems, c.autoEncrypt.caPems()...))
|
2019-03-13 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
if err = c.check(config, pool, cert); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.base = &config
|
|
|
|
c.manual.cert = cert
|
|
|
|
c.manual.caPems = pems
|
|
|
|
c.caPool = pool
|
|
|
|
c.version++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAutoEncryptCA updates the autoEncrypt.caPems. This is supposed to be called
|
|
|
|
// from the server in order to be able to accept TLS connections with TLS
|
|
|
|
// certificates.
|
|
|
|
// Or it is being called on the client side when CA changes are detected.
|
|
|
|
func (c *Configurator) UpdateAutoEncryptCA(connectCAPems []string) error {
|
|
|
|
c.Lock()
|
|
|
|
// order of defers matters because log acquires a RLock()
|
|
|
|
defer c.log("UpdateAutoEncryptCA")
|
|
|
|
defer c.Unlock()
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
pool, err := pool(append(c.manual.caPems, append(c.autoEncrypt.manualCAPems, connectCAPems...)...))
|
|
|
|
if err != nil {
|
|
|
|
c.RUnlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = c.check(*c.base, pool, c.manual.cert); err != nil {
|
|
|
|
c.RUnlock()
|
2019-03-13 09:29:06 +00:00
|
|
|
return err
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
c.autoEncrypt.connectCAPems = connectCAPems
|
|
|
|
c.caPool = pool
|
|
|
|
c.version++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAutoEncryptCert
|
|
|
|
func (c *Configurator) UpdateAutoEncryptCert(pub, priv string) error {
|
|
|
|
// order of defers matters because log acquires a RLock()
|
|
|
|
defer c.log("UpdateAutoEncryptCert")
|
|
|
|
cert, err := tls.X509KeyPair([]byte(pub), []byte(priv))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to load cert/key pair: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
c.Lock()
|
2019-06-27 20:22:07 +00:00
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
c.autoEncrypt.cert = &cert
|
|
|
|
c.version++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAutoEncrypt sets everything under autoEncrypt. This is being called on the
|
|
|
|
// client when it received its cert from AutoEncrypt endpoint.
|
|
|
|
func (c *Configurator) UpdateAutoEncrypt(manualCAPems, connectCAPems []string, pub, priv string, verifyServerHostname bool) error {
|
|
|
|
// order of defers matters because log acquires a RLock()
|
|
|
|
defer c.log("UpdateAutoEncrypt")
|
|
|
|
cert, err := tls.X509KeyPair([]byte(pub), []byte(priv))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to load cert/key pair: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
pool, err := pool(append(c.manual.caPems, append(manualCAPems, connectCAPems...)...))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.autoEncrypt.manualCAPems = manualCAPems
|
|
|
|
c.autoEncrypt.connectCAPems = connectCAPems
|
|
|
|
c.autoEncrypt.cert = &cert
|
|
|
|
c.caPool = pool
|
|
|
|
c.autoEncrypt.verifyServerHostname = verifyServerHostname
|
2019-03-13 09:29:06 +00:00
|
|
|
c.version++
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2020-04-30 20:12:17 +00:00
|
|
|
func (c *Configurator) UpdateAreaPeerDatacenterUseTLS(peerDatacenter string, useTLS bool) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
c.version++
|
|
|
|
c.peerDatacenterUseTLS[peerDatacenter] = useTLS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configurator) getAreaForPeerDatacenterUseTLS(peerDatacenter string) bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
if v, ok := c.peerDatacenterUseTLS[peerDatacenter]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
func (c *Configurator) Base() Config {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return *c.base
|
|
|
|
}
|
|
|
|
|
|
|
|
func pool(pems []string) (*x509.CertPool, error) {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
for _, pem := range pems {
|
|
|
|
if !pool.AppendCertsFromPEM([]byte(pem)) {
|
|
|
|
return nil, fmt.Errorf("Couldn't parse PEM %s", pem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(pool.Subjects()) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return pool, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configurator) check(config Config, pool *x509.CertPool, cert *tls.Certificate) error {
|
2019-03-13 09:29:06 +00:00
|
|
|
// Check if a minimum TLS version was set
|
|
|
|
if config.TLSMinVersion != "" {
|
|
|
|
if _, ok := TLSLookup[config.TLSMinVersion]; !ok {
|
2020-02-19 22:22:31 +00:00
|
|
|
return fmt.Errorf("TLSMinVersion: value %s not supported, please specify one of [%s]", config.TLSMinVersion, TLSVersions)
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// Ensure we have a CA if VerifyOutgoing is set
|
2019-06-27 20:22:07 +00:00
|
|
|
if config.VerifyOutgoing && pool == nil {
|
2019-03-13 09:29:06 +00:00
|
|
|
return fmt.Errorf("VerifyOutgoing set, and no CA certificate provided!")
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// Ensure we have a CA and cert if VerifyIncoming is set
|
2019-06-27 20:22:07 +00:00
|
|
|
if config.anyVerifyIncoming() {
|
2019-12-06 20:36:13 +00:00
|
|
|
autoEncryptMsg := " AutoEncrypt only secures the connection between client and server and doesn't affect incoming connections on the client."
|
2019-06-27 20:22:07 +00:00
|
|
|
if pool == nil {
|
2019-12-06 20:36:13 +00:00
|
|
|
errMsg := "VerifyIncoming set, and no CA certificate provided!"
|
|
|
|
if config.AutoEncryptTLS {
|
|
|
|
errMsg += autoEncryptMsg
|
|
|
|
}
|
|
|
|
return fmt.Errorf(errMsg)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2019-11-12 18:40:32 +00:00
|
|
|
if cert == nil {
|
2019-12-06 20:36:13 +00:00
|
|
|
errMsg := "VerifyIncoming set, and no Cert/Key pair provided!"
|
|
|
|
if config.AutoEncryptTLS {
|
|
|
|
errMsg += autoEncryptMsg
|
|
|
|
}
|
|
|
|
return fmt.Errorf(errMsg)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
func (c Config) anyVerifyIncoming() bool {
|
|
|
|
return c.baseVerifyIncoming() || c.VerifyIncomingRPC || c.VerifyIncomingHTTPS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) verifyIncomingRPC() bool {
|
|
|
|
return c.baseVerifyIncoming() || c.VerifyIncomingRPC
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Config) verifyIncomingHTTPS() bool {
|
|
|
|
return c.baseVerifyIncoming() || c.VerifyIncomingHTTPS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) baseVerifyIncoming() bool {
|
|
|
|
return c.VerifyIncoming
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func loadKeyPair(certFile, keyFile string) (*tls.Certificate, error) {
|
|
|
|
if certFile == "" || keyFile == "" {
|
2019-11-12 18:40:32 +00:00
|
|
|
return nil, nil
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to load cert/key pair: %v", err)
|
|
|
|
}
|
|
|
|
return &cert, nil
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
func loadCAs(caFile, caPath string) ([]string, error) {
|
|
|
|
if caFile == "" && caPath == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pems := []string{}
|
|
|
|
|
|
|
|
readFn := func(path string) error {
|
|
|
|
pem, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error loading from %s: %s", path, err)
|
|
|
|
}
|
|
|
|
pems = append(pems, string(pem))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
walkFn := func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !info.IsDir() {
|
|
|
|
if err := readFn(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
if caFile != "" {
|
2019-06-27 20:22:07 +00:00
|
|
|
err := readFn(caFile)
|
|
|
|
if err != nil {
|
|
|
|
return pems, err
|
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
} else if caPath != "" {
|
2019-06-27 20:22:07 +00:00
|
|
|
err := filepath.Walk(caPath, walkFn)
|
2019-03-13 09:29:06 +00:00
|
|
|
if err != nil {
|
2019-06-27 20:22:07 +00:00
|
|
|
return pems, err
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
if len(pems) == 0 {
|
|
|
|
return pems, fmt.Errorf("Error loading from CAPath: no CAs found")
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
return pems, nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 09:14:59 +00:00
|
|
|
// commonTLSConfig generates a *tls.Config from the base configuration the
|
|
|
|
// Configurator has. It accepts an additional flag in case a config is needed
|
|
|
|
// for incoming TLS connections.
|
2019-03-13 09:29:06 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
2019-06-27 20:22:07 +00:00
|
|
|
func (c *Configurator) commonTLSConfig(verifyIncoming bool) *tls.Config {
|
|
|
|
// this needs to be outside of RLock because it acquires an RLock itself
|
|
|
|
verifyServerHostname := c.VerifyServerHostname()
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2014-11-18 16:03:36 +00:00
|
|
|
tlsConfig := &tls.Config{
|
2019-06-27 20:22:07 +00:00
|
|
|
InsecureSkipVerify: !verifyServerHostname,
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 08:29:39 +00:00
|
|
|
// Set the cipher suites
|
2019-02-26 15:52:07 +00:00
|
|
|
if len(c.base.CipherSuites) != 0 {
|
|
|
|
tlsConfig.CipherSuites = c.base.CipherSuites
|
2017-04-27 08:29:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConfig.PreferServerCipherSuites = c.base.PreferServerCipherSuites
|
2019-02-26 15:52:07 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// GetCertificate is used when acting as a server and responding to
|
2019-11-12 18:40:32 +00:00
|
|
|
// client requests. Default to the manually configured cert, but allow
|
|
|
|
// autoEncrypt cert too so that a client can encrypt incoming
|
|
|
|
// connections without having a manual cert configured.
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConfig.GetCertificate = func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
|
2020-01-22 10:32:17 +00:00
|
|
|
return c.Cert(), nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
// GetClientCertificate is used when acting as a client and responding
|
|
|
|
// to a server requesting a certificate. Return the autoEncrypt certificate
|
|
|
|
// if possible, otherwise default to the manually provisioned one.
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
2019-06-27 20:22:07 +00:00
|
|
|
cert := c.autoEncrypt.cert
|
|
|
|
if cert == nil {
|
|
|
|
cert = c.manual.cert
|
|
|
|
}
|
|
|
|
|
2020-06-30 17:52:57 +00:00
|
|
|
if cert == nil {
|
|
|
|
// the return value MUST not be nil but an empty certificate will be
|
|
|
|
// treated the same as having no client certificate
|
|
|
|
cert = &tls.Certificate{}
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
return cert, nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
tlsConfig.ClientCAs = c.caPool
|
|
|
|
tlsConfig.RootCAs = c.caPool
|
2019-03-13 09:29:06 +00:00
|
|
|
|
|
|
|
// This is possible because TLSLookup also contains "" with golang's
|
|
|
|
// default (tls10). And because the initial check makes sure the
|
|
|
|
// version correctly matches.
|
|
|
|
tlsConfig.MinVersion = TLSLookup[c.base.TLSMinVersion]
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2019-02-27 09:14:59 +00:00
|
|
|
// Set ClientAuth if necessary
|
2019-06-27 20:22:07 +00:00
|
|
|
if verifyIncoming {
|
2019-02-27 09:14:59 +00:00
|
|
|
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
return tlsConfig
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:32:17 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) Cert() *tls.Certificate {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
cert := c.manual.cert
|
|
|
|
if cert == nil {
|
|
|
|
cert = c.autoEncrypt.cert
|
|
|
|
}
|
|
|
|
return cert
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) VerifyIncomingRPC() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.base.verifyIncomingRPC()
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) outgoingRPCTLSDisabled() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
// if AutoEncrypt enabled, always use TLS
|
|
|
|
if c.base.AutoEncryptTLS {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// if CAs are provided or VerifyOutgoing is set, use TLS
|
2020-04-30 20:12:17 +00:00
|
|
|
if c.base.VerifyOutgoing {
|
2019-06-27 20:22:07 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
func (c *Configurator) MutualTLSCapable() bool {
|
|
|
|
return c.mutualTLSCapable()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) mutualTLSCapable() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.caPool != nil && (c.autoEncrypt.cert != nil || c.manual.cert != nil)
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
2019-06-27 20:22:07 +00:00
|
|
|
func (c *Configurator) verifyOutgoing() bool {
|
2019-03-13 09:29:06 +00:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
// If AutoEncryptTLS is enabled and there is a CA, then verify
|
|
|
|
// outgoing.
|
|
|
|
if c.base.AutoEncryptTLS && c.caPool != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.base.VerifyOutgoing
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
func (c *Configurator) ServerSNI(dc, nodeName string) string {
|
|
|
|
// Strip the trailing '.' from the domain if any
|
|
|
|
domain := strings.TrimSuffix(c.domain(), ".")
|
|
|
|
|
|
|
|
if nodeName == "" || nodeName == "*" {
|
|
|
|
return "server." + dc + "." + domain
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeName + ".server." + dc + "." + domain
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) domain() string {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.base.Domain
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) verifyIncomingRPC() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
return c.base.verifyIncomingRPC()
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) verifyIncomingHTTPS() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
return c.base.verifyIncomingHTTPS()
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) enableAgentTLSForChecks() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.base.EnableAgentTLSForChecks
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) serverNameOrNodeName() string {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
if c.base.ServerName != "" {
|
|
|
|
return c.base.ServerName
|
|
|
|
}
|
|
|
|
return c.base.NodeName
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2017-02-01 20:52:04 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) VerifyServerHostname() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.base.VerifyServerHostname || c.autoEncrypt.verifyServerHostname
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:32:17 +00:00
|
|
|
// IncomingGRPCConfig generates a *tls.Config for incoming GRPC connections.
|
|
|
|
func (c *Configurator) IncomingGRPCConfig() *tls.Config {
|
|
|
|
c.log("IncomingGRPCConfig")
|
|
|
|
|
|
|
|
// false has the effect that this config doesn't require a client cert
|
|
|
|
// verification. This is because there is no verify_incoming_grpc
|
|
|
|
// configuration option. And using verify_incoming would be backwards
|
|
|
|
// incompatible, because even if it was set before, it didn't have an
|
|
|
|
// effect on the grpc server.
|
|
|
|
config := c.commonTLSConfig(false)
|
|
|
|
config.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) {
|
|
|
|
return c.IncomingGRPCConfig(), nil
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2019-02-27 09:14:59 +00:00
|
|
|
// IncomingRPCConfig generates a *tls.Config for incoming RPC connections.
|
2019-03-13 09:29:06 +00:00
|
|
|
func (c *Configurator) IncomingRPCConfig() *tls.Config {
|
|
|
|
c.log("IncomingRPCConfig")
|
|
|
|
config := c.commonTLSConfig(c.verifyIncomingRPC())
|
|
|
|
config.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) {
|
|
|
|
return c.IncomingRPCConfig(), nil
|
|
|
|
}
|
|
|
|
return config
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// IncomingALPNRPCConfig generates a *tls.Config for incoming RPC connections
|
|
|
|
// directly using TLS with ALPN instead of the older byte-prefixed protocol.
|
|
|
|
func (c *Configurator) IncomingALPNRPCConfig(alpnProtos []string) *tls.Config {
|
|
|
|
c.log("IncomingALPNRPCConfig")
|
|
|
|
// Since the ALPN-RPC variation is indirectly exposed to the internet via
|
|
|
|
// mesh gateways we force mTLS and full server name verification.
|
|
|
|
config := c.commonTLSConfig(true)
|
|
|
|
config.InsecureSkipVerify = false
|
|
|
|
|
|
|
|
config.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) {
|
|
|
|
return c.IncomingALPNRPCConfig(alpnProtos), nil
|
|
|
|
}
|
|
|
|
config.NextProtos = alpnProtos
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// IncomingInsecureRPCConfig means that it doesn't verify incoming even thought
|
|
|
|
// it might have been configured. This is only supposed to be used by the
|
|
|
|
// servers for the insecure RPC server. At the time of writing only the
|
|
|
|
// AutoEncrypt.Sign call is supported on that server. And it might be the only
|
|
|
|
// usecase ever.
|
|
|
|
func (c *Configurator) IncomingInsecureRPCConfig() *tls.Config {
|
|
|
|
c.log("IncomingInsecureRPCConfig")
|
|
|
|
config := c.commonTLSConfig(false)
|
|
|
|
config.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) {
|
|
|
|
return c.IncomingInsecureRPCConfig(), nil
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2019-02-27 09:14:59 +00:00
|
|
|
// IncomingHTTPSConfig generates a *tls.Config for incoming HTTPS connections.
|
2019-03-13 09:29:06 +00:00
|
|
|
func (c *Configurator) IncomingHTTPSConfig() *tls.Config {
|
|
|
|
c.log("IncomingHTTPSConfig")
|
|
|
|
config := c.commonTLSConfig(c.verifyIncomingHTTPS())
|
|
|
|
config.NextProtos = []string{"h2", "http/1.1"}
|
|
|
|
config.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) {
|
|
|
|
return c.IncomingHTTPSConfig(), nil
|
|
|
|
}
|
|
|
|
return config
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 09:14:59 +00:00
|
|
|
// IncomingTLSConfig generates a *tls.Config for outgoing TLS connections for
|
2019-03-06 17:13:28 +00:00
|
|
|
// checks. This function is separated because there is an extra flag to
|
2019-02-27 09:14:59 +00:00
|
|
|
// consider for checks. EnableAgentTLSForChecks and InsecureSkipVerify has to
|
|
|
|
// be checked for checks.
|
2019-03-13 09:29:06 +00:00
|
|
|
func (c *Configurator) OutgoingTLSConfigForCheck(skipVerify bool) *tls.Config {
|
|
|
|
c.log("OutgoingTLSConfigForCheck")
|
|
|
|
if !c.enableAgentTLSForChecks() {
|
2019-02-27 09:14:59 +00:00
|
|
|
return &tls.Config{
|
2019-03-13 09:29:06 +00:00
|
|
|
InsecureSkipVerify: skipVerify,
|
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
config := c.commonTLSConfig(false)
|
|
|
|
config.InsecureSkipVerify = skipVerify
|
|
|
|
config.ServerName = c.serverNameOrNodeName()
|
2019-03-05 20:35:43 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
return config
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OutgoingRPCConfig generates a *tls.Config for outgoing RPC connections. If
|
|
|
|
// there is a CA or VerifyOutgoing is set, a *tls.Config will be provided,
|
|
|
|
// otherwise we assume that no TLS should be used.
|
2019-03-13 09:29:06 +00:00
|
|
|
func (c *Configurator) OutgoingRPCConfig() *tls.Config {
|
|
|
|
c.log("OutgoingRPCConfig")
|
|
|
|
if c.outgoingRPCTLSDisabled() {
|
|
|
|
return nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
return c.commonTLSConfig(false)
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// OutgoingALPNRPCConfig generates a *tls.Config for outgoing RPC connections
|
|
|
|
// directly using TLS with ALPN instead of the older byte-prefixed protocol.
|
|
|
|
// If there is a CA or VerifyOutgoing is set, a *tls.Config will be provided,
|
|
|
|
// otherwise we assume that no TLS should be used which completely disables the
|
|
|
|
// ALPN variation.
|
|
|
|
func (c *Configurator) OutgoingALPNRPCConfig() *tls.Config {
|
|
|
|
c.log("OutgoingALPNRPCConfig")
|
|
|
|
if !c.mutualTLSCapable() {
|
|
|
|
return nil // ultimately this will hard-fail as TLS is required
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the ALPN-RPC variation is indirectly exposed to the internet via
|
|
|
|
// mesh gateways we force mTLS and full server name verification.
|
|
|
|
config := c.commonTLSConfig(true)
|
|
|
|
config.InsecureSkipVerify = false
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2019-02-27 09:14:59 +00:00
|
|
|
// OutgoingRPCWrapper wraps the result of OutgoingRPCConfig in a DCWrapper. It
|
|
|
|
// decides if verify server hostname should be used.
|
2019-03-13 09:29:06 +00:00
|
|
|
func (c *Configurator) OutgoingRPCWrapper() DCWrapper {
|
|
|
|
c.log("OutgoingRPCWrapper")
|
2019-02-26 15:52:07 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// Generate the wrapper based on dc
|
|
|
|
return func(dc string, conn net.Conn) (net.Conn, error) {
|
2020-04-30 20:12:17 +00:00
|
|
|
if c.UseTLS(dc) {
|
|
|
|
return c.wrapTLSClient(dc, conn)
|
|
|
|
}
|
|
|
|
return conn, nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2019-02-26 15:52:07 +00:00
|
|
|
|
2020-04-30 20:12:17 +00:00
|
|
|
func (c *Configurator) UseTLS(dc string) bool {
|
|
|
|
return !c.outgoingRPCTLSDisabled() && c.getAreaForPeerDatacenterUseTLS(dc)
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// OutgoingALPNRPCWrapper wraps the result of OutgoingALPNRPCConfig in an
|
|
|
|
// ALPNWrapper. It configures all of the negotiation plumbing.
|
|
|
|
func (c *Configurator) OutgoingALPNRPCWrapper() ALPNWrapper {
|
|
|
|
c.log("OutgoingALPNRPCWrapper")
|
|
|
|
if !c.mutualTLSCapable() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(dc, nodeName, alpnProto string, conn net.Conn) (net.Conn, error) {
|
|
|
|
return c.wrapALPNTLSClient(dc, nodeName, alpnProto, conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// AutoEncryptCertNotAfter returns NotAfter from the auto_encrypt cert. In case
|
|
|
|
// there is no cert, it will return a time in the past.
|
|
|
|
func (c *Configurator) AutoEncryptCertNotAfter() time.Time {
|
|
|
|
c.RLock()
|
2019-11-12 18:40:32 +00:00
|
|
|
defer c.RUnlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
tlsCert := c.autoEncrypt.cert
|
2019-11-12 18:40:32 +00:00
|
|
|
if tlsCert == nil || tlsCert.Certificate == nil {
|
2019-06-27 20:22:07 +00:00
|
|
|
return time.Now().AddDate(0, 0, -1)
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
|
|
|
|
if err != nil {
|
|
|
|
return time.Now().AddDate(0, 0, -1)
|
|
|
|
}
|
|
|
|
return cert.NotAfter
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutoEncryptCertExpired returns if the auto_encrypt cert is expired.
|
|
|
|
func (c *Configurator) AutoEncryptCertExpired() bool {
|
|
|
|
return c.AutoEncryptCertNotAfter().Before(time.Now())
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) log(name string) {
|
|
|
|
if c.logger != nil {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2020-03-09 20:59:02 +00:00
|
|
|
c.logger.Trace(name, "version", c.version)
|
2017-02-01 20:52:04 +00:00
|
|
|
}
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
2017-04-27 08:29:39 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// Wrap a net.Conn into a client tls connection, performing any
|
|
|
|
// additional verification as needed.
|
|
|
|
//
|
|
|
|
// As of go 1.3, crypto/tls only supports either doing no certificate
|
|
|
|
// verification, or doing full verification including of the peer's
|
|
|
|
// DNS name. For consul, we want to validate that the certificate is
|
|
|
|
// signed by a known CA, but because consul doesn't use DNS names for
|
|
|
|
// node names, we don't verify the certificate DNS names. Since go 1.3
|
|
|
|
// no longer supports this mode of operation, we have to do it
|
|
|
|
// manually.
|
|
|
|
func (c *Configurator) wrapTLSClient(dc string, conn net.Conn) (net.Conn, error) {
|
|
|
|
config := c.OutgoingRPCConfig()
|
2019-06-27 20:22:07 +00:00
|
|
|
verifyServerHostname := c.VerifyServerHostname()
|
|
|
|
verifyOutgoing := c.verifyOutgoing()
|
|
|
|
domain := c.domain()
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
if verifyServerHostname {
|
|
|
|
// Strip the trailing '.' from the domain if any
|
|
|
|
domain = strings.TrimSuffix(domain, ".")
|
|
|
|
config.ServerName = "server." + dc + "." + domain
|
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
tlsConn := tls.Client(conn, config)
|
2019-03-13 09:29:06 +00:00
|
|
|
|
|
|
|
// If crypto/tls is doing verification, there's no need to do
|
|
|
|
// our own.
|
|
|
|
if !config.InsecureSkipVerify {
|
|
|
|
return tlsConn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If verification is not turned on, don't do it.
|
|
|
|
if !verifyOutgoing {
|
|
|
|
return tlsConn, nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
err := tlsConn.Handshake()
|
|
|
|
if err != nil {
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following is lightly-modified from the doFullHandshake
|
|
|
|
// method in crypto/tls's handshake_client.go.
|
|
|
|
opts := x509.VerifyOptions{
|
|
|
|
Roots: config.RootCAs,
|
|
|
|
CurrentTime: time.Now(),
|
|
|
|
DNSName: "",
|
|
|
|
Intermediates: x509.NewCertPool(),
|
|
|
|
}
|
|
|
|
|
|
|
|
certs := tlsConn.ConnectionState().PeerCertificates
|
|
|
|
for i, cert := range certs {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
opts.Intermediates.AddCert(cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = certs[0].Verify(opts)
|
|
|
|
if err != nil {
|
|
|
|
tlsConn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsConn, err
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// Wrap a net.Conn into a client tls connection suitable for secure ALPN-RPC,
|
|
|
|
// performing any additional verification as needed.
|
|
|
|
func (c *Configurator) wrapALPNTLSClient(dc, nodeName, alpnProto string, conn net.Conn) (net.Conn, error) {
|
|
|
|
if dc == "" {
|
|
|
|
return nil, fmt.Errorf("cannot dial using ALPN-RPC without a target datacenter")
|
|
|
|
} else if nodeName == "" {
|
|
|
|
return nil, fmt.Errorf("cannot dial using ALPN-RPC without a target node")
|
|
|
|
} else if alpnProto == "" {
|
|
|
|
return nil, fmt.Errorf("cannot dial using ALPN-RPC without a target alpn protocol")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := c.OutgoingALPNRPCConfig()
|
|
|
|
if config == nil {
|
|
|
|
return nil, fmt.Errorf("cannot dial via a mesh gateway when outgoing TLS is disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the ALPN-RPC variation is indirectly exposed to the internet via
|
|
|
|
// mesh gateways we force mTLS and full hostname validation (forcing
|
|
|
|
// verify_server_hostname and verify_outgoing to be effectively true).
|
|
|
|
|
|
|
|
config.ServerName = c.ServerSNI(dc, nodeName)
|
|
|
|
config.NextProtos = []string{alpnProto}
|
|
|
|
|
|
|
|
tlsConn := tls.Client(conn, config)
|
|
|
|
|
|
|
|
// NOTE: For this handshake to succeed the server must have key material
|
|
|
|
// for either "<nodename>.server.<datacenter>.<domain>" or
|
|
|
|
// "*.server.<datacenter>.<domain>" in addition to the
|
|
|
|
// "server.<datacenter>.<domain>" required for standard TLS'd RPC.
|
|
|
|
if err := tlsConn.Handshake(); err != nil {
|
|
|
|
tlsConn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cs := tlsConn.ConnectionState(); !cs.NegotiatedProtocolIsMutual {
|
|
|
|
tlsConn.Close()
|
|
|
|
return nil, fmt.Errorf("could not negotiate ALPN protocol %q with %q", alpnProto, config.ServerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsConn, nil
|
|
|
|
}
|
|
|
|
|
2019-02-27 09:14:59 +00:00
|
|
|
// ParseCiphers parse ciphersuites from the comma-separated string into
|
|
|
|
// recognized slice
|
2017-04-27 08:29:39 +00:00
|
|
|
func ParseCiphers(cipherStr string) ([]uint16, error) {
|
|
|
|
suites := []uint16{}
|
|
|
|
|
|
|
|
cipherStr = strings.TrimSpace(cipherStr)
|
|
|
|
if cipherStr == "" {
|
|
|
|
return []uint16{}, nil
|
|
|
|
}
|
|
|
|
ciphers := strings.Split(cipherStr, ",")
|
|
|
|
|
2020-06-05 19:56:19 +00:00
|
|
|
// Note: this needs to be kept up to date with the cipherMap in CipherString
|
2017-04-27 08:29:39 +00:00
|
|
|
cipherMap := map[string]uint16{
|
2020-03-10 20:44:26 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA": tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256": tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
|
2017-04-27 08:29:39 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256": tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
2020-03-10 20:44:26 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA": tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
2017-04-27 08:29:39 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384": tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
2018-03-13 16:50:41 +00:00
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
2020-03-10 20:44:26 +00:00
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256": tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256": tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
2018-03-13 16:50:41 +00:00
|
|
|
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
2020-03-10 20:44:26 +00:00
|
|
|
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384": tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
2017-04-27 08:29:39 +00:00
|
|
|
}
|
|
|
|
for _, cipher := range ciphers {
|
|
|
|
if v, ok := cipherMap[cipher]; ok {
|
|
|
|
suites = append(suites, v)
|
|
|
|
} else {
|
|
|
|
return suites, fmt.Errorf("unsupported cipher %q", cipher)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return suites, nil
|
|
|
|
}
|
2020-06-05 19:56:19 +00:00
|
|
|
|
|
|
|
// CipherString performs the inverse operation of ParseCiphers
|
|
|
|
func CipherString(ciphers []uint16) (string, error) {
|
|
|
|
// Note: this needs to be kept up to date with the cipherMap in ParseCiphers
|
|
|
|
cipherMap := map[uint16]string{
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
|
|
|
|
}
|
|
|
|
|
|
|
|
cipherStrings := make([]string, len(ciphers))
|
|
|
|
for i, cipher := range ciphers {
|
|
|
|
if v, ok := cipherMap[cipher]; ok {
|
|
|
|
cipherStrings[i] = v
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("unsupported cipher %d", cipher)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(cipherStrings, ","), nil
|
|
|
|
}
|