mtls

package
v0.1.4701 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 20 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 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Export

func Export(iss Issued, dir string) (string, error)

Export writes what a device needs to dir/magpie-<name>: client.pem, client-key.pem (0600) and ca.pem, and returns that folder.

func WriteFile

func WriteFile(path string, b []byte, mode os.FileMode) error

WriteFile is writeFile, for the certificate Settings imports.

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.

func (Client) Good

func (c Client) Good(now time.Time) bool

Good: issued, not revoked and not run out at now.

type Issued

type Issued struct {
	Client
	Cert, Key, CA []byte // PEM
}

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

func (s Store) CA() (*x509.Certificate, crypto.Signer, error)

CA is the store's CA, made on first use.

func (Store) CAFile

func (s Store) CAFile() string

CAFile is the CA's certificate, the file a device trusts the server by.

func (Store) HasCA

func (s Store) HasCA() bool

HasCA: the CA was made.

func (Store) Issue

func (s Store) Issue(name string) (Issued, error)

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

func (s Store) List() ([]Client, error)

List is every certificate issued, the newest first.

func (Store) Pool

func (s Store) Pool() (*x509.CertPool, error)

Pool is the CA as a pool a server verifies client certificates by.

func (Store) Revoke

func (s Store) Revoke(name string) (int, error)

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

func (s Store) ServerCert(names []string, ips []net.IP) (tls.Certificate, error)

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.

Jump to

Keyboard shortcuts

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