2015-03-13 16:37:32 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-12-15 22:33:55 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-07-23 20:51:45 +00:00
|
|
|
// We must import sha512 so that it registers with the runtime so that
|
|
|
|
// certificates that use it can be parsed.
|
|
|
|
_ "crypto/sha512"
|
2015-03-13 16:56:08 +00:00
|
|
|
"crypto/tls"
|
2017-08-03 11:33:06 +00:00
|
|
|
"crypto/x509"
|
2015-03-13 16:37:32 +00:00
|
|
|
"fmt"
|
2016-06-02 16:40:25 +00:00
|
|
|
"io"
|
2017-08-03 11:33:06 +00:00
|
|
|
"io/ioutil"
|
2015-03-13 16:37:32 +00:00
|
|
|
"net"
|
2016-07-12 23:32:47 +00:00
|
|
|
|
2017-06-22 19:29:53 +00:00
|
|
|
"github.com/hashicorp/vault/helper/parseutil"
|
2017-08-23 16:00:09 +00:00
|
|
|
"github.com/hashicorp/vault/helper/proxyutil"
|
2017-07-31 15:28:06 +00:00
|
|
|
"github.com/hashicorp/vault/helper/reload"
|
2016-07-12 23:32:47 +00:00
|
|
|
"github.com/hashicorp/vault/helper/tlsutil"
|
2017-12-15 22:33:55 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2015-03-13 16:37:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListenerFactory is the factory function to create a listener.
|
2017-12-15 22:33:55 +00:00
|
|
|
type ListenerFactory func(map[string]interface{}, io.Writer, cli.Ui) (net.Listener, map[string]string, reload.ReloadFunc, error)
|
2015-03-13 16:37:32 +00:00
|
|
|
|
|
|
|
// BuiltinListeners is the list of built-in listener types.
|
|
|
|
var BuiltinListeners = map[string]ListenerFactory{
|
2017-06-22 19:29:53 +00:00
|
|
|
"tcp": tcpListenerFactory,
|
2015-03-13 16:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewListener creates a new listener of the given type with the given
|
|
|
|
// configuration. The type is looked up in the BuiltinListeners map.
|
2017-12-15 22:33:55 +00:00
|
|
|
func NewListener(t string, config map[string]interface{}, logger io.Writer, ui cli.Ui) (net.Listener, map[string]string, reload.ReloadFunc, error) {
|
2015-03-13 16:37:32 +00:00
|
|
|
f, ok := BuiltinListeners[t]
|
|
|
|
if !ok {
|
2016-03-10 02:40:46 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("unknown listener type: %s", t)
|
2015-03-13 16:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 22:33:55 +00:00
|
|
|
return f(config, logger, ui)
|
2015-03-13 16:37:32 +00:00
|
|
|
}
|
2015-03-13 16:56:08 +00:00
|
|
|
|
2017-08-23 16:00:09 +00:00
|
|
|
func listenerWrapProxy(ln net.Listener, config map[string]interface{}) (net.Listener, error) {
|
|
|
|
behaviorRaw, ok := config["proxy_protocol_behavior"]
|
|
|
|
if !ok {
|
|
|
|
return ln, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
behavior, ok := behaviorRaw.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("failed parsing proxy_protocol_behavior value: not a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
authorizedAddrsRaw, ok := config["proxy_protocol_authorized_addrs"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("proxy_protocol_behavior set but no proxy_protocol_authorized_addrs value")
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyProtoConfig := &proxyutil.ProxyProtoConfig{
|
|
|
|
Behavior: behavior,
|
|
|
|
}
|
|
|
|
if err := proxyProtoConfig.SetAuthorizedAddrs(authorizedAddrsRaw); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed parsing proxy_protocol_authorized_addrs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newLn, err := proxyutil.WrapInProxyProto(ln, proxyProtoConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed configuring PROXY protocol wrapper: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newLn, nil
|
|
|
|
}
|
|
|
|
|
2015-03-13 16:56:08 +00:00
|
|
|
func listenerWrapTLS(
|
2015-04-04 19:06:41 +00:00
|
|
|
ln net.Listener,
|
|
|
|
props map[string]string,
|
2017-12-15 22:33:55 +00:00
|
|
|
config map[string]interface{},
|
|
|
|
ui cli.Ui) (net.Listener, map[string]string, reload.ReloadFunc, error) {
|
2015-04-04 19:06:41 +00:00
|
|
|
props["tls"] = "disabled"
|
|
|
|
|
2015-11-25 19:37:57 +00:00
|
|
|
if v, ok := config["tls_disable"]; ok {
|
2017-06-22 19:29:53 +00:00
|
|
|
disabled, err := parseutil.ParseBool(v)
|
2015-11-25 19:37:57 +00:00
|
|
|
if err != nil {
|
2016-03-10 02:40:46 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("invalid value for 'tls_disable': %v", err)
|
2015-11-25 19:37:57 +00:00
|
|
|
}
|
|
|
|
if disabled {
|
2016-03-10 02:40:46 +00:00
|
|
|
return ln, props, nil, nil
|
2015-11-25 19:37:57 +00:00
|
|
|
}
|
2015-03-13 16:56:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 22:33:55 +00:00
|
|
|
certFileRaw, ok := config["tls_cert_file"]
|
2015-03-13 16:56:08 +00:00
|
|
|
if !ok {
|
2016-03-10 02:40:46 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("'tls_cert_file' must be set")
|
2015-03-13 16:56:08 +00:00
|
|
|
}
|
2017-12-15 22:33:55 +00:00
|
|
|
certFile := certFileRaw.(string)
|
|
|
|
keyFileRaw, ok := config["tls_key_file"]
|
2015-03-13 16:56:08 +00:00
|
|
|
if !ok {
|
2016-03-10 02:40:46 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("'tls_key_file' must be set")
|
2015-03-13 16:56:08 +00:00
|
|
|
}
|
2017-12-15 22:33:55 +00:00
|
|
|
keyFile := keyFileRaw.(string)
|
2015-03-13 16:56:08 +00:00
|
|
|
|
2017-12-15 22:33:55 +00:00
|
|
|
cg := reload.NewCertificateGetter(certFile, keyFile, "")
|
2017-07-31 15:28:06 +00:00
|
|
|
if err := cg.Reload(config); err != nil {
|
2017-12-15 22:33:55 +00:00
|
|
|
// We try the key without a passphrase first and if we get an incorrect
|
|
|
|
// passphrase response, try again after prompting for a passphrase
|
|
|
|
if errwrap.Contains(err, x509.IncorrectPasswordError.Error()) {
|
|
|
|
var passphrase string
|
|
|
|
passphrase, err = ui.AskSecret(fmt.Sprintf("Enter passphrase for %s:", keyFile))
|
|
|
|
if err == nil {
|
|
|
|
cg = reload.NewCertificateGetter(certFile, keyFile, passphrase)
|
|
|
|
if err = cg.Reload(config); err == nil {
|
|
|
|
goto PASSPHRASECORRECT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil, nil, errwrap.Wrapf("error loading TLS cert: {{err}}", err)
|
2015-03-13 16:56:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 22:33:55 +00:00
|
|
|
PASSPHRASECORRECT:
|
2017-06-22 19:29:53 +00:00
|
|
|
var tlsvers string
|
|
|
|
tlsversRaw, ok := config["tls_min_version"]
|
2015-07-23 03:19:41 +00:00
|
|
|
if !ok {
|
|
|
|
tlsvers = "tls12"
|
2017-06-22 19:29:53 +00:00
|
|
|
} else {
|
|
|
|
tlsvers = tlsversRaw.(string)
|
2015-07-23 03:19:41 +00:00
|
|
|
}
|
2015-07-23 20:51:45 +00:00
|
|
|
|
2015-03-13 16:56:08 +00:00
|
|
|
tlsConf := &tls.Config{}
|
2017-07-31 15:28:06 +00:00
|
|
|
tlsConf.GetCertificate = cg.GetCertificate
|
2016-07-20 17:57:49 +00:00
|
|
|
tlsConf.NextProtos = []string{"h2", "http/1.1"}
|
2016-07-12 23:32:47 +00:00
|
|
|
tlsConf.MinVersion, ok = tlsutil.TLSLookup[tlsvers]
|
2015-07-23 03:19:41 +00:00
|
|
|
if !ok {
|
2016-03-10 02:40:46 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("'tls_min_version' value %s not supported, please specify one of [tls10,tls11,tls12]", tlsvers)
|
2015-07-23 03:19:41 +00:00
|
|
|
}
|
2015-05-20 23:01:40 +00:00
|
|
|
tlsConf.ClientAuth = tls.RequestClientCert
|
2015-03-13 16:56:08 +00:00
|
|
|
|
2017-01-23 18:48:35 +00:00
|
|
|
if v, ok := config["tls_cipher_suites"]; ok {
|
2017-06-22 19:29:53 +00:00
|
|
|
ciphers, err := tlsutil.ParseCiphers(v.(string))
|
2017-01-23 18:48:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("invalid value for 'tls_cipher_suites': %v", err)
|
|
|
|
}
|
|
|
|
tlsConf.CipherSuites = ciphers
|
|
|
|
}
|
|
|
|
if v, ok := config["tls_prefer_server_cipher_suites"]; ok {
|
2017-06-22 19:29:53 +00:00
|
|
|
preferServer, err := parseutil.ParseBool(v)
|
2017-01-23 18:48:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("invalid value for 'tls_prefer_server_cipher_suites': %v", err)
|
|
|
|
}
|
|
|
|
tlsConf.PreferServerCipherSuites = preferServer
|
|
|
|
}
|
2017-09-25 18:41:46 +00:00
|
|
|
var requireVerifyCerts bool
|
|
|
|
var err error
|
2017-03-08 15:21:31 +00:00
|
|
|
if v, ok := config["tls_require_and_verify_client_cert"]; ok {
|
2017-09-25 18:41:46 +00:00
|
|
|
requireVerifyCerts, err = parseutil.ParseBool(v)
|
2017-03-08 15:21:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("invalid value for 'tls_require_and_verify_client_cert': %v", err)
|
|
|
|
}
|
2017-09-25 18:41:46 +00:00
|
|
|
if requireVerifyCerts {
|
2017-03-08 15:21:31 +00:00
|
|
|
tlsConf.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
}
|
2017-08-03 11:33:06 +00:00
|
|
|
if tlsClientCaFile, ok := config["tls_client_ca_file"]; ok {
|
|
|
|
caPool := x509.NewCertPool()
|
|
|
|
data, err := ioutil.ReadFile(tlsClientCaFile.(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("failed to read tls_client_ca_file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !caPool.AppendCertsFromPEM(data) {
|
|
|
|
return nil, nil, nil, fmt.Errorf("failed to parse CA certificate in tls_client_ca_file")
|
|
|
|
}
|
|
|
|
tlsConf.ClientCAs = caPool
|
|
|
|
}
|
2017-03-08 15:21:31 +00:00
|
|
|
}
|
2017-09-25 18:41:46 +00:00
|
|
|
if v, ok := config["tls_disable_client_certs"]; ok {
|
|
|
|
disableClientCerts, err := parseutil.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("invalid value for 'tls_disable_client_certs': %v", err)
|
|
|
|
}
|
|
|
|
if disableClientCerts && requireVerifyCerts {
|
|
|
|
return nil, nil, nil, fmt.Errorf("'tls_disable_client_certs' and 'tls_require_and_verify_client_cert' are mutually exclusive")
|
|
|
|
}
|
2018-02-28 15:07:23 +00:00
|
|
|
if disableClientCerts {
|
|
|
|
tlsConf.ClientAuth = tls.NoClientCert
|
|
|
|
}
|
2017-09-25 18:41:46 +00:00
|
|
|
}
|
2017-01-23 18:48:35 +00:00
|
|
|
|
2015-03-13 16:56:08 +00:00
|
|
|
ln = tls.NewListener(ln, tlsConf)
|
2015-04-04 19:06:41 +00:00
|
|
|
props["tls"] = "enabled"
|
2017-07-31 15:28:06 +00:00
|
|
|
return ln, props, cg.Reload, nil
|
2015-03-13 16:56:08 +00:00
|
|
|
}
|