@agledger/verify-core 1.4.2 → 2.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.
package/dist/chain.d.ts CHANGED
@@ -1,17 +1,69 @@
1
+ /**
2
+ * The shared per-chain verification walk.
3
+ *
4
+ * This is the single body of logic the SDK /verify, CLI, MCP server, and
5
+ * @agledger/verify all run, replacing four hand-vendored copies. It walks ONE
6
+ * hash chain (a single record's lifecycle, or a single per-org schema-event
7
+ * chain) over a normalized entry shape and emits the canonical failure
8
+ * taxonomy.
9
+ *
10
+ * Two input surfaces feed it through thin adapters:
11
+ * - the live `/audit-export` JSON (per-record; see audit-export.ts)
12
+ * - the offline NDJSON vault dump (full-vault; @agledger/verify)
13
+ *
14
+ * Checks split into two tiers:
15
+ * - ALWAYS-RUN (every surface): position monotonicity, payloadHash =
16
+ * sha256(cose_sign1), previous-hash link, COSE_Sign1 decode, the signed
17
+ * protected-header chain-claim cross-check, and the Ed25519 signature.
18
+ * - INPUT-GATED (only when the normalized entry, or the caller, supplies the
19
+ * inputs): binding-integrity, OIDC-actor cross-check, temporal
20
+ * key-validity, the agent-signature re-check, and key anchoring (a
21
+ * registry marked by `applyKeyTrust`). The first three run on
22
+ * the dump and on any export from an engine that ships the row fields
23
+ * (>= v0.26.x). The agent-signature check needs the cert's public key,
24
+ * which neither surface carries, so it runs only when the caller passes
25
+ * `agentKeys`. A check without its input is reported `skipped_no_input`,
26
+ * NEVER folded into a green verdict.
27
+ *
28
+ * Failure ordering is fixed so `brokenAt` is deterministic. The null-key
29
+ * branch comes LAST, after the binding/OIDC checks, so a row written without a
30
+ * signing key is still subject to every structural check, and a tamper finding
31
+ * on it is reported under its own code rather than as CHAIN_ENTRY_UNSIGNED.
32
+ * That branch grades the entry the way the engine does: an unsigned entry
33
+ * after a signed one in its chain, or written at or after the earliest key
34
+ * activation (`signingSince`), is CHAIN_ENTRY_UNSIGNED; an earlier one is a
35
+ * `skipped` signature, reduced coverage rather than a break.
36
+ */
37
+ import { type AgentPublicKeyJwk } from './primitives.js';
1
38
  import type { FailureCode } from './failures.js';
2
- /** Where a verification key came from: the trust-anchor provenance. */
3
- export type KeySource = 'out-of-band' | 'embedded';
39
+ /**
40
+ * Where a verification key came from. `supplied`: the caller passed it (from
41
+ * `GET /v1/verification-keys`, `/.well-known/scitt-keys`, its own records).
42
+ * `embedded`: the artifact under verification shipped it. Neither says the key
43
+ * is trusted: a key the Server serves comes from its database, which is what
44
+ * a key-registry attacker writes to. Trust comes from walking the signed key
45
+ * statements from a pinned anchor (`trustAnchors`, see `computeKeyTrust`),
46
+ * reported per key as `trust`.
47
+ */
48
+ export type KeySource = 'supplied' | 'embedded';
49
+ /**
50
+ * What the key-statement walk concluded about a key (see `applyKeyTrust`).
51
+ * `anchored`: signed statements link it to a pinned anchor. `unanchored`:
52
+ * nothing signed does, so entries under it fail CHAIN_SIGNING_KEY_UNANCHORED.
53
+ * `undecided`: the only link runs through a signature this host cannot
54
+ * compute, so entries under it are CHAIN_UNSUPPORTED_ALGORITHM, not tamper.
55
+ */
56
+ export type KeyTrustState = 'anchored' | 'unanchored' | 'undecided';
4
57
  /** A public key the walk can verify signatures against. */
