@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.
- package/LICENSE +18 -18
- package/README.md +797 -29
- package/dist/acknowledgements.d.ts +32 -0
- package/dist/acknowledgements.js +88 -0
- package/dist/anchors.d.ts +349 -0
- package/dist/anchors.js +654 -0
- package/dist/attestation.d.ts +98 -0
- package/dist/attestation.js +145 -0
- package/dist/audit-export.d.ts +343 -20
- package/dist/audit-export.js +400 -22
- package/dist/batch-head.d.ts +28 -0
- package/dist/batch-head.js +169 -0
- package/dist/batch.d.ts +69 -0
- package/dist/batch.js +99 -0
- package/dist/bulk-export.d.ts +161 -0
- package/dist/bulk-export.js +711 -0
- package/dist/chain.d.ts +135 -13
- package/dist/chain.js +229 -24
- package/dist/claim.d.ts +23 -0
- package/dist/claim.js +100 -0
- package/dist/export-anchors.d.ts +84 -0
- package/dist/export-anchors.js +109 -0
- package/dist/export-evidence.d.ts +90 -0
- package/dist/export-evidence.js +186 -0
- package/dist/export-pages.d.ts +476 -0
- package/dist/export-pages.js +586 -0
- package/dist/failures.d.ts +20 -16
- package/dist/failures.js +44 -16
- package/dist/federation-wire.d.ts +78 -11
- package/dist/federation-wire.js +199 -57
- package/dist/history-seal.d.ts +100 -0
- package/dist/history-seal.js +301 -0
- package/dist/index.d.ts +36 -8
- package/dist/index.js +17 -4
- package/dist/key-statements.d.ts +138 -5
- package/dist/key-statements.js +191 -20
- package/dist/ml-dsa-kats.d.ts +18 -0
- package/dist/ml-dsa-kats.js +241 -0
- package/dist/primitives.d.ts +255 -18
- package/dist/primitives.js +612 -55
- package/dist/read-log.d.ts +350 -0
- package/dist/read-log.js +816 -0
- package/dist/schema-chain-export.d.ts +82 -0
- package/dist/schema-chain-export.js +474 -0
- package/dist/walk-anchors.d.ts +115 -0
- package/dist/walk-anchors.js +133 -0
- package/dist/walk-checkpoints.d.ts +125 -0
- package/dist/walk-checkpoints.js +118 -0
- package/package.json +6 -5
package/dist/federation-wire.js
CHANGED
|
@@ -15,7 +15,11 @@
|
|
|
15
15
|
*
|
|
16
16
|
* bodyHash = sha256 hex over the RFC 8785 (JCS) canonical JSON of `body`
|
|
17
17
|
* signInput = `<scheme>:<recipientHubId>:<method>:<path>:<bodyHash>:<timestamp>:<nonce>`
|
|
18
|
-
* verify `signature`
|
|
18
|
+
* verify `signature` over the UTF-8 sign-input under `senderPublicKey`
|
|
19
|
+
*
|
|
20
|
+
* The algorithm is the key's own: ML-DSA-87 or ES384, the signature base64url
|
|
21
|
+
* without padding, or Ed25519 with a hex signature on a message a Server
|
|
22
|
+
* before 4.0.0 sealed.
|
|
19
23
|
*
|
|
20
24
|
* then the entry's own fields against the body it sealed (the state the entry
|
|
21
25
|
* projects is the state the body carried, and so on). Entries from before the
|
|
@@ -24,17 +28,32 @@
|
|
|
24
28
|
*/
|
|
25
29
|
import { createHash } from 'node:crypto';
|
|
26
30
|
import { rfc3339Ms } from './instant.js';
|
|
27
|
-
import {
|
|
31
|
+
import { declaredKeyAlgorithm, hostRefusalCause, judgingKeyStatements, resolveKeyAlgorithm, runtimeCanCompute, signatureShape, verifySignatureBytes } from './primitives.js';
|
|
28
32
|
export const FEDERATION_WIRE_SCHEME = 'agledger.federation.v2';
|
|
29
33
|
/** The domain a federation key statement's signatures sign under. */
|
|
30
34
|
export const FEDERATION_KEY_STATEMENT_DOMAIN = 'agledger.federation.key-statement.v1';
|
|
31
35
|
/** The 12-byte SPKI prefix of an Ed25519 key: SEQUENCE { AlgorithmIdentifier(id-Ed25519), BIT STRING }. */
|
|
32
36
|
const ED25519_SPKI_PREFIX = Buffer.from('302a300506032b6570032100', 'hex');
|
|
37
|
+
/** The algorithms a federation key signs messages with. Ed25519 signed them before Server 4.0.0. */
|
|
38
|
+
const FEDERATION_ALGORITHMS = new Set(['ML-DSA-87', 'ES384', 'Ed25519']);
|
|
39
|
+
/** The order of P-384, for the low-S form an ES384 federation signature takes. */
|
|
40
|
+
const P384_ORDER = BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973');
|
|
41
|
+
/**
|
|
42
|
+
* The federation algorithm a base64 SPKI DER key is for, or null. Read from
|
|
43
|
+
* the key's structure first, so an ML-DSA or Ed25519 key reads as itself on a
|
|
44
|
+
* host that cannot load it.
|
|
45
|
+
*/
|
|
46
|
+
function federationKeyAlgorithm(spkiBase64) {
|
|
47
|
+
const declared = declaredKeyAlgorithm(Buffer.from(spkiBase64, 'base64'));
|
|
48
|
+
const alg = declared ?? resolveKeyAlgorithm(spkiBase64);
|
|
49
|
+
return typeof alg === 'object' && FEDERATION_ALGORITHMS.has(alg.name) ? alg : null;
|
|
50
|
+
}
|
|
33
51
|
/**
|
|
34
|
-
* A federation key as base64 SPKI DER
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* nothing under
|
|
52
|
+
* A federation key as base64 SPKI DER: an ML-DSA-87 or ES384 key as it is,
|
|
53
|
+
* and an Ed25519 key (a Server before 4.0.0) from either form that Server's
|
|
54
|
+
* peer table held, SPKI DER or the raw 32-byte key. Returns null for anything
|
|
55
|
+
* else (so a caller refuses it by name rather than verifying nothing under
|
|
56
|
+
* it).
|
|
38
57
|
*/
|
|
39
58
|
export function normalizeFederationKey(publicKeyBase64) {
|
|
40
59
|
if (typeof publicKeyBase64 !== 'string' || publicKeyBase64.length === 0)
|
|
@@ -48,9 +67,40 @@ export function normalizeFederationKey(publicKeyBase64) {
|
|
|
48
67
|
}
|
|
49
68
|
if (bytes.length === 32)
|
|
50
69
|
return Buffer.concat([ED25519_SPKI_PREFIX, bytes]).toString('base64');
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
70
|
+
const spki = bytes.toString('base64');
|
|
71
|
+
return federationKeyAlgorithm(spki) === null ? null : spki;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The bytes of a federation signature in the one spelling its key's algorithm
|
|
75
|
+
* takes, else null: 128 hex characters under an Ed25519 key; under ML-DSA-87
|
|
76
|
+
* or ES384, base64url without padding that decodes to exactly the algorithm's
|
|
77
|
+
* length and encodes back to the same characters, and for ES384 the low-S
|
|
78
|
+
* form of r||s.
|
|
79
|
+
*/
|
|
80
|
+
function federationSignatureBytes(alg, signature) {
|
|
81
|
+
if (typeof signature !== 'string')
|
|
82
|
+
return null;
|
|
83
|
+
if (alg.name === 'Ed25519')
|
|
84
|
+
return HEX_SIG.test(signature) ? Buffer.from(signature, 'hex') : null;
|
|
85
|
+
if (signature.length !== Math.ceil((alg.signatureLength * 4) / 3))
|
|
86
|
+
return null;
|
|
87
|
+
const bytes = Buffer.from(signature, 'base64url');
|
|
88
|
+
if (bytes.length !== alg.signatureLength || bytes.toString('base64url') !== signature)
|
|
89
|
+
return null;
|
|
90
|
+
if (alg.name === 'ES384' && BigInt(`0x${bytes.subarray(bytes.length / 2).toString('hex')}`) > P384_ORDER / 2n)
|
|
91
|
+
return null;
|
|
92
|
+
return bytes;
|
|
93
|
+
}
|
|
94
|
+
/** How a signature under `alg` is spelled, for a failure line. */
|
|
95
|
+
function signatureSpelling(alg) {
|
|
96
|
+
return alg.name === 'Ed25519'
|
|
97
|
+
? '64 bytes of hex'
|
|
98
|
+
: `${alg.signatureLength} bytes of base64url without padding${alg.name === 'ES384' ? ', r||s with low S' : ''}`;
|
|
99
|
+
}
|
|
100
|
+
/** Why this host cannot compute `alg`, for a line that says nothing was verified. */
|
|
101
|
+
function cannotCompute(alg) {
|
|
102
|
+
const cause = hostRefusalCause(alg.name);
|
|
103
|
+
return `this host cannot compute ${alg.name}${cause === null ? '' : ` (${cause})`}`;
|
|
54
104
|
}
|
|
55
105
|
const INSTANT_US = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z$/;
|
|
56
106
|
const HEX64 = /^[0-9a-f]{64}$/;
|
|
@@ -70,16 +120,29 @@ function keyRef(value, extra) {
|
|
|
70
120
|
return null;
|
|
71
121
|
return value;
|
|
72
122
|
}
|
|
73
|
-
function verifyStatementSignature(publicKey, signature, payload) {
|
|
74
|
-
|
|
123
|
+
function verifyStatementSignature(publicKey, signature, payload, hubId) {
|
|
124
|
+
// A federation key's history decides which keys a peer hub signs with: a
|
|
125
|
+
// key statement, read as this host reads it in a rerun that assumes none.
|
|
126
|
+
return judgingKeyStatements(() => verifyStatementSignatureOnHost(publicKey, signature, payload, hubId));
|
|
127
|
+
}
|
|
128
|
+
function verifyStatementSignatureOnHost(publicKey, signature, payload, hubId) {
|
|
129
|
+
const alg = federationKeyAlgorithm(publicKey);
|
|
130
|
+
if (alg === null)
|
|
75
131
|
return false;
|
|
76
|
-
|
|
132
|
+
// Checks that need no computation run before this host's capability is asked.
|
|
133
|
+
const bytes = federationSignatureBytes(alg, signature);
|
|
134
|
+
if (bytes === null || signatureShape(alg, bytes) !== 'signature')
|
|
135
|
+
return false;
|
|
136
|
+
if (!runtimeCanCompute(alg)) {
|
|
137
|
+
throw new TypeError(`The federation key history for hub ${hubId} cannot be checked: ${cannotCompute(alg)}.`);
|
|
138
|
+
}
|
|
139
|
+
return verifySignatureBytes(publicKey, Buffer.from(`${FEDERATION_KEY_STATEMENT_DOMAIN}:${payload}`, 'utf8'), bytes);
|
|
77
140
|
}
|
|
78
141
|
/**
|
|
79
142
|
* The payload of one served key statement when it holds as the Server holds
|
|
80
143
|
* it, else why not. Holds: the payload is its own JCS form with only the
|
|
81
144
|
* statement fields, names `hubId`, its `typ` is the statement's `kind`, the
|
|
82
|
-
* keys are
|
|
145
|
+
* keys are federation keys as SPKI DER matching their digests, the instants are
|
|
83
146
|
* microsecond RFC 3339, a succession hands over at the instant its subject
|
|
84
147
|
* takes over and carries the predecessor's signature, a genesis carries none,
|
|
85
148
|
* and every signature verifies.
|
|
@@ -107,7 +170,7 @@ function verifyKeyStatement(stmt, hubId) {
|
|
|
107
170
|
return `it names hub ${JSON.stringify(parsed.hubId)}`;
|
|
108
171
|
const subject = keyRef(parsed.subject, 'activatedAt');
|
|
109
172
|
if (subject === null)
|
|
110
|
-
return 'its subject is not
|
|
173
|
+
return 'its subject is not a federation key with its digest';
|
|
111
174
|
const activatedAt = subject.activatedAt;
|
|
112
175
|
if (!(activatedAt === null || (typeof activatedAt === 'string' && INSTANT_US.test(activatedAt))))
|
|
113
176
|
return 'its subject.activatedAt is not a microsecond RFC 3339 instant or null';
|
|
@@ -115,7 +178,7 @@ function verifyKeyStatement(stmt, hubId) {
|
|
|
115
178
|
if (parsed.predecessor !== undefined) {
|
|
116
179
|
const pred = keyRef(parsed.predecessor, 'retiredAt');
|
|
117
180
|
if (pred === null)
|
|
118
|
-
return 'its predecessor is not
|
|
181
|
+
return 'its predecessor is not a federation key with its digest';
|
|
119
182
|
if (typeof pred.retiredAt !== 'string' || pred.retiredAt !== activatedAt)
|
|
120
183
|
return 'its predecessor.retiredAt is not its subject.activatedAt';
|
|
121
184
|
if (pred.spkiSha256 === subject.spkiSha256)
|
|
@@ -127,10 +190,10 @@ function verifyKeyStatement(stmt, hubId) {
|
|
|
127
190
|
}
|
|
128
191
|
if (typ === 'succession' && predecessor === undefined)
|
|
129
192
|
return 'it is a succession with no predecessor';
|
|
130
|
-
if (!verifyStatementSignature(subject.publicKey,
|
|
193
|
+
if (!verifyStatementSignature(subject.publicKey, stmt.subjectSignature, stmt.payload, hubId))
|
|
131
194
|
return 'its subjectSignature does not verify under its subject key';
|
|
132
195
|
if (typ === 'succession') {
|
|
133
|
-
if (typeof stmt.predecessorSignature !== 'string' || predecessor === undefined || !verifyStatementSignature(predecessor.publicKey, stmt.predecessorSignature, stmt.payload)) {
|
|
196
|
+
if (typeof stmt.predecessorSignature !== 'string' || predecessor === undefined || !verifyStatementSignature(predecessor.publicKey, stmt.predecessorSignature, stmt.payload, hubId)) {
|
|
134
197
|
return 'its predecessorSignature does not verify under its predecessor key';
|
|
135
198
|
}
|
|
136
199
|
}
|
|
@@ -143,6 +206,20 @@ function verifyKeyStatement(stmt, hubId) {
|
|
|
143
206
|
...(predecessor ? { predecessor } : {}),
|
|
144
207
|
};
|
|
145
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* A federation key history that does not hold for the keys pinned beside it:
|
|
211
|
+
* the pin is not a key the history admits, the statement that admits it does
|
|
212
|
+
* not verify, or the document is another hub's or another domain's. A
|
|
213
|
+
* `TypeError`, as every refused `federationKeys` input is, carrying the
|
|
214
|
+
* canonical `code`.
|
|
215
|
+
*/
|
|
216
|
+
export class FederationKeyHistoryError extends TypeError {
|
|
217
|
+
code = 'FEDERATION_KEY_HISTORY_INVALID';
|
|
218
|
+
constructor(message) {
|
|
219
|
+
super(message);
|
|
220
|
+
this.name = 'FederationKeyHistoryError';
|
|
221
|
+
}
|
|
222
|
+
}
|
|
146
223
|
/**
|
|
147
224
|
* The keys a pin and the histories given for its hub trust: the pin, and
|
|
148
225
|
* every key a chain of successions links to it, each succession counted only
|
|
@@ -151,30 +228,30 @@ function verifyKeyStatement(stmt, hubId) {
|
|
|
151
228
|
* signature, so it links nothing.
|
|
152
229
|
*/
|
|
153
230
|
function trustedKeysFromHistory(hubId, pins, histories) {
|
|
154
|
-
const ed25519 = algorithmByName('Ed25519');
|
|
155
|
-
if (ed25519 === null || !runtimeCanCompute(ed25519)) {
|
|
156
|
-
throw new TypeError(`The federation key history for hub ${hubId} cannot be checked: this host cannot compute Ed25519 (an active OpenSSL FIPS provider carries no EdDSA).`);
|
|
157
|
-
}
|
|
158
231
|
const statements = [];
|
|
232
|
+
const dropped = [];
|
|
159
233
|
const seenIds = new Set();
|
|
160
234
|
for (const { doc, label } of histories) {
|
|
161
235
|
if (!isRecord(doc) || !Array.isArray(doc.statements))
|
|
162
|
-
throw new
|
|
236
|
+
throw new FederationKeyHistoryError(`${label} is not a federation key-history document (no statements array).`);
|
|
163
237
|
if (doc.hubId !== hubId)
|
|
164
|
-
throw new
|
|
238
|
+
throw new FederationKeyHistoryError(`${label} is the key history of hub ${JSON.stringify(doc.hubId)}, not ${hubId}.`);
|
|
165
239
|
if (doc.statementDomain !== FEDERATION_KEY_STATEMENT_DOMAIN) {
|
|
166
|
-
throw new
|
|
240
|
+
throw new FederationKeyHistoryError(`${label} signs under ${JSON.stringify(doc.statementDomain)}, not ${FEDERATION_KEY_STATEMENT_DOMAIN}.`);
|
|
167
241
|
}
|
|
168
242
|
doc.statements.forEach((stmt, j) => {
|
|
169
243
|
const id = isRecord(stmt) && typeof stmt.id === 'string' ? stmt.id : `#${j}`;
|
|
170
244
|
if (seenIds.has(id))
|
|
171
245
|
return;
|
|
172
246
|
seenIds.add(id);
|
|
247
|
+
// A statement that does not verify takes no part in the walk, as on the
|
|
248
|
+
// Server: a row is not trust, and a statement the pinned key does not
|
|
249
|
+
// rest on cannot take the pin's own history down.
|
|
173
250
|
const verified = verifyKeyStatement(stmt, hubId);
|
|
174
|
-
if (typeof verified === 'string')
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
251
|
+
if (typeof verified === 'string')
|
|
252
|
+
dropped.push(`${label}.statements[${j}] (${id}): ${verified}`);
|
|
253
|
+
else
|
|
254
|
+
statements.push(verified);
|
|
178
255
|
});
|
|
179
256
|
}
|
|
180
257
|
const admission = new Map();
|
|
@@ -207,7 +284,8 @@ function trustedKeysFromHistory(hubId, pins, histories) {
|
|
|
207
284
|
const listed = pins.filter((p) => publicKeyOf.has(spkiSha256(p.spki)));
|
|
208
285
|
if (listed.length === 0) {
|
|
209
286
|
const named = pins.map((p) => `${p.label} (${spkiSha256(p.spki).slice(0, 16)})`).join(', ');
|
|
210
|
-
|
|
287
|
+
const unverified = dropped.length > 0 ? ` ${dropped.length} statement(s) do not verify and take no part: ${dropped.join('; ')}.` : '';
|
|
288
|
+
throw new FederationKeyHistoryError(`${histories.map((h) => h.label).join(', ')}: no key pinned for hub ${hubId} is a key the history given for it admits (${named}), so the history is not linked to anything you hold. Pin a key the history lists: its current key, or one you held before the change.${unverified}`);
|
|
211
289
|
}
|
|
212
290
|
const trusted = new Set();
|
|
213
291
|
const standalone = new Set();
|
|
@@ -226,28 +304,33 @@ function trustedKeysFromHistory(hubId, pins, histories) {
|
|
|
226
304
|
queue.push(...(links.get(k) ?? []));
|
|
227
305
|
}
|
|
228
306
|
}
|
|
307
|
+
// Newest first, whichever key the walk started from: a first key (no signed
|
|
308
|
+
// activation) is the oldest.
|
|
309
|
+
const linked = [...trusted].flatMap((digest) => {
|
|
310
|
+
const publicKey = publicKeyOf.get(digest);
|
|
311
|
+
// Every trusted digest was reached through publicKeyOf, so none is skipped.
|
|
312
|
+
if (publicKey === undefined)
|
|
313
|
+
return [];
|
|
314
|
+
return [{
|
|
315
|
+
publicKey,
|
|
316
|
+
activatedAt: admission.get(digest)?.subject.activatedAt ?? null,
|
|
317
|
+
retiredAt: retiredAt.get(digest) ?? null,
|
|
318
|
+
}];
|
|
319
|
+
}).sort((x, y) => (x.activatedAt === y.activatedAt ? 0 : x.activatedAt === null ? 1 : y.activatedAt === null ? -1 : x.activatedAt < y.activatedAt ? 1 : -1));
|
|
229
320
|
return [
|
|
230
|
-
...
|
|
231
|
-
const publicKey = publicKeyOf.get(digest);
|
|
232
|
-
// Every trusted digest was reached through publicKeyOf, so none is skipped.
|
|
233
|
-
if (publicKey === undefined)
|
|
234
|
-
return [];
|
|
235
|
-
return [{
|
|
236
|
-
publicKey,
|
|
237
|
-
activatedAt: admission.get(digest)?.subject.activatedAt ?? null,
|
|
238
|
-
retiredAt: retiredAt.get(digest) ?? null,
|
|
239
|
-
}];
|
|
240
|
-
}),
|
|
321
|
+
...linked,
|
|
241
322
|
...[...standalone].map((publicKey) => ({ publicKey, activatedAt: null, retiredAt: null })),
|
|
242
323
|
];
|
|
243
324
|
}
|
|
244
325
|
/**
|
|
245
326
|
* Build the registry from `{ [hubId]: publicKey }` or
|
|
246
327
|
* `[{ hubId, publicKey, history? }]`. Throws `TypeError` naming the entry on
|
|
247
|
-
* any key that is not
|
|
248
|
-
* that is another hub's
|
|
249
|
-
* none of the keys pinned for its hub, so a bad input is refused
|
|
250
|
-
* runs rather than reported as a signature failure.
|
|
328
|
+
* any key that is not a federation key, and `FederationKeyHistoryError` (code
|
|
329
|
+
* `FEDERATION_KEY_HISTORY_INVALID`) on a history that is another hub's or
|
|
330
|
+
* admits none of the keys pinned for its hub, so a bad input is refused
|
|
331
|
+
* before any verification runs rather than reported as a signature failure.
|
|
332
|
+
* A statement that does not verify takes no part: the keys it alone would
|
|
333
|
+
* link are not trusted.
|
|
251
334
|
*/
|
|
252
335
|
export function buildFederationKeyRegistry(input) {
|
|
253
336
|
const entries = Array.isArray(input)
|
|
@@ -269,7 +352,7 @@ export function buildFederationKeyRegistry(input) {
|
|
|
269
352
|
throw new TypeError(`${label} names no hubId.`);
|
|
270
353
|
const spki = typeof key === 'string' ? normalizeFederationKey(key) : null;
|
|
271
354
|
if (spki === null) {
|
|
272
|
-
throw new TypeError(`${label} is not
|
|
355
|
+
throw new TypeError(`${label} is not a federation public key: ML-DSA-87 or ES384 as base64 SPKI DER, or the Ed25519 key of a Server before 4.0.0 (base64 SPKI DER, or base64 of the raw 32 bytes).`);
|
|
273
356
|
}
|
|
274
357
|
const hub = byHub.get(hubId) ?? { pins: [], histories: [] };
|
|
275
358
|
hub.pins.push({ spki, label });
|
|
@@ -493,23 +576,82 @@ export function checkFederationWire(entryType, recordId, payload, wire, keys, op
|
|
|
493
576
|
};
|
|
494
577
|
}
|
|
495
578
|
}
|
|
496
|
-
const
|
|
497
|
-
if (
|
|
498
|
-
return { outcome: '
|
|
499
|
-
}
|
|
500
|
-
let signature;
|
|
501
|
-
try {
|
|
502
|
-
signature = Buffer.from(wire.signature, 'hex');
|
|
579
|
+
const alg = federationKeyAlgorithm(key.publicKey);
|
|
580
|
+
if (alg === null) {
|
|
581
|
+
return { outcome: 'invalid', detail: `The federation key supplied for ${wire.senderHubId} is not an ML-DSA-87, ES384 or Ed25519 key.` };
|
|
503
582
|
}
|
|
504
|
-
|
|
505
|
-
|
|
583
|
+
// Checks that need no computation run before this host's capability is asked.
|
|
584
|
+
const signature = federationSignatureBytes(alg, wire.signature);
|
|
585
|
+
if (signature === null || signatureShape(alg, signature) !== 'signature') {
|
|
586
|
+
return { outcome: 'invalid', detail: `Sealed signature from ${wire.senderHubId} is not ${signatureSpelling(alg)} and not all zero, the form a signature under its ${alg.name} federation key takes.` };
|
|
506
587
|
}
|
|
507
|
-
if (
|
|
508
|
-
return { outcome: '
|
|
588
|
+
if (!runtimeCanCompute(alg)) {
|
|
589
|
+
return { outcome: 'unsupported', detail: `Sealed message from ${wire.senderHubId} could not be checked: ${cannotCompute(alg)}. Not verified, and not tamper evidence.` };
|
|
509
590
|
}
|
|
510
|
-
const ok =
|
|
591
|
+
const ok = verifySignatureBytes(key.publicKey, Buffer.from(federationSignInput(wire), 'utf8'), signature);
|
|
511
592
|
if (!ok) {
|
|
512
593
|
return { outcome: 'invalid', detail: `Sealed signature from ${wire.senderHubId} does not verify under that hub's federation key over the sign-input the entry seals.` };
|
|
513
594
|
}
|
|
514
595
|
return { outcome: 'ok', signatureChecked: true, afterRetirement };
|
|
515
596
|
}
|
|
597
|
+
// --- Signatures a Server returns to its peer ---
|
|
598
|
+
/** Domain an acknowledgement signature covers. */
|
|
599
|
+
export const FEDERATION_ACK_DOMAIN = 'agledger.federation.ack.v1';
|
|
600
|
+
/** Domain a co-sign counter-signature covers. */
|
|
601
|
+
export const FEDERATION_CO_SIGN_DOMAIN = 'agledger.federation.co-sign.v1';
|
|
602
|
+
/** What an acknowledgement acknowledges: a state transition, a signal or a dispute leg. */
|
|
603
|
+
export const FEDERATION_ACK_KINDS = ['ack', 'signal', 'dispute'];
|
|
604
|
+
/** The members of a co-sign claim, exactly. */
|
|
605
|
+
export const FEDERATION_CO_SIGN_CLAIM_FIELDS = ['recordId', 'outcome', 'recommendation', 'outcomeHash', 'state', 'performerHubId', 'validUntil'];
|
|
606
|
+
function checkPeerSignature(what, signerPublicKey, signature, signInput) {
|
|
607
|
+
const invalid = (detail) => ({ state: 'invalid', code: 'FEDERATION_SIGNATURE_INVALID', detail });
|
|
608
|
+
const spki = typeof signerPublicKey === 'string' ? normalizeFederationKey(signerPublicKey) : null;
|
|
609
|
+
const alg = spki === null ? null : federationKeyAlgorithm(spki);
|
|
610
|
+
if (spki === null || alg === null) {
|
|
611
|
+
return invalid(`${what} names a signer key that is not a federation key: ML-DSA-87 or ES384 as base64 SPKI DER, or the Ed25519 key of a Server before 4.0.0.`);
|
|
612
|
+
}
|
|
613
|
+
// Checks that need no computation run before this host's capability is asked.
|
|
614
|
+
const bytes = typeof signature === 'string' ? federationSignatureBytes(alg, signature) : null;
|
|
615
|
+
if (bytes === null || signatureShape(alg, bytes) !== 'signature')
|
|
616
|
+
return invalid(`${what} is not a signature in the form of its ${alg.name} key: ${signatureSpelling(alg)}, not all zero.`);
|
|
617
|
+
if (!runtimeCanCompute(alg))
|
|
618
|
+
return { state: 'unsupported', detail: `${what} cannot be checked: ${cannotCompute(alg)}.` };
|
|
619
|
+
if (!verifySignatureBytes(spki, Buffer.from(signInput, 'utf8'), bytes))
|
|
620
|
+
return invalid(`${what} does not verify under the signer's ${alg.name} key.`);
|
|
621
|
+
return { state: 'ok' };
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Verify the acknowledgement a receiving Server returned for a federated
|
|
625
|
+
* transition, signal or dispute leg (`serverSignature`). It covers
|
|
626
|
+
* `agledger.federation.ack.v1:<kind>:<recordId>:<value>:<serverTimestamp>`,
|
|
627
|
+
* so an acknowledgement made for one kind does not verify as another.
|
|
628
|
+
*/
|
|
629
|
+
export function verifyFederationAck(ack) {
|
|
630
|
+
const invalid = (detail) => ({ state: 'invalid', code: 'FEDERATION_SIGNATURE_INVALID', detail });
|
|
631
|
+
if (!isRecord(ack))
|
|
632
|
+
return invalid('The acknowledgement is not an object.');
|
|
633
|
+
const { kind, recordId, value, serverTimestamp } = ack;
|
|
634
|
+
if (typeof kind !== 'string' || !FEDERATION_ACK_KINDS.includes(kind)) {
|
|
635
|
+
return invalid(`The acknowledgement's kind is ${JSON.stringify(kind)}, not one of ${FEDERATION_ACK_KINDS.join(', ')}.`);
|
|
636
|
+
}
|
|
637
|
+
if (typeof recordId !== 'string' || typeof value !== 'string' || typeof serverTimestamp !== 'string') {
|
|
638
|
+
return invalid('The acknowledgement carries no recordId, value or serverTimestamp string.');
|
|
639
|
+
}
|
|
640
|
+
return checkPeerSignature('The acknowledgement signature', ack.signerPublicKey, ack.serverSignature, `${FEDERATION_ACK_DOMAIN}:${kind}:${recordId}:${value}:${serverTimestamp}`);
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Verify a co-sign counter-signature: the counterparty Server's signature
|
|
644
|
+
* over `agledger.federation.co-sign.v1:` and the RFC 8785 form of the claim.
|
|
645
|
+
* The claim holds exactly the fields in {@link FEDERATION_CO_SIGN_CLAIM_FIELDS}.
|
|
646
|
+
*/
|
|
647
|
+
export function verifyFederationCoSign(input) {
|
|
648
|
+
const invalid = (detail) => ({ state: 'invalid', code: 'FEDERATION_SIGNATURE_INVALID', detail });
|
|
649
|
+
if (!isRecord(input) || !isRecord(input.claim))
|
|
650
|
+
return invalid('The co-sign input carries no claim object.');
|
|
651
|
+
const keys = Object.keys(input.claim);
|
|
652
|
+
const fields = FEDERATION_CO_SIGN_CLAIM_FIELDS;
|
|
653
|
+
if (keys.length !== fields.length || !fields.every((f) => f in input.claim)) {
|
|
654
|
+
return invalid(`The co-sign claim holds ${keys.sort().join(', ') || 'no fields'}; it holds exactly ${[...fields].sort().join(', ')}.`);
|
|
655
|
+
}
|
|
656
|
+
return checkPeerSignature('The counter-signature', input.signerPublicKey, input.counterSignature, `${FEDERATION_CO_SIGN_DOMAIN}:${canonicalizeJson(input.claim)}`);
|
|
657
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { type KeyRegistry } from './chain.js';
|
|
2
|
+
import { type KeyTrust } from './key-statements.js';
|
|
3
|
+
/** COSE content type (header label 3) of a signed history seal. */
|
|
4
|
+
export declare const HISTORY_SEAL_CTY = "application/vnd.agledger.history-seal+cbor";
|
|
5
|
+
/** The chain key the platform-ops chain's leaf carries. */
|
|
6
|
+
export declare const PLATFORM_OPS_CHAIN_KEY = "00000000-0000-0000-0000-000000000000";
|
|
7
|
+
/** A history seal as an artifact carries it: an export's `exportMetadata.historySeals[]`, a dump's `vault_history_seals.ndjson` row. */
|
|
8
|
+
export interface HistorySealInput {
|
|
9
|
+
id: string;
|
|
10
|
+
/** The key whose retirement wrote the seal. */
|
|
11
|
+
retiredKeyId: string;
|
|
12
|
+
signingKeyId: string;
|
|
13
|
+
/** The signed seal, base64 or bytes. */
|
|
14
|
+
coseSign1: string | Uint8Array;
|
|
15
|
+
/** When the Server stored the seal: what its key's window is held to. */
|
|
16
|
+
createdAt?: string | null;
|
|
17
|
+
/** The `treeSize` beside the seal, bound to the signed one when given. */
|
|
18
|
+
treeSize?: number;
|
|
19
|
+
/** The `rootHash` beside the seal (lowercase hex), bound to the signed root when given. */
|
|
20
|
+
rootHash?: string;
|
|
21
|
+
}
|
|
22
|
+
/** One chain's inclusion proof in one seal: an export's `historySeals[].proof`, a dump's `vault_history_seal_leaves.ndjson` row. */
|
|
23
|
+
export interface HistorySealProofInput {
|
|
24
|
+
leaf: {
|
|
25
|
+
chainKey: string;
|
|
26
|
+
chainPosition: number;
|
|
27
|
+
entryHash: string;
|
|
28
|
+
};
|
|
29
|
+
leafIndex: number;
|
|
30
|
+
/** Lowercase hex sibling hashes, from the leaf to the root. */
|
|
31
|
+
auditPath: readonly string[];
|
|
32
|
+
}
|
|
33
|
+
export interface HistorySealVerdict {
|
|
34
|
+
id: string;
|
|
35
|
+
/** `ok`: decodes, is bound to its closure and verifies under an anchored ML-DSA key. `unsupported`: this host cannot compute its key's algorithm. */
|
|
36
|
+
state: 'ok' | 'unsupported' | 'invalid';
|
|
37
|
+
detail: string;
|
|
38
|
+
/** The retired key the seal names, or the key it is filed for when its payload does not decode. */
|
|
39
|
+
subjectKeyId: string | null;
|
|
40
|
+
/** The retirement the seal signs for that key. */
|
|
41
|
+
subjectRetiredAt: string | null;
|
|
42
|
+
treeSize: number | null;
|
|
43
|
+
/** The signed root, lowercase hex. */
|
|
44
|
+
root: string | null;
|
|
45
|
+
signingKeyId: string | null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Judge one history seal against the keys the artifact's entries are verified
|
|
49
|
+
* with (after `applyKeyTrust`) and the walk that anchored them. Never throws
|
|
50
|
+
* on a malformed seal: it is `invalid`, with the reason.
|
|
51
|
+
*/
|
|
52
|
+
export declare function judgeHistorySeal(seal: HistorySealInput, keys: KeyRegistry, trust: KeyTrust): HistorySealVerdict;
|
|
53
|
+
/** How the seals of an artifact cover one classical signing key. */
|
|
54
|
+
export type HistorySealCover = {
|
|
55
|
+
state: 'sealed';
|
|
56
|
+
seal: HistorySealVerdict;
|
|
57
|
+
} | {
|
|
58
|
+
state: 'invalid';
|
|
59
|
+
detail: string;
|
|
60
|
+
} | {
|
|
61
|
+
state: 'unsupported';
|
|
62
|
+
detail: string;
|
|
63
|
+
} | {
|
|
64
|
+
state: 'unsealed';
|
|
65
|
+
detail: string;
|
|
66
|
+
};
|
|
67
|
+
/** The seals of one artifact, judged, and which seal covers which key. */
|
|
68
|
+
export interface HistorySealPlan {
|
|
69
|
+
/** Every seal the artifact carries, judged, by id (lowercase). */
|
|
70
|
+
seals: ReadonlyMap<string, HistorySealVerdict>;
|
|
71
|
+
/**
|
|
72
|
+
* How a signing key's entries are covered, or null when the key is held to
|
|
73
|
+
* no seal: an ML-DSA key, an unknown key, or any key on a walk that trusts
|
|
74
|
+
* no ML-DSA key.
|
|
75
|
+
*/
|
|
76
|
+
coverOf(keyId: string): HistorySealCover | null;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Judge the seals an artifact carries and decide which one covers each
|
|
80
|
+
* classical key. `trust` is the key walk; null (no `trustAnchors`) holds no
|
|
81
|
+
* key to any seal.
|
|
82
|
+
*/
|
|
83
|
+
export declare function planHistorySeals(seals: readonly HistorySealInput[], keys: KeyRegistry, trust: KeyTrust | null): HistorySealPlan;
|
|
84
|
+
/** One chain's leaf in one seal, once its proof reaches the signed root. */
|
|
85
|
+
export type HistorySealLeaf = {
|
|
86
|
+
state: 'ok';
|
|
87
|
+
chainPosition: number;
|
|
88
|
+
entryHash: string;
|
|
89
|
+
} | {
|
|
90
|
+
state: 'missing';
|
|
91
|
+
} | {
|
|
92
|
+
state: 'invalid';
|
|
93
|
+
detail: string;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Resolve a chain's leaf in a seal whose verdict is `ok`: the proof must be
|
|
97
|
+
* for this chain and reach the signed root. `proof` is null or undefined when
|
|
98
|
+
* the artifact carries none for this chain (the seal has no leaf for it).
|
|
99
|
+
*/
|
|
100
|
+
export declare function resolveHistorySealLeaf(seal: HistorySealVerdict, chainKey: string, proof: HistorySealProofInput | null | undefined): HistorySealLeaf;
|