@agledger/verify-core 2.2.0 → 4.0.0

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.
Files changed (49) hide show
  1. package/LICENSE +18 -18
  2. package/README.md +797 -29
  3. package/dist/acknowledgements.d.ts +32 -0
  4. package/dist/acknowledgements.js +88 -0
  5. package/dist/anchors.d.ts +349 -0
  6. package/dist/anchors.js +654 -0
  7. package/dist/attestation.d.ts +98 -0
  8. package/dist/attestation.js +145 -0
  9. package/dist/audit-export.d.ts +343 -20
  10. package/dist/audit-export.js +400 -22
  11. package/dist/batch-head.d.ts +28 -0
  12. package/dist/batch-head.js +169 -0
  13. package/dist/batch.d.ts +69 -0
  14. package/dist/batch.js +99 -0
  15. package/dist/bulk-export.d.ts +161 -0
  16. package/dist/bulk-export.js +711 -0
  17. package/dist/chain.d.ts +135 -13
  18. package/dist/chain.js +229 -24
  19. package/dist/claim.d.ts +23 -0
  20. package/dist/claim.js +100 -0
  21. package/dist/export-anchors.d.ts +84 -0
  22. package/dist/export-anchors.js +109 -0
  23. package/dist/export-evidence.d.ts +90 -0
  24. package/dist/export-evidence.js +186 -0
  25. package/dist/export-pages.d.ts +476 -0
  26. package/dist/export-pages.js +586 -0
  27. package/dist/failures.d.ts +20 -16
  28. package/dist/failures.js +44 -16
  29. package/dist/federation-wire.d.ts +78 -11
  30. package/dist/federation-wire.js +199 -57
  31. package/dist/history-seal.d.ts +100 -0
  32. package/dist/history-seal.js +301 -0
  33. package/dist/index.d.ts +36 -8
  34. package/dist/index.js +17 -4
  35. package/dist/key-statements.d.ts +138 -5
  36. package/dist/key-statements.js +191 -20
  37. package/dist/ml-dsa-kats.d.ts +18 -0
  38. package/dist/ml-dsa-kats.js +241 -0
  39. package/dist/primitives.d.ts +255 -18
  40. package/dist/primitives.js +612 -55
  41. package/dist/read-log.d.ts +350 -0
  42. package/dist/read-log.js +816 -0
  43. package/dist/schema-chain-export.d.ts +82 -0
  44. package/dist/schema-chain-export.js +474 -0
  45. package/dist/walk-anchors.d.ts +115 -0
  46. package/dist/walk-anchors.js +133 -0
  47. package/dist/walk-checkpoints.d.ts +125 -0
  48. package/dist/walk-checkpoints.js +118 -0
  49. package/package.json +6 -5
