2018-05-01 23:45:06 +00:00
|
|
|
// Package tlsconfig provides convenience functions for configuring TLS connections from the
|
|
|
|
// command line.
|
|
|
|
package tlsconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
// Config is the user provided parameters to create a tls.Config
|
|
|
|
type TLSParameters struct {
|
|
|
|
Cert string
|
|
|
|
Key string
|
|
|
|
GetCertificate *CertReloader
|
|
|
|
ClientCAs []string
|
|
|
|
RootCAs []string
|
|
|
|
ServerName string
|
|
|
|
CurvePreferences []tls.CurveID
|
2022-01-03 12:21:04 +00:00
|
|
|
minVersion uint16 // min tls version. If zero, TLS1.0 is defined as minimum.
|
|
|
|
maxVersion uint16 // max tls version. If zero, last TLS version is used defined as limit (currently TLS1.3)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
// GetConfig returns a TLS configuration according to the Config set by the user.
|
|
|
|
func GetConfig(p *TLSParameters) (*tls.Config, error) {
|
|
|
|
tlsconfig := &tls.Config{}
|
2018-11-20 20:34:56 +00:00
|
|
|
if p.Cert != "" && p.Key != "" {
|
2018-11-15 15:43:50 +00:00
|
|
|
cert, err := tls.LoadX509KeyPair(p.Cert, p.Key)
|
2018-05-01 23:45:06 +00:00
|
|
|
if err != nil {
|
2018-11-15 15:43:50 +00:00
|
|
|
return nil, errors.Wrap(err, "Error parsing X509 key pair")
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2018-11-15 15:43:50 +00:00
|
|
|
tlsconfig.Certificates = []tls.Certificate{cert}
|
2018-11-20 20:34:56 +00:00
|
|
|
// BuildNameToCertificate parses Certificates and builds NameToCertificate from common name
|
|
|
|
// and SAN fields of leaf certificates
|
2018-11-15 15:43:50 +00:00
|
|
|
tlsconfig.BuildNameToCertificate()
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 20:34:56 +00:00
|
|
|
if p.GetCertificate != nil {
|
|
|
|
// GetCertificate is called when client supplies SNI info or Certificates is empty.
|
|
|
|
// Order of retrieving certificate is GetCertificate, NameToCertificate and lastly first element of Certificates
|
|
|
|
tlsconfig.GetCertificate = p.GetCertificate.Cert
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
if len(p.ClientCAs) > 0 {
|
2018-05-01 23:45:06 +00:00
|
|
|
// set of root certificate authorities that servers use if required to verify a client certificate
|
|
|
|
// by the policy in ClientAuth
|
2018-11-15 15:43:50 +00:00
|
|
|
clientCAs, err := LoadCert(p.ClientCAs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error loading client CAs")
|
|
|
|
}
|
|
|
|
tlsconfig.ClientCAs = clientCAs
|
2018-05-01 23:45:06 +00:00
|
|
|
// server's policy for TLS Client Authentication. Default is no client cert
|
2018-11-15 15:43:50 +00:00
|
|
|
tlsconfig.ClientAuth = tls.RequireAndVerifyClientCert
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
if len(p.RootCAs) > 0 {
|
|
|
|
rootCAs, err := LoadCert(p.RootCAs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Error loading root CAs")
|
2018-08-27 18:53:08 +00:00
|
|
|
}
|
2018-11-15 15:43:50 +00:00
|
|
|
tlsconfig.RootCAs = rootCAs
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
if p.ServerName != "" {
|
|
|
|
tlsconfig.ServerName = p.ServerName
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
if len(p.CurvePreferences) > 0 {
|
|
|
|
tlsconfig.CurvePreferences = p.CurvePreferences
|
2018-05-01 23:45:06 +00:00
|
|
|
} else {
|
2018-11-15 15:43:50 +00:00
|
|
|
// Cloudflare optimize CurveP256
|
|
|
|
tlsconfig.CurvePreferences = []tls.CurveID{tls.CurveP256}
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 12:21:04 +00:00
|
|
|
tlsconfig.MinVersion = p.minVersion
|
|
|
|
tlsconfig.MaxVersion = p.maxVersion
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
return tlsconfig, nil
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
// LoadCert creates a CertPool containing all certificates in a PEM-format file.
|
|
|
|
func LoadCert(certPaths []string) (*x509.CertPool, error) {
|
|
|
|
ca := x509.NewCertPool()
|
|
|
|
for _, certPath := range certPaths {
|
|
|
|
caCert, err := ioutil.ReadFile(certPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "Error reading certificate %s", certPath)
|
|
|
|
}
|
|
|
|
if !ca.AppendCertsFromPEM(caCert) {
|
|
|
|
return nil, errors.Wrapf(err, "Error parsing certificate %s", certPath)
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2018-11-15 15:43:50 +00:00
|
|
|
return ca, nil
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|