1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.1 KiB
Go

// rtls is for creating TLS configurations for use by servers and clients.
2 years ago
package rtls
import (
2 years ago
"crypto/tls"
"crypto/x509"
"fmt"
"os"
)
const (
NoCACert = ""
NoServerName = ""
)
// MTLSState indicates whether mutual TLS is enabled or disabled.
type MTLSState tls.ClientAuthType
const (
MTLSStateDisabled MTLSState = MTLSState(tls.NoClientCert)
MTLSStateEnabled MTLSState = MTLSState(tls.RequireAndVerifyClientCert)
)
// CreateClientConfig creates a new tls.Config for use by a client. The certFile and keyFile
// parameters are the paths to the client's certificate and key files, which will be used to
// authenticate the client to the server if mutual TLS is active. The caCertFile parameter
// is the path to the CA certificate file, which the client will use to verify any certificate
// presented by the server. serverName can also be set, informing the client which hostname
// should appear in the returned certificate. If noverify is true, the client will not verify
// the server's certificate.
func CreateClientConfig(certFile, keyFile, caCertFile, serverName string, noverify bool) (*tls.Config, error) {
var err error
config := createBaseTLSConfig(serverName, noverify)
if certFile != "" && keyFile != "" {
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
}
if caCertFile != "" {
2 years ago
asn1Data, err := os.ReadFile(caCertFile)
if err != nil {
return nil, err
}
config.RootCAs = x509.NewCertPool()
ok := config.RootCAs.AppendCertsFromPEM(asn1Data)
if !ok {
return nil, fmt.Errorf("failed to load CA certificate(s) for server verification in %q", caCertFile)
}
}
return config, nil
}
// CreateServerConfig creates a new tls.Config for use by a server. The certFile and keyFile
// parameters are the paths to the server's certificate and key files, which will be used to
// authenticate the server to the client. The caCertFile parameter is the path to the CA
// certificate file, which the server will use to verify any certificate presented by the
// client. If mtls is MTLSStateEnabled, the server will require the client to present a
// valid certificate.
func CreateServerConfig(certFile, keyFile, caCertFile string, mtls MTLSState) (*tls.Config, error) {
var err error
config := createBaseTLSConfig(NoServerName, false)
2 years ago
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
if caCertFile != "" {
2 years ago
asn1Data, err := os.ReadFile(caCertFile)
if err != nil {
return nil, err
}
config.ClientCAs = x509.NewCertPool()
ok := config.ClientCAs.AppendCertsFromPEM(asn1Data)
if !ok {
return nil, fmt.Errorf("failed to load CA certificate(s) for client verification in %q", caCertFile)
}
}
config.ClientAuth = tls.ClientAuthType(mtls)
return config, nil
}
2 years ago
func createBaseTLSConfig(serverName string, noverify bool) *tls.Config {
2 years ago
return &tls.Config{
ServerName: serverName,
2 years ago
InsecureSkipVerify: noverify,
NextProtos: []string{"h2", "http/1.1"},
MinVersion: uint16(tls.VersionTLS12),
2 years ago
}
}