Documentation
¶
Overview ¶
Package e2e end-to-end encrypts one terminal socket at a time between a paired browser and the desktop it is paired with, so that the relay carrying it -- shared or run by a customer alike -- can never read what crosses it, the same guarantee push notifications already have. It is a byte-for-byte port of flockdeck-relay's own internal/e2e package, which is the reference implementation of the scheme: what this package does is exactly what a browser (WebCrypto) and this desktop have to do to interoperate.
Why this is a copy, not an import ¶
flockdeck-relay's internal/e2e lives at github.com/jmwri/flockdeck-relay/internal/e2e, in a different module from this one (github.com/jmwri/flockdeck). Go's internal-import rule only allows a package rooted at ".../internal/x" to be imported by code whose own import path shares everything before "internal" -- which, across two separate modules and repositories, this desktop's code never can. There is no way to depend on that package from here, however the module graph is arranged, so this file exists instead: the same algorithm, the same constants, the same wire format, kept in step by hand. Anyone changing one must change the other identically, and the tests in e2e_test.go -- ported the same way, from flockdeck-relay's internal/e2e/e2e_test.go -- are what catches a copy that has drifted.
Keys ¶
Every device (a paired browser) and every host (a desktop, this one included) holds one long-term P-256 key pair, generated locally with GenerateStaticKey. The private half never leaves the machine that made it -- not in memory, not on disk, and never over the wire. The public half is registered with the relay once (POST /api/v1/device/e2e-key or /api/v1/host/e2e-key) and from then on travels exactly where a device's or a host's name already travels: in AccountView, to whichever of the account's browsers or desktops asks. The relay's part in all of this is exactly its part in push notifications -- a switchboard for public keys, never a party that could compute a shared secret from them, because it never holds a private one.
Handshake ¶
A browser opening a terminal runs a fresh handshake over that socket before anything is typed: StartDeviceHandshake makes an ephemeral key pair, good for this one socket alone, and sends its public half as the first frame (the "hello"). The desktop reads it, and RespondHostHandshake makes its own ephemeral pair and answers with its public half (the "response"). Both sides then hold four public keys between them -- the device's static and ephemeral, the host's static and ephemeral -- and each holds two of the matching private keys, which is enough for each to compute the same three Diffie-Hellman terms the other computes from its own two:
T1 = ECDH(device ephemeral, host static) T2 = ECDH(device static, host ephemeral) T3 = ECDH(device ephemeral, host ephemeral)
T3 alone would be an ordinary anonymous ECDH handshake: secret against eavesdropping, but not proof of who is on the other end, since the relay forwards the ephemeral keys and could in principle have substituted its own. T1 and T2 fold in the two sides' long-term keys, so deriving the right session keys from all three terms together needs the device's static private key and the host's static private key -- exactly the two the relay has never had. That makes the handshake mutually authenticated against a relay that merely stores and forwards keys and frames as written, though, as with any scheme whose only root of trust is a key registered with a server, it does not defend against a relay that actively swaps a static public key for its own the moment it is registered; closing that gap needs an out-of-band check -- comparing a fingerprint on both screens -- which is a client-side feature this package leaves room for but does not itself provide.
T1, T2 and T3 are concatenated and fed to HKDF-SHA256 (RFC 5869), salted with both ephemeral public keys so the extract is bound to this exact exchange, to make two independent AES-256-GCM keys -- one for each direction, so the two ends never encrypt with the same key -- and two 4-byte nonce prefixes to go with them. Losing an ephemeral private key later (a browser tab closed, a desktop restarted) reveals nothing about a past session: T3, and with it the whole derived secret, cannot be recomputed without it. That is this scheme's forward secrecy.
Frames ¶
Once a Session is derived, every message that follows on the same socket -- one terminal keystroke, one chunk of output -- is one call to Seal on the way out and Open on the way in. A frame is a version byte, an 8-byte big-endian counter and an AES-256-GCM-sealed body; the version and which of the two sides sent it are folded into the AEAD's associated data, and the counter must strictly increase by exactly one each time, which an ordered, reliable WebSocket -- what every terminal socket already is -- never gives a legitimate frame reason to violate. A relay that dropped, reordered, duplicated or altered a single byte of a frame would only make Open refuse it; there is nothing in the scheme it could change instead.
What the relay does with any of this ¶
Nothing. The relay never imports this package (nor could it import this copy of it, being in the other module). It stores and hands out public keys exactly as it already does with a device's push subscription keys, and it carries a terminal socket's bytes exactly as it already carries every other stream through the tunnel -- opaque past the outer framing smux and WebSocket need to deliver them at all. A relay that held every byte this package ever sent or received could not read a single terminal keystroke without one of the two static private keys, which it was never given.
Index ¶
Constants ¶
const Version byte = 1
Version is the wire version of every handshake message and frame this package makes. It travels in a data frame's first byte and in the AEAD's associated data, so a frame made under a later or earlier version of this scheme is refused rather than misread as this one's.
Variables ¶
This section is empty.
Functions ¶
func DecodePublicKey ¶
DecodePublicKey reads a public key as EncodePublicKey writes it, refusing anything that is not base64url or not a point on the curve -- which is all the relay itself ever needs to check of a key it is asked to store, having no way to know anything more about whether it is genuinely the sender's.
func EncodePublicKey ¶
EncodePublicKey is how a public key is given to the relay to store, and how the relay gives one back: unpadded base64url of the uncompressed P-256 point (65 bytes), the same encoding flockdeck-relay's push.go already uses for a subscription's p256dh.
func GenerateStaticKey ¶
func GenerateStaticKey() (*ecdh.PrivateKey, error)
GenerateStaticKey makes a fresh long-term P-256 key pair: what a device or a host generates once, keeps the private half of forever (until it chooses to rotate), and registers the public half of with the relay.
Types ¶
type DeviceHandshake ¶
type DeviceHandshake struct {
// contains filtered or unexported fields
}
DeviceHandshake is a browser's side of opening one terminal session, from the moment it makes its ephemeral key until the desktop's response completes it.
func StartDeviceHandshake ¶
func StartDeviceHandshake(deviceStaticPriv *ecdh.PrivateKey, hostStaticPub *ecdh.PublicKey) (*DeviceHandshake, []byte, error)
StartDeviceHandshake begins a browser's side of a fresh terminal session with the desktop whose long-term public key is hostStaticPub -- read from the relay's own account view, where every desktop's key already travels. It returns the hello message to send as the terminal socket's very first frame, ahead of anything a person types or the desktop sends back.
func (*DeviceHandshake) Finish ¶
func (h *DeviceHandshake) Finish(response []byte) (*Session, error)
Finish completes a browser's handshake with the desktop's response -- its ephemeral public key, exactly as StartDeviceHandshake's hello was -- and returns the session the two now independently share.
type Role ¶
type Role byte
Role says which end of a session a Session is, which decides which of its two directional keys it seals with and which it opens with, and which side's long-term and ephemeral keys take which position in the handshake both ends must compute identically.
The two roles. Their numeric value is part of the wire format -- it goes into the AEAD's associated data as the sender's role -- and so is fixed.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is one terminal socket's derived keys: two AES-256-GCM ciphers, one for each direction, each with its own nonce prefix and its own frame counter. It is good for the life of the socket it was made for alone -- nothing in this package ever persists one -- and is not safe for concurrent use: a socket's reader calls Open and its writer calls Seal, each from one goroutine of its own, exactly as a typical connection's read and write sides already run.
func RespondHostHandshake ¶
func RespondHostHandshake(hostStaticPriv *ecdh.PrivateKey, deviceStaticPub *ecdh.PublicKey, hello []byte) (*Session, []byte, error)
RespondHostHandshake is a desktop's whole side of the handshake in one call: given the browser's hello and the long-term public key the relay says that device holds (fetched from /api/v1/host/devices and matched to this socket by the Flockdeck-Remote-Device header the relay already sets, which names the device without the device having to say so itself), it returns the session and the response to send back as the terminal socket's second frame.
func (*Session) Open ¶
Open authenticates and decrypts a frame Seal made of this session's incoming direction. It refuses one of the wrong wire version, one whose counter is not exactly the next expected -- an old frame replayed, or one out of order, which the ordered, reliable socket a terminal already runs on gives a genuine frame no way to arrive as -- or one that does not authenticate at all, which is any tampering with it, the relay's included: nothing this package ever gives the relay lets it produce a frame that passes this check.
func (*Session) Seal ¶
Seal encrypts plaintext as the next frame of this session's outgoing direction: a wire version byte, an 8-byte big-endian counter -- 0 for the first frame after the handshake, then strictly one more each call -- and the AEAD's output, ciphertext followed by its 16-byte tag. The counter is never reused for the life of a Session; at one frame a nanosecond it would take longer than the age of the universe to repeat, which is the only way two frames could ever share a nonce.