Documentation
¶
Overview ¶
Package transport provides net.Listener wrappers for servers: a TLS listener that performs the handshake eagerly with optional CRL checking, and keepalive listeners that enable TCP keepalive on accepted connections.
TLSInfo holds the file-based TLS settings for a server and builds a tls.Config whose certificate is rotated by a tlsconfig.KeypairReloader (polling every 5 minutes). Only CertFile, KeyFile, TrustedCAFile, ClientCAFile, ClientAuthType, CipherSuites, CRLVerifier and HandshakeFailure are used by this package; the remaining fields are retained for configuration compatibility and are not enforced.
Usage:
ln, err := net.Listen("tcp", ":8443")
if err != nil {
return err
}
info := &transport.TLSInfo{
CertFile: "server.pem",
KeyFile: "server-key.pem",
TrustedCAFile: "ca.pem",
ClientAuthType: tls.VerifyClientCertIfGiven,
CRLVerifier: verifier, // optional crlcache.Verifier
}
defer info.Close()
tlsLn, err := transport.NewTLSListener(ln, info) // handshakes in the background
if err != nil {
return err
}
srv := &http.Server{Handler: h, TLSConfig: info.Config()}
return srv.Serve(tlsLn)
NewKeepAliveListener wraps a listener so accepted TCP connections get a 30s keepalive; with scheme "https" it also wraps connections with tls.Server (lazy handshake) using the supplied tls.Config.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewKeepAliveListener ¶
NewKeepAliveListener wraps l so that accepted connections get TCP keepalive enabled with a 30s period. With scheme "https" the connection is also wrapped with tls.Server(tlscfg) (handshake happens lazily on first I/O) and tlscfg must be non-nil, otherwise an error is returned. Accepted connections must implement SetKeepAlive/SetKeepAlivePeriod (e.g. *net.TCPConn) or Accept panics. Be careful when wrapping the returned listener with another listener: packages like net/http expect Accept to return *tls.Conn. See http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
func NewTLSListener ¶
NewTLSListener wraps l so that every accepted connection is TLS-handshaked in its own goroutine before Accept returns it; connections that fail the handshake or the CRL check (when tlsinfo.CRLVerifier is set) are closed and reported to tlsinfo.HandshakeFailure. It calls tlsinfo.ServerTLSWithReloader, so the tls.Config is available afterwards via tlsinfo.Config(). If tlsinfo is nil or Empty, l is closed and an error returned. The caller must Close the returned listener; Close blocks until the accept loop and pending handshakes finish. Handshakes have no deadline.
Types ¶
type TLSInfo ¶
type TLSInfo struct {
// CertFile and KeyFile are the PEM server certificate chain and key; required.
CertFile string
KeyFile string
// ClientCAFile is a PEM bundle used to verify client certificates.
// When empty, TrustedCAFile is used for that purpose as well.
ClientCAFile string
// TrustedCAFile is a PEM bundle used as RootCAs (and ClientCAs when
// ClientCAFile is empty). When empty, OS roots are used.
TrustedCAFile string
// ClientAuthType is passed to tls.Config.ClientAuth (default NoClientCert).
ClientAuthType tls.ClientAuthType
// CRLVerifier, when set, is consulted by NewTLSListener for every
// certificate in the client's verified chains; a Revoked status rejects
// the connection, errors and Unknown are logged and allowed.
CRLVerifier crlcache.Verifier
// InsecureSkipVerify is not used by this package.
InsecureSkipVerify bool
// SkipClientSANVerify is not used by this package.
SkipClientSANVerify bool
// ServerName ensures the cert matches the given host in case of discovery / virtual hosting.
// It is not used by this package.
ServerName string
// HandshakeFailure is optionally called when a connection fails to handshake. The
// connection will be closed immediately afterwards.
HandshakeFailure func(*tls.Conn, error)
// CipherSuites is a list of supported cipher suites.
// If empty, Go auto-populates it by default.
// Note that cipher suites are prioritized in the given order.
CipherSuites []string
// AllowedCN is a CN which must be provided by a client.
// It is not enforced by this package.
AllowedCN string
// AllowedHostname is an IP address or hostname that must match the TLS
// certificate provided by a client. It is not enforced by this package.
AllowedHostname string
// EmptyCN indicates that the cert must have empty CN.
// It is not enforced by this package.
EmptyCN bool
// contains filtered or unexported fields
}
TLSInfo is the file-based TLS configuration of a server listener. ServerTLSWithReloader builds and caches the tls.Config; Close releases the reloader. It is not safe for concurrent use while being initialized.
Fields that are read by this package: CertFile, KeyFile, TrustedCAFile, ClientCAFile, ClientAuthType, CipherSuites, CRLVerifier, HandshakeFailure. InsecureSkipVerify, SkipClientSANVerify, ServerName, AllowedCN, AllowedHostname and EmptyCN are currently not enforced.
func (*TLSInfo) Close ¶
func (info *TLSInfo) Close()
Close stops the certificate reloader and drops the cached tls.Config. It is safe to call when ServerTLSWithReloader was never called.
func (*TLSInfo) Config ¶
Config returns the tls.Config built by ServerTLSWithReloader, or nil if it has not been called (or after Close).
func (*TLSInfo) Empty ¶
Empty reports whether CertFile or KeyFile is missing, i.e. TLS cannot be served.
func (*TLSInfo) ServerTLSWithReloader ¶
ServerTLSWithReloader builds (once) and returns the server tls.Config from the files, applies CipherSuites, and installs a KeypairReloader polling every 5 minutes as GetCertificate. It returns an error if the certificate has already expired. Subsequent calls return the cached config.