@mago-foundation/mago-ski 0.2.0 → 0.3.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/README.md +30 -3
- package/dist/hosts/github/run.d.ts +3 -1
- package/dist/hosts/github/run.js +29 -9
- package/dist/hosts/pi/index.js +48 -5
- package/dist/src/admin.d.ts +136 -1
- package/dist/src/admin.js +221 -84
- package/dist/src/cli.js +335 -10
- package/dist/src/fs-safe.d.ts +15 -2
- package/dist/src/fs-safe.js +32 -3
- package/dist/src/gate/adapters/claude-code-hook.d.ts +1 -0
- package/dist/src/gate/adapters/claude-code-hook.js +4 -0
- package/dist/src/gate/adapters/claude-code-settings.d.ts +13 -0
- package/dist/src/gate/adapters/claude-code-settings.js +49 -0
- package/dist/src/gate/adapters/claude-code.d.ts +33 -0
- package/dist/src/gate/adapters/claude-code.js +193 -0
- package/dist/src/gate/adapters/client.d.ts +13 -0
- package/dist/src/gate/adapters/client.js +57 -0
- package/dist/src/gate/adapters/codex-hook.d.ts +1 -0
- package/dist/src/gate/adapters/codex-hook.js +5 -0
- package/dist/src/gate/adapters/codex-requirements.d.ts +19 -0
- package/dist/src/gate/adapters/codex-requirements.js +56 -0
- package/dist/src/gate/adapters/codex.d.ts +16 -0
- package/dist/src/gate/adapters/codex.js +574 -0
- package/dist/src/gate/adapters/hook-main.d.ts +7 -0
- package/dist/src/gate/adapters/hook-main.js +35 -0
- package/dist/src/gate/adapters/host-pid.d.ts +1 -0
- package/dist/src/gate/adapters/host-pid.js +20 -0
- package/dist/src/gate/admission.d.ts +54 -0
- package/dist/src/gate/admission.js +490 -0
- package/dist/src/gate/contract.d.ts +51 -0
- package/dist/src/gate/contract.js +94 -0
- package/dist/src/gate/decision-log.d.ts +38 -0
- package/dist/src/gate/decision-log.js +251 -0
- package/dist/src/gate/generations.d.ts +67 -0
- package/dist/src/gate/generations.js +518 -0
- package/dist/src/gate/proc.d.ts +31 -0
- package/dist/src/gate/proc.js +79 -0
- package/dist/src/gate/refresh.d.ts +11 -0
- package/dist/src/gate/refresh.js +30 -0
- package/dist/src/gate/run-verifier.d.ts +23 -0
- package/dist/src/gate/run-verifier.js +89 -0
- package/dist/src/gate/service.d.ts +80 -0
- package/dist/src/gate/service.js +722 -0
- package/dist/src/gate/strict-json.d.ts +11 -0
- package/dist/src/gate/strict-json.js +190 -0
- package/dist/src/gate/trust-store.d.ts +36 -0
- package/dist/src/gate/trust-store.js +251 -0
- package/dist/src/gate/verifier-worker.d.ts +8 -0
- package/dist/src/gate/verifier-worker.js +66 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/keys.d.ts +4 -0
- package/dist/src/keys.js +23 -14
- package/dist/src/policy.d.ts +145 -0
- package/dist/src/policy.js +554 -35
- package/dist/src/recovery-admin.d.ts +173 -0
- package/dist/src/recovery-admin.js +400 -0
- package/dist/src/root-events.d.ts +107 -0
- package/dist/src/root-events.js +376 -0
- package/dist/src/timestamp.d.ts +61 -0
- package/dist/src/timestamp.js +107 -0
- package/dist/src/tree-digest.d.ts +1 -0
- package/dist/src/tree-digest.js +1 -1
- package/dist/src/verify.d.ts +33 -0
- package/dist/src/verify.js +4 -2
- package/docs/hosts/adapter-interface.md +8 -2
- package/docs/hosts/github-action.md +1 -1
- package/docs/hosts/pi.md +2 -0
- package/docs/spec/gui-admin-rack-v1.md +289 -0
- package/docs/spec/root-recovery-v1.md +33 -12
- package/docs/spec/trust-documents-v1.md +8 -2
- package/docs/spec/verification.md +9 -2
- package/docs/threat-model.md +4 -2
- package/package.json +3 -1
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
// Root recovery (docs/spec/root-recovery-v1.md): a recovery set of offline keys, pinned by fingerprint in the host
|
|
2
|
+
// policy, signs numbered root events that revoke or replace the root key. Hosts walk the log from the policy's
|
|
3
|
+
// starting point at every trust check; one event that breaks a rule makes the whole log UNVERIFIABLE.
|
|
4
|
+
import { sign, verify } from 'node:crypto';
|
|
5
|
+
import { canonicalBytes, exactKeys, isRecord, parseCanonicalJsonBytes, requireArray, requirePositiveInteger, requireString } from "./canonical-json.js";
|
|
6
|
+
import { preAuthEncoding } from "./dsse.js";
|
|
7
|
+
import { parseSpki, validateKeyId } from "./keys.js";
|
|
8
|
+
import { CLOCK_SKEW_MS, formatTimestamp, parseTimestamp, requireValidDate } from "./time.js";
|
|
9
|
+
import { sha256Hex } from "./tree-digest.js";
|
|
10
|
+
export const ROOT_EVENT_PAYLOAD_TYPE = 'application/vnd.mago-ski.root-event+json';
|
|
11
|
+
export const ROOT_EVENT_TYPE = 'mago.root-event/v1';
|
|
12
|
+
export const ROOT_EVENTS_TYPE = 'mago.root-events/v1';
|
|
13
|
+
export const MAX_ROOT_EVENTS = 256;
|
|
14
|
+
export const MAX_ROOT_EVENTS_BYTES = 4 * 1024 * 1024;
|
|
15
|
+
export const MAX_RECOVERY_KEYS = 16;
|
|
16
|
+
/** 16 current + 16 new recovery keys + the new root + the new timestamp key. */
|
|
17
|
+
export const MAX_EVENT_SIGNATURES = 2 * MAX_RECOVERY_KEYS + 2;
|
|
18
|
+
export const MAX_ROOT_EVENT_REASON = 1024;
|
|
19
|
+
const MAX_EVENT_PAYLOAD_BYTES = 64 * 1024;
|
|
20
|
+
const BASE64_PATTERN = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/u;
|
|
21
|
+
/**
|
|
22
|
+
* Reasons are shown in verdicts, terminals, notifications, annotations and Pi block messages the model reads.
|
|
23
|
+
* Besides the C0 controls that requireString refuses, refuse every other control (Cc), format (Cf: bidirectional
|
|
24
|
+
* marks, zero-width characters, soft hyphens, tag characters), surrogate (Cs), private-use (Co) and unassigned (Cn)
|
|
25
|
+
* code point, line and paragraph separators, and variation selectors, so a reason cannot hide, reorder or smuggle
|
|
26
|
+
* text where it is displayed. One stolen recovery key can sign a revoke, so its reason must stay plain text.
|
|
27
|
+
*/
|
|
28
|
+
const UNSAFE_DISPLAY_PATTERN = /[\p{Cc}\p{Cf}\p{Cs}\p{Co}\p{Cn}\p{Zl}\p{Zp}\u{FE00}-\u{FE0F}\u{E0100}-\u{E01EF}\u{180B}-\u{180F}]/u;
|
|
29
|
+
/** True when the text has no control, line-separator, bidirectional or invisible formatting characters. */
|
|
30
|
+
export function isDisplaySafeText(text) {
|
|
31
|
+
return !UNSAFE_DISPLAY_PATTERN.test(text);
|
|
32
|
+
}
|
|
33
|
+
export function requireDisplaySafeText(value, label, maxLength) {
|
|
34
|
+
const text = requireString(value, label, maxLength);
|
|
35
|
+
if (!isDisplaySafeText(text))
|
|
36
|
+
throw new Error(`${label} must not contain control, line-separator, bidirectional or invisible formatting characters`);
|
|
37
|
+
return text;
|
|
38
|
+
}
|
|
39
|
+
function requireSortedUnique(values, label) {
|
|
40
|
+
for (let index = 1; index < values.length; index += 1)
|
|
41
|
+
if (values[index - 1] >= values[index])
|
|
42
|
+
throw new Error(`${label} must be unique and sorted`);
|
|
43
|
+
}
|
|
44
|
+
function decodeBase64(value, label, maxLength) {
|
|
45
|
+
if (typeof value !== 'string' || value.length === 0 || value.length > maxLength || !BASE64_PATTERN.test(value))
|
|
46
|
+
throw new Error(`${label} must be canonical base64`);
|
|
47
|
+
const bytes = Buffer.from(value, 'base64');
|
|
48
|
+
if (bytes.toString('base64') !== value)
|
|
49
|
+
throw new Error(`${label} must be canonical base64`);
|
|
50
|
+
return bytes;
|
|
51
|
+
}
|
|
52
|
+
/** Validates a recovery set as written in a policy or a rotate event (snake_case JSON). */
|
|
53
|
+
export function parseRecoverySet(value, label) {
|
|
54
|
+
const entry = exactKeys(value, ['key_ids', 'rotate_threshold', 'revoke_threshold'], label);
|
|
55
|
+
const keyIds = requireArray(entry.key_ids, `${label} key_ids`, MAX_RECOVERY_KEYS)
|
|
56
|
+
.map((item, index) => validateKeyId(item, `${label} key_ids[${index}]`));
|
|
57
|
+
if (keyIds.length === 0)
|
|
58
|
+
throw new Error(`${label} key_ids must list at least one key`);
|
|
59
|
+
requireSortedUnique(keyIds, `${label} key_ids`);
|
|
60
|
+
const rotateThreshold = requirePositiveInteger(entry.rotate_threshold, `${label} rotate_threshold`);
|
|
61
|
+
const revokeThreshold = requirePositiveInteger(entry.revoke_threshold, `${label} revoke_threshold`);
|
|
62
|
+
if (!(revokeThreshold <= rotateThreshold && rotateThreshold <= keyIds.length))
|
|
63
|
+
throw new Error(`${label} needs 1 <= revoke_threshold <= rotate_threshold <= number of key_ids`);
|
|
64
|
+
return { keyIds, rotateThreshold, revokeThreshold };
|
|
65
|
+
}
|
|
66
|
+
export function recoverySetJson(set) {
|
|
67
|
+
return { key_ids: [...set.keyIds], rotate_threshold: set.rotateThreshold, revoke_threshold: set.revokeThreshold };
|
|
68
|
+
}
|
|
69
|
+
const keyJson = (key) => ({ key_id: key.keyId, public_key: key.spki });
|
|
70
|
+
export function rootEventPayload(event) {
|
|
71
|
+
const payload = {
|
|
72
|
+
type: ROOT_EVENT_TYPE,
|
|
73
|
+
sequence: event.sequence,
|
|
74
|
+
action: event.action,
|
|
75
|
+
root_key_id: event.rootKeyId,
|
|
76
|
+
issued_at: formatTimestamp(event.issuedAt),
|
|
77
|
+
reason: event.reason,
|
|
78
|
+
signers: [...event.signers].sort((a, b) => (a.keyId < b.keyId ? -1 : 1)).map(keyJson),
|
|
79
|
+
};
|
|
80
|
+
if (event.action === 'rotate') {
|
|
81
|
+
if (!event.newRoot || !event.newRecovery)
|
|
82
|
+
throw new Error('a rotate event needs a new root and a new recovery set');
|
|
83
|
+
payload.new_root = keyJson(event.newRoot);
|
|
84
|
+
payload.new_recovery = recoverySetJson(event.newRecovery);
|
|
85
|
+
if (event.newTimestamp)
|
|
86
|
+
payload.new_timestamp = keyJson(event.newTimestamp);
|
|
87
|
+
}
|
|
88
|
+
return payload;
|
|
89
|
+
}
|
|
90
|
+
const EVENT_FIELDS = ['type', 'sequence', 'action', 'root_key_id', 'issued_at', 'reason', 'signers'];
|
|
91
|
+
const ROTATE_FIELDS = [...EVENT_FIELDS, 'new_root', 'new_recovery'];
|
|
92
|
+
function parseKey(value, label) {
|
|
93
|
+
const entry = exactKeys(value, ['key_id', 'public_key'], label);
|
|
94
|
+
return parseSpki(entry.public_key, `${label} public_key`, validateKeyId(entry.key_id, `${label} key_id`));
|
|
95
|
+
}
|
|
96
|
+
/** Reads and checks the shape of a root event payload. Signatures and the rules against host state are checked by the walk. */
|
|
97
|
+
export function parseRootEventPayload(bytes) {
|
|
98
|
+
const label = 'root event';
|
|
99
|
+
const raw = parseCanonicalJsonBytes(bytes, label);
|
|
100
|
+
if (!isRecord(raw))
|
|
101
|
+
throw new Error(`${label} must be a JSON object`);
|
|
102
|
+
const action = raw.action;
|
|
103
|
+
if (action !== 'revoke' && action !== 'rotate')
|
|
104
|
+
throw new Error(`${label} action must be "revoke" or "rotate"`);
|
|
105
|
+
const fields = action === 'revoke' ? EVENT_FIELDS : 'new_timestamp' in raw ? [...ROTATE_FIELDS, 'new_timestamp'] : ROTATE_FIELDS;
|
|
106
|
+
const value = exactKeys(raw, fields, label);
|
|
107
|
+
if (value.type !== ROOT_EVENT_TYPE)
|
|
108
|
+
throw new Error(`${label} has unsupported type`);
|
|
109
|
+
const signers = requireArray(value.signers, `${label} signers`, 2 * MAX_RECOVERY_KEYS)
|
|
110
|
+
.map((item, index) => parseKey(item, `${label} signers[${index}]`));
|
|
111
|
+
if (signers.length === 0)
|
|
112
|
+
throw new Error(`${label} needs at least one signer`);
|
|
113
|
+
requireSortedUnique(signers.map((key) => key.keyId), `${label} signers`);
|
|
114
|
+
const event = {
|
|
115
|
+
sequence: requirePositiveInteger(value.sequence, `${label} sequence`),
|
|
116
|
+
action,
|
|
117
|
+
rootKeyId: validateKeyId(value.root_key_id, `${label} root_key_id`),
|
|
118
|
+
issuedAt: parseTimestamp(value.issued_at, `${label} issued_at`),
|
|
119
|
+
reason: requireDisplaySafeText(value.reason, `${label} reason`, MAX_ROOT_EVENT_REASON),
|
|
120
|
+
signers,
|
|
121
|
+
};
|
|
122
|
+
if (action === 'rotate') {
|
|
123
|
+
event.newRoot = parseKey(value.new_root, `${label} new_root`);
|
|
124
|
+
event.newRecovery = parseRecoverySet(value.new_recovery, `${label} new_recovery`);
|
|
125
|
+
if (value.new_timestamp !== undefined)
|
|
126
|
+
event.newTimestamp = parseKey(value.new_timestamp, `${label} new_timestamp`);
|
|
127
|
+
}
|
|
128
|
+
return event;
|
|
129
|
+
}
|
|
130
|
+
/** Parses a DSSE envelope object with 0..max signatures (a proposal may have none yet). */
|
|
131
|
+
export function parseMultiEnvelope(value, label, options) {
|
|
132
|
+
const entry = exactKeys(value, ['payloadType', 'payload', 'signatures'], label);
|
|
133
|
+
const payloadType = requireString(entry.payloadType, `${label} payloadType`, 256);
|
|
134
|
+
const payloadBytes = decodeBase64(entry.payload, `${label} payload`, Math.ceil(MAX_EVENT_PAYLOAD_BYTES / 3) * 4);
|
|
135
|
+
const items = requireArray(entry.signatures, `${label} signatures`, MAX_EVENT_SIGNATURES);
|
|
136
|
+
if (items.length < options.minSignatures)
|
|
137
|
+
throw new Error(`${label} must carry at least ${options.minSignatures} signature(s)`);
|
|
138
|
+
const signatures = [];
|
|
139
|
+
const sigBytes = [];
|
|
140
|
+
for (const [index, item] of items.entries()) {
|
|
141
|
+
const signature = exactKeys(item, ['keyid', 'sig'], `${label} signatures[${index}]`);
|
|
142
|
+
const keyid = validateKeyId(signature.keyid, `${label} signatures[${index}] keyid`);
|
|
143
|
+
const bytes = decodeBase64(signature.sig, `${label} signatures[${index}] sig`, 128);
|
|
144
|
+
if (bytes.length !== 64)
|
|
145
|
+
throw new Error(`${label} signatures[${index}] must be a 64-byte Ed25519 signature`);
|
|
146
|
+
signatures.push({ keyid, sig: signature.sig });
|
|
147
|
+
sigBytes.push(bytes);
|
|
148
|
+
}
|
|
149
|
+
requireSortedUnique(signatures.map((item) => item.keyid), `${label} signatures`);
|
|
150
|
+
return { payloadType, payload: entry.payload, signatures, payloadBytes, sigBytes };
|
|
151
|
+
}
|
|
152
|
+
/** Keys whose signatures a root event must carry, exactly: its signers, plus the new root and new timestamp key of a rotate. */
|
|
153
|
+
export function requiredSigners(event) {
|
|
154
|
+
const keys = [...event.signers];
|
|
155
|
+
if (event.newRoot)
|
|
156
|
+
keys.push(event.newRoot);
|
|
157
|
+
if (event.newTimestamp)
|
|
158
|
+
keys.push(event.newTimestamp);
|
|
159
|
+
return keys.sort((a, b) => (a.keyId < b.keyId ? -1 : 1));
|
|
160
|
+
}
|
|
161
|
+
/** Adds (or replaces) one signature on an envelope, keeping signatures sorted by keyid. */
|
|
162
|
+
export function addEnvelopeSignature(envelope, keyId, privateKey) {
|
|
163
|
+
const payload = Buffer.from(envelope.payload, 'base64');
|
|
164
|
+
const sig = sign(null, preAuthEncoding(envelope.payloadType, payload), privateKey).toString('base64');
|
|
165
|
+
const signatures = [...envelope.signatures.filter((item) => item.keyid !== keyId), { keyid: keyId, sig }]
|
|
166
|
+
.sort((a, b) => (a.keyid < b.keyid ? -1 : 1));
|
|
167
|
+
return { payloadType: envelope.payloadType, payload: envelope.payload, signatures };
|
|
168
|
+
}
|
|
169
|
+
export function unsignedEventEnvelope(event) {
|
|
170
|
+
return {
|
|
171
|
+
payloadType: ROOT_EVENT_PAYLOAD_TYPE,
|
|
172
|
+
payload: canonicalBytes(rootEventPayload(event)).toString('base64'),
|
|
173
|
+
signatures: [],
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
export function emptyRootEventLog() {
|
|
177
|
+
return canonicalBytes({ events: [], type: ROOT_EVENTS_TYPE });
|
|
178
|
+
}
|
|
179
|
+
export function rootEventLogBytes(envelopes) {
|
|
180
|
+
return canonicalBytes({ events: envelopes, type: ROOT_EVENTS_TYPE });
|
|
181
|
+
}
|
|
182
|
+
/** `sha256:` + the SHA-256 of the envelope as canonical JSON with its trailing LF (the host state's event_digest). */
|
|
183
|
+
export function eventDigest(envelope) {
|
|
184
|
+
return `sha256:${sha256Hex(canonicalBytes({ payload: envelope.payload, payloadType: envelope.payloadType, signatures: envelope.signatures }))}`;
|
|
185
|
+
}
|
|
186
|
+
/** Parses the log file. Each envelope's structure is checked; signatures and rules are checked by the walk. */
|
|
187
|
+
export function parseRootEventLog(bytes) {
|
|
188
|
+
if (bytes.byteLength > MAX_ROOT_EVENTS_BYTES)
|
|
189
|
+
throw new Error(`root event log exceeds the ${MAX_ROOT_EVENTS_BYTES}-byte limit`);
|
|
190
|
+
const value = exactKeys(parseCanonicalJsonBytes(bytes, 'root event log'), ['events', 'type'], 'root event log');
|
|
191
|
+
if (value.type !== ROOT_EVENTS_TYPE)
|
|
192
|
+
throw new Error('root event log has unsupported type');
|
|
193
|
+
return requireArray(value.events, 'root event log events', MAX_ROOT_EVENTS)
|
|
194
|
+
.map((item, index) => parseMultiEnvelope(item, `root event ${index + 1}`, { minSignatures: 1 }));
|
|
195
|
+
}
|
|
196
|
+
function checkRoles(state, label) {
|
|
197
|
+
if (state.recovery.keyIds.includes(state.activeRootKeyId))
|
|
198
|
+
throw new Error(`${label}: the root key must not also be a recovery key`);
|
|
199
|
+
if (state.timestampKeyId !== null) {
|
|
200
|
+
if (state.timestampKeyId === state.activeRootKeyId)
|
|
201
|
+
throw new Error(`${label}: the timestamp key must not also be the root key`);
|
|
202
|
+
if (state.recovery.keyIds.includes(state.timestampKeyId))
|
|
203
|
+
throw new Error(`${label}: the timestamp key must not also be a recovery key`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/** Checks one event against the state just before it, including every signature, and returns the state after it. */
|
|
207
|
+
function applyEvent(state, envelope, sequence, now) {
|
|
208
|
+
const label = `root event ${sequence}`;
|
|
209
|
+
if (envelope.payloadType !== ROOT_EVENT_PAYLOAD_TYPE)
|
|
210
|
+
throw new Error(`${label} has the wrong payload type`);
|
|
211
|
+
let event;
|
|
212
|
+
try {
|
|
213
|
+
event = parseRootEventPayload(envelope.payloadBytes);
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
throw new Error(`${label}: ${error.message}`);
|
|
217
|
+
}
|
|
218
|
+
if (event.sequence !== sequence)
|
|
219
|
+
throw new Error(`${label} carries sequence ${event.sequence}`);
|
|
220
|
+
if (event.rootKeyId !== state.activeRootKeyId)
|
|
221
|
+
throw new Error(`${label} names root key ${event.rootKeyId}, but the active root key is ${state.activeRootKeyId}`);
|
|
222
|
+
if (event.issuedAt.getTime() > now.getTime() + CLOCK_SKEW_MS)
|
|
223
|
+
throw new Error(`${label} is issued in the future (${formatTimestamp(event.issuedAt)})`);
|
|
224
|
+
if (state.lastIssuedAt && event.issuedAt < state.lastIssuedAt)
|
|
225
|
+
throw new Error(`${label} is issued before the event before it`);
|
|
226
|
+
const current = state.recovery;
|
|
227
|
+
const inCurrent = (keyId) => current.keyIds.includes(keyId);
|
|
228
|
+
const signerIds = event.signers.map((key) => key.keyId);
|
|
229
|
+
let next;
|
|
230
|
+
if (event.action === 'revoke') {
|
|
231
|
+
if (state.revoked)
|
|
232
|
+
throw new Error(`${label} revokes root key ${event.rootKeyId}, which root event ${state.revoked.sequence} already revoked`);
|
|
233
|
+
if (!signerIds.every(inCurrent))
|
|
234
|
+
throw new Error(`${label} is signed by a key that is not in the current recovery set`);
|
|
235
|
+
if (signerIds.length < current.revokeThreshold)
|
|
236
|
+
throw new Error(`${label} has ${signerIds.length} recovery signature(s); a revoke needs ${current.revokeThreshold}`);
|
|
237
|
+
const retired = new Set(state.retired);
|
|
238
|
+
retired.add(state.activeRootKeyId);
|
|
239
|
+
next = { ...state, revoked: { sequence, reason: event.reason }, retired, lastIssuedAt: event.issuedAt };
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
const newRoot = event.newRoot;
|
|
243
|
+
const newRecovery = event.newRecovery;
|
|
244
|
+
const newTimestamp = event.newTimestamp;
|
|
245
|
+
const inNew = (keyId) => newRecovery.keyIds.includes(keyId);
|
|
246
|
+
if (inCurrent(newRoot.keyId) || inNew(newRoot.keyId))
|
|
247
|
+
throw new Error(`${label}: the new root key must not be a recovery key`);
|
|
248
|
+
if (newRoot.keyId === event.rootKeyId ? state.revoked !== null : state.retired.has(newRoot.keyId))
|
|
249
|
+
throw new Error(`${label}: the new root key ${newRoot.keyId} has left the root role and cannot return`);
|
|
250
|
+
if (newRoot.keyId === state.timestampKeyId || newRoot.keyId === newTimestamp?.keyId)
|
|
251
|
+
throw new Error(`${label}: the new root key must not also be a timestamp key`);
|
|
252
|
+
for (const keyId of newRecovery.keyIds)
|
|
253
|
+
if (state.retired.has(keyId) || keyId === state.activeRootKeyId || keyId === state.timestampKeyId)
|
|
254
|
+
throw new Error(`${label}: recovery key ${keyId} has held the root or timestamp role`);
|
|
255
|
+
if (newTimestamp) {
|
|
256
|
+
const id = newTimestamp.keyId;
|
|
257
|
+
if (id === event.rootKeyId || id === newRoot.keyId)
|
|
258
|
+
throw new Error(`${label}: the new timestamp key must not be a root key`);
|
|
259
|
+
if (inCurrent(id) || inNew(id))
|
|
260
|
+
throw new Error(`${label}: the new timestamp key must not be a recovery key`);
|
|
261
|
+
if (state.retired.has(id))
|
|
262
|
+
throw new Error(`${label}: the new timestamp key ${id} has left a role and cannot return`);
|
|
263
|
+
}
|
|
264
|
+
if (!signerIds.every((keyId) => inCurrent(keyId) || inNew(keyId)))
|
|
265
|
+
throw new Error(`${label} is signed by a key that is in neither the current nor the new recovery set`);
|
|
266
|
+
const fromCurrent = signerIds.filter(inCurrent).length;
|
|
267
|
+
const fromNew = signerIds.filter(inNew).length;
|
|
268
|
+
if (fromCurrent < current.rotateThreshold)
|
|
269
|
+
throw new Error(`${label} has ${fromCurrent} signature(s) from the current recovery set; a rotate needs ${current.rotateThreshold}`);
|
|
270
|
+
if (fromNew < newRecovery.rotateThreshold)
|
|
271
|
+
throw new Error(`${label} has ${fromNew} signature(s) from the new recovery set; it needs ${newRecovery.rotateThreshold}`);
|
|
272
|
+
const formerRecovery = new Set(state.formerRecovery);
|
|
273
|
+
for (const keyId of current.keyIds)
|
|
274
|
+
if (!inNew(keyId))
|
|
275
|
+
formerRecovery.add(keyId);
|
|
276
|
+
const retired = new Set(state.retired);
|
|
277
|
+
if (newRoot.keyId !== state.activeRootKeyId)
|
|
278
|
+
retired.add(state.activeRootKeyId);
|
|
279
|
+
if (newTimestamp && state.timestampKeyId !== null && newTimestamp.keyId !== state.timestampKeyId)
|
|
280
|
+
retired.add(state.timestampKeyId);
|
|
281
|
+
next = {
|
|
282
|
+
activeRootKeyId: newRoot.keyId,
|
|
283
|
+
recovery: newRecovery,
|
|
284
|
+
timestampKeyId: newTimestamp ? newTimestamp.keyId : state.timestampKeyId,
|
|
285
|
+
revoked: null,
|
|
286
|
+
retired,
|
|
287
|
+
formerRecovery,
|
|
288
|
+
lastIssuedAt: event.issuedAt,
|
|
289
|
+
};
|
|
290
|
+
checkRoles(next, label);
|
|
291
|
+
}
|
|
292
|
+
// Signatures last: exactly the required keys, each valid. An unknown or invalid signature rejects the event.
|
|
293
|
+
const required = requiredSigners(event);
|
|
294
|
+
if (envelope.signatures.length !== required.length || envelope.signatures.some((item, index) => item.keyid !== required[index].keyId))
|
|
295
|
+
throw new Error(`${label} must be signed by exactly ${required.map((key) => key.keyId).join(', ')}`);
|
|
296
|
+
const pae = preAuthEncoding(envelope.payloadType, envelope.payloadBytes);
|
|
297
|
+
for (const [index, key] of required.entries()) {
|
|
298
|
+
let valid = false;
|
|
299
|
+
try {
|
|
300
|
+
valid = verify(null, pae, key.publicKey, envelope.sigBytes[index]);
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
valid = false;
|
|
304
|
+
}
|
|
305
|
+
if (!valid)
|
|
306
|
+
throw new Error(`${label} has an invalid signature from ${key.keyId}`);
|
|
307
|
+
}
|
|
308
|
+
return next;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Walks the root event log from the policy's starting point. Throws (the log is UNVERIFIABLE) if any event after
|
|
312
|
+
* `eventsAfter` breaks a rule; events are never skipped. Hashes every event so rollback and fork checks can use them.
|
|
313
|
+
*/
|
|
314
|
+
export function walkRootEvents(bytes, start, now) {
|
|
315
|
+
requireValidDate(now, 'Verification time');
|
|
316
|
+
checkRoles({ activeRootKeyId: start.rootKeyId, recovery: start.recovery, timestampKeyId: start.timestampKeyId }, 'policy');
|
|
317
|
+
const envelopes = parseRootEventLog(bytes);
|
|
318
|
+
if (envelopes.length < start.eventsAfter)
|
|
319
|
+
throw new Error(`root event log has ${envelopes.length} event(s), fewer than root_events_after (${start.eventsAfter})`);
|
|
320
|
+
let state = {
|
|
321
|
+
activeRootKeyId: start.rootKeyId,
|
|
322
|
+
recovery: start.recovery,
|
|
323
|
+
timestampKeyId: start.timestampKeyId,
|
|
324
|
+
revoked: null,
|
|
325
|
+
retired: new Set(),
|
|
326
|
+
formerRecovery: new Set(),
|
|
327
|
+
lastIssuedAt: null,
|
|
328
|
+
};
|
|
329
|
+
for (let index = start.eventsAfter; index < envelopes.length; index += 1)
|
|
330
|
+
state = applyEvent(state, envelopes[index], index + 1, now);
|
|
331
|
+
return {
|
|
332
|
+
activeRootKeyId: state.activeRootKeyId,
|
|
333
|
+
recovery: { ...state.recovery, keyIds: [...state.recovery.keyIds] },
|
|
334
|
+
timestampKeyId: state.timestampKeyId,
|
|
335
|
+
revoked: state.revoked,
|
|
336
|
+
count: envelopes.length,
|
|
337
|
+
eventDigests: envelopes.map(eventDigest),
|
|
338
|
+
logSha256: sha256Hex(bytes),
|
|
339
|
+
retired: [...state.retired].sort(),
|
|
340
|
+
formerRecovery: [...state.formerRecovery].sort(),
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
const WALK_CACHE_LIMIT = 32;
|
|
344
|
+
const walkCache = new Map();
|
|
345
|
+
/**
|
|
346
|
+
* walkRootEvents with a cache of successful walks, keyed by the starting point and the log's SHA-256. A walk that
|
|
347
|
+
* succeeded stays valid as time passes (only "issued in the future" depends on the clock, and that only relaxes),
|
|
348
|
+
* so an unchanged log costs one hash. Failed walks are not cached.
|
|
349
|
+
*/
|
|
350
|
+
export function walkRootEventsCached(bytes, start, now) {
|
|
351
|
+
const key = JSON.stringify([start.rootKeyId, recoverySetJson(start.recovery), start.timestampKeyId, start.eventsAfter, sha256Hex(bytes)]);
|
|
352
|
+
const hit = walkCache.get(key);
|
|
353
|
+
if (hit) {
|
|
354
|
+
requireValidDate(now, 'Verification time');
|
|
355
|
+
return cloneWalk(hit);
|
|
356
|
+
}
|
|
357
|
+
const walk = walkRootEvents(bytes, start, now);
|
|
358
|
+
if (walkCache.size >= WALK_CACHE_LIMIT)
|
|
359
|
+
walkCache.delete(walkCache.keys().next().value);
|
|
360
|
+
walkCache.set(key, cloneWalk(walk));
|
|
361
|
+
return walk;
|
|
362
|
+
}
|
|
363
|
+
function cloneWalk(walk) {
|
|
364
|
+
return {
|
|
365
|
+
...walk,
|
|
366
|
+
recovery: { ...walk.recovery, keyIds: [...walk.recovery.keyIds] },
|
|
367
|
+
revoked: walk.revoked ? { ...walk.revoked } : null,
|
|
368
|
+
eventDigests: [...walk.eventDigests],
|
|
369
|
+
retired: [...walk.retired],
|
|
370
|
+
formerRecovery: [...walk.formerRecovery],
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/** Tests only. */
|
|
374
|
+
export function clearRootWalkCache() {
|
|
375
|
+
walkCache.clear();
|
|
376
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { KeyObject } from 'node:crypto';
|
|
2
|
+
import { type PublicKeyInfo } from './keys.ts';
|
|
3
|
+
export declare const TIMESTAMP_PAYLOAD_TYPE = "application/vnd.mago-ski.timestamp+json";
|
|
4
|
+
export declare const TIMESTAMP_TYPE = "mago.timestamp/v1";
|
|
5
|
+
export declare const MAX_TIMESTAMP_BYTES: number;
|
|
6
|
+
export declare const DEFAULT_MAX_TIMESTAMP_LIFETIME = "24h";
|
|
7
|
+
export declare const MAX_TIMESTAMP_LIFETIME_MS: number;
|
|
8
|
+
/** The timestamp is older than one this host has already seen; callers match on the class, not the message. */
|
|
9
|
+
export declare class TimestampRollbackError extends Error {
|
|
10
|
+
}
|
|
11
|
+
/** The timestamp names other document digests than the files the host read. */
|
|
12
|
+
export declare class TimestampBindingError extends Error {
|
|
13
|
+
}
|
|
14
|
+
export interface Timestamp {
|
|
15
|
+
version: number;
|
|
16
|
+
issuedAt: Date;
|
|
17
|
+
expiresAt: Date;
|
|
18
|
+
key: PublicKeyInfo;
|
|
19
|
+
rootEvents: {
|
|
20
|
+
sha256: string;
|
|
21
|
+
count: number;
|
|
22
|
+
};
|
|
23
|
+
trustRoot: {
|
|
24
|
+
sha256: string;
|
|
25
|
+
version: number;
|
|
26
|
+
};
|
|
27
|
+
revocations: {
|
|
28
|
+
sha256: string;
|
|
29
|
+
version: number;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** What a host read, to compare against the timestamp. */
|
|
33
|
+
export interface FreshnessInputs {
|
|
34
|
+
rootEvents: {
|
|
35
|
+
sha256: string;
|
|
36
|
+
count: number;
|
|
37
|
+
};
|
|
38
|
+
trustRoot: {
|
|
39
|
+
sha256: string;
|
|
40
|
+
};
|
|
41
|
+
revocations: {
|
|
42
|
+
sha256: string;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export declare function timestampPayload(timestamp: Timestamp): Record<string, unknown>;
|
|
46
|
+
export declare function signTimestamp(timestamp: Timestamp, privateKey: KeyObject): Buffer;
|
|
47
|
+
export declare function parseTimestampPayload(bytes: Uint8Array): Timestamp;
|
|
48
|
+
/** Reads a timestamp signed by `keyId` without the time checks (for the signing job, to find the next version). */
|
|
49
|
+
export declare function readTimestampForUpdate(bytes: Uint8Array, keyId: string): Timestamp;
|
|
50
|
+
/**
|
|
51
|
+
* Verifies a timestamp against the active timestamp key, the clock, the policy's lifetime cap and the lowest version
|
|
52
|
+
* the host accepts, then checks that it names exactly the files the host read. Throws with the reason on any failure.
|
|
53
|
+
* The trust root and revocation list versions are compared by the caller once those documents are parsed.
|
|
54
|
+
*/
|
|
55
|
+
export declare function verifyTimestamp(bytes: Uint8Array, options: {
|
|
56
|
+
keyId: string;
|
|
57
|
+
now: Date;
|
|
58
|
+
maxLifetimeMs: number;
|
|
59
|
+
minimumVersion?: number | undefined;
|
|
60
|
+
inputs: FreshnessInputs;
|
|
61
|
+
}): Timestamp;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { canonicalBytes, exactKeys, parseCanonicalJsonBytes, requirePositiveInteger } from "./canonical-json.js";
|
|
2
|
+
import { envelopeBytes, parseEnvelope, signEnvelope, verifyEnvelopeSignature } from "./dsse.js";
|
|
3
|
+
import { parseSpki, validateKeyId } from "./keys.js";
|
|
4
|
+
import { CLOCK_SKEW_MS, formatTimestamp, parseTimestamp, requireValidDate } from "./time.js";
|
|
5
|
+
export const TIMESTAMP_PAYLOAD_TYPE = 'application/vnd.mago-ski.timestamp+json';
|
|
6
|
+
export const TIMESTAMP_TYPE = 'mago.timestamp/v1';
|
|
7
|
+
export const MAX_TIMESTAMP_BYTES = 64 * 1024;
|
|
8
|
+
export const DEFAULT_MAX_TIMESTAMP_LIFETIME = '24h';
|
|
9
|
+
export const MAX_TIMESTAMP_LIFETIME_MS = 7 * 24 * 60 * 60 * 1000;
|
|
10
|
+
const SHA256_HEX = /^[0-9a-f]{64}$/u;
|
|
11
|
+
/** The timestamp is older than one this host has already seen; callers match on the class, not the message. */
|
|
12
|
+
export class TimestampRollbackError extends Error {
|
|
13
|
+
}
|
|
14
|
+
/** The timestamp names other document digests than the files the host read. */
|
|
15
|
+
export class TimestampBindingError extends Error {
|
|
16
|
+
}
|
|
17
|
+
function requireSha256(value, label) {
|
|
18
|
+
if (typeof value !== 'string' || !SHA256_HEX.test(value))
|
|
19
|
+
throw new Error(`${label} must be 64 lowercase hex characters`);
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
function requireCount(value, label) {
|
|
23
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0)
|
|
24
|
+
throw new Error(`${label} must be a non-negative integer`);
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
export function timestampPayload(timestamp) {
|
|
28
|
+
return {
|
|
29
|
+
type: TIMESTAMP_TYPE,
|
|
30
|
+
version: timestamp.version,
|
|
31
|
+
issued_at: formatTimestamp(timestamp.issuedAt),
|
|
32
|
+
expires_at: formatTimestamp(timestamp.expiresAt),
|
|
33
|
+
timestamp_key: { key_id: timestamp.key.keyId, public_key: timestamp.key.spki },
|
|
34
|
+
root_events: { sha256: timestamp.rootEvents.sha256, count: timestamp.rootEvents.count },
|
|
35
|
+
trust_root: { sha256: timestamp.trustRoot.sha256, version: timestamp.trustRoot.version },
|
|
36
|
+
revocations: { sha256: timestamp.revocations.sha256, version: timestamp.revocations.version },
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function signTimestamp(timestamp, privateKey) {
|
|
40
|
+
return envelopeBytes(signEnvelope(TIMESTAMP_PAYLOAD_TYPE, canonicalBytes(timestampPayload(timestamp)), timestamp.key.keyId, privateKey));
|
|
41
|
+
}
|
|
42
|
+
export function parseTimestampPayload(bytes) {
|
|
43
|
+
const label = 'timestamp';
|
|
44
|
+
const value = exactKeys(parseCanonicalJsonBytes(bytes, label), ['type', 'version', 'issued_at', 'expires_at', 'timestamp_key', 'root_events', 'trust_root', 'revocations'], label);
|
|
45
|
+
if (value.type !== TIMESTAMP_TYPE)
|
|
46
|
+
throw new Error(`${label} has unsupported type`);
|
|
47
|
+
const keyEntry = exactKeys(value.timestamp_key, ['key_id', 'public_key'], `${label} timestamp_key`);
|
|
48
|
+
const key = parseSpki(keyEntry.public_key, `${label} timestamp_key public_key`, validateKeyId(keyEntry.key_id, `${label} timestamp_key key_id`));
|
|
49
|
+
const events = exactKeys(value.root_events, ['sha256', 'count'], `${label} root_events`);
|
|
50
|
+
const trustRoot = exactKeys(value.trust_root, ['sha256', 'version'], `${label} trust_root`);
|
|
51
|
+
const revocations = exactKeys(value.revocations, ['sha256', 'version'], `${label} revocations`);
|
|
52
|
+
const issuedAt = parseTimestamp(value.issued_at, `${label} issued_at`);
|
|
53
|
+
const expiresAt = parseTimestamp(value.expires_at, `${label} expires_at`);
|
|
54
|
+
if (expiresAt <= issuedAt)
|
|
55
|
+
throw new Error(`${label} expires before it is issued`);
|
|
56
|
+
return {
|
|
57
|
+
version: requirePositiveInteger(value.version, `${label} version`),
|
|
58
|
+
issuedAt,
|
|
59
|
+
expiresAt,
|
|
60
|
+
key,
|
|
61
|
+
rootEvents: { sha256: requireSha256(events.sha256, `${label} root_events sha256`), count: requireCount(events.count, `${label} root_events count`) },
|
|
62
|
+
trustRoot: { sha256: requireSha256(trustRoot.sha256, `${label} trust_root sha256`), version: requirePositiveInteger(trustRoot.version, `${label} trust_root version`) },
|
|
63
|
+
revocations: { sha256: requireSha256(revocations.sha256, `${label} revocations sha256`), version: requirePositiveInteger(revocations.version, `${label} revocations version`) },
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** Reads a timestamp signed by `keyId` without the time checks (for the signing job, to find the next version). */
|
|
67
|
+
export function readTimestampForUpdate(bytes, keyId) {
|
|
68
|
+
if (bytes.byteLength > MAX_TIMESTAMP_BYTES)
|
|
69
|
+
throw new Error(`timestamp exceeds the ${MAX_TIMESTAMP_BYTES}-byte limit`);
|
|
70
|
+
const envelope = parseEnvelope(bytes, 'timestamp');
|
|
71
|
+
if (envelope.payloadType !== TIMESTAMP_PAYLOAD_TYPE)
|
|
72
|
+
throw new Error('timestamp has the wrong payload type');
|
|
73
|
+
if (envelope.signatures[0].keyid !== keyId)
|
|
74
|
+
throw new Error(`timestamp is not signed by the active timestamp key ${keyId}`);
|
|
75
|
+
const timestamp = parseTimestampPayload(envelope.payloadBytes);
|
|
76
|
+
if (timestamp.key.keyId !== keyId)
|
|
77
|
+
throw new Error('timestamp names a different timestamp key than the one that signed it');
|
|
78
|
+
if (!verifyEnvelopeSignature(envelope, timestamp.key.publicKey))
|
|
79
|
+
throw new Error('timestamp signature is invalid');
|
|
80
|
+
return timestamp;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Verifies a timestamp against the active timestamp key, the clock, the policy's lifetime cap and the lowest version
|
|
84
|
+
* the host accepts, then checks that it names exactly the files the host read. Throws with the reason on any failure.
|
|
85
|
+
* The trust root and revocation list versions are compared by the caller once those documents are parsed.
|
|
86
|
+
*/
|
|
87
|
+
export function verifyTimestamp(bytes, options) {
|
|
88
|
+
requireValidDate(options.now, 'Verification time');
|
|
89
|
+
const timestamp = readTimestampForUpdate(bytes, options.keyId);
|
|
90
|
+
const now = options.now.getTime();
|
|
91
|
+
if (timestamp.issuedAt.getTime() > now + CLOCK_SKEW_MS)
|
|
92
|
+
throw new Error(`timestamp is issued in the future (${formatTimestamp(timestamp.issuedAt)})`);
|
|
93
|
+
if (timestamp.expiresAt.getTime() - timestamp.issuedAt.getTime() > options.maxLifetimeMs)
|
|
94
|
+
throw new Error('timestamp lifetime is longer than the policy\'s freshness max_lifetime');
|
|
95
|
+
if (timestamp.expiresAt.getTime() <= now)
|
|
96
|
+
throw new Error(`timestamp expired at ${formatTimestamp(timestamp.expiresAt)}; the timestamp job may have stopped`);
|
|
97
|
+
if (options.minimumVersion !== undefined && timestamp.version < options.minimumVersion)
|
|
98
|
+
throw new TimestampRollbackError(`timestamp version ${timestamp.version} is older than version ${options.minimumVersion} already seen (rollback)`);
|
|
99
|
+
const { inputs } = options;
|
|
100
|
+
if (timestamp.rootEvents.sha256 !== inputs.rootEvents.sha256 || timestamp.rootEvents.count !== inputs.rootEvents.count)
|
|
101
|
+
throw new TimestampBindingError('the root event log does not match the timestamp');
|
|
102
|
+
if (timestamp.trustRoot.sha256 !== inputs.trustRoot.sha256)
|
|
103
|
+
throw new TimestampBindingError('the trust root does not match the timestamp');
|
|
104
|
+
if (timestamp.revocations.sha256 !== inputs.revocations.sha256)
|
|
105
|
+
throw new TimestampBindingError('the revocation list does not match the timestamp');
|
|
106
|
+
return timestamp;
|
|
107
|
+
}
|
|
@@ -24,6 +24,7 @@ export interface SkillTree {
|
|
|
24
24
|
/** Paths left out of the digest: the root `.git`, the in-tree certificate, and `.skilldigestignore` entries. */
|
|
25
25
|
excluded: string[];
|
|
26
26
|
}
|
|
27
|
+
export declare function decodeEntryName(name: Buffer): string;
|
|
27
28
|
/** Executable bit of a regular file. Windows has no POSIX mode bits, so it always reports false there. */
|
|
28
29
|
export declare function isExecutable(mode: number | bigint): boolean;
|
|
29
30
|
export declare function sha256Hex(bytes: Uint8Array): string;
|
package/dist/src/tree-digest.js
CHANGED
package/dist/src/verify.d.ts
CHANGED
|
@@ -4,9 +4,34 @@ import { type SkillTree, type TreeFile } from './tree-digest.ts';
|
|
|
4
4
|
import { type TrustRoot } from './trust-root.ts';
|
|
5
5
|
export declare const VERDICTS: readonly ["VERIFIED", "UNAPPROVED_CHANGE", "NO_CERTIFICATE", "UNTRUSTED_SIGNER", "REVOKED", "EXPIRED", "UNVERIFIABLE"];
|
|
6
6
|
export type Verdict = (typeof VERDICTS)[number];
|
|
7
|
+
/**
|
|
8
|
+
* Precedence rank for candidate selection (lower rank wins):
|
|
9
|
+
* VERIFIED (0) beats all failures, followed by the failure priority order.
|
|
10
|
+
*/
|
|
11
|
+
export declare const VERDICT_RANK: Readonly<Record<Verdict, number>>;
|
|
12
|
+
/** Root recovery state behind a trust context (policy v2 with "recovery"). */
|
|
13
|
+
export interface RootStatus {
|
|
14
|
+
/** root_fingerprint in the policy. */
|
|
15
|
+
pinnedRootKeyId: string;
|
|
16
|
+
/** The root key that signed the trust root and revocation list after walking the root event log. */
|
|
17
|
+
activeRootKeyId: string;
|
|
18
|
+
/** Events in the root event log. */
|
|
19
|
+
sequence: number;
|
|
20
|
+
recovery: {
|
|
21
|
+
keyIds: string[];
|
|
22
|
+
rotateThreshold: number;
|
|
23
|
+
revokeThreshold: number;
|
|
24
|
+
};
|
|
25
|
+
timestampKeyId: string | null;
|
|
26
|
+
timestamp: {
|
|
27
|
+
version: number;
|
|
28
|
+
expiresAt: Date;
|
|
29
|
+
} | null;
|
|
30
|
+
}
|
|
7
31
|
export interface TrustContext {
|
|
8
32
|
trustRoot: TrustRoot;
|
|
9
33
|
revocations: Revocations;
|
|
34
|
+
root?: RootStatus;
|
|
10
35
|
}
|
|
11
36
|
export interface MinimumVersions {
|
|
12
37
|
trustRootVersion: number;
|
|
@@ -19,6 +44,14 @@ export type TrustContextResult = {
|
|
|
19
44
|
ok: false;
|
|
20
45
|
verdict: 'EXPIRED' | 'UNVERIFIABLE';
|
|
21
46
|
reason: string;
|
|
47
|
+
/** The active root key is revoked by a root event: every Skill is blocked, in shadow mode too. */
|
|
48
|
+
rootRevoked?: boolean;
|
|
49
|
+
/** A version, sequence or timestamp is lower than the host state's: the documents are older than already seen. */
|
|
50
|
+
rollback?: true;
|
|
51
|
+
/** The host state file is malformed (`corrupt`), could not be read (`unreadable`) or could not be written (`write`). */
|
|
52
|
+
stateError?: 'corrupt' | 'unreadable' | 'write';
|
|
53
|
+
/** The timestamp is valid but names other document digests or versions than the files read (the binding check). */
|
|
54
|
+
bindingMismatch?: true;
|
|
22
55
|
};
|
|
23
56
|
export interface CertificateEvaluation {
|
|
24
57
|
verdict: Verdict;
|
package/dist/src/verify.js
CHANGED
|
@@ -20,7 +20,7 @@ export const VERDICTS = [
|
|
|
20
20
|
* Precedence rank for candidate selection (lower rank wins):
|
|
21
21
|
* VERIFIED (0) beats all failures, followed by the failure priority order.
|
|
22
22
|
*/
|
|
23
|
-
const VERDICT_RANK = {
|
|
23
|
+
export const VERDICT_RANK = {
|
|
24
24
|
VERIFIED: 0,
|
|
25
25
|
REVOKED: 1,
|
|
26
26
|
UNAPPROVED_CHANGE: 2,
|
|
@@ -54,6 +54,7 @@ export function loadTrustContext(options) {
|
|
|
54
54
|
ok: false,
|
|
55
55
|
verdict: 'UNVERIFIABLE',
|
|
56
56
|
reason: `trust root version ${trustRoot.version} is older than version ${options.minimum.trustRootVersion} already seen (rollback)`,
|
|
57
|
+
rollback: true,
|
|
57
58
|
};
|
|
58
59
|
}
|
|
59
60
|
if (options.minimum && revocations.version < options.minimum.revocationsVersion) {
|
|
@@ -61,6 +62,7 @@ export function loadTrustContext(options) {
|
|
|
61
62
|
ok: false,
|
|
62
63
|
verdict: 'UNVERIFIABLE',
|
|
63
64
|
reason: `revocation list version ${revocations.version} is older than version ${options.minimum.revocationsVersion} already seen (rollback)`,
|
|
65
|
+
rollback: true,
|
|
64
66
|
};
|
|
65
67
|
}
|
|
66
68
|
return { ok: true, context: { trustRoot, revocations } };
|
|
@@ -363,7 +365,7 @@ export async function verifySkill(options) {
|
|
|
363
365
|
}
|
|
364
366
|
export function unverifiable(skillDir, skillName, reason, now, verdict = 'UNVERIFIABLE') {
|
|
365
367
|
requireValidDate(now, 'Verification time');
|
|
366
|
-
const safeVerdict = verdict === 'VERIFIED' || !(verdict
|
|
368
|
+
const safeVerdict = verdict === 'VERIFIED' || !Object.hasOwn(VERDICT_RANK, verdict) ? 'UNVERIFIABLE' : verdict;
|
|
367
369
|
return {
|
|
368
370
|
verdict: safeVerdict,
|
|
369
371
|
reason,
|