zhangly1 on X: Claude on another computer on the LAN needed magpie at https://. Settings → Network and sharing → HTTPS (magpie https on) serves other computers TLS on the gateway's own port, with a certificate magpie's CA (the mTLS one, in ~/.config/magpie/mtls) signs for this computer's names and addresses; this computer keeps plain http://. The switch is read per connection, so no restart. - Certificate row: Import… takes the user's own PEM certificate (with chain) and unencrypted key, in any files and order; a pair that can't be served (no key, no cert, a stranger key, a passphrase, run out, nothing PEM) is refused with the reason in the page's language and leaves nothing behind. Remove goes back to magpie's CA. --tls-cert still comes first. - magpie's CA row: SHA-256 shown, Save puts magpie-ca.crt in Downloads; the gateway serves it without a key at /magpie-ca.crt to any address the share lets in. Rows under it give curl -kO, macOS, Windows, Linux and NODE_EXTRA_CA_CERTS steps with Copy. - A page open from another computer can't turn HTTPS on or off under itself (it would lose its own address). - magpie https status|on|off|import|remove|ca --out; the serve banner names magpie's CA or the imported file instead of "(mTLS)". - docs/reference.md and gateway-routing.md say all of it, and that Windows' curl.exe needs --ssl-no-revoke for any private CA. Run: - gofmt -l clean; go vet linux -tags nogui ./internal/..., GOOS=windows and GOOS=darwin -tags nogui vet of the touched packages (WSL, mote refused ssh all evening). - WSL: go test -tags nogui gateway -run HTTPS|MTLS|ClientCert|CAFile|TLS|LAN ok; gui -run HTTPS|MTLS|Keep ok; settings, settingsapi, mtls ok; cli -run Help|HTTPS|TLS|Serve|Banner|Flag|Unknown ok. The whole cli package hit its 40m timeout in TestEveryAgentFieldNameWritesNothing (#5234, #4247, not this change). - GUI (WSL Playwright, chromium + webkit): https-settings (en, zh, 390px, strings in all four languages) ok, plus mtls-settings, allow-ips, browser-access, cors-origins, gui-de/ja/zh-tw, i18n-keys, i18n-en-no-cjk, lan-*, public-url*, api-key-theme, connect-address-kept, gateway-connect-keys, gateway-caller-keys, side-gateway-lan, menu-scroll. Reds at load ~130: api-key-theme (#5044), connect-address-kept (#5972), gateway-caller-keys (#2572), gui-ja (#5943), lan-docker (#4692), all red the same at base10bef5fe0run beside it; allow-ips and gateway-connect-keys went red once under that load and pass 18/18 at concurrency 1, base and this change side by side. - Real: a sandbox HOME gateway built from this tree on WSL (MAGPIE_ADDR=0.0.0.0:3593, a gateway key, magpie https on, magpie serve). From the Windows host: curl -k /magpie-ca.crt 200 without a key; Node fetch with NODE_EXTRA_CA_CERTS=magpie-ca.crt 200 on /v1/models, 401 without the key, SELF_SIGNED_CERT_IN_CHAIN without the CA; plain http:// 400; curl.exe --cacert needs --ssl-no-revoke (schannel), then 200. Cert SANs: localhost, the hostname, .local, 127.0.0.1, ::1 and every address. magpie https import of an openssl CA's leaf on the running gateway: Node trusting that CA 200, magpie's CA alone refused. remove + off on the running gateway: plain http:// from Windows 200. This computer's plain http:// 200 throughout. - At the pushed tree (on origin/mainee818e8bc), WSL: gofmt -l clean; go vet -tags nogui linux, GOOS=windows, GOOS=darwin nogui of gateway, gui, cli ok; go test -run 'HTTPS|CAFile|MTLS|LAN' gateway, gui ok. GUI https-settings, mtls-settings, i18n-keys, gui-de/ja/zh-tw, allow-ips, lan-* (both engines): 147/155; reds are gui-ja (#5943), lan-docker (#4692), allow-ips webkit under load, and gui-zh-tw chromium under load (2 of 3 green alone, filed #5983). Broken by hand: - TLSOn without HTTPSOn: TestHTTPSOtherComputer red. - lanGuard without the caFileOpen exemption: TestHTTPSCAFile red. - mtls config without GetCertificate = settingsCert: all three serving tests red. - app.js without renderHTTPS(s): https-settings red in both engines. Not tried: the macOS, Windows and Linux system trust commands on a real store (they change the box's trust); Claude Desktop itself; a darwin GUI run (mote unreachable); the full suite before the tag (started after it).
462 lines
14 KiB
Go
462 lines
14 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 is writeFile, for the certificate Settings imports.
|
|
func WriteFile(path string, b []byte, mode os.FileMode) error { return writeFile(path, b, mode) }
|
|
|
|
// 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)
|
|
}
|