hhdaxia on X asked for mTLS so a shared magpie isn't open to the internet on a key alone; clxsh's #5596 asks Remote Magpie to send a client certificate and key (PEM) to an Nginx doing mTLS, the server still verified, the Gateway Key still sent, for models, the test and streaming. Server side (magpie's own CA, internal/mtls): - Settings -> Network and sharing -> Require a client certificate, or `magpie mtls on|off|issue <device>|list|revoke`. Issue saves client.pem, client-key.pem and ca.pem to ~/Downloads/magpie-<device>; the key is kept nowhere else. - With it on, a peer that isn't this computer or a WSL distro must finish the TLS handshake with a certificate the CA issued and hasn't revoked (RequireAndVerifyClientCert + VerifyConnection -> Admits). The TLS config is chosen per connection, so the switch needs no relisten; without --tls-cert the server certificate is made by the CA in memory. - lanGuard and ClientCertGuard (magpie web's page) refuse 403 what still arrives without a verified one: plain HTTP, a proxy or tunnel on loopback (unless MAGPIE_TRUST_PROXY=1), the page port, a connection from before the switch, a certificate revoked since its handshake. - Loopback, this computer's own address and a WSL agent with its key are answered as before. - A page open from another computer can't shut itself out: without a certificate it can't turn mTLS on, and it can't revoke or reissue the certificate it came with. Client side: a provider with a base URL of the user's own (Remote Magpie included) takes ClientCert, ClientKey and ServerCA paths (editor: Advanced -> Client certificate). netproxy.WithClientCert shows them to that provider's own hosts only, on a transport per identity keyed on the files' mtimes; through an https:// proxy such a request is refused. Save refuses half a pair, a pair that doesn't match and a CA that isn't one. Run (on mote, at 4024df0f9, this commit before its rebase): - gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows. - go test -count=1 ./internal/mtls ./internal/netproxy ./internal/provider ./internal/settings ./internal/settingsapi ./internal/cli ./internal/gui ./internal/gateway: ok (gateway 893s). The first run had three reds of this change, each green at base and fixed here: TestEveryRouteHasACommandOrIsNamed (the mtls routes now name `magpie mtls`), TestSettingsSaveKeepsWhatItDoesNotSend (MTLS kept), and TestGoroutineDumpAnswersWhileRequestsWait (mtlsRefuses asks whether the peer is remote before reading settings, so a local request never waits on the settings lock). TestHandedOffGatewayAnswersAConnectionNotedLate was reset because ServeTLS now always wraps the listener: tlsListener hands a connection it took as Close came to the server instead of dropping it. -count=3 of these and every TLS/MTLS/Handover/Relisten test ok. - Every route without a certificate, with a foreign one, a revoked one and a good one: TestMTLSOtherComputerHandshake, TestMTLSGuardEveryRoute, TestMTLSThisComputerAndProxies, TestMTLSToggleWithoutRelisten, TestMTLSRevokeMidConnection, TestClientCertGuardPage. Remote Magpie against an httptest server asking RequireAndVerifyClientCert: models and the connection test fail without the certificate and pass with it (TestRemoteMagpieClientCert). - GUI: the 92 files near the change (settings-*, provider-*, editor-*, lan-*, i18n*, allow-ips, remote-*, mtls-settings new) in Chromium and WebKit: 2904 pass, 74 fail, all in nine files red the same way at base0791c91c5and listed in #5872 (editor-thumbs-batched also #5886; provider-fetch-again #5057 #5232; provider-many-keys, -models-rest, -picks-stay, -typed #5475 #4396 #5233 #4673; remote-magpie #4398; settings-hints, settings-lines-wrap run alone at base and here: the same 8 reds). The whole GUI suite and the whole Go suite were not run: mote's suite lock had about 15 runs queued at load 31-40. Rebased on031938ca9(one conflict, internal/gui/api.go's route list, both kept) and run again there: gofmt clean, vet on three systems, go test ./internal/{gui,cli,mtls,netproxy,provider,settings,settingsapi} ok (cli with the next commit's line), the gateway's TLS, MTLS, Handover, Relisten, HandedOff, GoroutineDump, ClientCert and RemoteMagpie tests ok, TestCoreScriptBudget ok, and mtls-settings, i18n-keys, allow-ips and provider-insecure-tls in Chromium and WebKit: 143 pass, 0 fail. Broken by hand, each red: - lanGuard without refuseWithoutClientCert: TestMTLSGuardEveryRoute, TestMTLSThisComputerAndProxies, TestMTLSRevokeMidConnection. - VerifyClientCertIfGiven and no Admits: TestMTLSOtherComputerHandshake. - netproxy.With keeping another provider's certificate: TestClientCertOnlyItsHost. - The page-lockout and own-serial guards in internal/gui/mtls.go: TestSettingsMTLSPageLockout. Not tried: a real Nginx doing mTLS; a Windows or Linux run (vet only); installing the certificate in a browser (there is no .p12 yet); a proxy that checks certificates itself with MAGPIE_TRUST_PROXY=1.
459 lines
13 KiB
Go
459 lines
13 KiB
Go
// 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.
|
|
package mtls
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/json"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// Store is the CA kept in Dir: ca.pem, ca-key.pem and clients.json, the
|
|
// certificates issued.
|
|
type Store struct{ Dir string }
|
|
|
|
// 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 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"`
|
|
}
|
|
|
|
// Good: issued, not revoked and not run out at now.
|
|
func (c Client) Good(now time.Time) bool { return c.Revoked.IsZero() && now.Before(c.Expires) }
|
|
|
|
// Validity of what the CA signs: the CA ten years, a client certificate
|
|
// two, a server certificate (made again at each start) one.
|
|
const (
|
|
caLife = 10 * 365 * 24 * time.Hour
|
|
clientLife = 2 * 365 * 24 * time.Hour
|
|
serverLife = 365 * 24 * time.Hour
|
|
)
|
|
|
|
var mu sync.Mutex // one change to a store's files at a time, in this process
|
|
|
|
func (s Store) caFile() string { return filepath.Join(s.Dir, "ca.pem") }
|
|
func (s Store) caKeyFile() string { return filepath.Join(s.Dir, "ca-key.pem") }
|
|
func (s Store) clientsFile() string { return filepath.Join(s.Dir, "clients.json") }
|
|
|
|
// CAFile is the CA's certificate, the file a device trusts the server by.
|
|
func (s Store) CAFile() string { return s.caFile() }
|
|
|
|
// HasCA: the CA was made.
|
|
func (s Store) HasCA() bool {
|
|
_, err := os.Stat(s.caKeyFile())
|
|
return err == nil
|
|
}
|
|
|
|
// CA is the store's CA, made on first use.
|
|
func (s Store) CA() (*x509.Certificate, crypto.Signer, error) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
return s.ca()
|
|
}
|
|
|
|
func (s Store) ca() (*x509.Certificate, crypto.Signer, error) {
|
|
cb, cerr := os.ReadFile(s.caFile())
|
|
kb, kerr := os.ReadFile(s.caKeyFile())
|
|
if cerr == nil && kerr == nil {
|
|
return parsePair(cb, kb)
|
|
}
|
|
if !errors.Is(cerr, os.ErrNotExist) && cerr != nil {
|
|
return nil, nil, cerr
|
|
}
|
|
if !errors.Is(kerr, os.ErrNotExist) && kerr != nil {
|
|
return nil, nil, kerr
|
|
}
|
|
if cerr == nil || kerr == nil {
|
|
// one of the two alone: never made over, which would orphan every
|
|
// certificate the CA issued
|
|
return nil, nil, fmt.Errorf("magpie's mTLS CA in %s is missing its certificate or its key", s.Dir)
|
|
}
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
host, _ := os.Hostname()
|
|
now := time.Now()
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: serial(),
|
|
Subject: pkix.Name{CommonName: strings.TrimSpace("magpie CA " + host), Organization: []string{"magpie"}},
|
|
NotBefore: now.Add(-time.Hour),
|
|
NotAfter: now.Add(caLife),
|
|
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature,
|
|
BasicConstraintsValid: true,
|
|
IsCA: true,
|
|
MaxPathLenZero: true,
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
kder, err := x509.MarshalPKCS8PrivateKey(key)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if err := os.MkdirAll(s.Dir, 0o700); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if err := writeFile(s.caKeyFile(), pemOf("PRIVATE KEY", kder), 0o600); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if err := writeFile(s.caFile(), pemOf("CERTIFICATE", der), 0o644); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
cert, err := x509.ParseCertificate(der)
|
|
return cert, key, err
|
|
}
|
|
|
|
func parsePair(cb, kb []byte) (*x509.Certificate, crypto.Signer, error) {
|
|
p, _ := pem.Decode(cb)
|
|
if p == nil {
|
|
return nil, nil, errors.New("magpie's mTLS CA certificate isn't PEM")
|
|
}
|
|
cert, err := x509.ParseCertificate(p.Bytes)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
k, _ := pem.Decode(kb)
|
|
if k == nil {
|
|
return nil, nil, errors.New("magpie's mTLS CA key isn't PEM")
|
|
}
|
|
key, err := x509.ParsePKCS8PrivateKey(k.Bytes)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
signer, ok := key.(crypto.Signer)
|
|
if !ok {
|
|
return nil, nil, errors.New("magpie's mTLS CA key can't sign")
|
|
}
|
|
return cert, signer, nil
|
|
}
|
|
|
|
// Pool is the CA as a pool a server verifies client certificates by.
|
|
func (s Store) Pool() (*x509.CertPool, error) {
|
|
ca, _, err := s.CA()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pool := x509.NewCertPool()
|
|
pool.AddCert(ca)
|
|
return pool, nil
|
|
}
|
|
|
|
// 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 Issued struct {
|
|
Client
|
|
Cert, Key, CA []byte // PEM
|
|
}
|
|
|
|
// 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 (s Store) Issue(name string) (Issued, error) {
|
|
name = strings.TrimSpace(name)
|
|
if name == "" {
|
|
return Issued{}, errors.New("a client certificate needs a name, the device's: magpie mtls issue <name>")
|
|
}
|
|
if strings.ContainsAny(name, "/\\:*?\"<>|") || strings.ContainsFunc(name, func(r rune) bool { return r < ' ' }) {
|
|
return Issued{}, fmt.Errorf("%q: a name can't hold / \\ : * ? \" < > | or control characters", name)
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
ca, caKey, err := s.ca()
|
|
if err != nil {
|
|
return Issued{}, err
|
|
}
|
|
clients, err := s.clients()
|
|
if err != nil {
|
|
return Issued{}, err
|
|
}
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
return Issued{}, err
|
|
}
|
|
now := time.Now()
|
|
sn := serial()
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: sn,
|
|
Subject: pkix.Name{CommonName: name, Organization: []string{"magpie"}},
|
|
NotBefore: now.Add(-time.Hour),
|
|
NotAfter: now.Add(clientLife),
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, ca, &key.PublicKey, caKey)
|
|
if err != nil {
|
|
return Issued{}, err
|
|
}
|
|
kder, err := x509.MarshalPKCS8PrivateKey(key)
|
|
if err != nil {
|
|
return Issued{}, err
|
|
}
|
|
for i := range clients {
|
|
if strings.EqualFold(clients[i].Name, name) && clients[i].Revoked.IsZero() {
|
|
clients[i].Revoked = now
|
|
}
|
|
}
|
|
c := Client{Name: name, Serial: sn.Text(16), Issued: now, Expires: tmpl.NotAfter}
|
|
clients = append(clients, c)
|
|
if err := s.saveClients(clients); err != nil {
|
|
return Issued{}, err
|
|
}
|
|
return Issued{Client: c, Cert: pemOf("CERTIFICATE", der), Key: pemOf("PRIVATE KEY", kder), CA: pemOf("CERTIFICATE", ca.Raw)}, nil
|
|
}
|
|
|
|
// List is every certificate issued, the newest first.
|
|
func (s Store) List() ([]Client, error) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
c, err := s.clients()
|
|
slices.SortStableFunc(c, func(a, b Client) int { return b.Issued.Compare(a.Issued) })
|
|
return c, err
|
|
}
|
|
|
|
// 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 (s Store) Revoke(name string) (int, error) {
|
|
name = strings.TrimSpace(name)
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
clients, err := s.clients()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n := 0
|
|
for i := range clients {
|
|
if (strings.EqualFold(clients[i].Name, name) || strings.EqualFold(clients[i].Serial, name)) && clients[i].Revoked.IsZero() {
|
|
clients[i].Revoked = time.Now()
|
|
n++
|
|
}
|
|
}
|
|
if n == 0 {
|
|
return 0, fmt.Errorf("no certificate issued to %q is in use: magpie mtls list", name)
|
|
}
|
|
return n, s.saveClients(clients)
|
|
}
|
|
|
|
// 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 (s Store) Admits(cert *x509.Certificate) error {
|
|
clients, err := s.cachedClients()
|
|
if err != nil {
|
|
return fmt.Errorf("the client certificates issued can't be read: %w", err)
|
|
}
|
|
sn := cert.SerialNumber.Text(16)
|
|
for _, c := range clients {
|
|
if c.Serial == sn {
|
|
if !c.Revoked.IsZero() {
|
|
return fmt.Errorf("the client certificate %q was revoked", c.Name)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
return fmt.Errorf("the client certificate %q isn't one this magpie issued", cert.Subject.CommonName)
|
|
}
|
|
|
|
var cache struct {
|
|
sync.Mutex
|
|
gen int64 // saves in this process, so one within the file's mtime tick is seen
|
|
path string
|
|
mod time.Time
|
|
size int64
|
|
clients []Client
|
|
}
|
|
|
|
// cachedClients is clients.json, parsed again only when it changed: it is
|
|
// asked on every handshake.
|
|
func (s Store) cachedClients() ([]Client, error) {
|
|
fi, err := os.Stat(s.clientsFile())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cache.Lock()
|
|
defer cache.Unlock()
|
|
if cache.path == s.clientsFile() && cache.mod.Equal(fi.ModTime()) && cache.size == fi.Size() && cache.gen == saves.Load() {
|
|
return cache.clients, nil
|
|
}
|
|
gen := saves.Load()
|
|
mu.Lock()
|
|
c, err := s.clients()
|
|
mu.Unlock()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cache.path, cache.mod, cache.size, cache.clients, cache.gen = s.clientsFile(), fi.ModTime(), fi.Size(), c, gen
|
|
return c, nil
|
|
}
|
|
|
|
func (s Store) clients() ([]Client, error) {
|
|
b, err := os.ReadFile(s.clientsFile())
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var out []Client
|
|
if err := json.Unmarshal(b, &out); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", s.clientsFile(), err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s Store) saveClients(c []Client) error {
|
|
b, err := json.MarshalIndent(c, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer saves.Add(1)
|
|
return writeFile(s.clientsFile(), append(b, '\n'), 0o600)
|
|
}
|
|
|
|
var saves atomic.Int64
|
|
|
|
// 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.
|
|
func (s Store) ServerCert(names []string, ips []net.IP) (tls.Certificate, error) {
|
|
ca, caKey, err := s.CA()
|
|
if err != nil {
|
|
return tls.Certificate{}, err
|
|
}
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
return tls.Certificate{}, err
|
|
}
|
|
now := time.Now()
|
|
cn := "magpie"
|
|
if len(names) > 0 {
|
|
cn = names[0]
|
|
}
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: serial(),
|
|
Subject: pkix.Name{CommonName: cn, Organization: []string{"magpie"}},
|
|
NotBefore: now.Add(-time.Hour),
|
|
NotAfter: now.Add(serverLife),
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
DNSNames: names,
|
|
IPAddresses: ips,
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, ca, &key.PublicKey, caKey)
|
|
if err != nil {
|
|
return tls.Certificate{}, err
|
|
}
|
|
leaf, err := x509.ParseCertificate(der)
|
|
if err != nil {
|
|
return tls.Certificate{}, err
|
|
}
|
|
return tls.Certificate{Certificate: [][]byte{der, ca.Raw}, PrivateKey: key, Leaf: leaf}, nil
|
|
}
|
|
|
|
// Export writes what a device needs to dir/magpie-<name>: client.pem,
|
|
// client-key.pem (0600) and ca.pem, and returns that folder.
|
|
func Export(iss Issued, dir string) (string, error) {
|
|
out := filepath.Join(dir, "magpie-"+strings.ReplaceAll(iss.Name, " ", "-"))
|
|
if _, err := os.Stat(out); err == nil {
|
|
for i := 2; ; i++ {
|
|
o := fmt.Sprintf("%s-%d", out, i)
|
|
if _, err := os.Stat(o); errors.Is(err, os.ErrNotExist) {
|
|
out = o
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if err := os.MkdirAll(out, 0o700); err != nil {
|
|
return "", err
|
|
}
|
|
for _, f := range []struct {
|
|
name string
|
|
b []byte
|
|
mode os.FileMode
|
|
}{{"client.pem", iss.Cert, 0o644}, {"client-key.pem", iss.Key, 0o600}, {"ca.pem", iss.CA, 0o644}} {
|
|
if err := writeFile(filepath.Join(out, f.name), f.b, f.mode); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func serial() *big.Int {
|
|
n, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 127))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return n.Add(n, big.NewInt(1))
|
|
}
|
|
|
|
func pemOf(typ string, der []byte) []byte {
|
|
return pem.EncodeToMemory(&pem.Block{Type: typ, Bytes: der})
|
|
}
|
|
|
|
// writeFile writes b to path through a temp file renamed over it, so a
|
|
// reader never sees half of one.
|
|
func writeFile(path string, b []byte, mode os.FileMode) error {
|
|
tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(tmp.Name())
|
|
if err := tmp.Chmod(mode); err != nil {
|
|
tmp.Close()
|
|
return err
|
|
}
|
|
if _, err := tmp.Write(b); err != nil {
|
|
tmp.Close()
|
|
return err
|
|
}
|
|
if err := tmp.Sync(); err != nil {
|
|
tmp.Close()
|
|
return err
|
|
}
|
|
if err := tmp.Close(); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmp.Name(), path)
|
|
}
|