5
58
  export interface VerificationKey {
6
59
  keyId: string;
7
60
  /** SPKI DER, base64-encoded (the engine's vault_signing_keys.public_key shape). */
8
61
  spkiBase64: string;
9
62
  /**
10
- * `out-of-band` = supplied by the caller from a trusted source (e.g.
11
- * /v1/verification-keys, /.well-known/scitt-keys). `embedded` = shipped inside
12
- * the artifact being verified (the export/dump the engine produced). Surfaced
13
- * in the result so a caller can tell whether it verified the engine against an
14
- * independent key or against the engine's own answer key.
63
+ * `supplied` = passed by the caller. `embedded` = shipped inside the
64
+ * artifact being verified (the export or dump the engine produced). Surfaced
65
+ * in the result so a caller can tell whether it verified against a key it
66
+ * brought or against the artifact's own answer key.
15
67
  */
16
68
  source: KeySource;
17
69
  /**
@@ -23,13 +75,59 @@ export interface VerificationKey {
23
75
  * code path; only the key material does.
24
76
  */
25
77
  algorithm?: string;
26
- /** Optional temporal-validity window (present on the dump path only). */
78
+ /** Optional temporal-validity window. */
27
79
  activatedAt?: string;
28
80
  retiredAt?: string | null;
81
+ /**
82
+ * The instant from which `distrustedKeys` (`VAULT_DISTRUSTED_KEYS` on the
83
+ * Server) voids what this key signs, set by `applyKeyTrust` when it ends the
84
+ * window before `retiredAt`. Entries written after it fail CHAIN_KEY_EXPIRED
85
+ * worded as past the distrust cutoff: the key was not retired there.
86
+ */
87
+ distrustCutoff?: string;
88
+ /**
89
+ * Set by `applyKeyTrust` on a walk over a dump, for a key `distrustedKeys`
90
+ * names: the instant from which what it signs counts for nothing, and the
91
+ * earliest retirement a key the walk trusts signed for it (see the
92
+ * `DistrustSpan` of `computeKeyTrust`). An entry it signed outside its
93
+ * trust (the key unanchored, or the entry written at or after the cutoff),
94
+ * written before that retirement and whose signature verifies under it, is
95
+ * accounted for: listed as CHAIN_SIGNED_BY_DISTRUSTED_KEY, never a failure.
96
+ */
97
+ distrustSpan?: {
98
+ cutoff: string | null;
99
+ retiredAt: string | null;
100
+ };
101
+ /**
102
+ * The key-statement walk's verdict on this key, set by `applyKeyTrust`.
103
+ * Absent when no walk ran (no `trustAnchors`), and the result then reports
104
+ * `optionalChecks.key_anchoring: skipped_no_input`; with a walk and no
105
+ * entry reaching the check, `not_checked`.
106
+ */
107
+ trust?: KeyTrustState;
29
108
  }
30
109
  export type KeyRegistry = ReadonlyMap<string, VerificationKey>;
110
+ /**
111
+ * Index keys by key id. A key whose `spkiBase64` is not a non-empty string
112
+ * carries no key material (a registry row with its public key nulled), so it
113
+ * is left out: an entry naming it fails CHAIN_SIGNATURE_MISSING_KEY.
114
+ */
31
115
  export declare function buildKeyRegistry(keys: readonly VerificationKey[]): KeyRegistry;
