@oneaddress/partner-sdk 1.8.0 → 2.1.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 +191 -2
- package/LICENSE +201 -0
- package/README.md +349 -18
- package/dist/{chunk-O2D5XGSN.mjs → chunk-LIHXDUUA.mjs} +441 -14
- package/dist/{chunk-PUB6ZVJ2.mjs → chunk-P55E2BQT.mjs} +503 -64
- package/dist/edge.d.mts +1 -1
- package/dist/edge.d.ts +1 -1
- package/dist/edge.js +835 -62
- package/dist/edge.mjs +2 -2
- package/dist/index.d.mts +487 -41
- package/dist/index.d.ts +487 -41
- package/dist/index.js +966 -76
- package/dist/index.mjs +6 -2
- package/dist/{src-UOJOUUGB.mjs → src-ENJJ6YZU.mjs} +29 -1
- package/package.json +3 -3
|
@@ -3178,6 +3178,64 @@ function isSafeWebhookUrl(url, opts2 = {}) {
|
|
|
3178
3178
|
return { ok: true };
|
|
3179
3179
|
}
|
|
3180
3180
|
|
|
3181
|
+
// ../crypto/src/endpoint-health.ts
|
|
3182
|
+
var ENDPOINT_FAILURE_THRESHOLD = 3;
|
|
3183
|
+
var MISSING_STATUSES = /* @__PURE__ */ new Set([404, 405, 410]);
|
|
3184
|
+
function classifyEndpointProbe(input) {
|
|
3185
|
+
const status = typeof input.httpStatus === "number" && Number.isFinite(input.httpStatus) ? input.httpStatus : null;
|
|
3186
|
+
if (status === null || status === 0) {
|
|
3187
|
+
const detail = input.error ? `: ${input.error}` : "";
|
|
3188
|
+
return {
|
|
3189
|
+
health: "unreachable",
|
|
3190
|
+
dispatchable: false,
|
|
3191
|
+
summary: `No response from the endpoint${detail}. Check the host is running and reachable.`
|
|
3192
|
+
};
|
|
3193
|
+
}
|
|
3194
|
+
if (status >= 200 && status < 300) {
|
|
3195
|
+
return {
|
|
3196
|
+
health: "healthy",
|
|
3197
|
+
dispatchable: true,
|
|
3198
|
+
summary: `Receiver accepted a signed test request (HTTP ${status}).`
|
|
3199
|
+
};
|
|
3200
|
+
}
|
|
3201
|
+
if (status === 401 || status === 403) {
|
|
3202
|
+
return {
|
|
3203
|
+
health: "auth",
|
|
3204
|
+
dispatchable: false,
|
|
3205
|
+
summary: `Receiver rejected our signature (HTTP ${status}). Check the signing secret matches on both sides.`
|
|
3206
|
+
};
|
|
3207
|
+
}
|
|
3208
|
+
if (MISSING_STATUSES.has(status)) {
|
|
3209
|
+
return {
|
|
3210
|
+
health: "missing",
|
|
3211
|
+
dispatchable: false,
|
|
3212
|
+
summary: `Host answered but no receiver is listening at this path (HTTP ${status}). A stopped tunnel or a stale URL looks exactly like this.`
|
|
3213
|
+
};
|
|
3214
|
+
}
|
|
3215
|
+
if (status === 429) {
|
|
3216
|
+
return {
|
|
3217
|
+
health: "throttled",
|
|
3218
|
+
dispatchable: false,
|
|
3219
|
+
summary: `Receiver is up and rate-limiting us (HTTP 429). Back off and retry, or raise the ceiling; nothing is wrong with the URL.`
|
|
3220
|
+
};
|
|
3221
|
+
}
|
|
3222
|
+
if (status >= 500) {
|
|
3223
|
+
return {
|
|
3224
|
+
health: "degraded",
|
|
3225
|
+
dispatchable: false,
|
|
3226
|
+
summary: `Receiver is present but erroring (HTTP ${status}).`
|
|
3227
|
+
};
|
|
3228
|
+
}
|
|
3229
|
+
return {
|
|
3230
|
+
health: "rejected",
|
|
3231
|
+
dispatchable: false,
|
|
3232
|
+
summary: `Receiver refused the request (HTTP ${status}).`
|
|
3233
|
+
};
|
|
3234
|
+
}
|
|
3235
|
+
function countsAsFailure(verdict) {
|
|
3236
|
+
return !verdict.dispatchable;
|
|
3237
|
+
}
|
|
3238
|
+
|
|
3181
3239
|
// ../crypto/src/partner-key-attestation.ts
|
|
3182
3240
|
var PARTNER_KEY_ATTESTATION_V1 = "oneaddress-partner-key-attestation:v1";
|
|
3183
3241
|
var PARTNER_KEY_ATTESTATION_ALG = "ECDSA-P256";
|
|
@@ -3259,6 +3317,8 @@ async function verifyPartnerKeyAttestationWithRoots(input, signatureHex, rootKey
|
|
|
3259
3317
|
|
|
3260
3318
|
// ../crypto/src/registry-head.ts
|
|
3261
3319
|
var REGISTRY_HEAD_V1 = "oneaddress-partner-key-registry-head:v1";
|
|
3320
|
+
var REGISTRY_HEAD_V2 = "oneaddress-partner-key-registry-head:v2";
|
|
3321
|
+
var KNOWN_VERSIONS = /* @__PURE__ */ new Set([REGISTRY_HEAD_V1, REGISTRY_HEAD_V2]);
|
|
3262
3322
|
var REGISTRY_HEAD_ALG = "ECDSA-P256";
|
|
3263
3323
|
function assertNoLineBreak(field, value) {
|
|
3264
3324
|
if (typeof value !== "string" || value.length === 0) {
|
|
@@ -3275,6 +3335,9 @@ function assertMapSafe(field, value) {
|
|
|
3275
3335
|
}
|
|
3276
3336
|
}
|
|
3277
3337
|
function buildRegistryHeadStatement(payload) {
|
|
3338
|
+
if (!KNOWN_VERSIONS.has(payload.version)) {
|
|
3339
|
+
throw new Error(`[registry-head] unknown head version: ${String(payload.version)}`);
|
|
3340
|
+
}
|
|
3278
3341
|
assertNoLineBreak("issued_at", payload.issued_at);
|
|
3279
3342
|
assertNoLineBreak("expires_at", payload.expires_at);
|
|
3280
3343
|
assertMapSafe("root_key_id", payload.root_key_id);
|
|
@@ -3291,14 +3354,42 @@ function buildRegistryHeadStatement(payload) {
|
|
|
3291
3354
|
}
|
|
3292
3355
|
seen.add(partnerId);
|
|
3293
3356
|
}
|
|
3294
|
-
|
|
3295
|
-
|
|
3357
|
+
const lines = [
|
|
3358
|
+
payload.version,
|
|
3296
3359
|
`issued_at:${payload.issued_at}`,
|
|
3297
3360
|
`expires_at:${payload.expires_at}`,
|
|
3298
3361
|
`root_key_id:${payload.root_key_id}`,
|
|
3299
3362
|
`heads:${entries.length}`,
|
|
3300
3363
|
...entries.map(([partnerId, keyId]) => `${partnerId}:${keyId}`)
|
|
3301
|
-
]
|
|
3364
|
+
];
|
|
3365
|
+
if (payload.version === REGISTRY_HEAD_V1) {
|
|
3366
|
+
if (payload.staged && Object.keys(payload.staged).length > 0) {
|
|
3367
|
+
throw new Error("[registry-head] v1 statement cannot carry staged entries");
|
|
3368
|
+
}
|
|
3369
|
+
return lines.join("\n");
|
|
3370
|
+
}
|
|
3371
|
+
const stagedEntries = Object.entries(payload.staged ?? {}).map(([partnerId, entry]) => {
|
|
3372
|
+
assertMapSafe("staged partner_id", partnerId);
|
|
3373
|
+
assertMapSafe("staged key_id", entry?.key_id);
|
|
3374
|
+
assertMapSafe("staged key_sha256", entry?.key_sha256);
|
|
3375
|
+
if (!/^[0-9a-f]{64}$/i.test(entry.key_sha256)) {
|
|
3376
|
+
throw new Error(`[registry-head] staged key_sha256 must be 64 hex chars: ${partnerId}`);
|
|
3377
|
+
}
|
|
3378
|
+
return [partnerId.toLowerCase(), entry.key_id.toLowerCase(), entry.key_sha256.toLowerCase()];
|
|
3379
|
+
});
|
|
3380
|
+
stagedEntries.sort((a, b) => a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0);
|
|
3381
|
+
const stagedSeen = /* @__PURE__ */ new Set();
|
|
3382
|
+
for (const [partnerId] of stagedEntries) {
|
|
3383
|
+
if (stagedSeen.has(partnerId)) {
|
|
3384
|
+
throw new Error(`[registry-head] duplicate partner_id in staged: ${partnerId}`);
|
|
3385
|
+
}
|
|
3386
|
+
stagedSeen.add(partnerId);
|
|
3387
|
+
}
|
|
3388
|
+
lines.push(`staged:${stagedEntries.length}`);
|
|
3389
|
+
for (const [partnerId, keyId, digest] of stagedEntries) {
|
|
3390
|
+
lines.push(`${partnerId}:${keyId}:${digest}`);
|
|
3391
|
+
}
|
|
3392
|
+
return lines.join("\n");
|
|
3302
3393
|
}
|
|
3303
3394
|
function b64ToBytes2(b64) {
|
|
3304
3395
|
const normalised = b64.replace(/-/g, "+").replace(/_/g, "/");
|
|
@@ -3319,7 +3410,7 @@ function hexToBytes4(hex) {
|
|
|
3319
3410
|
}
|
|
3320
3411
|
async function verifyRegistryHead(doc, roots, now = /* @__PURE__ */ new Date()) {
|
|
3321
3412
|
try {
|
|
3322
|
-
if (doc
|
|
3413
|
+
if (!doc || !KNOWN_VERSIONS.has(doc.version)) return "malformed";
|
|
3323
3414
|
if (doc.algorithm !== REGISTRY_HEAD_ALG) return "malformed";
|
|
3324
3415
|
if (typeof doc.signature !== "string" || !/^[0-9a-f]{128}$/i.test(doc.signature)) return "malformed";
|
|
3325
3416
|
if (!doc.heads || typeof doc.heads !== "object") return "malformed";
|
|
@@ -3334,7 +3425,14 @@ async function verifyRegistryHead(doc, roots, now = /* @__PURE__ */ new Date())
|
|
|
3334
3425
|
issued_at: doc.issued_at,
|
|
3335
3426
|
expires_at: doc.expires_at,
|
|
3336
3427
|
root_key_id: doc.root_key_id,
|
|
3337
|
-
heads: doc.heads
|
|
3428
|
+
heads: doc.heads,
|
|
3429
|
+
// Passed through rather than dropped: the staged map is part of what the
|
|
3430
|
+
// root signed on a v2 head, so omitting it here would rebuild different
|
|
3431
|
+
// bytes and report `bad-signature` on a perfectly good document. On a v1
|
|
3432
|
+
// head the builder REFUSES a non-empty map, which lands here as
|
|
3433
|
+
// `malformed` — the right answer for a document claiming staged keys its
|
|
3434
|
+
// signature does not cover.
|
|
3435
|
+
staged: doc.staged
|
|
3338
3436
|
});
|
|
3339
3437
|
const rootKey = await crypto.subtle.importKey(
|
|
3340
3438
|
"spki",
|
|
@@ -3360,6 +3458,307 @@ function headNamesKey(doc, partnerId, keyId) {
|
|
|
3360
3458
|
const named = doc.heads?.[partnerId.toLowerCase()];
|
|
3361
3459
|
return typeof named === "string" && named.toLowerCase() === keyId.toLowerCase();
|
|
3362
3460
|
}
|
|
3461
|
+
function headNamesPartner(doc, partnerId) {
|
|
3462
|
+
return typeof doc.heads?.[partnerId.toLowerCase()] === "string";
|
|
3463
|
+
}
|
|
3464
|
+
async function partnerKeyFingerprint(publicKeyB64) {
|
|
3465
|
+
const digest = await crypto.subtle.digest(
|
|
3466
|
+
"SHA-256",
|
|
3467
|
+
b64ToBytes2(publicKeyB64)
|
|
3468
|
+
);
|
|
3469
|
+
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
3470
|
+
}
|
|
3471
|
+
async function headNamesStagedKey(doc, partnerId, keyId, publicKeyB64) {
|
|
3472
|
+
try {
|
|
3473
|
+
const entry = doc.staged?.[partnerId.toLowerCase()];
|
|
3474
|
+
if (!entry || typeof entry.key_id !== "string" || typeof entry.key_sha256 !== "string") return false;
|
|
3475
|
+
if (entry.key_id.toLowerCase() !== keyId.toLowerCase()) return false;
|
|
3476
|
+
if (!/^[0-9a-f]{64}$/i.test(entry.key_sha256)) return false;
|
|
3477
|
+
return await partnerKeyFingerprint(publicKeyB64) === entry.key_sha256.toLowerCase();
|
|
3478
|
+
} catch {
|
|
3479
|
+
return false;
|
|
3480
|
+
}
|
|
3481
|
+
}
|
|
3482
|
+
|
|
3483
|
+
// ../crypto/src/verify-partner-keys.ts
|
|
3484
|
+
var ACCEPTABLE = /* @__PURE__ */ new Set(["ok", "exempt"]);
|
|
3485
|
+
var EMPTY_BOUND = /* @__PURE__ */ new Set();
|
|
3486
|
+
async function saysRootNotConfigured(res) {
|
|
3487
|
+
if (res.status !== 503) return false;
|
|
3488
|
+
try {
|
|
3489
|
+
const body = await res.json();
|
|
3490
|
+
return typeof body.error === "string" && body.error.includes("root not configured");
|
|
3491
|
+
} catch {
|
|
3492
|
+
return false;
|
|
3493
|
+
}
|
|
3494
|
+
}
|
|
3495
|
+
async function outcomeFor(partnerId, key, reg, bound) {
|
|
3496
|
+
if (!reg.head) return "head-unusable";
|
|
3497
|
+
if (!key.key_signature || !key.root_key_id) {
|
|
3498
|
+
if (!key.countersignature_exempt) return "unsigned";
|
|
3499
|
+
if (headNamesPartner(reg.head, partnerId)) return "exempt-in-head";
|
|
3500
|
+
return bound.has(partnerId) ? "exempt" : "exempt-unbound";
|
|
3501
|
+
}
|
|
3502
|
+
const attested = await verifyPartnerKeyAttestationWithRoots(
|
|
3503
|
+
{
|
|
3504
|
+
partnerId,
|
|
3505
|
+
keyId: key.key_id,
|
|
3506
|
+
algorithm: key.algorithm,
|
|
3507
|
+
publicKeyB64: key.public_key_b64
|
|
3508
|
+
},
|
|
3509
|
+
key.key_signature,
|
|
3510
|
+
key.root_key_id,
|
|
3511
|
+
reg.roots
|
|
3512
|
+
);
|
|
3513
|
+
if (!attested) return "bad-attestation";
|
|
3514
|
+
return headNamesKey(reg.head, partnerId, key.key_id) ? "ok" : "not-in-head";
|
|
3515
|
+
}
|
|
3516
|
+
async function crossoverOutcomeFor(partnerId, crossover, reg) {
|
|
3517
|
+
if (!reg.head) return "head-unusable";
|
|
3518
|
+
return await headNamesStagedKey(reg.head, partnerId, crossover.key_id, crossover.public_key_b64) ? "ok" : "not-authorised";
|
|
3519
|
+
}
|
|
3520
|
+
function createPartnerKeyVerifier(env = {}) {
|
|
3521
|
+
const baseUrl = env.baseUrl ?? "";
|
|
3522
|
+
const report = env.report ?? (() => {
|
|
3523
|
+
});
|
|
3524
|
+
let registryPromise = null;
|
|
3525
|
+
function worthCaching(load) {
|
|
3526
|
+
return load.ok && load.registry.head !== null;
|
|
3527
|
+
}
|
|
3528
|
+
async function loadRegistry() {
|
|
3529
|
+
try {
|
|
3530
|
+
const [rootsRes, headRes] = await Promise.all([
|
|
3531
|
+
fetch(baseUrl + "/.well-known/oneaddress-key-registry.json", { cache: "no-cache" }),
|
|
3532
|
+
fetch(baseUrl + "/.well-known/oneaddress-key-registry-head.json", { cache: "no-cache" })
|
|
3533
|
+
]);
|
|
3534
|
+
if (!rootsRes.ok) {
|
|
3535
|
+
const configured = await saysRootNotConfigured(rootsRes);
|
|
3536
|
+
return {
|
|
3537
|
+
ok: false,
|
|
3538
|
+
failure: {
|
|
3539
|
+
reason: configured ? "roots-not-configured" : "roots-http",
|
|
3540
|
+
status: rootsRes.status
|
|
3541
|
+
}
|
|
3542
|
+
};
|
|
3543
|
+
}
|
|
3544
|
+
let roots;
|
|
3545
|
+
try {
|
|
3546
|
+
const rootsBody = await rootsRes.json();
|
|
3547
|
+
roots = rootsBody.keys ?? [];
|
|
3548
|
+
} catch {
|
|
3549
|
+
return { ok: false, failure: { reason: "roots-unreadable", status: rootsRes.status } };
|
|
3550
|
+
}
|
|
3551
|
+
if (roots.length === 0) {
|
|
3552
|
+
return { ok: false, failure: { reason: "roots-empty", status: rootsRes.status } };
|
|
3553
|
+
}
|
|
3554
|
+
let head = null;
|
|
3555
|
+
let headVerdict = "absent";
|
|
3556
|
+
if (headRes.ok) {
|
|
3557
|
+
head = await headRes.json();
|
|
3558
|
+
headVerdict = await verifyRegistryHead(head, roots);
|
|
3559
|
+
if (headVerdict !== "ok" && headVerdict !== "expired") head = null;
|
|
3560
|
+
}
|
|
3561
|
+
return { ok: true, registry: { roots, head, headVerdict } };
|
|
3562
|
+
} catch {
|
|
3563
|
+
return { ok: false, failure: { reason: "fetch-failed" } };
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
async function verifyActivePartnerKeys(keys, context) {
|
|
3567
|
+
try {
|
|
3568
|
+
const pending = registryPromise ?? (registryPromise = loadRegistry());
|
|
3569
|
+
const load = await pending;
|
|
3570
|
+
if (!worthCaching(load)) registryPromise = null;
|
|
3571
|
+
if (!load.ok) {
|
|
3572
|
+
report(context, { registry: "unavailable", enforced: false, ...load.failure });
|
|
3573
|
+
return { blocked: {}, crossoverBlocked: {} };
|
|
3574
|
+
}
|
|
3575
|
+
const reg = load.registry;
|
|
3576
|
+
let bound = EMPTY_BOUND;
|
|
3577
|
+
if (env.bindSenderHeldKeys) {
|
|
3578
|
+
try {
|
|
3579
|
+
bound = await env.bindSenderHeldKeys(keys);
|
|
3580
|
+
} catch {
|
|
3581
|
+
report(context, { binder: "threw", enforced: true });
|
|
3582
|
+
}
|
|
3583
|
+
}
|
|
3584
|
+
const tally = {
|
|
3585
|
+
"ok": [],
|
|
3586
|
+
"exempt": [],
|
|
3587
|
+
"exempt-in-head": [],
|
|
3588
|
+
"exempt-unbound": [],
|
|
3589
|
+
"unsigned": [],
|
|
3590
|
+
"bad-attestation": [],
|
|
3591
|
+
"not-in-head": [],
|
|
3592
|
+
"head-unusable": [],
|
|
3593
|
+
"error": []
|
|
3594
|
+
};
|
|
3595
|
+
const blocked = {};
|
|
3596
|
+
const crossoverBlocked = {};
|
|
3597
|
+
const crossoverTally = { "ok": [], "not-authorised": [], "head-unusable": [], "error": [] };
|
|
3598
|
+
for (const [partnerId, key] of Object.entries(keys)) {
|
|
3599
|
+
let outcome;
|
|
3600
|
+
try {
|
|
3601
|
+
outcome = await outcomeFor(partnerId, key, reg, bound);
|
|
3602
|
+
} catch {
|
|
3603
|
+
outcome = "error";
|
|
3604
|
+
}
|
|
3605
|
+
tally[outcome].push(partnerId);
|
|
3606
|
+
if (!ACCEPTABLE.has(outcome)) blocked[partnerId] = outcome;
|
|
3607
|
+
if (!key?.crossover) continue;
|
|
3608
|
+
let xo;
|
|
3609
|
+
try {
|
|
3610
|
+
xo = await crossoverOutcomeFor(partnerId, key.crossover, reg);
|
|
3611
|
+
} catch {
|
|
3612
|
+
xo = "error";
|
|
3613
|
+
}
|
|
3614
|
+
crossoverTally[xo].push(partnerId);
|
|
3615
|
+
if (xo !== "ok") crossoverBlocked[partnerId] = xo;
|
|
3616
|
+
}
|
|
3617
|
+
if (Object.keys(crossoverBlocked).length > 0) {
|
|
3618
|
+
const failedXo = Object.keys(crossoverTally).filter((o) => o !== "ok" && crossoverTally[o].length > 0);
|
|
3619
|
+
report(context, {
|
|
3620
|
+
headVerdict: reg.headVerdict,
|
|
3621
|
+
enforced: true,
|
|
3622
|
+
surface: "crossover",
|
|
3623
|
+
...Object.fromEntries(failedXo.map((o) => [`crossover-${o}`, crossoverTally[o]]))
|
|
3624
|
+
});
|
|
3625
|
+
}
|
|
3626
|
+
if (Object.keys(blocked).length > 0) {
|
|
3627
|
+
const failed = Object.keys(tally).filter((o) => !ACCEPTABLE.has(o) && tally[o].length > 0);
|
|
3628
|
+
report(context, {
|
|
3629
|
+
headVerdict: reg.headVerdict,
|
|
3630
|
+
enforced: true,
|
|
3631
|
+
total: Object.keys(keys).length,
|
|
3632
|
+
ok: tally.ok.length,
|
|
3633
|
+
// Counted, not listed: an operator reading a real finding needs to know
|
|
3634
|
+
// how many keys were skipped as exempt, without the ids adding noise.
|
|
3635
|
+
exempt: tally.exempt.length,
|
|
3636
|
+
...Object.fromEntries(failed.map((o) => [o, tally[o]]))
|
|
3637
|
+
});
|
|
3638
|
+
}
|
|
3639
|
+
return { blocked, crossoverBlocked };
|
|
3640
|
+
} catch {
|
|
3641
|
+
report(context, { registry: "verifier-error", enforced: false });
|
|
3642
|
+
return { blocked: {}, crossoverBlocked: {} };
|
|
3643
|
+
}
|
|
3644
|
+
}
|
|
3645
|
+
function __resetRegistryCacheForTests() {
|
|
3646
|
+
registryPromise = null;
|
|
3647
|
+
}
|
|
3648
|
+
return { verifyActivePartnerKeys, __resetRegistryCacheForTests };
|
|
3649
|
+
}
|
|
3650
|
+
var PARTNER_KEY_REFUSED_MESSAGE = "Some services are temporarily unavailable and were not updated. Please try again in a moment.";
|
|
3651
|
+
|
|
3652
|
+
// ../crypto/src/sender-held-keys.ts
|
|
3653
|
+
var SENDER_HELD_KEY_ALGORITHM = "ECDH-P256-AES-GCM-256";
|
|
3654
|
+
function memorySenderHeldKeyStore() {
|
|
3655
|
+
const keys = /* @__PURE__ */ new Map();
|
|
3656
|
+
return {
|
|
3657
|
+
all: async () => [...keys.values()],
|
|
3658
|
+
put: async (key) => {
|
|
3659
|
+
keys.set(key.keyId, key);
|
|
3660
|
+
}
|
|
3661
|
+
};
|
|
3662
|
+
}
|
|
3663
|
+
function toB64(bytes) {
|
|
3664
|
+
let s = "";
|
|
3665
|
+
for (const b of bytes) s += String.fromCharCode(b);
|
|
3666
|
+
return btoa(s);
|
|
3667
|
+
}
|
|
3668
|
+
async function keyIdForPublicKey(spki) {
|
|
3669
|
+
const d = new Uint8Array(await globalThis.crypto.subtle.digest("SHA-256", spki)).slice(0, 16);
|
|
3670
|
+
d[6] = d[6] & 15 | 128;
|
|
3671
|
+
d[8] = d[8] & 63 | 128;
|
|
3672
|
+
const h = [...d].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
3673
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
3674
|
+
}
|
|
3675
|
+
function createSenderHeldKeyBinder(storeFor) {
|
|
3676
|
+
let pending = null;
|
|
3677
|
+
async function mint() {
|
|
3678
|
+
if (typeof globalThis.crypto === "undefined" || !globalThis.crypto.subtle) return null;
|
|
3679
|
+
let store = storeFor();
|
|
3680
|
+
try {
|
|
3681
|
+
const existing = await store.all();
|
|
3682
|
+
if (existing.length > 0) {
|
|
3683
|
+
return [...existing].sort((a, b) => b.createdAt.localeCompare(a.createdAt))[0];
|
|
3684
|
+
}
|
|
3685
|
+
} catch {
|
|
3686
|
+
store = memorySenderHeldKeyStore();
|
|
3687
|
+
}
|
|
3688
|
+
const pair = await globalThis.crypto.subtle.generateKey(
|
|
3689
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
3690
|
+
false,
|
|
3691
|
+
["deriveBits"]
|
|
3692
|
+
);
|
|
3693
|
+
const spki = new Uint8Array(await globalThis.crypto.subtle.exportKey("spki", pair.publicKey));
|
|
3694
|
+
const key = {
|
|
3695
|
+
keyId: await keyIdForPublicKey(spki),
|
|
3696
|
+
publicKeyB64: toB64(spki),
|
|
3697
|
+
privateKey: pair.privateKey,
|
|
3698
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
3699
|
+
};
|
|
3700
|
+
try {
|
|
3701
|
+
await store.put(key);
|
|
3702
|
+
} catch {
|
|
3703
|
+
}
|
|
3704
|
+
return key;
|
|
3705
|
+
}
|
|
3706
|
+
function getOrCreate() {
|
|
3707
|
+
pending ??= mint().catch(() => {
|
|
3708
|
+
pending = null;
|
|
3709
|
+
return null;
|
|
3710
|
+
});
|
|
3711
|
+
return pending;
|
|
3712
|
+
}
|
|
3713
|
+
const bind = async (keys) => {
|
|
3714
|
+
const bound = /* @__PURE__ */ new Set();
|
|
3715
|
+
const flagged = Object.entries(keys).filter(([, k]) => k?.countersignature_exempt === true);
|
|
3716
|
+
if (flagged.length === 0) return bound;
|
|
3717
|
+
const own = await getOrCreate();
|
|
3718
|
+
if (!own) return bound;
|
|
3719
|
+
for (const [partnerId, k] of flagged) {
|
|
3720
|
+
k.key_id = own.keyId;
|
|
3721
|
+
k.public_key_b64 = own.publicKeyB64;
|
|
3722
|
+
k.algorithm = SENDER_HELD_KEY_ALGORITHM;
|
|
3723
|
+
k.key_signature = null;
|
|
3724
|
+
k.root_key_id = null;
|
|
3725
|
+
delete k.crossover;
|
|
3726
|
+
bound.add(partnerId);
|
|
3727
|
+
}
|
|
3728
|
+
return bound;
|
|
3729
|
+
};
|
|
3730
|
+
return Object.assign(bind, { getOrCreate });
|
|
3731
|
+
}
|
|
3732
|
+
async function openSealedWithSenderHeldKey(share, envelope, partnerId, store) {
|
|
3733
|
+
let keys;
|
|
3734
|
+
try {
|
|
3735
|
+
keys = await store.all();
|
|
3736
|
+
} catch {
|
|
3737
|
+
return { ok: false, reason: "no-key" };
|
|
3738
|
+
}
|
|
3739
|
+
const key = keys.find((k) => k.keyId === share.key_id);
|
|
3740
|
+
if (!key) return { ok: false, reason: "no-key" };
|
|
3741
|
+
try {
|
|
3742
|
+
const data = await decryptSession(share, envelope, {
|
|
3743
|
+
name: "this device",
|
|
3744
|
+
async deriveSharedSecret(epkSpki) {
|
|
3745
|
+
const epk = await globalThis.crypto.subtle.importKey(
|
|
3746
|
+
"spki",
|
|
3747
|
+
epkSpki,
|
|
3748
|
+
{ name: "ECDH", namedCurve: "P-256" },
|
|
3749
|
+
false,
|
|
3750
|
+
[]
|
|
3751
|
+
);
|
|
3752
|
+
return new Uint8Array(
|
|
3753
|
+
await globalThis.crypto.subtle.deriveBits({ name: "ECDH", public: epk }, key.privateKey, 256)
|
|
3754
|
+
);
|
|
3755
|
+
}
|
|
3756
|
+
}, partnerId);
|
|
3757
|
+
return { ok: true, data };
|
|
3758
|
+
} catch {
|
|
3759
|
+
return { ok: false, reason: "unreadable" };
|
|
3760
|
+
}
|
|
3761
|
+
}
|
|
3363
3762
|
|
|
3364
3763
|
// ../crypto/src/index.ts
|
|
3365
3764
|
function b64ToBytes3(b64) {
|
|
@@ -3381,7 +3780,15 @@ function keyB64ToBytes(key) {
|
|
|
3381
3780
|
function bytesToB64(bytes) {
|
|
3382
3781
|
return btoa(String.fromCodePoint(...bytes));
|
|
3383
3782
|
}
|
|
3384
|
-
var subtle = () =>
|
|
3783
|
+
var subtle = () => {
|
|
3784
|
+
const webcrypto = globalThis.crypto;
|
|
3785
|
+
if (!webcrypto?.subtle) {
|
|
3786
|
+
throw new Error(
|
|
3787
|
+
"Web Crypto is unavailable in this runtime, so no encryption or key operation can run. In Node this needs version 19 or later, where globalThis.crypto is enabled by default (Node 18 has it behind --experimental-global-webcrypto, and is itself end-of-life). Check with `node -v` and upgrade, or re-run with NODE_OPTIONS=--experimental-global-webcrypto as a one-off."
|
|
3788
|
+
);
|
|
3789
|
+
}
|
|
3790
|
+
return webcrypto.subtle;
|
|
3791
|
+
};
|
|
3385
3792
|
async function importP256PublicKey(keyBytes) {
|
|
3386
3793
|
if (keyBytes.length === 65 && keyBytes[0] === 4) {
|
|
3387
3794
|
return subtle().importKey(
|
|
@@ -3761,21 +4168,21 @@ async function encryptSessionForAll(partners) {
|
|
|
3761
4168
|
);
|
|
3762
4169
|
return Object.fromEntries(results.map((r) => [r.partner_id, r]));
|
|
3763
4170
|
}
|
|
3764
|
-
async function decryptSession(share, envelopeB64, privateKeyB64, partnerId) {
|
|
4171
|
+
async function decryptSession(share, envelopeB64, privateKeyB64, partnerId, expectedAlg) {
|
|
3765
4172
|
if (share.alg !== "ECDH-ES+HKDF-SHA256+A256GCM" && share.alg !== "RSA-OAEP-256+A256GCM" && share.alg !== "MLKEM768X25519+HKDF-SHA256+A256GCM") {
|
|
3766
4173
|
throw new Error(`decryptSession: unsupported alg ${share.alg}`);
|
|
3767
4174
|
}
|
|
3768
4175
|
if (share.v !== 2) {
|
|
3769
4176
|
throw new Error(`decryptSession: unsupported share version ${share.v}`);
|
|
3770
4177
|
}
|
|
4178
|
+
if (expectedAlg && share.alg !== expectedAlg) {
|
|
4179
|
+
throw new Error(
|
|
4180
|
+
`decryptSession: algorithm downgrade - the dispatch wraps SK_n as ${share.alg}, but this key is provisioned for ${expectedAlg}. Refusing rather than attempting a different wrap than the recipient chose.`
|
|
4181
|
+
);
|
|
4182
|
+
}
|
|
3771
4183
|
const ops = typeof privateKeyB64 === "string" ? null : privateKeyB64;
|
|
3772
4184
|
if (ops) {
|
|
3773
|
-
|
|
3774
|
-
throw new Error(
|
|
3775
|
-
`decryptSession: key source "${ops.name}" performs private-key operations without releasing the key, which no KMS offers for ML-KEM. Use an exportable key source for post-quantum shares.`
|
|
3776
|
-
);
|
|
3777
|
-
}
|
|
3778
|
-
const needed = share.alg === "RSA-OAEP-256+A256GCM" ? "decryptRsaOaep" : "deriveSharedSecret";
|
|
4185
|
+
const needed = share.alg === "RSA-OAEP-256+A256GCM" ? "decryptRsaOaep" : share.alg === "MLKEM768X25519+HKDF-SHA256+A256GCM" ? "decapsulateXWing" : "deriveSharedSecret";
|
|
3779
4186
|
if (typeof ops[needed] !== "function") {
|
|
3780
4187
|
throw new Error(
|
|
3781
4188
|
`decryptSession: key source "${ops.name}" cannot do ${needed}, which ${share.alg} requires.`
|
|
@@ -3787,7 +4194,13 @@ async function decryptSession(share, envelopeB64, privateKeyB64, partnerId) {
|
|
|
3787
4194
|
if (!share.epk) {
|
|
3788
4195
|
throw new Error("decryptSession: hybrid share missing epk (KEM ciphertext)");
|
|
3789
4196
|
}
|
|
3790
|
-
const
|
|
4197
|
+
const kemCiphertext = b64UrlToBytes(share.epk);
|
|
4198
|
+
const sharedSecret = ops ? toArrayBuffer(await ops.decapsulateXWing(kemCiphertext)) : toArrayBuffer(XWing.decapsulate(kemCiphertext, b64ToBytes3(privateKeyB64)));
|
|
4199
|
+
if (ops && sharedSecret.byteLength !== 32) {
|
|
4200
|
+
throw new Error(
|
|
4201
|
+
`decryptSession: key source "${ops.name}" returned a ${sharedSecret.byteLength}-byte shared secret; X-Wing decapsulation is 32 bytes with no KDF applied.`
|
|
4202
|
+
);
|
|
4203
|
+
}
|
|
3791
4204
|
const wrappedBytes = b64UrlToBytes(share.wrapped_key);
|
|
3792
4205
|
if (wrappedBytes.length < WRAP_SALT_LENGTH + WRAP_IV_LENGTH + 16) {
|
|
3793
4206
|
throw new Error("decryptSession: wrapped_key too short");
|
|
@@ -4001,6 +4414,9 @@ export {
|
|
|
4001
4414
|
isFreshTimestamp,
|
|
4002
4415
|
isIpLiteralHost,
|
|
4003
4416
|
isSafeWebhookUrl,
|
|
4417
|
+
ENDPOINT_FAILURE_THRESHOLD,
|
|
4418
|
+
classifyEndpointProbe,
|
|
4419
|
+
countsAsFailure,
|
|
4004
4420
|
PARTNER_KEY_ATTESTATION_V1,
|
|
4005
4421
|
PARTNER_KEY_ATTESTATION_ALG,
|
|
4006
4422
|
buildPartnerKeyAttestation,
|
|
@@ -4009,10 +4425,21 @@ export {
|
|
|
4009
4425
|
verifyPartnerKeyAttestation,
|
|
4010
4426
|
verifyPartnerKeyAttestationWithRoots,
|
|
4011
4427
|
REGISTRY_HEAD_V1,
|
|
4428
|
+
REGISTRY_HEAD_V2,
|
|
4012
4429
|
REGISTRY_HEAD_ALG,
|
|
4013
4430
|
buildRegistryHeadStatement,
|
|
4014
4431
|
verifyRegistryHead,
|
|
4015
4432
|
headNamesKey,
|
|
4433
|
+
headNamesPartner,
|
|
4434
|
+
partnerKeyFingerprint,
|
|
4435
|
+
headNamesStagedKey,
|
|
4436
|
+
createPartnerKeyVerifier,
|
|
4437
|
+
PARTNER_KEY_REFUSED_MESSAGE,
|
|
4438
|
+
SENDER_HELD_KEY_ALGORITHM,
|
|
4439
|
+
memorySenderHeldKeyStore,
|
|
4440
|
+
keyIdForPublicKey,
|
|
4441
|
+
createSenderHeldKeyBinder,
|
|
4442
|
+
openSealedWithSenderHeldKey,
|
|
4016
4443
|
normaliseAddress,
|
|
4017
4444
|
generatePartnerKeyPair,
|
|
4018
4445
|
generateHybridPartnerKeyPair,
|