2014-11-18 16:03:36 +00:00
|
|
|
package tlsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2019-03-13 09:29:06 +00:00
|
|
|
"log"
|
2014-11-18 16:03:36 +00:00
|
|
|
"net"
|
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"
|
2017-04-27 08:29:39 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-rootcerts"
|
2014-11-18 16:03:36 +00:00
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
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
|
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-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
|
|
|
|
base *Config
|
|
|
|
cert *tls.Certificate
|
|
|
|
cas *x509.CertPool
|
|
|
|
logger *log.Logger
|
|
|
|
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.
|
|
|
|
func NewConfigurator(config Config, logger *log.Logger) (*Configurator, error) {
|
|
|
|
c := &Configurator{logger: logger}
|
|
|
|
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-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 {
|
|
|
|
cert, err := loadKeyPair(config.CertFile, config.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-12-17 17:56:18 +00:00
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
cas, err := loadCAs(config.CAFile, config.CAPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
if err = c.check(config, cas, cert); err != nil {
|
|
|
|
return err
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
c.Lock()
|
|
|
|
c.base = &config
|
|
|
|
c.cert = cert
|
|
|
|
c.cas = cas
|
|
|
|
c.version++
|
|
|
|
c.Unlock()
|
|
|
|
c.log("Update")
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func (c *Configurator) check(config Config, cas *x509.CertPool, cert *tls.Certificate) error {
|
|
|
|
// Check if a minimum TLS version was set
|
|
|
|
if config.TLSMinVersion != "" {
|
|
|
|
if _, ok := TLSLookup[config.TLSMinVersion]; !ok {
|
|
|
|
return fmt.Errorf("TLSMinVersion: value %s not supported, please specify one of [tls10,tls11,tls12]", config.TLSMinVersion)
|
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
|
|
|
|
if config.VerifyOutgoing && cas == nil {
|
|
|
|
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
|
|
|
|
if config.VerifyIncoming || config.VerifyIncomingRPC || config.VerifyIncomingHTTPS {
|
|
|
|
if cas == nil {
|
|
|
|
return fmt.Errorf("VerifyIncoming set, and no CA certificate provided!")
|
|
|
|
}
|
|
|
|
if cert == nil {
|
|
|
|
return fmt.Errorf("VerifyIncoming set, and no Cert/Key pair provided!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func loadKeyPair(certFile, keyFile string) (*tls.Certificate, error) {
|
|
|
|
if certFile == "" || keyFile == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
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-03-13 09:29:06 +00:00
|
|
|
func loadCAs(caFile, caPath string) (*x509.CertPool, error) {
|
|
|
|
if caFile != "" {
|
|
|
|
return rootcerts.LoadCAFile(caFile)
|
|
|
|
} else if caPath != "" {
|
|
|
|
pool, err := rootcerts.LoadCAPath(caPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// make sure to not return an empty pool because this is not
|
|
|
|
// the users intention when providing a path for CAs.
|
|
|
|
if len(pool.Subjects()) == 0 {
|
|
|
|
return nil, fmt.Errorf("Error loading CA: path %q has no CAs", caPath)
|
|
|
|
}
|
|
|
|
return pool, nil
|
|
|
|
}
|
|
|
|
return nil, 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.
|
|
|
|
func (c *Configurator) commonTLSConfig(additionalVerifyIncomingFlag bool) *tls.Config {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
2014-11-18 16:03:36 +00:00
|
|
|
tlsConfig := &tls.Config{
|
2019-03-05 20:35:43 +00:00
|
|
|
InsecureSkipVerify: !c.base.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-03-13 09:29:06 +00:00
|
|
|
tlsConfig.GetCertificate = func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
return c.cert, nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
return c.cert, nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConfig.ClientCAs = c.cas
|
|
|
|
tlsConfig.RootCAs = c.cas
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if c.base.VerifyIncoming || additionalVerifyIncomingFlag {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) outgoingRPCTLSDisabled() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.cas == nil && !c.base.VerifyOutgoing
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) someValuesFromConfig() (bool, bool, string) {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.base.VerifyServerHostname, c.base.VerifyOutgoing, c.base.Domain
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
|
|
|
func (c *Configurator) verifyIncomingHTTPS() bool {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.base.VerifyIncomingHTTPS
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
if c.outgoingRPCTLSDisabled() {
|
|
|
|
return nil
|
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) {
|
|
|
|
return c.wrapTLSClient(dc, conn)
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2019-02-26 15:52:07 +00:00
|
|
|
|
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()
|
|
|
|
c.logger.Printf("[DEBUG] tlsutil: %s with version %d", name, 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) {
|
|
|
|
var err error
|
|
|
|
var tlsConn *tls.Conn
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
config := c.OutgoingRPCConfig()
|
|
|
|
verifyServerHostname, verifyOutgoing, domain := c.someValuesFromConfig()
|
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
|
|
|
|
}
|
|
|
|
tlsConn = tls.Client(conn, config)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tlsConn.Handshake(); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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, ",")
|
|
|
|
|
|
|
|
cipherMap := map[string]uint16{
|
2018-03-13 16:50:41 +00:00
|
|
|
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305": tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305": tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
2017-04-27 08:29:39 +00:00
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256": tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256": tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384": tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
"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_SHA256": tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256": tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA": tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA": tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
|
|
|
"TLS_RSA_WITH_AES_128_GCM_SHA256": tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
"TLS_RSA_WITH_AES_256_GCM_SHA384": tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
"TLS_RSA_WITH_AES_128_CBC_SHA256": tls.TLS_RSA_WITH_AES_128_CBC_SHA256,
|
|
|
|
"TLS_RSA_WITH_AES_128_CBC_SHA": tls.TLS_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
"TLS_RSA_WITH_AES_256_CBC_SHA": tls.TLS_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
"TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
|
|
|
|
"TLS_RSA_WITH_3DES_EDE_CBC_SHA": tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
|
|
|
|
"TLS_RSA_WITH_RC4_128_SHA": tls.TLS_RSA_WITH_RC4_128_SHA,
|
|
|
|
"TLS_ECDHE_RSA_WITH_RC4_128_SHA": tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
|
2018-03-13 17:30:18 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_RC4_128_SHA": tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
|
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
|
|
|
|
}
|