Documentation
¶
Overview ¶
Package tls provides utilities for working with TLS certificates and configurations.
Index ¶
- Constants
- func CertificateFromKeyAndCertificateFiles(ctx context.Context, key, cert string, waitConfig FileWaitConfig) ([]tls.Certificate, error)
- func CertificatesFromSinglePEMFile(ctx context.Context, singlePEMFile string, waitConfig FileWaitConfig) ([]tls.Certificate, error)
- func CreateAndSaveSelfSignedKeyPair(config SelfSignedConfig, certPath, keyPath string) (*tls.Certificate, *x509.CertPool, error)
- func CreateSelfSignedKeyPair(config SelfSignedConfig) (*tls.Certificate, *x509.CertPool, error)
- func LoadCertPoolFromFS(fsys fs.FS, certPoolPath string) (*x509.CertPool, error)
- func LoadCertPoolFromFile(certPoolPath string) (*x509.CertPool, error)
- func LoadKeyPairAndCertsFromFile(path string) (*tls.Certificate, error)
- func LoadKeyPairFromFiles(certPath, keyPath string) (*tls.Certificate, error)
- func LoadX509CertFromFile(certPath string) (*x509.Certificate, error)
- func NewACMETLSConfig(ctx context.Context, c ACMEConfig) (*tls.Config, error)
- func NewAutocertManagerFromConfig(c ACMEConfig) *autocert.Manager
- func NewClientTLSConfig(c ClientConfig) (*tls.Config, error)
- func NewLocalTLSConfig(ctx context.Context, config LocalConfig) (*tls.Config, error)
- func NewSelfSignedTLSConfig(config SelfSignedConfig) (*tls.Config, error)
- func NewServerTLSConfig(ctx context.Context, c ServerConfig) (*tls.Config, error)
- func SaveTLSCertificateToFile(cert *tls.Certificate, filename string, perm int) error
- func SaveTLSCertificateToFiles(cert *tls.Certificate, certPath, keyPath string) error
- type ACMEConfig
- type CertificateSubject
- type ClientConfig
- type ConfigFunc
- type DNS01Options
- type FileWaitConfig
- type LocalConfig
- type SANConfig
- type SelfSignedConfig
- type ServerConfig
Constants ¶
const ( ACMEChallengeTLSALPN01 = "tls-alpn-01" ACMEChallengeDNS01 = "dns-01" )
ACME challenge types accepted by ACMEConfig.Type.
Variables ¶
This section is empty.
Functions ¶
func CertificateFromKeyAndCertificateFiles ¶ added in v0.37.0
func CertificateFromKeyAndCertificateFiles(ctx context.Context, key, cert string, waitConfig FileWaitConfig) ([]tls.Certificate, error)
CertificateFromKeyAndCertificateFiles loads certificate and key from separate files.
func CertificatesFromSinglePEMFile ¶ added in v0.37.0
func CertificatesFromSinglePEMFile(ctx context.Context, singlePEMFile string, waitConfig FileWaitConfig) ([]tls.Certificate, error)
CertificatesFromSinglePEMFile loads certificate and key from a single PEM file.
func CreateAndSaveSelfSignedKeyPair ¶
func CreateAndSaveSelfSignedKeyPair(config SelfSignedConfig, certPath, keyPath string) (*tls.Certificate, *x509.CertPool, error)
CreateAndSaveSelfSignedKeyPair creates and saves a self-signed key pair to files.
func CreateSelfSignedKeyPair ¶
func CreateSelfSignedKeyPair(config SelfSignedConfig) (*tls.Certificate, *x509.CertPool, error)
CreateSelfSignedKeyPair creates a self-signed key pair in memory. pulled from inet.af/tcpproxy
func LoadCertPoolFromFS ¶ added in v0.55.1
LoadCertPoolFromFS loads a certificate pool from a PEM file in the provided filesystem.
func LoadCertPoolFromFile ¶
LoadCertPoolFromFile loads a certificate pool from a PEM file.
func LoadKeyPairAndCertsFromFile ¶
func LoadKeyPairAndCertsFromFile(path string) (*tls.Certificate, error)
LoadKeyPairAndCertsFromFile From: https://gist.github.com/ukautz/cd118e298bbd8f0a88fc LoadKeyPairAndCertsFromFile reads file, divides into key and certificates
func LoadKeyPairFromFiles ¶
func LoadKeyPairFromFiles(certPath, keyPath string) (*tls.Certificate, error)
LoadKeyPairFromFiles loads a TLS certificate and key pair from PEM files.
func LoadX509CertFromFile ¶
func LoadX509CertFromFile(certPath string) (*x509.Certificate, error)
LoadX509CertFromFile loads an X.509 certificate from a PEM file.
func NewACMETLSConfig ¶ added in v0.65.0
NewACMETLSConfig obtains a certificate via ACME using the challenge type selected by c.Type, dispatching to the tls-alpn-01 (autocert-backed) or dns-01 (lego-backed) implementation.
func NewAutocertManagerFromConfig ¶ added in v0.37.0
func NewAutocertManagerFromConfig(c ACMEConfig) *autocert.Manager
NewAutocertManagerFromConfig creates an ACME autocert manager from the given config.
func NewClientTLSConfig ¶ added in v0.37.0
func NewClientTLSConfig(c ClientConfig) (*tls.Config, error)
NewClientTLSConfig creates a TLS configuration for a client from the given config.
func NewLocalTLSConfig ¶ added in v0.37.0
NewLocalTLSConfig creates a TLS configuration from local certificate and key files.
func NewSelfSignedTLSConfig ¶ added in v0.37.0
func NewSelfSignedTLSConfig(config SelfSignedConfig) (*tls.Config, error)
NewSelfSignedTLSConfig creates a TLS configuration with a self-signed certificate.
func NewServerTLSConfig ¶ added in v0.37.0
NewServerTLSConfig creates a TLS configuration for a server from the given config.
func SaveTLSCertificateToFile ¶ added in v0.37.6
func SaveTLSCertificateToFile(cert *tls.Certificate, filename string, perm int) error
SaveTLSCertificateToFile saves a tls.Certificate to a file
func SaveTLSCertificateToFiles ¶
func SaveTLSCertificateToFiles(cert *tls.Certificate, certPath, keyPath string) error
SaveTLSCertificateToFiles saves a tls.Certificate to a certificate and key file
Types ¶
type ACMEConfig ¶ added in v0.65.0
type ACMEConfig struct {
// Type selects the ACME challenge mechanism: ACMEChallengeTLSALPN01 or
// ACMEChallengeDNS01. An absent/empty Type (the zero value) defaults to
// ACMEChallengeTLSALPN01, so callers who only need TLS-ALPN-01 can omit
// this field entirely.
Type string `mapstructure:"type" json:",omitempty"`
Email string `mapstructure:"email" json:",omitempty"`
DirectoryURL string `mapstructure:"directory-url" json:",omitempty"`
CacheDirectory string `mapstructure:"cache-directory" json:",omitempty"`
// Domains is, for Type=dns-01, the SANs to include on the single
// certificate obtained upfront (may include wildcards, e.g.
// "*.example.com"); for Type=tls-alpn-01 (default), the whitelist of
// hosts autocert will provision certificates for on demand via
// autocert.HostWhitelist. Wildcard entries are rejected for
// tls-alpn-01 - TLS-ALPN-01 cannot validate wildcard identifiers (RFC
// 8555 permits wildcards only via dns-01), and autocert's HostWhitelist
// silently ignores such entries rather than erroring, so
// NewACMETLSConfig validates this upfront instead.
Domains []string `mapstructure:"domains" json:",omitempty"`
// DNS01 holds fields that apply only when Type is ACMEChallengeDNS01.
DNS01 DNS01Options `mapstructure:"dns01,squash" json:",omitzero"`
}
ACMEConfig specifies parameters for obtaining a certificate via ACME, using either the tls-alpn-01 challenge (delegated to golang.org/x/crypto/acme/autocert) or the dns-01 challenge (a hand-rolled github.com/go-acme/lego/v5 client). The two challenge types share the same account identity and cache directory, but obtain certificates through entirely independent runtime implementations - only the config surface is unified here.
type CertificateSubject ¶ added in v0.37.0
type CertificateSubject struct {
Country []string `mapstructure:"c" json:"country,omitzero"`
Organization []string `mapstructure:"o" json:"organization,omitzero"`
OrganizationalUnit []string `mapstructure:"ou" json:"organizational_unit,omitzero"`
Locality []string `mapstructure:"l" json:"locality,omitzero"`
Province []string `mapstructure:"st" json:"province,omitzero"`
StreetAddress []string `mapstructure:"street" json:"street_address,omitzero"`
PostalCode []string `mapstructure:"postalcode" json:"postal_code,omitzero"`
SerialNumber string `mapstructure:"serialnumber" json:"serial_number,omitzero"`
CommonName string `mapstructure:"cn" json:"common_name,omitzero"`
}
CertificateSubject defines X.509 certificate subject information.
type ClientConfig ¶
type ClientConfig struct {
RootCAFile string `mapstructure:"root-ca-file" json:",omitempty"`
Certificate string `mapstructure:"cert" json:",omitempty"`
Key string `mapstructure:"key" json:",omitempty"`
InsecureSkipVerify bool `mapstructure:"insecure-skip-verify"`
}
ClientConfig specifies TLS client configuration.
type ConfigFunc ¶ added in v0.37.0
ConfigFunc is a function type that returns a TLS configuration.
func NewACMETLSConfigFunc ¶ added in v0.65.0
func NewACMETLSConfigFunc(ctx context.Context, c ACMEConfig) ConfigFunc
NewACMETLSConfigFunc creates a ConfigFunc for ACME certificate configuration. ctx bounds the lifetime of any background renewal goroutine started for the dns-01 challenge type, and is also passed to every ACME call made on this arm's behalf.
func NewLocalTLSConfigFunc ¶ added in v0.37.0
func NewLocalTLSConfigFunc(ctx context.Context, c LocalConfig) ConfigFunc
NewLocalTLSConfigFunc creates a ConfigFunc for loading certificates from local files.
func NewSelfSignedTLSConfigFunc ¶ added in v0.37.0
func NewSelfSignedTLSConfigFunc(c SelfSignedConfig) ConfigFunc
NewSelfSignedTLSConfigFunc creates a ConfigFunc for self-signed certificate configuration.
type DNS01Options ¶ added in v0.65.0
type DNS01Options struct {
// PropagationTimeout bounds how long lego waits for DNS propagation of
// the challenge TXT record. Zero uses lego's default (60s).
PropagationTimeout time.Duration `mapstructure:"dns01-propagation-timeout" json:",omitempty"`
// PollingInterval controls how often lego polls while waiting for
// propagation. Zero uses lego's default (2s).
PollingInterval time.Duration `mapstructure:"dns01-polling-interval" json:",omitempty"`
// Provider performs Present/CleanUp of the ACME dns-01 challenge TXT
// record. Must be set programmatically; it is not config-decodable.
Provider challenge.Provider `mapstructure:"-" json:"-"`
}
DNS01Options specifies dns-01-only ACME parameters.
type FileWaitConfig ¶ added in v0.37.0
type FileWaitConfig struct {
WaitInterval uint `mapstructure:"file-wait-interval" json:",omitzero"`
WaitMax uint `mapstructure:"file-wait-max" json:",omitzero"`
}
FileWaitConfig specifies wait parameters for loading certificate files.
type LocalConfig ¶ added in v0.37.0
type LocalConfig struct {
SinglePEMFile string `mapstructure:"single-pem-file" json:",omitzero"`
Certificate string `mapstructure:"cert" json:",omitzero"`
Key string `mapstructure:"key" json:",omitzero"`
FileWait FileWaitConfig `mapstructure:"file-wait,squash" json:",omitzero"`
}
LocalConfig specifies local certificate and key file locations.
type SANConfig ¶ added in v0.37.0
type SANConfig struct {
DNSNames []string `mapstructure:"dns-names" json:"dns_names,omitzero"`
IPAddresses []string `mapstructure:"ip-addresses" json:"ip_addresses,omitzero"`
}
SANConfig specifies Subject Alternative Names for a certificate (DNS names and IP addresses).
type SelfSignedConfig ¶
type SelfSignedConfig struct {
Subject CertificateSubject `mapstructure:"subject" json:"subject"`
SAN SANConfig `mapstructure:"san" json:"san"`
Duration string `mapstructure:"duration" json:"duration,omitzero"`
IsCA bool `mapstructure:"ca" json:"is_ca,omitzero"`
Bits int `mapstructure:"bits" json:"bits,omitzero"`
CacheDirectory string `mapstructure:"cache-directory" json:"cache_directory,omitzero"`
Alias string `mapstructure:"alias" json:"alias,omitzero"`
}
SelfSignedConfig specifies parameters for generating a self-signed certificate.
type ServerConfig ¶
type ServerConfig struct {
ServerName string `json:"server_name,omitzero" mapstructure:"server-name"`
ACME ACMEConfig `json:"acme" mapstructure:"acme"`
SelfSigned SelfSignedConfig `json:"self_signed" mapstructure:"self-signed"`
LocalConfig LocalConfig `json:"local" mapstructure:"local"`
ClientAuthType string `mapstructure:"client-auth-type" json:"client_auth_type,omitzero"`
ClientCAFile string `mapstructure:"client-ca-file" json:"client_ca_file,omitzero"`
NextProtos []string `json:"next_protos,omitzero" mapstructure:"next-protos"`
TLSMinVersion string `json:"tls_min_version,omitzero" mapstructure:"tls-min-version"`
}
ServerConfig specifies TLS configuration for a server.