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.
214 lines
5.8 KiB
Go
214 lines
5.8 KiB
Go
package mtls
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func leafOf(t *testing.T, b []byte) *x509.Certificate {
|
|
t.Helper()
|
|
p, _ := pem.Decode(b)
|
|
if p == nil {
|
|
t.Fatal("not PEM")
|
|
}
|
|
c, err := x509.ParseCertificate(p.Bytes)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return c
|
|
}
|
|
|
|
func verifies(t *testing.T, st Store, c *x509.Certificate) error {
|
|
t.Helper()
|
|
pool, err := st.Pool()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = c.Verify(x509.VerifyOptions{Roots: pool, KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}})
|
|
return err
|
|
}
|
|
|
|
// TestIssueAdmitsRevoke: a certificate issued verifies by the CA and is
|
|
// admitted; revoked, it is refused; issuing the name again revokes the one
|
|
// before and admits the new one alone.
|
|
func TestIssueAdmitsRevoke(t *testing.T) {
|
|
st := Store{Dir: filepath.Join(t.TempDir(), "mtls")}
|
|
a, err := st.Issue("laptop")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ca := leafOf(t, a.CA)
|
|
if !ca.IsCA {
|
|
t.Fatal("ca.pem isn't a CA")
|
|
}
|
|
la := leafOf(t, a.Cert)
|
|
if err := verifies(t, st, la); err != nil {
|
|
t.Fatalf("issued certificate doesn't verify: %v", err)
|
|
}
|
|
if _, err := tls.X509KeyPair(a.Cert, a.Key); err != nil {
|
|
t.Fatalf("cert and key don't pair: %v", err)
|
|
}
|
|
if err := st.Admits(la); err != nil {
|
|
t.Fatalf("issued certificate refused: %v", err)
|
|
}
|
|
b, err := st.Issue("LAPTOP")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.Admits(la); err == nil {
|
|
t.Fatal("a name issued again still admits the certificate before")
|
|
}
|
|
lb := leafOf(t, b.Cert)
|
|
if err := st.Admits(lb); err != nil {
|
|
t.Fatalf("new certificate refused: %v", err)
|
|
}
|
|
if n, err := st.Revoke("laptop"); err != nil || n != 1 {
|
|
t.Fatalf("revoke = %d, %v", n, err)
|
|
}
|
|
if err := st.Admits(lb); err == nil || !strings.Contains(err.Error(), "revoked") {
|
|
t.Fatalf("revoked certificate: %v", err)
|
|
}
|
|
if _, err := st.Revoke("laptop"); err == nil {
|
|
t.Fatal("revoking nothing in use is no error")
|
|
}
|
|
l, err := st.List()
|
|
if err != nil || len(l) != 2 {
|
|
t.Fatalf("list = %v, %v", l, err)
|
|
}
|
|
for _, c := range l {
|
|
if c.Good(c.Issued) {
|
|
t.Errorf("%s still good", c.Serial)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestRevokeBySerial: a serial names one certificate.
|
|
func TestRevokeBySerial(t *testing.T) {
|
|
st := Store{Dir: t.TempDir()}
|
|
a, _ := st.Issue("a")
|
|
b, _ := st.Issue("b")
|
|
if n, err := st.Revoke(strings.ToUpper(a.Serial)); err != nil || n != 1 {
|
|
t.Fatalf("revoke = %d, %v", n, err)
|
|
}
|
|
if st.Admits(leafOf(t, a.Cert)) == nil || st.Admits(leafOf(t, b.Cert)) != nil {
|
|
t.Fatal("the serial revoked the wrong certificate")
|
|
}
|
|
}
|
|
|
|
// TestAdmitsRefusesForeign: a certificate another CA signed, or this CA's
|
|
// own not on the list (the list lost or unreadable), is refused.
|
|
func TestAdmitsRefusesForeign(t *testing.T) {
|
|
st := Store{Dir: t.TempDir()}
|
|
other := Store{Dir: t.TempDir()}
|
|
st.Issue("mine")
|
|
o, _ := other.Issue("theirs")
|
|
lo := leafOf(t, o.Cert)
|
|
if verifies(t, st, lo) == nil {
|
|
t.Fatal("another CA's certificate verifies")
|
|
}
|
|
if st.Admits(lo) == nil {
|
|
t.Fatal("another CA's certificate admitted")
|
|
}
|
|
m, _ := st.Issue("phone")
|
|
lm := leafOf(t, m.Cert)
|
|
if err := os.WriteFile(st.clientsFile(), []byte("{not json"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.Admits(lm); err == nil {
|
|
t.Fatal("an unreadable list admits")
|
|
}
|
|
if _, err := st.Issue("x"); err == nil {
|
|
t.Fatal("issue over an unreadable list wrote over it")
|
|
}
|
|
os.Remove(st.clientsFile())
|
|
if err := st.Admits(lm); err == nil {
|
|
t.Fatal("a lost list admits")
|
|
}
|
|
}
|
|
|
|
// TestCAHalfMissing: a CA with its key or its certificate gone is an
|
|
// error, never made over (which would orphan every certificate issued).
|
|
func TestCAHalfMissing(t *testing.T) {
|
|
st := Store{Dir: t.TempDir()}
|
|
if _, _, err := st.CA(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
if fi, _ := os.Stat(st.caKeyFile()); fi.Mode().Perm() != 0o600 {
|
|
t.Errorf("ca-key.pem mode %v", fi.Mode().Perm())
|
|
}
|
|
}
|
|
os.Remove(st.caFile())
|
|
if _, _, err := st.CA(); err == nil {
|
|
t.Fatal("a CA without its certificate was made over")
|
|
}
|
|
if _, err := os.Stat(st.caFile()); err == nil {
|
|
t.Fatal("ca.pem written again")
|
|
}
|
|
}
|
|
|
|
func TestIssueName(t *testing.T) {
|
|
st := Store{Dir: t.TempDir()}
|
|
for _, n := range []string{"", " ", "a/b", `a\b`, "a:b", "a\nb"} {
|
|
if _, err := st.Issue(n); err == nil {
|
|
t.Errorf("%q issued", n)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestServerCert: signed by the CA for the names and addresses given.
|
|
func TestServerCert(t *testing.T) {
|
|
st := Store{Dir: t.TempDir()}
|
|
c, err := st.ServerCert([]string{"localhost", "box.local"}, []net.IP{net.IPv4(192, 168, 1, 5)})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pool, _ := st.Pool()
|
|
for _, name := range []string{"box.local", "192.168.1.5"} {
|
|
if _, err := c.Leaf.Verify(x509.VerifyOptions{Roots: pool, DNSName: name}); err != nil {
|
|
t.Errorf("%s: %v", name, err)
|
|
}
|
|
}
|
|
if _, err := c.Leaf.Verify(x509.VerifyOptions{Roots: pool, DNSName: "evil.example"}); err == nil {
|
|
t.Error("signed for a name not given")
|
|
}
|
|
}
|
|
|
|
// TestExport: three files, the key 0600, a second export beside the first.
|
|
func TestExport(t *testing.T) {
|
|
st := Store{Dir: t.TempDir()}
|
|
out := t.TempDir()
|
|
iss, _ := st.Issue("my phone")
|
|
d1, err := Export(iss, out)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if filepath.Base(d1) != "magpie-my-phone" {
|
|
t.Fatalf("folder %s", d1)
|
|
}
|
|
d2, _ := Export(iss, out)
|
|
if d2 == d1 {
|
|
t.Fatal("second export over the first")
|
|
}
|
|
for _, f := range []string{"client.pem", "client-key.pem", "ca.pem"} {
|
|
if _, err := os.Stat(filepath.Join(d1, f)); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
if _, err := tls.LoadX509KeyPair(filepath.Join(d1, "client.pem"), filepath.Join(d1, "client-key.pem")); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
if fi, _ := os.Stat(filepath.Join(d1, "client-key.pem")); fi.Mode().Perm() != 0o600 {
|
|
t.Errorf("key mode %v", fi.Mode().Perm())
|
|
}
|
|
}
|
|
}
|