Documentation
¶
Overview ¶
Package mtls is magpie's own certificate authority for mutual TLS (hhdaxia on X: "希望能支持 mtls,避免暴露端口到公网被 compromise"; #5596). With it on (settings' MTLS, `magpie mtls on`), the gateway and `magpie web`'s page serve other computers over HTTPS with a server certificate this CA signs (or the user's own, --tls-cert), and a connection from another computer is let in only with a client certificate this CA issued and nobody revoked. Client certificates are issued for a device by name (`magpie mtls issue <name>`, Settings → Network and sharing) and exported as PEM files beside the CA's own certificate, which the device trusts the server by: Remote magpie takes all three (provider.ClientCert, ClientKey and ServerCA), as curl does (--cert, --key, --cacert).
The CA's key never leaves this computer: it is kept in magpie's folder (0600) and is neither synced nor backed up, as the files here are no part of those.
Index ¶
- Variables
- func Export(iss Issued, dir string) (string, error)
- func LocalNames(h string) []string
- func LocalScopeIP(ca *x509.Certificate, ip net.IP) bool
- func LocalScopeName(ca *x509.Certificate, name string) bool
- func WriteFile(path string, b []byte, mode os.FileMode) error
- type Client
- type Issued
- type Store
- func (s Store) Admits(cert *x509.Certificate) error
- func (s Store) CA() (*x509.Certificate, crypto.Signer, error)
- func (s Store) CAFile() string
- func (s Store) HasCA() bool
- func (s Store) HasLocalCA() bool
- func (s Store) Issue(name string) (Issued, error)
- func (s Store) List() ([]Client, error)
- func (s Store) LoadLocalCA() (*x509.Certificate, crypto.Signer, error)
- func (s Store) LocalCA() (*x509.Certificate, crypto.Signer, error)
- func (s Store) LocalCAFile() string
- func (s Store) LocalServerCert(names []string, ips []net.IP) (tls.Certificate, error)
- func (s Store) Pool() (*x509.CertPool, error)
- func (s Store) RemoveLocalCA() error
- func (s Store) Revoke(name string) (int, error)
- func (s Store) ServerCert(names []string, ips []net.IP) (tls.Certificate, error)
Constants ¶
This section is empty.
Variables ¶
var LocalIPRanges = func() []*net.IPNet { var out []*net.IPNet for _, c := range []string{"127.0.0.0/8", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "169.254.0.0/16", "100.64.0.0/10", "::1/128", "fc00::/7", "fe80::/10"} { _, n, err := net.ParseCIDR(c) if err != nil { panic(err) } out = append(out, n) } return out }()
LocalIPRanges are the addresses the local CA may sign for: loopback, the private ranges (RFC 1918, IPv6 unique-local), link-local and the shared range Tailscale and CGNAT use — none of them reached on the internet.
Functions ¶
func Export ¶
Export writes what a device needs to dir/magpie-<name>: client.pem, client-key.pem (0600) and ca.pem, and returns that folder.
func LocalNames ¶
LocalNames are the DNS names the local CA may sign for, from this computer's hostname h: localhost (and, as a constraint, any x.localhost), h and h.local for a single-label h, or h itself when it ends in a name never delegated. A hostname that is a public suffix (a Mac named "dev" or "app") is left out: the constraint "dev" would cover every .dev site.
func LocalScopeIP ¶
func LocalScopeIP(ca *x509.Certificate, ip net.IP) bool
LocalScopeIP: the local CA's constraints let it sign for ip.
func LocalScopeName ¶
func LocalScopeName(ca *x509.Certificate, name string) bool
LocalScopeName: the local CA's constraints let it sign for the DNS name.
Types ¶
type Client ¶
type Client struct {
Name string `json:"name"`
Serial string `json:"serial"`
Issued time.Time `json:"issued"`
Expires time.Time `json:"expires"`
Revoked time.Time `json:"revoked,omitzero"`
}
Client is a client certificate issued: Name the device's, as the user gave it; Serial the certificate's, in hex; Revoked when it was taken back, zero while it is good.
type Issued ¶
Issued is a client certificate as it was issued, with its key: the only time the key is seen, as the store keeps none.
type Store ¶
type Store struct{ Dir string }
Store is the CA kept in Dir: ca.pem, ca-key.pem and clients.json, the certificates issued.
func (Store) Admits ¶
func (s Store) Admits(cert *x509.Certificate) error
Admits says whether cert, a client certificate the CA's pool verified, is let in: issued here and not revoked. One the list doesn't hold (its file lost, or edited) is refused, as is any when the list can't be read.
func (Store) HasLocalCA ¶
HasLocalCA: the local CA was made, and not removed.
func (Store) Issue ¶
Issue signs a client certificate for the device name. One issued before under the same name, still good, is revoked: the name is the device's, and a device issued again (a lost or new one) has the one certificate.
func (Store) LoadLocalCA ¶
LoadLocalCA is the local CA, nil (and no error) when none was made.
func (Store) LocalCAFile ¶
LocalCAFile is the local CA's certificate (NODE_EXTRA_CA_CERTS takes it).
func (Store) LocalServerCert ¶
LocalServerCert is a server certificate the local CA signs for names and ips, each of which its constraints must allow; made in memory.
func (Store) RemoveLocalCA ¶
RemoveLocalCA deletes the local CA and its key.
func (Store) Revoke ¶
Revoke takes back the good certificates of the device name, or the one whose serial name is. A connection already made with one ends with its next request: the gateway asks Admits on each handshake and each request.
func (Store) ServerCert ¶
ServerCert is a server certificate the CA signs for names and ips, with its key: made in memory each time the gateway starts serving, never written.