@@ -0,0 +1,350 @@
1
+ /**
2
+ * The cross-party read log as the Server serves it over its API, verified
3
+ * offline: the signed tree heads of `GET /v1/audit/org-reads/checkpoints` and
4
+ * `GET /v1/audit/org-reads/checkpoints/{id}` (the cosign response included),
5
+ * the inclusion proofs of `GET /v1/audit/org-reads/checkpoints/{id}/proof`,
6
+ * and the leaves of `GET /v1/audit/org-reads`.
7
+ *
8
+ * Each tree head is a COSE_Sign1 envelope over a vault-checkpoint in-toto
9
+ * Statement: the RFC 9162 Merkle root over leaves [0, treeSize) as subject and
10
+ * as `chain_tip_hash`, the tree size as chain position and `count`, the range
11
+ * 0 to treeSize - 1, the org as CWT `sub`, the time as CWT `iat`, and the
12
+ * signing key as the protected-header kid. Every one of those is held to the
13
+ * field the document serves beside it, under the codes a dump's read log is
14
+ * held to (TENANT_CHECKPOINT_*), and the signature is verified under a key
15
+ * resolved and walked from the caller's anchors as `verifyAuditExport`
16
+ * resolves an export's keys.
17
+ *
18
+ * A leaf as the API lists it carries its leaf hash and the columns of its
19
+ * row, not the envelope that hash is taken over, so its columns are bound to
20
+ * nothing signed: what a listing establishes is the leaf hashes. A leaf as a
21
+ * dump's `org_admin_reads` row carries the envelope (`cose_sign1`), and is
22
+ * held to it as the dump verifier holds it (TENANT_READ_*).
23
+ */
24
+ import { type VerifyExportOptions } from './audit-export.js';
25
+ import type { FailureCode } from './failures.js';
26
+ import { type RuntimeBelowFloor, type Uncomputable } from './primitives.js';
27
+ import { type KeyTrustReport, type Verdict } from './key-statements.js';
28
+ /**
29
+ * One signed tree head: `GET /v1/audit/org-reads/checkpoints/{id}`, one row of
30
+ * the checkpoint listing's `data`, or the `POST .../cosign` response (whose
31
+ * `nextSteps` is not read).
32
+ *
33
+ * The Server sends every field. Those marked optional are optional here so a
34
+ * client's own type for the response is taken as it is: a tree head without
35
+ * `coseSign1Base64` is not a tree head (`TypeError`), and one without `logId`
36
+ * or `signingKeyId` fails `TENANT_CHECKPOINT_CLAIM_MISMATCH`, since its
37
+ * envelope signs both.
38
+ */
39
+ export interface OrgReadCheckpointInput {
40
+ id: string;
41
+ orgId: string;
42
+ treeSize: number;
43
+ /** Lowercase hex RFC 9162 root over leaves [0, treeSize). */
44
+ rootHash: string;
45
+ /** RFC 3339; the envelope signs it, in whole seconds, as CWT `iat`. */
46
+ checkpointAt: string;
47
+ /** The log the tree head is about; the Server writes the org id, and signs it as CWT `sub`. */
48
+ logId?: string;
49
+ /** Base64 of the COSE_Sign1 envelope: the signed tree head. */
50
+ coseSign1Base64?: string;
51
+ /** The vault key that signed it, or null on a tree head written before the install signed. */
52
+ signingKeyId?: string | null;
53
+ /** A witness's signature over the tree head, stored as the witness gave it; null until cosigned. */
54
+ witnessSignature?: string | null;
55
+ witnessKeyId?: string | null;
56
+ witnessCosignedAt?: string | null;
57
+ nextSteps?: unknown;
58
+ }
59
+ /** One page of `GET /v1/audit/org-reads/checkpoints`. */
60
+ export interface OrgReadCheckpointsPage {
61
+ /** The sweep's cadence and the leaves it has not covered yet. Unsigned; not read. */
62
+ checkpointing?: object;
63
+ data: OrgReadCheckpointInput[];
64
+ total?: number;
65
+ nextCursor?: string | null;
66
+ hasMore?: boolean;
67
+ }
68
+ /** `GET /v1/audit/org-reads/checkpoints/{id}/proof?leaf=<leafIndex>`. */
69
+ export interface OrgReadProofInput {
70
+ leafIndex: number;
71
+ leafHash: string;
72
+ /** The Server sends it; a proof without it fails `TENANT_READ_PROOF_INVALID`. */
73
+ treeSize?: number;
74
+ rootHash: string;
75
+ /** The RFC 9162 section 2.1.3.1 audit path, leaf level first, each lowercase hex. */
76
+ path: string[];
77
+ }
78
+ /** One leaf of `GET /v1/audit/org-reads` (`data[]`). */
79
+ export interface OrgReadEntryInput {
80
+ id: string;
81
+ orgId: string;
82
+ leafIndex: number;
83
+ /** hex(sha256(0x00 || cose_sign1)) of the leaf's envelope, which the listing does not carry. */
84
+ leafHash: string;
85
+ recordId: string;
86
+ callerKeyId: string | null;
87
+ callerOidc?: {
88
+ iss: string;
89
+ sub: string;
90
+ } | null;
91
+ filterApplied: string;
92
+ readContext: string;
93
+ exportBatchId: string | null;
94
+ readAt: string;
95
+ }
96
+ /** One page of `GET /v1/audit/org-reads`. */
97
+ export interface OrgReadEntriesPage {
98
+ data: OrgReadEntryInput[];
99
+ total?: number;
100
+ nextCursor?: string | null;
101
+ hasMore?: boolean;
102
+ }
103
+ /** One row of a dump's `org_admin_reads.ndjson`: a leaf with the envelope its hash is taken over. */
104
+ export interface OrgReadDumpRow {
105
+ id: string;
106
+ org_id: string;
107
+ caller_key_id: string;
108
+ record_id: string;
109
+ filter_applied: string;
110
+ read_context: string;
111
+ export_batch_id: string | null;
112
+ read_at: string;
113
+ leaf_index: number;
114
+ leaf_hash: string;
115
+ /** Base64 of the leaf's COSE_Sign1 envelope (an org-read claim). */
116
+ cose_sign1: string;
117
+ actor_oidc_iss?: string | null;
118
+ actor_oidc_sub?: string | null;
119
+ }
120
+ /**
121
+ * A witness key: SPKI DER, base64, of an Ed25519, ES256, ES384 or ML-DSA key.
122
+ * `keyId` is matched against the checkpoint's `witnessKeyId` exactly.
123
+ */
124
+ export interface OrgReadCosignerKey {
125
+ keyId: string;
126
+ publicKey: string;
127
+ }
128
+ export interface VerifyOrgReadOptions extends Pick<VerifyExportOptions, 'publicKeys' | 'trustAnchors' | 'distrustedKeys'> {
129
+ /**
130
+ * The witness keys, `{ [witnessKeyId]: base64 SPKI DER }` or
131
+ * `[{ keyId, publicKey }]`. Take them from the witness, not from the
132
+ * Server: the Server stores whatever it is given as `witnessKeyId` and
133
+ * `witnessSignature`. A co-signature under a key given here is verified
134
+ * over the checkpoint's COSE_Sign1 bytes (the bytes `coseSign1Base64`
135
+ * decodes to), with the signature read as hex or base64. A key that is not
136
+ * SPKI DER base64 throws `TypeError`.
137
+ */
138
+ cosignerKeys?: Readonly<Record<string, string>> | ReadonlyArray<OrgReadCosignerKey>;
139
+ /**
140
+ * Fail a checkpoint that carries no co-signature verified under a key in
141
+ * `cosignerKeys` (TENANT_CHECKPOINT_COSIGNATURE_INVALID). For a witness
142
+ * that co-signs every tree head: a Server that served one without the
143
+ * co-signature is then a finding.
144
+ */
145
+ requireCosignature?: boolean;
146
+ }
147
+ /** Where a finding sits: a tree head by its tree size, a leaf by its index. */
148
+ export interface OrgReadFinding {
149
+ code: FailureCode;
150
+ detail: string;
151
+ treeSize?: number;
152
+ leafIndex?: number;
153
+ }
154
+ /**
155
+ * What a witness co-signature showed. `absent`: the checkpoint carries none.
156
+ * `verified`: it verifies under the key `cosignerKeys` gives for its
157
+ * `witnessKeyId`. `no_key`: no key was given for that id, so it was not
158
+ * checked. `failed`: it does not verify under that key, or was required and
159
+ * is missing or unchecked. `not_checked`: the key's algorithm could not be
160
+ * computed here.
161
+ */
162
+ export interface OrgReadCosignatureReport {
163
+ state: 'absent' | 'verified' | 'no_key' | 'failed' | 'not_checked';
164
+ witnessKeyId: string | null;
165
+ witnessCosignedAt: string | null;
166
+ /** The encoding the signature verified in, when it did. */
167
+ encoding?: 'hex' | 'base64';
168
+ detail: string;
169
+ }
170
+ /**
171
+ * What one tree head showed. `verified`: its claim matches its fields and its
172
+ * signature verifies inside its key's window. `unsigned`: written before the
173
+ * install signed, which proves nothing about who wrote it. `failed`: a
174
+ * finding. `not_checked`: its signature could not be computed here.
175
+ */
176
+ export interface OrgReadCheckpointReport {
177
+ id: string | null;
178
+ orgId: string | null;
179
+ treeSize: number | null;
180
+ rootHash: string | null;
181
+ signingKeyId: string | null;
182
+ /** The signed CWT iat, whole seconds, or null when the claim does not decode. */
183
+ signedIat: number | null;
184
+ state: 'verified' | 'unsigned' | 'failed' | 'not_checked';
185
+ cosignature: OrgReadCosignatureReport;
186
+ }
187
+ interface ResultBase {
188
+ valid: boolean;
189
+ /**
190
+ * `trusted`, `unanchored`, `unverifiable` or `failed`, as every verifier in
191
+ * this package reads them: `trusted` only when a signature verified under a
192
+ * key the caller's `trustAnchors` anchor.
193
+ */
194
+ verdict: Verdict;
195
+ /** The first finding that is not a check this host or build could not compute. */
196
+ brokenAt?: OrgReadFinding;
197
+ findings: OrgReadFinding[];
198
+ keyTrust: KeyTrustReport;
199
+ uncomputable: Uncomputable;
200
+ /** Present only below the Node.js floor: see `belowFloorNotice`. */
201
+ runtime?: RuntimeBelowFloor;
202
+ }
203
+ export interface VerifyOrgReadCheckpointResult extends ResultBase {
204
+ checkpoint: OrgReadCheckpointReport;
205
+ }
206
+ /** What the leaf a proof was walked for showed. */
207
+ export interface OrgReadLeafReport {
208
+ leafIndex: number | null;
209
+ /** The leaf hash the path was walked from. */
210
+ leafHash: string | null;
211
+ /**
212
+ * `envelope`: a dump row, whose hash was recomputed from its envelope bytes.
213
+ * `entry`: a row of `GET /v1/audit/org-reads`, which carries the hash and
214
+ * not the envelope. `proof`: no leaf was given; the proof's own leafHash.
215
+ */
216
+ source: 'envelope' | 'entry' | 'proof';
217
+ /**
218
+ * The leaf's own signature, when its envelope was given: `failed` when it
219
+ * was computed and does not verify, or verifies outside its key's window;
220
+ * `not_checked` when no envelope was given, this host cannot compute it, or
221
+ * the leaf failed before it was reached (its bytes do not hash to its leaf
222
+ * hash, its claim does not match its row, or its key is not given or not
223
+ * anchored).
224
+ */
225
+ signature: 'verified' | 'unsigned' | 'failed' | 'not_checked';
226
+ /** True when the leaf's columns were held to its signed claim (an envelope was given). */
227
+ fieldsBound: boolean;
228
+ }
229
+ export interface VerifyOrgReadProofResult extends ResultBase {
230
+ checkpoint: OrgReadCheckpointReport;
231
+ leaf: OrgReadLeafReport;
232
+ /** `included`: the path walks the leaf hash at its index to the checkpoint's signed root. */
233
+ inclusion: 'included' | 'failed';
234
+ }
235
+ /** One tree head of a walk, against the leaves the walk holds. */
236
+ export interface OrgReadListedCheckpoint extends OrgReadCheckpointReport {
237
+ source: string;
238
+ /**
239
+ * `root_recomputed`: the walk holds leaves 0 to treeSize - 1 and their root
240
+ * is the signed one. `root_mismatch`: it is not. `not_comparable`: the walk
241
+ * does not hold all of those leaves (or the tree head failed first).
242
+ */
243
+ leaves: 'root_recomputed' | 'root_mismatch' | 'not_comparable';
244
+ detail: string;
245
+ }
246
+ export interface VerifyOrgReadLogResult extends ResultBase {
247
+ orgId: string | null;
248
+ /**
249
+ * The leaves given: how many, the first and last index, how many carried
250
+ * their envelope (dump rows), and how many of those were signed and
251
+ * verified. `fromZero` is true when the first index is 0, which is what a
252
+ * root can be recomputed from.
253
+ */
254
+ leaves: {
255
+ held: number;
256
+ firstIndex: number | null;
257
+ lastIndex: number | null;
258
+ fromZero: boolean;
259
+ withEnvelope: number;
260
+ signed: number;
261
+ };
262
+ checkpoints: OrgReadListedCheckpoint[];
263
+ /**
264
+ * How far the leaves are held by something signed: the largest tree head
265
+ * whose root was recomputed from them, or `listing` when none was. Leaves
266
+ * above `treeSize` are held by the listing alone, which is unsigned.
267
+ */
268
+ heldTo: {
269
+ by: 'signed_checkpoint' | 'listing';
270
+ treeSize: number;
271
+ checkpointId: string | null;
272
+ detail: string;
273
+ };
274
+ /** True only when every leaf carried its envelope: a listing's columns are bound to nothing signed. */
275
+ fieldsBound: boolean;
276
+ }
277
+ /**
278
+ * Which read-log document a parsed JSON value is, or null:
279
+ * `org-read-checkpoint` (one tree head, or the cosign response),
280
+ * `org-read-checkpoints` (a page of the checkpoint listing),
281
+ * `org-read-proof`, `org-read-entries` (a page of `GET /v1/audit/org-reads`),
282
+ * `org-read-entry` (one row of it, or a dump's `org_admin_reads` row).
283
+ */
284
+ export declare function orgReadDocumentKind(value: unknown): 'org-read-checkpoint' | 'org-read-checkpoints' | 'org-read-proof' | 'org-read-entries' | 'org-read-entry' | null;
285
+ /**
286
+ * How far before the signed iat the time the Server stamps on a read-log row
287
+ * (`read_at`, `checkpoint_at`) may fall: the same clock skew a key window
288
+ * allows. The Server signs the claim at its own clock and the database stamps
289
+ * the column when it writes the row, after it signed (a batch read signs
290
+ * every leaf before it writes any), so the stamp may run seconds past the
291
+ * iat and is never earlier, beyond the two clocks' skew.
292
+ */
293
+ export declare const READ_LOG_STAMP_SKEW_SECONDS = 300;
294
+ /**
295
+ * Hold the time the Server stamped on a read-log row (`read_at` of a leaf,
296
+ * `checkpointAt` of a tree head) to the claim's signed iat: the stamp is not
297
+ * earlier than the iat by more than {@link READ_LOG_STAMP_SKEW_SECONDS}. The
298
+ * reason it is not held, or null. The dump verifier holds a dump's rows to the
299
+ * same rule.
300
+ */
301
+ export declare function readLogStampDisagreement(field: string, signedIat: unknown, stamped: unknown): string | null;
302
+ /**
303
+ * Verify one signed tree head of the read log offline. Its envelope must
304
+ * decode, its signed claim must match the fields served beside it (tree
305
+ * size, root, org, log, kid, time), its signature must verify under the key
306
+ * `publicKeys` gives for its `signingKeyId` (walked from `trustAnchors` when
307
+ * given, `distrustedKeys` applied), inside that key's window at its
308
+ * `checkpointAt`, which is not earlier than the signed iat beyond the clock
309
+ * skew {@link READ_LOG_STAMP_SKEW_SECONDS} allows. A
310
+ * witness co-signature is verified under `cosignerKeys`.
311
+ *
312
+ * Throws `TypeError` when the input is not a checkpoint document, and on a
313
+ * malformed option.
314
+ */
315
+ export declare function verifyOrgReadCheckpoint(checkpoint: OrgReadCheckpointInput, options?: VerifyOrgReadOptions): VerifyOrgReadCheckpointResult;
316
+ /**
317
+ * Verify an inclusion proof offline: the tree head it is for (as
318
+ * `verifyOrgReadCheckpoint` does), the proof's tree size and root against
319
+ * that tree head's, and the RFC 9162 audit path from the leaf hash at the
320
+ * leaf's index to the signed root.
321
+ *
322
+ * `leaf` is what the path is walked from. A dump's `org_admin_reads` row
323
+ * carries the envelope: its hash is recomputed from the bytes, and its claim
324
+ * and signature are held to its row, so its columns are bound. A row of
325
+ * `GET /v1/audit/org-reads` carries the hash alone: the hash is walked, and
326
+ * its other columns are bound to nothing signed. Without a leaf, the proof's
327
+ * own `leafHash` is walked, which establishes only that some leaf with that
328
+ * hash sits at that index.
329
+ */
330
+ export declare function verifyOrgReadProof(proof: OrgReadProofInput, checkpoint: OrgReadCheckpointInput, leaf?: OrgReadEntryInput | OrgReadDumpRow | null, options?: VerifyOrgReadOptions): VerifyOrgReadProofResult;
331
+ /**
332
+ * Verify a walk over the read log offline: the leaves given (rows of
333
+ * `GET /v1/audit/org-reads`, or a dump's `org_admin_reads` rows) and the tree
334
+ * heads given (pages or rows of `GET /v1/audit/org-reads/checkpoints`), all
335
+ * of one org.
336
+ *
337
+ * Every tree head is verified as `verifyOrgReadCheckpoint` verifies it, and
338
+ * two at one tree size with different roots fail TENANT_CHECKPOINT_FORK. The
339
+ * leaves must be contiguous (TENANT_READ_LEAF_INDEX_GAP); two different rows
340
+ * at one index fail EXPORT_PAGE_CONFLICT. When the leaves start at index 0,
341
+ * the root over leaves [0, treeSize) is recomputed for every tree head whose
342
+ * size they reach and held to its signed root (TENANT_CHECKPOINT_ROOT_MISMATCH):
343
+ * that establishes the leaf hashes a tree head signs, in order. A tree head
344
+ * above the last leaf given, and every tree head when the leaves do not start
345
+ * at 0, is verified on its own and reported `not_comparable`. A leaf that
346
+ * carries its envelope is held to it as a dump's is, linked to the leaf
347
+ * before it; a listing row's other columns are bound to nothing signed.
348
+ */
349
+ export declare function verifyOrgReadLog(entries: OrgReadEntriesPage | ReadonlyArray<OrgReadEntriesPage | OrgReadEntryInput | OrgReadDumpRow> | null | undefined, checkpoints: OrgReadCheckpointsPage | ReadonlyArray<OrgReadCheckpointsPage | OrgReadCheckpointInput> | null | undefined, options?: VerifyOrgReadOptions): VerifyOrgReadLogResult;
350
+ export {};