tlsca

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 28 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	CACertFile   = "ca.crt"
	CAKeyFile    = "ca.key"
	LeafCertFile = "leaf.crt"
	LeafKeyFile  = "leaf.key"
)

File names inside the data directory's tls/ folder.

View Source
const CAValidity = 10 * 365 * 24 * time.Hour

CAValidity is the lifetime of a new CA certificate.

View Source
const LeafValidity = 397 * 24 * time.Hour

LeafValidity is the lifetime of an issued leaf certificate (browsers cap locally trusted server certificates near 398 days).

View Source
const MobileConfigContentType = "application/x-apple-aspen-config"

MobileConfigContentType is the MIME type iOS and macOS expect for profiles.

View Source
const RenewBefore = 30 * 24 * time.Hour

RenewBefore is how close to expiry a leaf gets re-issued.

Variables

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

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

View Source
var ErrNoCA = errors.New("no local CA in this data directory")

ErrNoCA is returned by LoadCA when the directory holds no CA certificate.

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

func Fingerprint(der []byte) string

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

func NormalizeHostname(h string) string

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

func ValidDNSName(name string) error

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

func LoadCA(dir string) (*CA, error)

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

func LoadOrCreateCA(dir string, opts CAOptions) (ca *CA, created bool, err error)

LoadOrCreateCA loads the CA from dir or, when there is none, creates one and saves it. created reports which happened.

func NewCA

func NewCA(opts CAOptions) (*CA, error)

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) DER

func (ca *CA) DER() []byte

DER returns the CA certificate in DER form.

func (*CA) Fingerprint

func (ca *CA) Fingerprint() string

Fingerprint returns the CA certificate's SHA-256 fingerprint (see Fingerprint).

func (*CA) Issue

func (ca *CA) Issue(sans SANs, now time.Time, rnd io.Reader) (*tls.Certificate, error)

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

func (ca *CA) MobileConfig() []byte

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.

func (*CA) PEM

func (ca *CA) PEM() []byte

PEM returns the CA certificate in PEM form.

func (*CA) Save

func (ca *CA) Save(dir string) error

Save writes ca.key, then ca.crt (both 0600) into dir. A crash between the two leaves only a key behind, which LoadOrCreateCA replaces: the certificate was never served, so no device can trust it yet.

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) CA

func (m *Manager) CA() *CA

CA returns the local CA, or nil in bring-your-own mode.

func (*Manager) Check

func (m *Manager) Check() (bool, error)

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.

func (*Manager) Mode

func (m *Manager) Mode() Mode

Mode reports local-CA or bring-your-own mode.

func (*Manager) Reissue

func (m *Manager) Reissue() error

Reissue issues a new leaf now, whatever the state of the current one.

func (*Manager) Status

func (m *Manager) Status() Status

Status describes the CA and the served certificate.

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 Mode

type Mode string

Mode says where the served certificate comes from.

const (
	ModeLocalCA Mode = "local-ca"
	ModeBYO     Mode = "byo"
)

Certificate modes.

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

type SANs struct {
	DNS []string
	IPs []netip.Addr
}

SANs is the subject alternative name set of a leaf certificate.

func Discover

func Discover(in SANInput) SANs

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).

func SANsOf

func SANsOf(cert *x509.Certificate) SANs

SANsOf returns the SAN set of a certificate.

func (SANs) Equal

func (s SANs) Equal(o SANs) bool

Equal reports whether s and o contain the same names, ignoring order.

func (SANs) Strings

func (s SANs) Strings() []string

Strings returns the IP addresses followed by the DNS names, as text.

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`).

Jump to

Keyboard shortcuts

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