@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.
@@ -0,0 +1,429 @@
1
+ import type { KeyRegistry } from './chain.js';
2
+ /** Content type of a key statement's COSE_Sign1 (protected header label 3). */
3
+ export declare const KEY_STATEMENT_CTY = "application/vnd.agledger.key-statement+cbor";
4
+ export declare const KEY_STATEMENT_KINDS: readonly ['succession', 'closure', 'genesis'];
5
+ export type KeyStatementKind = (typeof KEY_STATEMENT_KINDS)[number];
6
+ /**
7
+ * One key statement as a source carries it. A dump row maps onto it with
8
+ * {@link keyStatementFromDumpRow}; a key document's `statements` with
9
+ * {@link keyStatementsFromVerificationKeys} or
10
+ * {@link keyStatementsFromExport}.
11
+ */
12
+ export interface KeyStatementInput {
13
+ /**
14
+ * Row id (the dump's or a key document's `id`), named in findings. Under the
15
+ * write order it breaks a tie between two statements whose `createdAt` are
16
+ * the same microsecond, as the engine's `created_at, id` does.
17
+ */
18
+ id?: string | null;
19
+ /** The source's own `kind` column, bound to the signed `typ`. */
20
+ kind: string;
21
+ /** The key id the source files the statement under, bound to the signed `subject.kid`. */
22
+ subjectKeyId?: string;
23
+ /**
24
+ * The source's `endorser_key_id` column, bound to the signed `endorser.kid`
25
+ * (`null` for a genesis). Omitted when the source carries no such column, as
26
+ * the key documents do not.
27
+ */
28
+ endorserKeyId?: string | null;
29
+ /**
30
+ * Where the statement was read from. `dump`: a row of a dump's
31
+ * `vault_key_statements.ndjson` ({@link keyStatementFromDumpRow} sets it),
32
+ * whose `created_at` the walk holds a distrusted key's statements to, as the
33
+ * engine does. Anything else, absent included, is a key document's (an
34
+ * export's or a supplied `/v1/verification-keys` entry's): its `createdAt`
35
+ * orders it but never keeps an edge out of a distrusted key.
36
+ */
37
+ source?: 'dump' | 'document';
38
+ /** The COSE_Sign1 signatures, base64 or bytes, in signing order. */
39
+ cose: ReadonlyArray<string | Uint8Array>;
40
+ /**
41
+ * The database write time (the dump's `created_at`, a key document's
42
+ * `createdAt`). Give it for every statement or for none: with it the walk
43
+ * applies the write order, without it the signed order (see the module
44
+ * comment). Statements are ordered by it at the precision given, then, for
45
+ * two at the same microsecond, by `id`, and otherwise in input order. Under
46
+ * the write order a statement whose `createdAt` is not a strict RFC 3339
47
+ * instant (a `T`, an offset or `Z`, a real calendar date) cannot be placed,
48
+ * and is KEY_STATEMENT_INVALID (the adapters give `''` for a row or
49
+ * document statement without one).
50
+ */
51
+ createdAt?: string;
52
+ }
53
+ /**
54
+ * A key the source lists, the walk's fallback for the material of an endorser
55
+ * no statement names as its subject, and the columns the drift check holds
56
+ * against the signed window. Window and status fields are compared only when
57
+ * given.
58
+ */
59
+ export interface TrustKeyInput {
60
+ keyId: string;
61
+ /** SPKI DER, base64. */
62
+ publicKey: string;
63
+ algorithm?: string | null;
64
+ status?: 'active' | 'retired' | null;
65
+ activatedAt?: string | null;
66
+ retiredAt?: string | null;
67
+ /**
68
+ * The instant from which the Server's own `VAULT_DISTRUSTED_KEYS` entry
69
+ * makes what the key signs count for nothing, as a key document or an
70
+ * export's `signingKeyWindows` publishes it (RFC 3339 UTC, microsecond
71
+ * precision). It is the source's word and only ever changes how a finding
72
+ * on the listed window is worded: a listed `retiredAt` that equals it,
73
+ * where the walk signs a later retirement or none, names the distrust entry
74
+ * the walk was not given (or says the one it was given disagrees) instead
75
+ * of reading as a rewritten column. It never ends, opens or widens a window,
76
+ * and never clears a finding; only `distrustedKeys` does what an entry does.
77
+ */
78
+ distrustedFrom?: string | null;
79
+ /**
80
+ * Where the key was read from. `dump`: a row of a dump's
81
+ * `vault_signing_keys.ndjson` ({@link trustKeyFromDumpRow} sets it), a
82
+ * registry fact the walk holds a distrusted key to: one no trusted key has
83
+ * retired is KEY_CLOSURE_INVALID. Anything else, absent included, is a key
84
+ * document's listing.
85
+ */
86
+ source?: 'dump' | 'document';
87
+ }
88
+ /**
89
+ * A key distrusted from outside the database, the verifier-side mirror of the
90
+ * Server's `VAULT_DISTRUSTED_KEYS`. See {@link parseDistrustedKeys}.
91
+ */
92
+ export interface DistrustedKey {
93
+ /** Full SHA-256 of the key's SPKI DER, lowercase hex. */
94
+ spkiSha256: string;
95
+ /**
96
+ * RFC 3339 UTC instant at microsecond precision from which what the key
97
+ * signs counts for nothing. Null: the earliest `retiredAt` a counting
98
+ * closure by a key that is not distrusted signs for it, and when there is
99
+ * none the key is trusted for nothing.
100
+ */
101
+ cutoff: string | null;
102
+ }
103
+ export interface ComputeKeyTrustInput {
104
+ keys: readonly TrustKeyInput[];
105
+ statements: readonly KeyStatementInput[];
106
+ /** `sha256:<64 hex>` SPKI digests pinned out of band. At least one. */
107
+ trustAnchors: readonly string[];
108
+ /** `VAULT_DISTRUSTED_KEYS` entries (strings) or parsed {@link DistrustedKey}s. */
109
+ distrustedKeys?: ReadonlyArray<string | DistrustedKey>;
110
+ }
111
+ export type KeyRegistryFindingCode = 'KEY_STATEMENT_INVALID' | 'KEY_CLOSURE_INVALID' | 'CHAIN_KEY_WINDOW_DRIFT';
112
+ /** A finding about the key registry itself, not about any chain entry. */
113
+ export interface KeyRegistryFinding {
114
+ code: KeyRegistryFindingCode;
115
+ keyId: string | null;
116
+ statementId: string | null;
117
+ detail: string;
118
+ }
119
+ /**
120
+ * Something the walk did that is not a finding and does not fail a verdict:
121
+ * a statement from a key document signed by a distrusted key that admits
122
+ * nothing here, because the document's write time is the holder's word and is
123
+ * never held against the key's cutoff, though the engine, holding it to the
124
+ * time it was stored, counts it. It still dates windows and cuts edges back.
125
+ * Also a listed key whose `distrustedFrom` differs from the instant the
126
+ * caller's `distrustedKeys` entry gives it, where no finding on its window
127
+ * already says so.
128
+ */
129
+ export interface KeyTrustNote {
130
+ keyId: string | null;
131
+ statementId: string | null;
132
+ detail: string;
133
+ }
134
+ /** What the walk concludes about one key. */
135
+ export interface KeyTrustEntry {
136
+ keyId: string;
137
+ spkiSha256: string;
138
+ /** Anchored: reached from `trustAnchors` over edges that count. */
139
+ trusted: boolean;
140
+ /**
141
+ * Reached only through a statement signed under an algorithm this host
142
+ * cannot compute, and the key's own algorithm is one of those. Entries
143
+ * signed by it cannot be verified here, which is not tamper evidence.
144
+ */
145
+ undecided: boolean;
146
+ /** The signed lower edge, or null when no counting statement signs one. */
147
+ activatedAt: string | null;
148
+ /** The signed upper edge, or null when no counting closure signs one. */
149
+ retiredAt: string | null;
150
+ /**
151
+ * The instant from which `distrustedKeys` voids what this key signs, when it
152
+ * ends the key's window before its signed retirement, else null. The key is
153
+ * not retired there: entries written after it fail CHAIN_KEY_EXPIRED as
154
+ * past the distrust cutoff, not as past a retirement.
155
+ */
156
+ distrustCutoff: string | null;
157
+ }
158
+ export interface KeyTrust {
159
+ /**
160
+ * `written` when every statement carried `createdAt` (a dump, or a key
161
+ * document from a Server that publishes each statement's write time), else
162
+ * `signed` (an older document; see the module comment).
163
+ */
164
+ order: 'written' | 'signed';
165
+ /** The anchors walked from, as `sha256:<hex>`. */
166
+ anchors: string[];
167
+ /** Every key the walk saw, by full SPKI SHA-256 (hex). */
168
+ byDigest: ReadonlyMap<string, KeyTrustEntry>;
169
+ /** SPKI digests of the trusted keys. */
170
+ trusted: ReadonlySet<string>;
171
+ /** SPKI digests of the undecided keys. */
172
+ undecided: ReadonlySet<string>;
173
+ findings: KeyRegistryFinding[];
174
+ /** Non-fatal: see {@link KeyTrustNote}. */
175
+ notes: KeyTrustNote[];
176
+ /**
177
+ * Non-fatal: statements of a dump that a `distrustedKeys` key signed, that
178
+ * count for nothing, and that were stored before a key the walk trusts
179
+ * retired it (its {@link DistrustSpan} `retiredAt`). The distrust entry and
180
+ * that retirement account for them: they are evidence of what the key
181
+ * signed, listed and never a finding. One stored after that retirement, or
182
+ * whose dropping reopens a key, stays a finding. Empty for a key document,
183
+ * whose write times are not held against anything.
184
+ */
185
+ accounted: KeyTrustNote[];
186
+ /** Each `distrustedKeys` key, by full SPKI SHA-256 (hex): see {@link DistrustSpan}. */
187
+ distrustSpans: ReadonlyMap<string, DistrustSpan>;
188
+ /**
189
+ * `dump` when every statement is a dump row (and there is at least one),
190
+ * else `document`. Only a dump's write times are the Server's word, so only
191
+ * a walk over a dump marks the keys `applyKeyTrust` lets a chain entry be
192
+ * accounted for under (see {@link DistrustSpan}).
193
+ */
194
+ source: 'dump' | 'document';
195
+ statements: {
196
+ total: number;
197
+ valid: number;
198
+ invalid: number;
199
+ unverifiable: number;
200
+ };
201
+ }
202
+ /**
203
+ * What a `distrustedKeys` entry covers for one key, as the engine's
204
+ * `distrustSpans` gives it. `cutoff`: the instant from which what the key
205
+ * signs counts for nothing (the entry's own, else `retiredAt`; null: trusted
206
+ * for nothing). `retiredAt`: the earliest retirement of the key that a key
207
+ * which is not distrusted signs, reached from the anchors without passing
208
+ * through a distrusted key, each closure's signed `retiredAt` capped at its
209
+ * own write time (null: none). On a dump, what the key signed before
210
+ * `retiredAt` and outside its trust is accounted for, and listed rather than
211
+ * failed; what it signed after `retiredAt` is not.
212
+ */
213
+ export interface DistrustSpan {
214
+ cutoff: string | null;
215
+ retiredAt: string | null;
216
+ }
217
+ /** The full SHA-256 of a base64 SPKI DER, lowercase hex. */
218
+ export declare function spkiSha256(spkiBase64: string): string;
219
+ /**
220
+ * Parse trust anchors: `sha256:<64 hex>` entries, as an array or a comma list
221
+ * (the Server's `VAULT_TRUST_ANCHORS` form). Returns the bare lowercase hex
222
+ * digests. Throws `TypeError` naming any entry in another shape.
223
+ */
224
+ export declare function parseTrustAnchors(raw: string | readonly string[]): string[];
225
+ /**
226
+ * Parse distrusted keys in the Server's `VAULT_DISTRUSTED_KEYS` form, as an
227
+ * array or a comma list: `sha256:<64 hex>`, each optionally `@<RFC 3339
228
+ * instant>`, the instant from which what the key signs counts for nothing.
229
+ * The instant comes back in UTC at microsecond precision. Throws `TypeError`
230
+ * naming any other entry, or a key named twice.
231
+ */
232
+ export declare function parseDistrustedKeys(raw: string | readonly string[]): DistrustedKey[];
233
+ /**
234
+ * Throw `TypeError` when a key is pinned and distrusted with no instant, as
235
+ * the Server refuses to boot on that pair (`VaultKeyDistrustedError`): an
236
+ * undated entry withdraws the key from before anything it signed, which
237
+ * leaves a pin nothing to vouch for. A pin beside a dated entry
238
+ * (`sha256:<hex>@<instant>`) is how a leaked key whose history is still
239
+ * needed is kept from signing anything new: the pin vouches for what it
240
+ * stored before the instant. `computeKeyTrust` itself walks any pair as the
241
+ * engine's walk does; a verifier calls this on what its caller passed before
242
+ * it walks.
243
+ */
244
+ export declare function assertNotPinnedAndDistrusted(trustAnchors: string | readonly string[] | undefined, distrustedKeys: ReadonlyArray<string | DistrustedKey> | undefined): void;
245
+ /**
246
+ * Walk the key statements from `trustAnchors` and decide which keys are
247
+ * trusted and what window each carries. Two passes, no fixpoint:
248
+ *
249
+ * 1. Reach every key from the anchors over every edge, ignoring closures.
250
+ * 2. Apply every closure whose signer pass 1 reaches: a key's window ends at
251
+ * the earliest `retiredAt` among them, a forced one voids every edge out
252
+ * of the key, forward and back, and any edge out of the key stored after
253
+ * its first such closure is void.
254
+ * 3. Reach again without the void edges, taking a key's edge back only
255
+ * through its sole admission. That set is the trusted set.
256
+ *
257
+ * The edges: a succession E->K links E forward to K and K back to E. A key's
258
+ * admission is the first genesis or succession naming it; pass 3 takes K's
259
+ * edge back only when the succession is K's admission and K has exactly one.
260
+ *
261
+ * Closures only remove edges and shorten windows, so a closure signed by a
262
+ * leaked key is at worst a denial of service, never a key trusted that was
263
+ * not before. A distrusted key is the one thing that makes a closure stop
264
+ * counting: what it stores at or after its cutoff counts for nothing.
265
+ *
266
+ * Throws `TypeError` on malformed anchors or distrusted keys, on no anchors
267
+ * at all, and when some statements carry `createdAt` and others do not.
268
+ */
269
+ export declare function computeKeyTrust(input: ComputeKeyTrustInput): KeyTrust;
270
+ /** A `vault_key_statements.ndjson` row, as the dump writes it. */
271
+ export interface DumpKeyStatementRow {
272
+ id: string;
273
+ kind: string;
274
+ subject_key_id: string;
275
+ endorser_key_id: string | null;
276
+ statement: string[];
277
+ created_at: string;
278
+ }
279
+ /**
280
+ * Map a dump statement row onto the walk's input, binding every column. The
281
+ * Server writes `subject_key_id` and `created_at` on every row, so one that is
282
+ * missing or not a string maps to `''`, which matches no signed key id and
283
+ * places the statement nowhere: it is KEY_STATEMENT_INVALID.
284
+ */
285
+ export declare function keyStatementFromDumpRow(row: DumpKeyStatementRow): KeyStatementInput;
286
+ /** A `vault_signing_keys.ndjson` row, as the dump writes it. */
287
+ export interface DumpSigningKeyRow {
288
+ key_id: string;
289
+ public_key: string;
290
+ algorithm?: string | null;
291
+ status?: string | null;
292
+ activated_at?: string | null;
293
+ retired_at?: string | null;
294
+ }
295
+ /** Map a dump key row onto the walk's input. */
296
+ export declare function trustKeyFromDumpRow(row: DumpSigningKeyRow): TrustKeyInput;
297
+ /**
298
+ * A key's statement as the key documents list it. A Server that publishes
299
+ * write order gives each its row `id` and `createdAt` (RFC 3339 UTC at
300
+ * microsecond precision); an older one gives neither.
301
+ */
302
+ export interface PublishedKeyStatement {
303
+ /** The statement row's id (a uuid): the write order's tie-break. */
304
+ id?: string;
305
+ kind: string;
306
+ /** When the database stored the statement: the write order. */
307
+ createdAt?: string;
308
+ cose: string[];
309
+ }
310
+ /**
311
+ * The statements of a document keyed by key id. When any statement of the
312
+ * document carries `createdAt`, the document publishes write order, so one
313
+ * without it (or with another type) maps to `''` and is KEY_STATEMENT_INVALID
314
+ * rather than turning the whole document back to the signed order. A
315
+ * statement with no `id` is named `<prefix><keyId>#<index>`. Internal: the
316
+ * export path reads supplied keys' statements with it; not re-exported.
317
+ */
318
+ export declare function statementsFromMap(byKey: Iterable<[string, unknown]>, source: string, prefix?: string): KeyStatementInput[];
319
+ /** The key statements of an audit export's `exportMetadata.signingKeyStatements`. */
320
+ export declare function keyStatementsFromExport(signingKeyStatements: Readonly<Record<string, readonly PublishedKeyStatement[]>> | undefined | null): KeyStatementInput[];
321
+ /** The `/v1/verification-keys` document, as far as the walk reads it. */
322
+ export interface VerificationKeysDocument {
323
+ data: ReadonlyArray<{
324
+ keyId: string;
325
+ publicKey: string;
326
+ algorithm?: string | null;
327
+ status?: string | null;
328
+ activatedAt?: string | null;
329
+ retiredAt?: string | null;
330
+ /** The Server's `VAULT_DISTRUSTED_KEYS` instant for the key: see {@link TrustKeyInput.distrustedFrom}. */
331
+ distrustedFrom?: string | null;
332
+ statements?: readonly PublishedKeyStatement[];
333
+ }>;
334
+ anchoredFrom?: string | null;
335
+ keyStatementFormat?: string;
336
+ }
337
+ /**
338
+ * The walk's inputs from a `/v1/verification-keys` document: its keys and
339
+ * the statements each carries. Throws `TypeError` when the document names a
340
+ * statement format this verifier does not read.
341
+ */
342
+ export declare function keyStatementsFromVerificationKeys(doc: VerificationKeysDocument): {
343
+ keys: TrustKeyInput[];
344
+ statements: KeyStatementInput[];
345
+ };
346
+ /**
347
+ * Mark every key of a registry with the walk's verdict, and give each anchored
348
+ * key the window its statements sign in place of whatever window it carried,
349
+ * as the engine grades entries. An anchored key no statement dates carries no
350
+ * edge on that side. A key is anchored only when its key id is the
351
+ * fingerprint of its own SPKI: a row filed under another key's id names that
352
+ * key's entries, and is anchored to nothing. On a walk over a dump, a key a
353
+ * `distrustedKeys` entry names also carries its {@link DistrustSpan}
354
+ * (`distrustSpan`), which lets `verifyChain` account for what it signed.
355
+ */
356
+ export declare function applyKeyTrust(registry: KeyRegistry, trust: KeyTrust): KeyRegistry;
357
+ /**
358
+ * Whether a verification can be read as trusted.
359
+ *
360
+ * - `walked`: the key statements were walked from the caller's anchors and at
361
+ * least one signature verified under a key they anchor. The one status a
362
+ * passing result is trusted on.
363
+ * - `no_anchor`: no `trustAnchors` were given, so no key was anchored and
364
+ * every key was taken on the word of whoever embedded or supplied it. A
365
+ * pass is not a trusted verdict.
366
+ * - `no_anchored_signature`: the walk ran, but no signature in the artifact
367
+ * verified under a key it anchors (every entry is unsigned history, or the
368
+ * chain broke first). An unsigned entry proves nothing about who wrote it,
369
+ * so a pass is not a trusted verdict either.
370
+ *
371
+ * Every surface reads `no_anchor` and `no_anchored_signature` alike: a pass
372
+ * on either is `unanchored`, never `trusted`.
373
+ */
374
+ export type KeyTrustStatus = 'walked' | 'no_anchor' | 'no_anchored_signature';
375
+ /**
376
+ * The one-word reading of a verification result, as every surface prints it:
377
+ * `failed` when it is not valid, `trusted` when it is valid and the walk
378
+ * anchored a signature (`keyTrust.status: 'walked'`), and `unanchored` when it
379
+ * is valid but rests on nothing pinned. Read this rather than `valid` alone: a
380
+ * valid result with no `trustAnchors` is `unanchored`.
381
+ */
382
+ export type Verdict = 'trusted' | 'unanchored' | 'failed';
383
+ /** The {@link Verdict} of a result that carries `valid` and a `keyTrust` report. */
384
+ export declare function verdictOf(result: {
385
+ valid: boolean;
386
+ keyTrust: Pick<KeyTrustReport, 'status'>;
387
+ }): Verdict;
388
+ /** What a verification result says about key anchoring. */
389
+ export interface KeyTrustReport {
390
+ /** See {@link KeyTrustStatus}. */
391
+ status: KeyTrustStatus;
392
+ detail: string;
393
+ /** The anchors walked from, as `sha256:<hex>`. */
394
+ anchors: string[];
395
+ /** The artifact's own claim of the key it was anchored from (`sha256:<hex>`), when it carries one. */
396
+ anchoredFrom: string | null;
397
+ /** Whether `anchoredFrom` is one of `anchors`; null when either is absent. */
398
+ anchoredFromPinned: boolean | null;
399
+ /** How the statements were ordered (see `computeKeyTrust`); null when no walk ran. */
400
+ order: 'written' | 'signed' | null;
401
+ anchoredKeyIds: string[];
402
+ unanchoredKeyIds: string[];
403
+ undecidedKeyIds: string[];
404
+ findings: KeyRegistryFinding[];
405
+ /** Non-fatal notes from the walk (see {@link KeyTrustNote}); they never fail a verdict. */
406
+ notes: KeyTrustNote[];
407
+ /**
408
+ * Non-fatal: the statements of a dump a `distrustedKeys` key signed that its
409
+ * entry and a trusted key's retirement of it account for (see
410
+ * {@link KeyTrust.accounted}). Listed, never a finding.
411
+ */
412
+ accounted: KeyTrustNote[];
413
+ }
414
+ /**
415
+ * Summarize a registry after `applyKeyTrust` (or with `trust` null when no
416
+ * walk ran). A walked report says `walked` until the caller settles it with
417
+ * {@link settleKeyTrust}, once it knows whether any signature verified under
418
+ * an anchored key. `anchoredFrom` that is not a string is read as absent.
419
+ */
420
+ export declare function reportKeyTrust(registry: KeyRegistry, trust: KeyTrust | null, anchoredFrom: string | null): KeyTrustReport;
421
+ /**
422
+ * Settle a walked report once the caller has counted the signatures that
423
+ * verified under an anchored key (an export's or a dump's entries whose
424
+ * signature checked out, since under a walk every such key is anchored).
425
+ * With none, the report becomes `no_anchored_signature`: the pin was walked,
426
+ * but nothing in the artifact is signed by a key it anchors, so a pass is not
427
+ * a trusted verdict. Any other report is returned as it is.
428
+ */
429
+ export declare function settleKeyTrust(report: KeyTrustReport, anchoredSignatures: number): KeyTrustReport;