@charter-agreement-protocol/verifier 0.6.0 → 0.7.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/CHANGELOG.md +102 -1
- package/README.md +60 -6
- package/SECURITY.md +89 -0
- package/conformance/cases/acceptance-equivocation.json +1 -1
- package/conformance/cases/acceptance-verify.json +1 -1
- package/conformance/cases/base64url-decode.json +1 -1
- package/conformance/cases/canonicalization-encode.json +1 -1
- package/conformance/cases/chain-verify.json +1 -1
- package/conformance/cases/chain-verify_profile.json +1 -1
- package/conformance/cases/charter_revision-decode.json +1 -1
- package/conformance/cases/descriptor_chain-verify.json +1 -1
- package/conformance/cases/digest-hash.json +1 -1
- package/conformance/cases/governing_revision.json +1 -1
- package/conformance/cases/json-decode.json +1 -1
- package/conformance/cases/party_descriptor-verify.json +1 -1
- package/conformance/cases/receipt-verify.json +1 -1
- package/conformance/cases/schema-validate.json +1 -1
- package/conformance/cases/signing-prepare.json +1 -0
- package/conformance/cases/termination-verify.json +1 -1
- package/conformance/index.json +1 -1
- package/dist/cli.js +1 -1
- package/dist/core.d.ts +37 -2
- package/dist/core.js +1014 -229
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +7 -4
package/dist/core.js
CHANGED
|
@@ -6,7 +6,7 @@ const CASE_FORMAT = "charter-agreement-protocol-conformance-cases";
|
|
|
6
6
|
const REPORT_FORMAT = "charter-agreement-protocol-conformance-report";
|
|
7
7
|
const MAXIMUM_CORPUS_FILES = 64;
|
|
8
8
|
const MAXIMUM_CORPUS_BYTES = 33_554_432;
|
|
9
|
-
export const CERTIFIED_INDEX_SHA256_BASE64URL = "
|
|
9
|
+
export const CERTIFIED_INDEX_SHA256_BASE64URL = "xiyty9a_VZTgiIYIKqGZQIAhJB-OQZRIcOX_zyNV-VQ";
|
|
10
10
|
export const CERTIFIED_REGISTRY_DIGEST = "sha-256:u754joyHGcLCTm1LYV2s6eHauUUdDfJDwwyhbAbxvzc";
|
|
11
11
|
// The fourth certified identity — the specification digest over the spec
|
|
12
12
|
// set — is pinned in priv/release-metadata.json and enforced by the
|
|
@@ -50,6 +50,7 @@ const SURFACES = [
|
|
|
50
50
|
"schema.validate", "party_descriptor.verify", "descriptor_chain.verify",
|
|
51
51
|
"charter_revision.decode", "acceptance.verify", "acceptance.equivocation",
|
|
52
52
|
"termination.verify", "chain.verify", "chain.verify_profile", "governing_revision", "receipt.verify",
|
|
53
|
+
"signing.prepare",
|
|
53
54
|
];
|
|
54
55
|
const CLASSES = [
|
|
55
56
|
"valid", "boundary_near", "exact_bound", "maximum_plus_one", "invalid_encoding",
|
|
@@ -61,21 +62,22 @@ const CLASSES = [
|
|
|
61
62
|
"extension_unknown_critical", "extension_optional_roundtrip", "extension_invalid",
|
|
62
63
|
];
|
|
63
64
|
const REQUIRED = {
|
|
64
|
-
"acceptance.equivocation": ["equivocation", "invalid_constraint"],
|
|
65
|
-
"acceptance.verify": ["valid", "invalid_constraint", "signature_invalid", "unknown_member", "profile_narrow_valid", "profile_algorithm_outside", "profile_revision_outside"],
|
|
66
|
-
"base64url.decode": ["valid", "exact_bound", "invalid_encoding"],
|
|
65
|
+
"acceptance.equivocation": ["equivocation", "invalid_constraint", "invalid_type"],
|
|
66
|
+
"acceptance.verify": ["valid", "invalid_constraint", "signature_invalid", "unknown_member", "profile_narrow_valid", "profile_algorithm_outside", "profile_revision_outside", "invalid_type"],
|
|
67
|
+
"base64url.decode": ["valid", "exact_bound", "invalid_encoding", "invalid_type"],
|
|
67
68
|
"canonicalization.encode": ["valid", "invalid_encoding", "invalid_type", "non_canonical_bytes"],
|
|
68
|
-
"chain.verify": ["valid", "exact_bound", "maximum_plus_one", "chain_fork", "supersession", "chain_invalid"],
|
|
69
|
-
"chain.verify_profile": ["profile_algorithm_outside", "profile_revision_outside", "profile_narrow_valid"],
|
|
69
|
+
"chain.verify": ["valid", "exact_bound", "maximum_plus_one", "chain_fork", "supersession", "chain_invalid", "invalid_type"],
|
|
70
|
+
"chain.verify_profile": ["profile_algorithm_outside", "profile_revision_outside", "profile_narrow_valid", "invalid_type"],
|
|
70
71
|
"charter_revision.decode": ["valid", "invalid_encoding", "invalid_type", "invalid_constraint", "invalid_cardinality", "unknown_member", "missing_required", "non_canonical_bytes", "extension_unknown_critical", "extension_invalid"],
|
|
71
|
-
"descriptor_chain.verify": ["signature_invalid", "chain_invalid", "descriptor_superseded", "descriptor_fork"],
|
|
72
|
+
"descriptor_chain.verify": ["signature_invalid", "chain_invalid", "descriptor_superseded", "descriptor_fork", "invalid_type"],
|
|
72
73
|
"digest.hash": ["valid", "invalid_type", "digest_mismatch"],
|
|
73
|
-
"governing_revision": ["precedence_selection", "terminated"],
|
|
74
|
+
"governing_revision": ["precedence_selection", "terminated", "invalid_type"],
|
|
74
75
|
"json.decode": ["valid", "boundary_near", "exact_bound", "maximum_plus_one", "invalid_encoding", "invalid_type"],
|
|
75
|
-
"party_descriptor.verify": ["valid", "signature_invalid", "invalid_encoding", "unknown_member", "invalid_constraint", "profile_narrow_valid", "profile_algorithm_outside", "profile_revision_outside"],
|
|
76
|
-
"receipt.verify": ["valid", "invalid_constraint", "signature_invalid", "unknown_member", "chain_fork", "terminated", "outcome_indeterminate", "extension_optional_roundtrip", "invalid_encoding", "extension_invalid", "profile_narrow_valid", "profile_algorithm_outside", "profile_revision_outside"],
|
|
76
|
+
"party_descriptor.verify": ["valid", "signature_invalid", "invalid_encoding", "unknown_member", "invalid_constraint", "profile_narrow_valid", "profile_algorithm_outside", "profile_revision_outside", "invalid_type"],
|
|
77
|
+
"receipt.verify": ["valid", "maximum_plus_one", "invalid_constraint", "signature_invalid", "unknown_member", "chain_fork", "terminated", "outcome_indeterminate", "extension_optional_roundtrip", "invalid_encoding", "extension_invalid", "profile_narrow_valid", "profile_algorithm_outside", "profile_revision_outside", "invalid_type"],
|
|
77
78
|
"schema.validate": ["valid", "invalid_type", "invalid_constraint", "invalid_cardinality", "unknown_member", "missing_required", "maximum_plus_one"],
|
|
78
|
-
"
|
|
79
|
+
"signing.prepare": ["valid", "exact_bound", "maximum_plus_one", "invalid_type", "invalid_constraint", "chain_invalid", "profile_algorithm_outside", "profile_revision_outside"],
|
|
80
|
+
"termination.verify": ["valid", "invalid_constraint", "signature_invalid", "unknown_member", "profile_narrow_valid", "profile_algorithm_outside", "profile_revision_outside", "invalid_type"],
|
|
79
81
|
};
|
|
80
82
|
const ok = (value) => ({ ok: true, value });
|
|
81
83
|
const fail = (code) => ({ ok: false, code });
|
|
@@ -145,24 +147,26 @@ const NUMBER_TOKEN = /(?:-?(?:0|[1-9][0-9]*))(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/y
|
|
|
145
147
|
// Retain text order separately from JavaScript's integer-key enumeration.
|
|
146
148
|
// Plain property access remains compatible with existing artifact consumers.
|
|
147
149
|
const JSON_MEMBER_ORDER = new WeakMap();
|
|
148
|
-
function readJsonValue(text, index, limits, depth = 0) {
|
|
150
|
+
function readJsonValue(text, index, limits, depth = 0, bytes) {
|
|
149
151
|
index = skipWhitespace(text, index);
|
|
150
152
|
const char = text[index];
|
|
151
153
|
if (char === undefined)
|
|
152
154
|
return { status: "need-character" };
|
|
153
|
-
if ((char === "{" || char === "[") && depth + 1 > limits.max_depth)
|
|
154
|
-
return { status: "error", code: "limit_exceeded" };
|
|
155
155
|
if (char === "{") {
|
|
156
156
|
const members = new Map();
|
|
157
157
|
let duplicate = false;
|
|
158
158
|
let count = 0;
|
|
159
159
|
let cursor = skipWhitespace(text, index + 1);
|
|
160
|
+
if (text[cursor] !== "}" && text[cursor] !== '"')
|
|
161
|
+
return { status: "syntax" };
|
|
162
|
+
if (depth + 1 > limits.max_depth)
|
|
163
|
+
return { status: "error", code: "limit_exceeded" };
|
|
160
164
|
if (text[cursor] === "}")
|
|
161
165
|
return { status: "ok", value: {}, index: cursor + 1 };
|
|
162
166
|
for (;;) {
|
|
163
167
|
if (text[cursor] !== '"')
|
|
164
168
|
return { status: "syntax" };
|
|
165
|
-
const key = readJsonString(text, cursor);
|
|
169
|
+
const key = readJsonString(text, cursor, bytes);
|
|
166
170
|
if (key.status !== "ok")
|
|
167
171
|
return key;
|
|
168
172
|
if (Buffer.byteLength(key.value) > limits.max_string_bytes)
|
|
@@ -170,7 +174,7 @@ function readJsonValue(text, index, limits, depth = 0) {
|
|
|
170
174
|
cursor = skipWhitespace(text, key.index);
|
|
171
175
|
if (text[cursor] !== ":")
|
|
172
176
|
return { status: "syntax" };
|
|
173
|
-
const item = readJsonValue(text, skipWhitespace(text, cursor + 1), limits, depth + 1);
|
|
177
|
+
const item = readJsonValue(text, skipWhitespace(text, cursor + 1), limits, depth + 1, bytes);
|
|
174
178
|
if (item.status !== "ok")
|
|
175
179
|
return item;
|
|
176
180
|
cursor = skipWhitespace(text, item.index);
|
|
@@ -199,10 +203,14 @@ function readJsonValue(text, index, limits, depth = 0) {
|
|
|
199
203
|
if (char === "[") {
|
|
200
204
|
const items = [];
|
|
201
205
|
let cursor = skipWhitespace(text, index + 1);
|
|
206
|
+
if (text[cursor] === undefined)
|
|
207
|
+
return { status: "syntax" };
|
|
208
|
+
if (depth + 1 > limits.max_depth)
|
|
209
|
+
return { status: "error", code: "limit_exceeded" };
|
|
202
210
|
if (text[cursor] === "]")
|
|
203
211
|
return { status: "ok", value: items, index: cursor + 1 };
|
|
204
212
|
for (;;) {
|
|
205
|
-
const item = readJsonValue(text, cursor, limits, depth + 1);
|
|
213
|
+
const item = readJsonValue(text, cursor, limits, depth + 1, bytes);
|
|
206
214
|
if (item.status !== "ok")
|
|
207
215
|
return item;
|
|
208
216
|
cursor = skipWhitespace(text, item.index);
|
|
@@ -223,7 +231,7 @@ function readJsonValue(text, index, limits, depth = 0) {
|
|
|
223
231
|
}
|
|
224
232
|
}
|
|
225
233
|
if (char === '"') {
|
|
226
|
-
const string = readJsonString(text, index);
|
|
234
|
+
const string = readJsonString(text, index, bytes);
|
|
227
235
|
if (string.status === "ok" && Buffer.byteLength(string.value) > limits.max_string_bytes)
|
|
228
236
|
return { status: "error", code: "limit_exceeded" };
|
|
229
237
|
return string.status === "ok" ? { status: "ok", value: string.value, index: string.index } : string;
|
|
@@ -267,7 +275,7 @@ function readJsonValue(text, index, limits, depth = 0) {
|
|
|
267
275
|
}
|
|
268
276
|
return { status: "syntax" };
|
|
269
277
|
}
|
|
270
|
-
function readJsonString(text, index) {
|
|
278
|
+
function readJsonString(text, index, bytes) {
|
|
271
279
|
if (text[index] !== '"')
|
|
272
280
|
return { status: "syntax" };
|
|
273
281
|
let end = index + 1;
|
|
@@ -283,7 +291,16 @@ function readJsonString(text, index) {
|
|
|
283
291
|
if (text[end] !== '"')
|
|
284
292
|
return { status: "syntax" };
|
|
285
293
|
try {
|
|
286
|
-
|
|
294
|
+
let literal = text.slice(index, end + 1);
|
|
295
|
+
if (bytes) {
|
|
296
|
+
try {
|
|
297
|
+
literal = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes.subarray(index, end + 1));
|
|
298
|
+
}
|
|
299
|
+
catch (_error) {
|
|
300
|
+
return { status: "error", code: "invalid_encoding" };
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const value = JSON.parse(literal);
|
|
287
304
|
if (/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/.test(value)) {
|
|
288
305
|
return { status: "syntax" };
|
|
289
306
|
}
|
|
@@ -306,6 +323,12 @@ function skipWhitespace(text, index) {
|
|
|
306
323
|
return index;
|
|
307
324
|
}
|
|
308
325
|
export function decodeJsonText(text, selected = {}) {
|
|
326
|
+
try {
|
|
327
|
+
selected = readOptionMembers(selected, Object.keys(DEFAULT_LIMITS));
|
|
328
|
+
}
|
|
329
|
+
catch (_error) {
|
|
330
|
+
return fail("invalid_type");
|
|
331
|
+
}
|
|
309
332
|
if (!validLimits(selected))
|
|
310
333
|
return fail("invalid_limits");
|
|
311
334
|
if (typeof text !== "string")
|
|
@@ -328,7 +351,22 @@ function decodeJsonBytes(bytes, limits = DEFAULT_LIMITS) {
|
|
|
328
351
|
try {
|
|
329
352
|
if (bytes.length > limits.max_bytes)
|
|
330
353
|
return fail("limit_exceeded");
|
|
331
|
-
|
|
354
|
+
// Byte offsets stay intact while syntax and container callbacks run.
|
|
355
|
+
// Decode UTF-8 only when the parser reaches a string token.
|
|
356
|
+
const text = bytes.toString("latin1");
|
|
357
|
+
const value = readJsonValue(text, 0, limits, 0, bytes);
|
|
358
|
+
if (value.status === "error")
|
|
359
|
+
return fail(value.code);
|
|
360
|
+
if (value.status === "need-character" || value.status === "syntax") {
|
|
361
|
+
// The reference classifies a parse failure by the input's encoding.
|
|
362
|
+
new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
363
|
+
return fail("invalid_syntax");
|
|
364
|
+
}
|
|
365
|
+
if (value.index === undefined || skipWhitespace(text, value.index) !== text.length)
|
|
366
|
+
return fail("trailing_bytes");
|
|
367
|
+
if (value.deferredCode)
|
|
368
|
+
return fail(value.deferredCode);
|
|
369
|
+
return ok(value.value);
|
|
332
370
|
}
|
|
333
371
|
catch (_error) {
|
|
334
372
|
return fail("invalid_encoding");
|
|
@@ -447,7 +485,12 @@ function decodeJws(compact, limits = DEFAULT_LIMITS, expectedTyp) {
|
|
|
447
485
|
typeof header.kid !== "string" || !/^[A-Za-z0-9._~-]{1,128}$/.test(header.kid))
|
|
448
486
|
return fail("protected_header_invalid");
|
|
449
487
|
const payloadValue = decodeJsonBytes(payloadBytes.value, limits);
|
|
450
|
-
if (!payloadValue.ok
|
|
488
|
+
if (!payloadValue.ok)
|
|
489
|
+
return fail("non_canonical_bytes");
|
|
490
|
+
const integerError = integerTokenError(payloadValue.value, schemaForType(expectedTyp));
|
|
491
|
+
if (integerError)
|
|
492
|
+
return fail(integerError);
|
|
493
|
+
if (canonicalTextError(payloadValue.value, payloadBytes.value.toString()))
|
|
451
494
|
return fail("non_canonical_bytes");
|
|
452
495
|
if (!isRecord(payloadValue.value))
|
|
453
496
|
return fail("protected_header_invalid");
|
|
@@ -461,48 +504,82 @@ function decodeJws(compact, limits = DEFAULT_LIMITS, expectedTyp) {
|
|
|
461
504
|
}
|
|
462
505
|
const ED25519_FIELD_PRIME = 2n ** 255n - 19n;
|
|
463
506
|
const ED25519_SUBGROUP_ORDER = 2n ** 252n + 27742317777372353535851937790883648493n;
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
507
|
+
const ED25519_D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n;
|
|
508
|
+
const ED25519_SQRT_MINUS_ONE = 0x2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0n;
|
|
509
|
+
function ed25519Field(value) {
|
|
510
|
+
const reduced = value % ED25519_FIELD_PRIME;
|
|
511
|
+
return reduced < 0n ? reduced + ED25519_FIELD_PRIME : reduced;
|
|
512
|
+
}
|
|
513
|
+
function squareEd25519(value, count) {
|
|
514
|
+
for (let i = 0; i < count; i++)
|
|
515
|
+
value = value * value % ED25519_FIELD_PRIME;
|
|
516
|
+
return value;
|
|
517
|
+
}
|
|
518
|
+
// Fixed addition chain for (p - 5) / 8 = 2^252 - 3: 251 squarings and 11 multiplies.
|
|
519
|
+
function ed25519Power(base) {
|
|
520
|
+
const x2 = squareEd25519(base, 1);
|
|
521
|
+
const x9 = squareEd25519(x2, 2) * base % ED25519_FIELD_PRIME;
|
|
522
|
+
const x11 = x9 * x2 % ED25519_FIELD_PRIME;
|
|
523
|
+
const x31 = squareEd25519(x11, 1) * x9 % ED25519_FIELD_PRIME;
|
|
524
|
+
const x10 = squareEd25519(x31, 5) * x31 % ED25519_FIELD_PRIME;
|
|
525
|
+
const x20 = squareEd25519(x10, 10) * x10 % ED25519_FIELD_PRIME;
|
|
526
|
+
const x40 = squareEd25519(x20, 20) * x20 % ED25519_FIELD_PRIME;
|
|
527
|
+
const x50 = squareEd25519(x40, 10) * x10 % ED25519_FIELD_PRIME;
|
|
528
|
+
const x100 = squareEd25519(x50, 50) * x50 % ED25519_FIELD_PRIME;
|
|
529
|
+
const x200 = squareEd25519(x100, 100) * x100 % ED25519_FIELD_PRIME;
|
|
530
|
+
const x250 = squareEd25519(x200, 50) * x50 % ED25519_FIELD_PRIME;
|
|
531
|
+
return squareEd25519(x250, 2) * base % ED25519_FIELD_PRIME;
|
|
532
|
+
}
|
|
533
|
+
// Homogeneous Edwards coordinates keep the doublings free of division.
|
|
534
|
+
function doubleEd25519Point([x, y, z]) {
|
|
535
|
+
const xSquared = ed25519Field(x * x);
|
|
536
|
+
const ySquared = ed25519Field(y * y);
|
|
537
|
+
const sum = ed25519Field(ySquared - xSquared);
|
|
538
|
+
const difference = ed25519Field(sum - 2n * z * z);
|
|
539
|
+
return [ed25519Field(2n * x * y * difference), ed25519Field(-sum * (xSquared + ySquared)), ed25519Field(sum * difference)];
|
|
540
|
+
}
|
|
474
541
|
function decodeLittleEndian(buffer) {
|
|
475
542
|
let value = 0n;
|
|
476
543
|
for (let index = buffer.length - 1; index >= 0; index -= 1)
|
|
477
544
|
value = (value << 8n) | BigInt(buffer[index]);
|
|
478
545
|
return value;
|
|
479
546
|
}
|
|
480
|
-
//
|
|
481
|
-
//
|
|
482
|
-
// eight-point torsion set is refused before OpenSSL sees the key or R.
|
|
547
|
+
// Decode a canonical curve point, reject negative zero and small order,
|
|
548
|
+
// and admit mixed order. Keys and signature R share this rule.
|
|
483
549
|
function strictEd25519Point(point) {
|
|
484
550
|
if (point.length !== 32)
|
|
485
551
|
return false;
|
|
486
|
-
|
|
552
|
+
const y = decodeLittleEndian(point) & ((1n << 255n) - 1n);
|
|
553
|
+
const sign = BigInt(point[31] >> 7);
|
|
554
|
+
if (y >= ED25519_FIELD_PRIME)
|
|
555
|
+
return false;
|
|
556
|
+
const ySquared = ed25519Field(y * y);
|
|
557
|
+
const numerator = ed25519Field(ySquared - 1n);
|
|
558
|
+
const denominator = ed25519Field(ED25519_D * ySquared + 1n);
|
|
559
|
+
if (denominator === 0n)
|
|
487
560
|
return false;
|
|
488
|
-
|
|
489
|
-
const
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
|
|
561
|
+
// RFC 8032 section 5.1.3 combines inversion and square root in one exponentiation.
|
|
562
|
+
const cubed = ed25519Field(denominator * denominator * denominator);
|
|
563
|
+
const seventh = ed25519Field(cubed * cubed * denominator);
|
|
564
|
+
let root = ed25519Field(numerator * cubed * ed25519Power(ed25519Field(numerator * seventh)));
|
|
565
|
+
if (ed25519Field(denominator * root * root) !== numerator)
|
|
566
|
+
root = ed25519Field(root * ED25519_SQRT_MINUS_ONE);
|
|
567
|
+
if (ed25519Field(denominator * root * root) !== numerator || (root === 0n && sign === 1n))
|
|
568
|
+
return false;
|
|
569
|
+
const x = (root & 1n) === sign ? root : ed25519Field(-root);
|
|
570
|
+
const [x8, y8, z8] = doubleEd25519Point(doubleEd25519Point(doubleEd25519Point([x, y, 1n])));
|
|
571
|
+
return !(x8 === 0n && ed25519Field(y8 - z8) === 0n);
|
|
572
|
+
}
|
|
573
|
+
function strictEd25519Signature(signature) {
|
|
574
|
+
return signature.length === 64 && strictEd25519Point(signature.subarray(0, 32)) &&
|
|
575
|
+
decodeLittleEndian(signature.subarray(32)) < ED25519_SUBGROUP_ORDER;
|
|
493
576
|
}
|
|
494
577
|
function ed25519(rawKey, message, signature) {
|
|
495
578
|
try {
|
|
496
579
|
const key = Buffer.from(rawKey, "base64url");
|
|
497
580
|
if (signature.length !== 64 || key.length !== 32)
|
|
498
581
|
return false;
|
|
499
|
-
if (!
|
|
500
|
-
return false;
|
|
501
|
-
const r = signature.subarray(0, 32);
|
|
502
|
-
const s = signature.subarray(32);
|
|
503
|
-
if (!strictEd25519Point(r))
|
|
504
|
-
return false;
|
|
505
|
-
if (decodeLittleEndian(s) >= ED25519_SUBGROUP_ORDER)
|
|
582
|
+
if (!strictEd25519Signature(signature))
|
|
506
583
|
return false;
|
|
507
584
|
const prefix = Buffer.from("302a300506032b6570032100", "hex");
|
|
508
585
|
const spki = createPublicKey({ key: Buffer.concat([prefix, key]), format: "der", type: "spki" });
|
|
@@ -537,8 +614,12 @@ function mldsa(keyAlgorithm, rawKey, message, signature) {
|
|
|
537
614
|
function keyMatchesRow(key, row) {
|
|
538
615
|
if (!key || key.algorithm !== row.keyAlgorithm || key.status !== "active")
|
|
539
616
|
return false;
|
|
540
|
-
|
|
541
|
-
|
|
617
|
+
return validPublicKey(key.public_key, row);
|
|
618
|
+
}
|
|
619
|
+
function validPublicKey(rawKey, row) {
|
|
620
|
+
const decoded = strictBase64url(rawKey);
|
|
621
|
+
return decoded.ok && decoded.value.length === row.publicKeyBytes &&
|
|
622
|
+
(row.keyAlgorithm !== "Ed25519" || strictEd25519Point(decoded.value));
|
|
542
623
|
}
|
|
543
624
|
function verifyDecodedJws(decoded, typ, keys, settings = DEFAULT_SETTINGS) {
|
|
544
625
|
const admission = profileError(decoded.header.alg, decoded.payload.protocol_revision, settings);
|
|
@@ -551,9 +632,9 @@ function verifyDecodedJws(decoded, typ, keys, settings = DEFAULT_SETTINGS) {
|
|
|
551
632
|
if (!key)
|
|
552
633
|
return fail("signature_invalid");
|
|
553
634
|
const bytes = Buffer.from(key.public_key, "base64url");
|
|
554
|
-
if (decoded.signature.length !== row.signatureBytes
|
|
635
|
+
if (decoded.signature.length !== row.signatureBytes)
|
|
555
636
|
return fail("signature_invalid");
|
|
556
|
-
if (row.keyAlgorithm === "Ed25519" &&
|
|
637
|
+
if (row.keyAlgorithm === "Ed25519" && !strictEd25519Signature(decoded.signature))
|
|
557
638
|
return fail("signature_invalid");
|
|
558
639
|
if (!runtimeSupportsAlgorithm(row.keyAlgorithm))
|
|
559
640
|
return fail("algorithm_unsupported_on_substrate");
|
|
@@ -564,8 +645,31 @@ function verifyDecodedJws(decoded, typ, keys, settings = DEFAULT_SETTINGS) {
|
|
|
564
645
|
return verified ? ok(null) : fail("signature_invalid");
|
|
565
646
|
}
|
|
566
647
|
catch (_error) {
|
|
567
|
-
return fail(
|
|
648
|
+
return fail(runtimeSupportsAlgorithm(row.keyAlgorithm) ? "signature_invalid" : "algorithm_unsupported_on_substrate");
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
function preparedArtifact(decoded, keys, facts, missingKeyCode, unnamed = false) {
|
|
652
|
+
return ok({ decoded, keys: keys.filter((key) => key.key_id === decoded.header.kid && key.status === "active"), facts, missingKeyCode, unnamed });
|
|
653
|
+
}
|
|
654
|
+
function finishPreparedArtifact(prepared, settings) {
|
|
655
|
+
if (!prepared.ok)
|
|
656
|
+
return prepared;
|
|
657
|
+
const { decoded, keys, facts, missingKeyCode, unnamed } = prepared.value;
|
|
658
|
+
if (unnamed)
|
|
659
|
+
return ok(facts);
|
|
660
|
+
if (keys.length === 0)
|
|
661
|
+
return fail(missingKeyCode);
|
|
662
|
+
if (decoded.header.typ !== "cap+receipt") {
|
|
663
|
+
const signature = verifyDecodedJws(decoded, decoded.header.typ, keys, settings);
|
|
664
|
+
return signature.ok ? ok(facts) : signature;
|
|
568
665
|
}
|
|
666
|
+
const candidates = keys.map((key) => ({ key, result: verifyDecodedJws(decoded, "cap+receipt", [key], settings) }));
|
|
667
|
+
const verifiedPublicKeys = new Set(candidates.filter((one) => one.result.ok).map((one) => one.key.public_key));
|
|
668
|
+
if (verifiedPublicKeys.size !== 1) {
|
|
669
|
+
const diagnostic = candidates.find((one) => !one.result.ok && ["algorithm_outside_profile", "revision_outside_profile", "algorithm_unsupported_on_substrate"].includes(one.result.code));
|
|
670
|
+
return fail(verifiedPublicKeys.size === 0 && diagnostic && !diagnostic.result.ok ? diagnostic.result.code : "signature_invalid");
|
|
671
|
+
}
|
|
672
|
+
return ok(facts);
|
|
569
673
|
}
|
|
570
674
|
const TIMESTAMP_GRAMMAR = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?Z$/;
|
|
571
675
|
// Exact RFC 3339 UTC parse mirroring Timestamp.ex: real calendar date, hour
|
|
@@ -603,7 +707,7 @@ function compareTimestamps(left, right) {
|
|
|
603
707
|
return 0;
|
|
604
708
|
return paddedLeft < paddedRight ? -1 : 1;
|
|
605
709
|
}
|
|
606
|
-
function
|
|
710
|
+
function prepareDescriptor(compact, predecessor = null, settings = DEFAULT_SETTINGS) {
|
|
607
711
|
const decoded = decodeJws(compact, settings.limits, "cap+party");
|
|
608
712
|
if (!decoded.ok)
|
|
609
713
|
return decoded;
|
|
@@ -628,16 +732,14 @@ function descriptorFromCompact(compact, predecessor = null, settings = DEFAULT_S
|
|
|
628
732
|
const descriptorAdmission = profileError(decoded.value.header.alg, payload.protocol_revision, settings);
|
|
629
733
|
if (descriptorAdmission)
|
|
630
734
|
return fail(descriptorAdmission);
|
|
631
|
-
const resolved = Array.isArray(keys) && keys.find((one) => one.key_id === decoded.value.header.kid && one.status === "active");
|
|
632
|
-
if (!resolved)
|
|
633
|
-
return fail("descriptor_key_invalid");
|
|
634
735
|
if (!parseTimestamp(payload.effective_from)) {
|
|
635
736
|
return fail("timestamp_invalid");
|
|
636
737
|
}
|
|
637
|
-
const
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
738
|
+
const facts = factsRecord({ ...decoded.value, digest, compact, lineage: [compact, ...(predecessor?.lineage ?? [])], protocol_revision: payload.protocol_revision, alg: decoded.value.header.alg });
|
|
739
|
+
return preparedArtifact(decoded.value, keys, facts, "descriptor_key_invalid");
|
|
740
|
+
}
|
|
741
|
+
function descriptorFromCompact(compact, predecessor = null, settings = DEFAULT_SETTINGS) {
|
|
742
|
+
return finishPreparedArtifact(prepareDescriptor(compact, predecessor, settings), settings);
|
|
641
743
|
}
|
|
642
744
|
// The nested schema admits registry encoding lengths; extraction checks
|
|
643
745
|
// the decoded bytes against the selected algorithm and canonical spelling.
|
|
@@ -816,6 +918,33 @@ function schemaError(value, fields) {
|
|
|
816
918
|
}
|
|
817
919
|
return null;
|
|
818
920
|
}
|
|
921
|
+
function schemaForType(typ) {
|
|
922
|
+
switch (typ) {
|
|
923
|
+
case "cap+party": return DESCRIPTOR_SCHEMA;
|
|
924
|
+
case "cap+acceptance": return ACCEPTANCE_SCHEMA;
|
|
925
|
+
case "cap+termination": return TERMINATION_SCHEMA;
|
|
926
|
+
case "cap+receipt": return RECEIPT_SCHEMA;
|
|
927
|
+
default: return [];
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
function integerTokenError(value, fields) {
|
|
931
|
+
if (!isRecord(value))
|
|
932
|
+
return null;
|
|
933
|
+
for (const rule of fields) {
|
|
934
|
+
const item = value[rule.name];
|
|
935
|
+
if (rule.type === "integer" && item instanceof Number)
|
|
936
|
+
return "invalid_type";
|
|
937
|
+
if (!rule.nested)
|
|
938
|
+
continue;
|
|
939
|
+
const items = rule.type === "array" && Array.isArray(item) ? item : [item];
|
|
940
|
+
for (const child of items) {
|
|
941
|
+
const error = integerTokenError(child, rule.nested);
|
|
942
|
+
if (error)
|
|
943
|
+
return error;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
819
948
|
const REVISION_SCHEMA = [
|
|
820
949
|
field("protocol_revision", "integer", integerRange(1, 2)), digestField("charter_id", false),
|
|
821
950
|
safeIntegerField("revision_number"), digestField("prev_revision_digest", false),
|
|
@@ -925,6 +1054,9 @@ function revisionFromText(text, limits = DEFAULT_LIMITS) {
|
|
|
925
1054
|
const decoded = decodeJsonText(text, limits);
|
|
926
1055
|
if (!decoded.ok)
|
|
927
1056
|
return decoded;
|
|
1057
|
+
const integerError = integerTokenError(decoded.value, REVISION_SCHEMA);
|
|
1058
|
+
if (integerError)
|
|
1059
|
+
return fail(integerError);
|
|
928
1060
|
const encoding = canonicalTextError(decoded.value, text);
|
|
929
1061
|
if (encoding)
|
|
930
1062
|
return fail(encoding);
|
|
@@ -964,7 +1096,7 @@ function revisionFromText(text, limits = DEFAULT_LIMITS) {
|
|
|
964
1096
|
const bytes = Buffer.from(text);
|
|
965
1097
|
return ok(factsRecord({ value, bytes, digest: taggedHash("charter_revision_content", bytes), protocol_revision: value.protocol_revision }));
|
|
966
1098
|
}
|
|
967
|
-
function
|
|
1099
|
+
function prepareAcceptance(compact, revision, chain, settings = DEFAULT_SETTINGS) {
|
|
968
1100
|
const decoded = decodeJws(compact, settings.limits, "cap+acceptance");
|
|
969
1101
|
if (!decoded.ok)
|
|
970
1102
|
return decoded;
|
|
@@ -983,14 +1115,13 @@ function acceptanceFromCompact(compact, revision, chain, settings = DEFAULT_SETT
|
|
|
983
1115
|
const acceptanceAdmission = profileError(decoded.value.header.alg, claims.protocol_revision, settings);
|
|
984
1116
|
if (acceptanceAdmission)
|
|
985
1117
|
return fail(acceptanceAdmission);
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
const signature = verifyDecodedJws(decoded.value, "cap+acceptance", descriptor.payload.verification_keys, settings);
|
|
989
|
-
if (!signature.ok)
|
|
990
|
-
return signature;
|
|
991
|
-
return ok(factsRecord({ claims, digest: taggedHash("acceptance_content", decoded.value.payloadBytes), descriptorPosition: chain.positions[claims.party_descriptor_digest], protocol_revision: claims.protocol_revision, alg: decoded.value.header.alg }));
|
|
1118
|
+
const facts = factsRecord({ claims, digest: taggedHash("acceptance_content", decoded.value.payloadBytes), descriptorPosition: chain.positions[claims.party_descriptor_digest], protocol_revision: claims.protocol_revision, alg: decoded.value.header.alg });
|
|
1119
|
+
return preparedArtifact(decoded.value, descriptor.payload.verification_keys, facts, "acceptance_invalid");
|
|
992
1120
|
}
|
|
993
|
-
function
|
|
1121
|
+
function acceptanceFromCompact(compact, revision, chain, settings = DEFAULT_SETTINGS) {
|
|
1122
|
+
return finishPreparedArtifact(prepareAcceptance(compact, revision, chain, settings), settings);
|
|
1123
|
+
}
|
|
1124
|
+
function prepareTermination(compact, revision, chain, settings = DEFAULT_SETTINGS) {
|
|
994
1125
|
const decoded = decodeJws(compact, settings.limits, "cap+termination");
|
|
995
1126
|
if (!decoded.ok)
|
|
996
1127
|
return decoded;
|
|
@@ -1008,12 +1139,11 @@ function terminationFromCompact(compact, revision, chain, settings = DEFAULT_SET
|
|
|
1008
1139
|
const terminationAdmission = profileError(decoded.value.header.alg, claims.protocol_revision, settings);
|
|
1009
1140
|
if (terminationAdmission)
|
|
1010
1141
|
return fail(terminationAdmission);
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
return ok(factsRecord({ claims, digest: taggedHash("termination_content", decoded.value.payloadBytes), descriptorPosition: chain.positions[claims.party_descriptor_digest], protocol_revision: claims.protocol_revision, alg: decoded.value.header.alg }));
|
|
1142
|
+
const facts = factsRecord({ claims, digest: taggedHash("termination_content", decoded.value.payloadBytes), descriptorPosition: chain.positions[claims.party_descriptor_digest], protocol_revision: claims.protocol_revision, alg: decoded.value.header.alg });
|
|
1143
|
+
return preparedArtifact(decoded.value, descriptor.payload.verification_keys, facts, "termination_invalid");
|
|
1144
|
+
}
|
|
1145
|
+
function terminationFromCompact(compact, revision, chain, settings = DEFAULT_SETTINGS) {
|
|
1146
|
+
return finishPreparedArtifact(prepareTermination(compact, revision, chain, settings), settings);
|
|
1017
1147
|
}
|
|
1018
1148
|
// Chain-level structural checks mirroring Chain.verify: unique revision
|
|
1019
1149
|
// digests, exactly one genesis defining one charter identity, exact
|
|
@@ -1069,39 +1199,33 @@ function katVerifies(keyAlgorithm, message = KNOWN_ANSWER_MESSAGE) {
|
|
|
1069
1199
|
// The known-answer signature over any other message must fail: a runtime
|
|
1070
1200
|
// whose verify accepts everything is never counted as supporting a key type.
|
|
1071
1201
|
const KNOWN_ANSWER_TAMPERED = Buffer.from("charter-agreement-protocol capability known-answer, tampered");
|
|
1072
|
-
//
|
|
1073
|
-
|
|
1074
|
-
// as well as runtimes that support the ML-DSA key types. A runtime whose key
|
|
1075
|
-
// objects do not report their type (Bun's ML-DSA keys, for example) is asked
|
|
1076
|
-
// once through the known-answer vector instead, and the answer is kept. A
|
|
1077
|
-
// provider failure during the real crypto call is classified by the selected
|
|
1078
|
-
// vector only.
|
|
1079
|
-
const UNTYPED_KEY_SUPPORT = new Map();
|
|
1202
|
+
// Keep the declared type and known-answer result once per key algorithm.
|
|
1203
|
+
const RUNTIME_SUPPORT = new Map();
|
|
1080
1204
|
function runtimeSupportsAlgorithm(keyAlgorithm) {
|
|
1205
|
+
const known = RUNTIME_SUPPORT.get(keyAlgorithm);
|
|
1206
|
+
if (known !== undefined)
|
|
1207
|
+
return known;
|
|
1081
1208
|
const vector = KNOWN_ANSWERS[keyAlgorithm];
|
|
1082
1209
|
if (!vector)
|
|
1083
1210
|
return false;
|
|
1084
1211
|
const prefix = keyAlgorithm === "Ed25519" ? Buffer.from("302a300506032b6570032100", "hex") : ML_DSA_SPKI_PREFIXES[keyAlgorithm];
|
|
1212
|
+
let declared = false;
|
|
1085
1213
|
try {
|
|
1086
1214
|
const key = createPublicKey({ key: Buffer.concat([prefix, Buffer.from(vector.publicKey, "base64url")]), format: "der", type: "spki" });
|
|
1087
|
-
|
|
1088
|
-
return key.asymmetricKeyType === keyAlgorithm.toLowerCase();
|
|
1215
|
+
declared = key.asymmetricKeyType === undefined || key.asymmetricKeyType === keyAlgorithm.toLowerCase();
|
|
1089
1216
|
}
|
|
1090
1217
|
catch (_error) {
|
|
1091
|
-
|
|
1218
|
+
declared = false;
|
|
1092
1219
|
}
|
|
1093
|
-
const
|
|
1094
|
-
|
|
1095
|
-
return known;
|
|
1096
|
-
const supported = katVerifies(keyAlgorithm) && !katVerifies(keyAlgorithm, KNOWN_ANSWER_TAMPERED);
|
|
1097
|
-
UNTYPED_KEY_SUPPORT.set(keyAlgorithm, supported);
|
|
1220
|
+
const supported = declared && katVerifies(keyAlgorithm) && !katVerifies(keyAlgorithm, KNOWN_ANSWER_TAMPERED);
|
|
1221
|
+
RUNTIME_SUPPORT.set(keyAlgorithm, supported);
|
|
1098
1222
|
return supported;
|
|
1099
1223
|
}
|
|
1100
1224
|
export function capabilities() {
|
|
1101
1225
|
const algorithms = ALG_ROWS.map((row) => ({
|
|
1102
1226
|
name: row.name,
|
|
1103
1227
|
keyAlgorithm: row.keyAlgorithm,
|
|
1104
|
-
verifiable:
|
|
1228
|
+
verifiable: runtimeSupportsAlgorithm(row.keyAlgorithm),
|
|
1105
1229
|
}));
|
|
1106
1230
|
// A browser page has no `process`; it then reports no linked library.
|
|
1107
1231
|
const versions = typeof process === "undefined" ? undefined : process.versions;
|
|
@@ -1135,8 +1259,9 @@ function parseProfile(spec) {
|
|
|
1135
1259
|
const names = spec.algorithms;
|
|
1136
1260
|
if (names.length === 0 || new Set(names).size !== names.length)
|
|
1137
1261
|
return { ok: false, code: "invalid_profile" };
|
|
1138
|
-
|
|
1139
|
-
|
|
1262
|
+
for (const name of names) {
|
|
1263
|
+
if (typeof name !== "string" || !registryNames.includes(name))
|
|
1264
|
+
return { ok: false, code: "invalid_profile" };
|
|
1140
1265
|
}
|
|
1141
1266
|
algorithms = names;
|
|
1142
1267
|
}
|
|
@@ -1183,10 +1308,10 @@ function revisionWithProfile(text, settings) {
|
|
|
1183
1308
|
return admission ? fail(admission) : revision;
|
|
1184
1309
|
}
|
|
1185
1310
|
function boundedArtifacts(lists, limits) {
|
|
1311
|
+
if (!lists.every(Array.isArray))
|
|
1312
|
+
return "invalid_type";
|
|
1186
1313
|
let bytes = 0;
|
|
1187
1314
|
for (const list of lists) {
|
|
1188
|
-
if (!Array.isArray(list))
|
|
1189
|
-
return "invalid_type";
|
|
1190
1315
|
for (let i = 0; i < list.length; i++) {
|
|
1191
1316
|
if (i >= limits.max_artifact_set_items)
|
|
1192
1317
|
return "limit_exceeded";
|
|
@@ -1209,8 +1334,6 @@ function chainFromInput(input, settings = DEFAULT_SETTINGS) {
|
|
|
1209
1334
|
const bounded = boundedArtifacts([input.revisions, input.acceptances, input.descriptors, input.terminations], settings.limits);
|
|
1210
1335
|
if (bounded)
|
|
1211
1336
|
return fail(bounded);
|
|
1212
|
-
if (!Array.isArray(input.revisions) || input.revisions.length === 0)
|
|
1213
|
-
return fail("chain_invalid");
|
|
1214
1337
|
const descriptors = descriptorViews(input.descriptors, settings);
|
|
1215
1338
|
if (!descriptors.ok)
|
|
1216
1339
|
return descriptors;
|
|
@@ -1268,12 +1391,11 @@ function chainFromInput(input, settings = DEFAULT_SETTINGS) {
|
|
|
1268
1391
|
const decoded = decodeJws(compact, settings.limits, "cap+acceptance");
|
|
1269
1392
|
if (!decoded.ok)
|
|
1270
1393
|
return decoded;
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
const verified = acceptanceFromCompact(compact, revision, descriptors.value, settings);
|
|
1394
|
+
const routed = signingContextFromSet(byDigest, descriptors.value, decoded.value.payload.revision_digest, decoded.value.payload.party_descriptor_digest);
|
|
1395
|
+
if (!routed.ok)
|
|
1396
|
+
return routed;
|
|
1397
|
+
const { revision, chain } = routed.value;
|
|
1398
|
+
const verified = acceptanceFromCompact(compact, revision, chain, settings);
|
|
1277
1399
|
if (!verified.ok)
|
|
1278
1400
|
return verified;
|
|
1279
1401
|
const coordinate = `${verified.value.claims.revision_digest}\0${verified.value.claims.party_descriptor_digest}\0${verified.value.claims.party_role}`;
|
|
@@ -1302,12 +1424,11 @@ function chainFromInput(input, settings = DEFAULT_SETTINGS) {
|
|
|
1302
1424
|
const decoded = decodeJws(compact, settings.limits, "cap+termination");
|
|
1303
1425
|
if (!decoded.ok)
|
|
1304
1426
|
return decoded;
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
const verified = terminationFromCompact(compact, revision, descriptors.value, settings);
|
|
1427
|
+
const routed = signingContextFromSet(byDigest, descriptors.value, decoded.value.payload.governing_revision_digest, decoded.value.payload.party_descriptor_digest);
|
|
1428
|
+
if (!routed.ok)
|
|
1429
|
+
return routed;
|
|
1430
|
+
const { revision, chain } = routed.value;
|
|
1431
|
+
const verified = terminationFromCompact(compact, revision, chain, settings);
|
|
1311
1432
|
if (!verified.ok)
|
|
1312
1433
|
return verified;
|
|
1313
1434
|
terminations.push(verified.value);
|
|
@@ -1337,6 +1458,14 @@ function chainFromInput(input, settings = DEFAULT_SETTINGS) {
|
|
|
1337
1458
|
const topology = active.length > 0 && !linear ? "forked" : "linear";
|
|
1338
1459
|
return ok(factsRecord({ descriptors: descriptors.value, revisions, acceptances, terminations, accepted, acceptedDigests: accepted.map((one) => one.digest).sort(), supersededDigests: [...superseded].sort(), topology, charterId }));
|
|
1339
1460
|
}
|
|
1461
|
+
// Both set verification and pre-sign checks route through this lookup.
|
|
1462
|
+
function signingContextFromSet(revisions, descriptors, revisionDigest, descriptorDigest, code = "chain_invalid") {
|
|
1463
|
+
if (typeof revisionDigest !== "string" || typeof descriptorDigest !== "string")
|
|
1464
|
+
return fail(code);
|
|
1465
|
+
const revision = revisions.get(revisionDigest);
|
|
1466
|
+
const chain = descriptors.histories.find((history) => history.descriptors.some((one) => one.digest === descriptorDigest));
|
|
1467
|
+
return revision && chain ? ok({ revision, chain }) : fail(code);
|
|
1468
|
+
}
|
|
1340
1469
|
// Governing mirrors the reference semantics exactly: candidates are accepted,
|
|
1341
1470
|
// non-superseded, and effective at the instant (start-inclusive, end-exclusive
|
|
1342
1471
|
// via exact fraction comparison); a unique highest-numbered head governs only
|
|
@@ -1414,7 +1543,7 @@ function receiptSigningKeys(chain, claimedRevision, role) {
|
|
|
1414
1543
|
}));
|
|
1415
1544
|
return [...new Map(keys.map((key) => [canonical(key), key])).values()];
|
|
1416
1545
|
}
|
|
1417
|
-
function
|
|
1546
|
+
function prepareReceipt(compact, context, settings = DEFAULT_SETTINGS) {
|
|
1418
1547
|
const revisionOnly = context.kind === "revision";
|
|
1419
1548
|
const chain = context.kind === "chain" ? context.chain : { accepted: [context.revision], charterId: context.revision.value.charter_id || context.revision.digest };
|
|
1420
1549
|
const decoded = decodeJws(compact, settings.limits, "cap+receipt");
|
|
@@ -1438,7 +1567,8 @@ function receiptFromCompact(compact, context, settings = DEFAULT_SETTINGS) {
|
|
|
1438
1567
|
const admission = profileError(decoded.value.header.alg, claims.protocol_revision, settings);
|
|
1439
1568
|
if (admission)
|
|
1440
1569
|
return fail(admission);
|
|
1441
|
-
|
|
1570
|
+
const facts = factsRecord({ claims, digest: taggedHash("receipt_content", decoded.value.payloadBytes), ...projection.value, optionalExtensions: Object.keys(claims.extensions?.optional || {}).sort(), protocol_revision: claims.protocol_revision, alg: decoded.value.header.alg }, ["signature"]);
|
|
1571
|
+
return preparedArtifact(decoded.value, [], facts, "signature_invalid", true);
|
|
1442
1572
|
}
|
|
1443
1573
|
// The envelope is admitted before any key is looked up, so an
|
|
1444
1574
|
// out-of-profile receipt reports the profile rejection in both context
|
|
@@ -1446,15 +1576,12 @@ function receiptFromCompact(compact, context, settings = DEFAULT_SETTINGS) {
|
|
|
1446
1576
|
const envelopeAdmission = profileError(decoded.value.header.alg, claims.protocol_revision, settings);
|
|
1447
1577
|
if (envelopeAdmission)
|
|
1448
1578
|
return fail(envelopeAdmission);
|
|
1449
|
-
const
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
return fail(verifiedPublicKeys.size === 0 && diagnostic && !diagnostic.result.ok ? diagnostic.result.code : "signature_invalid");
|
|
1456
|
-
}
|
|
1457
|
-
return ok(factsRecord({ claims, digest: taggedHash("receipt_content", decoded.value.payloadBytes), ...projection.value, optionalExtensions: Object.keys(claims.extensions?.optional || {}).sort(), protocol_revision: claims.protocol_revision, alg: decoded.value.header.alg }));
|
|
1579
|
+
const keys = receiptSigningKeys(context.chain, claimedRevision, claims.issuing_party_role);
|
|
1580
|
+
const facts = factsRecord({ claims, digest: taggedHash("receipt_content", decoded.value.payloadBytes), ...projection.value, optionalExtensions: Object.keys(claims.extensions?.optional || {}).sort(), protocol_revision: claims.protocol_revision, alg: decoded.value.header.alg });
|
|
1581
|
+
return preparedArtifact(decoded.value, keys, facts, "signature_invalid");
|
|
1582
|
+
}
|
|
1583
|
+
function receiptFromCompact(compact, context, settings = DEFAULT_SETTINGS) {
|
|
1584
|
+
return finishPreparedArtifact(prepareReceipt(compact, context, settings), settings);
|
|
1458
1585
|
}
|
|
1459
1586
|
function receiptWithContext(compact, context, settings) {
|
|
1460
1587
|
if (typeof context === "string") {
|
|
@@ -1463,62 +1590,284 @@ function receiptWithContext(compact, context, settings) {
|
|
|
1463
1590
|
return revision;
|
|
1464
1591
|
return receiptFromCompact(compact, { kind: "revision", revision: revision.value }, settings);
|
|
1465
1592
|
}
|
|
1593
|
+
if (!isRecord(context))
|
|
1594
|
+
return fail("invalid_type");
|
|
1466
1595
|
const chain = chainFromInput(context, settings);
|
|
1467
1596
|
return chain.ok ? receiptFromCompact(compact, { kind: "chain", chain: chain.value }, settings) : chain;
|
|
1468
1597
|
}
|
|
1598
|
+
const CASE_OPTION_MEMBERS = {
|
|
1599
|
+
limits: Object.fromEntries(Object.keys(DEFAULT_LIMITS).map((name) => [name, name])),
|
|
1600
|
+
profile: { algorithms: "algorithms", revisions: "revisions" },
|
|
1601
|
+
revisions: { min: "min", max: "max" },
|
|
1602
|
+
};
|
|
1603
|
+
function mappedCaseOptions(selected, kind) {
|
|
1604
|
+
if (!isRecord(selected))
|
|
1605
|
+
return fail("invalid_type");
|
|
1606
|
+
const members = CASE_OPTION_MEMBERS[kind];
|
|
1607
|
+
const mapped = {};
|
|
1608
|
+
for (const [name, value] of Object.entries(selected)) {
|
|
1609
|
+
if (!Object.hasOwn(members, name))
|
|
1610
|
+
return fail("invalid_type");
|
|
1611
|
+
mapped[members[name]] = value;
|
|
1612
|
+
}
|
|
1613
|
+
return ok(mapped);
|
|
1614
|
+
}
|
|
1615
|
+
function closedCaseOptions(input) {
|
|
1616
|
+
const limits = mappedCaseOptions(Object.hasOwn(input, "limits") ? input.limits : {}, "limits");
|
|
1617
|
+
if (!limits.ok || !validLimits(limits.value))
|
|
1618
|
+
return fail("invalid_limits");
|
|
1619
|
+
const profile = mappedCaseOptions(Object.hasOwn(input, "profile") ? input.profile : {}, "profile");
|
|
1620
|
+
if (!profile.ok)
|
|
1621
|
+
return fail("invalid_profile");
|
|
1622
|
+
const range = mappedCaseOptions(Object.hasOwn(profile.value, "revisions") ? profile.value.revisions : {}, "revisions");
|
|
1623
|
+
if (!range.ok || !parseProfile(profile.value).ok)
|
|
1624
|
+
return fail("invalid_profile");
|
|
1625
|
+
return ok(null);
|
|
1626
|
+
}
|
|
1627
|
+
function signingCaseContext(context, options) {
|
|
1628
|
+
if (!isRecord(context))
|
|
1629
|
+
return ok({ value: context, kind: context === null ? "descriptor" : "raw" });
|
|
1630
|
+
if ("set" in context) {
|
|
1631
|
+
const set = context.set;
|
|
1632
|
+
const members = ["revisions", "acceptances", "descriptors", "terminations"];
|
|
1633
|
+
if (!isRecord(set))
|
|
1634
|
+
return fail("invalid_type");
|
|
1635
|
+
return ok({ value: Object.fromEntries(members.map((name) => [name, set?.[name] ?? null])), kind: "set" });
|
|
1636
|
+
}
|
|
1637
|
+
if ("chain" in context) {
|
|
1638
|
+
if (!exactKeys(context.chain, [...CHAIN_MEMBERS]))
|
|
1639
|
+
return fail("invalid_type");
|
|
1640
|
+
const verified = chainWithOptions(context.chain, { limits: options.limits });
|
|
1641
|
+
return verified.ok ? ok({ value: context.chain, kind: "chain" }) : fail(verified.code);
|
|
1642
|
+
}
|
|
1643
|
+
if ("revision_text" in context) {
|
|
1644
|
+
const decoded = decodeCharterRevision(context.revision_text);
|
|
1645
|
+
return decoded.ok ? ok({ value: context.revision_text, kind: "revision" }) : fail(decoded.code);
|
|
1646
|
+
}
|
|
1647
|
+
if ("predecessor_compacts" in context) {
|
|
1648
|
+
if (!Array.isArray(context.predecessor_compacts))
|
|
1649
|
+
return fail("invalid_type");
|
|
1650
|
+
let previous = null;
|
|
1651
|
+
for (const compact of context.predecessor_compacts) {
|
|
1652
|
+
if (typeof compact !== "string")
|
|
1653
|
+
return fail("invalid_type");
|
|
1654
|
+
const verified = verifyDescriptor(compact, previous);
|
|
1655
|
+
if (!verified.ok)
|
|
1656
|
+
return fail(verified.code);
|
|
1657
|
+
previous = verified.facts;
|
|
1658
|
+
}
|
|
1659
|
+
return ok({ value: previous, kind: "descriptor" });
|
|
1660
|
+
}
|
|
1661
|
+
return ok({ value: context, kind: "raw" });
|
|
1662
|
+
}
|
|
1663
|
+
function signingCaseOptions(input) {
|
|
1664
|
+
const limits = input.limits ?? {};
|
|
1665
|
+
const selectedProfile = input.profile ?? {};
|
|
1666
|
+
const range = selectedProfile.revisions;
|
|
1667
|
+
const profile = {
|
|
1668
|
+
algorithms: Object.hasOwn(selectedProfile, "algorithms") ? selectedProfile.algorithms : DEFAULT_SETTINGS.profile.algorithms,
|
|
1669
|
+
revisions: range ? { min: range.min, max: range.max } : { min: 1, max: 3 },
|
|
1670
|
+
};
|
|
1671
|
+
return { algorithm: input.algorithm, limits, profile };
|
|
1672
|
+
}
|
|
1673
|
+
function executeArtifactCase(one) {
|
|
1674
|
+
const input = one.input;
|
|
1675
|
+
switch (one.surface) {
|
|
1676
|
+
case "signing.prepare": {
|
|
1677
|
+
const options = signingCaseOptions(input);
|
|
1678
|
+
const context = signingCaseContext(input.context, options);
|
|
1679
|
+
if (!context.ok)
|
|
1680
|
+
return invalid(context.code);
|
|
1681
|
+
const key = strictBase64url(input.public_key);
|
|
1682
|
+
if (!key.ok)
|
|
1683
|
+
return invalid(key.code);
|
|
1684
|
+
const kind = input.kind === "party_descriptor" ? "descriptor" :
|
|
1685
|
+
["acceptance", "termination", "receipt"].includes(input.kind) ? input.kind : "unknown";
|
|
1686
|
+
const matches = kind === "descriptor" ? context.value.kind === "descriptor" :
|
|
1687
|
+
kind === "receipt" ? ["chain", "revision"].includes(context.value.kind) :
|
|
1688
|
+
(kind === "acceptance" || kind === "termination") && context.value.kind === "set";
|
|
1689
|
+
const prepared = prepareSigning(kind, input.kid, input.claims, encodeBase64url(key.value), matches ? context.value.value : {}, options);
|
|
1690
|
+
if (!prepared.ok)
|
|
1691
|
+
return invalid(prepared.code);
|
|
1692
|
+
return valid({ protected_segment: prepared.plan.input.protectedSegment, payload_segment: prepared.plan.input.payloadSegment, key: prepared.plan.key });
|
|
1693
|
+
}
|
|
1694
|
+
case "party_descriptor.verify": {
|
|
1695
|
+
// A case may name its predecessor history, genesis first; each entry
|
|
1696
|
+
// verifies under the full profile against the one before it.
|
|
1697
|
+
let predecessor = null;
|
|
1698
|
+
if (input.predecessor_compacts !== undefined && !Array.isArray(input.predecessor_compacts))
|
|
1699
|
+
return invalid("invalid_type");
|
|
1700
|
+
for (const compact of (input.predecessor_compacts ?? [])) {
|
|
1701
|
+
if (typeof compact !== "string")
|
|
1702
|
+
return invalid("invalid_type");
|
|
1703
|
+
const verified = verifyDescriptor(compact, predecessor);
|
|
1704
|
+
if (!verified.ok)
|
|
1705
|
+
return invalid(verified.code);
|
|
1706
|
+
predecessor = verified.facts;
|
|
1707
|
+
}
|
|
1708
|
+
const verified = verifyDescriptor(input.compact, predecessor, { profile: input.profile });
|
|
1709
|
+
if (!verified.ok)
|
|
1710
|
+
return invalid(verified.code);
|
|
1711
|
+
return valid({ descriptor_digest: verified.facts.descriptor_digest, party_id: verified.facts.party_id, descriptor_number: verified.facts.descriptor_number });
|
|
1712
|
+
}
|
|
1713
|
+
case "acceptance.verify": {
|
|
1714
|
+
// The runner calls the public function, so the corpus checks the code
|
|
1715
|
+
// consumers run.
|
|
1716
|
+
const verified = verifyAcceptance(input.compact, input.revision_text, input.descriptor_compacts, { profile: input.profile });
|
|
1717
|
+
if (!verified.ok)
|
|
1718
|
+
return invalid(verified.code);
|
|
1719
|
+
return valid({ acceptance_digest: verified.facts.acceptance_digest, revision_digest: verified.facts.claims.revision_digest, party_descriptor_digest: verified.facts.claims.party_descriptor_digest, descriptor_position: verified.facts.descriptor_position });
|
|
1720
|
+
}
|
|
1721
|
+
case "termination.verify": {
|
|
1722
|
+
const verified = verifyTermination(input.compact, input.revision_text, input.descriptor_compacts, { profile: input.profile });
|
|
1723
|
+
if (!verified.ok)
|
|
1724
|
+
return invalid(verified.code);
|
|
1725
|
+
return valid({ termination_digest: verified.facts.termination_digest, governing_revision_digest: verified.facts.claims.governing_revision_digest, party_descriptor_digest: verified.facts.claims.party_descriptor_digest, reason_code: verified.facts.claims.reason_code, descriptor_position: verified.facts.descriptor_position });
|
|
1726
|
+
}
|
|
1727
|
+
case "receipt.verify": {
|
|
1728
|
+
const settings = resolveOptions({ limits: input.limits, profile: input.profile });
|
|
1729
|
+
if (!settings.ok)
|
|
1730
|
+
return invalid(settings.code);
|
|
1731
|
+
if (!Object.hasOwn(input, "revision_text") && !exactKeys(input.chain, [...CHAIN_MEMBERS]))
|
|
1732
|
+
return invalid("invalid_type");
|
|
1733
|
+
if (!Object.hasOwn(input, "revision_text")) {
|
|
1734
|
+
const context = chainWithOptions(input.chain, { limits: input.limits });
|
|
1735
|
+
if (!context.ok)
|
|
1736
|
+
return invalid(context.code);
|
|
1737
|
+
}
|
|
1738
|
+
return project(receiptWithContext(input.compact, Object.hasOwn(input, "revision_text") ? input.revision_text : input.chain, settings.value), (facts) => ({ receipt_digest: facts.digest, revision_number: facts.claims.revision_number, revision_digest: facts.claims.revision_digest, decision: facts.claims.decision, outcome: facts.claims.outcome, chain_conflict: facts.chainConflict, governing_match: facts.governingMatch, deployment_digest_matched: facts.deploymentMatched, optional_extensions_retained: facts.optionalExtensions }));
|
|
1739
|
+
}
|
|
1740
|
+
default:
|
|
1741
|
+
return null;
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1469
1744
|
function execute(one) {
|
|
1745
|
+
try {
|
|
1746
|
+
if (!isRecord(one) || !isRecord(one.input))
|
|
1747
|
+
return invalid("invalid_type");
|
|
1748
|
+
return executeInputCase(one);
|
|
1749
|
+
}
|
|
1750
|
+
catch (_error) {
|
|
1751
|
+
return invalid("invalid_type");
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
function isIntegerToken(value) {
|
|
1755
|
+
return typeof value === "bigint" || (typeof value === "number" && Number.isInteger(value));
|
|
1756
|
+
}
|
|
1757
|
+
function caseKindMatches(input) {
|
|
1758
|
+
const value = input.value;
|
|
1759
|
+
switch (input.kind) {
|
|
1760
|
+
case "integer": return isIntegerToken(value);
|
|
1761
|
+
case "float": return value instanceof Number || (typeof value === "number" && !Number.isInteger(value));
|
|
1762
|
+
case "string": return typeof value === "string";
|
|
1763
|
+
case "boolean": return typeof value === "boolean";
|
|
1764
|
+
case "null": return value === null;
|
|
1765
|
+
case "array": return Array.isArray(value);
|
|
1766
|
+
case "object": return isRecord(value);
|
|
1767
|
+
default: return false;
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
function canonicalInteger(value) {
|
|
1771
|
+
const integer = BigInt(value);
|
|
1772
|
+
return integer > MAXIMUM_SAFE_INTEGER || integer < -MAXIMUM_SAFE_INTEGER ? invalid("integer_magnitude") :
|
|
1773
|
+
valid({ text: integer.toString() });
|
|
1774
|
+
}
|
|
1775
|
+
function executeCanonicalCase(input) {
|
|
1776
|
+
if (Object.hasOwn(input, "text")) {
|
|
1777
|
+
const decoded = decodeJsonText(input.text);
|
|
1778
|
+
if (!decoded.ok)
|
|
1779
|
+
return invalid(decoded.code);
|
|
1780
|
+
return canonical(decoded.value) === input.text ? valid({ text: input.text }) : invalid("non_canonical_bytes");
|
|
1781
|
+
}
|
|
1782
|
+
if (input.tag === "integer") {
|
|
1783
|
+
if (typeof input.text_value !== "string" || !/^[+-]?[0-9]+$/.test(input.text_value))
|
|
1784
|
+
return invalid("invalid_type");
|
|
1785
|
+
return canonicalInteger(BigInt(input.text_value));
|
|
1786
|
+
}
|
|
1787
|
+
if (input.tag === "string_codepoint") {
|
|
1788
|
+
if (typeof input.value !== "string" || !/^[+-]?[0-9a-fA-F]+$/.test(input.value))
|
|
1789
|
+
return invalid("invalid_type");
|
|
1790
|
+
const point = Number.parseInt(input.value, 16);
|
|
1791
|
+
if (point < 0 || point > 0x10ffff || (point >= 0xd800 && point <= 0xdfff))
|
|
1792
|
+
return invalid("invalid_type");
|
|
1793
|
+
const text = JSON.stringify(String.fromCodePoint(point));
|
|
1794
|
+
const decoded = decodeJsonText(text);
|
|
1795
|
+
return decoded.ok ? valid({ text }) : invalid(decoded.code);
|
|
1796
|
+
}
|
|
1797
|
+
if (input.tag === "object" && Array.isArray(input.members)) {
|
|
1798
|
+
const pairs = input.members;
|
|
1799
|
+
if (!pairs.every((pair) => Array.isArray(pair) && pair.length === 2 && typeof pair[0] === "string" &&
|
|
1800
|
+
isRecord(pair[1]) && pair[1].tag === "integer" && isIntegerToken(pair[1].value)))
|
|
1801
|
+
return invalid("invalid_type");
|
|
1802
|
+
const names = pairs.map(([name]) => name);
|
|
1803
|
+
if (new Set(names).size !== names.length)
|
|
1804
|
+
return invalid("duplicate_member");
|
|
1805
|
+
for (const [name, item] of pairs) {
|
|
1806
|
+
const encodedName = decodeJsonText(JSON.stringify(name));
|
|
1807
|
+
if (!encodedName.ok)
|
|
1808
|
+
return invalid(encodedName.code);
|
|
1809
|
+
const encoded = canonicalInteger(item.value);
|
|
1810
|
+
if (encoded.status === "invalid")
|
|
1811
|
+
return encoded;
|
|
1812
|
+
}
|
|
1813
|
+
return valid({ text: canonical(Object.fromEntries(pairs.map(([name, item]) => [name, Number(item.value)]))) });
|
|
1814
|
+
}
|
|
1815
|
+
return invalid("invalid_type");
|
|
1816
|
+
}
|
|
1817
|
+
function executeDigestCase(input) {
|
|
1818
|
+
const domain = input.domain ?? "charter_revision_content";
|
|
1819
|
+
if (typeof domain !== "string" || !Object.hasOwn(SEPARATORS, domain) || domain === "signature_registry")
|
|
1820
|
+
return invalid("invalid_type");
|
|
1821
|
+
if (!Object.hasOwn(input, "bytes_base64url"))
|
|
1822
|
+
return invalid("invalid_type");
|
|
1823
|
+
const bytes = strictBase64url(input.bytes_base64url);
|
|
1824
|
+
if (!bytes.ok)
|
|
1825
|
+
return invalid(bytes.code);
|
|
1826
|
+
if (!Object.hasOwn(input, "tagged"))
|
|
1827
|
+
return valid({ algorithm: "sha-256" });
|
|
1828
|
+
if (typeof input.tagged !== "string")
|
|
1829
|
+
return invalid("invalid_type");
|
|
1830
|
+
const separator = input.tagged.indexOf(":");
|
|
1831
|
+
if (separator < 0)
|
|
1832
|
+
return invalid("digest_encoding_invalid");
|
|
1833
|
+
if (input.tagged.slice(0, separator) !== "sha-256")
|
|
1834
|
+
return invalid("digest_algorithm_unsupported");
|
|
1835
|
+
const declared = strictBase64url(input.tagged.slice(separator + 1));
|
|
1836
|
+
if (!declared.ok || declared.value.length !== 32)
|
|
1837
|
+
return invalid("digest_encoding_invalid");
|
|
1838
|
+
return input.tagged === taggedHash(domain, bytes.value) ? valid({}) : invalid("digest_mismatch");
|
|
1839
|
+
}
|
|
1840
|
+
function executeInputCase(one) {
|
|
1841
|
+
const options = closedCaseOptions(one.input);
|
|
1842
|
+
if (!options.ok)
|
|
1843
|
+
return invalid(options.code);
|
|
1844
|
+
if (Object.hasOwn(one.input, "kind") && Object.hasOwn(one.input, "value") && !caseKindMatches(one.input))
|
|
1845
|
+
return invalid("invalid_type");
|
|
1846
|
+
const artifact = executeArtifactCase(one);
|
|
1847
|
+
if (artifact)
|
|
1848
|
+
return artifact;
|
|
1470
1849
|
const input = one.input;
|
|
1471
1850
|
switch (one.surface) {
|
|
1472
1851
|
case "base64url.decode":
|
|
1473
1852
|
return project(strictBase64url(input.text), (bytes) => ({ bytes_base64url: bytes.toString("base64url") }));
|
|
1474
1853
|
case "json.decode": {
|
|
1475
|
-
if (
|
|
1854
|
+
if ("text" in input)
|
|
1855
|
+
return project(decodeJsonText(input.text, input.limits), jsonProjection);
|
|
1856
|
+
if (!("bytes_base64url" in input))
|
|
1476
1857
|
return invalid("invalid_type");
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
if (!decoded.ok)
|
|
1482
|
-
return invalid(decoded.code);
|
|
1483
|
-
return project(jsonWithinLimits(decoded.value, bytes, input.limits), jsonProjection);
|
|
1858
|
+
const bytes = strictBase64url(input.bytes_base64url);
|
|
1859
|
+
if (!bytes.ok)
|
|
1860
|
+
return invalid(bytes.code);
|
|
1861
|
+
return project(decodeJsonBytes(bytes.value, { ...DEFAULT_LIMITS, ...input.limits }), jsonProjection);
|
|
1484
1862
|
}
|
|
1485
1863
|
case "canonicalization.encode":
|
|
1486
|
-
|
|
1487
|
-
const literal = input.text_value !== undefined ? input.text_value : String(input.value);
|
|
1488
|
-
if (/^-?[0-9]+$/.test(literal) && (BigInt(literal) > MAXIMUM_SAFE_INTEGER || BigInt(literal) < -MAXIMUM_SAFE_INTEGER))
|
|
1489
|
-
return invalid("integer_magnitude");
|
|
1490
|
-
return valid({ text: literal });
|
|
1491
|
-
}
|
|
1492
|
-
if (input.tag === "object") {
|
|
1493
|
-
const names = input.members.map(([key]) => key);
|
|
1494
|
-
if (new Set(names).size !== names.length)
|
|
1495
|
-
return invalid("duplicate_member");
|
|
1496
|
-
return valid({ text: canonical(Object.fromEntries(input.members.map(([key, value]) => [key, value.value]))) });
|
|
1497
|
-
}
|
|
1498
|
-
if (input.tag === "string_codepoint")
|
|
1499
|
-
return invalid("invalid_encoding");
|
|
1500
|
-
if (input.kind === "improper_object")
|
|
1501
|
-
return invalid("invalid_type");
|
|
1502
|
-
return canonical(JSON.parse(input.text)) === input.text ? valid({ text: input.text }) : invalid("non_canonical_bytes");
|
|
1864
|
+
return executeCanonicalCase(input);
|
|
1503
1865
|
case "digest.hash":
|
|
1504
|
-
|
|
1505
|
-
return invalid("invalid_type");
|
|
1506
|
-
if (input.tagged !== undefined) {
|
|
1507
|
-
if (typeof input.tagged !== "string")
|
|
1508
|
-
return invalid("invalid_type");
|
|
1509
|
-
const separator = input.tagged.indexOf(":");
|
|
1510
|
-
if (separator < 1)
|
|
1511
|
-
return invalid("digest_encoding_invalid");
|
|
1512
|
-
if (input.tagged.slice(0, separator) !== "sha-256")
|
|
1513
|
-
return invalid("digest_algorithm_unsupported");
|
|
1514
|
-
if (!/^[A-Za-z0-9_-]{43}$/.test(input.tagged.slice(separator + 1)))
|
|
1515
|
-
return invalid("digest_encoding_invalid");
|
|
1516
|
-
if (input.tagged !== taggedHash(input.domain || "charter_revision_content", Buffer.from(input.bytes_base64url, "base64url")))
|
|
1517
|
-
return invalid("digest_mismatch");
|
|
1518
|
-
}
|
|
1519
|
-
return valid({ algorithm: "sha-256" });
|
|
1866
|
+
return executeDigestCase(input);
|
|
1520
1867
|
case "schema.validate": {
|
|
1521
1868
|
const members = input.members;
|
|
1869
|
+
if (!isRecord(members))
|
|
1870
|
+
return invalid("invalid_type");
|
|
1522
1871
|
if (Object.keys(members).some((key) => key !== "name"))
|
|
1523
1872
|
return invalid("unknown_member");
|
|
1524
1873
|
if (!("name" in members))
|
|
@@ -1531,21 +1880,6 @@ function execute(one) {
|
|
|
1531
1880
|
return invalid("cardinality_violation");
|
|
1532
1881
|
return valid({ members });
|
|
1533
1882
|
}
|
|
1534
|
-
case "party_descriptor.verify": {
|
|
1535
|
-
// A case may name its predecessor history, genesis first; each entry
|
|
1536
|
-
// verifies under the full profile against the one before it.
|
|
1537
|
-
let predecessor = null;
|
|
1538
|
-
for (const compact of (input.predecessor_compacts ?? [])) {
|
|
1539
|
-
const verified = verifyDescriptor(compact, predecessor);
|
|
1540
|
-
if (!verified.ok)
|
|
1541
|
-
throw new Error("corpus predecessor history does not verify");
|
|
1542
|
-
predecessor = verified.facts;
|
|
1543
|
-
}
|
|
1544
|
-
const verified = verifyDescriptor(input.compact, predecessor, { profile: input.profile });
|
|
1545
|
-
if (!verified.ok)
|
|
1546
|
-
return invalid(verified.code);
|
|
1547
|
-
return valid({ descriptor_digest: verified.facts.descriptor_digest, party_id: verified.facts.party_id, descriptor_number: verified.facts.descriptor_number });
|
|
1548
|
-
}
|
|
1549
1883
|
case "descriptor_chain.verify": {
|
|
1550
1884
|
const chain = descriptorChain(input.compacts);
|
|
1551
1885
|
return project(chain, (value) => {
|
|
@@ -1553,15 +1887,11 @@ function execute(one) {
|
|
|
1553
1887
|
});
|
|
1554
1888
|
}
|
|
1555
1889
|
case "charter_revision.decode":
|
|
1556
|
-
return project(revisionFromText(input.text), (revision) =>
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
if (!verified.ok)
|
|
1562
|
-
return invalid(verified.code);
|
|
1563
|
-
return valid({ acceptance_digest: verified.facts.acceptance_digest, revision_digest: verified.facts.claims.revision_digest, party_descriptor_digest: verified.facts.claims.party_descriptor_digest, descriptor_position: verified.facts.descriptor_position });
|
|
1564
|
-
}
|
|
1890
|
+
return project(revisionFromText(input.text), (revision) => {
|
|
1891
|
+
const binding = revision.value.abp_bindings[0];
|
|
1892
|
+
return { revision_digest: revision.digest, revision_number: revision.value.revision_number, precedence_declaration: revision.value.precedence_declaration,
|
|
1893
|
+
abp_binding: binding ? { blueprint_id: binding.blueprint_id, release_number: binding.release_number, content_digest: binding.content_digest, deployment_digest: binding.deployment_digest } : null };
|
|
1894
|
+
});
|
|
1565
1895
|
case "acceptance.equivocation": {
|
|
1566
1896
|
const chain = descriptorChain(input.descriptor_compacts);
|
|
1567
1897
|
if (!chain.ok || !Array.isArray(input.signed_revisions) || input.signed_revisions.length !== 2) {
|
|
@@ -1581,12 +1911,6 @@ function execute(one) {
|
|
|
1581
1911
|
return invalid("acceptance_equivocation_invalid");
|
|
1582
1912
|
return valid({ kind: "acceptance_equivocation", revision_number: facts[0].value.claims.revision_number, revision_digests: facts.map((oneFact) => oneFact.value.claims.revision_digest).sort(), winner: null });
|
|
1583
1913
|
}
|
|
1584
|
-
case "termination.verify": {
|
|
1585
|
-
const verified = verifyTermination(input.compact, input.revision_text, input.descriptor_compacts, { profile: input.profile });
|
|
1586
|
-
if (!verified.ok)
|
|
1587
|
-
return invalid(verified.code);
|
|
1588
|
-
return valid({ termination_digest: verified.facts.termination_digest, governing_revision_digest: verified.facts.claims.governing_revision_digest, party_descriptor_digest: verified.facts.claims.party_descriptor_digest, reason_code: verified.facts.claims.reason_code, descriptor_position: verified.facts.descriptor_position });
|
|
1589
|
-
}
|
|
1590
1914
|
case "chain.verify": {
|
|
1591
1915
|
const chain = chainWithOptions(input, { limits: input.limits });
|
|
1592
1916
|
return project(chain, (facts) => ({ charter_id: facts.charterId, topology: facts.topology, accepted_revision_digests: facts.acceptedDigests, superseded_revision_digests: facts.supersededDigests }));
|
|
@@ -1601,22 +1925,22 @@ function execute(one) {
|
|
|
1601
1925
|
case "governing_revision": {
|
|
1602
1926
|
const chain = chainWithOptions(input, { limits: input.limits });
|
|
1603
1927
|
if (!chain.ok)
|
|
1604
|
-
return invalid(
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
return
|
|
1928
|
+
return invalid(chain.code);
|
|
1929
|
+
if (!Array.isArray(input.queries))
|
|
1930
|
+
return invalid("invalid_type");
|
|
1931
|
+
const revisions = [];
|
|
1932
|
+
for (const query of input.queries) {
|
|
1933
|
+
if (!isRecord(query) || typeof query.at !== "string")
|
|
1934
|
+
return invalid("invalid_type");
|
|
1935
|
+
const at = parseTimestamp(query.at);
|
|
1936
|
+
if (!at)
|
|
1937
|
+
return invalid("timestamp_invalid");
|
|
1938
|
+
revisions.push(governing(chain.value, at));
|
|
1939
|
+
}
|
|
1940
|
+
return valid({ governing_revisions: revisions });
|
|
1617
1941
|
}
|
|
1618
1942
|
default:
|
|
1619
|
-
|
|
1943
|
+
return invalid("invalid_type");
|
|
1620
1944
|
}
|
|
1621
1945
|
}
|
|
1622
1946
|
function corpusDigest(index) {
|
|
@@ -1624,6 +1948,24 @@ function corpusDigest(index) {
|
|
|
1624
1948
|
delete without.corpus_digest;
|
|
1625
1949
|
return taggedHash("corpus_index", Buffer.from(canonical(without)));
|
|
1626
1950
|
}
|
|
1951
|
+
function readCaseJson(text) {
|
|
1952
|
+
return JSON.parse(text, (_key, value, context) => typeof value !== "number" || !context?.source ? value : /[.eE]/.test(context.source) ? new Number(value) :
|
|
1953
|
+
Number.isSafeInteger(value) ? value : BigInt(context.source));
|
|
1954
|
+
}
|
|
1955
|
+
function encodeCaseJson(value) {
|
|
1956
|
+
if (typeof value === "bigint")
|
|
1957
|
+
return value.toString();
|
|
1958
|
+
if (value instanceof Number) {
|
|
1959
|
+
const text = canonical(value);
|
|
1960
|
+
return /^-?[0-9]+$/.test(text) ? `${text}.0` : text;
|
|
1961
|
+
}
|
|
1962
|
+
if (Array.isArray(value))
|
|
1963
|
+
return `[${value.map(encodeCaseJson).join(",")}]`;
|
|
1964
|
+
if (isRecord(value)) {
|
|
1965
|
+
return `{${Object.keys(value).sort().map((key) => `${canonical(key)}:${encodeCaseJson(value[key])}`).join(",")}}`;
|
|
1966
|
+
}
|
|
1967
|
+
return canonical(value);
|
|
1968
|
+
}
|
|
1627
1969
|
export function loadCorpus(root) {
|
|
1628
1970
|
const observedEntries = walk(root);
|
|
1629
1971
|
if (observedEntries.length === 0 || observedEntries.length > MAXIMUM_CORPUS_FILES ||
|
|
@@ -1655,8 +1997,8 @@ export function loadCorpus(root) {
|
|
|
1655
1997
|
throw new Error("case file changed during read");
|
|
1656
1998
|
if (rawHash(bytes) !== String(entry.sha256_base64url))
|
|
1657
1999
|
throw new Error("file hash");
|
|
1658
|
-
const file =
|
|
1659
|
-
if (
|
|
2000
|
+
const file = readCaseJson(bytes.toString("utf8"));
|
|
2001
|
+
if (encodeCaseJson(file) !== bytes.toString("utf8") || file.format !== CASE_FORMAT || file.cases.length !== entry.cases)
|
|
1660
2002
|
throw new Error("case file");
|
|
1661
2003
|
cases.push(...file.cases);
|
|
1662
2004
|
}
|
|
@@ -1854,30 +2196,232 @@ export function decodeArtifact(compact) {
|
|
|
1854
2196
|
}
|
|
1855
2197
|
// Strict signature verification over exact bytes with a base64url public
|
|
1856
2198
|
// key under the registry row for `alg` — the wrong-key guard primitive.
|
|
2199
|
+
const uint8ArrayTag = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
|
|
2200
|
+
function isUint8Array(value) {
|
|
2201
|
+
return ArrayBuffer.isView(value) && uint8ArrayTag.call(value) === "Uint8Array";
|
|
2202
|
+
}
|
|
2203
|
+
const CHAIN_MEMBERS = ["revisions", "acceptances", "descriptors", "terminations"];
|
|
2204
|
+
const PREDECESSOR_MEMBERS = ["lineage", "descriptor_digest", "digest"];
|
|
2205
|
+
const FRAMING_MEMBERS = ["alg", "protectedSegment", "payloadSegment", "message"];
|
|
2206
|
+
const ARTIFACT_LIST_MEMBERS = new Set([...CHAIN_MEMBERS, "lineage"]);
|
|
2207
|
+
const COPY_LIMIT = Symbol();
|
|
2208
|
+
const COPY_CYCLE = Symbol();
|
|
2209
|
+
const typedArrayByteLength = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), "byteLength").get;
|
|
2210
|
+
function boundedMemberNames(value, maximum) {
|
|
2211
|
+
const names = [];
|
|
2212
|
+
for (const name in value) {
|
|
2213
|
+
if (!Object.hasOwn(value, name))
|
|
2214
|
+
continue;
|
|
2215
|
+
if (names.length >= maximum)
|
|
2216
|
+
throw COPY_LIMIT;
|
|
2217
|
+
names.push(name);
|
|
2218
|
+
}
|
|
2219
|
+
return names;
|
|
2220
|
+
}
|
|
2221
|
+
function copyInput(input, limits, mode, members) {
|
|
2222
|
+
const frames = [];
|
|
2223
|
+
const active = new Set();
|
|
2224
|
+
let bytes = 0;
|
|
2225
|
+
const maxBytes = mode === "json" ? limits.max_bytes : limits.max_artifact_set_bytes + limits.max_bytes;
|
|
2226
|
+
let artifactBytes = 0;
|
|
2227
|
+
function count(size) {
|
|
2228
|
+
bytes += size;
|
|
2229
|
+
if (bytes > maxBytes)
|
|
2230
|
+
throw COPY_LIMIT;
|
|
2231
|
+
}
|
|
2232
|
+
function copy(value, depth, artifacts = false, selectedMembers) {
|
|
2233
|
+
if (typeof value === "string") {
|
|
2234
|
+
const size = Buffer.byteLength(value);
|
|
2235
|
+
if (mode === "json" && size > limits.max_string_bytes)
|
|
2236
|
+
throw COPY_LIMIT;
|
|
2237
|
+
count(mode === "json" ? Buffer.byteLength(JSON.stringify(value)) : size);
|
|
2238
|
+
return value;
|
|
2239
|
+
}
|
|
2240
|
+
if (value === null || typeof value !== "object") {
|
|
2241
|
+
if (mode === "json")
|
|
2242
|
+
count(["number", "boolean"].includes(typeof value) || value === null ? JSON.stringify(value).length : 5);
|
|
2243
|
+
return value;
|
|
2244
|
+
}
|
|
2245
|
+
if (ArrayBuffer.isView(value) && !isUint8Array(value))
|
|
2246
|
+
throw new TypeError();
|
|
2247
|
+
if (isUint8Array(value)) {
|
|
2248
|
+
const length = typedArrayByteLength.call(value);
|
|
2249
|
+
if (length > limits.max_bytes)
|
|
2250
|
+
throw COPY_LIMIT;
|
|
2251
|
+
count(length);
|
|
2252
|
+
return Buffer.from(value);
|
|
2253
|
+
}
|
|
2254
|
+
if (value instanceof Number) {
|
|
2255
|
+
const number = Number.prototype.valueOf.call(value);
|
|
2256
|
+
count(JSON.stringify(number).length);
|
|
2257
|
+
return new Number(number);
|
|
2258
|
+
}
|
|
2259
|
+
if (mode === "input" && !Array.isArray(value) && !selectedMembers)
|
|
2260
|
+
throw new TypeError();
|
|
2261
|
+
if (active.has(value))
|
|
2262
|
+
throw COPY_CYCLE;
|
|
2263
|
+
if (depth + 1 > (mode === "json" ? limits.max_depth : LIMIT_MAXIMUMS.max_depth))
|
|
2264
|
+
throw COPY_LIMIT;
|
|
2265
|
+
const array = Array.isArray(value);
|
|
2266
|
+
const length = array ? value.length : 0;
|
|
2267
|
+
if (array && (!Number.isInteger(length) || length < 0 || length > 0xffff_ffff))
|
|
2268
|
+
throw new TypeError();
|
|
2269
|
+
if (array && length > (mode === "json" ? limits.max_array_items : limits.max_artifact_set_items))
|
|
2270
|
+
throw COPY_LIMIT;
|
|
2271
|
+
if (mode === "json")
|
|
2272
|
+
count(2);
|
|
2273
|
+
const keys = array ? null : selectedMembers ? selectedMembers.filter((name) => Object.prototype.propertyIsEnumerable.call(value, name)) : boundedMemberNames(value, limits.max_object_members);
|
|
2274
|
+
const target = array ? [] : Object.create(null);
|
|
2275
|
+
active.add(value);
|
|
2276
|
+
frames.push({ source: value, target, keys, length: array ? length : keys.length, index: 0, depth: depth + 1, artifacts });
|
|
2277
|
+
const order = JSON_MEMBER_ORDER.get(value);
|
|
2278
|
+
if (order)
|
|
2279
|
+
JSON_MEMBER_ORDER.set(target, [...order]);
|
|
2280
|
+
return target;
|
|
2281
|
+
}
|
|
2282
|
+
try {
|
|
2283
|
+
if (mode === "input" && (input === null || typeof input !== "object"))
|
|
2284
|
+
return ok(input);
|
|
2285
|
+
const result = copy(input, 0, mode === "input" && Array.isArray(input), members);
|
|
2286
|
+
while (frames.length) {
|
|
2287
|
+
const frame = frames[frames.length - 1];
|
|
2288
|
+
if (frame.index === frame.length) {
|
|
2289
|
+
active.delete(frame.source);
|
|
2290
|
+
frames.pop();
|
|
2291
|
+
continue;
|
|
2292
|
+
}
|
|
2293
|
+
const index = frame.index++;
|
|
2294
|
+
const key = frame.keys ? frame.keys[index] : String(index);
|
|
2295
|
+
if (mode === "json" && index)
|
|
2296
|
+
count(1);
|
|
2297
|
+
if (mode === "json" && frame.keys) {
|
|
2298
|
+
const size = Buffer.byteLength(key);
|
|
2299
|
+
if (mode === "json" && size > limits.max_string_bytes)
|
|
2300
|
+
throw COPY_LIMIT;
|
|
2301
|
+
count(Buffer.byteLength(JSON.stringify(key)) + 1);
|
|
2302
|
+
}
|
|
2303
|
+
const value = frame.source[key];
|
|
2304
|
+
if (frame.artifacts && typeof value === "string") {
|
|
2305
|
+
artifactBytes += Buffer.byteLength(value);
|
|
2306
|
+
if (artifactBytes > limits.max_artifact_set_bytes)
|
|
2307
|
+
throw COPY_LIMIT;
|
|
2308
|
+
}
|
|
2309
|
+
const nestedMembers = mode === "input" ? key === "limits" ? Object.keys(DEFAULT_LIMITS) :
|
|
2310
|
+
key === "profile" ? ["algorithms", "revisions"] : key === "revisions" && !Array.isArray(value) ? ["min", "max"] : undefined : undefined;
|
|
2311
|
+
frame.target[key] = copy(value, frame.depth, mode === "input" && ARTIFACT_LIST_MEMBERS.has(key), nestedMembers);
|
|
2312
|
+
}
|
|
2313
|
+
return ok(result);
|
|
2314
|
+
}
|
|
2315
|
+
catch (error) {
|
|
2316
|
+
if (error === COPY_LIMIT)
|
|
2317
|
+
return fail(mode === "json" ? "signing_input_invalid" : "limit_exceeded");
|
|
2318
|
+
if (error === COPY_CYCLE && mode === "json")
|
|
2319
|
+
return fail("signing_input_invalid");
|
|
2320
|
+
return fail("invalid_type");
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
function plainInputs(...inputs) {
|
|
2324
|
+
const copies = [];
|
|
2325
|
+
for (const input of inputs) {
|
|
2326
|
+
const copied = copyInput(input, DEFAULT_LIMITS, "input");
|
|
2327
|
+
if (!copied.ok)
|
|
2328
|
+
return copied;
|
|
2329
|
+
copies.push(copied.value);
|
|
2330
|
+
}
|
|
2331
|
+
return ok(copies);
|
|
2332
|
+
}
|
|
2333
|
+
function readOptionMembers(value, names, nested = {}) {
|
|
2334
|
+
if (!isRecord(value))
|
|
2335
|
+
return value;
|
|
2336
|
+
const record = Object.create(null);
|
|
2337
|
+
for (const name of boundedMemberNames(value, LIMIT_MAXIMUMS.max_object_members)) {
|
|
2338
|
+
if (!names.includes(name))
|
|
2339
|
+
record[name] = null;
|
|
2340
|
+
}
|
|
2341
|
+
for (const name of names) {
|
|
2342
|
+
const member = value[name];
|
|
2343
|
+
if (member !== undefined || name in value)
|
|
2344
|
+
record[name] = nested[name] ? nested[name](member) : member;
|
|
2345
|
+
}
|
|
2346
|
+
return record;
|
|
2347
|
+
}
|
|
2348
|
+
function predecessorCopyLimits(limits) {
|
|
2349
|
+
return { ...limits, max_artifact_set_items: LIMIT_MAXIMUMS.max_artifact_set_items, max_artifact_set_bytes: LIMIT_MAXIMUMS.max_artifact_set_bytes };
|
|
2350
|
+
}
|
|
2351
|
+
function retainedInput(value, members) {
|
|
2352
|
+
if (!isRecord(value))
|
|
2353
|
+
return ok(value);
|
|
2354
|
+
const retained = Object.create(null);
|
|
2355
|
+
for (const name of members) {
|
|
2356
|
+
if (Object.prototype.propertyIsEnumerable.call(value, name))
|
|
2357
|
+
retained[name] = value[name];
|
|
2358
|
+
}
|
|
2359
|
+
if (members === CHAIN_MEMBERS && !CHAIN_MEMBERS.every((name) => Array.isArray(retained[name])))
|
|
2360
|
+
return fail("invalid_type");
|
|
2361
|
+
return ok(retained);
|
|
2362
|
+
}
|
|
2363
|
+
function plainVerificationInputs(options, inputs, jsonIndex = -1, predecessorIndex = -1, inputMembers = {}) {
|
|
2364
|
+
try {
|
|
2365
|
+
const selected = readOptionMembers(options, ["limits", "profile", "algorithm"], {
|
|
2366
|
+
limits: (value) => readOptionMembers(value, Object.keys(DEFAULT_LIMITS)),
|
|
2367
|
+
profile: (value) => readOptionMembers(value, ["algorithms", "revisions"], {
|
|
2368
|
+
revisions: (range) => readOptionMembers(range, ["min", "max"]),
|
|
2369
|
+
}),
|
|
2370
|
+
});
|
|
2371
|
+
const settings = resolveOptions(selected);
|
|
2372
|
+
if (!settings.ok)
|
|
2373
|
+
return settings;
|
|
2374
|
+
const copiedOptions = copyInput(selected, { ...MAXIMUM_LIMITS, max_artifact_set_bytes: MAXIMUM_LIMITS.max_bytes }, "input", ["limits", "profile", "algorithm"]);
|
|
2375
|
+
if (!copiedOptions.ok)
|
|
2376
|
+
return copiedOptions;
|
|
2377
|
+
for (const [index, members] of Object.entries(inputMembers)) {
|
|
2378
|
+
const retained = retainedInput(inputs[Number(index)], members);
|
|
2379
|
+
if (!retained.ok)
|
|
2380
|
+
return retained;
|
|
2381
|
+
inputs[Number(index)] = retained.value;
|
|
2382
|
+
}
|
|
2383
|
+
const copies = [];
|
|
2384
|
+
for (let i = 0; i < inputs.length; i++) {
|
|
2385
|
+
const copied = copyInput(inputs[i], i === predecessorIndex ? predecessorCopyLimits(settings.value.limits) : settings.value.limits, i === jsonIndex ? "json" : "input", i === predecessorIndex ? PREDECESSOR_MEMBERS : inputMembers[i]);
|
|
2386
|
+
if (!copied.ok)
|
|
2387
|
+
return copied;
|
|
2388
|
+
copies.push(copied.value);
|
|
2389
|
+
}
|
|
2390
|
+
return ok([...copies, copiedOptions.value]);
|
|
2391
|
+
}
|
|
2392
|
+
catch (_error) {
|
|
2393
|
+
return fail("invalid_type");
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
1857
2396
|
export function verifySignature(message, signature, publicKeyBase64url, alg, options = {}) {
|
|
2397
|
+
const plain = plainVerificationInputs(options, [message, signature, publicKeyBase64url, alg]);
|
|
2398
|
+
if (!plain.ok)
|
|
2399
|
+
return false;
|
|
2400
|
+
[message, signature, publicKeyBase64url, alg, options] = plain.value;
|
|
2401
|
+
if (!isUint8Array(message) || !isUint8Array(signature) || typeof publicKeyBase64url !== "string" || typeof alg !== "string")
|
|
2402
|
+
return false;
|
|
1858
2403
|
const settings = resolveOptions(options);
|
|
1859
2404
|
if (!settings.ok || !settings.value.profile.algorithms.includes(alg) || message.length > settings.value.limits.max_bytes)
|
|
1860
2405
|
return false;
|
|
1861
2406
|
const row = ALG_ROWS.find((one) => one.name === alg);
|
|
1862
2407
|
if (!row)
|
|
1863
2408
|
return false;
|
|
1864
|
-
|
|
1865
|
-
if (key.length !== row.publicKeyBytes)
|
|
2409
|
+
if (!validPublicKey(publicKeyBase64url, row) || !runtimeSupportsAlgorithm(row.keyAlgorithm))
|
|
1866
2410
|
return false;
|
|
1867
2411
|
if (row.keyAlgorithm === "Ed25519")
|
|
1868
|
-
return ed25519(publicKeyBase64url, message, signature);
|
|
1869
|
-
return mldsa(row.keyAlgorithm, publicKeyBase64url, message, signature);
|
|
2412
|
+
return ed25519(publicKeyBase64url, Buffer.from(message), Buffer.from(signature));
|
|
2413
|
+
return mldsa(row.keyAlgorithm, publicKeyBase64url, Buffer.from(message), Buffer.from(signature));
|
|
1870
2414
|
}
|
|
1871
2415
|
const PREDECESSOR_PASS_THROUGH = new Set(["algorithm_outside_profile", "revision_outside_profile", "algorithm_unsupported_on_substrate"]);
|
|
1872
2416
|
function reverifyPredecessor(supplied, settings) {
|
|
1873
2417
|
if (supplied === null)
|
|
1874
2418
|
return ok(null);
|
|
1875
2419
|
if (!isRecord(supplied) || !Array.isArray(supplied.lineage) || supplied.lineage.length === 0)
|
|
1876
|
-
return fail("
|
|
2420
|
+
return fail("invalid_type");
|
|
1877
2421
|
let previous = null;
|
|
1878
2422
|
for (const compact of [...supplied.lineage].reverse()) {
|
|
1879
2423
|
if (typeof compact !== "string")
|
|
1880
|
-
return fail("
|
|
2424
|
+
return fail("invalid_type");
|
|
1881
2425
|
const verified = descriptorFromCompact(compact, previous, settings);
|
|
1882
2426
|
// A profile rejection, or a runtime that cannot check the signature, is
|
|
1883
2427
|
// reported as itself, as a chain view reports it; any other failure is a
|
|
@@ -1892,6 +2436,10 @@ function reverifyPredecessor(supplied, settings) {
|
|
|
1892
2436
|
return ok(previous);
|
|
1893
2437
|
}
|
|
1894
2438
|
export function verifyDescriptor(compact, predecessor = null, options = {}) {
|
|
2439
|
+
const plain = plainVerificationInputs(options, [compact, predecessor], -1, 1);
|
|
2440
|
+
if (!plain.ok)
|
|
2441
|
+
return plain;
|
|
2442
|
+
[compact, predecessor, options] = plain.value;
|
|
1895
2443
|
const settings = resolveOptions(options);
|
|
1896
2444
|
if (!settings.ok)
|
|
1897
2445
|
return settings;
|
|
@@ -1915,6 +2463,10 @@ export function verifyDescriptor(compact, predecessor = null, options = {}) {
|
|
|
1915
2463
|
};
|
|
1916
2464
|
}
|
|
1917
2465
|
export function verifyDescriptorChain(compacts, options = {}) {
|
|
2466
|
+
const plain = plainVerificationInputs(options, [compacts]);
|
|
2467
|
+
if (!plain.ok)
|
|
2468
|
+
return plain;
|
|
2469
|
+
[compacts, options] = plain.value;
|
|
1918
2470
|
const settings = resolveOptions(options);
|
|
1919
2471
|
if (!settings.ok)
|
|
1920
2472
|
return settings;
|
|
@@ -1924,6 +2476,10 @@ export function verifyDescriptorChain(compacts, options = {}) {
|
|
|
1924
2476
|
return { ok: true, facts: verified.value };
|
|
1925
2477
|
}
|
|
1926
2478
|
export function verifyAcceptance(compact, revisionText, descriptorCompacts, options = {}) {
|
|
2479
|
+
const plain = plainVerificationInputs(options, [compact, revisionText, descriptorCompacts]);
|
|
2480
|
+
if (!plain.ok)
|
|
2481
|
+
return plain;
|
|
2482
|
+
[compact, revisionText, descriptorCompacts, options] = plain.value;
|
|
1927
2483
|
const settings = resolveOptions(options);
|
|
1928
2484
|
if (!settings.ok)
|
|
1929
2485
|
return settings;
|
|
@@ -1939,6 +2495,10 @@ export function verifyAcceptance(compact, revisionText, descriptorCompacts, opti
|
|
|
1939
2495
|
return { ok: true, facts: factsRecord({ acceptance_digest: verified.value.digest, claims: verified.value.claims, descriptor_position: verified.value.descriptorPosition, protocol_revision: verified.value.protocol_revision, alg: verified.value.alg }) };
|
|
1940
2496
|
}
|
|
1941
2497
|
export function verifyTermination(compact, revisionText, descriptorCompacts, options = {}) {
|
|
2498
|
+
const plain = plainVerificationInputs(options, [compact, revisionText, descriptorCompacts]);
|
|
2499
|
+
if (!plain.ok)
|
|
2500
|
+
return plain;
|
|
2501
|
+
[compact, revisionText, descriptorCompacts, options] = plain.value;
|
|
1942
2502
|
const settings = resolveOptions(options);
|
|
1943
2503
|
if (!settings.ok)
|
|
1944
2504
|
return settings;
|
|
@@ -1954,12 +2514,20 @@ export function verifyTermination(compact, revisionText, descriptorCompacts, opt
|
|
|
1954
2514
|
return { ok: true, facts: factsRecord({ termination_digest: verified.value.digest, claims: verified.value.claims, descriptor_position: verified.value.descriptorPosition, protocol_revision: verified.value.protocol_revision, alg: verified.value.alg }) };
|
|
1955
2515
|
}
|
|
1956
2516
|
export function verifyChain(chainInput, options = {}) {
|
|
2517
|
+
const plain = plainVerificationInputs(options, [chainInput], -1, -1, { 0: CHAIN_MEMBERS });
|
|
2518
|
+
if (!plain.ok)
|
|
2519
|
+
return plain;
|
|
2520
|
+
[chainInput, options] = plain.value;
|
|
1957
2521
|
const verified = chainWithOptions(chainInput, options);
|
|
1958
2522
|
if (!verified.ok)
|
|
1959
2523
|
return { ok: false, code: verified.code };
|
|
1960
2524
|
return { ok: true, facts: verified.value };
|
|
1961
2525
|
}
|
|
1962
2526
|
export function verifyReceipt(compact, context, options = {}) {
|
|
2527
|
+
const plain = plainVerificationInputs(options, [compact, context], -1, -1, { 1: CHAIN_MEMBERS });
|
|
2528
|
+
if (!plain.ok)
|
|
2529
|
+
return plain;
|
|
2530
|
+
[compact, context, options] = plain.value;
|
|
1963
2531
|
const settings = resolveOptions(options);
|
|
1964
2532
|
if (!settings.ok)
|
|
1965
2533
|
return settings;
|
|
@@ -1987,6 +2555,10 @@ export function verifyReceipt(compact, context, options = {}) {
|
|
|
1987
2555
|
// key operation.
|
|
1988
2556
|
// ---------------------------------------------------------------------------
|
|
1989
2557
|
export function checkSigningClaims(kind, claims) {
|
|
2558
|
+
const plain = copyInput(claims, MAXIMUM_LIMITS, "json");
|
|
2559
|
+
return plain.ok ? signingClaims(kind, plain.value) : plain;
|
|
2560
|
+
}
|
|
2561
|
+
function signingClaims(kind, claims) {
|
|
1990
2562
|
if (!claims || typeof claims !== "object" || Array.isArray(claims))
|
|
1991
2563
|
return { ok: false, code: "invalid_type" };
|
|
1992
2564
|
const error = kind === "descriptor" ? signingDescriptorError(claims) :
|
|
@@ -2089,6 +2661,10 @@ function descriptorClaimsError(payload) {
|
|
|
2089
2661
|
// The unique governing revision in a verified view at one UTC instant -
|
|
2090
2662
|
// a digest, or "contested"/"none" (never silently resolved).
|
|
2091
2663
|
export function governingRevision(chainInput, at, options = {}) {
|
|
2664
|
+
const plain = plainVerificationInputs(options, [chainInput, at], -1, -1, { 0: CHAIN_MEMBERS });
|
|
2665
|
+
if (!plain.ok)
|
|
2666
|
+
return plain;
|
|
2667
|
+
[chainInput, at, options] = plain.value;
|
|
2092
2668
|
const instant = typeof at === "string" ? parseTimestamp(at) : null;
|
|
2093
2669
|
if (!instant)
|
|
2094
2670
|
return { ok: false, code: "invalid_type" };
|
|
@@ -2103,6 +2679,10 @@ export function governingRevision(chainInput, at, options = {}) {
|
|
|
2103
2679
|
// Decode one canonical unsigned Charter Revision: the parsed claims plus
|
|
2104
2680
|
// its content digest, computed over the exact text bytes.
|
|
2105
2681
|
export function decodeCharterRevision(text, options = {}) {
|
|
2682
|
+
const plain = plainVerificationInputs(options, [text]);
|
|
2683
|
+
if (!plain.ok)
|
|
2684
|
+
return plain;
|
|
2685
|
+
[text, options] = plain.value;
|
|
2106
2686
|
const settings = resolveOptions(options);
|
|
2107
2687
|
if (!settings.ok)
|
|
2108
2688
|
return settings;
|
|
@@ -2136,6 +2716,163 @@ export function descriptorDigest(compact) {
|
|
|
2136
2716
|
return decoded;
|
|
2137
2717
|
return { ok: true, digest: decoded.digest };
|
|
2138
2718
|
}
|
|
2719
|
+
let createSigningPlan;
|
|
2720
|
+
let finishSigningPlan;
|
|
2721
|
+
const SIGNING_PLAN_TOKEN = Symbol();
|
|
2722
|
+
/** A prepared input with retained verification context and settings. */
|
|
2723
|
+
export class SigningPlan {
|
|
2724
|
+
kind;
|
|
2725
|
+
key;
|
|
2726
|
+
#input;
|
|
2727
|
+
#publicKey;
|
|
2728
|
+
#context;
|
|
2729
|
+
#limits;
|
|
2730
|
+
#profile;
|
|
2731
|
+
constructor(token, kind, input, key, publicKey, context, settings) {
|
|
2732
|
+
if (token !== SIGNING_PLAN_TOKEN)
|
|
2733
|
+
throw new TypeError("SigningPlan requires prepareSigning");
|
|
2734
|
+
this.kind = kind;
|
|
2735
|
+
this.key = key;
|
|
2736
|
+
this.#input = { ...input, message: Buffer.from(input.message) };
|
|
2737
|
+
this.#publicKey = publicKey;
|
|
2738
|
+
this.#context = context;
|
|
2739
|
+
this.#limits = { ...settings.limits };
|
|
2740
|
+
this.#profile = { ...settings.profile, algorithms: [...settings.profile.algorithms] };
|
|
2741
|
+
Object.freeze(this);
|
|
2742
|
+
}
|
|
2743
|
+
get input() {
|
|
2744
|
+
return { ...this.#input, message: Buffer.from(this.#input.message) };
|
|
2745
|
+
}
|
|
2746
|
+
static {
|
|
2747
|
+
createSigningPlan = (kind, input, key, publicKey, context, settings) => new SigningPlan(SIGNING_PLAN_TOKEN, kind, input, key, publicKey, context, settings);
|
|
2748
|
+
finishSigningPlan = (plan, signature) => {
|
|
2749
|
+
if (!(#input in plan) || !isUint8Array(signature))
|
|
2750
|
+
return fail("invalid_type");
|
|
2751
|
+
const assembled = assembleUnderLimits(plan.#input, Buffer.from(signature), plan.#limits);
|
|
2752
|
+
if (!assembled.ok)
|
|
2753
|
+
return assembled;
|
|
2754
|
+
const options = { limits: plan.#limits, profile: { algorithms: plan.#profile.algorithms, revisions: { min: plan.#profile.minRevision, max: plan.#profile.maxRevision } } };
|
|
2755
|
+
const context = plan.#context;
|
|
2756
|
+
const verified = context.kind === "descriptor" ? verifyDescriptor(assembled.compact, context.predecessor, options) :
|
|
2757
|
+
context.kind === "acceptance" ? verifyAcceptance(assembled.compact, context.revisionText, context.descriptorCompacts, options) :
|
|
2758
|
+
context.kind === "termination" ? verifyTermination(assembled.compact, context.revisionText, context.descriptorCompacts, options) :
|
|
2759
|
+
verifyReceipt(assembled.compact, context.context, options);
|
|
2760
|
+
return verified.ok ? assembled : fail(verified.code);
|
|
2761
|
+
};
|
|
2762
|
+
}
|
|
2763
|
+
}
|
|
2764
|
+
/** Check everything that can be checked before producing a signature. */
|
|
2765
|
+
export function prepareSigning(kind, kid, claims, publicKey, context, options = {}) {
|
|
2766
|
+
const optionInputs = plainVerificationInputs(options, []);
|
|
2767
|
+
if (!optionInputs.ok)
|
|
2768
|
+
return optionInputs;
|
|
2769
|
+
options = optionInputs.value[0];
|
|
2770
|
+
try {
|
|
2771
|
+
const member = (name) => Object.prototype.propertyIsEnumerable.call(context, name);
|
|
2772
|
+
const descriptorContext = context === null || (isRecord(context) && member("lineage") &&
|
|
2773
|
+
(member("descriptor_digest") || member("digest")));
|
|
2774
|
+
const setContext = isRecord(context) && ["revisions", "acceptances", "descriptors", "terminations"].every(member);
|
|
2775
|
+
const validContext = kind === "descriptor" ? descriptorContext :
|
|
2776
|
+
kind === "acceptance" || kind === "termination" ? setContext :
|
|
2777
|
+
kind === "receipt" ? typeof context === "string" || setContext : false;
|
|
2778
|
+
if (!validContext)
|
|
2779
|
+
return fail("invalid_type");
|
|
2780
|
+
}
|
|
2781
|
+
catch (_error) {
|
|
2782
|
+
return fail("invalid_type");
|
|
2783
|
+
}
|
|
2784
|
+
const plain = plainVerificationInputs(options, [kind, kid, claims, publicKey], 2);
|
|
2785
|
+
if (!plain.ok)
|
|
2786
|
+
return plain;
|
|
2787
|
+
[kind, kid, claims, publicKey, options] = plain.value;
|
|
2788
|
+
const settings = resolveOptions(options);
|
|
2789
|
+
if (!settings.ok)
|
|
2790
|
+
return settings;
|
|
2791
|
+
const built = buildSigningInput(kind, kid, claims, options.algorithm, settings.value.limits);
|
|
2792
|
+
if (!built.ok)
|
|
2793
|
+
return built;
|
|
2794
|
+
const row = ALG_ROWS.find((one) => one.name === built.input.alg);
|
|
2795
|
+
if (!validPublicKey(publicKey, row))
|
|
2796
|
+
return fail("signing_key_invalid");
|
|
2797
|
+
try {
|
|
2798
|
+
const retained = retainedInput(context, kind === "descriptor" ? PREDECESSOR_MEMBERS : CHAIN_MEMBERS);
|
|
2799
|
+
if (!retained.ok)
|
|
2800
|
+
return retained;
|
|
2801
|
+
context = retained.value;
|
|
2802
|
+
}
|
|
2803
|
+
catch (_error) {
|
|
2804
|
+
return fail("invalid_type");
|
|
2805
|
+
}
|
|
2806
|
+
const copiedContext = copyInput(context, kind === "descriptor" ? predecessorCopyLimits(settings.value.limits) : settings.value.limits, "input", kind === "descriptor" ? PREDECESSOR_MEMBERS : CHAIN_MEMBERS);
|
|
2807
|
+
if (!copiedContext.ok)
|
|
2808
|
+
return copiedContext;
|
|
2809
|
+
context = copiedContext.value;
|
|
2810
|
+
const provisional = `${built.input.message.toString("utf8")}.${encodeBase64url(Buffer.alloc(row.signatureBytes))}`;
|
|
2811
|
+
let prepared;
|
|
2812
|
+
let verifyWith;
|
|
2813
|
+
if (kind === "descriptor") {
|
|
2814
|
+
const previous = reverifyPredecessor(context, settings.value);
|
|
2815
|
+
if (!previous.ok)
|
|
2816
|
+
return previous;
|
|
2817
|
+
prepared = prepareDescriptor(provisional, previous.value, settings.value);
|
|
2818
|
+
verifyWith = { kind, predecessor: previous.value ? { descriptor_digest: previous.value.digest, lineage: [...previous.value.lineage] } : null };
|
|
2819
|
+
}
|
|
2820
|
+
else if (kind === "acceptance" || kind === "termination") {
|
|
2821
|
+
// Limits cover the whole set; the caller's profile covers the routed view.
|
|
2822
|
+
const chain = chainFromInput(context, { ...settings.value, profile: DEFAULT_SETTINGS.profile });
|
|
2823
|
+
if (!chain.ok)
|
|
2824
|
+
return chain;
|
|
2825
|
+
const refusal = kind === "acceptance" ? acceptanceRefusalInChain(claims, chain.value) : terminationRefusalInChain(claims, chain.value);
|
|
2826
|
+
if (!refusal.ok)
|
|
2827
|
+
return refusal;
|
|
2828
|
+
const routed = signingContextFromSet(new Map(chain.value.revisions.map((one) => [one.digest, one])), chain.value.descriptors, kind === "acceptance" ? claims.revision_digest : claims.governing_revision_digest, claims.party_descriptor_digest, `${kind}_claims_mismatch`);
|
|
2829
|
+
if (!routed.ok)
|
|
2830
|
+
return routed;
|
|
2831
|
+
const revisionText = routed.value.revision.bytes.toString("utf8");
|
|
2832
|
+
const descriptorCompacts = routed.value.chain.descriptors.map((one) => one.compact);
|
|
2833
|
+
const revision = revisionWithProfile(revisionText, settings.value);
|
|
2834
|
+
if (!revision.ok)
|
|
2835
|
+
return revision;
|
|
2836
|
+
const partyChain = descriptorChain(descriptorCompacts, settings.value);
|
|
2837
|
+
if (!partyChain.ok)
|
|
2838
|
+
return partyChain;
|
|
2839
|
+
prepared = kind === "acceptance" ? prepareAcceptance(provisional, revision.value, partyChain.value, settings.value) : prepareTermination(provisional, revision.value, partyChain.value, settings.value);
|
|
2840
|
+
verifyWith = { kind, revisionText, descriptorCompacts: [...descriptorCompacts] };
|
|
2841
|
+
}
|
|
2842
|
+
else if (typeof context === "string") {
|
|
2843
|
+
const revision = revisionWithProfile(context, settings.value);
|
|
2844
|
+
if (!revision.ok)
|
|
2845
|
+
return revision;
|
|
2846
|
+
prepared = prepareReceipt(provisional, { kind: "revision", revision: revision.value }, settings.value);
|
|
2847
|
+
verifyWith = { kind, context };
|
|
2848
|
+
}
|
|
2849
|
+
else {
|
|
2850
|
+
const chain = chainFromInput(context, settings.value);
|
|
2851
|
+
if (!chain.ok)
|
|
2852
|
+
return chain;
|
|
2853
|
+
prepared = prepareReceipt(provisional, { kind: "chain", chain: chain.value }, settings.value);
|
|
2854
|
+
verifyWith = { kind, context: context };
|
|
2855
|
+
}
|
|
2856
|
+
if (!prepared.ok)
|
|
2857
|
+
return prepared;
|
|
2858
|
+
if (!prepared.value.unnamed && !prepared.value.keys.some((key) => keyMatchesRow(key, row) && key.public_key === publicKey))
|
|
2859
|
+
return fail("signing_key_mismatch");
|
|
2860
|
+
if (!runtimeSupportsAlgorithm(row.keyAlgorithm))
|
|
2861
|
+
return fail("algorithm_unsupported_on_substrate");
|
|
2862
|
+
return { ok: true, plan: createSigningPlan(kind, built.input, prepared.value.unnamed ? "unnamed" : "matched", publicKey, verifyWith, settings.value) };
|
|
2863
|
+
}
|
|
2864
|
+
/** Assemble under the retained limits and run public verification. */
|
|
2865
|
+
export function finishSigning(plan, signature) {
|
|
2866
|
+
try {
|
|
2867
|
+
if (!(plan instanceof SigningPlan))
|
|
2868
|
+
return fail("invalid_type");
|
|
2869
|
+
const plain = plainInputs(signature);
|
|
2870
|
+
return plain.ok ? finishSigningPlan(plan, plain.value[0]) : plain;
|
|
2871
|
+
}
|
|
2872
|
+
catch (_error) {
|
|
2873
|
+
return fail("invalid_type");
|
|
2874
|
+
}
|
|
2875
|
+
}
|
|
2139
2876
|
const PRODUCER_TYPES = {
|
|
2140
2877
|
descriptor: "cap+party",
|
|
2141
2878
|
acceptance: "cap+acceptance",
|
|
@@ -2165,7 +2902,7 @@ function jsonValueError(value) {
|
|
|
2165
2902
|
// binding -> kid -> framing -> size gate -> claims schema -> provisional
|
|
2166
2903
|
// decode (the reference producer's exact ordering, so error precedence
|
|
2167
2904
|
// matches too).
|
|
2168
|
-
function buildSigningInput(kind, kid, claims, algorithm) {
|
|
2905
|
+
function buildSigningInput(kind, kid, claims, algorithm, limits = DEFAULT_LIMITS) {
|
|
2169
2906
|
if (typeof algorithm === "undefined")
|
|
2170
2907
|
algorithm = defaultEmissionName();
|
|
2171
2908
|
if (typeof algorithm !== "string" || !(algorithm in emissions())) {
|
|
@@ -2175,8 +2912,15 @@ function buildSigningInput(kind, kid, claims, algorithm) {
|
|
|
2175
2912
|
return { ok: false, code: "signing_input_invalid" };
|
|
2176
2913
|
}
|
|
2177
2914
|
const record = claims;
|
|
2178
|
-
|
|
2915
|
+
try {
|
|
2916
|
+
if (jsonValueError(record))
|
|
2917
|
+
return { ok: false, code: "signing_input_invalid" };
|
|
2918
|
+
}
|
|
2919
|
+
catch (_error) {
|
|
2179
2920
|
return { ok: false, code: "signing_input_invalid" };
|
|
2921
|
+
}
|
|
2922
|
+
if (integerTokenError(record, schemaForType(PRODUCER_TYPES[kind])))
|
|
2923
|
+
return fail("signing_input_invalid");
|
|
2180
2924
|
const revision = record.protocol_revision;
|
|
2181
2925
|
if (typeof revision !== "number" || !Number.isInteger(revision) || revision !== emissions()[algorithm]) {
|
|
2182
2926
|
return { ok: false, code: "signing_input_invalid" };
|
|
@@ -2187,38 +2931,56 @@ function buildSigningInput(kind, kid, claims, algorithm) {
|
|
|
2187
2931
|
const row = ALG_ROWS.find((one) => one.name === algorithm);
|
|
2188
2932
|
if (!row)
|
|
2189
2933
|
return { ok: false, code: "signing_input_invalid" };
|
|
2190
|
-
|
|
2934
|
+
let payloadSegment;
|
|
2935
|
+
try {
|
|
2936
|
+
payloadSegment = encodeBase64url(Buffer.from(canonical(record), "utf8"));
|
|
2937
|
+
}
|
|
2938
|
+
catch (_error) {
|
|
2939
|
+
return { ok: false, code: "signing_input_invalid" };
|
|
2940
|
+
}
|
|
2191
2941
|
const protectedSegment = encodeBase64url(Buffer.from(canonical({ alg: algorithm, kid, typ: PRODUCER_TYPES[kind] }), "utf8"));
|
|
2192
2942
|
const message = Buffer.from(`${protectedSegment}.${payloadSegment}`, "utf8");
|
|
2193
2943
|
const signatureSegmentLength = Math.ceil((row.signatureBytes * 4) / 3);
|
|
2194
|
-
if (message.length + 1 + signatureSegmentLength >
|
|
2944
|
+
if (message.length + 1 + signatureSegmentLength > limits.max_bytes) {
|
|
2195
2945
|
return { ok: false, code: "signing_input_invalid" };
|
|
2196
2946
|
}
|
|
2197
2947
|
// The reference build's decode step: the claims schema gate runs inside
|
|
2198
2948
|
// the producer, after framing and size - the same precedence as
|
|
2199
2949
|
// decode_for_signing inside the Elixir build pipeline.
|
|
2200
|
-
const claimsGate =
|
|
2950
|
+
const claimsGate = signingClaims(kind, record);
|
|
2201
2951
|
if (!claimsGate.ok)
|
|
2202
2952
|
return { ok: false, code: "signing_input_invalid" };
|
|
2203
2953
|
// The provisional check: a zero signature at the row's exact length must
|
|
2204
2954
|
// frame and bind.
|
|
2205
2955
|
const zeroSignature = Buffer.alloc(row.signatureBytes);
|
|
2206
2956
|
const provisional = `${protectedSegment}.${payloadSegment}.${encodeBase64url(zeroSignature)}`;
|
|
2207
|
-
const decoded = decodeSigningArtifact(provisional);
|
|
2957
|
+
const decoded = decodeSigningArtifact(provisional, limits);
|
|
2208
2958
|
if (!decoded.ok)
|
|
2209
2959
|
return { ok: false, code: "signing_input_invalid" };
|
|
2210
2960
|
return { ok: true, input: { alg: algorithm, protectedSegment, payloadSegment, message } };
|
|
2211
2961
|
}
|
|
2212
2962
|
export function descriptorSigningInput(kid, claims, algorithm) {
|
|
2963
|
+
const plain = plainVerificationInputs({}, [kid, claims, algorithm], 1);
|
|
2964
|
+
if (!plain.ok)
|
|
2965
|
+
return plain;
|
|
2966
|
+
[kid, claims, algorithm] = plain.value;
|
|
2213
2967
|
return buildSigningInput("descriptor", kid, claims, algorithm);
|
|
2214
2968
|
}
|
|
2215
2969
|
export function receiptSigningInput(kid, claims, algorithm) {
|
|
2970
|
+
const plain = plainVerificationInputs({}, [kid, claims, algorithm], 1);
|
|
2971
|
+
if (!plain.ok)
|
|
2972
|
+
return plain;
|
|
2973
|
+
[kid, claims, algorithm] = plain.value;
|
|
2216
2974
|
return buildSigningInput("receipt", kid, claims, algorithm);
|
|
2217
2975
|
}
|
|
2218
2976
|
// The reference producer ordering: build (shape, emission binding, framing,
|
|
2219
2977
|
// claims schema) FIRST, then the set-aware R1-R3 refusal checks - so a
|
|
2220
2978
|
// claims defect reports the producer code even when the view is also bad.
|
|
2221
2979
|
export function acceptanceSigningInput(kid, claims, chainInput, algorithm) {
|
|
2980
|
+
const plain = plainVerificationInputs({}, [kid, claims, chainInput, algorithm], 1, -1, { 2: CHAIN_MEMBERS });
|
|
2981
|
+
if (!plain.ok)
|
|
2982
|
+
return plain;
|
|
2983
|
+
[kid, claims, chainInput, algorithm] = plain.value;
|
|
2222
2984
|
const built = buildSigningInput("acceptance", kid, claims, algorithm);
|
|
2223
2985
|
if (!built.ok)
|
|
2224
2986
|
return built;
|
|
@@ -2228,6 +2990,10 @@ export function acceptanceSigningInput(kid, claims, chainInput, algorithm) {
|
|
|
2228
2990
|
return built;
|
|
2229
2991
|
}
|
|
2230
2992
|
export function terminationSigningInput(kid, claims, chainInput, algorithm) {
|
|
2993
|
+
const plain = plainVerificationInputs({}, [kid, claims, chainInput, algorithm], 1, -1, { 2: CHAIN_MEMBERS });
|
|
2994
|
+
if (!plain.ok)
|
|
2995
|
+
return plain;
|
|
2996
|
+
[kid, claims, chainInput, algorithm] = plain.value;
|
|
2231
2997
|
const built = buildSigningInput("termination", kid, claims, algorithm);
|
|
2232
2998
|
if (!built.ok)
|
|
2233
2999
|
return built;
|
|
@@ -2236,19 +3002,26 @@ export function terminationSigningInput(kid, claims, chainInput, algorithm) {
|
|
|
2236
3002
|
return { ok: false, code: refusal.code };
|
|
2237
3003
|
return built;
|
|
2238
3004
|
}
|
|
2239
|
-
function decodeSigningArtifact(compact) {
|
|
2240
|
-
const decoded = decodeJws(compact);
|
|
3005
|
+
function decodeSigningArtifact(compact, limits = DEFAULT_LIMITS) {
|
|
3006
|
+
const decoded = decodeJws(compact, limits);
|
|
2241
3007
|
if (!decoded.ok)
|
|
2242
3008
|
return decoded;
|
|
2243
3009
|
const kind = Object.keys(PRODUCER_TYPES).find((one) => PRODUCER_TYPES[one] === decoded.value.header.typ);
|
|
2244
3010
|
if (!kind)
|
|
2245
3011
|
return fail("signing_input_invalid");
|
|
2246
|
-
const checked =
|
|
3012
|
+
const checked = signingClaims(kind, decoded.value.payload);
|
|
2247
3013
|
return checked.ok ? ok(null) : checked;
|
|
2248
3014
|
}
|
|
2249
3015
|
// Assemble a validated signing input and its exact raw signature (the
|
|
2250
3016
|
// reference assemble): registry-row length, full artifact decode, size gate.
|
|
2251
3017
|
export function assembleCompact(input, signature) {
|
|
3018
|
+
const plain = plainVerificationInputs({}, [input, signature], -1, -1, { 0: FRAMING_MEMBERS });
|
|
3019
|
+
if (!plain.ok)
|
|
3020
|
+
return plain.code === "limit_exceeded" ? fail("signing_input_invalid") : plain;
|
|
3021
|
+
[input, signature] = plain.value;
|
|
3022
|
+
return assembleUnderLimits(input, signature, DEFAULT_LIMITS);
|
|
3023
|
+
}
|
|
3024
|
+
function assembleUnderLimits(input, signature, limits) {
|
|
2252
3025
|
if (!input || typeof input !== "object" || !Buffer.isBuffer(signature))
|
|
2253
3026
|
return { ok: false, code: "invalid_type" };
|
|
2254
3027
|
// The input is a struct the producer minted: the message must BE the
|
|
@@ -2264,9 +3037,9 @@ export function assembleCompact(input, signature) {
|
|
|
2264
3037
|
if (signature.length !== row.signatureBytes)
|
|
2265
3038
|
return { ok: false, code: "signature_invalid" };
|
|
2266
3039
|
const compact = `${input.message.toString("utf8")}.${encodeBase64url(signature)}`;
|
|
2267
|
-
if (Buffer.byteLength(compact) >
|
|
3040
|
+
if (Buffer.byteLength(compact) > limits.max_bytes)
|
|
2268
3041
|
return { ok: false, code: "signing_input_invalid" };
|
|
2269
|
-
const decoded = decodeSigningArtifact(compact);
|
|
3042
|
+
const decoded = decodeSigningArtifact(compact, limits);
|
|
2270
3043
|
if (!decoded.ok)
|
|
2271
3044
|
return { ok: false, code: "signing_input_invalid" };
|
|
2272
3045
|
return { ok: true, compact };
|
|
@@ -2290,7 +3063,7 @@ function refusalChain(chainInput) {
|
|
|
2290
3063
|
if (!chainInput || typeof chainInput !== "object" || Array.isArray(chainInput))
|
|
2291
3064
|
return { invalidSet: "signing_input_invalid" };
|
|
2292
3065
|
const chain = chainFromInput(chainInput);
|
|
2293
|
-
return chain.ok ? { chain: chain.value } : { invalidSet: chain.code
|
|
3066
|
+
return chain.ok ? { chain: chain.value } : { invalidSet: chain.code };
|
|
2294
3067
|
}
|
|
2295
3068
|
function refusalPartyMatches(revision, claims) {
|
|
2296
3069
|
return revision.value.parties.some((party) => party.role === claims.party_role && party.party_descriptor_digest === claims.party_descriptor_digest);
|
|
@@ -2305,13 +3078,19 @@ function refusalAncestorOf(candidate, target, byDigest) {
|
|
|
2305
3078
|
return typeof previous === "string" ? refusalAncestorOf(previous, target, byDigest) : false;
|
|
2306
3079
|
}
|
|
2307
3080
|
export function acceptanceRefusal(claims, chainInput) {
|
|
3081
|
+
const plain = plainVerificationInputs({}, [claims, chainInput], 0, -1, { 1: CHAIN_MEMBERS });
|
|
3082
|
+
if (!plain.ok)
|
|
3083
|
+
return plain;
|
|
3084
|
+
[claims, chainInput] = plain.value;
|
|
2308
3085
|
if (!refusalClaimsShape(claims, ["revision_digest", "charter_id", "party_role", "party_descriptor_digest"], ["revision_number"])) {
|
|
2309
3086
|
return { ok: false, code: "signing_input_invalid" };
|
|
2310
3087
|
}
|
|
2311
3088
|
const resolved = refusalChain(chainInput);
|
|
2312
3089
|
if ("invalidSet" in resolved)
|
|
2313
3090
|
return { ok: false, code: resolved.invalidSet };
|
|
2314
|
-
|
|
3091
|
+
return acceptanceRefusalInChain(claims, resolved.chain);
|
|
3092
|
+
}
|
|
3093
|
+
function acceptanceRefusalInChain(claims, chain) {
|
|
2315
3094
|
// R1 claims-truth: the named revision exists and every coordinate binds.
|
|
2316
3095
|
const revision = chain.revisions.find((one) => one.digest === claims.revision_digest);
|
|
2317
3096
|
if (!revision)
|
|
@@ -2341,6 +3120,10 @@ export function acceptanceRefusal(claims, chainInput) {
|
|
|
2341
3120
|
return { ok: true };
|
|
2342
3121
|
}
|
|
2343
3122
|
export function terminationRefusal(claims, chainInput) {
|
|
3123
|
+
const plain = plainVerificationInputs({}, [claims, chainInput], 0, -1, { 1: CHAIN_MEMBERS });
|
|
3124
|
+
if (!plain.ok)
|
|
3125
|
+
return plain;
|
|
3126
|
+
[claims, chainInput] = plain.value;
|
|
2344
3127
|
if (!refusalClaimsShape(claims, ["governing_revision_digest", "charter_id", "party_role", "party_descriptor_digest", "reason_code"], [])) {
|
|
2345
3128
|
return { ok: false, code: "signing_input_invalid" };
|
|
2346
3129
|
}
|
|
@@ -2350,7 +3133,9 @@ export function terminationRefusal(claims, chainInput) {
|
|
|
2350
3133
|
const resolved = refusalChain(chainInput);
|
|
2351
3134
|
if ("invalidSet" in resolved)
|
|
2352
3135
|
return { ok: false, code: resolved.invalidSet };
|
|
2353
|
-
|
|
3136
|
+
return terminationRefusalInChain(claims, resolved.chain);
|
|
3137
|
+
}
|
|
3138
|
+
function terminationRefusalInChain(claims, chain) {
|
|
2354
3139
|
// R1 claims-truth: the named revision exists, the reason is listed in its
|
|
2355
3140
|
// termination rules, and the signing party is one of its parties.
|
|
2356
3141
|
const revision = chain.revisions.find((one) => one.digest === claims.governing_revision_digest);
|
|
@@ -2365,7 +3150,7 @@ export function terminationRefusal(claims, chainInput) {
|
|
|
2365
3150
|
// R3 governing coverage: the named revision must be THE unique governing
|
|
2366
3151
|
// revision at the notice's own effective instant — a stale or contested
|
|
2367
3152
|
// view refuses.
|
|
2368
|
-
if (governing(chain,
|
|
3153
|
+
if (governing(chain, parseTimestamp(claims.effective_at)) !== revision.digest)
|
|
2369
3154
|
return refused();
|
|
2370
3155
|
return { ok: true };
|
|
2371
3156
|
}
|