|
@@ -52,19 +52,23 @@ var clientCipherSuites = []uint16{
|
|
// known weak algorithms removed.
|
|
// known weak algorithms removed.
|
|
var DefaultServerAcceptedCiphers = append(clientCipherSuites, acceptedCBCCiphers...)
|
|
var DefaultServerAcceptedCiphers = append(clientCipherSuites, acceptedCBCCiphers...)
|
|
|
|
|
|
-// ServerDefault is a secure-enough TLS configuration for the server TLS configuration.
|
|
|
|
-var ServerDefault = tls.Config{
|
|
|
|
- // Avoid fallback to SSL protocols < TLS1.0
|
|
|
|
- MinVersion: tls.VersionTLS10,
|
|
|
|
- PreferServerCipherSuites: true,
|
|
|
|
- CipherSuites: DefaultServerAcceptedCiphers,
|
|
|
|
|
|
+// ServerDefault returns a secure-enough TLS configuration for the server TLS configuration.
|
|
|
|
+func ServerDefault() *tls.Config {
|
|
|
|
+ return &tls.Config{
|
|
|
|
+ // Avoid fallback to SSL protocols < TLS1.0
|
|
|
|
+ MinVersion: tls.VersionTLS10,
|
|
|
|
+ PreferServerCipherSuites: true,
|
|
|
|
+ CipherSuites: DefaultServerAcceptedCiphers,
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-// ClientDefault is a secure-enough TLS configuration for the client TLS configuration.
|
|
|
|
-var ClientDefault = tls.Config{
|
|
|
|
- // Prefer TLS1.2 as the client minimum
|
|
|
|
- MinVersion: tls.VersionTLS12,
|
|
|
|
- CipherSuites: clientCipherSuites,
|
|
|
|
|
|
+// ClientDefault returns a secure-enough TLS configuration for the client TLS configuration.
|
|
|
|
+func ClientDefault() *tls.Config {
|
|
|
|
+ return &tls.Config{
|
|
|
|
+ // Prefer TLS1.2 as the client minimum
|
|
|
|
+ MinVersion: tls.VersionTLS12,
|
|
|
|
+ CipherSuites: clientCipherSuites,
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// certPool returns an X.509 certificate pool from `caFile`, the certificate file.
|
|
// certPool returns an X.509 certificate pool from `caFile`, the certificate file.
|
|
@@ -78,20 +82,15 @@ func certPool(caFile string) (*x509.CertPool, error) {
|
|
if !certPool.AppendCertsFromPEM(pem) {
|
|
if !certPool.AppendCertsFromPEM(pem) {
|
|
return nil, fmt.Errorf("failed to append certificates from PEM file: %q", caFile)
|
|
return nil, fmt.Errorf("failed to append certificates from PEM file: %q", caFile)
|
|
}
|
|
}
|
|
- s := certPool.Subjects()
|
|
|
|
- subjects := make([]string, len(s))
|
|
|
|
- for i, subject := range s {
|
|
|
|
- subjects[i] = string(subject)
|
|
|
|
- }
|
|
|
|
- logrus.Debugf("Trusting certs with subjects: %v", subjects)
|
|
|
|
|
|
+ logrus.Debugf("Trusting %d certs", len(certPool.Subjects()))
|
|
return certPool, nil
|
|
return certPool, nil
|
|
}
|
|
}
|
|
|
|
|
|
// Client returns a TLS configuration meant to be used by a client.
|
|
// Client returns a TLS configuration meant to be used by a client.
|
|
func Client(options Options) (*tls.Config, error) {
|
|
func Client(options Options) (*tls.Config, error) {
|
|
- tlsConfig := ClientDefault
|
|
|
|
|
|
+ tlsConfig := ClientDefault()
|
|
tlsConfig.InsecureSkipVerify = options.InsecureSkipVerify
|
|
tlsConfig.InsecureSkipVerify = options.InsecureSkipVerify
|
|
- if !options.InsecureSkipVerify {
|
|
|
|
|
|
+ if !options.InsecureSkipVerify && options.CAFile != "" {
|
|
CAs, err := certPool(options.CAFile)
|
|
CAs, err := certPool(options.CAFile)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
@@ -99,7 +98,7 @@ func Client(options Options) (*tls.Config, error) {
|
|
tlsConfig.RootCAs = CAs
|
|
tlsConfig.RootCAs = CAs
|
|
}
|
|
}
|
|
|
|
|
|
- if options.CertFile != "" && options.KeyFile != "" {
|
|
|
|
|
|
+ if options.CertFile != "" || options.KeyFile != "" {
|
|
tlsCert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
|
|
tlsCert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Could not load X509 key pair: %v. Make sure the key is not encrypted", err)
|
|
return nil, fmt.Errorf("Could not load X509 key pair: %v. Make sure the key is not encrypted", err)
|
|
@@ -107,12 +106,12 @@ func Client(options Options) (*tls.Config, error) {
|
|
tlsConfig.Certificates = []tls.Certificate{tlsCert}
|
|
tlsConfig.Certificates = []tls.Certificate{tlsCert}
|
|
}
|
|
}
|
|
|
|
|
|
- return &tlsConfig, nil
|
|
|
|
|
|
+ return tlsConfig, nil
|
|
}
|
|
}
|
|
|
|
|
|
// Server returns a TLS configuration meant to be used by a server.
|
|
// Server returns a TLS configuration meant to be used by a server.
|
|
func Server(options Options) (*tls.Config, error) {
|
|
func Server(options Options) (*tls.Config, error) {
|
|
- tlsConfig := ServerDefault
|
|
|
|
|
|
+ tlsConfig := ServerDefault()
|
|
tlsConfig.ClientAuth = options.ClientAuth
|
|
tlsConfig.ClientAuth = options.ClientAuth
|
|
tlsCert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
|
|
tlsCert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -129,5 +128,5 @@ func Server(options Options) (*tls.Config, error) {
|
|
}
|
|
}
|
|
tlsConfig.ClientCAs = CAs
|
|
tlsConfig.ClientCAs = CAs
|
|
}
|
|
}
|
|
- return &tlsConfig, nil
|
|
|
|
|
|
+ return tlsConfig, nil
|
|
}
|
|
}
|