32
- /** Inputs the input-gated checks consume; present only on the dump path. */
116
+ /**
117
+ * Agent cert keys for the agent-signature check, indexed by the RFC 7638
118
+ * thumbprint (`sha256:<hex>`) the engine seals as
119
+ * `predicate.on_behalf_of.cert.thumbprint`. Values are SPKI DER, base64.
120
+ */
121
+ export type AgentKeyRegistry = ReadonlyMap<string, string>;
122
+ /**
123
+ * Index caller-supplied agent cert keys by thumbprint. A key is matched to an
124
+ * entry only through the thumbprint that entry signed, so a key supplied for
125
+ * the wrong cert is never checked against it: it simply matches nothing.
126
+ * That is also why the source of a key needs no trust: the signed thumbprint
127
+ * is what binds it. Throws `TypeError` on anything that is not an Ed25519 JWK.
128
+ */
129
+ export declare function buildAgentKeyRegistry(jwks: readonly AgentPublicKeyJwk[]): AgentKeyRegistry;
130
+ /** One chain entry, plus the inputs the input-gated checks consume when the surface carries them. */
33
131
  export interface NormalizedEntry {
34
132
  /** Identity for messages: recordId (export) or chainKey (dump). */
35
133
  scopeId: string;
@@ -39,8 +137,14 @@ export interface NormalizedEntry {
39
137
  /** Base64-encoded canonical COSE_Sign1 envelope. */
40
138
  coseSign1: string;
41
139
  signingKeyId: string | null;
42
- /** ISO-8601 write time, for temporal key-validity (dump path). */
43
- createdAt?: string;
140
+ /**
141
+ * ISO-8601 write time, for temporal key-validity and the unsigned-entry
142
+ * rule. The engine writes one on every entry, so wherever the walk needs it
143
+ * (the entry's key carries a window, or the entry is unsigned and the key
144
+ * set dates the start of signing) a missing or unparseable value fails
145
+ * CHAIN_MALFORMED_ENTRY rather than skipping the check.
146
+ */
147
+ createdAt?: string | null;
44
148
  /** Inputs for the binding-integrity check (dump path). */
45
149
  binding?: {
46
150
  recordId: string | null;
@@ -53,16 +157,44 @@ export interface NormalizedEntry {
53
157
  sub: string | null;
54
158
  synthesized: boolean | undefined;
55
159
  };
160
+ /**
161
+ * Inputs for the actor-attribution cross-check: the row columns a report
162
+ * displays as "who did this". The export carries them as `actorId` /
163
+ * `actorRole` / `actorOwnerId`, the dump as `actor_key_id` / `actor_role` /
164
+ * `actor_owner_id`. Absent on older artifacts, which skips the check.
165
+ */
166
+ actorAttribution?: {
167
+ actorId: string | null;
168
+ actorRole: string | null;
169
+ actorOwnerId: string | null;
170
+ };
56
171
  }
57
- export type OptionalCheck = 'payload_binding' | 'oidc_actor' | 'key_temporal';
58
- export type CheckApplicability = 'applied' | 'skipped_no_input';
172
+ /**
173
+ * Checks that run only when their input is present. `agent_signature` is
174
+ * applied when at least one entry carrying an engine-validated agent signature
175
+ * had its cert key supplied by the caller (`agentKeys`); neither the export
176
+ * nor the dump carries cert public keys.
177
+ */
178
+ export type OptionalCheck = 'payload_binding' | 'oidc_actor' | 'actor_attribution' | 'key_temporal' | 'agent_signature' | 'key_anchoring';
179
+ /**
180
+ * `applied`: the check ran. `skipped_no_input`: the input it needs is absent.
181
+ * `not_checked`: the input was given but no entry reached the check, because
182
+ * the chain broke before it or no entry was signed. Only `key_anchoring` has
183
+ * a caller-level input and so can be `not_checked`.
184
+ */
185
+ export type CheckApplicability = 'applied' | 'skipped_no_input' | 'not_checked';
59
186
  export interface SignatureOutcome {
60
187
  /**
61
188
  * - `ok` / `invalid` / `decode-fail`: the signature was checked (and passed,
62
189
  * failed, or the envelope would not decode).
63
- * - `unsigned`: the entry carries no signature by design.
190
+ * - `unsigned`: retained for type compatibility; `verifyChain` no longer
191
+ * produces it. An all-zero signature on an entry that names a key is
192
+ * `invalid` (CHAIN_SIGNATURE_INVALID), as the engine grades it.
64
193
  * - `skipped`: the chain is intact but this entry has no signing key, so the
65
- * signature check was deliberately not run (engine booted without a key).
194
+ * signature check was deliberately not run. Only an entry written before
195
+ * the install began signing gets here: before any signed entry in its
196
+ * chain and before the earliest key activation. Any other unsigned entry
197
+ * fails CHAIN_ENTRY_UNSIGNED.
66
198
  * - `not-checked`: a structural/chain check failed at or before this entry,
67
199
  * so verification short-circuited before reaching the signature. Reads as a
68
200
  * consequence of an upstream break, never as a benign skip.
@@ -72,8 +204,12 @@ export interface SignatureOutcome {
72
204
  * CHAIN_UNSUPPORTED_ALGORITHM. A failure state, never a benign skip, and
73
205
  * never tamper evidence: distinguishing it from `invalid` is the whole
74
206
  * point, since only `invalid` means a signature was checked and failed.
207
+ * - `accounted`: the signature verifies under a key `distrustedKeys` names,
208
+ * outside what the key is trusted for, and the entry was written before a
209
+ * key the walk trusts retired it (dumps only). Not a failure and not a
210
+ * verified entry: see {@link AccountedEntry}.
75
211
  */
76
- state: 'ok' | 'invalid' | 'unsigned' | 'skipped' | 'not-checked' | 'decode-fail' | 'unsupported';
212
+ state: 'ok' | 'invalid' | 'unsigned' | 'skipped' | 'not-checked' | 'decode-fail' | 'unsupported' | 'accounted';
77
213
  /** Provenance of the key the signature was checked against ('ok' / 'invalid'). */
78
214
  keySource?: KeySource;
79
215
  }
@@ -85,9 +221,46 @@ export interface ChainEntryResult {
85
221
  code: FailureCode;
86
222
  detail: string;
87
223
  };
224
+ /** Set, with `valid: true` and signature `accounted`, on an entry the distrust entry accounts for. */
225
+ accounted?: {
226
+ code: AccountedEntryCode;
227
+ detail: string;
228
+ };
88
229
  signature: SignatureOutcome['state'];
89
230
  keySource?: KeySource;
90
231
  }
232
+ /** The code an {@link AccountedEntry} carries. Not a {@link FailureCode}: it never fails a verdict. */
233
+ export declare const ACCOUNTED_ENTRY_CODE = "CHAIN_SIGNED_BY_DISTRUSTED_KEY";
234
+ export type AccountedEntryCode = typeof ACCOUNTED_ENTRY_CODE;
235
+ /**
236
+ * A chain entry of a dump whose signature verifies under a key
237
+ * `distrustedKeys` names, that falls outside what the key is trusted for
238
+ * (the key is unanchored, or the entry was written at or after its cutoff),
239
+ * and that was written before a key the walk trusts retired it. The distrust
240
+ * entry and that retirement account for it, as the engine's scan lists it in
241
+ * `distrustedEntries`: it is listed, it is not verified, and it fails
242
+ * nothing. An entry written after that retirement, or under a distrusted key
243
+ * no trusted key has retired, fails as before (CHAIN_SIGNING_KEY_UNANCHORED
244
+ * or CHAIN_KEY_EXPIRED). An audit export never carries one.
245
+ *
246
+ * `chain` and `recordId`/`orgId` name the chain as the engine does: a record
247
+ * chain by its record id, the platform-ops chain (record id
248
+ * 00000000-0000-0000-0000-000000000000) as `admin`, and an org's schema chain
249
+ * (`schema:<orgId>`, `schema:__platform__` for the platform's) as `schema`.
250
+ */
251
+ export interface AccountedEntry {
252
+ code: AccountedEntryCode;
253
+ chain: 'record' | 'admin' | 'schema';
254
+ recordId: string | null;
255
+ orgId: string | null;
256
+ /** The chain's scope as the walk was given it (a record id, or a dump's `chain_key`). */
257
+ scopeId: string;
258
+ position: number;
259
+ keyId: string;
260
+ detail: string;
261
+ }
262
+ /** Name a chain scope (a record id or a dump `chain_key`) as {@link AccountedEntry} does. */
263
+ export declare function chainOfScope(scopeId: string): Pick<AccountedEntry, 'chain' | 'recordId' | 'orgId'>;
91
264
  export interface ChainResult {
92
265
  scopeId: string;
93
266
  valid: boolean;
@@ -99,6 +272,12 @@ export interface ChainResult {
99
272
  detail: string;
100
273
  };
101
274
  entries: ChainEntryResult[];
275
+ /**
276
+ * Entries the distrust entry accounts for (see {@link AccountedEntry}), in
277
+ * chain order. They count toward `valid` and not toward `verifiedEntries`
278
+ * or `signatureCoverage.signed`.
279
+ */
280
+ accounted: AccountedEntry[];
102
281
  signatureCoverage: {
103
282
  signed: number;
104
283
  unsigned: number;
@@ -107,22 +286,83 @@ export interface ChainResult {
107
286
  };
108
287
  /** Which input-gated checks actually ran on this chain vs were skipped for absent input. */
109
288
  optionalChecks: Record<OptionalCheck, CheckApplicability>;
110
- /** How many signature checks resolved against out-of-band vs embedded keys. */
289
+ /** How many signature checks resolved against supplied vs embedded keys. */
111
290
  keyProvenance: {
112
- outOfBand: number;
291
+ supplied: number;
113
292
  embedded: number;
114
293
  };
294
+ /**
295
+ * Agent signatures on this chain. `present` counts entries that passed
296
+ * every other check and whose signed payload carries
297
+ * `predicate.on_behalf_of.agent_signature`; `verified`
298
+ * counts those re-checked against a caller-supplied cert key and found
299
+ * good. `present > verified` on a valid chain means some were not checked
300
+ * (no key supplied, or the identity was a caller assertion), never that
301
+ * they failed.
302
+ */
303
+ agentSignatures: {
304
+ present: number;
305
+ verified: number;
306
+ };
115
307
  }
116
308
  export interface VerifyChainOptions {
117
309
  /** Require every entry's signingKeyId to equal this id (else CHAIN_KEY_POLICY_VIOLATION). */
118
310
  requireKeyId?: string;
119
311
  /**
120
- * High-assurance auditor mode: refuse to verify against keys shipped inside
121
- * the artifact. An entry whose only available key is `embedded` fails
122
- * CHAIN_KEY_POLICY_VIOLATION. Forces the caller to supply keys out of band.
312
+ * Refuse to verify against keys shipped inside the artifact: an entry whose
313
+ * only available key is `embedded` fails CHAIN_KEY_POLICY_VIOLATION. This
314
+ * says where a key came from, not that it is trusted; a key the Server
315
+ * serves comes from its database. Anchor keys with `computeKeyTrust` /
316
+ * `applyKeyTrust` for that.
317
+ */
318
+ requireSuppliedKeys?: boolean;
319
+ /**
320
+ * Agent cert keys (see `buildAgentKeyRegistry`). When an entry's signed
321
+ * payload carries an engine-validated `on_behalf_of.agent_signature` and its
322
+ * sealed cert thumbprint matches a key here, the signature is re-verified
323
+ * offline; a failure is CHAIN_AGENT_SIGNATURE_INVALID.
123
324
  */
124
- requireOutOfBandKeys?: boolean;
325
+ agentKeys?: AgentKeyRegistry;
326
+ /**
327
+ * The instant this install began signing: the earliest `activatedAt` across
328
+ * its whole signing key set, retired keys included. An entry with a null
329
+ * `signingKeyId` whose `createdAt` is at or after it fails
330
+ * CHAIN_ENTRY_UNSIGNED.
331
+ *
332
+ * Omitted, it is derived from `keys` (`earliestKeyActivation`), which is
333
+ * right whenever `keys` is the whole key set, as it is on the dump path.
334
+ * Pass it when the caller knows windows for keys it has no public key for.
335
+ * `null` says the key set carries no activation time, so only the other
336
+ * half of the rule (an unsigned entry after a signed one) applies. A string
337
+ * that does not parse as a date throws `TypeError` rather than silently
338
+ * switching the check off.
339
+ */
340
+ signingSince?: string | null;
125
341
  }
342
+ /**
343
+ * The earliest `activatedAt` among `keys`, as the ISO string it was given in,
344
+ * or `null` when no key carries a parseable one. This is the instant from
345
+ * which the engine treats every chain entry and checkpoint as required to be
346
+ * signed; pass every key the verifier knows, retired ones included, since the
347
+ * engine reads it as `min(activated_at)` over its whole key registry.
348
+ */
349
+ export declare function earliestKeyActivation(keys: Iterable<{
350
+ activatedAt?: string | null;
351
+ }>): string | null;
352
+ /**
353
+ * Whether a row with no signing key id, written at `writtenAt`, falls inside
354
+ * the era in which the install signs everything: at or after `signingSince`
355
+ * (see `earliestKeyActivation`). This is the rule for an unsigned checkpoint
356
+ * (CHECKPOINT_UNSIGNED) and an unsigned read-log leaf or tree head, and the
357
+ * time half of the rule for an unsigned entry (CHAIN_ENTRY_UNSIGNED).
358
+ *
359
+ * False when `signingSince` is absent or unparseable: with no instant from
360
+ * which the install signs, nothing has to be signed. True when `signingSince`
361
+ * is known and `writtenAt` is missing or not strict RFC 3339: the engine times every
362
+ * row, so a row with no readable time was edited, and it cannot be placed
363
+ * before signing began. It fails closed rather than reading as early history.
364
+ */
365
+ export declare function writtenWhileSigning(writtenAt: unknown, signingSince: string | null | undefined): boolean;
126
366
  /**
127
367
  * Verify a single hash chain. `entries` are all the entries for one scope; they
128
368
  * are sorted by chainPosition internally (so a reordered export array still