Documentation
¶
Overview ¶
Package tlsca runs the built-in, name-constrained local certificate authority: CA create/load, leaf issue and renewal from the discovered SAN set, hot swap through tls.Config.GetCertificate, bring-your-own certificate reload, fingerprints and the iOS .mobileconfig profile.
Index ¶
- Constants
- Variables
- func Fingerprint(der []byte) string
- func LoadBYO(certPath, keyPath string) (*tls.Certificate, error)
- func LoadLeaf(dir string) (*tls.Certificate, error)
- func LoadLeafCert(dir string) (*x509.Certificate, error)
- func NormalizeHostname(h string) string
- func SaveLeaf(dir string, leaf *tls.Certificate) error
- func ValidDNSName(name string) error
- type CA
- type CAOptions
- type Manager
- type ManagerOptions
- type Mode
- type SANInput
- type SANs
- type Status
Constants ¶
const ( CACertFile = "ca.crt" CAKeyFile = "ca.key" LeafCertFile = "leaf.crt" LeafKeyFile = "leaf.key" )
File names inside the data directory's tls/ folder.
const CAValidity = 10 * 365 * 24 * time.Hour
CAValidity is the lifetime of a new CA certificate.
const LeafValidity = 397 * 24 * time.Hour
LeafValidity is the lifetime of an issued leaf certificate (browsers cap locally trusted server certificates near 398 days).
const MobileConfigContentType = "application/x-apple-aspen-config"
MobileConfigContentType is the MIME type iOS and macOS expect for profiles.
const RenewBefore = 30 * 24 * time.Hour
RenewBefore is how close to expiry a leaf gets re-issued.
Variables ¶
var BaseDNSDomains = []string{"local", "home.arpa", "lan", "internal", "localdomain", "localhost"}
BaseDNSDomains are the DNS name constraints every bubbles CA carries, before the OS host name and --tls-extra-dns values are added.
var ErrBYO = errors.New("the server uses --tls-cert/--tls-key; replace those files instead")
ErrBYO is returned by Manager.Reissue in bring-your-own mode.
var ErrNoCA = errors.New("no local CA in this data directory")
ErrNoCA is returned by LoadCA when the directory holds no CA certificate.
var PermittedIPRanges = []netip.Prefix{ netip.MustParsePrefix("10.0.0.0/8"), netip.MustParsePrefix("172.16.0.0/12"), netip.MustParsePrefix("192.168.0.0/16"), netip.MustParsePrefix("169.254.0.0/16"), netip.MustParsePrefix("100.64.0.0/10"), netip.MustParsePrefix("127.0.0.0/8"), netip.MustParsePrefix("fc00::/7"), netip.MustParsePrefix("fe80::/10"), netip.MustParsePrefix("::1/128"), }
PermittedIPRanges are the IP name constraints of every bubbles CA: the private, link-local, CGNAT and loopback ranges (see Locked Decision 2).
Functions ¶
func Fingerprint ¶
Fingerprint formats the SHA-256 of der as upper-case hex in groups of four, separated by spaces ("AB12 CD34 ...").
func LoadBYO ¶
func LoadBYO(certPath, keyPath string) (*tls.Certificate, error)
LoadBYO reads a bring-your-own certificate pair (used by `bubbles cert show`).
func LoadLeaf ¶
func LoadLeaf(dir string) (*tls.Certificate, error)
LoadLeaf reads leaf.crt and leaf.key from dir.
func LoadLeafCert ¶
func LoadLeafCert(dir string) (*x509.Certificate, error)
LoadLeafCert reads only leaf.crt from dir.
func NormalizeHostname ¶
NormalizeHostname lower-cases an OS host name and drops a trailing dot. It returns "" when the result is not a valid DNS name (for example a Windows name containing an underscore), so callers can skip it.
func SaveLeaf ¶
func SaveLeaf(dir string, leaf *tls.Certificate) error
SaveLeaf writes leaf.key, then leaf.crt into dir (0600).
func ValidDNSName ¶
ValidDNSName reports whether name is a lower-case DNS name made of dot-separated labels of 1-63 characters [a-z0-9-] that neither start nor end with a hyphen, at most 253 characters in total, and not an IP literal.
Types ¶
type CA ¶
type CA struct {
Cert *x509.Certificate
// contains filtered or unexported fields
}
CA is the local certificate authority.
func LoadCA ¶
LoadCA reads the CA from dir. It returns an error wrapping ErrNoCA when ca.crt does not exist, and refuses a certificate without its key.
func LoadOrCreateCA ¶
LoadOrCreateCA loads the CA from dir or, when there is none, creates one and saves it. created reports which happened.
func NewCA ¶
NewCA creates a CA: ECDSA P-256, 10 years, CA:TRUE pathlen 0, key usage certSign|crlSign and critical name constraints (BaseDNSDomains, the host name and the extra names; PermittedIPRanges).
func (*CA) Fingerprint ¶
Fingerprint returns the CA certificate's SHA-256 fingerprint (see Fingerprint).
func (*CA) Issue ¶
Issue signs a new serverAuth leaf for sans with a fresh ECDSA P-256 key. NotBefore is now - 1h. Names the CA's constraints do not permit must have been removed by the caller (see Filter).
func (*CA) MobileConfig ¶
MobileConfig returns an unsigned Apple configuration profile that installs the CA as a root certificate. Identifiers and UUIDs derive from the CA fingerprint, so downloading the profile twice replaces rather than duplicates it on the device.
type CAOptions ¶
type CAOptions struct {
// InstanceName appears in the CA common name; "" means "bubbles".
InstanceName string
// Hostname is the OS host name, added to the permitted DNS constraints.
Hostname string
// ExtraDNS are the --tls-extra-dns names, added to the constraints.
ExtraDNS []string
// Now is the clock; nil means time.Now.
Now func() time.Time
// Rand is the entropy source; nil means crypto/rand.
Rand io.Reader
}
CAOptions are the inputs to NewCA.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager owns the served certificate: it issues and renews the leaf from the local CA (or reloads a bring-your-own pair) and hands the current one to TLS handshakes through GetCertificate, so swaps need no restart.
func NewManager ¶
func NewManager(opts ManagerOptions) (*Manager, error)
NewManager loads (or creates) the CA and a valid leaf, or loads the bring-your-own pair. On return GetCertificate is ready.
func (*Manager) Check ¶
Check re-evaluates the served certificate. In local-CA mode it recomputes the SAN set and re-issues the leaf when the set changed or fewer than 30 days remain; in bring-your-own mode it reloads the files when either one changed (a broken pair keeps the old one in service). It reports whether the served certificate was replaced.
func (*Manager) Current ¶
func (m *Manager) Current() *tls.Certificate
Current returns the certificate being served.
func (*Manager) GetCertificate ¶
func (m *Manager) GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error)
GetCertificate implements tls.Config.GetCertificate.
type ManagerOptions ¶
type ManagerOptions struct {
// Dir is the data directory's tls/ folder (CA and leaf files).
Dir string
// CertFile and KeyFile select bring-your-own mode when both are set.
CertFile, KeyFile string
// CA configures the CA when Dir has none yet.
CA CAOptions
// SANs returns the desired leaf SAN set (see Discover). Required in
// local-CA mode.
SANs func() (SANs, error)
// Now is the clock; nil means time.Now.
Now func() time.Time
// Rand is the entropy source; nil means crypto/rand.
Rand io.Reader
// Logger receives issuance and reload notices; nil discards them.
Logger *slog.Logger
}
ManagerOptions configure a Manager.
type SANInput ¶
type SANInput struct {
// Addrs are the ranked private LAN addresses (netutil.Addrs).
Addrs []netip.Addr
// Hostname is the OS host name; invalid names are skipped.
Hostname string
// MDNSName is the single-label mDNS name, advertised as <name>.local.
MDNSName string
// Extra are the --tls-extra-dns names.
Extra []string
}
SANInput is what the SAN set is derived from.
type SANs ¶
SANs is the subject alternative name set of a leaf certificate.
func Discover ¶
Discover returns the SAN set for in: the LAN addresses in rank order, 127.0.0.1, ::1, localhost, <hostname>, <short hostname>.local, <mdns-name>.local and the extra names, de-duplicated.
func Filter ¶
func Filter(ca *x509.Certificate, sans SANs) (ok SANs, dropped []string)
Filter splits sans into the names the CA certificate's name constraints permit and the ones it would make invalid (for example after the host was renamed, or an extra DNS name was added after the CA was created).
type Status ¶
type Status struct {
Mode Mode `json:"mode"`
CAFingerprint string `json:"ca_fingerprint,omitempty"`
LeafFingerprint string `json:"leaf_fingerprint"`
LeafNotAfter time.Time `json:"leaf_not_after"`
SANs []string `json:"sans"`
}
Status describes the served certificate (admin UI and `bubbles cert show`).