// SPDX-FileCopyrightText: Distronode Corporation // // SPDX-License-Identifier: Apache-2.0 // The signer (`wim signer`), the ssh-agent model: one long-running process per device holds the // device's secret keys and the data key that seals its prekey secrets, and answers sessions over a // local socket. It signs, seals and opens; it never hands out a key. Locking wipes the keys from // its memory for good: `wim signer unlock` starts a new signer, so the passphrase is never sent // over the socket, where something posing as a locked signer could collect it. // // What it stops and what it does not is in SECURITY-MODEL section 5.8. import { b64u, boxSchema, isUlid, ulid, canonBytes, createPrekey, isMeshId, openBox, prekeyDeleteAt, prekeySchema, sanitizeForDisplay, sealBox, sessionAddressSchema, verifyObject, type Opened, type Prekey, type SignType, } from "@worldinmotion/protocol"; import { createPrivateKey, sign as edSign, type KeyObject } from "node:crypto"; import { promises as fs } from "node:fs"; import { createConnection, createServer, type Server, type Socket } from "node:net"; import { z } from "zod"; import type { EndpointFiles } from "./agentpath.ts"; import { certBytes, channelFor, ephemeral, helloTranscript, runKey, type CertBody, type Channel, type HelloInfo } from "./channel.ts"; import { CODE_RE, decisionView, heldDigest, heldItemSchema, MAX_DECISION_IDS, MAX_DECISION_ITEMS, shownOf, signDecision, type DecisionItem, type HeldCopy } from "./decisions.ts"; import { MAX_LINE, unseal } from "./signer.ts"; import { openPrekeySecret, sealPrekeySecret, type SealedKeyFile, type Unlocked } from "./vault.ts"; /** A refusal with a code the client maps to a SignerError. */ export class AgentError extends Error { readonly code: string; constructor(code: string, message: string) { super(message); this.code = code; } } /** Key operations per second, with a burst: a room history page or a broadcast fits, a loop does not. */ export const AGENT_RATE = { perSecond: 100, burst: 1000 }; /** A token bucket on an injected clock: `take` is false while the bucket is empty. */ export class TokenBucket { private tokens: number; private last: number; private readonly perSecond: number; private readonly burst: number; private readonly now: () => number; constructor(perSecond: number, burst: number, now: () => number) { this.perSecond = perSecond; this.burst = burst; this.now = now; this.tokens = burst; this.last = now(); } take(): boolean { const now = this.now(); // A clock set back gives no time: never a negative refill, which would starve the bucket. const elapsed = Math.max(0, now - this.last); this.tokens = Math.min(this.burst, this.tokens + (elapsed * this.perSecond) / 1000); this.last = now; if (this.tokens < 1) return false; this.tokens -= 1; return true; } } /** One line of the signer's log: what was asked and the answer, never a key or a plaintext. */ export type LogEntry = { at: number; op: string; ok: boolean; code?: string; type?: string; mesh?: string; peer?: string }; /** The log as JSON lines (mode 0600), moved to `.1` past `maxBytes`. A failed write never stops the signer. */ export class AgentLog { private chain: Promise = Promise.resolve(); private readonly path: string; private readonly maxBytes: number; constructor(path: string, maxBytes = 1024 * 1024) { this.path = path; this.maxBytes = maxBytes; } write(entry: LogEntry): void { this.chain = this.chain .then(async () => { await fs.appendFile(this.path, `${JSON.stringify(entry)}\n`, { mode: 0o600 }); if ((await fs.stat(this.path)).size > this.maxBytes) await fs.rename(this.path, `${this.path}.1`); }) .catch(() => undefined); } /** Resolves once every write so far is on disk (or failed). */ flushed(): Promise { return this.chain; } } const bytes = (n?: number) => z.string().refine((s) => { try { return n === undefined || b64u.decode(s).length === n; } catch { return false; } }, "expected b64u bytes"); const id22 = z.string().length(22); const boxArgs = { mesh: z.string().refine(isMeshId, "expected a mesh id"), sender: id22, recipient: id22, aad: bytes(32) }; const sealArgs = z.strictObject({ ...boxArgs, recipientBox: bytes(32), plaintext: bytes(), prekey: z.strictObject({ id: id22, pub: bytes(32) }).optional() }); const openArgs = z.strictObject({ ...boxArgs, senderBox: bytes(32), box: boxSchema }); const open2Args = openArgs.extend({ entry: z.strictObject({ prekey: prekeySchema, sealed: z.string() }) }); const signArgs = z.strictObject({ type: z.string(), obj: z.record(z.string(), z.unknown()) }); const ARGS = { hello: z.strictObject({ nonce: bytes(32), epub: bytes(32) }), status: z.strictObject({}), sign: signArgs, seal: sealArgs, open: openArgs, open2: open2Args, prekey: z.strictObject({ mesh: boxArgs.mesh, at: z.int().min(0) }), adopt: z.strictObject({ prekey: prekeySchema, secret: bytes(32) }), decide: z.strictObject({ mesh: boxArgs.mesh, verdict: z.enum(["approve", "refuse", "resume"]), ids: z.array(z.string().refine(isUlid, "expected a held id")).max(MAX_DECISION_IDS), peer: z.string().min(1).max(200).optional(), code: z.string().regex(CODE_RE, "expected the six-digit code the asking process shows"), }), lock: z.strictObject({}), stop: z.strictObject({}), }; type Op = keyof typeof ARGS; /** * Whose a signed object says it is, per signature type. The signer signs only what names this * device, and never a prekey: those it makes itself (`prekey`), so no caller can get a prekey * signed for a secret it holds and read the device's traffic after the signer is locked. */ const OWNER: Record) => unknown> = { auth: (o) => o["device"], roster: (o) => o["by"], send: (o) => (o["from"] as { device?: unknown } | undefined)?.device, join: (o) => (o["device"] as { id?: unknown } | undefined)?.id, }; /** * An Ed25519 private key as Node's crypto takes it, made once per unlock. The PKCS#8 DER around the * seed is built in a buffer of its own (Buffer.concat would copy the seed into Node's shared pool, * where nothing zeroes it) and zeroed once the key object holds it. */ function ed25519Private(seed: Uint8Array): KeyObject { const prefix = Buffer.from("302e020100300506032b657004220420", "hex"); const der = Buffer.alloc(prefix.length + seed.length); prefix.copy(der); der.set(seed, prefix.length); try { return createPrivateKey({ key: der, format: "der", type: "pkcs8" }); } finally { der.fill(0); } } /** * How far an `auth` or `send` object's `at`, or a new prekey's, may be from the signer's clock. Receivers take a * message up to its freshness window old, so what the signer signs while unlocked stays usable * after a lock for that long (SECURITY-MODEL 5.8); it does not sign for later than this. */ export const SIGN_SKEW_MS = 5 * 60_000; /** How long a run key's certificate holds (see Signer.run). */ export const RUN_LIFETIME_MS = 24 * 60 * 60_000; /** Present on a `hello` answer whose signer opened a channel; JSON.stringify leaves symbol keys out. */ export const CHANNEL: unique symbol = Symbol("channel"); /** * How the signer takes a decision on held messages (`decide`): it reads the items from the state * directory itself, and the device a resume names from the mesh's roster, and asks the person in * its own terminal or dialog. `confirm` is why it cannot ask when it cannot (it runs under an * agent's variables, or has neither); `gone` aborts when the asker went away, which ends the ask. */ export type Desk = { read: (mesh: string, ids: readonly string[]) => Promise; /** The id of the roster's device named `name` in `mesh`, or undefined when there is none. */ device: (mesh: string, name: string) => Promise; confirm: ((lines: string[], gone?: AbortSignal) => Promise) | { cannot: string }; }; /** After a decline the signer takes the next decision only this long after (doubling to maxMs; a yes starts it over). */ export const DECLINE_BACKOFF = { firstMs: 10_000, maxMs: 5 * 60_000 }; export type SignerOptions = { file: SealedKeyFile; unlocked: Unlocked; now: () => number; pid: number; log: (entry: LogEntry) => void; rate?: { perSecond: number; burst: number }; /** Without one, every `decide` is refused. */ desk?: Desk; /** The wait after a decline (DECLINE_BACKOFF by default). */ backoff?: { firstMs: number; maxMs: number }; }; /** * The signer's state and its answer to each request, apart from any socket. Locked, it holds no * key and answers only `hello`, `status`, `lock` and `stop`. There is no unlock over the socket. */ export class Signer { readonly file: SealedKeyFile; readonly started: number; private keys: Unlocked | undefined; private signKey: KeyObject | undefined; /** * This run's own key and its certificate from the device key, so the signer proves it is this * device's signer, locked or not (`lock` and `stop` need that proof). It signs nothing but * hellos. The certificate says whether the signer is locked and holds for RUN_LIFETIME_MS: an * unlocked signer issues a new one at half of that, and `lock` issues a locked one (whose key can * only ever pose as a locked signer) before the device key goes; a locked signer stops when its * certificate runs out. */ private run!: { privateKey: KeyObject; body: CertBody; sig: string }; private readonly bucket: TokenBucket; private readonly counts: Record = {}; private refused = 0; private limited = 0; /** A `decide` is waiting for the person: another is refused meanwhile, never queued behind it. */ private deciding = false; /** After a decline: when the next decision is taken, and how long the wait after another decline is. */ private declined = { until: 0, ms: 0 }; private readonly o: SignerOptions; constructor(o: SignerOptions) { this.o = o; this.file = o.file; this.keys = o.unlocked; this.signKey = ed25519Private(o.unlocked.keys.signSec); this.issue(false, this.signKey); this.started = o.now(); const rate = o.rate ?? AGENT_RATE; this.bucket = new TokenBucket(rate.perSecond, rate.burst, o.now); } get locked(): boolean { return this.keys === undefined; } /** * Wipes the keys from memory as far as JavaScript can: the byte arrays are zeroed, and the key * object is dropped (OpenSSL frees its copy when it is collected; JavaScript cannot zero it). */ lock(): void { const k = this.keys; if (k === undefined) return; // The last thing the device key signs: a run key that can only say "locked". this.issue(true, this.signKey!); this.keys = undefined; this.signKey = undefined; for (const b of [k.keys.signSec, k.keys.boxSec, k.data]) b.fill(0); } /** A new run key and its certificate, signed with the device key. */ private issue(locked: boolean, signKey: KeyObject): void { const key = runKey(); const now = this.o.now(); const body: CertBody = { pub: b64u.encode(key.pub), locked, issued: now, expires: now + RUN_LIFETIME_MS, pid: this.o.pid }; this.run = { privateKey: key.privateKey, body, sig: b64u.encode(edSign(null, certBytes(body), signKey)) }; } /** * An unlocked signer issues a new run key when its certificate is half spent, or was issued * after the clock's now: a clock set back (NTP, a resumed VM) would otherwise leave clients * refusing a certificate from their future until the clock caught up. Run at every hello and on * the server's watch. A locked signer has no device key to issue with (SECURITY-MODEL 5.8). */ refresh(): void { const age = this.o.now() - this.run.body.issued; if (this.signKey !== undefined && (age < 0 || age >= RUN_LIFETIME_MS / 2)) this.issue(false, this.signKey); } /** A locked signer whose certificate ran out: it can no longer prove itself, and stops. */ get spent(): boolean { return this.locked && this.o.now() >= this.run.body.expires; } /** Its endpoint now names another socket (renamed or replaced): locked, and logged. */ replaced(): void { this.lock(); this.o.log({ at: this.o.now(), op: "replaced", ok: false, code: "replaced" }); } /** Answers one request, or throws an AgentError. Every answer is logged. */ /** `gone`: aborted when the asker went away (its connection closed); a `decide` then signs nothing. */ async handle(op: string, raw: unknown, gone?: AbortSignal): Promise> { const entry: LogEntry = { at: this.o.now(), op, ok: false }; try { const result = await this.answer(op, raw, entry, gone); entry.ok = true; // `hello` and `status` only ask about the signer; the counts are of what it was asked to do. if (op !== "hello" && op !== "status") this.counts[op] = (this.counts[op] ?? 0) + 1; return result; } catch (error) { const code = error instanceof AgentError ? error.code : "bad_request"; entry.code = code; if (code === "refused") this.refused++; throw error instanceof AgentError ? error : new AgentError("bad_request", (error as Error).message); } finally { this.o.log(entry); } } private async answer(op: string, raw: unknown, entry: LogEntry, gone: AbortSignal | undefined): Promise> { if (!Object.hasOwn(ARGS, op)) throw new AgentError("bad_request", `no operation ${JSON.stringify(op)}`); const parsed = ARGS[op as Op].safeParse(raw); if (!parsed.success) throw new AgentError("bad_request", `${op}: ${parsed.error.issues[0]!.message}`); const a = parsed.data as Record; switch (op as Op) { case "hello": return this.hello(b64u.decode(a["nonce"] as string), b64u.decode(a["epub"] as string)); case "status": return { id: this.file.id, name: this.file.name, locked: this.locked, pid: this.o.pid, started: this.started, ops: { ...this.counts }, refused: this.refused, limited: this.limited }; case "lock": { // The answer, sealed on the channel, says whether it was locked already: never the hello's flag. const was = this.locked; this.lock(); return { was }; } case "stop": return {}; } const keys = this.keys; if (keys === undefined) throw new AgentError("locked", "the signer is locked: run `wim signer unlock` in a terminal you opened yourself"); if (!this.bucket.take()) { this.limited++; throw new AgentError("rate_limited", "too many signer operations; slow down"); } if (op === "decide") return this.decide(a as z.infer, entry, gone); return this.keyOp(op as Exclude, a, keys, this.signKey!, entry); } /** * A decision on held messages, signed only once the person confirmed it here: the signer reads * every held copy of the ids from disk itself, shows them in its own terminal or dialog with the * request's code, and signs those ids, each with the digest of the copy it showed. One at a time, * and after a decline not again for a while. A lock while the person reads it signs nothing. * * Refused before the person is asked: a copy that is not a held item is dropped, and an id whose * copies differ in what the person would be shown is refused (one would be shown, the other * released). A resume names a session address whose device is in the roster, and covers only * that device's breaker holds. */ private async decide(req: z.infer, entry: LogEntry, gone: AbortSignal | undefined): Promise> { entry.mesh = req.mesh; const desk = this.o.desk; if (desk === undefined) throw new AgentError("refused", "this signer takes no decisions on held messages"); if (!(typeof desk.confirm === "function")) throw new AgentError("refused", desk.confirm.cannot); if (req.verdict === "resume" && req.peer === undefined) throw new AgentError("bad_request", "decide: a resume names its peer"); if (this.deciding) throw new AgentError("refused", "the signer is already asking you about another decision: answer that one first"); const wait = this.declined.until - this.o.now(); if (wait > 0) throw new AgentError("refused", `you declined a decision in the signer just now: it takes the next one in ${Math.ceil(wait / 1000)} s`); this.deciding = true; try { const peer = req.verdict === "resume" ? await this.resumed(desk, req.mesh, req.peer!) : undefined; const items = (await desk.read(req.mesh, req.ids)).filter((c) => req.ids.includes(c.item.id) && heldItemSchema.safeParse(c.item).success); const shown = new Map(); const seen = new Set(); const signed: DecisionItem[] = []; for (const { item } of items) { const view = shownOf(item); if ((shown.get(item.id) ?? view) !== view) throw new AgentError("refused", `held message ${item.id} is held in copies that differ in what you would be shown: nothing was signed (\`wim held list\` shows them)`); shown.set(item.id, view); if (peer !== undefined && !(item.reason === "breaker" && item.device === peer.id && item.from.slice(item.from.indexOf("@") + 1) === peer.device)) { throw new AgentError("refused", `held message ${item.id} is not held by the loop breaker from ${sanitizeForDisplay(req.peer!)}'s device: a resume releases only those`); } const one = { id: item.id, digest: heldDigest(item) }; if (seen.has(`${one.id} ${one.digest}`)) continue; seen.add(`${one.id} ${one.digest}`); signed.push(one); } if (signed.length > MAX_DECISION_ITEMS) throw new AgentError("refused", `these ids are held in ${signed.length} copies, more than one decision takes (${MAX_DECISION_ITEMS}): ask for fewer at once`); // A resume may lift a pause that holds nothing yet; anything else decides something held. if (items.length === 0 && req.verdict !== "resume") throw new AgentError("refused", "none of these ids is held on this device"); const yes = await desk.confirm(decisionView(req, items, peer), gone); // An asker that went away is no decline of the person's: it starts no wait. if (gone?.aborted === true) throw new AgentError("refused", "the process that asked went away before you answered: nothing was signed"); this.declined = yes ? { until: 0, ms: 0 } : this.nextBackoff(); if (!yes) throw new AgentError("refused", "you declined it in the signer: nothing changed"); const signKey = this.signKey; if (signKey === undefined) throw new AgentError("locked", "the signer was locked before it signed: nothing changed"); const at = this.o.now(); const body = { v: 1 as const, device: this.file.id, mesh: req.mesh, verdict: req.verdict, ...(req.peer === undefined ? {} : { peer: req.peer }), items: signed, at, n: ulid(at) }; return { decision: signDecision(body, signKey) }; } finally { this.deciding = false; } } /** The wait after one more decline: firstMs, then doubling, at most maxMs. */ private nextBackoff(): { until: number; ms: number } { const b = this.o.backoff ?? DECLINE_BACKOFF; const ms = Math.min(this.declined.ms === 0 ? b.firstMs : this.declined.ms * 2, b.maxMs); return { until: this.o.now() + ms, ms }; } /** A resume's peer: a session address whose device the mesh's roster names, never free text. */ private async resumed(desk: Desk, mesh: string, peer: string): Promise<{ session: string; device: string; id: string }> { if (!sessionAddressSchema.safeParse(peer).success) throw new AgentError("bad_request", "decide: a resume's peer is a session address (name@device)"); const at = peer.indexOf("@"); const device = peer.slice(at + 1); const id = await desk.device(mesh, device); if (id === undefined) throw new AgentError("refused", `no device named ${sanitizeForDisplay(device)} is in this mesh's roster: nothing to resume`); return { session: peer.slice(0, at), device, id }; } /** * Who it is and whether it is locked (HelloInfo), its own channel key, its run key's certificate, * and the run key's signature over the client's nonce, both channel keys and the HelloInfo; with * the channel (CHANNEL) for the server to use. The same locked or unlocked. An unlocked signer * whose certificate is half spent issues a new run key first. */ private hello(nonce: Uint8Array, clientPub: Uint8Array): Record { this.refresh(); const f = this.file; const info: HelloInfo = { id: f.id, name: f.name, signPub: f.signPub, boxPub: f.boxPub, locked: this.locked, pid: this.o.pid }; const mine = ephemeral(); const transcript = helloTranscript(nonce, clientPub, mine.pub, info); const channel = channelFor("signer", mine.privateKey, clientPub, transcript); return { ...info, epub: b64u.encode(mine.pub), cert: { ...this.run.body, sig: this.run.sig }, proof: b64u.encode(edSign(null, transcript, this.run.privateKey)), [CHANNEL]: channel }; } private keyOp(op: "sign" | "seal" | "open" | "open2" | "prekey" | "adopt", a: Record, k: Unlocked, signKey: KeyObject, entry: LogEntry): Record { const me = this.file.id; const refuse = (why: string): never => { throw new AgentError("refused", why); }; switch (op) { case "sign": { const type = a["type"] as string; const obj = a["obj"] as Record; entry.type = type; if (type === "prekey") refuse("prekeys are made by the signer itself (the prekey operation)"); const owner = OWNER[type]; if (owner === undefined) throw new AgentError("bad_request", `no signature type ${JSON.stringify(type)}`); if (owner(obj) !== me) refuse(`this ${type} object is not this device's`); if ((type === "auth" || type === "send") && !(typeof obj["at"] === "number" && Math.abs(obj["at"] - this.o.now()) <= SIGN_SKEW_MS)) { refuse(`this ${type} object's time is not within ${SIGN_SKEW_MS / 60_000} minutes of the signer's clock`); } entry.mesh = typeof obj["mesh"] === "string" ? obj["mesh"] : undefined; // The same bytes signObject signs: the type label, then the canonical object without sig. const { sig: _sig, ...rest } = obj; const message = Buffer.concat([Buffer.from(`wim/1 ${type}\n`, "utf8"), canonBytes(rest)]); return { sig: b64u.encode(edSign(null, message, signKey)) }; } case "seal": { const req = a as z.infer; Object.assign(entry, { mesh: req.mesh, peer: req.recipient }); if (req.sender !== me) refuse("a box from another device"); const box = sealBox(k.keys.boxSec, { mesh: req.mesh, sender: req.sender, recipient: req.recipient, aad: b64u.decode(req.aad), recipientBox: b64u.decode(req.recipientBox), plaintext: b64u.decode(req.plaintext), ...(req.prekey === undefined ? {} : { prekey: { id: req.prekey.id, pub: b64u.decode(req.prekey.pub) } }), }); return { box }; } case "open": case "open2": { const req = a as z.infer; Object.assign(entry, { mesh: req.mesh, peer: req.sender }); if (req.recipient !== me) refuse("a box to another device"); const open = { mesh: req.mesh, sender: req.sender, recipient: req.recipient, aad: b64u.decode(req.aad), senderBox: b64u.decode(req.senderBox), box: req.box }; if (op === "open") return opened(openBox(k.keys.boxSec, () => undefined, open)); return opened(this.openPrekeyBox(req.entry, open, k)); } case "prekey": { const mesh = a["mesh"] as string; entry.mesh = mesh; const at = a["at"] as number; // A prekey dated far ahead would sort first in the device's store and push current ones out. if (Math.abs(at - this.o.now()) > SIGN_SKEW_MS) refuse(`a prekey's time must be within ${SIGN_SKEW_MS / 60_000} minutes of the signer's clock`); const made = createPrekey({ mesh, deviceId: me, signSec: k.keys.signSec, at }); const sealed = sealPrekeySecret(k.data, made.prekey, made.secret); made.secret.fill(0); return { prekey: made.prekey, sealed }; } case "adopt": { const prekey = a["prekey"] as Prekey; entry.mesh = prekey.mesh; if (prekey.device !== me || !verifyObject("prekey", prekey, b64u.decode(this.file.signPub))) refuse("not one of this device's prekeys"); return { sealed: sealPrekeySecret(k.data, prekey, b64u.decode(a["secret"] as string)) }; } } } /** * A scheme 2 box, opened with a sealed prekey secret from `prekeys.json`. The signer keeps the * deletion schedule itself: past `until` + 72 hours a prekey is gone, even in a copy of the file. */ private openPrekeyBox(e: { prekey: Prekey; sealed: string }, req: Parameters[2], k: Unlocked): Opened { if (e.prekey.device !== this.file.id || prekeyDeleteAt(e.prekey) <= this.o.now()) return "key_expired"; if (!("s" in req.box) || req.box.p !== e.prekey.id) return "decrypt_failed"; const secret = openPrekeySecret(k.data, e.prekey, e.sealed); if (secret === undefined) return "decrypt_failed"; try { return openBox(undefined, () => secret, req); } finally { secret.fill(0); } } } function opened(o: Opened): Record { return typeof o === "string" ? { reason: o } : { opened: b64u.encode(o) }; } /** Whether a signer answers at `endpoint` now (a connect that succeeds). */ export function answers(endpoint: string): Promise { return new Promise((resolve) => { const socket = createConnection(endpoint); socket.once("connect", () => { socket.destroy(); resolve(true); }); socket.once("error", () => resolve(false)); }); } /** * The signer's socket. Each connection's requests are answered in order, one at a time; separate * connections run side by side. After an unlocked signer's `hello`, a connection's lines are * sealed on its channel. `stop` answers, then closes everything. * * The signer holds its endpoint's identity (POSIX: the socket file's inode) from the moment it * listens. If the path comes to name anything else (renamed away, or a process put its own socket * there), the signer locks and stops: checked before every request, and every `watchMs`. */ export class AgentServer { private server: Server | undefined; private readonly sockets = new Set(); private stopping = false; private identity: string | undefined; private watcher: NodeJS.Timeout | undefined; readonly stopped: Promise; private resolveStopped!: () => void; private readonly o: { signer: Signer; endpoint: string; files: EndpointFiles; watchMs?: number }; constructor(o: { signer: Signer; endpoint: string; files: EndpointFiles; watchMs?: number }) { this.o = o; this.stopped = new Promise((r) => (this.resolveStopped = r)); } /** * Listens. Refuses when a signer already answers there; a stale socket file is replaced. A * request that comes in before the endpoint's identity is recorded waits for it (`bound`), and * the listener is this server's from the start, so a close at any point closes it. */ async listen(): Promise { if (await answers(this.o.endpoint)) throw new AgentError("running", `a signer is already running at ${this.o.endpoint}`); await this.o.files.clear(this.o.endpoint); let markBound!: () => void; this.bound = new Promise((resolve) => (markBound = resolve)); const server = createServer((socket) => this.serve(socket)); this.server = server; await new Promise((resolve, reject) => { server.once("error", reject); server.listen(this.o.endpoint, () => resolve()); }); await this.o.files.secure(this.o.endpoint); this.identity = await this.o.files.id(this.o.endpoint); markBound(); this.watcher = setInterval(() => void this.watch(), this.o.watchMs ?? 2000); this.watcher.unref(); } /** Resolves once listen() has recorded the endpoint's identity. */ private bound: Promise = Promise.resolve(); /** Whether the endpoint still names this signer's own socket. */ private async owned(): Promise { return (await this.o.files.id(this.o.endpoint)) === this.identity; } /** Locks and stops when the endpoint is no longer this signer's own; stops when it is locked and its certificate ran out. */ async watch(): Promise { this.afterWatch(await this.owned()); } private afterWatch(owned: boolean): void { if (!owned) this.replacedNow(); else if (this.o.signer.spent) void this.close(); else this.o.signer.refresh(); } private replacedNow(): void { this.o.signer.replaced(); void this.close(); } private serve(socket: Socket): void { this.sockets.add(socket); socket.setEncoding("utf8"); let buffered = ""; let queue: Promise = Promise.resolve(); // `gone`: aborted when the connection closes, so a decision whose asker went away is never signed. const closed = new AbortController(); const state: { channel?: Channel; gone: AbortSignal } = { gone: closed.signal }; socket.on("error", () => undefined); socket.on("close", () => { this.sockets.delete(socket); closed.abort(); }); socket.on("data", (chunk: string) => { buffered += chunk; // A request past the limit ends the connection. if (buffered.length > MAX_LINE) { socket.destroy(); return; } for (let nl = buffered.indexOf("\n"); nl !== -1; nl = buffered.indexOf("\n")) { const line = buffered.slice(0, nl); buffered = buffered.slice(nl + 1); queue = queue.then(() => this.request(socket, line, state)); } }); } private async request(socket: Socket, line: string, state: { channel?: Channel; gone: AbortSignal }): Promise { await this.bound; return this.dispatch(socket, line, state, await this.owned()); } /** Someone else's socket at the path: nothing is answered, and the signer locks and stops. */ private dispatch(socket: Socket, line: string, state: { channel?: Channel; gone: AbortSignal }, owned: boolean): Promise { if (!owned) { socket.destroy(); this.replacedNow(); return Promise.resolve(); } return this.handleLine(line, state).then((handled) => this.answerLine(socket, state, handled)); } /** The answer to one line, or undefined for a sealed line that does not open (the connection then ends). */ private async handleLine(line: string, state: { channel?: Channel; gone: AbortSignal }): Promise<{ op: string; answer: Answer } | undefined> { const text = state.channel === undefined ? line : unseal(state.channel, line); if (text === undefined) return undefined; let i: unknown = 0; let op = ""; try { const req = JSON.parse(text) as { i?: unknown; op?: unknown; a?: unknown }; i = req.i; op = String(req.op); return { op, answer: { i, r: await this.o.signer.handle(op, req.a ?? {}, state.gone) } }; } catch (error) { return { op, answer: { i, e: { code: error instanceof AgentError ? error.code : "bad_request", message: (error as Error).message } } }; } } /** * Writes an answer (sealed on the channel when there is one), takes up the channel a `hello` * opened, and acts on `stop`. Kept synchronous: every branch after the awaits is here. */ private answerLine(socket: Socket, state: { channel?: Channel }, handled: { op: string; answer: Answer } | undefined): void { if (handled === undefined) { socket.destroy(); return; } const text = JSON.stringify(handled.answer); const out = `${state.channel === undefined ? text : JSON.stringify({ x: state.channel.seal(text) })}\n`; const opened = handled.answer.r?.[CHANNEL] as Channel | undefined; if (opened !== undefined) state.channel = opened; if (handled.op !== "stop" || handled.answer.r === undefined) { // A connection that went away meanwhile drops the answer (its error is ignored above). socket.write(out); return; } // Stopped listening before the answer goes out, so a client that has the answer finds the // endpoint closed (`wim signer unlock` relies on that to tell a signer from an impostor). The // answer is flushed, then this connection is closed like every other. this.server!.close(); socket.end(out, () => socket.destroy()); void this.close(socket); } /** * Stops listening, closes every connection (but `answering`, which closes itself once its * answer is out) and wipes the keys. It waits for no client: one that keeps its end of a * connection open, or a Windows pipe slow to report its close, does not keep it running. */ async close(answering?: Socket): Promise { if (this.stopping) return this.stopped; this.stopping = true; clearInterval(this.watcher); this.o.signer.lock(); for (const s of this.sockets) if (s !== answering) s.destroy(); await new Promise((resolve) => (this.server === undefined ? resolve() : this.server.close(() => resolve()))); this.resolveStopped(); } } type Answer = { i: unknown; r?: Record; e?: { code: string; message: string } };