| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | package tls
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"crypto/tls"
 | 
					
						
							|  |  |  | 	"crypto/x509"
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2017-07-01 16:17:53 -04:00
										 |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"net/http"
 | 
					
						
							| 
									
										
										
										
											2021-10-13 15:30:31 +08:00
										 |  |  | 	"os"
 | 
					
						
							| 
									
										
										
										
											2022-02-14 08:24:21 -08:00
										 |  |  | 	"path/filepath"
 | 
					
						
							| 
									
										
										
										
											2017-07-01 16:17:53 -04:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 12:26:08 -08:00
										 |  |  | func setTLSDefaults(ctls *tls.Config) {
 | 
					
						
							|  |  |  | 	ctls.MinVersion = tls.VersionTLS12
 | 
					
						
							|  |  |  | 	ctls.MaxVersion = tls.VersionTLS13
 | 
					
						
							|  |  |  | 	ctls.CipherSuites = []uint16{
 | 
					
						
							|  |  |  | 		tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
 | 
					
						
							|  |  |  | 		tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
 | 
					
						
							|  |  |  | 		tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
 | 
					
						
							|  |  |  | 		tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
 | 
					
						
							|  |  |  | 		tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
 | 
					
						
							|  |  |  | 		tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
 | 
					
						
							|  |  |  | 		tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
 | 
					
						
							|  |  |  | 		tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
 | 
					
						
							|  |  |  | 		tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | // NewTLSConfigFromArgs returns a TLS config based upon the passed
 | 
					
						
							|  |  |  | // in list of arguments. Typically these come straight from the
 | 
					
						
							|  |  |  | // Corefile.
 | 
					
						
							|  |  |  | // no args
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | //   - creates a Config with no cert and using system CAs
 | 
					
						
							|  |  |  | //   - use for a client that talks to a server with a public signed cert (CA installed in system)
 | 
					
						
							|  |  |  | //   - the client will not be authenticated by the server since there is no cert
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | // one arg: the path to CA PEM file
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | //   - creates a Config with no cert using a specific CA
 | 
					
						
							|  |  |  | //   - use for a client that talks to a server with a private signed cert (CA not installed in system)
 | 
					
						
							|  |  |  | //   - the client will not be authenticated by the server since there is no cert
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | // two args: path to cert PEM file, the path to private key PEM file
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | //   - creates a Config with a cert, using system CAs to validate the other end
 | 
					
						
							|  |  |  | //   - use for:
 | 
					
						
							|  |  |  | //   - a server; or,
 | 
					
						
							|  |  |  | //   - a client that talks to a server with a public cert and needs certificate-based authentication
 | 
					
						
							|  |  |  | //   - the other end will authenticate this end via the provided cert
 | 
					
						
							|  |  |  | //   - the cert of the other end will be verified via system CAs
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | // three args: path to cert PEM file, path to client private key PEM file, path to CA PEM file
 | 
					
						
							| 
									
										
										
										
											2023-06-09 18:08:23 +02:00
										 |  |  | //   - creates a Config with the cert, using specified CA to validate the other end
 | 
					
						
							|  |  |  | //   - use for:
 | 
					
						
							|  |  |  | //   - a server; or,
 | 
					
						
							|  |  |  | //   - a client that talks to a server with a privately signed cert and needs certificate-based
 | 
					
						
							|  |  |  | //     authentication
 | 
					
						
							|  |  |  | //   - the other end will authenticate this end via the provided cert
 | 
					
						
							|  |  |  | //   - this end will verify the other end's cert using the specified CA
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | func NewTLSConfigFromArgs(args ...string) (*tls.Config, error) {
 | 
					
						
							|  |  |  | 	var err error
 | 
					
						
							|  |  |  | 	var c *tls.Config
 | 
					
						
							|  |  |  | 	switch len(args) {
 | 
					
						
							|  |  |  | 	case 0:
 | 
					
						
							|  |  |  | 		// No client cert, use system CA
 | 
					
						
							|  |  |  | 		c, err = NewTLSClientConfig("")
 | 
					
						
							|  |  |  | 	case 1:
 | 
					
						
							|  |  |  | 		// No client cert, use specified CA
 | 
					
						
							|  |  |  | 		c, err = NewTLSClientConfig(args[0])
 | 
					
						
							|  |  |  | 	case 2:
 | 
					
						
							|  |  |  | 		// Client cert, use system CA
 | 
					
						
							|  |  |  | 		c, err = NewTLSConfig(args[0], args[1], "")
 | 
					
						
							|  |  |  | 	case 3:
 | 
					
						
							|  |  |  | 		// Client cert, use specified CA
 | 
					
						
							|  |  |  | 		c, err = NewTLSConfig(args[0], args[1], args[2])
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 		err = fmt.Errorf("maximum of three arguments allowed for TLS config, found %d", len(args))
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return c, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewTLSConfig returns a TLS config that includes a certificate
 | 
					
						
							|  |  |  | // Use for server TLS config or when using a client certificate
 | 
					
						
							|  |  |  | // If caPath is empty, system CAs will be used
 | 
					
						
							|  |  |  | func NewTLSConfig(certPath, keyPath, caPath string) (*tls.Config, error) {
 | 
					
						
							|  |  |  | 	cert, err := tls.LoadX509KeyPair(certPath, keyPath)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 		return nil, fmt.Errorf("could not load TLS cert: %s", err)
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	roots, err := loadRoots(caPath)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 12:26:08 -08:00
										 |  |  | 	tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}, RootCAs: roots}
 | 
					
						
							|  |  |  | 	setTLSDefaults(tlsConfig)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return tlsConfig, nil
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewTLSClientConfig returns a TLS config for a client connection
 | 
					
						
							|  |  |  | // If caPath is empty, system CAs will be used
 | 
					
						
							|  |  |  | func NewTLSClientConfig(caPath string) (*tls.Config, error) {
 | 
					
						
							|  |  |  | 	roots, err := loadRoots(caPath)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 12:26:08 -08:00
										 |  |  | 	tlsConfig := &tls.Config{RootCAs: roots}
 | 
					
						
							|  |  |  | 	setTLSDefaults(tlsConfig)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return tlsConfig, nil
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func loadRoots(caPath string) (*x509.CertPool, error) {
 | 
					
						
							|  |  |  | 	if caPath == "" {
 | 
					
						
							|  |  |  | 		return nil, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	roots := x509.NewCertPool()
 | 
					
						
							| 
									
										
										
										
											2022-02-14 08:24:21 -08:00
										 |  |  | 	pem, err := os.ReadFile(filepath.Clean(caPath))
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 		return nil, fmt.Errorf("error reading %s: %s", caPath, err)
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	ok := roots.AppendCertsFromPEM(pem)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 		return nil, fmt.Errorf("could not read root certs: %s", err)
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:18:34 -05:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return roots, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-07-01 16:17:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:46:40 -07:00
										 |  |  | // NewHTTPSTransport returns an HTTP transport configured using tls.Config
 | 
					
						
							| 
									
										
										
										
											2017-07-01 16:17:53 -04:00
										 |  |  | func NewHTTPSTransport(cc *tls.Config) *http.Transport {
 | 
					
						
							|  |  |  | 	tr := &http.Transport{
 | 
					
						
							|  |  |  | 		Proxy: http.ProxyFromEnvironment,
 | 
					
						
							|  |  |  | 		Dial: (&net.Dialer{
 | 
					
						
							|  |  |  | 			Timeout:   30 * time.Second,
 | 
					
						
							|  |  |  | 			KeepAlive: 30 * time.Second,
 | 
					
						
							|  |  |  | 		}).Dial,
 | 
					
						
							|  |  |  | 		TLSHandshakeTimeout: 10 * time.Second,
 | 
					
						
							|  |  |  | 		TLSClientConfig:     cc,
 | 
					
						
							| 
									
										
										
										
											2017-08-08 14:27:33 +08:00
										 |  |  | 		MaxIdleConnsPerHost: 25,
 | 
					
						
							| 
									
										
										
										
											2017-07-01 16:17:53 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return tr
 | 
					
						
							|  |  |  | }
 |