Documentation
¶
Overview ¶
Package signing signs HTTP requests with a shared key and verifies them.
A request signature is an HMAC-SHA256, keyed by a 32-byte hex key, over the method, the request URI as sent on the wire (escaped path and query), the Unix time, a random nonce, the body hash and an epoch. The verifier checks the signature, a time window, that the nonce is new and that the epoch is its own.
An epoch is random per verifier instance (a daemon start, a sharing listener), so a request captured by a program posing as the verifier is refused by the real one, whose epoch differs. A signer learns the epoch from a proof: it sends a fresh random challenge, and the verifier answers with its epoch and HMAC(key, "server", challenge, epoch). A signer sends signed requests only after checking such a proof, and checks the proof on replies too, so a listener without the key gets no signature at all. A whole reply is proved with HMAC(key, "server", challenge, epoch, status, body hash), so a relay cannot alter it either; WebSocket upgrades carry the short proof.
Every message is newline-joined parts that never contain a newline themselves, and each use has its own number of parts: a session value 2, a proof 3, a reply proof 5, a request signature 6. One kind of MAC can therefore never be taken for another.
Paired devices sign with their device key (package share); the CLI, MCP server and menu bar app sign requests to their own daemon with its API token (package server).
Index ¶
- Constants
- Variables
- func BodyHash(body []byte) string
- func Challenge(h http.Header) string
- func CheckEpoch(h http.Header, epoch string) error
- func Header(name string) string
- func MAC(key string, parts ...string) string
- func NewEpoch() string
- func Prove(h, req http.Header, key, epoch string)
- func ProveReply(h, req http.Header, key, epoch string, status int, body []byte)
- func Proven(key, challenge string, h http.Header) (string, bool)
- func ProvenReply(key, challenge string, h http.Header, status int, body []byte) (string, bool)
- func RandomHex(n int) string
- func Sign(h http.Header, key, method, uri string, body []byte, epoch string)
- func Signed(h http.Header) bool
- func ValidKey(k string) bool
- type Verifier
Constants ¶
const ( // Window is the allowed difference between the signing and the // verifying clock. Window = 2 * time.Minute // MaxBody bounds a signed request body, which is read whole to be hashed. MaxBody = 8 << 20 )
Variables ¶
var ( HeaderDate = Header("Date") HeaderNonce = Header("Nonce") HeaderSignature = Header("Signature") // HeaderEpoch names the verifier instance a request is meant for; // signatures cover it. Replies carry it next to the proof. HeaderEpoch = Header("Epoch") // HeaderChallenge carries a signer's fresh challenge; HeaderProof the // verifier's answer to it. HeaderChallenge = Header("Challenge") HeaderProof = Header("Proof") )
Header names of a signed request.
var ErrClock = errors.New("the request time differs from this computer's clock by more than two minutes")
ErrClock means a request was signed outside the time window.
var ErrEpoch = errors.New("signed for another instance; ask for a new proof")
ErrEpoch means a request was signed for another instance of the verifier, such as a daemon that has restarted since.
var ErrReplay = errors.New("replayed request")
ErrReplay means a nonce was used before.
var ErrSignature = errors.New("bad signature")
ErrSignature means a signature is missing, malformed or wrong.
Functions ¶
func CheckEpoch ¶
CheckEpoch reports ErrEpoch unless request header h names epoch.
func Prove ¶
Prove answers the challenge of request header req in reply header h: the epoch and a proof that the replying side holds key. A malformed challenge gets no answer.
func ProveReply ¶
ProveReply answers the challenge of request header req in reply header h for a whole reply: the epoch and a proof over the challenge, the epoch, the status and the body. A malformed challenge gets no answer.
func Proven ¶
Proven reports whether reply header h proves that the replying side holds key, for challenge, and returns the epoch the proof vouches for.
func ProvenReply ¶
ProvenReply reports whether reply header h proves, for challenge, that the replying side holds key and sent this status and body, and returns the epoch the proof vouches for.
Types ¶
type Verifier ¶
type Verifier struct {
// contains filtered or unexported fields
}
Verifier checks signed requests and remembers recent nonces to refuse replays. The zero value is ready to use.