2014-11-18 16:03:36 +00:00
|
|
|
package tlsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-06-27 20:22:07 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-05-11 23:05:39 +00:00
|
|
|
"strings"
|
2019-02-27 09:14:59 +00:00
|
|
|
"sync"
|
2021-06-17 23:35:58 +00:00
|
|
|
"sync/atomic"
|
2014-11-18 16:03:36 +00:00
|
|
|
"time"
|
2020-02-19 22:22:31 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2021-09-09 19:48:54 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2020-10-14 20:12:47 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/logging"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbconfig"
|
2022-03-24 19:32:25 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
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
|
|
|
|
2022-03-24 19:32:25 +00:00
|
|
|
// goTLSVersions maps types.TLSVersion to the Go internal value
|
|
|
|
var goTLSVersions = map[types.TLSVersion]uint16{
|
|
|
|
types.TLSVersionAuto: tls.VersionTLS12,
|
|
|
|
types.TLSv1_0: tls.VersionTLS10,
|
|
|
|
types.TLSv1_1: tls.VersionTLS11,
|
|
|
|
types.TLSv1_2: tls.VersionTLS12,
|
|
|
|
types.TLSv1_3: tls.VersionTLS13,
|
2017-02-01 20:52:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// ProtocolConfig contains configuration for a given protocol.
|
|
|
|
type ProtocolConfig 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
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// CAFile is a path to a certificate authority file. This is used with
|
|
|
|
// VerifyIncoming or VerifyOutgoing to verify the TLS connection.
|
|
|
|
CAFile string
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2022-03-18 10:46:58 +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.
|
|
|
|
CAPath string
|
|
|
|
|
|
|
|
// CertFile is used to provide a TLS certificate that is used for
|
|
|
|
// serving TLS connections. Must be provided to serve TLS connections.
|
|
|
|
CertFile string
|
|
|
|
|
|
|
|
// KeyFile is used to provide a TLS key that is used for serving TLS
|
|
|
|
// connections. Must be provided to serve TLS connections.
|
|
|
|
KeyFile string
|
|
|
|
|
|
|
|
// TLSMinVersion is the minimum accepted TLS version that can be used.
|
2022-03-24 19:32:25 +00:00
|
|
|
|
|
|
|
TLSMinVersion types.TLSVersion
|
2022-03-18 10:46:58 +00:00
|
|
|
|
|
|
|
// CipherSuites is the list of TLS cipher suites to use.
|
|
|
|
//
|
2022-03-24 19:32:25 +00:00
|
|
|
// We don't support the raw 0xNNNN values from
|
|
|
|
// https://golang.org/pkg/crypto/tls/#pkg-constants
|
|
|
|
// even though they are standardized by IANA because it would increase
|
|
|
|
// the likelihood of an operator inadvertently setting an insecure configuration
|
|
|
|
CipherSuites []types.TLSCipherSuite
|
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.
|
2022-03-18 10:46:58 +00:00
|
|
|
//
|
2022-07-15 18:15:50 +00:00
|
|
|
// Note: this setting doesn't apply to the external gRPC configuration, as Consul
|
2022-03-18 10:46:58 +00:00
|
|
|
// makes no outgoing connections using this protocol.
|
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.
|
2022-03-18 10:46:58 +00:00
|
|
|
//
|
|
|
|
// Note: this setting only applies to the Internal RPC configuration.
|
2015-05-11 22:14:56 +00:00
|
|
|
VerifyServerHostname bool
|
2022-08-24 16:31:38 +00:00
|
|
|
|
|
|
|
// UseAutoCert is used to enable usage of auto_encrypt/auto_config generated
|
|
|
|
// certificate & key material on external gRPC listener.
|
|
|
|
UseAutoCert bool
|
2022-03-18 10:46:58 +00:00
|
|
|
}
|
2015-05-11 22:14:56 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// Config configures the Configurator.
|
|
|
|
type Config struct {
|
2022-09-16 01:12:10 +00:00
|
|
|
// ServerMode indicates whether the configurator is attached to a server
|
|
|
|
// or client agent.
|
|
|
|
ServerMode bool
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// InternalRPC is used to configure the internal multiplexed RPC protocol.
|
|
|
|
InternalRPC ProtocolConfig
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// GRPC is used to configure the external (e.g. xDS) gRPC protocol.
|
|
|
|
GRPC ProtocolConfig
|
2017-04-27 08:29:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// HTTPS is used to configure the external HTTPS protocol.
|
|
|
|
HTTPS ProtocolConfig
|
2014-11-18 16:03:36 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
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
|
|
|
|
2020-07-30 14:15:12 +00:00
|
|
|
// AutoTLS opts the agent into provisioning agent
|
2019-06-27 20:22:07 +00:00
|
|
|
// TLS certificates.
|
2020-07-30 14:15:12 +00:00
|
|
|
AutoTLS bool
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// protocolConfig contains the loaded state (e.g. x509 certificates) for a given
|
|
|
|
// ProtocolConfig.
|
|
|
|
type protocolConfig struct {
|
|
|
|
// cert is the TLS certificate configured manually by the cert_file/key_file
|
|
|
|
// options in the configuration file.
|
|
|
|
cert *tls.Certificate
|
|
|
|
|
|
|
|
// manualCAPEMs contains the PEM-encoded CA certificates provided manually by
|
|
|
|
// the ca_file/ca_path options in the configuration file.
|
|
|
|
manualCAPEMs []string
|
|
|
|
|
|
|
|
// manualCAPool is a pool containing only manualCAPEM, for cases where it is
|
|
|
|
// not appropriate to trust the Connect CA (e.g. when verifying server identity
|
|
|
|
// in AuthorizeServerConn).
|
|
|
|
manualCAPool *x509.CertPool
|
|
|
|
|
|
|
|
// combinedCAPool is a pool containing both manualCAPEMs and the certificates
|
|
|
|
// received from auto-config/auto-encrypt.
|
|
|
|
combinedCAPool *x509.CertPool
|
2022-08-24 16:31:38 +00:00
|
|
|
|
|
|
|
// useAutoCert indicates wether we should use auto-encrypt/config data
|
|
|
|
// for TLS server/listener. NOTE: Only applies to external GRPC Server.
|
|
|
|
useAutoCert bool
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:29:07 +00:00
|
|
|
// Configurator provides tls.Config and net.Dial wrappers to enable TLS for
|
2022-03-18 10:46:58 +00:00
|
|
|
// clients and servers, for internal RPC, and external gRPC and HTTPS connections.
|
|
|
|
//
|
2021-06-21 16:29:07 +00:00
|
|
|
// Configurator receives an initial TLS configuration from agent configuration,
|
|
|
|
// and receives updates from config reloads, auto-encrypt, and auto-config.
|
2019-03-13 09:29:06 +00:00
|
|
|
type Configurator struct {
|
2021-06-28 22:19:44 +00:00
|
|
|
// version is increased each time the Configurator is updated. Must be accessed
|
|
|
|
// using sync/atomic. Also MUST be the first field in this struct to ensure
|
|
|
|
// 64-bit alignment. See https://golang.org/pkg/sync/atomic/#pkg-note-BUG.
|
|
|
|
version uint64
|
|
|
|
|
2021-06-17 23:35:58 +00:00
|
|
|
// lock synchronizes access to all fields on this struct except for logger and version.
|
2022-03-18 10:46:58 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
base *Config
|
2021-06-21 16:29:07 +00:00
|
|
|
// peerDatacenterUseTLS is a map of DC name to a bool indicating if the DC
|
|
|
|
// uses TLS for RPC requests.
|
2020-04-30 20:12:17 +00:00
|
|
|
peerDatacenterUseTLS map[string]bool
|
2019-06-27 20:22:07 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
grpc protocolConfig
|
|
|
|
https protocolConfig
|
|
|
|
internalRPC protocolConfig
|
|
|
|
|
2022-09-09 19:01:47 +00:00
|
|
|
// autoTLS stores configuration that is received from:
|
|
|
|
// - The auto-encrypt or auto-config features for client agents
|
|
|
|
// - The servercert.CertManager for server agents.
|
2022-03-18 10:46:58 +00:00
|
|
|
autoTLS struct {
|
|
|
|
extraCAPems []string
|
|
|
|
connectCAPems []string
|
|
|
|
cert *tls.Certificate
|
|
|
|
verifyServerHostname bool
|
2022-09-09 19:01:47 +00:00
|
|
|
peeringServerName string
|
2022-03-18 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 23:35:58 +00:00
|
|
|
// logger is not protected by a lock. It must never be changed after
|
|
|
|
// Configurator is created.
|
|
|
|
logger hclog.Logger
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
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),
|
|
|
|
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
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// ManualCAPems returns the currently loaded CAs for the internal RPC protocol
|
|
|
|
// in PEM format. It is used in the auto-config/auto-encrypt endpoints.
|
2019-06-27 20:22:07 +00:00
|
|
|
func (c *Configurator) ManualCAPems() []string {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2022-03-18 10:46:58 +00:00
|
|
|
return c.internalRPC.manualCAPEMs
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 18:15:50 +00:00
|
|
|
// GRPCManualCAPems returns the currently loaded CAs for the gRPC in PEM format.
|
|
|
|
func (c *Configurator) GRPCManualCAPems() []string {
|
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
|
|
|
return c.grpc.manualCAPEMs
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
grpc, err := c.loadProtocolConfig(config, config.GRPC)
|
2019-06-27 20:22:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
|
|
|
|
https, err := c.loadProtocolConfig(config, config.HTTPS)
|
2019-03-13 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
|
|
|
|
internalRPC, err := c.loadProtocolConfig(config, config.InternalRPC)
|
2021-08-26 22:04:32 +00:00
|
|
|
if err != nil {
|
2019-06-27 20:22:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-26 22:04:32 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
c.base = &config
|
2022-03-18 10:46:58 +00:00
|
|
|
c.grpc = *grpc
|
|
|
|
c.https = *https
|
|
|
|
c.internalRPC = *internalRPC
|
|
|
|
|
2021-06-17 23:35:58 +00:00
|
|
|
atomic.AddUint64(&c.version, 1)
|
|
|
|
c.log("Update")
|
2019-06-27 20:22:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// loadProtocolConfig loads the certificates etc. for a given ProtocolConfig
|
|
|
|
// and performs validation.
|
2022-03-24 19:32:25 +00:00
|
|
|
func (c *Configurator) loadProtocolConfig(base Config, pc ProtocolConfig) (*protocolConfig, error) {
|
|
|
|
cert, err := loadKeyPair(pc.CertFile, pc.KeyFile)
|
2022-03-18 10:46:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-24 19:32:25 +00:00
|
|
|
pems, err := LoadCAs(pc.CAFile, pc.CAPath)
|
2022-03-18 10:46:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
manualPool, err := newX509CertPool(pems)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
combinedPool, err := newX509CertPool(pems, c.autoTLS.connectCAPems, c.autoTLS.extraCAPems)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 19:32:25 +00:00
|
|
|
if pc.VerifyIncoming {
|
2022-03-18 10:46:58 +00:00
|
|
|
// Both auto-config and auto-encrypt require verifying the connection from the
|
|
|
|
// client to the server for secure operation. In order to be able to verify the
|
|
|
|
// server's certificate we must have some CA certs already provided. Therefore,
|
|
|
|
// even though both of those features can push down extra CA certificates which
|
|
|
|
// could be used to verify incoming connections, we still must consider it an
|
|
|
|
// error if none are provided in the initial configuration as those features
|
|
|
|
// cannot be successfully enabled without providing CA certificates to use those
|
|
|
|
// features.
|
|
|
|
if combinedPool == nil {
|
|
|
|
return nil, fmt.Errorf("VerifyIncoming set but no CA certificates were provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We will use the auto_encrypt/auto_config cert for TLS in the incoming APIs
|
|
|
|
// when available. Therefore the check here will ensure that either we enabled
|
|
|
|
// one of those two features or a certificate and key were provided manually
|
|
|
|
if cert == nil && !base.AutoTLS {
|
|
|
|
return nil, fmt.Errorf("VerifyIncoming requires either a Cert and Key pair in the configuration file, or auto_encrypt/auto_config be enabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a CA if VerifyOutgoing is set.
|
2022-03-24 19:32:25 +00:00
|
|
|
if pc.VerifyOutgoing && combinedPool == nil {
|
2022-03-18 10:46:58 +00:00
|
|
|
return nil, fmt.Errorf("VerifyOutgoing set but no CA certificates were provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &protocolConfig{
|
|
|
|
cert: cert,
|
|
|
|
manualCAPEMs: pems,
|
|
|
|
manualCAPool: manualPool,
|
|
|
|
combinedCAPool: combinedPool,
|
2022-08-24 16:31:38 +00:00
|
|
|
useAutoCert: pc.UseAutoCert,
|
2022-03-18 10:46:58 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-30 14:15:12 +00:00
|
|
|
// UpdateAutoTLSCA updates the autoEncrypt.caPems. This is supposed to be called
|
2019-06-27 20:22:07 +00:00
|
|
|
// 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.
|
2020-07-30 14:15:12 +00:00
|
|
|
func (c *Configurator) UpdateAutoTLSCA(connectCAPems []string) error {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
makePool := func(l protocolConfig) (*x509.CertPool, error) {
|
|
|
|
return newX509CertPool(l.manualCAPEMs, c.autoTLS.extraCAPems, connectCAPems)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make all of the pools up-front (before assigning anything) so that if any of
|
|
|
|
// them fails, we aren't left in a half-applied state.
|
|
|
|
internalRPCPool, err := makePool(c.internalRPC)
|
2019-06-27 20:22:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
grpcPool, err := makePool(c.grpc)
|
|
|
|
if err != nil {
|
2019-03-13 09:29:06 +00:00
|
|
|
return err
|
2014-11-18 16:03:36 +00:00
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
httpsPool, err := makePool(c.https)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-30 14:15:12 +00:00
|
|
|
c.autoTLS.connectCAPems = connectCAPems
|
2022-03-18 10:46:58 +00:00
|
|
|
c.internalRPC.combinedCAPool = internalRPCPool
|
|
|
|
c.grpc.combinedCAPool = grpcPool
|
|
|
|
c.https.combinedCAPool = httpsPool
|
|
|
|
|
2021-06-17 23:35:58 +00:00
|
|
|
atomic.AddUint64(&c.version, 1)
|
|
|
|
c.log("UpdateAutoTLSCA")
|
2019-06-27 20:22:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 19:01:47 +00:00
|
|
|
// UpdateAutoTLSCert receives the updated automatically-provisioned certificate.
|
2020-07-30 14:15:12 +00:00
|
|
|
func (c *Configurator) UpdateAutoTLSCert(pub, priv string) error {
|
2019-06-27 20:22:07 +00:00
|
|
|
cert, err := tls.X509KeyPair([]byte(pub), []byte(priv))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to load cert/key pair: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
|
2020-07-30 14:15:12 +00:00
|
|
|
c.autoTLS.cert = &cert
|
2021-06-17 23:35:58 +00:00
|
|
|
atomic.AddUint64(&c.version, 1)
|
|
|
|
c.log("UpdateAutoTLSCert")
|
2019-06-27 20:22:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 19:01:47 +00:00
|
|
|
// UpdateAutoTLSPeeringServerName receives the updated automatically-provisioned certificate.
|
|
|
|
func (c *Configurator) UpdateAutoTLSPeeringServerName(name string) {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
c.autoTLS.peeringServerName = name
|
|
|
|
atomic.AddUint64(&c.version, 1)
|
|
|
|
c.log("UpdateAutoTLSPeeringServerName")
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:20:39 +00:00
|
|
|
// UpdateAutoTLS receives updates from Auto-Config, only expected to be called on
|
|
|
|
// client agents.
|
2020-07-30 14:15:12 +00:00
|
|
|
func (c *Configurator) UpdateAutoTLS(manualCAPems, connectCAPems []string, pub, priv string, verifyServerHostname bool) error {
|
2019-06-27 20:22:07 +00:00
|
|
|
cert, err := tls.X509KeyPair([]byte(pub), []byte(priv))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to load cert/key pair: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
makePool := func(l protocolConfig) (*x509.CertPool, error) {
|
|
|
|
return newX509CertPool(l.manualCAPEMs, manualCAPems, connectCAPems)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make all of the pools up-front (before assigning anything) so that if any of
|
|
|
|
// them fails, we aren't left in a half-applied state.
|
|
|
|
internalRPCPool, err := makePool(c.internalRPC)
|
2019-06-27 20:22:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
grpcPool, err := makePool(c.grpc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
httpsPool, err := makePool(c.https)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-26 22:04:32 +00:00
|
|
|
c.autoTLS.extraCAPems = manualCAPems
|
2020-07-30 14:15:12 +00:00
|
|
|
c.autoTLS.connectCAPems = connectCAPems
|
|
|
|
c.autoTLS.cert = &cert
|
|
|
|
c.autoTLS.verifyServerHostname = verifyServerHostname
|
2022-03-18 10:46:58 +00:00
|
|
|
c.internalRPC.combinedCAPool = internalRPCPool
|
|
|
|
c.grpc.combinedCAPool = grpcPool
|
|
|
|
c.https.combinedCAPool = httpsPool
|
|
|
|
|
2021-06-17 23:35:58 +00:00
|
|
|
atomic.AddUint64(&c.version, 1)
|
|
|
|
c.log("UpdateAutoTLS")
|
2019-03-13 09:29:06 +00:00
|
|
|
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) {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2021-06-17 23:35:58 +00:00
|
|
|
atomic.AddUint64(&c.version, 1)
|
|
|
|
c.log("UpdateAreaPeerDatacenterUseTLS")
|
2020-04-30 20:12:17 +00:00
|
|
|
c.peerDatacenterUseTLS[peerDatacenter] = useTLS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configurator) getAreaForPeerDatacenterUseTLS(peerDatacenter string) bool {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2020-04-30 20:12:17 +00:00
|
|
|
if v, ok := c.peerDatacenterUseTLS[peerDatacenter]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
func (c *Configurator) Base() Config {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
return *c.base
|
|
|
|
}
|
|
|
|
|
2021-08-26 22:04:32 +00:00
|
|
|
// newX509CertPool loads all the groups of PEM encoded certificates into a
|
|
|
|
// single x509.CertPool.
|
|
|
|
//
|
|
|
|
// The groups argument is a varargs of slices so that callers do not need to
|
|
|
|
// append slices together. In some cases append can modify the backing array
|
|
|
|
// of the first slice passed to append, which will often result in hard to
|
|
|
|
// find bugs. By accepting a varargs of slices we remove the need for the
|
|
|
|
// caller to append the groups, which should prevent any such bugs.
|
|
|
|
func newX509CertPool(groups ...[]string) (*x509.CertPool, error) {
|
2022-04-14 20:55:10 +00:00
|
|
|
var haveCerts bool
|
2019-06-27 20:22:07 +00:00
|
|
|
pool := x509.NewCertPool()
|
2021-08-26 22:04:32 +00:00
|
|
|
for _, group := range groups {
|
|
|
|
for _, pem := range group {
|
|
|
|
if !pool.AppendCertsFromPEM([]byte(pem)) {
|
|
|
|
return nil, fmt.Errorf("failed to parse PEM %s", pem)
|
|
|
|
}
|
2022-04-14 20:55:10 +00:00
|
|
|
if len(pem) > 0 {
|
|
|
|
haveCerts = true
|
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-14 20:55:10 +00:00
|
|
|
if !haveCerts {
|
2019-06-27 20:22:07 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return pool, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-08 10:16:16 +00:00
|
|
|
func LoadCAs(caFile, caPath string) ([]string, error) {
|
2019-06-27 20:22:07 +00:00
|
|
|
if caFile == "" && caPath == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pems := []string{}
|
|
|
|
|
|
|
|
readFn := func(path string) error {
|
2022-11-10 16:26:01 +00:00
|
|
|
pem, err := os.ReadFile(path)
|
2019-06-27 20:22:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// internalRPCTLSConfig generates a *tls.Config for the internal RPC protocol.
|
|
|
|
//
|
2019-03-13 09:29:06 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
2022-03-18 10:46:58 +00:00
|
|
|
func (c *Configurator) internalRPCTLSConfig(verifyIncoming bool) *tls.Config {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2022-03-18 10:46:58 +00:00
|
|
|
|
|
|
|
config := c.commonTLSConfig(
|
|
|
|
c.internalRPC,
|
|
|
|
c.base.InternalRPC,
|
|
|
|
verifyIncoming,
|
|
|
|
)
|
|
|
|
config.InsecureSkipVerify = !c.base.InternalRPC.VerifyServerHostname
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (c *Configurator) commonTLSConfig(state protocolConfig, cfg ProtocolConfig, verifyIncoming bool) *tls.Config {
|
|
|
|
var tlsConfig tls.Config
|
2014-11-18 16:03:36 +00:00
|
|
|
|
2017-04-27 08:29:39 +00:00
|
|
|
// Set the cipher suites
|
2022-03-18 10:46:58 +00:00
|
|
|
if len(cfg.CipherSuites) != 0 {
|
2022-03-24 19:32:25 +00:00
|
|
|
// TLS cipher suites are validated on input in agent config builder,
|
|
|
|
// so it's safe to ignore the error case here.
|
|
|
|
|
|
|
|
cipherSuites, _ := cipherSuiteLookup(cfg.CipherSuites)
|
|
|
|
tlsConfig.CipherSuites = cipherSuites
|
2017-04-27 08:29:39 +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) {
|
2022-03-18 10:46:58 +00:00
|
|
|
if state.cert != nil {
|
|
|
|
return state.cert, nil
|
|
|
|
}
|
|
|
|
return c.autoTLS.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) {
|
2022-09-16 01:12:10 +00:00
|
|
|
cert := state.cert
|
|
|
|
|
|
|
|
// In the general case we only prefer to dial out with the autoTLS cert if we are a client.
|
|
|
|
// The server's autoTLS cert is exclusively for peering control plane traffic.
|
|
|
|
if !c.base.ServerMode && c.autoTLS.cert != nil {
|
|
|
|
cert = c.autoTLS.cert
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsConfig.ClientCAs = state.combinedCAPool
|
|
|
|
tlsConfig.RootCAs = state.combinedCAPool
|
2019-03-13 09:29:06 +00:00
|
|
|
|
2022-03-24 19:32:25 +00:00
|
|
|
// Error handling is not needed here because agent config builder handles ""
|
|
|
|
// or a nil value as TLSVersionAuto with goTLSVersions mapping TLSVersionAuto
|
|
|
|
// to TLS 1.2 and because the initial check makes sure a specified version is
|
|
|
|
// not invalid.
|
|
|
|
tlsConfig.MinVersion = goTLSVersions[cfg.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
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
return &tlsConfig
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// Cert returns the certificate used for connections on the internal RPC protocol.
|
|
|
|
//
|
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 {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2022-03-18 10:46:58 +00:00
|
|
|
cert := c.internalRPC.cert
|
2020-01-22 10:32:17 +00:00
|
|
|
if cert == nil {
|
2020-07-30 14:15:12 +00:00
|
|
|
cert = c.autoTLS.cert
|
2020-01-22 10:32:17 +00:00
|
|
|
}
|
|
|
|
return cert
|
|
|
|
}
|
|
|
|
|
2022-08-24 16:31:38 +00:00
|
|
|
// GRPCServerUseTLS returns whether there's a TLS certificate configured for
|
|
|
|
// (external) gRPC (either manually or by auto-config/auto-encrypt), and use
|
|
|
|
// of TLS for gRPC has not been explicitly disabled at auto-encrypt.
|
2022-03-18 10:46:58 +00:00
|
|
|
//
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
2022-08-24 16:31:38 +00:00
|
|
|
func (c *Configurator) GRPCServerUseTLS() bool {
|
2022-03-18 10:46:58 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2022-08-24 16:31:38 +00:00
|
|
|
return c.grpc.cert != nil || (c.grpc.useAutoCert && c.autoTLS.cert != nil)
|
2022-03-18 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyIncomingRPC returns true if we should verify incoming connnections to
|
|
|
|
// the internal RPC protocol.
|
2019-06-27 20:22:07 +00:00
|
|
|
func (c *Configurator) VerifyIncomingRPC() bool {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2022-03-18 10:46:58 +00:00
|
|
|
return c.base.InternalRPC.VerifyIncoming
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
2021-06-24 18:55:00 +00:00
|
|
|
func (c *Configurator) outgoingRPCTLSEnabled() bool {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
|
2021-06-24 18:55:00 +00:00
|
|
|
// use TLS if AutoEncrypt or VerifyOutgoing are enabled.
|
2022-03-18 10:46:58 +00:00
|
|
|
return c.base.AutoTLS || c.base.InternalRPC.VerifyOutgoing
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// MutualTLSCapable returns true if Configurator has a CA and a local TL
|
|
|
|
// certificate configured on the internal RPC protocol.
|
2020-03-09 20:59:02 +00:00
|
|
|
func (c *Configurator) MutualTLSCapable() bool {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2022-03-18 10:46:58 +00:00
|
|
|
return c.internalRPC.combinedCAPool != nil && (c.autoTLS.cert != nil || c.internalRPC.cert != nil)
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
// If AutoEncryptTLS is enabled and there is a CA, then verify
|
|
|
|
// outgoing.
|
2022-03-18 10:46:58 +00:00
|
|
|
if c.base.AutoTLS && c.internalRPC.combinedCAPool != nil {
|
2019-06-27 20:22:07 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
return c.base.InternalRPC.VerifyOutgoing
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// This function acquires a read lock because it reads from the config.
|
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 {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2019-06-27 20:22:07 +00:00
|
|
|
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) serverNameOrNodeName() string {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2019-03-13 09:29:06 +00:00
|
|
|
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 {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2022-03-18 10:46:58 +00:00
|
|
|
return c.base.InternalRPC.VerifyServerHostname || c.autoTLS.verifyServerHostname
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutoConfigTLSSettings constructs the pbconfig.TLS that will be returned by
|
|
|
|
// servers in the auto-config endpoint.
|
|
|
|
func (c *Configurator) AutoConfigTLSSettings() (*pbconfig.TLS, error) {
|
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
|
|
|
|
|
|
|
cfg := c.base.InternalRPC
|
|
|
|
|
|
|
|
cipherString, err := CipherString(cfg.CipherSuites)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pbconfig.TLS{
|
|
|
|
VerifyOutgoing: cfg.VerifyOutgoing,
|
|
|
|
VerifyServerHostname: cfg.VerifyServerHostname || c.autoTLS.verifyServerHostname,
|
2022-03-24 19:32:25 +00:00
|
|
|
MinVersion: types.ConsulAutoConfigTLSVersionStrings[cfg.TLSMinVersion],
|
2022-03-18 10:46:58 +00:00
|
|
|
CipherSuites: cipherString,
|
|
|
|
}, nil
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// IncomingGRPCConfig generates a *tls.Config for incoming external (e.g. xDS)
|
|
|
|
// GRPC connections.
|
|
|
|
//
|
|
|
|
// This function acquires a read lock because it reads from the config.
|
2021-09-08 15:48:41 +00:00
|
|
|
func (c *Configurator) IncomingGRPCConfig() *tls.Config {
|
2022-03-18 10:46:58 +00:00
|
|
|
c.log("IncomingGRPConfig")
|
|
|
|
|
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
|
|
|
|
|
|
|
config := c.commonTLSConfig(
|
|
|
|
c.grpc,
|
|
|
|
c.base.GRPC,
|
|
|
|
c.base.GRPC.VerifyIncoming,
|
|
|
|
)
|
2022-10-28 20:34:41 +00:00
|
|
|
config.GetConfigForClient = func(info *tls.ClientHelloInfo) (*tls.Config, error) {
|
|
|
|
conf := c.IncomingGRPCConfig()
|
|
|
|
// Do not enforce mutualTLS for peering SNI entries. This is necessary, because
|
|
|
|
// there is no way to specify an mTLS cert when establishing a peering connection.
|
|
|
|
// This bypass is only safe because the `grpc-middleware.AuthInterceptor` explicitly
|
|
|
|
// restricts the list of endpoints that can be called when peering SNI is present.
|
|
|
|
if c.autoTLS.peeringServerName != "" && info.ServerName == c.autoTLS.peeringServerName {
|
|
|
|
conf.ClientAuth = tls.NoClientCert
|
|
|
|
}
|
|
|
|
return conf, nil
|
2020-01-22 10:32:17 +00:00
|
|
|
}
|
2022-09-09 19:01:47 +00:00
|
|
|
config.GetCertificate = func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
if c.autoTLS.peeringServerName != "" && info.ServerName == c.autoTLS.peeringServerName {
|
|
|
|
// For peering control plane traffic we exclusively use the internally managed certificate.
|
|
|
|
// For all other traffic it is only a fallback if no manual certificate is provisioned.
|
|
|
|
return c.autoTLS.cert, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.grpc.cert != nil {
|
|
|
|
return c.grpc.cert, nil
|
|
|
|
}
|
|
|
|
return c.autoTLS.cert, nil
|
|
|
|
}
|
2020-01-22 10:32:17 +00:00
|
|
|
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")
|
2022-03-18 10:46:58 +00:00
|
|
|
config := c.internalRPCTLSConfig(c.base.InternalRPC.VerifyIncoming)
|
2019-03-13 09:29:06 +00:00
|
|
|
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.
|
2022-03-18 10:46:58 +00:00
|
|
|
config := c.internalRPCTLSConfig(true)
|
2020-03-09 20:59:02 +00:00
|
|
|
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")
|
2022-03-18 10:46:58 +00:00
|
|
|
config := c.internalRPCTLSConfig(false)
|
2019-06-27 20:22:07 +00:00
|
|
|
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")
|
2021-06-24 18:45:52 +00:00
|
|
|
|
|
|
|
c.lock.RLock()
|
2022-03-18 10:46:58 +00:00
|
|
|
defer c.lock.RUnlock()
|
2021-06-24 18:45:52 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
config := c.commonTLSConfig(
|
|
|
|
c.https,
|
|
|
|
c.base.HTTPS,
|
|
|
|
c.base.HTTPS.VerifyIncoming,
|
|
|
|
)
|
2019-03-13 09:29:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-25 06:35:34 +00:00
|
|
|
// OutgoingTLSConfigForCheck generates a *tls.Config for outgoing TLS connections
|
|
|
|
// for 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.
|
2021-02-25 06:35:34 +00:00
|
|
|
func (c *Configurator) OutgoingTLSConfigForCheck(skipVerify bool, serverName string) *tls.Config {
|
2019-03-13 09:29:06 +00:00
|
|
|
c.log("OutgoingTLSConfigForCheck")
|
2021-02-25 06:35:34 +00:00
|
|
|
|
2021-06-24 18:31:17 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
useAgentTLS := c.base.EnableAgentTLSForChecks
|
|
|
|
c.lock.RUnlock()
|
|
|
|
|
|
|
|
if !useAgentTLS {
|
2019-02-27 09:14:59 +00:00
|
|
|
return &tls.Config{
|
2019-03-13 09:29:06 +00:00
|
|
|
InsecureSkipVerify: skipVerify,
|
2021-02-25 06:35:34 +00:00
|
|
|
ServerName: serverName,
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 17:36:58 +00:00
|
|
|
if serverName == "" {
|
|
|
|
serverName = c.serverNameOrNodeName()
|
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
config := c.internalRPCTLSConfig(false)
|
2019-03-13 09:29:06 +00:00
|
|
|
config.InsecureSkipVerify = skipVerify
|
2021-02-25 06:35:34 +00:00
|
|
|
config.ServerName = serverName
|
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
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// OutgoingRPCConfig generates a *tls.Config for outgoing internal 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")
|
2021-06-24 18:55:00 +00:00
|
|
|
if !c.outgoingRPCTLSEnabled() {
|
2019-03-13 09:29:06 +00:00
|
|
|
return nil
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
return c.internalRPCTLSConfig(false)
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 18:31:17 +00:00
|
|
|
// outgoingALPNRPCConfig generates a *tls.Config for outgoing RPC connections
|
2020-03-09 20:59:02 +00:00
|
|
|
// 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.
|
2021-06-24 18:31:17 +00:00
|
|
|
func (c *Configurator) outgoingALPNRPCConfig() *tls.Config {
|
|
|
|
c.log("outgoingALPNRPCConfig")
|
|
|
|
if !c.MutualTLSCapable() {
|
2020-03-09 20:59:02 +00:00
|
|
|
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.
|
2022-03-18 10:46:58 +00:00
|
|
|
config := c.internalRPCTLSConfig(true)
|
2020-03-09 20:59:02 +00:00
|
|
|
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
|
|
|
|
2021-06-24 18:55:00 +00:00
|
|
|
// UseTLS returns true if the outgoing RPC requests have been explicitly configured
|
|
|
|
// to use TLS (via VerifyOutgoing or AutoTLS, and the target DC supports TLS.
|
2020-04-30 20:12:17 +00:00
|
|
|
func (c *Configurator) UseTLS(dc string) bool {
|
2021-06-24 18:55:00 +00:00
|
|
|
return c.outgoingRPCTLSEnabled() && c.getAreaForPeerDatacenterUseTLS(dc)
|
2020-04-30 20:12:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 18:31:17 +00:00
|
|
|
// OutgoingALPNRPCWrapper wraps the result of outgoingALPNRPCConfig in an
|
2020-03-09 20:59:02 +00:00
|
|
|
// ALPNWrapper. It configures all of the negotiation plumbing.
|
|
|
|
func (c *Configurator) OutgoingALPNRPCWrapper() ALPNWrapper {
|
|
|
|
c.log("OutgoingALPNRPCWrapper")
|
2021-06-24 18:31:17 +00:00
|
|
|
if !c.MutualTLSCapable() {
|
2020-03-09 20:59:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 15:42:32 +00:00
|
|
|
return c.wrapALPNTLSClient
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 19:19:34 +00:00
|
|
|
// AutoEncryptCert returns the TLS certificate received from auto-encrypt.
|
|
|
|
func (c *Configurator) AutoEncryptCert() *x509.Certificate {
|
2021-06-17 22:48:44 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2020-07-30 14:15:12 +00:00
|
|
|
tlsCert := c.autoTLS.cert
|
2019-11-12 18:40:32 +00:00
|
|
|
if tlsCert == nil || tlsCert.Certificate == nil {
|
2021-06-21 19:19:34 +00:00
|
|
|
return nil
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
|
|
|
|
if err != nil {
|
2021-06-21 19:19:34 +00:00
|
|
|
return nil
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
2021-06-21 19:19:34 +00:00
|
|
|
return cert
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 20:34:41 +00:00
|
|
|
func (c *Configurator) PeeringServerName() string {
|
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
|
|
|
return c.autoTLS.peeringServerName
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func (c *Configurator) log(name string) {
|
2021-06-17 23:35:58 +00:00
|
|
|
if c.logger != nil && c.logger.IsTrace() {
|
|
|
|
c.logger.Trace(name, "version", atomic.LoadUint64(&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(),
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:48:54 +00:00
|
|
|
cs := tlsConn.ConnectionState()
|
|
|
|
for _, cert := range cs.PeerCertificates[1:] {
|
2019-03-13 09:29:06 +00:00
|
|
|
opts.Intermediates.AddCert(cert)
|
|
|
|
}
|
2021-09-09 19:48:54 +00:00
|
|
|
_, err = cs.PeerCertificates[0].Verify(opts)
|
2019-03-13 09:29:06 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-06-24 18:31:17 +00:00
|
|
|
config := c.outgoingALPNRPCConfig()
|
2020-03-09 20:59:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsConn, nil
|
|
|
|
}
|
|
|
|
|
2021-10-04 17:22:30 +00:00
|
|
|
type TLSConn interface {
|
|
|
|
ConnectionState() tls.ConnectionState
|
|
|
|
}
|
|
|
|
|
2021-08-26 22:04:32 +00:00
|
|
|
// AuthorizeServerConn is used to validate that the connection is being established
|
|
|
|
// by a Consul server in the same datacenter.
|
|
|
|
//
|
|
|
|
// The identity of the connection is checked by verifying that the certificate
|
|
|
|
// presented is signed by the Agent TLS CA, and has a DNSName that matches the
|
|
|
|
// local ServerSNI name.
|
|
|
|
//
|
2021-10-08 16:08:56 +00:00
|
|
|
// Note this check is only performed if VerifyServerHostname and VerifyIncomingRPC
|
|
|
|
// are both enabled, otherwise it does no authorization.
|
2021-10-04 17:22:30 +00:00
|
|
|
func (c *Configurator) AuthorizeServerConn(dc string, conn TLSConn) error {
|
2021-10-08 16:08:56 +00:00
|
|
|
if !c.VerifyIncomingRPC() || !c.VerifyServerHostname() {
|
2021-08-26 22:04:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.lock.RLock()
|
2022-03-18 10:46:58 +00:00
|
|
|
caPool := c.internalRPC.manualCAPool
|
2021-08-26 22:04:32 +00:00
|
|
|
c.lock.RUnlock()
|
|
|
|
|
|
|
|
expected := c.ServerSNI(dc, "")
|
2021-09-09 19:48:54 +00:00
|
|
|
cs := conn.ConnectionState()
|
|
|
|
var errs error
|
|
|
|
for _, chain := range cs.VerifiedChains {
|
2021-08-26 22:04:32 +00:00
|
|
|
if len(chain) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-09 19:48:54 +00:00
|
|
|
opts := x509.VerifyOptions{
|
|
|
|
DNSName: expected,
|
|
|
|
Intermediates: x509.NewCertPool(),
|
|
|
|
Roots: caPool,
|
|
|
|
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
}
|
|
|
|
for _, cert := range cs.PeerCertificates[1:] {
|
|
|
|
opts.Intermediates.AddCert(cert)
|
|
|
|
}
|
|
|
|
_, err := cs.PeerCertificates[0].Verify(opts)
|
2021-08-26 22:04:32 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-04 17:22:30 +00:00
|
|
|
errs = multierror.Append(errs, err)
|
2021-08-26 22:04:32 +00:00
|
|
|
}
|
2021-10-08 16:08:56 +00:00
|
|
|
if errs == nil {
|
|
|
|
errs = fmt.Errorf("no verified chains")
|
|
|
|
}
|
2021-09-09 19:48:54 +00:00
|
|
|
return fmt.Errorf("AuthorizeServerConn failed certificate validation for certificate with a SAN.DNSName of %v: %w", expected, errs)
|
|
|
|
|
2021-08-26 22:04:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 19:32:25 +00:00
|
|
|
// NOTE: any new cipher suites will also need to be added in types/tls.go
|
|
|
|
// TODO: should this be moved into types/tls.go? Would importing Go's tls
|
|
|
|
// package in there be acceptable?
|
|
|
|
var goTLSCipherSuites = map[types.TLSCipherSuite]uint16{
|
|
|
|
types.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256: tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
|
|
|
|
types.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA: tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
types.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
types.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA: tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
|
|
|
types.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
|
|
|
|
types.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256: tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
|
|
|
|
types.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA: tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
types.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
types.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA: tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
types.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
}
|
|
|
|
|
|
|
|
func cipherSuiteLookup(ciphers []types.TLSCipherSuite) ([]uint16, error) {
|
2017-04-27 08:29:39 +00:00
|
|
|
suites := []uint16{}
|
|
|
|
|
2022-03-24 19:32:25 +00:00
|
|
|
if len(ciphers) == 0 {
|
2017-04-27 08:29:39 +00:00
|
|
|
return []uint16{}, nil
|
|
|
|
}
|
2022-03-24 19:32:25 +00:00
|
|
|
|
2017-04-27 08:29:39 +00:00
|
|
|
for _, cipher := range ciphers {
|
2022-03-24 19:32:25 +00:00
|
|
|
if v, ok := goTLSCipherSuites[cipher]; ok {
|
2017-04-27 08:29:39 +00:00
|
|
|
suites = append(suites, v)
|
|
|
|
} else {
|
|
|
|
return suites, fmt.Errorf("unsupported cipher %q", cipher)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return suites, nil
|
|
|
|
}
|
2020-06-05 19:56:19 +00:00
|
|
|
|
2022-03-24 19:32:25 +00:00
|
|
|
// CipherString performs the inverse operation of types.ParseCiphers
|
|
|
|
func CipherString(ciphers []types.TLSCipherSuite) (string, error) {
|
|
|
|
err := types.ValidateConsulAgentCipherSuites(ciphers)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cipherStrings := make([]string, len(ciphers))
|
|
|
|
for i, cipher := range ciphers {
|
2022-03-24 19:32:25 +00:00
|
|
|
cipherStrings[i] = string(cipher)
|
2020-06-05 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(cipherStrings, ","), nil
|
|
|
|
}
|