@verax-ai/proxy 0.4.2 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/anchors.d.ts +24 -0
- package/dist/anchors.js +86 -0
- package/dist/control-sync.js +15 -1
- package/dist/explain.d.ts +3 -0
- package/dist/explain.js +39 -5
- package/dist/index.d.ts +9 -3
- package/dist/index.js +5 -1
- package/dist/ledger.d.ts +3 -3
- package/dist/ledger.js +20 -12
- package/dist/proxy.js +37 -16
- package/dist/revoke-sync.d.ts +26 -0
- package/dist/revoke-sync.js +176 -0
- package/dist/third-party.d.ts +7 -0
- package/dist/third-party.js +53 -0
- package/dist/transparency-receipt.d.ts +38 -0
- package/dist/transparency-receipt.js +277 -0
- package/dist/types.d.ts +43 -2
- package/dist/verify-ledger.d.ts +33 -0
- package/dist/verify-ledger.js +337 -13
- package/package.json +1 -1
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { signDecisionRecord } from "@cedulon/core";
|
|
4
|
+
import { sha256Canonical } from "./hash.js";
|
|
5
|
+
/**
|
|
6
|
+
* Same file as `packages/body/src/revoke.ts` and the issuer's `POST /revoke`.
|
|
7
|
+
* The file stays the switch: the body refuses a listed jti whether or not the
|
|
8
|
+
* ledger has caught up. This module keeps the ledger's account of that list.
|
|
9
|
+
*
|
|
10
|
+
* The ledger's list is replayed from its `verax.revoke` records. A jti in the
|
|
11
|
+
* file and not in that list is one `token-revoked` record; a jti in that list
|
|
12
|
+
* and gone from the file is one `revoke-removed` record, because removing a
|
|
13
|
+
* line lets that token in again and nothing else would say so. Both are
|
|
14
|
+
* denies with no effect: a revocation refuses, it does not let a call through,
|
|
15
|
+
* and a verifier that predates these records reads them as ordinary denies.
|
|
16
|
+
*
|
|
17
|
+
* Like a CLI halt, a revocation is not in the ledger until the body opens,
|
|
18
|
+
* the halt doors sync, or the next call reaches the proxy. A call made with
|
|
19
|
+
* the revoked token is refused before the proxy and does not sync.
|
|
20
|
+
*/
|
|
21
|
+
const REVOKED = "revoked-jti.jsonl";
|
|
22
|
+
const SUBJECT = "verax.revoke";
|
|
23
|
+
const CONTROL_EFFECT = "verax.control";
|
|
24
|
+
const MAX_JTI = 256;
|
|
25
|
+
const MAX_BY = 256;
|
|
26
|
+
export function createRevokeMemory() {
|
|
27
|
+
return { loaded: false, stamp: null, revoked: new Set() };
|
|
28
|
+
}
|
|
29
|
+
export function revokeUnchanged(stateDir, memory) {
|
|
30
|
+
return memory.loaded && sameStamp(memory.stamp, fileStamp(stateDir));
|
|
31
|
+
}
|
|
32
|
+
export async function syncRevokeControl(opts) {
|
|
33
|
+
const memory = opts.memory;
|
|
34
|
+
if (revokeUnchanged(opts.stateDir, memory))
|
|
35
|
+
return;
|
|
36
|
+
let stamp = fileStamp(opts.stateDir);
|
|
37
|
+
let listed = [];
|
|
38
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
39
|
+
listed = readListed(opts.stateDir);
|
|
40
|
+
const after = fileStamp(opts.stateDir);
|
|
41
|
+
if (sameStamp(stamp, after))
|
|
42
|
+
break;
|
|
43
|
+
stamp = after;
|
|
44
|
+
}
|
|
45
|
+
if (!memory.loaded) {
|
|
46
|
+
memory.revoked = await replay(opts);
|
|
47
|
+
memory.loaded = true;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const inFile = new Set();
|
|
51
|
+
for (const row of listed) {
|
|
52
|
+
if (inFile.has(row.jti))
|
|
53
|
+
continue;
|
|
54
|
+
inFile.add(row.jti);
|
|
55
|
+
if (memory.revoked.has(row.jti))
|
|
56
|
+
continue;
|
|
57
|
+
await writeRevoke(opts, {
|
|
58
|
+
action: "revoke",
|
|
59
|
+
jti: row.jti,
|
|
60
|
+
by: row.by,
|
|
61
|
+
via: row.via,
|
|
62
|
+
atMs: row.atMs ?? opts.now(),
|
|
63
|
+
});
|
|
64
|
+
memory.revoked.add(row.jti);
|
|
65
|
+
}
|
|
66
|
+
for (const jti of [...memory.revoked]) {
|
|
67
|
+
if (inFile.has(jti))
|
|
68
|
+
continue;
|
|
69
|
+
await writeRevoke(opts, { action: "unrevoke", jti, by: "unknown", via: "file", atMs: opts.now() });
|
|
70
|
+
memory.revoked.delete(jti);
|
|
71
|
+
}
|
|
72
|
+
memory.stamp = stamp;
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
// A partial append is already on disk. Replay the ledger next time rather
|
|
76
|
+
// than trust a list that may be ahead of it or behind it.
|
|
77
|
+
memory.loaded = false;
|
|
78
|
+
throw err;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function fileStamp(stateDir) {
|
|
82
|
+
try {
|
|
83
|
+
const st = statSync(join(stateDir, REVOKED));
|
|
84
|
+
return { size: st.size, mtimeMs: st.mtimeMs };
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
if (err.code === "ENOENT")
|
|
88
|
+
return null;
|
|
89
|
+
throw err;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function sameStamp(a, b) {
|
|
93
|
+
if (a === null || b === null)
|
|
94
|
+
return a === b;
|
|
95
|
+
return a.size === b.size && a.mtimeMs === b.mtimeMs;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Lines that name no usable jti are skipped here. The body's own check reads
|
|
99
|
+
* the same file and decides what such a line does to a token; this list only
|
|
100
|
+
* records jtis it can name.
|
|
101
|
+
*/
|
|
102
|
+
function readListed(stateDir) {
|
|
103
|
+
let text;
|
|
104
|
+
try {
|
|
105
|
+
text = readFileSync(join(stateDir, REVOKED), "utf8");
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
if (err.code === "ENOENT")
|
|
109
|
+
return [];
|
|
110
|
+
throw err;
|
|
111
|
+
}
|
|
112
|
+
const out = [];
|
|
113
|
+
for (const raw of text.split("\n")) {
|
|
114
|
+
const line = raw.replace(/\r$/, "");
|
|
115
|
+
if (line.trim() === "")
|
|
116
|
+
continue;
|
|
117
|
+
let row;
|
|
118
|
+
try {
|
|
119
|
+
row = JSON.parse(line);
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (!row || typeof row !== "object")
|
|
125
|
+
continue;
|
|
126
|
+
if (typeof row.jti !== "string" || row.jti === "" || row.jti.length > MAX_JTI)
|
|
127
|
+
continue;
|
|
128
|
+
const named = typeof row.by === "string" && row.by !== "" && row.by.length <= MAX_BY;
|
|
129
|
+
const atMs = typeof row.atMs === "number" && Number.isFinite(row.atMs) ? row.atMs : null;
|
|
130
|
+
out.push({ jti: row.jti, by: named ? row.by : "unknown", via: named ? "http" : "file", atMs });
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
async function replay(opts) {
|
|
135
|
+
const revoked = new Set();
|
|
136
|
+
for (const decision of await opts.decisions()) {
|
|
137
|
+
if (decision.claims.subject !== SUBJECT || decision.claims.effectClass !== CONTROL_EFFECT)
|
|
138
|
+
continue;
|
|
139
|
+
const ref = decision.claims.ref;
|
|
140
|
+
if (typeof ref !== "string")
|
|
141
|
+
continue;
|
|
142
|
+
const row = (await opts.inputsLog.get(ref))?.revoke;
|
|
143
|
+
if (!row || typeof row.jti !== "string")
|
|
144
|
+
continue;
|
|
145
|
+
if (row.action === "revoke")
|
|
146
|
+
revoked.add(row.jti);
|
|
147
|
+
else if (row.action === "unrevoke")
|
|
148
|
+
revoked.delete(row.jti);
|
|
149
|
+
}
|
|
150
|
+
return revoked;
|
|
151
|
+
}
|
|
152
|
+
async function writeRevoke(opts, revoke) {
|
|
153
|
+
const ref = opts.nonce();
|
|
154
|
+
const inputs = {
|
|
155
|
+
principal: { brain: "verax-proxy", scopes: [] },
|
|
156
|
+
inputs: [],
|
|
157
|
+
revoke,
|
|
158
|
+
};
|
|
159
|
+
const inputsHash = sha256Canonical(inputs);
|
|
160
|
+
await opts.inputsLog.append(ref, inputs);
|
|
161
|
+
await opts.writer.appendDecisionChained((prevRecordHash) => signDecisionRecord({
|
|
162
|
+
decider: "verax-proxy",
|
|
163
|
+
subject: SUBJECT,
|
|
164
|
+
requestHash: sha256Canonical(revoke),
|
|
165
|
+
policyHash: opts.policyHash,
|
|
166
|
+
inputsHash,
|
|
167
|
+
decision: "deny",
|
|
168
|
+
reasonCode: revoke.action === "revoke" ? "token-revoked" : "revoke-removed",
|
|
169
|
+
ref,
|
|
170
|
+
effectHash: null,
|
|
171
|
+
effectClass: CONTROL_EFFECT,
|
|
172
|
+
timestampMs: revoke.atMs,
|
|
173
|
+
nonce: ref,
|
|
174
|
+
prevRecordHash,
|
|
175
|
+
}, opts.recordSigner.privateKeyPem, opts.recordSigner.publicKeyPem));
|
|
176
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type SignedEffectExtract } from "@cedulon/effect-extract";
|
|
2
|
+
/** Compare key material, not PEM whitespace or line endings. */
|
|
3
|
+
export declare function samePublicKey(a: string | null | undefined, b: string | null | undefined): boolean;
|
|
4
|
+
/** External receipts are meaningful only under a reader-supplied pin. */
|
|
5
|
+
export declare function thirdPartyReceiptCoversRow(receipt: SignedEffectExtract | undefined, row: unknown, key: string): boolean;
|
|
6
|
+
/** A valid signature must also describe this dispatch, not another call. */
|
|
7
|
+
export declare function thirdPartyReceiptRejection(value: unknown, key: string, ref: string | undefined, effectClass: string, effectHash: string): string | null;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createPublicKey } from "node:crypto";
|
|
2
|
+
import { canonical } from "@cedulon/core";
|
|
3
|
+
import { verifyEffectExtract } from "@cedulon/effect-extract";
|
|
4
|
+
/** Compare key material, not PEM whitespace or line endings. */
|
|
5
|
+
export function samePublicKey(a, b) {
|
|
6
|
+
if (!a || !b)
|
|
7
|
+
return false;
|
|
8
|
+
try {
|
|
9
|
+
return createPublicKey(a).export({ type: "spki", format: "der" })
|
|
10
|
+
.equals(createPublicKey(b).export({ type: "spki", format: "der" }));
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** External receipts are meaningful only under a reader-supplied pin. */
|
|
17
|
+
export function thirdPartyReceiptCoversRow(receipt, row, key) {
|
|
18
|
+
try {
|
|
19
|
+
return !!receipt && createPublicKey(key).asymmetricKeyType === "ed25519" &&
|
|
20
|
+
verifyEffectExtract(receipt, key) && receipt.body.effects.length === 1 &&
|
|
21
|
+
canonical(receipt.body.effects[0]) === canonical(row);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** A valid signature must also describe this dispatch, not another call. */
|
|
28
|
+
export function thirdPartyReceiptRejection(value, key, ref, effectClass, effectHash) {
|
|
29
|
+
const receipt = value;
|
|
30
|
+
try {
|
|
31
|
+
if (!receipt || !verifyEffectExtract(receipt, key))
|
|
32
|
+
return "signature";
|
|
33
|
+
if (receipt.body.effects.length !== 1)
|
|
34
|
+
return "effect-count";
|
|
35
|
+
const row = receipt.body.effects[0];
|
|
36
|
+
if (!ref || row.ref !== ref)
|
|
37
|
+
return "ref";
|
|
38
|
+
if (row.effectClass !== effectClass)
|
|
39
|
+
return "effect-class";
|
|
40
|
+
if (row.effectHash !== effectHash)
|
|
41
|
+
return "effect-hash";
|
|
42
|
+
if (receipt.body.deciderId !== "verax-proxy")
|
|
43
|
+
return "decider";
|
|
44
|
+
if (receipt.body.windowStartMs !== row.timestampMs)
|
|
45
|
+
return "window-start";
|
|
46
|
+
if (receipt.body.windowEndMs !== row.timestampMs + 1)
|
|
47
|
+
return "window-end";
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return "malformed-receipt";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export declare class CborTag {
|
|
2
|
+
readonly tag: number;
|
|
3
|
+
readonly value: unknown;
|
|
4
|
+
constructor(tag: number, value: unknown);
|
|
5
|
+
}
|
|
6
|
+
/** RFC 8949 definite-length items; floats and indefinite lengths are refused. */
|
|
7
|
+
export declare function decodeCborItem(bytes: Uint8Array): unknown;
|
|
8
|
+
/** RFC 9052 4.4: ["Signature1", body_protected, external_aad = h'', payload], CBOR-encoded. */
|
|
9
|
+
export declare function sign1Structure(protectedBytes: Uint8Array, payload: Uint8Array): Buffer;
|
|
10
|
+
export type Sign1 = {
|
|
11
|
+
protectedBytes: Uint8Array;
|
|
12
|
+
protectedHeader: Map<unknown, unknown>;
|
|
13
|
+
unprotected: Map<unknown, unknown>;
|
|
14
|
+
payload: Uint8Array | null;
|
|
15
|
+
signature: Uint8Array;
|
|
16
|
+
};
|
|
17
|
+
/** A COSE_Sign1, tagged 18 or untagged. */
|
|
18
|
+
export declare function decodeSign1(bytes: Uint8Array): Sign1;
|
|
19
|
+
/** COSE algorithms a receipt may use: EdDSA/Ed25519 (-8, -19), ES256 (-7), ES384 (-35). */
|
|
20
|
+
export declare function verifyCoseSignature(alg: unknown, publicKeyPem: string, data: Buffer, signature: Uint8Array): boolean;
|
|
21
|
+
/** RFC 9162 2.1.3.2: the root an inclusion proof yields for a leaf entry. */
|
|
22
|
+
export declare function rootFromInclusion(leafEntry: Uint8Array, leafIndex: number, treeSize: number, path: readonly Uint8Array[]): Buffer;
|
|
23
|
+
export type ReceiptStage = "receipt-decode" | "vds-gate" | "inclusion" | "receipt-sig";
|
|
24
|
+
export type ReceiptCheck = {
|
|
25
|
+
ok: true;
|
|
26
|
+
root: string;
|
|
27
|
+
treeSize: number;
|
|
28
|
+
leafIndex: number;
|
|
29
|
+
} | {
|
|
30
|
+
ok: false;
|
|
31
|
+
stage: ReceiptStage;
|
|
32
|
+
reason: string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Checks one receipt for one leaf entry under the service key the reader
|
|
36
|
+
* holds. vds is read from the protected header only.
|
|
37
|
+
*/
|
|
38
|
+
export declare function verifyReceipt(receipt: Uint8Array, leafEntry: Uint8Array, publicKeyPem: string): ReceiptCheck;
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* COSE Receipts from a SCITT Transparency Service (RFC 9943) over an RFC 9162
|
|
3
|
+
* SHA-256 Merkle tree, read without the service.
|
|
4
|
+
*
|
|
5
|
+
* A receipt is a COSE_Sign1, normally tagged 18, whose protected header names
|
|
6
|
+
* the verifiable data structure at label 395 (1 is RFC9162_SHA256) and whose
|
|
7
|
+
* unprotected header carries, at label 396 key -1, one inclusion proof: the
|
|
8
|
+
* CBOR array [tree_size, leaf_index, audit_path]. The payload is detached: it
|
|
9
|
+
* is the tree root, which the verifier rebuilds from the leaf entry and the
|
|
10
|
+
* path and then checks the signature over. A receipt carries no key id, so the
|
|
11
|
+
* service key is always the reader's to supply.
|
|
12
|
+
*
|
|
13
|
+
* This is a second CBOR reader beside @cedulon/cose on purpose. That one reads
|
|
14
|
+
* Cedulon's own objects and refuses tags, floats and non-empty unprotected
|
|
15
|
+
* headers, which is right for them; a receipt written by someone else's
|
|
16
|
+
* service uses all three.
|
|
17
|
+
*/
|
|
18
|
+
import { createHash, createPublicKey, verify as verifySignature } from "node:crypto";
|
|
19
|
+
export class CborTag {
|
|
20
|
+
tag;
|
|
21
|
+
value;
|
|
22
|
+
constructor(tag, value) {
|
|
23
|
+
this.tag = tag;
|
|
24
|
+
this.value = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const MAX_BYTES = 65_536;
|
|
28
|
+
const MAX_DEPTH = 16;
|
|
29
|
+
const MAX_ITEMS = 4_096;
|
|
30
|
+
/** RFC 8949 definite-length items; floats and indefinite lengths are refused. */
|
|
31
|
+
export function decodeCborItem(bytes) {
|
|
32
|
+
if (bytes.length > MAX_BYTES)
|
|
33
|
+
throw new Error("cbor-too-large");
|
|
34
|
+
let at = 0;
|
|
35
|
+
let items = 0;
|
|
36
|
+
const need = (n) => {
|
|
37
|
+
if (at + n > bytes.length)
|
|
38
|
+
throw new Error("cbor-eof");
|
|
39
|
+
};
|
|
40
|
+
const length = (ai) => {
|
|
41
|
+
if (ai < 24)
|
|
42
|
+
return ai;
|
|
43
|
+
const size = ai === 24 ? 1 : ai === 25 ? 2 : ai === 26 ? 4 : ai === 27 ? 8 : 0;
|
|
44
|
+
if (size === 0)
|
|
45
|
+
throw new Error("cbor-indefinite-or-reserved");
|
|
46
|
+
need(size);
|
|
47
|
+
let n = 0n;
|
|
48
|
+
for (let i = 0; i < size; i += 1)
|
|
49
|
+
n = (n << 8n) | BigInt(bytes[at + i]);
|
|
50
|
+
at += size;
|
|
51
|
+
if (n > BigInt(Number.MAX_SAFE_INTEGER))
|
|
52
|
+
throw new Error("cbor-int-too-large");
|
|
53
|
+
return Number(n);
|
|
54
|
+
};
|
|
55
|
+
const item = (depth) => {
|
|
56
|
+
if (depth > MAX_DEPTH)
|
|
57
|
+
throw new Error("cbor-too-deep");
|
|
58
|
+
items += 1;
|
|
59
|
+
if (items > MAX_ITEMS)
|
|
60
|
+
throw new Error("cbor-too-many-items");
|
|
61
|
+
need(1);
|
|
62
|
+
const ib = bytes[at];
|
|
63
|
+
at += 1;
|
|
64
|
+
const major = ib >> 5;
|
|
65
|
+
const ai = ib & 31;
|
|
66
|
+
if (major === 7) {
|
|
67
|
+
if (ai === 20)
|
|
68
|
+
return false;
|
|
69
|
+
if (ai === 21)
|
|
70
|
+
return true;
|
|
71
|
+
if (ai === 22)
|
|
72
|
+
return null;
|
|
73
|
+
throw new Error("cbor-simple-or-float");
|
|
74
|
+
}
|
|
75
|
+
const n = length(ai);
|
|
76
|
+
switch (major) {
|
|
77
|
+
case 0:
|
|
78
|
+
return n;
|
|
79
|
+
case 1:
|
|
80
|
+
return -1 - n;
|
|
81
|
+
case 2:
|
|
82
|
+
need(n);
|
|
83
|
+
at += n;
|
|
84
|
+
return bytes.slice(at - n, at);
|
|
85
|
+
case 3:
|
|
86
|
+
need(n);
|
|
87
|
+
at += n;
|
|
88
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes.slice(at - n, at));
|
|
89
|
+
case 4: {
|
|
90
|
+
if (n > MAX_ITEMS)
|
|
91
|
+
throw new Error("cbor-too-many-items");
|
|
92
|
+
const out = [];
|
|
93
|
+
for (let i = 0; i < n; i += 1)
|
|
94
|
+
out.push(item(depth + 1));
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
case 5: {
|
|
98
|
+
if (n > MAX_ITEMS)
|
|
99
|
+
throw new Error("cbor-too-many-items");
|
|
100
|
+
const out = new Map();
|
|
101
|
+
for (let i = 0; i < n; i += 1) {
|
|
102
|
+
const key = item(depth + 1);
|
|
103
|
+
if (key instanceof Uint8Array || key instanceof Map || Array.isArray(key))
|
|
104
|
+
throw new Error("cbor-key-type");
|
|
105
|
+
if (out.has(key))
|
|
106
|
+
throw new Error("cbor-duplicate-key");
|
|
107
|
+
out.set(key, item(depth + 1));
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
case 6:
|
|
112
|
+
return new CborTag(n, item(depth + 1));
|
|
113
|
+
default:
|
|
114
|
+
throw new Error("cbor-major");
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const value = item(0);
|
|
118
|
+
if (at !== bytes.length)
|
|
119
|
+
throw new Error("cbor-trailing");
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
function head(major, n) {
|
|
123
|
+
if (n < 24)
|
|
124
|
+
return Buffer.of((major << 5) | n);
|
|
125
|
+
if (n < 0x100)
|
|
126
|
+
return Buffer.of((major << 5) | 24, n);
|
|
127
|
+
if (n < 0x10000)
|
|
128
|
+
return Buffer.of((major << 5) | 25, n >> 8, n & 0xff);
|
|
129
|
+
const b = Buffer.alloc(5);
|
|
130
|
+
b[0] = (major << 5) | 26;
|
|
131
|
+
b.writeUInt32BE(n, 1);
|
|
132
|
+
return b;
|
|
133
|
+
}
|
|
134
|
+
/** RFC 9052 4.4: ["Signature1", body_protected, external_aad = h'', payload], CBOR-encoded. */
|
|
135
|
+
export function sign1Structure(protectedBytes, payload) {
|
|
136
|
+
const text = Buffer.from("Signature1", "utf8");
|
|
137
|
+
return Buffer.concat([
|
|
138
|
+
head(4, 4),
|
|
139
|
+
head(3, text.length),
|
|
140
|
+
text,
|
|
141
|
+
head(2, protectedBytes.length),
|
|
142
|
+
protectedBytes,
|
|
143
|
+
head(2, 0),
|
|
144
|
+
head(2, payload.length),
|
|
145
|
+
payload,
|
|
146
|
+
]);
|
|
147
|
+
}
|
|
148
|
+
/** A COSE_Sign1, tagged 18 or untagged. */
|
|
149
|
+
export function decodeSign1(bytes) {
|
|
150
|
+
let value = decodeCborItem(bytes);
|
|
151
|
+
if (value instanceof CborTag) {
|
|
152
|
+
if (value.tag !== 18)
|
|
153
|
+
throw new Error(`cose-tag-${value.tag}`);
|
|
154
|
+
value = value.value;
|
|
155
|
+
}
|
|
156
|
+
if (!Array.isArray(value) || value.length !== 4)
|
|
157
|
+
throw new Error("cose-sign1-shape");
|
|
158
|
+
const [p, u, payload, signature] = value;
|
|
159
|
+
if (!(p instanceof Uint8Array) || !(u instanceof Map) || !(signature instanceof Uint8Array)) {
|
|
160
|
+
throw new Error("cose-sign1-members");
|
|
161
|
+
}
|
|
162
|
+
if (payload !== null && !(payload instanceof Uint8Array))
|
|
163
|
+
throw new Error("cose-sign1-payload");
|
|
164
|
+
const protectedHeader = p.length === 0 ? new Map() : decodeCborItem(p);
|
|
165
|
+
if (!(protectedHeader instanceof Map))
|
|
166
|
+
throw new Error("cose-protected-header");
|
|
167
|
+
return { protectedBytes: p, protectedHeader, unprotected: u, payload, signature };
|
|
168
|
+
}
|
|
169
|
+
/** COSE algorithms a receipt may use: EdDSA/Ed25519 (-8, -19), ES256 (-7), ES384 (-35). */
|
|
170
|
+
export function verifyCoseSignature(alg, publicKeyPem, data, signature) {
|
|
171
|
+
const key = createPublicKey(publicKeyPem);
|
|
172
|
+
if (alg === -8 || alg === -19) {
|
|
173
|
+
return key.asymmetricKeyType === "ed25519" && verifySignature(null, data, key, signature);
|
|
174
|
+
}
|
|
175
|
+
const curve = key.asymmetricKeyType === "ec" ? key.asymmetricKeyDetails?.namedCurve : undefined;
|
|
176
|
+
if (alg === -7) {
|
|
177
|
+
return curve === "prime256v1" && verifySignature("sha256", data, { key, dsaEncoding: "ieee-p1363" }, signature);
|
|
178
|
+
}
|
|
179
|
+
if (alg === -35) {
|
|
180
|
+
return curve === "secp384r1" && verifySignature("sha384", data, { key, dsaEncoding: "ieee-p1363" }, signature);
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
const sha256 = (...parts) => createHash("sha256").update(Buffer.concat(parts)).digest();
|
|
185
|
+
/** RFC 9162 2.1.3.2: the root an inclusion proof yields for a leaf entry. */
|
|
186
|
+
export function rootFromInclusion(leafEntry, leafIndex, treeSize, path) {
|
|
187
|
+
if (!Number.isSafeInteger(leafIndex) || !Number.isSafeInteger(treeSize) || leafIndex < 0 || leafIndex >= treeSize) {
|
|
188
|
+
throw new Error("inclusion-index");
|
|
189
|
+
}
|
|
190
|
+
let fn = leafIndex;
|
|
191
|
+
let sn = treeSize - 1;
|
|
192
|
+
// RFC 9162 2.1.1: a leaf hashes as SHA-256(0x00 || entry), an interior node as SHA-256(0x01 || left || right).
|
|
193
|
+
let r = sha256(Buffer.of(0x00), leafEntry);
|
|
194
|
+
for (const p of path) {
|
|
195
|
+
if (p.length !== 32)
|
|
196
|
+
throw new Error("inclusion-node-size");
|
|
197
|
+
if (sn === 0)
|
|
198
|
+
throw new Error("inclusion-path-too-long");
|
|
199
|
+
if (fn % 2 === 1 || fn === sn) {
|
|
200
|
+
r = sha256(Buffer.of(0x01), p, r);
|
|
201
|
+
while (fn % 2 === 0 && fn !== 0) {
|
|
202
|
+
fn = Math.floor(fn / 2);
|
|
203
|
+
sn = Math.floor(sn / 2);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
r = sha256(Buffer.of(0x01), r, p);
|
|
208
|
+
}
|
|
209
|
+
fn = Math.floor(fn / 2);
|
|
210
|
+
sn = Math.floor(sn / 2);
|
|
211
|
+
}
|
|
212
|
+
if (sn !== 0)
|
|
213
|
+
throw new Error("inclusion-path-too-short");
|
|
214
|
+
return r;
|
|
215
|
+
}
|
|
216
|
+
const UNDERSTOOD = new Set([1, 395]);
|
|
217
|
+
/**
|
|
218
|
+
* Checks one receipt for one leaf entry under the service key the reader
|
|
219
|
+
* holds. vds is read from the protected header only.
|
|
220
|
+
*/
|
|
221
|
+
export function verifyReceipt(receipt, leafEntry, publicKeyPem) {
|
|
222
|
+
let sign1;
|
|
223
|
+
try {
|
|
224
|
+
sign1 = decodeSign1(receipt);
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
return { ok: false, stage: "receipt-decode", reason: err instanceof Error ? err.message : String(err) };
|
|
228
|
+
}
|
|
229
|
+
// RFC 9052 3.1: crit lists labels a reader must understand, is non-empty and
|
|
230
|
+
// sits in the protected header. This reader understands alg and vds only.
|
|
231
|
+
const crit = sign1.protectedHeader.get(2);
|
|
232
|
+
if (crit !== undefined && (!Array.isArray(crit) || crit.length === 0 || !crit.every((l) => UNDERSTOOD.has(l)))) {
|
|
233
|
+
return { ok: false, stage: "receipt-decode", reason: "a critical header this reader does not understand" };
|
|
234
|
+
}
|
|
235
|
+
if (sign1.unprotected.has(2))
|
|
236
|
+
return { ok: false, stage: "receipt-decode", reason: "crit in the unprotected header" };
|
|
237
|
+
const vds = sign1.protectedHeader.get(395);
|
|
238
|
+
if (vds !== 1)
|
|
239
|
+
return { ok: false, stage: "vds-gate", reason: `vds ${String(vds)} is not RFC9162_SHA256` };
|
|
240
|
+
const vdp = sign1.unprotected.get(396);
|
|
241
|
+
const proofs = vdp instanceof Map ? vdp.get(-1) : undefined;
|
|
242
|
+
if (!Array.isArray(proofs) || proofs.length !== 1 || !(proofs[0] instanceof Uint8Array)) {
|
|
243
|
+
return { ok: false, stage: "receipt-decode", reason: "no single inclusion proof at 396/-1" };
|
|
244
|
+
}
|
|
245
|
+
if (sign1.payload !== null)
|
|
246
|
+
return { ok: false, stage: "receipt-decode", reason: "receipt payload is not detached" };
|
|
247
|
+
let proof;
|
|
248
|
+
try {
|
|
249
|
+
proof = decodeCborItem(proofs[0]);
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
return { ok: false, stage: "receipt-decode", reason: err instanceof Error ? err.message : String(err) };
|
|
253
|
+
}
|
|
254
|
+
if (!Array.isArray(proof) || proof.length !== 3)
|
|
255
|
+
return { ok: false, stage: "receipt-decode", reason: "inclusion proof shape" };
|
|
256
|
+
const [treeSize, leafIndex, path] = proof;
|
|
257
|
+
if (typeof treeSize !== "number" || typeof leafIndex !== "number" || !Array.isArray(path) || !path.every((p) => p instanceof Uint8Array)) {
|
|
258
|
+
return { ok: false, stage: "receipt-decode", reason: "inclusion proof members" };
|
|
259
|
+
}
|
|
260
|
+
let root;
|
|
261
|
+
try {
|
|
262
|
+
root = rootFromInclusion(leafEntry, leafIndex, treeSize, path);
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
return { ok: false, stage: "inclusion", reason: err instanceof Error ? err.message : String(err) };
|
|
266
|
+
}
|
|
267
|
+
let verified = false;
|
|
268
|
+
try {
|
|
269
|
+
verified = verifyCoseSignature(sign1.protectedHeader.get(1), publicKeyPem, sign1Structure(sign1.protectedBytes, root), sign1.signature);
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
verified = false;
|
|
273
|
+
}
|
|
274
|
+
if (!verified)
|
|
275
|
+
return { ok: false, stage: "receipt-sig", reason: "signature does not verify over the rebuilt root" };
|
|
276
|
+
return { ok: true, root: root.toString("hex"), treeSize, leafIndex };
|
|
277
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -24,6 +24,8 @@ export type ToolResult = {
|
|
|
24
24
|
}[];
|
|
25
25
|
isError: boolean;
|
|
26
26
|
claimedHash?: string;
|
|
27
|
+
/** Adapter-verified downstream extract. Side field: excluded from resultHash and the agent response. */
|
|
28
|
+
effectReceipt?: SignedEffectExtract;
|
|
27
29
|
};
|
|
28
30
|
export type PolicyDecision = {
|
|
29
31
|
decision: DecisionKind;
|
|
@@ -63,7 +65,7 @@ export type EffectAttestation = {
|
|
|
63
65
|
export type LedgerEffect = {
|
|
64
66
|
row: EffectRow;
|
|
65
67
|
witnessClass: WitnessClass;
|
|
66
|
-
/** Hash of the ToolResult (or thrown payload).
|
|
68
|
+
/** Hash of the normal ToolResult (or thrown payload). Unattested on third-party rows. */
|
|
67
69
|
resultHash?: string;
|
|
68
70
|
/** One-row extract signed at call time. */
|
|
69
71
|
receipt?: SignedEffectExtract;
|
|
@@ -89,7 +91,7 @@ export type ExtractWindow = {
|
|
|
89
91
|
export type Ledger = {
|
|
90
92
|
appendDecision(signed: SignedDecisionRecord): Promise<void>;
|
|
91
93
|
appendDecisionChained(build: (prevRecordHash: string | null) => SignedDecisionRecord): Promise<void>;
|
|
92
|
-
appendEffect(row: EffectRow, witnessClass?: WitnessClass, resultHash?: string): Promise<void>;
|
|
94
|
+
appendEffect(row: EffectRow, witnessClass?: WitnessClass, resultHash?: string, externalReceipt?: SignedEffectExtract): Promise<void>;
|
|
93
95
|
decisions(): Promise<SignedDecisionRecord[]>;
|
|
94
96
|
effects(): Promise<LedgerEffect[]>;
|
|
95
97
|
lastDecisionHash(): Promise<string | null>;
|
|
@@ -119,6 +121,39 @@ export type ControlInput = {
|
|
|
119
121
|
atMs: number;
|
|
120
122
|
line: number;
|
|
121
123
|
lineHash: string;
|
|
124
|
+
/**
|
|
125
|
+
* Copied as the history line carries it; `verax verify` judges it. Only an
|
|
126
|
+
* HTTP halt or resume made with a passkey assertion has one.
|
|
127
|
+
*/
|
|
128
|
+
signature?: ControlSignature;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* The operator's WebAuthn assertion over one halt or resume. The challenge is
|
|
132
|
+
* rebuilt from `purpose` (`verax.halt` or `verax.resume`, from the record's
|
|
133
|
+
* subject) and `prev`, the SHA-256 of the history line before this one
|
|
134
|
+
* (`null` for the first line). A line is appended once, so a signature made
|
|
135
|
+
* for one tail of the history does not fit any later one.
|
|
136
|
+
*/
|
|
137
|
+
export type ControlSignature = {
|
|
138
|
+
credentialId: string;
|
|
139
|
+
sub: string;
|
|
140
|
+
authenticatorData: string;
|
|
141
|
+
clientDataJSON: string;
|
|
142
|
+
signature: string;
|
|
143
|
+
rpId: string;
|
|
144
|
+
prev: string | null;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* One change to `revoked-jti.jsonl` copied into the ledger. `revoke` is a jti
|
|
148
|
+
* that appeared in the file; `unrevoke` is one that was in the ledger's list
|
|
149
|
+
* and is gone from the file, which lets that token in again.
|
|
150
|
+
*/
|
|
151
|
+
export type RevokeInput = {
|
|
152
|
+
action: "revoke" | "unrevoke";
|
|
153
|
+
jti: string;
|
|
154
|
+
by: string;
|
|
155
|
+
via: "http" | "file";
|
|
156
|
+
atMs: number;
|
|
122
157
|
};
|
|
123
158
|
/**
|
|
124
159
|
* The operator's own WebAuthn assertion for one HTTP approval.
|
|
@@ -153,6 +188,7 @@ export type DecisionInputs = {
|
|
|
153
188
|
signature?: ApprovalSignature;
|
|
154
189
|
};
|
|
155
190
|
control?: ControlInput;
|
|
191
|
+
revoke?: RevokeInput;
|
|
156
192
|
};
|
|
157
193
|
/** How an operator's approval reached the ledger: a person at `verax approve`, a non-interactive `--from-script` run, or the body's `/api/approve`. */
|
|
158
194
|
export type ApprovalChannel = "cli" | "cli-script" | "http";
|
|
@@ -195,6 +231,11 @@ export type ExplainWarning = {
|
|
|
195
231
|
detail: string;
|
|
196
232
|
};
|
|
197
233
|
export type ExplainOpts = {
|
|
234
|
+
thirdPartyTrust?: {
|
|
235
|
+
publicKeyPem: string;
|
|
236
|
+
party: string;
|
|
237
|
+
independent: boolean;
|
|
238
|
+
};
|
|
198
239
|
issuerTrust?: {
|
|
199
240
|
publicKeyPem: string | readonly string[];
|
|
200
241
|
source?: "env" | "own-key";
|