@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
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verifying a signed batch head (see `batch.ts` for the cadence).
|
|
3
|
+
*
|
|
4
|
+
* A head is one COSE_Sign1 under a vault key, in the key-statement envelope:
|
|
5
|
+
* protected header `{1: alg, 3: cty, 4: kid}` with `cty` =
|
|
6
|
+
* {@link BATCH_HEAD_CTY}, signed over the RFC 9052 `Sig_structure` with an
|
|
7
|
+
* empty external_aad. Its payload is a deterministic-CBOR map of exactly
|
|
8
|
+
* seven members:
|
|
9
|
+
*
|
|
10
|
+
* typ 'batch-head'
|
|
11
|
+
* iss the Server's issuer URL (informational)
|
|
12
|
+
* batch the head's id (a uuid string)
|
|
13
|
+
* treeSize how many leaves the root covers
|
|
14
|
+
* root bstr of 32 bytes: the RFC 9162 root over the leaves in index order
|
|
15
|
+
* leaf the leaf format, {@link BATCH_LEAF_FORMAT}
|
|
16
|
+
* iat epoch seconds, informational
|
|
17
|
+
*
|
|
18
|
+
* The head's key can differ from the key the entries it covers name. It is
|
|
19
|
+
* resolved, anchored and held to its window like an entry's key, at the
|
|
20
|
+
* head's own write time.
|
|
21
|
+
*/
|
|
22
|
+
import { STRICT_CBOR_DECODE } from './cbor-strict.js';
|
|
23
|
+
import { decode as cborDecode, encode as cborEncode, rfc8949EncodeOptions } from 'cborg';
|
|
24
|
+
import { BATCH_HEAD_CTY, BATCH_LEAF_FORMAT } from './batch.js';
|
|
25
|
+
import { distrustAccounts, placeInKeyWindow } from './chain.js';
|
|
26
|
+
import { STATEMENT_COSE_ALG, decodeSign1, sigStructure } from './key-statements.js';
|
|
27
|
+
import { describeUnsupportedAlgorithm, resolveKeyAlgorithmOrHostRefused, runtimeCanCompute, signatureShape, verifySignatureBytes } from './primitives.js';
|
|
28
|
+
/** The payload, or null when the bytes are not the canonical encoding of a head. */
|
|
29
|
+
function decodeBatchHeadPayload(bytes) {
|
|
30
|
+
let raw;
|
|
31
|
+
try {
|
|
32
|
+
raw = cborDecode(bytes, { ...STRICT_CBOR_DECODE, useMaps: false });
|
|
33
|
+
if (Buffer.compare(Buffer.from(cborEncode(raw, rfc8949EncodeOptions)), Buffer.from(bytes)) !== 0)
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw) || raw instanceof Uint8Array)
|
|
40
|
+
return null;
|
|
41
|
+
const r = raw;
|
|
42
|
+
const { typ, iss, batch, treeSize, root, leaf, iat } = r;
|
|
43
|
+
if (typ !== 'batch-head' || typeof iss !== 'string' || typeof batch !== 'string'
|
|
44
|
+
|| typeof treeSize !== 'number' || !Number.isSafeInteger(treeSize) || treeSize < 0
|
|
45
|
+
|| !(root instanceof Uint8Array) || root.length !== 32
|
|
46
|
+
|| typeof leaf !== 'string' || typeof iat !== 'number' || !Number.isInteger(iat)
|
|
47
|
+
|| Object.keys(r).length !== 7) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return { batch, treeSize, root: Buffer.from(root).toString('hex'), leaf };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Judge one batch head against the keys the artifact's entries are verified
|
|
54
|
+
* with (after `applyKeyTrust`, when a walk ran). Never throws on a malformed
|
|
55
|
+
* head: it is `invalid`, with the reason.
|
|
56
|
+
*/
|
|
57
|
+
export function judgeBatchHead(head, keys) {
|
|
58
|
+
const id = typeof head?.id === 'string' ? head.id : '';
|
|
59
|
+
const signingKeyId = typeof head?.signingKeyId === 'string' ? head.signingKeyId : null;
|
|
60
|
+
const verdict = (state, detail, payload = null) => ({
|
|
61
|
+
id,
|
|
62
|
+
state,
|
|
63
|
+
detail,
|
|
64
|
+
treeSize: payload?.treeSize ?? null,
|
|
65
|
+
root: payload?.root ?? null,
|
|
66
|
+
signingKeyId,
|
|
67
|
+
});
|
|
68
|
+
if (id === '' || signingKeyId === null)
|
|
69
|
+
return verdict('invalid', 'The batch head carries no id or no signingKeyId.');
|
|
70
|
+
const sign1 = decodeSign1(head.coseSign1);
|
|
71
|
+
if (sign1 === null)
|
|
72
|
+
return verdict('invalid', `Batch head ${id} does not decode as a tagged COSE_Sign1.`);
|
|
73
|
+
if (sign1.cty !== BATCH_HEAD_CTY)
|
|
74
|
+
return verdict('invalid', `Batch head ${id} is not signed as a batch head (content type ${JSON.stringify(sign1.cty)}).`);
|
|
75
|
+
const payload = decodeBatchHeadPayload(sign1.payloadBstr);
|
|
76
|
+
if (payload === null)
|
|
77
|
+
return verdict('invalid', `Batch head ${id}'s payload does not decode as a batch head.`);
|
|
78
|
+
if (sign1.kid !== signingKeyId) {
|
|
79
|
+
return verdict('invalid', `Batch head ${id} is filed under key ${signingKeyId} and its signed kid is ${sign1.kid ?? 'absent'}.`, payload);
|
|
80
|
+
}
|
|
81
|
+
if (payload.batch.toLowerCase() !== id.toLowerCase()) {
|
|
82
|
+
return verdict('invalid', `Batch head ${id} signs the id ${payload.batch}.`, payload);
|
|
83
|
+
}
|
|
84
|
+
if (payload.leaf !== BATCH_LEAF_FORMAT) {
|
|
85
|
+
return verdict('invalid', `Batch head ${id} names the leaf format ${JSON.stringify(payload.leaf)}; this verifier reads ${BATCH_LEAF_FORMAT}.`, payload);
|
|
86
|
+
}
|
|
87
|
+
if (head.treeSize !== undefined && head.treeSize !== payload.treeSize) {
|
|
88
|
+
return verdict('invalid', `Batch head ${id}'s tree_size column is ${head.treeSize} and it signs ${payload.treeSize}.`, payload);
|
|
89
|
+
}
|
|
90
|
+
if (head.rootHash !== undefined && (typeof head.rootHash !== 'string' || head.rootHash.toLowerCase() !== payload.root)) {
|
|
91
|
+
return verdict('invalid', `Batch head ${id}'s root_hash column differs from the root it signs.`, payload);
|
|
92
|
+
}
|
|
93
|
+
const key = keys.get(signingKeyId);
|
|
94
|
+
if (!key)
|
|
95
|
+
return verdict('invalid', `No public key is available for batch head ${id}'s signing key ${signingKeyId}.`, payload);
|
|
96
|
+
const withKey = (state, detail) => ({ ...verdict(state, detail, payload), keySource: key.source });
|
|
97
|
+
// A key whose bytes declare Ed25519 or ML-DSA and that this host refused to
|
|
98
|
+
// load is a host refusal, as it is for an entry's key, not a malformed key.
|
|
99
|
+
const algorithm = resolveKeyAlgorithmOrHostRefused(key.spkiBase64);
|
|
100
|
+
if (typeof algorithm !== 'object') {
|
|
101
|
+
return withKey(algorithm === 'unrecognized' ? 'unsupported' : 'invalid', `Batch head ${id}'s key ${signingKeyId} ${describeUnsupportedAlgorithm(key.spkiBase64)}`);
|
|
102
|
+
}
|
|
103
|
+
// Names compare without case, as they do for an entry's key.
|
|
104
|
+
if (typeof key.algorithm === 'string' && key.algorithm.toLowerCase() !== algorithm.name.toLowerCase()) {
|
|
105
|
+
return withKey('invalid', `The key registry declares algorithm=${key.algorithm} for key ${signingKeyId}, but the key material is ${algorithm.name}.`);
|
|
106
|
+
}
|
|
107
|
+
// Before the alg label: a key this build cannot compute is not verified,
|
|
108
|
+
// and that is not tamper evidence, whatever label its head carries.
|
|
109
|
+
if (!algorithm.verifiable) {
|
|
110
|
+
return withKey('unsupported', `Batch head ${id}'s key ${signingKeyId} ${describeUnsupportedAlgorithm(key.spkiBase64)}`);
|
|
111
|
+
}
|
|
112
|
+
// Checks that need no computation run before this host's capability or the
|
|
113
|
+
// key's undecided trust is asked, so they read the same on every host.
|
|
114
|
+
if (sign1.alg !== STATEMENT_COSE_ALG[algorithm.name]) {
|
|
115
|
+
return withKey('invalid', `Batch head ${id}'s protected-header alg is not the one key ${signingKeyId} (${algorithm.name}) signs under.`);
|
|
116
|
+
}
|
|
117
|
+
if (signatureShape(algorithm, sign1.signature) !== 'signature') {
|
|
118
|
+
return withKey('invalid', `Batch head ${id}'s signature is not a signature under key ${signingKeyId} (${algorithm.name}): all zero, or not ${algorithm.signatureLength} bytes.`);
|
|
119
|
+
}
|
|
120
|
+
if (key.trust === 'undecided') {
|
|
121
|
+
return withKey('unsupported', `Batch head ${id}'s key ${signingKeyId} is linked to a pinned anchor only through a key statement signed under an algorithm this host cannot compute, and ${describeUnsupportedAlgorithm(key.spkiBase64)}`);
|
|
122
|
+
}
|
|
123
|
+
if (!runtimeCanCompute(algorithm)) {
|
|
124
|
+
return withKey('unsupported', `Batch head ${id}'s key ${signingKeyId} ${describeUnsupportedAlgorithm(key.spkiBase64)}`);
|
|
125
|
+
}
|
|
126
|
+
// The distrust entry accounts for a head its key signed before a trusted
|
|
127
|
+
// key retired it; the signature below still has to verify.
|
|
128
|
+
const accounted = distrustAccounts(key, head.createdAt);
|
|
129
|
+
if (!accounted) {
|
|
130
|
+
if (key.trust === 'unanchored') {
|
|
131
|
+
return withKey('invalid', `Batch head ${id}'s key ${signingKeyId} is not linked by any signed key statement to a pinned trust anchor.`);
|
|
132
|
+
}
|
|
133
|
+
const placed = placeInKeyWindow(key, head.createdAt, `Batch head ${id}`);
|
|
134
|
+
// `accounted` is what `distrustAccounts` answered above, so it cannot be
|
|
135
|
+
// reached here; it is refused rather than read as inside the window.
|
|
136
|
+
if (placed.placement !== 'inside') {
|
|
137
|
+
return withKey('invalid', 'detail' in placed ? placed.detail : `Batch head ${id} was written outside key ${signingKeyId}'s window.`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (!verifySignatureBytes(key.spkiBase64, sigStructure(sign1.protectedBstr, sign1.payloadBstr), sign1.signature)) {
|
|
141
|
+
return withKey('invalid', `Batch head ${id}'s signature does not verify under key ${signingKeyId}.`);
|
|
142
|
+
}
|
|
143
|
+
return accounted
|
|
144
|
+
? withKey('accounted', `Batch head ${id} verifies under key ${signingKeyId}, which distrustedKeys names, and was written in the span that entry and the key's trusted retirement account for. It attests nothing.`)
|
|
145
|
+
: withKey('ok', `Batch head ${id} verifies under key ${signingKeyId}.`);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Judge every head an artifact carries and index them by id (lowercase). A
|
|
149
|
+
* head listed twice under one id with different bytes is `invalid`: nothing
|
|
150
|
+
* says which one the entries mean.
|
|
151
|
+
*/
|
|
152
|
+
export function buildBatchHeadRegistry(heads, keys) {
|
|
153
|
+
const out = new Map();
|
|
154
|
+
const bytesOf = new Map();
|
|
155
|
+
for (const head of heads) {
|
|
156
|
+
const judged = judgeBatchHead(head, keys);
|
|
157
|
+
const id = judged.id.toLowerCase();
|
|
158
|
+
const bytes = typeof head?.coseSign1 === 'string' ? head.coseSign1 : Buffer.from(head?.coseSign1 ?? []).toString('base64');
|
|
159
|
+
if (out.has(id) && bytesOf.get(id) !== bytes) {
|
|
160
|
+
out.set(id, { ...judged, state: 'invalid', detail: `Batch head ${judged.id} is listed twice with different bytes.` });
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (!out.has(id)) {
|
|
164
|
+
out.set(id, judged);
|
|
165
|
+
bytesOf.set(id, bytes);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
}
|
package/dist/batch.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { KeySource } from './chain.js';
|
|
2
|
+
/** Protected-header label of the batch marker. Its value is 1. */
|
|
3
|
+
export declare const BATCH_MARKER_LABEL = -65541;
|
|
4
|
+
/** Content type of a batch head's COSE_Sign1 (protected header label 3). */
|
|
5
|
+
export declare const BATCH_HEAD_CTY = "application/vnd.agledger.batch-head+cbor";
|
|
6
|
+
/** The leaf format a batch head names in its signed payload. */
|
|
7
|
+
export declare const BATCH_LEAF_FORMAT = "chain-key-uuid16.position-u64be.entry-sha256";
|
|
8
|
+
/**
|
|
9
|
+
* How long the Server lets a batched entry wait for its head before its scan
|
|
10
|
+
* reports it. An entry no head covers yet is pending inside this, and
|
|
11
|
+
* BATCH_UNATTESTED past it.
|
|
12
|
+
*/
|
|
13
|
+
export declare const BATCH_PENDING_GRACE_SECONDS = 300;
|
|
14
|
+
/** Whether a protected header carries the batch marker; false on any structural failure. */
|
|
15
|
+
export declare function isBatchedProtectedHeader(protectedBstr: Uint8Array): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* The 56 leaf bytes for one entry, or null when the chain key is not a uuid,
|
|
18
|
+
* the position is not a positive safe integer, or the hash is not 64 lowercase
|
|
19
|
+
* hex characters.
|
|
20
|
+
*/
|
|
21
|
+
export declare function batchLeafBytes(chainKey: string, chainPosition: number, entryHash: string): Uint8Array | null;
|
|
22
|
+
/**
|
|
23
|
+
* The inclusion proof an artifact carries for one batched entry: an export's
|
|
24
|
+
* `entries[].integrity.batch`, or a dump's `vault_batch_leaves.ndjson` row.
|
|
25
|
+
*/
|
|
26
|
+
export interface BatchProofInput {
|
|
27
|
+
/** The head that covers the entry. */
|
|
28
|
+
batchId: string;
|
|
29
|
+
leafIndex: number;
|
|
30
|
+
treeSize: number;
|
|
31
|
+
/** Lowercase hex node hashes, leaf to root. */
|
|
32
|
+
auditPath: readonly string[];
|
|
33
|
+
/**
|
|
34
|
+
* The entry hash the artifact's leaf row names (a dump's `entry_hash`). An
|
|
35
|
+
* export carries none: the leaf is derived from the entry itself.
|
|
36
|
+
*/
|
|
37
|
+
entryHash?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* What the verifier concluded about one batch head (see `judgeBatchHead`).
|
|
41
|
+
*
|
|
42
|
+
* - `ok`: the head's signature verifies under a key the walk accepts, inside
|
|
43
|
+
* that key's window at the head's write time, and its signed payload is a
|
|
44
|
+
* head for the id it is filed under.
|
|
45
|
+
* - `accounted`: it verifies in every respect under a key `distrustedKeys`
|
|
46
|
+
* names, written in the span that entry and the trusted retirement of the
|
|
47
|
+
* key account for. It attests nothing; the entries it covers are listed.
|
|
48
|
+
* - `unsupported`: its key commits to an algorithm this host cannot compute.
|
|
49
|
+
* Not verified, and not tamper evidence.
|
|
50
|
+
* - `invalid`: anything else.
|
|
51
|
+
*/
|
|
52
|
+
export interface BatchHeadVerdict {
|
|
53
|
+
id: string;
|
|
54
|
+
state: 'ok' | 'accounted' | 'unsupported' | 'invalid';
|
|
55
|
+
detail: string;
|
|
56
|
+
/** The signed tree size and root (lowercase hex); null when the payload did not decode. */
|
|
57
|
+
treeSize: number | null;
|
|
58
|
+
root: string | null;
|
|
59
|
+
signingKeyId: string | null;
|
|
60
|
+
keySource?: KeySource;
|
|
61
|
+
}
|
|
62
|
+
/** Batch heads by id, as `verifyChain` reads them. */
|
|
63
|
+
export type BatchHeadRegistry = ReadonlyMap<string, BatchHeadVerdict>;
|
|
64
|
+
/**
|
|
65
|
+
* Check one entry's inclusion proof against a head whose verdict is `ok` or
|
|
66
|
+
* `accounted`. Returns null when the proof reaches the signed root from the
|
|
67
|
+
* entry's own leaf, else what is wrong with it.
|
|
68
|
+
*/
|
|
69
|
+
export declare function checkBatchProof(chainKey: string, chainPosition: number, payloadHash: string, proof: BatchProofInput, head: BatchHeadVerdict): string | null;
|
package/dist/batch.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The batched signing cadence: an entry that carries no signature of its own
|
|
3
|
+
* and is attested by a signed batch head.
|
|
4
|
+
*
|
|
5
|
+
* A Server on the batched cadence writes a record-chain or schema-chain entry
|
|
6
|
+
* hash-chained as always, with the batch marker in its protected header
|
|
7
|
+
* (label {@link BATCH_MARKER_LABEL}, value 1) and an empty signature. The
|
|
8
|
+
* marker is inside the protected header, so the entry's `payloadHash` covers
|
|
9
|
+
* it. A batch head then signs an RFC 9162 Merkle root over the leaves of the
|
|
10
|
+
* entries it covers. An entry is verified by its leaf, an inclusion proof from
|
|
11
|
+
* that leaf to the head's root, and the head's signature
|
|
12
|
+
* (see `judgeBatchHead`). The platform-ops chain is never batched.
|
|
13
|
+
*
|
|
14
|
+
* A leaf is 56 bytes ({@link BATCH_LEAF_FORMAT}): the chain key's 16 uuid
|
|
15
|
+
* bytes, the entry's chain position as a big-endian u64, and the entry's
|
|
16
|
+
* `payloadHash` as 32 bytes. The chain key of a record chain is the record id.
|
|
17
|
+
*/
|
|
18
|
+
import { STRICT_CBOR_DECODE } from './cbor-strict.js';
|
|
19
|
+
import { decode as cborDecode } from 'cborg';
|
|
20
|
+
import { rfc9162LeafHash, verifyRfc9162Inclusion } from './primitives.js';
|
|
21
|
+
/** Protected-header label of the batch marker. Its value is 1. */
|
|
22
|
+
export const BATCH_MARKER_LABEL = -65541;
|
|
23
|
+
/** Content type of a batch head's COSE_Sign1 (protected header label 3). */
|
|
24
|
+
export const BATCH_HEAD_CTY = 'application/vnd.agledger.batch-head+cbor';
|
|
25
|
+
/** The leaf format a batch head names in its signed payload. */
|
|
26
|
+
export const BATCH_LEAF_FORMAT = 'chain-key-uuid16.position-u64be.entry-sha256';
|
|
27
|
+
/**
|
|
28
|
+
* How long the Server lets a batched entry wait for its head before its scan
|
|
29
|
+
* reports it. An entry no head covers yet is pending inside this, and
|
|
30
|
+
* BATCH_UNATTESTED past it.
|
|
31
|
+
*/
|
|
32
|
+
export const BATCH_PENDING_GRACE_SECONDS = 300;
|
|
33
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
34
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
35
|
+
/** Whether a protected header carries the batch marker; false on any structural failure. */
|
|
36
|
+
export function isBatchedProtectedHeader(protectedBstr) {
|
|
37
|
+
try {
|
|
38
|
+
const header = cborDecode(protectedBstr, { ...STRICT_CBOR_DECODE, useMaps: true });
|
|
39
|
+
if (!(header instanceof Map))
|
|
40
|
+
return false;
|
|
41
|
+
const marker = header.get(BATCH_MARKER_LABEL);
|
|
42
|
+
return marker === 1 || marker === 1n;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The 56 leaf bytes for one entry, or null when the chain key is not a uuid,
|
|
50
|
+
* the position is not a positive safe integer, or the hash is not 64 lowercase
|
|
51
|
+
* hex characters.
|
|
52
|
+
*/
|
|
53
|
+
export function batchLeafBytes(chainKey, chainPosition, entryHash) {
|
|
54
|
+
if (typeof chainKey !== 'string' || !UUID.test(chainKey))
|
|
55
|
+
return null;
|
|
56
|
+
if (!Number.isSafeInteger(chainPosition) || chainPosition < 1)
|
|
57
|
+
return null;
|
|
58
|
+
if (typeof entryHash !== 'string' || !HEX64.test(entryHash))
|
|
59
|
+
return null;
|
|
60
|
+
const out = Buffer.alloc(56);
|
|
61
|
+
Buffer.from(chainKey.replaceAll('-', ''), 'hex').copy(out, 0);
|
|
62
|
+
out.writeBigUInt64BE(BigInt(chainPosition), 16);
|
|
63
|
+
Buffer.from(entryHash, 'hex').copy(out, 24);
|
|
64
|
+
return new Uint8Array(out);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check one entry's inclusion proof against a head whose verdict is `ok` or
|
|
68
|
+
* `accounted`. Returns null when the proof reaches the signed root from the
|
|
69
|
+
* entry's own leaf, else what is wrong with it.
|
|
70
|
+
*/
|
|
71
|
+
export function checkBatchProof(chainKey, chainPosition, payloadHash, proof, head) {
|
|
72
|
+
if (head.treeSize === null || head.root === null)
|
|
73
|
+
return `Batch head ${head.id} carries no signed tree size or root.`;
|
|
74
|
+
if (proof.entryHash !== undefined && proof.entryHash !== payloadHash) {
|
|
75
|
+
return `The batch leaf for this entry names entry hash ${String(proof.entryHash).slice(0, 16)}..., and the entry's payloadHash is ${payloadHash.slice(0, 16)}...`;
|
|
76
|
+
}
|
|
77
|
+
const leaf = batchLeafBytes(chainKey, chainPosition, payloadHash);
|
|
78
|
+
if (leaf === null)
|
|
79
|
+
return `No batch leaf can be built for chain key ${JSON.stringify(chainKey)} at position ${chainPosition}.`;
|
|
80
|
+
if (!Number.isSafeInteger(proof.leafIndex) || !Number.isSafeInteger(proof.treeSize) || proof.leafIndex < 0) {
|
|
81
|
+
return 'The batch proof carries a leafIndex or treeSize that is not a non-negative integer.';
|
|
82
|
+
}
|
|
83
|
+
if (proof.treeSize !== head.treeSize) {
|
|
84
|
+
return `The batch proof is for a tree of ${proof.treeSize} leaves, and head ${head.id} signs ${head.treeSize}.`;
|
|
85
|
+
}
|
|
86
|
+
if (proof.leafIndex >= head.treeSize) {
|
|
87
|
+
return `The batch proof names leaf ${proof.leafIndex}, past the ${head.treeSize} leaves head ${head.id} signs.`;
|
|
88
|
+
}
|
|
89
|
+
const hashes = proof.auditPath;
|
|
90
|
+
if (!Array.isArray(hashes) || !hashes.every((h) => typeof h === 'string' && HEX64.test(h))) {
|
|
91
|
+
return 'The batch proof\'s auditPath is not an array of 64-character lowercase hex hashes.';
|
|
92
|
+
}
|
|
93
|
+
const path = hashes.map((h) => new Uint8Array(Buffer.from(h, 'hex')));
|
|
94
|
+
const root = new Uint8Array(Buffer.from(head.root, 'hex'));
|
|
95
|
+
if (!verifyRfc9162Inclusion(rfc9162LeafHash(leaf), proof.leafIndex, proof.treeSize, path, root)) {
|
|
96
|
+
return `The batch proof does not reach the root head ${head.id} signs from this entry's leaf (chain key, position ${chainPosition}, payloadHash).`;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { type AnchorDocumentInput } from './anchors.js';
|
|
2
|
+
import { type AcknowledgementSource } from './acknowledgements.js';
|
|
3
|
+
import { type VerifyExportOptions, type VerifyExportResult } from './audit-export.js';
|
|
4
|
+
import { type VaultCheckpointsPage } from './walk-checkpoints.js';
|
|
5
|
+
import { type BulkExportPage, type ExportWalkInput, type ExportWalkResultBase } from './export-pages.js';
|
|
6
|
+
export interface VerifyBulkExportOptions extends Pick<VerifyExportOptions, 'publicKeys' | 'requireKeyId' | 'requireSuppliedKeys' | 'trustAnchors' | 'distrustedKeys' | 'agentKeys' | 'federationKeys' | 'allowAfterRetirement'> {
|
|
7
|
+
/**
|
|
8
|
+
* Anchor documents from the Server's anchor bucket. Each is compared with
|
|
9
|
+
* the chain its checkpoint signs, when the walk holds that chain, as on a
|
|
10
|
+
* record export (`VerifyExportOptions.anchorDocuments`), judged through the
|
|
11
|
+
* walk's read time, the latest page read time (`readTime.exportDate`): an
|
|
12
|
+
* anchor signed after it, less one second, is listed and not judged. With
|
|
13
|
+
* no page read time, an anchor at or below the last entry the walk holds of
|
|
14
|
+
* its chain is judged and one above it is listed. On a
|
|
15
|
+
* chain the walk holds in part, an anchor outside the run it holds reads
|
|
16
|
+
* `chain_held_in_part`. Acknowledgements of lost history are the
|
|
17
|
+
* RESTORE_EPOCH entries of the platform-ops chain, when the walk holds that
|
|
18
|
+
* chain whole (a walk under a platform key), and those `acknowledgements`
|
|
19
|
+
* gives.
|
|
20
|
+
*
|
|
21
|
+
* A document of a chain the walk holds no entry of is held to the walk's
|
|
22
|
+
* query as a listed checkpoint is (`checkpoints`, `chainSet`): inside it,
|
|
23
|
+
* one that verifies under a trusted key and was signed before the read
|
|
24
|
+
* fails ANCHOR_CHAIN_CUT unless an acknowledgement accounts for it.
|
|
25
|
+
*/
|
|
26
|
+
anchorDocuments?: ReadonlyArray<AnchorDocumentInput>;
|
|
27
|
+
/**
|
|
28
|
+
* Acknowledgements of lost history the walk does not carry, as
|
|
29
|
+
* `VerifyExportOptions.acknowledgements` takes them: record exports, pages
|
|
30
|
+
* of a walk under a platform key, or `acknowledgedRewinds` items. An org
|
|
31
|
+
* admin's walk leaves out the platform-ops chain that holds them.
|
|
32
|
+
*/
|
|
33
|
+
acknowledgements?: ReadonlyArray<AcknowledgementSource>;
|
|
34
|
+
/**
|
|
35
|
+
* Pages of `GET /v1/audit-vault/checkpoints`. Each listed checkpoint filed
|
|
36
|
+
* under a chain the walk holds is verified (claim, key, signature, key
|
|
37
|
+
* window) and held to the entry at its position; one at the last position
|
|
38
|
+
* the walk holds holds the chain's end there (`chains[].heldTo`). A
|
|
39
|
+
* disagreement fails the chain: CHECKPOINT_HASH_MISMATCH, or
|
|
40
|
+
* CHECKPOINT_ROW_MISSING for one signed before the read time above the
|
|
41
|
+
* last entry of a chain whose tip the walk establishes.
|
|
42
|
+
*
|
|
43
|
+
* A listed checkpoint of a chain the walk holds no entry of is held to the
|
|
44
|
+
* walk's query (`chainSet`): one inside it that verifies under a trusted
|
|
45
|
+
* key and was signed before the read fails CHECKPOINT_ROW_MISSING (the
|
|
46
|
+
* walk left out a chain it serves); one outside it (another org's chain on
|
|
47
|
+
* an org admin's walk, a schema chain, another record under a recordId
|
|
48
|
+
* filter, a checkpoint signed after the caller's read time) is not
|
|
49
|
+
* compared; one the pages cannot place either way holds the verdict at
|
|
50
|
+
* best `unanchored`.
|
|
51
|
+
*/
|
|
52
|
+
checkpoints?: ReadonlyArray<VaultCheckpointsPage>;
|
|
53
|
+
/**
|
|
54
|
+
* When the last page of the walk was read (RFC 3339). Anchors and listed
|
|
55
|
+
* checkpoints are judged through it, less one second, in place of the
|
|
56
|
+
* pages' own `exportMetadata.exportDate`, which nothing signs: one above
|
|
57
|
+
* the last entry held of its chain that is set aside as signed after the
|
|
58
|
+
* read on the pages' own read time, or on none, holds the verdict at best
|
|
59
|
+
* `unanchored` (`capped`). A time earlier than the signed iat of an entry
|
|
60
|
+
* the pages hold throws `TypeError` naming the entry.
|
|
61
|
+
*
|
|
62
|
+
* For batched entries, a page that states its own read time is read at
|
|
63
|
+
* it, whatever this says; this dates the pages that state none (a page
|
|
64
|
+
* from a Server before 4.0.0). `readTime` reports which source dated each
|
|
65
|
+
* chain.
|
|
66
|
+
*
|
|
67
|
+
* A batched entry that carries no inclusion proof is read as waiting for
|
|
68
|
+
* its batch head when it was written within `BATCH_PENDING_GRACE_SECONDS`
|
|
69
|
+
* before its page's read time, and fails BATCH_UNATTESTED when written
|
|
70
|
+
* earlier. An entry on a page nothing dates fails BATCH_UNATTESTED, as on
|
|
71
|
+
* a record export that states no read time, and `readTime.undated` lists
|
|
72
|
+
* it. Every page was read at or before the last, so the last page's time
|
|
73
|
+
* excuses no entry its own page's time would not.
|
|
74
|
+
*/
|
|
75
|
+
exportDate?: string;
|
|
76
|
+
}
|
|
77
|
+
/** The result of `verifyBulkExport`. */
|
|
78
|
+
export interface VerifyBulkExportResult extends ExportWalkResultBase {
|
|
79
|
+
kind: 'bulk';
|
|
80
|
+
/**
|
|
81
|
+
* What the Server stated about each page, in page order: its check of the
|
|
82
|
+
* links between rows of one record that sit next to each other on the page
|
|
83
|
+
* (`chainMetadata.hashChainValid`, null where a page states none). Unsigned,
|
|
84
|
+
* and no part of the verdict.
|
|
85
|
+
*/
|
|
86
|
+
serverStatement: {
|
|
87
|
+
hashChainValid: Array<boolean | null>;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* The query the pages were served under, as far as the pages establish
|
|
91
|
+
* it. A `to` bound ends every chain at an instant rather than at its tip,
|
|
92
|
+
* so a chain's tip is established only on a walk whose query is.
|
|
93
|
+
*/
|
|
94
|
+
query: BulkWalkQuery;
|
|
95
|
+
/**
|
|
96
|
+
* Whether the set of chains the walk holds was held to anything signed.
|
|
97
|
+
* The pages' cursors are unsigned, so the chains of the walk are the
|
|
98
|
+
* ones these pages hold, unless a listed checkpoint (`checkpoints`) or an
|
|
99
|
+
* anchor document of another chain shows one missing.
|
|
100
|
+
* - `pages_only`: nothing was given to hold the set to.
|
|
101
|
+
* - `held_to_listing`: every listed checkpoint and anchor document of a
|
|
102
|
+
* chain the walk holds no entry of was placed inside or outside the
|
|
103
|
+
* walk's query; one inside it that was signed before the read is a
|
|
104
|
+
* failure (`missing`).
|
|
105
|
+
* - `undecided`: some could not be placed (`undecided`), and the verdict
|
|
106
|
+
* reads at best `unanchored`.
|
|
107
|
+
*/
|
|
108
|
+
chainSet: BulkWalkChainSet;
|
|
109
|
+
/** The acknowledgements `acknowledgements` gave, each verified from its own envelope. */
|
|
110
|
+
suppliedAcknowledgements: VerifyExportResult['suppliedAcknowledgements'];
|
|
111
|
+
}
|
|
112
|
+
/** What held the set of chains a bulk walk holds. */
|
|
113
|
+
export interface BulkWalkChainSet {
|
|
114
|
+
state: 'pages_only' | 'held_to_listing' | 'undecided';
|
|
115
|
+
detail: string;
|
|
116
|
+
/** Chains a signed checkpoint inside the walk's query names that the walk holds no entry of. */
|
|
117
|
+
missing: string[];
|
|
118
|
+
/** Chains the walk holds no entry of whose checkpoints could not be placed inside or outside its query, each with why. */
|
|
119
|
+
undecided: Array<{
|
|
120
|
+
chainKey: string;
|
|
121
|
+
reason: string;
|
|
122
|
+
}>;
|
|
123
|
+
/** Chains the walk holds no entry of whose checkpoints are outside its query, by why. */
|
|
124
|
+
outside: Array<{
|
|
125
|
+
chainKey: string;
|
|
126
|
+
reason: string;
|
|
127
|
+
}>;
|
|
128
|
+
}
|
|
129
|
+
/** The query a bulk walk was served under. */
|
|
130
|
+
export interface BulkWalkQuery {
|
|
131
|
+
/** The pages establish the query's time bounds. */
|
|
132
|
+
established: boolean;
|
|
133
|
+
/**
|
|
134
|
+
* `query_hash`: a page's `bulkExport.queryHash` (or `bulkExports[].queryHash`)
|
|
135
|
+
* is the hash of this filter. `cursor`: the walk's cursors carry its scope.
|
|
136
|
+
* Null when neither establishes it.
|
|
137
|
+
*/
|
|
138
|
+
source: 'query_hash' | 'cursor' | null;
|
|
139
|
+
filter: {
|
|
140
|
+
from: string | null;
|
|
141
|
+
to: string | null;
|
|
142
|
+
recordId: string | null;
|
|
143
|
+
agentId: string | null;
|
|
144
|
+
limit: number | null;
|
|
145
|
+
} | null;
|
|
146
|
+
detail: string;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Verify a walk of the bulk export offline.
|
|
150
|
+
*
|
|
151
|
+
* `pages` is the walk in page order: parsed JSON pages, or the NDJSON text
|
|
152
|
+
* of each page (`?format=ndjson`), or one string holding the NDJSON of every
|
|
153
|
+
* page one after another. A single page is read as a walk of one.
|
|
154
|
+
*
|
|
155
|
+
* Throws `TypeError` when the input holds no page or is not bulk export
|
|
156
|
+
* pages (a page of the schema-chain export names `verifySchemaChainExport`),
|
|
157
|
+
* on a malformed option, and on `expectedEntryCount`, which holds one chain and a walk holds
|
|
158
|
+
* many. A malformed entry inside a well-formed page is a failure on its
|
|
159
|
+
* chain, never a throw.
|
|
160
|
+
*/
|
|
161
|
+
export declare function verifyBulkExport(pages: ExportWalkInput<BulkExportPage>, options?: VerifyBulkExportOptions): VerifyBulkExportResult;
|