|
| 1 | +## Establishing a Connection |
| 2 | + |
| 3 | +Sendspin has two standard ways to establish connections: Server and Client initiated. Server Initiated connections are recommended as they provide standardized multi-server behavior, but require mDNS which may not be available in all environments. |
| 4 | + |
| 5 | +Sendspin Servers must support both methods described below. |
| 6 | + |
| 7 | +### Server Initiated Connections |
| 8 | + |
| 9 | +Clients announce their presence via mDNS using: |
| 10 | +- Service type: `_sendspin._tcp.local.` |
| 11 | +- Port: The port the Sendspin client is listening on (recommended: `8928`) |
| 12 | +- TXT record: `path` key specifying the WebSocket endpoint, REQUIRED (recommended value: `/sendspin`) |
| 13 | +- TXT record: `name` key specifying the friendly name of the player (optional) |
| 14 | + |
| 15 | +The server discovers available clients through mDNS and connects to each client via WebSocket using the advertised address and path. |
| 16 | + |
| 17 | +**Note:** Do not manually connect to servers if you are advertising `_sendspin._tcp`. |
| 18 | + |
| 19 | +#### Multiple servers (server-initiated) |
| 20 | + |
| 21 | +A client holds at most one admitted connection at a time, classified by the highest-ranked activity in its declared [`activities`](messaging.md#server--client-serveractivate); from highest to lowest: |
| 22 | + |
| 23 | +- `'management'` |
| 24 | +- `'playback'` |
| 25 | +- `'pairing'` |
| 26 | + |
| 27 | +A connection with empty `activities` ranks lowest. |
| 28 | + |
| 29 | +Clients must persistently store the `server_id` of the server that most recently held the admitted connection while `'playback'` was among its `activities` (the "last-playback server"). |
| 30 | + |
| 31 | +When a new server connects, the client lets the handshake complete before applying admission; the new connection is provisional until its first [`server/activate`](messaging.md#server--client-serveractivate) declares its priority. The incoming connection's priority is compared to the current connection's: higher or equal is accepted, lower is rejected. Two exceptions: |
| 32 | + |
| 33 | +- A [pairing attempt](pairing.md#entering-and-leaving-pairing) is not displaced by an incoming `'playback'` or `'pairing'` connection. |
| 34 | +- When both the current holder and the incoming connection have empty `activities`, the incoming is admitted only if its `server_id` matches the last-playback server (and the existing one's does not); otherwise the existing is kept. |
| 35 | + |
| 36 | +Subsequent `server/activate` updates do not trigger arbitration. A provisional connection that has not sent `server/activate` within 30 seconds is dropped. |
| 37 | + |
| 38 | +A displaced connection receives [`client/goodbye`](messaging.md#client--server-clientgoodbye) reason `'another_server'` (or [`pair/abort`](pairing.md#client--server-pairabort) reason `concurrent_attempt` if it is a pairing handshake). A rejected incoming receives [`client/goodbye`](messaging.md#client--server-clientgoodbye) reason `'concurrent_attempt'` (or [`pair/abort`](pairing.md#client--server-pairabort) reason `concurrent_attempt` for pairings). The client then closes the connection. |
| 39 | + |
| 40 | +### Client Initiated Connections |
| 41 | + |
| 42 | +If clients prefer to initiate the connection instead of waiting for the server to connect, the server must be discoverable via mDNS using: |
| 43 | +- Service type: `_sendspin-server._tcp.local.` |
| 44 | +- Port: The port the Sendspin server is listening on (recommended: `8927`) |
| 45 | +- TXT record: `path` key specifying the WebSocket endpoint, REQUIRED (recommended value: `/sendspin`) |
| 46 | +- TXT record: `name` key specifying the friendly name of the server (optional) |
| 47 | + |
| 48 | +Clients discover the server through mDNS and initiate a WebSocket connection using the advertised address and path. |
| 49 | + |
| 50 | +**Note:** Do not advertise `_sendspin._tcp` if the client plans to initiate the connection. |
| 51 | + |
| 52 | +#### Multiple servers (client-initiated) |
| 53 | + |
| 54 | +Unlike server-initiated connections, servers cannot reclaim clients by reconnecting. How clients handle multiple discovered servers, server selection, and switching is implementation-defined. |
| 55 | + |
| 56 | +**Note:** After this point, Sendspin works independently of how the connection was established. The Sendspin client is always the consumer of data like audio or metadata, regardless of who initiated the connection. |
| 57 | + |
| 58 | +## Encryption |
| 59 | + |
| 60 | +All Sendspin connections use end-to-end encryption based on the [Noise Protocol Framework](https://noiseprotocol.org/noise.html). Encryption is mandatory for all connections established through the standard discovery mechanisms described in [Establishing a Connection](#establishing-a-connection). |
| 61 | + |
| 62 | +### Pattern |
| 63 | + |
| 64 | +Sendspin uses the `KKpsk2` Noise pattern. Both static keys are pre-known to both parties (the `client_id` of the client and the `server_id` of the server are the static public keys), and a [Pre-Shared Key](#pre-shared-key) is mixed in at the end of the handshake's second message. |
| 65 | + |
| 66 | +The **server is the Noise initiator**, the **client is the Noise responder**, regardless of which side initiated the WebSocket connection. |
| 67 | + |
| 68 | +**Security properties.** Forward secrecy is provided by the ephemeral-key DH in each handshake: compromise of static keys or the PSK does not retroactively decrypt prior sessions. Replay protection is provided by Noise's per-direction transport counter; a repeated or out-of-order ciphertext fails AEAD decryption and aborts the connection. |
| 69 | + |
| 70 | +### Cipher Suites |
| 71 | + |
| 72 | +A suite specifies the `<DH>_<cipher>_<hash>` part of the full Noise protocol name. Sendspin defines two: |
| 73 | + |
| 74 | +- `25519_ChaChaPoly_SHA256` - software-friendly suite |
| 75 | +- `25519_AESGCM_SHA256` - hardware-accelerated suite (AES-NI / ARMv8 Crypto Extensions) |
| 76 | + |
| 77 | +Servers must support both suites. Clients must support at least one. |
| 78 | + |
| 79 | +The client picks one suite and announces it in [`client/init`](messaging.md#client--server-clientinit); since servers are required to support every suite, no negotiation is needed. |
| 80 | + |
| 81 | +### Identities |
| 82 | + |
| 83 | +The `client_id` and `server_id` fields are the base64url-encoded (no padding) Curve25519 public keys of the client and server respectively, 43 characters each. These keys serve both as routing/persistence identifiers and as the static keys used in the Noise handshake. |
| 84 | + |
| 85 | +**Key rotation.** Each side's static keypair is intended to be long-lived; the identifier is the pubkey, so rotating the keypair changes the identity. A server that rotates its static keypair (e.g., reprovisioned hardware, migrated host, lost private key) appears to clients as a different server. Operators who want to preserve identity across server moves must preserve the server's static private key (e.g., as part of the server's backup/restore set). |
| 86 | + |
| 87 | +### Pre-Shared Key |
| 88 | + |
| 89 | +The PSK is mixed into the handshake state at the end of the second handshake message (the `psk2` modifier). The transport-mode keys derived after the handshake therefore include the PSK, but the first handshake message's payload (sent by the server) is encrypted under static-key DH only. |
| 90 | + |
| 91 | +To let the client select the right PSK before the PSK must be mixed in, the server includes a `psk_id` in the first handshake message's payload. The identifier is a 43-character base64url-encoded value (no padding) of a 32-byte SHA-256 output, derived deterministically from the PSK: |
| 92 | + |
| 93 | +``` |
| 94 | +psk_id = base64url(SHA-256("sendspin-psk-id-v1" || PSK)) |
| 95 | +``` |
| 96 | + |
| 97 | +The label is the UTF-8 byte sequence of the literal characters shown (no NUL terminator, no surrounding quotes); `||` denotes byte concatenation. The same formula applies to all three PSK categories (long-term, Pairing, Sentinel); the client stores each of its PSKs tagged with its category and, on match, the stored category determines how to proceed. The single handshake pattern (`KKpsk2`) is used in all three cases; only the PSK input differs. |
| 98 | + |
| 99 | +The **Sentinel PSK** is a published constant used as the PSK input whenever no other PSK applies - i.e., before any pairing record exists. It provides no authentication on its own (its value is public); authentication, when needed, is established later during [Pairing](pairing.md#pairing). The sentinel value is: |
| 100 | + |
| 101 | +``` |
| 102 | +Sentinel PSK = SHA-256("sendspin-sentinel-psk-v1") |
| 103 | + = 0x1b5e24dbc1aed95fc2a5a338a90c05df44bd10f5ec1f4cd66cbf86272767b9d3 |
| 104 | +``` |
| 105 | + |
| 106 | +and its `psk_id` is therefore also a published constant: |
| 107 | + |
| 108 | +``` |
| 109 | +Sentinel psk_id = 0x185b15f6d2da4909bd1dc156a4ab206103abef0153bcd52d926170b95cf7ce8a |
| 110 | + = base64url "GFsV9tLaSQm9HcFWpKsgYQOr7wFTvNUtkmFwuVz3zoo" |
| 111 | +``` |
| 112 | + |
| 113 | +The client decrypts the first handshake message's payload using only the static keys, compares the included `psk_id` to hashes of each of its candidate PSKs, and selects the PSK whose hash matches. It then mixes that PSK as required to process the second handshake message. If no candidate matches, the handshake fails. |
| 114 | + |
| 115 | +Two storage variants are supported for long-term [Sendspin PSK](README.md#definitions) records, distinguished by whether the client also stores the server's `server_id`. The wire bytes and `psk_id` lookup are identical; only the post-match check differs. |
| 116 | + |
| 117 | +- **Stored-pubkey model**: each long-term PSK is persisted alongside the server's `server_id`. After a `psk_id` match, the client verifies that the matched PSK's stored `server_id` equals the one in [`server/init`](messaging.md#server--client-serverinit); mismatch fails the handshake. Authentication relies on both the static keys and the PSK. |
| 118 | +- **Shared-PSK model**: PSKs are persisted without an associated `server_id`; the `server_id` from [`server/init`](messaging.md#server--client-serverinit) is accepted at face value. Convenient for storage-constrained clients, but with weaker security properties - multiple servers may share the same PSK. |
| 119 | + |
| 120 | +### Prologue |
| 121 | + |
| 122 | +The prologue mixed into the Noise handshake state on both sides is the concatenation of the exact bytes of [`client/init`](messaging.md#client--server-clientinit) followed by the exact bytes of [`server/init`](messaging.md#server--client-serverinit), as transmitted on the wire (the JSON-encoded UTF-8 message body, without the WebSocket framing). This binds the cleartext init exchange to the handshake; tampering causes the handshake to fail. |
| 123 | + |
| 124 | +### Failure Handling |
| 125 | + |
| 126 | +Any handshake-phase failure - malformed cleartext message, unsupported `version`, unknown `suite`, handshake timeout, `psk_id` lookup miss, Noise AEAD failure, or AEAD failure once in transport mode - closes the WebSocket without sending any application-level error message. Implementations SHOULD apply a timeout (e.g., 30 seconds) for each side to receive the next expected message during the prologue and Noise-handshake phases. |
| 127 | + |
| 128 | +### Re-handshake |
| 129 | + |
| 130 | +The server may rerun the Noise handshake in transport mode to swap session keys without closing the WebSocket - typically to promote the trust class after a successful [pairing](pairing.md#pairing), to switch from Sentinel to a Pairing PSK, or to rotate session keys on long-running connections. |
| 131 | + |
| 132 | +The server initiates, as in the original handshake. The two [`noise/handshake`](messaging.md#client--server-noisehandshake) messages are sent as encrypted binary frames inside the current channel; `psk_id` in noise message 1 selects the PSK for the new session. `client/init` and `server/init` are not re-sent - `client_id`, `server_id`, and `suite` carry over. The new handshake's prologue is the prior handshake's hash `h`. No other messages flow during the exchange; once the new keys are in place, the connection continues with the usual [`server/hello`](messaging.md#server--client-serverhello) → [`client/hello`](messaging.md#client--server-clienthello) (the client re-asserts `trust_level`) → [`server/activate`](messaging.md#server--client-serveractivate). |
0 commit comments