UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

183 lines • 7.83 kB
/** * Roster entry kinds for hosted model-key custody (row CK11; custody design 2.2 and 3.1). * * A `device-kem` entry publishes a device's HPKE recipient key. A service entry (`custody`, current or * standby, and `placement-registry`, optionally superseding an earlier registry generation) publishes a * KMS-held key of the Custody Service. A service entry is usable by a device only after THAT device ran * `checkServiceKey` on it and stored a `LocalKeyVerification` record (`verified-keys.ts`); * attestations other devices posted are informational and never make an entry valid. * * Everything here is a parser or a pure check over untrusted input: parsers return `undefined` for any * shape they do not recognise, and verification returns `false` instead of throwing. */ import { canonicalJson } from "@earendil-works/pi-agent-core"; import { verifyEcdsaP256Sha256 } from "./ecdsa-p256.js"; const UNKNOWN_ROSTER = { state: "unknown" }; /** * The view as of wall-clock `now` and monotonic `nowMono`: `unknown` at the earlier of the roster's lifetime * elapsed on the monotonic clock since receipt and `max(now, iat) >= exp` on the wall clock (15.15 H-1, 15.16 M-1). */ export function rosterViewAt(view, now, nowMono) { if (view.state === "unknown") return view; const effectiveNow = Math.max(now, view.rosterIssuedAt); const age = nowMono - view.receivedMono; const lifetime = view.rosterExpiresAt - view.rosterIssuedAt; return effectiveNow >= view.rosterExpiresAt || age < 0 || age >= lifetime ? UNKNOWN_ROSTER : view; } /** The view to hold after verifying `incoming`: it replaces `held` only when strictly newer (15.16 M-2). */ export function heldRosterView(held, incoming) { if (incoming.state === "unknown" || held.state === "unknown") return incoming.state === "unknown" ? held : incoming; const newer = incoming.rosterVersion > held.rosterVersion || (incoming.rosterVersion === held.rosterVersion && incoming.rosterIssuedAt > held.rosterIssuedAt); return newer ? incoming : held; } function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function isNonEmptyString(value) { return typeof value === "string" && value.length > 0; } function isNonNegativeInteger(value) { return typeof value === "number" && Number.isSafeInteger(value) && value >= 0; } function hasOnlyKeys(value, allowed) { return Object.keys(value).every((key) => allowed.includes(key)); } const SERVICE_SIG_KEYS = ["alg", "keyId", "sig"]; const ATTESTATION_KEYS = ["deviceId", "checkedAt", "sig"]; const DEVICE_KEM_KEYS = ["kind", "deviceId", "kemPk", "sig"]; const SERVICE_ENTRY_KEYS = [ "kind", "accountId", "generation", "kemKeyId", "kemPk", "sigKeyId", "sigPk", "kmsKeyArns", "templateVersion", "role", "supersedes", "createdAt", "serviceSig", "attestations", ]; function parseServiceSig(value) { if (!isRecord(value) || !hasOnlyKeys(value, SERVICE_SIG_KEYS)) return undefined; if (value.alg !== "ECDSA-P256-SHA256" || !isNonEmptyString(value.keyId) || !isNonEmptyString(value.sig)) return undefined; return { alg: value.alg, keyId: value.keyId, sig: value.sig }; } function parseAttestation(value) { if (!isRecord(value) || !hasOnlyKeys(value, ATTESTATION_KEYS)) return undefined; if (!isNonEmptyString(value.deviceId) || !isNonNegativeInteger(value.checkedAt) || !isNonEmptyString(value.sig)) { return undefined; } return { deviceId: value.deviceId, checkedAt: value.checkedAt, sig: value.sig }; } export function parseDeviceKemEntry(value) { if (!isRecord(value) || !hasOnlyKeys(value, DEVICE_KEM_KEYS) || value.kind !== "device-kem") return undefined; if (!isNonEmptyString(value.deviceId) || !isNonEmptyString(value.kemPk) || !isNonEmptyString(value.sig)) return undefined; return { kind: "device-kem", deviceId: value.deviceId, kemPk: value.kemPk, sig: value.sig }; } /** * Strict parser for a service key entry. Kind-specific shape is enforced here so later checks never see * a custody entry without a KEM key, a registry entry carrying one, or `supersedes` on a custody entry. */ export function parseServiceKeyEntry(value) { if (!isRecord(value) || !hasOnlyKeys(value, SERVICE_ENTRY_KEYS)) return undefined; const { kind, role } = value; if (kind !== "custody" && kind !== "placement-registry") return undefined; if (role !== "current" && role !== "standby") return undefined; if (!isNonEmptyString(value.accountId) || !isNonNegativeInteger(value.generation)) return undefined; if (!isNonEmptyString(value.sigKeyId) || !isNonEmptyString(value.sigPk)) return undefined; if (!isNonNegativeInteger(value.templateVersion) || !isNonNegativeInteger(value.createdAt)) return undefined; if (!Array.isArray(value.kmsKeyArns) || !value.kmsKeyArns.every(isNonEmptyString)) return undefined; const serviceSig = parseServiceSig(value.serviceSig); if (!serviceSig || !Array.isArray(value.attestations)) return undefined; const attestations = []; for (const raw of value.attestations) { const attestation = parseAttestation(raw); if (!attestation) return undefined; attestations.push(attestation); } const { kemKeyId, kemPk, supersedes } = value; let kem; if (kind === "custody") { if (!isNonEmptyString(kemKeyId) || !isNonEmptyString(kemPk) || supersedes !== undefined) return undefined; kem = { kemKeyId, kemPk }; } else { if (kemKeyId !== undefined || kemPk !== undefined) return undefined; if (supersedes !== undefined && !isNonEmptyString(supersedes)) return undefined; } return { kind, accountId: value.accountId, generation: value.generation, ...kem, sigKeyId: value.sigKeyId, sigPk: value.sigPk, kmsKeyArns: [...value.kmsKeyArns], templateVersion: value.templateVersion, role, ...(typeof supersedes === "string" ? { supersedes } : {}), createdAt: value.createdAt, serviceSig, attestations, }; } /** * The bytes `serviceSig` covers: the canonical JSON of every field except `serviceSig` itself and the * informational `attestations` (devices append those after signing). Absent optional fields are omitted, * never serialized as null. */ export function serviceEntrySigningPayload(entry) { const signed = { kind: entry.kind, accountId: entry.accountId, generation: entry.generation, sigKeyId: entry.sigKeyId, sigPk: entry.sigPk, kmsKeyArns: [...entry.kmsKeyArns], templateVersion: entry.templateVersion, role: entry.role, createdAt: entry.createdAt, }; if (entry.kemKeyId !== undefined) signed.kemKeyId = entry.kemKeyId; if (entry.kemPk !== undefined) signed.kemPk = entry.kemPk; if (entry.supersedes !== undefined) signed.supersedes = entry.supersedes; return new TextEncoder().encode(canonicalJson(signed)); } /** * `serviceSig` is a self-signature: made with the entry's own signing key (`keyId === sigKeyId`) over * `serviceEntrySigningPayload`. It proves the entry's fields were signed by that KMS key; whether the key * itself is trustworthy is decided only by this device's own `checkServiceKey` record. */ export async function verifyServiceSig(entry) { if (entry.serviceSig.keyId !== entry.sigKeyId) return false; return verifyEcdsaP256Sha256(entry.sigPk, serviceEntrySigningPayload(entry), entry.serviceSig.sig); } //# sourceMappingURL=roster-entries.js.map