@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/claim.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface SignedClaim {
|
|
2
|
+
position: number;
|
|
3
|
+
previousHash: string | null;
|
|
4
|
+
/** The protected-header kid, lowercase hex. */
|
|
5
|
+
kid: string;
|
|
6
|
+
/** The signed CWT `iss` (claim 1): the issuer the Server signed under. */
|
|
7
|
+
iss: string;
|
|
8
|
+
/** The signed CWT `sub` (claim 2): the chain key the envelope is about. */
|
|
9
|
+
sub: string;
|
|
10
|
+
/** The signed CWT `iat` (claim 6), whole seconds. */
|
|
11
|
+
iat: number;
|
|
12
|
+
/** `subject[0].digest.sha256`, lowercase hex, or undefined when absent. */
|
|
13
|
+
subjectSha256: string | undefined;
|
|
14
|
+
/** The in-toto Statement's `predicateType`. */
|
|
15
|
+
predicateType: string;
|
|
16
|
+
predicate: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
/** Decode an envelope's signed claim, or null where the Server's decoder returns null. */
|
|
19
|
+
export declare function decodeSignedClaim(coseSign1: Uint8Array): SignedClaim | null;
|
|
20
|
+
/** The kid an unsigned envelope carries: eight zero bytes. */
|
|
21
|
+
export declare const UNSIGNED_KID: string;
|
|
22
|
+
/** sha256 of a UUID's 16 bytes, the digest the Server signs as a record's subject. */
|
|
23
|
+
export declare function uuidSubjectDigest(uuid: unknown): string | null;
|
package/dist/claim.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The signed claim inside a checkpoint or read-log envelope, decoded the way
|
|
3
|
+
* the Server decodes it so a dump is held to the same claim checks the Server's own scan
|
|
4
|
+
* applies. An envelope the Server would not decode as a claim decodes to null
|
|
5
|
+
* here, and the caller reports that as a claim mismatch, as the Server does.
|
|
6
|
+
*
|
|
7
|
+
* Only what those checks read is returned: the chain position and previous
|
|
8
|
+
* hash, the signed kid, the first subject's sha256 digest, and the predicate.
|
|
9
|
+
*/
|
|
10
|
+
import { decode as cborDecode } from 'cborg';
|
|
11
|
+
import { STRICT_CBOR_DECODE } from './cbor-strict.js';
|
|
12
|
+
import { decodeCoseSign1, sha256Hex } from './primitives.js';
|
|
13
|
+
const COSE_HEADER_KID = 4;
|
|
14
|
+
const COSE_HEADER_CWT_CLAIMS = 15;
|
|
15
|
+
const CWT_LABEL_ISS = 1;
|
|
16
|
+
const CWT_LABEL_SUB = 2;
|
|
17
|
+
const CWT_LABEL_IAT = 6;
|
|
18
|
+
const AGLEDGER_LABEL_CHAIN = -65537;
|
|
19
|
+
const AGLEDGER_LABEL_ACTOR = -65539;
|
|
20
|
+
const CHAIN_SUBLABEL_POSITION = 1;
|
|
21
|
+
const CHAIN_SUBLABEL_PREVIOUS_HASH = 2;
|
|
22
|
+
const ACTOR_SUBLABEL_KEY_ID = 1;
|
|
23
|
+
const ACTOR_SUBLABEL_ROLE = 2;
|
|
24
|
+
const ACTOR_SUBLABEL_OWNER_ID = 3;
|
|
25
|
+
const PREDICATE_TYPE = /^https:\/\/agledger\.ai\/predicates\/(.+)\/v1$/;
|
|
26
|
+
const hex = (b) => Buffer.from(b).toString('hex');
|
|
27
|
+
/** Decode an envelope's signed claim, or null where the Server's decoder returns null. */
|
|
28
|
+
export function decodeSignedClaim(coseSign1) {
|
|
29
|
+
const parts = decodeCoseSign1(coseSign1);
|
|
30
|
+
if (!parts)
|
|
31
|
+
return null;
|
|
32
|
+
try {
|
|
33
|
+
const ph = cborDecode(parts.protectedBstr, { ...STRICT_CBOR_DECODE, useMaps: true });
|
|
34
|
+
const payload = cborDecode(parts.payloadBstr, { ...STRICT_CBOR_DECODE, useMaps: false });
|
|
35
|
+
const cwt = ph.get(COSE_HEADER_CWT_CLAIMS);
|
|
36
|
+
const chain = ph.get(AGLEDGER_LABEL_CHAIN);
|
|
37
|
+
if (!(cwt instanceof Map) || !(chain instanceof Map))
|
|
38
|
+
return null;
|
|
39
|
+
const actor = cwt.get(AGLEDGER_LABEL_ACTOR);
|
|
40
|
+
if (!(actor instanceof Map))
|
|
41
|
+
return null;
|
|
42
|
+
const kid = ph.get(COSE_HEADER_KID);
|
|
43
|
+
if (!(kid instanceof Uint8Array))
|
|
44
|
+
return null;
|
|
45
|
+
const iat = cwt.get(CWT_LABEL_IAT);
|
|
46
|
+
const iss = cwt.get(CWT_LABEL_ISS);
|
|
47
|
+
const sub = cwt.get(CWT_LABEL_SUB);
|
|
48
|
+
if (typeof iss !== 'string' || typeof sub !== 'string')
|
|
49
|
+
return null;
|
|
50
|
+
if (typeof iat !== 'number' && typeof iat !== 'bigint')
|
|
51
|
+
return null;
|
|
52
|
+
const position = chain.get(CHAIN_SUBLABEL_POSITION);
|
|
53
|
+
if (typeof position !== 'number' && typeof position !== 'bigint')
|
|
54
|
+
return null;
|
|
55
|
+
const prev = chain.get(CHAIN_SUBLABEL_PREVIOUS_HASH);
|
|
56
|
+
if (prev !== null && !(prev instanceof Uint8Array))
|
|
57
|
+
return null;
|
|
58
|
+
if (!(actor.get(ACTOR_SUBLABEL_KEY_ID) instanceof Uint8Array))
|
|
59
|
+
return null;
|
|
60
|
+
if (!(actor.get(ACTOR_SUBLABEL_OWNER_ID) instanceof Uint8Array))
|
|
61
|
+
return null;
|
|
62
|
+
const role = actor.get(ACTOR_SUBLABEL_ROLE);
|
|
63
|
+
if (role !== 'admin' && role !== 'agent' && role !== 'platform')
|
|
64
|
+
return null;
|
|
65
|
+
const predicateType = payload['predicateType'];
|
|
66
|
+
if (typeof predicateType !== 'string' || !PREDICATE_TYPE.test(predicateType))
|
|
67
|
+
return null;
|
|
68
|
+
const subject = payload['subject'];
|
|
69
|
+
if (!Array.isArray(subject))
|
|
70
|
+
return null;
|
|
71
|
+
const first = subject[0];
|
|
72
|
+
const digest = first?.digest?.['sha256'];
|
|
73
|
+
const predicate = payload['predicate'];
|
|
74
|
+
return {
|
|
75
|
+
position: typeof position === 'bigint' ? Number(position) : position,
|
|
76
|
+
previousHash: prev === null ? null : hex(prev),
|
|
77
|
+
kid: hex(kid),
|
|
78
|
+
iss,
|
|
79
|
+
sub,
|
|
80
|
+
iat: typeof iat === 'bigint' ? Number(iat) : iat,
|
|
81
|
+
subjectSha256: digest instanceof Uint8Array ? hex(digest) : undefined,
|
|
82
|
+
predicateType,
|
|
83
|
+
predicate: predicate !== null && typeof predicate === 'object' && !Array.isArray(predicate)
|
|
84
|
+
? predicate
|
|
85
|
+
: {},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** The kid an unsigned envelope carries: eight zero bytes. */
|
|
93
|
+
export const UNSIGNED_KID = '0'.repeat(16);
|
|
94
|
+
/** sha256 of a UUID's 16 bytes, the digest the Server signs as a record's subject. */
|
|
95
|
+
export function uuidSubjectDigest(uuid) {
|
|
96
|
+
if (typeof uuid !== 'string')
|
|
97
|
+
return null;
|
|
98
|
+
const h = uuid.replaceAll('-', '').toLowerCase();
|
|
99
|
+
return /^[0-9a-f]{32}$/.test(h) ? sha256Hex(Buffer.from(h, 'hex')) : null;
|
|
100
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an export's `checkpointEvidence` block says beside its checkpoint,
|
|
3
|
+
* held to the export itself and to the anchor bucket: the coverage fields
|
|
4
|
+
* against the entries the export carries, the acknowledgements of lost
|
|
5
|
+
* history it lists, and anchor documents the caller supplies against the
|
|
6
|
+
* chain.
|
|
7
|
+
*/
|
|
8
|
+
import type { CheckApplicability, KeyRegistry } from './chain.js';
|
|
9
|
+
import { type AcknowledgementReport, type AnchorChainView, type AnchorComparison, type AnchorDocumentInput, type RewindAcknowledgement } from './anchors.js';
|
|
10
|
+
import type { FailureCode } from './failures.js';
|
|
11
|
+
export interface ExportEvidenceFinding {
|
|
12
|
+
position: number;
|
|
13
|
+
code: FailureCode;
|
|
14
|
+
detail: string;
|
|
15
|
+
}
|
|
16
|
+
/** How the coverage fields of `checkpointEvidence` stand against the entries. */
|
|
17
|
+
export interface CheckpointCoverageReport {
|
|
18
|
+
/** `applied` when the block states at least one coverage field, `skipped_no_input` otherwise. */
|
|
19
|
+
applicability: CheckApplicability;
|
|
20
|
+
/** The fields that were held to the entries and the embedded checkpoint. */
|
|
21
|
+
checked: string[];
|
|
22
|
+
}
|
|
23
|
+
/** The export's anchors: the comparison, and where the embedded checkpoint stands in the bucket. */
|
|
24
|
+
export interface ExportAnchorsReport extends AnchorComparison {
|
|
25
|
+
/**
|
|
26
|
+
* Whether a supplied document holds the embedded checkpoint. `same_bytes`:
|
|
27
|
+
* a document at its position carries the same envelope
|
|
28
|
+
* (`coseSign1Sha256`). `same_tip`: one carries a checkpoint re-signed over
|
|
29
|
+
* the same tip, as after a restore. `absent`: documents were supplied and
|
|
30
|
+
* none is at that position (`checkpointEvidence.anchor.pending` says when it
|
|
31
|
+
* has not landed yet). Null with no documents or no embedded checkpoint.
|
|
32
|
+
*/
|
|
33
|
+
embeddedCheckpoint: {
|
|
34
|
+
position: number;
|
|
35
|
+
/** `checkpointEvidence.anchor.objectKey`, the Server's unsigned hint of where it lies. */
|
|
36
|
+
objectKey: string | null;
|
|
37
|
+
found: 'same_bytes' | 'same_tip' | 'absent';
|
|
38
|
+
source: string | null;
|
|
39
|
+
} | null;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Hold `lastEntryPosition`, `coveredThroughPosition`, `coverage`,
|
|
43
|
+
* `uncoveredFromPosition` and `uncoveredEntries` to the entries the export
|
|
44
|
+
* carries and the checkpoint it embeds, by the rule the Server computes them
|
|
45
|
+
* with. Each is checked only when the block states it. A stated last position
|
|
46
|
+
* above the last entry carried is CHAIN_POSITION_GAP (entries were cut);
|
|
47
|
+
* every other disagreement is CHECKPOINT_COVERAGE_MISMATCH.
|
|
48
|
+
*/
|
|
49
|
+
export declare function verifyCheckpointCoverage(evidence: unknown, positions: readonly number[]): {
|
|
50
|
+
report: CheckpointCoverageReport;
|
|
51
|
+
finding?: ExportEvidenceFinding;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* What a page walk adds to one chain's anchor comparison, where the chain
|
|
55
|
+
* came from pages rather than from a record export.
|
|
56
|
+
*/
|
|
57
|
+
export interface ExportAnchorsWalkView {
|
|
58
|
+
/** The latest signed iat judged when the evidence block states no `anchorsJudgedThroughIat`; null for none. */
|
|
59
|
+
judgedThroughIat: number | null;
|
|
60
|
+
/** `judgedThroughIat` comes from the caller's read time, and takes precedence over the evidence block's. */
|
|
61
|
+
readTimeGiven?: boolean;
|
|
62
|
+
/** The run the walk holds, when it holds the chain in part. */
|
|
63
|
+
heldInPart?: AnchorChainView['heldInPart'];
|
|
64
|
+
/** Acknowledgements of lost history read beside the export's own (a walk's platform-ops chain). */
|
|
65
|
+
acknowledgements?: readonly RewindAcknowledgement[];
|
|
66
|
+
/** Checkpoint envelopes the walk holds for the chain beside the evidence block's. */
|
|
67
|
+
checkpoints?: AnchorChainView['checkpoints'];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Verify the acknowledgements `checkpointEvidence.acknowledgedRewinds` lists,
|
|
71
|
+
* then compare the supplied anchor documents with the export's chain, judging
|
|
72
|
+
* only anchors signed at or before `anchorsJudgedThroughIat` (or, on a walk
|
|
73
|
+
* whose block states none, the walk's own judged-through time).
|
|
74
|
+
*/
|
|
75
|
+
export declare function verifyExportAnchors(chainKey: string, evidence: unknown, entries: ReadonlyArray<{
|
|
76
|
+
chainPosition: number;
|
|
77
|
+
payloadHash: unknown;
|
|
78
|
+
}>, documents: readonly AnchorDocumentInput[] | undefined, keys: KeyRegistry, signingSince: string | null, walk?: ExportAnchorsWalkView, caller?: {
|
|
79
|
+
judgedThroughIat: number;
|
|
80
|
+
}): {
|
|
81
|
+
anchors: ExportAnchorsReport;
|
|
82
|
+
acknowledgedRewinds: AcknowledgementReport;
|
|
83
|
+
findings: ExportEvidenceFinding[];
|
|
84
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { compareCheckpointAnchors, verifyRewindAcknowledgements, } from './anchors.js';
|
|
2
|
+
import { entryRun, sha256Hex } from './primitives.js';
|
|
3
|
+
function isRecord(value) {
|
|
4
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Hold `lastEntryPosition`, `coveredThroughPosition`, `coverage`,
|
|
8
|
+
* `uncoveredFromPosition` and `uncoveredEntries` to the entries the export
|
|
9
|
+
* carries and the checkpoint it embeds, by the rule the Server computes them
|
|
10
|
+
* with. Each is checked only when the block states it. A stated last position
|
|
11
|
+
* above the last entry carried is CHAIN_POSITION_GAP (entries were cut);
|
|
12
|
+
* every other disagreement is CHECKPOINT_COVERAGE_MISMATCH.
|
|
13
|
+
*/
|
|
14
|
+
export function verifyCheckpointCoverage(evidence, positions) {
|
|
15
|
+
const report = { applicability: 'skipped_no_input', checked: [] };
|
|
16
|
+
if (!isRecord(evidence))
|
|
17
|
+
return { report };
|
|
18
|
+
const held = positions.length === 0 ? null : Math.max(...positions);
|
|
19
|
+
const cp = isRecord(evidence['checkpoint']) ? evidence['checkpoint'] : null;
|
|
20
|
+
const cpAt = cp !== null && Number.isSafeInteger(cp['chainPosition']) ? cp['chainPosition'] : null;
|
|
21
|
+
// The Server's coverage rule, from the embedded checkpoint and the last entry.
|
|
22
|
+
const expected = { lastEntryPosition: held, coveredThroughPosition: cpAt };
|
|
23
|
+
if (cpAt === null) {
|
|
24
|
+
Object.assign(expected, { coverage: 'no_checkpoint', uncoveredFromPosition: held === null ? null : 1, uncoveredEntries: held ?? 0 });
|
|
25
|
+
}
|
|
26
|
+
else if (held === null || cpAt > held) {
|
|
27
|
+
Object.assign(expected, { coverage: 'chain_short_of_checkpoint', uncoveredFromPosition: null, uncoveredEntries: 0 });
|
|
28
|
+
}
|
|
29
|
+
else if (cpAt === held) {
|
|
30
|
+
Object.assign(expected, { coverage: 'tip_covered', uncoveredFromPosition: null, uncoveredEntries: 0 });
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
Object.assign(expected, { coverage: 'tip_uncovered', uncoveredFromPosition: cpAt + 1, uncoveredEntries: held - cpAt });
|
|
34
|
+
}
|
|
35
|
+
const differs = [];
|
|
36
|
+
for (const [field, want] of Object.entries(expected)) {
|
|
37
|
+
if (!(field in evidence))
|
|
38
|
+
continue;
|
|
39
|
+
report.checked.push(field);
|
|
40
|
+
if (evidence[field] !== want)
|
|
41
|
+
differs.push(`${field} ${JSON.stringify(evidence[field])} where the export's entries and checkpoint give ${JSON.stringify(want)}`);
|
|
42
|
+
}
|
|
43
|
+
if (report.checked.length > 0)
|
|
44
|
+
report.applicability = 'applied';
|
|
45
|
+
const stated = evidence['lastEntryPosition'];
|
|
46
|
+
if (typeof stated === 'number' && Number.isSafeInteger(stated) && stated > (held ?? 0)) {
|
|
47
|
+
return {
|
|
48
|
+
report,
|
|
49
|
+
finding: {
|
|
50
|
+
position: (held ?? 0) + 1,
|
|
51
|
+
code: 'CHAIN_POSITION_GAP',
|
|
52
|
+
detail: `checkpointEvidence.lastEntryPosition is ${stated} and the last entry the export carries is at ${held ?? 'no position'}: ${entryRun((held ?? 0) + 1, stated, 'were')} cut from the export.`,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (differs.length > 0) {
|
|
57
|
+
return { report, finding: { position: 0, code: 'CHECKPOINT_COVERAGE_MISMATCH', detail: `checkpointEvidence states ${differs.join('; ')}.` } };
|
|
58
|
+
}
|
|
59
|
+
return { report };
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Verify the acknowledgements `checkpointEvidence.acknowledgedRewinds` lists,
|
|
63
|
+
* then compare the supplied anchor documents with the export's chain, judging
|
|
64
|
+
* only anchors signed at or before `anchorsJudgedThroughIat` (or, on a walk
|
|
65
|
+
* whose block states none, the walk's own judged-through time).
|
|
66
|
+
*/
|
|
67
|
+
export function verifyExportAnchors(chainKey, evidence, entries, documents, keys, signingSince, walk, caller) {
|
|
68
|
+
const ev = isRecord(evidence) ? evidence : {};
|
|
69
|
+
const acks = verifyRewindAcknowledgements(ev['acknowledgedRewinds'], chainKey, keys, signingSince);
|
|
70
|
+
const stated = ev['anchorsJudgedThroughIat'];
|
|
71
|
+
// The caller's read time first: the block's and the pages' are unsigned.
|
|
72
|
+
const readTimeGiven = caller !== undefined || walk?.readTimeGiven === true;
|
|
73
|
+
const judged = caller !== undefined
|
|
74
|
+
? caller.judgedThroughIat
|
|
75
|
+
: walk?.readTimeGiven === true
|
|
76
|
+
? walk.judgedThroughIat
|
|
77
|
+
: typeof stated === 'number' && Number.isSafeInteger(stated) ? stated : walk?.judgedThroughIat ?? null;
|
|
78
|
+
const cp = isRecord(ev['checkpoint']) ? ev['checkpoint'] : null;
|
|
79
|
+
const cpAt = cp !== null && Number.isSafeInteger(cp['chainPosition']) ? cp['chainPosition'] : null;
|
|
80
|
+
const cpSha = cp !== null && typeof cp['coseSign1'] === 'string' ? sha256Hex(Buffer.from(cp['coseSign1'], 'base64')) : null;
|
|
81
|
+
const held = entries.length === 0 ? null : Math.max(...entries.map((e) => e.chainPosition));
|
|
82
|
+
const comparison = compareCheckpointAnchors(documents ?? [], {
|
|
83
|
+
chainKey,
|
|
84
|
+
entries,
|
|
85
|
+
lastEntryPosition: held,
|
|
86
|
+
judgedThroughIat: judged,
|
|
87
|
+
readTimeGiven,
|
|
88
|
+
checkpoints: [...(cpAt !== null && cpSha !== null ? [{ chainPosition: cpAt, coseSign1Sha256: cpSha }] : []), ...(walk?.checkpoints ?? [])],
|
|
89
|
+
...(walk?.heldInPart !== undefined ? { heldInPart: walk.heldInPart } : {}),
|
|
90
|
+
}, keys, [...acks.acknowledgements, ...(walk?.acknowledgements ?? [])]);
|
|
91
|
+
let embedded = null;
|
|
92
|
+
if (cpAt !== null && (documents?.length ?? 0) > 0) {
|
|
93
|
+
const anchor = isRecord(ev['anchor']) ? ev['anchor'] : null;
|
|
94
|
+
const objectKey = anchor !== null && typeof anchor['objectKey'] === 'string' ? anchor['objectKey'] : null;
|
|
95
|
+
const at = comparison.verdicts.filter((v) => v.position === cpAt && v.kind === 'agree');
|
|
96
|
+
const same = at.find((v) => v.kind === 'agree' && v.how === 'same_bytes') ?? at[0];
|
|
97
|
+
embedded = {
|
|
98
|
+
position: cpAt,
|
|
99
|
+
objectKey,
|
|
100
|
+
found: same === undefined ? 'absent' : same.kind === 'agree' ? same.how : 'absent',
|
|
101
|
+
source: same?.source ?? null,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
anchors: { ...comparison, embeddedCheckpoint: embedded },
|
|
106
|
+
acknowledgedRewinds: acks.report,
|
|
107
|
+
findings: [...acks.findings, ...comparison.findings],
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The signed evidence an export carries beside its chain: the record's latest
|
|
3
|
+
* checkpoint (`exportMetadata.checkpointEvidence.checkpoint`) and, on a
|
|
4
|
+
* `?receipts=true` export, one SCITT Receipt per entry (`integrity.receipt`).
|
|
5
|
+
*
|
|
6
|
+
* A checkpoint is what holds a chain's length: a chain cut from the end still
|
|
7
|
+
* links and verifies entry by entry, and the counts the export states are
|
|
8
|
+
* unsigned. The embedded checkpoint is held to the same checks, in the same
|
|
9
|
+
* order and under the same codes, as a dump's `vault_checkpoints` row, with
|
|
10
|
+
* its key resolved through the same registry (and trust walk) as the entries.
|
|
11
|
+
*/
|
|
12
|
+
import type { CheckApplicability, KeyRegistry } from './chain.js';
|
|
13
|
+
import type { FailureCode } from './failures.js';
|
|
14
|
+
/** The checkpoint a `checkpointEvidence` block embeds (only the fields the verifier reads). */
|
|
15
|
+
export interface ExportCheckpointInput {
|
|
16
|
+
id?: string;
|
|
17
|
+
chainPosition: number;
|
|
18
|
+
/** `sha256:<hex>`, the row's copy of the signed `predicate.chain_tip_hash`. */
|
|
19
|
+
chainTipHash: string;
|
|
20
|
+
signingKeyId: string | null;
|
|
21
|
+
createdAt: string;
|
|
22
|
+
/** Base64 COSE_Sign1 over the vault-checkpoint in-toto Statement. */
|
|
23
|
+
coseSign1: string;
|
|
24
|
+
/** Lowercase hex sha256 of the `coseSign1` bytes. */
|
|
25
|
+
coseSign1Sha256?: string;
|
|
26
|
+
/** The Server's own answer; never read as one. */
|
|
27
|
+
envelopeVerified?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** `exportMetadata.checkpointEvidence` (Server 3.0.0 and later), the fields the verifier reads. */
|
|
30
|
+
export interface CheckpointEvidenceInput {
|
|
31
|
+
version?: number;
|
|
32
|
+
chainKey?: string;
|
|
33
|
+
coverage?: 'tip_covered' | 'tip_uncovered' | 'no_checkpoint' | 'chain_short_of_checkpoint' | (string & {});
|
|
34
|
+
lastEntryPosition?: number | null;
|
|
35
|
+
coveredThroughPosition?: number | null;
|
|
36
|
+
checkpoint?: ExportCheckpointInput | null;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
export interface CheckpointFinding {
|
|
40
|
+
position: number;
|
|
41
|
+
code: FailureCode;
|
|
42
|
+
detail: string;
|
|
43
|
+
}
|
|
44
|
+
/** What the embedded checkpoint showed. */
|
|
45
|
+
export interface CheckpointEvidenceReport {
|
|
46
|
+
/** The position the checkpoint signs, or null when the export embeds none. */
|
|
47
|
+
position: number | null;
|
|
48
|
+
/** `verified`: signed, its key resolved and its signature verified. `unsigned`: written before signing began. `accounted`: signed by a key `distrustedKeys` sets aside. */
|
|
49
|
+
state: 'verified' | 'unsigned' | 'accounted' | 'failed' | 'absent';
|
|
50
|
+
}
|
|
51
|
+
interface EntryView {
|
|
52
|
+
chainPosition: number;
|
|
53
|
+
payloadHash: unknown;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Hold the chain to the checkpoint the export embeds. Mirrors the dump's
|
|
57
|
+
* checkpoint walk: the entry at its position, its hash, its signed claim
|
|
58
|
+
* against its row (predicate type, position, count, range, tip, subject,
|
|
59
|
+
* kid), then unsigned-after-signing, key resolution, signature and the key
|
|
60
|
+
* window at the signed iat.
|
|
61
|
+
*/
|
|
62
|
+
export declare function verifyExportCheckpoint(recordId: string, evidence: unknown, entries: readonly EntryView[], keys: KeyRegistry, signingSince: string | null): {
|
|
63
|
+
applicability: CheckApplicability;
|
|
64
|
+
report: CheckpointEvidenceReport;
|
|
65
|
+
finding?: CheckpointFinding;
|
|
66
|
+
};
|
|
67
|
+
/** Receipts present on the export vs verified. */
|
|
68
|
+
export interface ReceiptCounts {
|
|
69
|
+
present: number;
|
|
70
|
+
verified: number;
|
|
71
|
+
}
|
|
72
|
+
interface ReceiptEntryView {
|
|
73
|
+
chainPosition: number;
|
|
74
|
+
coseSign1: unknown;
|
|
75
|
+
receipt: unknown;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Verify every Receipt a `?receipts=true` export carries: the Transparency
|
|
79
|
+
* Service signature under the key its protected-header kid names, resolved
|
|
80
|
+
* through the same registry as the entries, and the RFC 9162 inclusion of the
|
|
81
|
+
* entry's own COSE_Sign1 bytes at leaf `chainPosition - 1` of a tree no
|
|
82
|
+
* larger than the chain. A Receipt on some entries and not others is a
|
|
83
|
+
* finding: the Server issues one per entry or none.
|
|
84
|
+
*/
|
|
85
|
+
export declare function verifyExportReceipts(entries: readonly ReceiptEntryView[], keys: KeyRegistry): {
|
|
86
|
+
applicability: CheckApplicability;
|
|
87
|
+
counts: ReceiptCounts;
|
|
88
|
+
finding?: CheckpointFinding;
|
|
89
|
+
};
|
|
90
|
+
export {};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { distrustAccounts, writtenWhileSigning } from './chain.js';
|
|
2
|
+
import { placeSignedIat, VAULT_CHECKPOINT_PREDICATE_TYPE } from './anchors.js';
|
|
3
|
+
import { decodeSignedClaim, UNSIGNED_KID, uuidSubjectDigest } from './claim.js';
|
|
4
|
+
import { decodeCoseSign1, extractKid, extractReceiptInclusionProof, sha256Hex, verifyCoseSign1, verifyReceipt } from './primitives.js';
|
|
5
|
+
/**
|
|
6
|
+
* Hold the chain to the checkpoint the export embeds. Mirrors the dump's
|
|
7
|
+
* checkpoint walk: the entry at its position, its hash, its signed claim
|
|
8
|
+
* against its row (predicate type, position, count, range, tip, subject,
|
|
9
|
+
* kid), then unsigned-after-signing, key resolution, signature and the key
|
|
10
|
+
* window at the signed iat.
|
|
11
|
+
*/
|
|
12
|
+
export function verifyExportCheckpoint(recordId, evidence, entries, keys, signingSince) {
|
|
13
|
+
if (evidence === undefined || evidence === null) {
|
|
14
|
+
return { applicability: 'skipped_no_input', report: { position: null, state: 'absent' } };
|
|
15
|
+
}
|
|
16
|
+
const ev = evidence;
|
|
17
|
+
const fail = (position, code, detail) => ({
|
|
18
|
+
applicability: 'applied',
|
|
19
|
+
report: { position, state: 'failed' },
|
|
20
|
+
finding: { position, code, detail },
|
|
21
|
+
});
|
|
22
|
+
if (typeof evidence !== 'object' || Array.isArray(evidence)) {
|
|
23
|
+
return fail(0, 'CHECKPOINT_CLAIM_MISMATCH', 'exportMetadata.checkpointEvidence is not an object.');
|
|
24
|
+
}
|
|
25
|
+
const cp = ev.checkpoint;
|
|
26
|
+
if (cp === undefined || cp === null) {
|
|
27
|
+
// `no_checkpoint` is the only coverage with no checkpoint behind it.
|
|
28
|
+
if (ev.coverage !== undefined && ev.coverage !== 'no_checkpoint') {
|
|
29
|
+
return fail(0, 'CHECKPOINT_ROW_MISSING', `checkpointEvidence states coverage ${JSON.stringify(ev.coverage)} but embeds no checkpoint.`);
|
|
30
|
+
}
|
|
31
|
+
return { applicability: 'skipped_no_input', report: { position: null, state: 'absent' } };
|
|
32
|
+
}
|
|
33
|
+
if (typeof cp !== 'object' || !Number.isSafeInteger(cp.chainPosition) || cp.chainPosition < 1) {
|
|
34
|
+
return fail(0, 'CHECKPOINT_CLAIM_MISMATCH', 'checkpointEvidence.checkpoint carries no positive integer chainPosition.');
|
|
35
|
+
}
|
|
36
|
+
const at = cp.chainPosition;
|
|
37
|
+
const label = `Checkpoint at position ${at}`;
|
|
38
|
+
if (ev.chainKey !== undefined && ev.chainKey !== recordId) {
|
|
39
|
+
return fail(at, 'CHECKPOINT_CLAIM_MISMATCH', `${label}: checkpointEvidence.chainKey ${JSON.stringify(ev.chainKey)} is not this export's record ${recordId}.`);
|
|
40
|
+
}
|
|
41
|
+
const entry = entries.find((e) => e.chainPosition === at);
|
|
42
|
+
if (!entry) {
|
|
43
|
+
return fail(at, 'CHECKPOINT_ROW_MISSING', `${label}: the export holds no entry at that position (it holds ${entries.length}), so entries the checkpoint signs were cut.`);
|
|
44
|
+
}
|
|
45
|
+
const tip = typeof cp.chainTipHash === 'string' && cp.chainTipHash.startsWith('sha256:') ? cp.chainTipHash.slice('sha256:'.length) : null;
|
|
46
|
+
if (tip === null || entry.payloadHash !== tip) {
|
|
47
|
+
return fail(at, 'CHECKPOINT_HASH_MISMATCH', `${label}: chainTipHash ${JSON.stringify(cp.chainTipHash ?? null)} is not the payloadHash of the entry there.`);
|
|
48
|
+
}
|
|
49
|
+
const bytes = typeof cp.coseSign1 === 'string' ? Buffer.from(cp.coseSign1, 'base64') : Buffer.alloc(0);
|
|
50
|
+
if (cp.coseSign1Sha256 !== undefined && cp.coseSign1Sha256 !== sha256Hex(bytes)) {
|
|
51
|
+
return fail(at, 'CHECKPOINT_CLAIM_MISMATCH', `${label}: coseSign1Sha256 is not the sha256 of coseSign1.`);
|
|
52
|
+
}
|
|
53
|
+
// Set aside before the claim is read, as the dump does: a row signed by a
|
|
54
|
+
// distrusted key the walk accounts for is never evidence.
|
|
55
|
+
if (cp.signingKeyId !== null && cp.signingKeyId !== undefined) {
|
|
56
|
+
const key = keys.get(cp.signingKeyId);
|
|
57
|
+
if (key && distrustAccounts(key, cp.createdAt) && verifyCoseSign1(bytes, key.spkiBase64) === 'ok') {
|
|
58
|
+
return { applicability: 'applied', report: { position: at, state: 'accounted' } };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const claim = decodeSignedClaim(bytes);
|
|
62
|
+
const disagreement = claim === null
|
|
63
|
+
? 'cose_sign1 does not decode as a signed AGLedger claim'
|
|
64
|
+
: firstDisagreement([
|
|
65
|
+
['predicateType', claim.predicateType, VAULT_CHECKPOINT_PREDICATE_TYPE],
|
|
66
|
+
['position', claim.position, at],
|
|
67
|
+
['count', claim.predicate['count'], at],
|
|
68
|
+
['range.start', claim.predicate['range']?.['start'], 1],
|
|
69
|
+
['range.end', claim.predicate['range']?.['end'], at],
|
|
70
|
+
['chain_tip_hash', claim.predicate['chain_tip_hash'], `sha256:${tip}`],
|
|
71
|
+
['subject digest', claim.subjectSha256, uuidSubjectDigest(recordId)],
|
|
72
|
+
['kid', claim.kid, cp.signingKeyId ?? UNSIGNED_KID],
|
|
73
|
+
]);
|
|
74
|
+
if (disagreement !== null) {
|
|
75
|
+
return fail(at, 'CHECKPOINT_CLAIM_MISMATCH', `${label}: checkpoint claim does not match its row: ${disagreement}`);
|
|
76
|
+
}
|
|
77
|
+
if (cp.signingKeyId === null || cp.signingKeyId === undefined) {
|
|
78
|
+
if (writtenWhileSigning(cp.createdAt, signingSince)) {
|
|
79
|
+
return fail(at, 'CHECKPOINT_UNSIGNED', `${label}: checkpoint has no signingKeyId but was written ${String(cp.createdAt)}, at or after the earliest signing key activation ${String(signingSince)}`);
|
|
80
|
+
}
|
|
81
|
+
return { applicability: 'applied', report: { position: at, state: 'unsigned' } };
|
|
82
|
+
}
|
|
83
|
+
const key = keys.get(cp.signingKeyId);
|
|
84
|
+
if (!key) {
|
|
85
|
+
return fail(at, 'CHAIN_SIGNATURE_MISSING_KEY', `${label}: checkpoint signingKeyId "${cp.signingKeyId}" is not among the keys this verification holds.`);
|
|
86
|
+
}
|
|
87
|
+
const keyed = keyVerdict(key, label);
|
|
88
|
+
if (keyed !== null)
|
|
89
|
+
return fail(at, keyed.code, keyed.detail);
|
|
90
|
+
const outcome = verifyCoseSign1(bytes, key.spkiBase64);
|
|
91
|
+
if (outcome !== 'ok') {
|
|
92
|
+
return outcome === 'unsupported-key-algorithm'
|
|
93
|
+
? fail(at, 'CHAIN_UNSUPPORTED_ALGORITHM', `${label}: this checkpoint's signature could NOT BE CHECKED: key ${key.keyId} commits to an algorithm this host cannot compute.`)
|
|
94
|
+
: fail(at, 'CHECKPOINT_SIGNATURE_INVALID', `${label}: checkpoint COSE_Sign1 signature does not verify (${outcome})`);
|
|
95
|
+
}
|
|
96
|
+
// Placed by the iat it signs, with the Server's clock-skew allowance at
|
|
97
|
+
// each edge: the row's createdAt is unsigned.
|
|
98
|
+
const placed = placeSignedIat(key, claim.iat, 'Checkpoint');
|
|
99
|
+
switch (placed.placement) {
|
|
100
|
+
case 'inside':
|
|
101
|
+
case 'accounted':
|
|
102
|
+
return { applicability: 'applied', report: { position: at, state: 'verified' } };
|
|
103
|
+
case 'not-yet-active':
|
|
104
|
+
return fail(at, 'CHECKPOINT_KEY_NOT_YET_ACTIVE', `${label}: ${placed.detail}`);
|
|
105
|
+
case 'expired':
|
|
106
|
+
return fail(at, 'CHECKPOINT_KEY_EXPIRED', `${label}: ${placed.detail}`);
|
|
107
|
+
case 'unplaceable':
|
|
108
|
+
return fail(at, 'CHAIN_MALFORMED_ENTRY', `${label}: ${placed.detail}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** The walk's verdict on a key a checkpoint or Receipt names, in the dump's order. */
|
|
112
|
+
function keyVerdict(key, label) {
|
|
113
|
+
if (key.trust === 'unanchored') {
|
|
114
|
+
return { code: 'CHECKPOINT_KEY_UNANCHORED', detail: `${label}: key ${key.keyId} is published with the export, but no signed key statement links it to a trust anchor.` };
|
|
115
|
+
}
|
|
116
|
+
if (key.trust === 'undecided') {
|
|
117
|
+
return { code: 'CHAIN_UNSUPPORTED_ALGORITHM', detail: `${label}: the signature could NOT BE CHECKED. Key ${key.keyId} is reached only through a key statement signed under an algorithm this host cannot compute.` };
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
function firstDisagreement(fields) {
|
|
122
|
+
for (const [field, signed, row] of fields) {
|
|
123
|
+
if (signed !== row)
|
|
124
|
+
return `the signed ${field} ${JSON.stringify(signed ?? null)} is not the row's ${JSON.stringify(row ?? null)}`;
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Verify every Receipt a `?receipts=true` export carries: the Transparency
|
|
130
|
+
* Service signature under the key its protected-header kid names, resolved
|
|
131
|
+
* through the same registry as the entries, and the RFC 9162 inclusion of the
|
|
132
|
+
* entry's own COSE_Sign1 bytes at leaf `chainPosition - 1` of a tree no
|
|
133
|
+
* larger than the chain. A Receipt on some entries and not others is a
|
|
134
|
+
* finding: the Server issues one per entry or none.
|
|
135
|
+
*/
|
|
136
|
+
export function verifyExportReceipts(entries, keys) {
|
|
137
|
+
const carrying = entries.filter((e) => e.receipt !== undefined && e.receipt !== null);
|
|
138
|
+
const counts = { present: carrying.length, verified: 0 };
|
|
139
|
+
if (carrying.length === 0)
|
|
140
|
+
return { applicability: 'skipped_no_input', counts };
|
|
141
|
+
const fail = (position, detail, code = 'RECEIPT_INVALID') => ({
|
|
142
|
+
applicability: 'applied',
|
|
143
|
+
counts,
|
|
144
|
+
finding: { position, code, detail },
|
|
145
|
+
});
|
|
146
|
+
for (const e of entries) {
|
|
147
|
+
const at = e.chainPosition;
|
|
148
|
+
if (e.receipt === undefined || e.receipt === null) {
|
|
149
|
+
return fail(at, `Entry ${at} carries no receipt while ${carrying.length} other entries of this export do; the Server issues one for every entry or for none.`);
|
|
150
|
+
}
|
|
151
|
+
if (typeof e.receipt !== 'string' || typeof e.coseSign1 !== 'string') {
|
|
152
|
+
return fail(at, `Entry ${at}: receipt is not base64 text.`);
|
|
153
|
+
}
|
|
154
|
+
const receipt = Buffer.from(e.receipt, 'base64');
|
|
155
|
+
const claim = decodeReceiptKid(receipt);
|
|
156
|
+
if (claim === null)
|
|
157
|
+
return fail(at, `Entry ${at}: receipt does not decode as a COSE_Sign1 with a kid.`);
|
|
158
|
+
const key = keys.get(claim);
|
|
159
|
+
if (!key) {
|
|
160
|
+
return fail(at, `Entry ${at}: receipt kid "${claim}" is not among the keys this verification holds.`, 'CHAIN_SIGNATURE_MISSING_KEY');
|
|
161
|
+
}
|
|
162
|
+
const keyed = keyVerdict(key, `Entry ${at} receipt`);
|
|
163
|
+
if (keyed !== null)
|
|
164
|
+
return fail(at, keyed.detail, keyed.code === 'CHECKPOINT_KEY_UNANCHORED' ? 'RECEIPT_INVALID' : keyed.code);
|
|
165
|
+
const proof = receiptLeaf(receipt);
|
|
166
|
+
if (proof === null || proof.leafIndex !== at - 1 || proof.treeSize > entries.length || proof.treeSize < at) {
|
|
167
|
+
return fail(at, `Entry ${at}: receipt names leaf ${proof?.leafIndex ?? 'none'} of a ${proof?.treeSize ?? '?'}-leaf tree, not leaf ${at - 1} of this ${entries.length}-entry chain.`);
|
|
168
|
+
}
|
|
169
|
+
const outcome = verifyReceipt(receipt, Buffer.from(e.coseSign1, 'base64'), key.spkiBase64);
|
|
170
|
+
if (outcome === 'unsupported-algorithm') {
|
|
171
|
+
return fail(at, `Entry ${at}: this receipt's signature could NOT BE CHECKED: key ${key.keyId} commits to an algorithm this host cannot compute.`, 'CHAIN_UNSUPPORTED_ALGORITHM');
|
|
172
|
+
}
|
|
173
|
+
if (outcome !== 'ok')
|
|
174
|
+
return fail(at, `Entry ${at}: receipt does not verify (${outcome}).`);
|
|
175
|
+
counts.verified += 1;
|
|
176
|
+
}
|
|
177
|
+
return { applicability: 'applied', counts };
|
|
178
|
+
}
|
|
179
|
+
function decodeReceiptKid(bytes) {
|
|
180
|
+
const parts = decodeCoseSign1(bytes);
|
|
181
|
+
return parts ? extractKid(parts.protectedBstr) : null;
|
|
182
|
+
}
|
|
183
|
+
function receiptLeaf(bytes) {
|
|
184
|
+
const proof = extractReceiptInclusionProof(bytes);
|
|
185
|
+
return proof === null ? null : { leafIndex: proof.leafIndex, treeSize: proof.treeSize };
|
|
186
|
+
}
|