tls

package
v0.68.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 39 Imported by: 1

Documentation

Overview

Package tls provides utilities for working with TLS certificates and configurations.

Index

Constants

View Source
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

func LoadCertPoolFromFS(fsys fs.FS, certPoolPath string) (*x509.CertPool, error)

LoadCertPoolFromFS loads a certificate pool from a PEM file in the provided filesystem.

func LoadCertPoolFromFile

func LoadCertPoolFromFile(certPoolPath string) (*x509.CertPool, error)

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

func NewACMETLSConfig(ctx context.Context, c ACMEConfig) (*tls.Config, error)

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

func NewLocalTLSConfig(ctx context.Context, config LocalConfig) (*tls.Config, error)

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

func NewServerTLSConfig(ctx context.Context, c ServerConfig) (*tls.Config, error)

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 `json:"type,omitempty" mapstructure:"type"`

	Email          string `json:"email,omitempty"           mapstructure:"email"`
	DirectoryURL   string `json:"directory_url,omitempty"   mapstructure:"directory-url"`
	CacheDirectory string `json:"cache_directory,omitempty" mapstructure:"cache-directory"`

	// 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 `json:"domains,omitempty" mapstructure:"domains"`

	// DNS01 holds fields that apply only when Type is ACMEChallengeDNS01.
	DNS01 DNS01Options `json:"dns01,omitzero" mapstructure:"dns01,squash"`
}

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 `json:"country,omitzero"             mapstructure:"c"`
	Organization       []string `json:"organization,omitzero"        mapstructure:"o"`
	OrganizationalUnit []string `json:"organizational_unit,omitzero" mapstructure:"ou"`
	Locality           []string `json:"locality,omitzero"            mapstructure:"l"`
	Province           []string `json:"province,omitzero"            mapstructure:"st"`
	StreetAddress      []string `json:"street_address,omitzero"      mapstructure:"street"`
	PostalCode         []string `json:"postal_code,omitzero"         mapstructure:"postalcode"`
	SerialNumber       string   `json:"serial_number,omitzero"       mapstructure:"serialnumber"`
	CommonName         string   `json:"common_name,omitzero"         mapstructure:"cn"`
}

CertificateSubject defines X.509 certificate subject information.

type ClientConfig

type ClientConfig struct {
	RootCAFile         string `json:"root_ca_file,omitempty"         mapstructure:"root-ca-file"`
	Certificate        string `json:"certificate,omitempty"          mapstructure:"cert"`
	Key                string `json:"key,omitempty"                  mapstructure:"key"`
	InsecureSkipVerify bool   `json:"insecure_skip_verify,omitempty" mapstructure:"insecure-skip-verify"`
}

ClientConfig specifies TLS client configuration.

type ConfigFunc added in v0.37.0

type ConfigFunc func() (*tls.Config, error)

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 `json:"propagation_timeout,omitempty" mapstructure:"dns01-propagation-timeout"`

	// PollingInterval controls how often lego polls while waiting for
	// propagation. Zero uses lego's default (2s).
	PollingInterval time.Duration `json:"polling_interval,omitempty" mapstructure:"dns01-polling-interval"`

	// Provider performs Present/CleanUp of the ACME dns-01 challenge TXT
	// record. Must be set programmatically; it is not config-decodable.
	Provider challenge.Provider `json:"-" mapstructure:"-"`
}

DNS01Options specifies dns-01-only ACME parameters.

type FileWaitConfig added in v0.37.0

type FileWaitConfig struct {
	WaitInterval uint `json:"wait_interval,omitzero" mapstructure:"file-wait-interval"`
	WaitMax      uint `json:"wait_max,omitzero"      mapstructure:"file-wait-max"`
}

FileWaitConfig specifies wait parameters for loading certificate files.

type LocalConfig added in v0.37.0

type LocalConfig struct {
	SinglePEMFile string         `json:"single_pem_file,omitzero" mapstructure:"single-pem-file"`
	Certificate   string         `json:"certificate,omitzero"     mapstructure:"cert"`
	Key           string         `json:"key,omitzero"             mapstructure:"key"`
	FileWait      FileWaitConfig `json:"file_wait,omitzero"       mapstructure:"file-wait,squash"`
}

LocalConfig specifies local certificate and key file locations.

type SANConfig added in v0.37.0

type SANConfig struct {
	DNSNames    []string `json:"dns_names,omitzero"    mapstructure:"dns-names"`
	IPAddresses []string `json:"ip_addresses,omitzero" mapstructure:"ip-addresses"`
}

SANConfig specifies Subject Alternative Names for a certificate (DNS names and IP addresses).

type SelfSignedConfig

type SelfSignedConfig struct {
	Subject        CertificateSubject `json:"subject"                  mapstructure:"subject"`
	SAN            SANConfig          `json:"san"                      mapstructure:"san"`
	Duration       string             `json:"duration,omitzero"        mapstructure:"duration"`
	IsCA           bool               `json:"is_ca,omitzero"           mapstructure:"ca"`
	Bits           int                `json:"bits,omitzero"            mapstructure:"bits"`
	CacheDirectory string             `json:"cache_directory,omitzero" mapstructure:"cache-directory"`
	Alias          string             `json:"alias,omitzero"           mapstructure:"alias"`
}

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 `json:"client_auth_type,omitzero" mapstructure:"client-auth-type"`
	ClientCAFile   string `json:"client_ca_file,omitzero"   mapstructure:"client-ca-file"`

	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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL