@agledger/verify-core 1.5.0 → 2.1.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
@@ -17,7 +17,8 @@
17
17
  * protected-header chain-claim cross-check, and the Ed25519 signature.
18
18
  * - INPUT-GATED (only when the normalized entry, or the caller, supplies the
19
19
  * inputs): binding-integrity, OIDC-actor cross-check, temporal
20
- * key-validity, and the agent-signature re-check. The first three run on
20
+ * key-validity, the agent-signature re-check, and key anchoring (a
21
+ * registry marked by `applyKeyTrust`). The first three run on
21
22
  * the dump and on any export from an engine that ships the row fields
22
23
  * (>= v0.26.x). The agent-signature check needs the cert's public key,
23
24
  * which neither surface carries, so it runs only when the caller passes
@@ -25,24 +26,45 @@
25
26
  * NEVER folded into a green verdict.
26
27
  *
27
28
  * Failure ordering is fixed so `brokenAt` is deterministic. The null-key
28
- * signature skip happens LAST, after the binding/OIDC checks, so a row written
29
- * without a signing key is still subject to every structural check.
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.
30
36
  */
31
37
  import { type AgentPublicKeyJwk } from './primitives.js';
38
+ import { type FederationKeyRegistry } from './federation-wire.js';
32
39
  import type { FailureCode } from './failures.js';
33
- /** Where a verification key came from: the trust-anchor provenance. */
34
- export type KeySource = 'out-of-band' | 'embedded';
40
+ /**
41
+ * Where a verification key came from. `supplied`: the caller passed it (from
42
+ * `GET /v1/verification-keys`, `/.well-known/scitt-keys`, its own records).
43
+ * `embedded`: the artifact under verification shipped it. Neither says the key
44
+ * is trusted: a key the Server serves comes from its database, which is what
45
+ * a key-registry attacker writes to. Trust comes from walking the signed key
46
+ * statements from a pinned anchor (`trustAnchors`, see `computeKeyTrust`),
47
+ * reported per key as `trust`.
48
+ */
49
+ export type KeySource = 'supplied' | 'embedded';
50
+ /**
51
+ * What the key-statement walk concluded about a key (see `applyKeyTrust`).
52
+ * `anchored`: signed statements link it to a pinned anchor. `unanchored`:
53
+ * nothing signed does, so entries under it fail CHAIN_SIGNING_KEY_UNANCHORED.
54
+ * `undecided`: the only link runs through a signature this host cannot
55
+ * compute, so entries under it are CHAIN_UNSUPPORTED_ALGORITHM, not tamper.
56
+ */
57
+ export type KeyTrustState = 'anchored' | 'unanchored' | 'undecided';
35
58
  /** A public key the walk can verify signatures against. */
36
59
  export interface VerificationKey {
37
60
  keyId: string;
38
61
  /** SPKI DER, base64-encoded (the engine's vault_signing_keys.public_key shape). */
39
62
  spkiBase64: string;
40
63
  /**
41
- * `out-of-band` = supplied by the caller from a trusted source (e.g.
42
- * /v1/verification-keys, /.well-known/scitt-keys). `embedded` = shipped inside
43
- * the artifact being verified (the export/dump the engine produced). Surfaced
44
- * in the result so a caller can tell whether it verified the engine against an
45
- * independent key or against the engine's own answer key.
64
+ * `supplied` = passed by the caller. `embedded` = shipped inside the
65
+ * artifact being verified (the export or dump the engine produced). Surfaced
66
+ * in the result so a caller can tell whether it verified against a key it
67
+ * brought or against the artifact's own answer key.
46
68
  */
47
69
  source: KeySource;
48
70
  /**
@@ -54,11 +76,43 @@ export interface VerificationKey {
54
76
  * code path; only the key material does.
55
77
  */
56
78
  algorithm?: string;
57
- /** Optional temporal-validity window (present on the dump path only). */
79
+ /** Optional temporal-validity window. */
58
80
  activatedAt?: string;
59
81
  retiredAt?: string | null;
82
+ /**
83
+ * The instant from which `distrustedKeys` (`VAULT_DISTRUSTED_KEYS` on the
84
+ * Server) voids what this key signs, set by `applyKeyTrust` when it ends the
85
+ * window before `retiredAt`. Entries written after it fail CHAIN_KEY_EXPIRED
86
+ * worded as past the distrust cutoff: the key was not retired there.
87
+ */
88
+ distrustCutoff?: string;
89
+ /**
90
+ * Set by `applyKeyTrust` on a walk over a dump, for a key `distrustedKeys`
91
+ * names: the instant from which what it signs counts for nothing, and the
92
+ * earliest retirement a key the walk trusts signed for it (see the
93
+ * `DistrustSpan` of `computeKeyTrust`). An entry it signed outside its
94
+ * trust (the key unanchored, or the entry written at or after the cutoff),
95
+ * written before that retirement and whose signature verifies under it, is
96
+ * accounted for: listed as CHAIN_SIGNED_BY_DISTRUSTED_KEY, never a failure.
97
+ */
98
+ distrustSpan?: {
99
+ cutoff: string | null;
100
+ retiredAt: string | null;
101
+ };
102
+ /**
103
+ * The key-statement walk's verdict on this key, set by `applyKeyTrust`.
104
+ * Absent when no walk ran (no `trustAnchors`), and the result then reports
105
+ * `optionalChecks.key_anchoring: skipped_no_input`; with a walk and no
106
+ * entry reaching the check, `not_checked`.
107
+ */
108
+ trust?: KeyTrustState;
60
109
  }
61
110
  export type KeyRegistry = ReadonlyMap<string, VerificationKey>;
111
+ /**
112
+ * Index keys by key id. A key whose `spkiBase64` is not a non-empty string
113
+ * carries no key material (a registry row with its public key nulled), so it
114
+ * is left out: an entry naming it fails CHAIN_SIGNATURE_MISSING_KEY.
115
+ */
62
116
  export declare function buildKeyRegistry(keys: readonly VerificationKey[]): KeyRegistry;
63
117
  /**
64
118
  * Agent cert keys for the agent-signature check, indexed by the RFC 7638
@@ -84,8 +138,14 @@ export interface NormalizedEntry {
84
138
  /** Base64-encoded canonical COSE_Sign1 envelope. */
85
139
  coseSign1: string;
86
140
  signingKeyId: string | null;
87
- /** ISO-8601 write time, for temporal key-validity (dump path). */
88
- createdAt?: string;
141
+ /**
142
+ * ISO-8601 write time, for temporal key-validity and the unsigned-entry
143
+ * rule. The engine writes one on every entry, so wherever the walk needs it
144
+ * (the entry's key carries a window, or the entry is unsigned and the key
145
+ * set dates the start of signing) a missing or unparseable value fails
146
+ * CHAIN_MALFORMED_ENTRY rather than skipping the check.
147
+ */
148
+ createdAt?: string | null;
89
149
  /** Inputs for the binding-integrity check (dump path). */
90
150
  binding?: {
91
151
  recordId: string | null;
@@ -109,6 +169,15 @@ export interface NormalizedEntry {
109
169
  actorRole: string | null;
110
170
  actorOwnerId: string | null;
111
171
  };
172
+ /**
173
+ * The evidence body an `?evidence=true` export inlines at the entry's
174
+ * `evidence` (export path). Present on the entry, however it is spelled, →
175
+ * the evidence-binding check runs: the body is an unsigned projection, and
176
+ * its authority comes only from hashing to the hash the entry signs.
177
+ */
178
+ inlinedEvidence?: {
179
+ value: unknown;
180
+ };
112
181
  }
113
182
  /**
114
183
  * Checks that run only when their input is present. `agent_signature` is
@@ -116,15 +185,26 @@ export interface NormalizedEntry {
116
185
  * had its cert key supplied by the caller (`agentKeys`); neither the export
117
186
  * nor the dump carries cert public keys.
118
187
  */
119
- export type OptionalCheck = 'payload_binding' | 'oidc_actor' | 'actor_attribution' | 'key_temporal' | 'agent_signature';
120
- export type CheckApplicability = 'applied' | 'skipped_no_input';
188
+ export type OptionalCheck = 'payload_binding' | 'oidc_actor' | 'actor_attribution' | 'key_temporal' | 'agent_signature' | 'key_anchoring' | 'federation_wire' | 'evidence_binding';
189
+ /**
190
+ * `applied`: the check ran. `skipped_no_input`: the input it needs is absent.
191
+ * `not_checked`: the input was given but no entry reached the check, because
192
+ * the chain broke before it or no entry was signed. Only `key_anchoring` has
193
+ * a caller-level input and so can be `not_checked`.
194
+ */
195
+ export type CheckApplicability = 'applied' | 'skipped_no_input' | 'not_checked';
121
196
  export interface SignatureOutcome {
122
197
  /**
123
198
  * - `ok` / `invalid` / `decode-fail`: the signature was checked (and passed,
124
199
  * failed, or the envelope would not decode).
125
- * - `unsigned`: the entry carries no signature by design.
200
+ * - `unsigned`: retained for type compatibility; `verifyChain` no longer
201
+ * produces it. An all-zero signature on an entry that names a key is
202
+ * `invalid` (CHAIN_SIGNATURE_INVALID), as the engine grades it.
126
203
  * - `skipped`: the chain is intact but this entry has no signing key, so the
127
- * signature check was deliberately not run (engine booted without a key).
204
+ * signature check was deliberately not run. Only an entry written before
205
+ * the install began signing gets here: before any signed entry in its
206
+ * chain and before the earliest key activation. Any other unsigned entry
207
+ * fails CHAIN_ENTRY_UNSIGNED.
128
208
  * - `not-checked`: a structural/chain check failed at or before this entry,
129
209
  * so verification short-circuited before reaching the signature. Reads as a
130
210
  * consequence of an upstream break, never as a benign skip.
@@ -134,8 +214,12 @@ export interface SignatureOutcome {
134
214
  * CHAIN_UNSUPPORTED_ALGORITHM. A failure state, never a benign skip, and
135
215
  * never tamper evidence: distinguishing it from `invalid` is the whole
136
216
  * point, since only `invalid` means a signature was checked and failed.
217
+ * - `accounted`: the signature verifies under a key `distrustedKeys` names,
218
+ * outside what the key is trusted for, and the entry was written before a
219
+ * key the walk trusts retired it (dumps only). Not a failure and not a
220
+ * verified entry: see {@link AccountedEntry}.
137
221
  */
138
- state: 'ok' | 'invalid' | 'unsigned' | 'skipped' | 'not-checked' | 'decode-fail' | 'unsupported';
222
+ state: 'ok' | 'invalid' | 'unsigned' | 'skipped' | 'not-checked' | 'decode-fail' | 'unsupported' | 'accounted';
139
223
  /** Provenance of the key the signature was checked against ('ok' / 'invalid'). */
140
224
  keySource?: KeySource;
141
225
  }
@@ -147,9 +231,46 @@ export interface ChainEntryResult {
147
231
  code: FailureCode;
148
232
  detail: string;
149
233
  };
234
+ /** Set, with `valid: true` and signature `accounted`, on an entry the distrust entry accounts for. */
235
+ accounted?: {
236
+ code: AccountedEntryCode;
237
+ detail: string;
238
+ };
150
239
  signature: SignatureOutcome['state'];
151
240
  keySource?: KeySource;
152
241
  }
242
+ /** The code an {@link AccountedEntry} carries. Not a {@link FailureCode}: it never fails a verdict. */
243
+ export declare const ACCOUNTED_ENTRY_CODE = "CHAIN_SIGNED_BY_DISTRUSTED_KEY";
244
+ export type AccountedEntryCode = typeof ACCOUNTED_ENTRY_CODE;
245
+ /**
246
+ * A chain entry of a dump whose signature verifies under a key
247
+ * `distrustedKeys` names, that falls outside what the key is trusted for
248
+ * (the key is unanchored, or the entry was written at or after its cutoff),
249
+ * and that was written before a key the walk trusts retired it. The distrust
250
+ * entry and that retirement account for it, as the engine's scan lists it in
251
+ * `distrustedEntries`: it is listed, it is not verified, and it fails
252
+ * nothing. An entry written after that retirement, or under a distrusted key
253
+ * no trusted key has retired, fails as before (CHAIN_SIGNING_KEY_UNANCHORED
254
+ * or CHAIN_KEY_EXPIRED). An audit export never carries one.
255
+ *
256
+ * `chain` and `recordId`/`orgId` name the chain as the engine does: a record
257
+ * chain by its record id, the platform-ops chain (record id
258
+ * 00000000-0000-0000-0000-000000000000) as `admin`, and an org's schema chain
259
+ * (`schema:<orgId>`, `schema:__platform__` for the platform's) as `schema`.
260
+ */
261
+ export interface AccountedEntry {
262
+ code: AccountedEntryCode;
263
+ chain: 'record' | 'admin' | 'schema';
264
+ recordId: string | null;
265
+ orgId: string | null;
266
+ /** The chain's scope as the walk was given it (a record id, or a dump's `chain_key`). */
267
+ scopeId: string;
268
+ position: number;
269
+ keyId: string;
270
+ detail: string;
271
+ }
272
+ /** Name a chain scope (a record id or a dump `chain_key`) as {@link AccountedEntry} does. */
273
+ export declare function chainOfScope(scopeId: string): Pick<AccountedEntry, 'chain' | 'recordId' | 'orgId'>;
153
274
  export interface ChainResult {
154
275
  scopeId: string;
155
276
  valid: boolean;
@@ -161,6 +282,12 @@ export interface ChainResult {
161
282
  detail: string;
162
283
  };
163
284
  entries: ChainEntryResult[];
285
+ /**
286
+ * Entries the distrust entry accounts for (see {@link AccountedEntry}), in
287
+ * chain order. They count toward `valid` and not toward `verifiedEntries`
288
+ * or `signatureCoverage.signed`.
289
+ */
290
+ accounted: AccountedEntry[];
164
291
  signatureCoverage: {
165
292
  signed: number;
166
293
  unsigned: number;
@@ -169,9 +296,9 @@ export interface ChainResult {
169
296
  };
170
297
  /** Which input-gated checks actually ran on this chain vs were skipped for absent input. */
171
298
  optionalChecks: Record<OptionalCheck, CheckApplicability>;
172
- /** How many signature checks resolved against out-of-band vs embedded keys. */
299
+ /** How many signature checks resolved against supplied vs embedded keys. */
173
300
  keyProvenance: {
174
- outOfBand: number;
301
+ supplied: number;
175
302
  embedded: number;
176
303
  };
177
304
  /**
@@ -187,16 +314,57 @@ export interface ChainResult {
187
314
  present: number;
188
315
  verified: number;
189
316
  };
317
+ /**
318
+ * Federation messages sealed on this chain (`payload.wire` on a receiving
319
+ * Server's FEDERATION_* entries). `present` counts entries that passed every
320
+ * other check and carry one, each held to its own body hash and to the entry
321
+ * that carries it; `verified` counts those whose sealed signature was also
322
+ * checked under a `federationKeys` entry for the sending hub and found good;
323
+ * `failed` counts those that failed a check (a body hash, the entry, or the
324
+ * signature under the key given for the hub). `present - verified - failed`
325
+ * is the messages from a hub with no key supplied. `afterRetirement`
326
+ * counts verified ones sealed under a key the sender's history retired
327
+ * before the sender signed the message (see `FederationWireOutcome`): in
328
+ * order when the receiver revoked the peer after them, so check its
329
+ * `federation.peer_revoked` audit row.
330
+ */
331
+ federationWire: {
332
+ present: number;
333
+ verified: number;
334
+ failed: number;
335
+ afterRetirement: number;
336
+ };
337
+ /**
338
+ * Evidence bodies an `?evidence=true` export inlines (`entries[].evidence`
339
+ * on COMPLETION_SUBMITTED and DISPUTE_EVIDENCE). `present` counts entries
340
+ * that passed every other check and carry one; `verified` those whose body
341
+ * hashes, under RFC 8785 and SHA-256, to the signed `payload.evidenceHash`
342
+ * or `payload.payloadHash`; `clientHashed` those on an encrypted-mode
343
+ * completion (`payload.verifiedBy: "client"`), whose signed hash covers a
344
+ * cleartext the export does not carry, so the inlined ciphertext cannot be
345
+ * re-bound offline; `failed` those that did not hash to it, or sit where
346
+ * nothing signed binds a body. Always zero on a dump, which carries none.
347
+ */
348
+ evidenceBinding: EvidenceBindingCounts;
349
+ }
350
+ /** See {@link ChainResult.evidenceBinding}. */
351
+ export interface EvidenceBindingCounts {
352
+ present: number;
353
+ verified: number;
354
+ clientHashed: number;
355
+ failed: number;
190
356
  }
191
357
  export interface VerifyChainOptions {
192
358
  /** Require every entry's signingKeyId to equal this id (else CHAIN_KEY_POLICY_VIOLATION). */
193
359
  requireKeyId?: string;
194
360
  /**
195
- * High-assurance auditor mode: refuse to verify against keys shipped inside
196
- * the artifact. An entry whose only available key is `embedded` fails
197
- * CHAIN_KEY_POLICY_VIOLATION. Forces the caller to supply keys out of band.
361
+ * Refuse to verify against keys shipped inside the artifact: an entry whose
362
+ * only available key is `embedded` fails CHAIN_KEY_POLICY_VIOLATION. This
363
+ * says where a key came from, not that it is trusted; a key the Server
364
+ * serves comes from its database. Anchor keys with `computeKeyTrust` /
365
+ * `applyKeyTrust` for that.
198
366
  */
199
- requireOutOfBandKeys?: boolean;
367
+ requireSuppliedKeys?: boolean;
200
368
  /**
201
369
  * Agent cert keys (see `buildAgentKeyRegistry`). When an entry's signed
202
370
  * payload carries an engine-validated `on_behalf_of.agent_signature` and its
@@ -204,7 +372,54 @@ export interface VerifyChainOptions {
204
372
  * offline; a failure is CHAIN_AGENT_SIGNATURE_INVALID.
205
373
  */
206
374
  agentKeys?: AgentKeyRegistry;
375
+ /**
376
+ * Sending hubs' federation keys (see `buildFederationKeyRegistry`). An entry
377
+ * that seals a peer's message (`payload.wire`) from a hub listed here has
378
+ * that message's signature re-verified under the key; a failure is
379
+ * CHAIN_FEDERATION_WIRE_INVALID. The hash and the entry's agreement with
380
+ * the sealed body are checked on every sealed message, with or without a key.
381
+ */
382
+ federationKeys?: FederationKeyRegistry;
383
+ /**
384
+ * The instant this install began signing: the earliest `activatedAt` across
385
+ * its whole signing key set, retired keys included. An entry with a null
386
+ * `signingKeyId` whose `createdAt` is at or after it fails
387
+ * CHAIN_ENTRY_UNSIGNED.
388
+ *
389
+ * Omitted, it is derived from `keys` (`earliestKeyActivation`), which is
390
+ * right whenever `keys` is the whole key set, as it is on the dump path.
391
+ * Pass it when the caller knows windows for keys it has no public key for.
392
+ * `null` says the key set carries no activation time, so only the other
393
+ * half of the rule (an unsigned entry after a signed one) applies. A string
394
+ * that does not parse as a date throws `TypeError` rather than silently
395
+ * switching the check off.
396
+ */
397
+ signingSince?: string | null;
207
398
  }
399
+ /**
400
+ * The earliest `activatedAt` among `keys`, as the ISO string it was given in,
401
+ * or `null` when no key carries a parseable one. This is the instant from
402
+ * which the engine treats every chain entry and checkpoint as required to be
403
+ * signed; pass every key the verifier knows, retired ones included, since the
404
+ * engine reads it as `min(activated_at)` over its whole key registry.
405
+ */
406
+ export declare function earliestKeyActivation(keys: Iterable<{
407
+ activatedAt?: string | null;
408
+ }>): string | null;
409
+ /**
410
+ * Whether a row with no signing key id, written at `writtenAt`, falls inside
411
+ * the era in which the install signs everything: at or after `signingSince`
412
+ * (see `earliestKeyActivation`). This is the rule for an unsigned checkpoint
413
+ * (CHECKPOINT_UNSIGNED) and an unsigned read-log leaf or tree head, and the
414
+ * time half of the rule for an unsigned entry (CHAIN_ENTRY_UNSIGNED).
415
+ *
416
+ * False when `signingSince` is absent or unparseable: with no instant from
417
+ * which the install signs, nothing has to be signed. True when `signingSince`
418
+ * is known and `writtenAt` is missing or not strict RFC 3339: the engine times every
419
+ * row, so a row with no readable time was edited, and it cannot be placed
420
+ * before signing began. It fails closed rather than reading as early history.
421
+ */
422
+ export declare function writtenWhileSigning(writtenAt: unknown, signingSince: string | null | undefined): boolean;
208
423
  /**
209
424
  * Verify a single hash chain. `entries` are all the entries for one scope; they
210
425
  * are sorted by chainPosition internally (so a reordered export array still