@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
79 lines • 3.44 kB
JavaScript
/**
* Verifier-signed statements: the trust anchor of the key-facts channel (custody design 15.10).
*
* The verifier serves `{ "payload": <base64url of the exact signed bytes>, "sig": <base64url P1363> }`, signed
* under `"omo-key-facts/v1\n" || bytes` by a verifier key this release pins. The client verifies the bytes it
* received BEFORE parsing them, then parses strictly: the bytes must be exactly their canonical JSON (sorted keys,
* no whitespace), so duplicate keys and trailing bytes are refused. TLS and the host CA store carry the bytes and
* are trusted for nothing.
*/
import { canonicalJson, toStoreValue } from "@earendil-works/pi-agent-core";
import { verifyP1363Sha256 } from "./ecdsa-p256.js";
const STATEMENT_DOMAIN = "omo-key-facts/v1\n";
const BASE64URL = /^[A-Za-z0-9_-]*$/;
export function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
/** Strict unpadded base64url: one accepted encoding per byte string. */
function decodeBase64Url(value) {
if (!BASE64URL.test(value))
return undefined;
const bytes = new Uint8Array(Buffer.from(value, "base64url"));
return Buffer.from(bytes).toString("base64url") === value ? bytes : undefined;
}
function parseEnvelope(body) {
let value;
try {
value = JSON.parse(body);
}
catch {
return undefined;
}
if (!isRecord(value) || Object.keys(value).length !== 2)
return undefined;
const { payload, sig } = value;
return typeof payload === "string" && typeof sig === "string" ? { payload, sig } : undefined;
}
function parseCanonical(bytes) {
let text;
let value;
try {
// `ignoreBOM: true` keeps a leading byte-order mark in the text instead of stripping it, so signed bytes
// that are EF BB BF + canonical JSON fail the equality below: one encoding per byte string (senpi#5961).
text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
value = JSON.parse(text);
}
catch {
return undefined;
}
return isRecord(value) && canonicalJson(toStoreValue(value)) === text ? value : undefined;
}
/**
* The statement in `body` if a pinned key that `canSign` admits signed exactly its payload bytes and the
* statement names that key as its `verifierKeyId`; `undefined` on any failure.
*/
export async function verifySignedStatement(body, pins, canSign) {
const envelope = parseEnvelope(body);
const bytes = envelope && decodeBase64Url(envelope.payload);
const signature = envelope && decodeBase64Url(envelope.sig);
if (!bytes || !signature)
return undefined;
const message = new Uint8Array([...new TextEncoder().encode(STATEMENT_DOMAIN), ...bytes]);
let signer;
for (const pin of pins) {
if (canSign(pin) && (await verifyP1363Sha256(pin.spki, message, signature))) {
signer = pin;
break;
}
}
if (!signer)
return undefined;
const fields = parseCanonical(bytes);
return fields && fields.verifierKeyId === signer.keyId && fields.v === 1 ? { signer, fields } : undefined;
}
export function hasExactKeys(record, required, optional = []) {
const keys = Object.keys(record);
return (required.every((key) => keys.includes(key)) &&
keys.every((key) => required.includes(key) || optional.includes(key)));
}
//# sourceMappingURL=signed-statement.js.map