@calimero-network/mero-js 19.21.0 → 19.23.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/dist/admin-api/admin-types.d.ts +23 -1
- package/dist/index.browser.mjs +2 -2
- package/dist/index.browser.mjs.map +4 -4
- package/dist/index.cjs +359 -138
- package/dist/index.cjs.map +4 -4
- package/dist/index.mjs +359 -138
- package/dist/index.mjs.map +4 -4
- package/dist/sealed/index.d.ts +1 -1
- package/dist/sealed/index.js +1 -1
- package/dist/sealed/noise.d.ts +44 -0
- package/dist/sealed/noise.js +137 -0
- package/dist/sealed/sealed.d.ts +60 -40
- package/dist/sealed/sealed.js +270 -126
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -26,6 +26,7 @@ __export(index_exports, {
|
|
|
26
26
|
CloudClient: () => CloudClient,
|
|
27
27
|
DEFAULT_LOCAL_NODE_PORTS: () => DEFAULT_LOCAL_NODE_PORTS,
|
|
28
28
|
EphemeralClient: () => EphemeralClient,
|
|
29
|
+
HANDSHAKE_PATH: () => HANDSHAKE_PATH,
|
|
29
30
|
HTTPError: () => HTTPError,
|
|
30
31
|
IntentRefusedError: () => IntentRefusedError,
|
|
31
32
|
LocalStorageTokenStore: () => LocalStorageTokenStore,
|
|
@@ -2862,12 +2863,12 @@ var CERT_DOMAIN = new TextEncoder().encode("calimero.device.cert.v1");
|
|
|
2862
2863
|
var ACCOUNT_ID_DOMAIN = new TextEncoder().encode("calimero.account.genesis.v1");
|
|
2863
2864
|
var DEVICE_ID_DOMAIN = new TextEncoder().encode("calimero.device.id.v1");
|
|
2864
2865
|
var ACCOUNT_GENESIS_VERSION = 2;
|
|
2865
|
-
async function mintDeviceId(account,
|
|
2866
|
-
if (
|
|
2867
|
-
throw new Error(`nonce must be 16 bytes, got ${
|
|
2866
|
+
async function mintDeviceId(account, nonce2) {
|
|
2867
|
+
if (nonce2.length !== 16) {
|
|
2868
|
+
throw new Error(`nonce must be 16 bytes, got ${nonce2.length}`);
|
|
2868
2869
|
}
|
|
2869
2870
|
const accountBytes = fromHex(account, "account", 32);
|
|
2870
|
-
return hex(await domainHash(DEVICE_ID_DOMAIN, [accountBytes,
|
|
2871
|
+
return hex(await domainHash(DEVICE_ID_DOMAIN, [accountBytes, nonce2]));
|
|
2871
2872
|
}
|
|
2872
2873
|
async function deviceCertPayload(input) {
|
|
2873
2874
|
return domainHash(CERT_DOMAIN, [
|
|
@@ -5097,12 +5098,12 @@ function base64(bytes) {
|
|
|
5097
5098
|
var ROUTING_PROOF_DOMAIN = new TextEncoder().encode(
|
|
5098
5099
|
"calimero.mdma.routing-read.v1\0"
|
|
5099
5100
|
);
|
|
5100
|
-
async function signRoutingChallenge(
|
|
5101
|
+
async function signRoutingChallenge(nonce2, deviceSecret) {
|
|
5101
5102
|
fromHex(deviceSecret, "deviceSecret", 32);
|
|
5102
5103
|
const key = await importSigningKey(deviceSecret);
|
|
5103
5104
|
const message = concat(
|
|
5104
5105
|
ROUTING_PROOF_DOMAIN,
|
|
5105
|
-
new TextEncoder().encode(
|
|
5106
|
+
new TextEncoder().encode(nonce2)
|
|
5106
5107
|
);
|
|
5107
5108
|
const signature = new Uint8Array(
|
|
5108
5109
|
await crypto.subtle.sign({ name: "Ed25519" }, key, message)
|
|
@@ -5192,11 +5193,11 @@ var CloudClient = class {
|
|
|
5192
5193
|
*/
|
|
5193
5194
|
async signInWithAccount(rootSecret) {
|
|
5194
5195
|
const root = await accountRootFromSecret(rootSecret);
|
|
5195
|
-
const { nonce } = await this.getAccountLoginChallenge();
|
|
5196
|
+
const { nonce: nonce2 } = await this.getAccountLoginChallenge();
|
|
5196
5197
|
return this.submitAccountLogin({
|
|
5197
5198
|
rootPublicKey: root.publicKey,
|
|
5198
|
-
nonce,
|
|
5199
|
-
signature: await signAccountLogin({ rootSecret, nonce })
|
|
5199
|
+
nonce: nonce2,
|
|
5200
|
+
signature: await signAccountLogin({ rootSecret, nonce: nonce2 })
|
|
5200
5201
|
});
|
|
5201
5202
|
}
|
|
5202
5203
|
/**
|
|
@@ -5615,12 +5616,12 @@ var CloudClient = class {
|
|
|
5615
5616
|
*/
|
|
5616
5617
|
async linkAccount(rootSecret) {
|
|
5617
5618
|
const root = await accountRootFromSecret(rootSecret);
|
|
5618
|
-
const { nonce } = await this.getAccountLinkChallenge();
|
|
5619
|
+
const { nonce: nonce2 } = await this.getAccountLinkChallenge();
|
|
5619
5620
|
return this.submitAccountLink({
|
|
5620
5621
|
accountId: root.accountId,
|
|
5621
5622
|
rootPublicKey: root.publicKey,
|
|
5622
|
-
nonce,
|
|
5623
|
-
signature: await signAccountLink({ rootSecret, nonce })
|
|
5623
|
+
nonce: nonce2,
|
|
5624
|
+
signature: await signAccountLink({ rootSecret, nonce: nonce2 })
|
|
5624
5625
|
});
|
|
5625
5626
|
}
|
|
5626
5627
|
/**
|
|
@@ -6305,7 +6306,7 @@ async function signWarrant(input) {
|
|
|
6305
6306
|
const publicKey = await derivePublicKey(input.deviceSecret);
|
|
6306
6307
|
const method = new TextEncoder().encode(input.method);
|
|
6307
6308
|
const commitment = await intentHash(input.method, input.argsJson);
|
|
6308
|
-
const
|
|
6309
|
+
const nonce2 = u64le(input.nonce);
|
|
6309
6310
|
const notAfter = u64le(input.notAfter);
|
|
6310
6311
|
const preimage = await domainHash(SIGN_DOMAIN2, [
|
|
6311
6312
|
context,
|
|
@@ -6319,7 +6320,7 @@ async function signWarrant(input) {
|
|
|
6319
6320
|
...accountHeads,
|
|
6320
6321
|
u64le(governanceFloor.length),
|
|
6321
6322
|
...governanceFloor,
|
|
6322
|
-
|
|
6323
|
+
nonce2,
|
|
6323
6324
|
notAfter
|
|
6324
6325
|
]);
|
|
6325
6326
|
const signature = new Uint8Array(
|
|
@@ -6339,7 +6340,7 @@ async function signWarrant(input) {
|
|
|
6339
6340
|
...accountHeads,
|
|
6340
6341
|
u32le(governanceFloor.length),
|
|
6341
6342
|
...governanceFloor,
|
|
6342
|
-
|
|
6343
|
+
nonce2,
|
|
6343
6344
|
notAfter,
|
|
6344
6345
|
signature
|
|
6345
6346
|
)
|
|
@@ -6396,7 +6397,7 @@ var RelayClient = class {
|
|
|
6396
6397
|
*/
|
|
6397
6398
|
async execute(contextId, method, argsJson = {}) {
|
|
6398
6399
|
const executor = this.executorAccount ?? (await this.describe(contextId)).executorAccount;
|
|
6399
|
-
const
|
|
6400
|
+
const nonce2 = await this.config.nonces.next();
|
|
6400
6401
|
const ttl = this.config.ttlSeconds ?? DEFAULT_TTL_SECONDS;
|
|
6401
6402
|
const warrant = await signWarrant({
|
|
6402
6403
|
context: contextId,
|
|
@@ -6404,7 +6405,7 @@ var RelayClient = class {
|
|
|
6404
6405
|
executor,
|
|
6405
6406
|
method,
|
|
6406
6407
|
argsJson,
|
|
6407
|
-
nonce,
|
|
6408
|
+
nonce: nonce2,
|
|
6408
6409
|
notAfter: BigInt(Math.floor(Date.now() / 1e3)) + BigInt(ttl),
|
|
6409
6410
|
deviceSecret: this.config.deviceSecret
|
|
6410
6411
|
});
|
|
@@ -6594,14 +6595,10 @@ async function defaultNonceSource(deviceSecret) {
|
|
|
6594
6595
|
}
|
|
6595
6596
|
}
|
|
6596
6597
|
|
|
6597
|
-
// src/sealed/
|
|
6598
|
-
var
|
|
6599
|
-
var
|
|
6600
|
-
var
|
|
6601
|
-
var NONCE_LEN = 12;
|
|
6602
|
-
var REQUEST_DOMAIN = "calimero/sealed-http/v1/request";
|
|
6603
|
-
var RESPONSE_DOMAIN = "calimero/sealed-http/v1/response";
|
|
6604
|
-
var TRANSPORT_BINDING_DOMAIN = "calimero.tee-attest.transport-key.v1";
|
|
6598
|
+
// src/sealed/noise.ts
|
|
6599
|
+
var PROTOCOL_NAME = "Noise_NK_25519_AESGCM_SHA256";
|
|
6600
|
+
var DH_LEN = 32;
|
|
6601
|
+
var TAG_LEN = 16;
|
|
6605
6602
|
var PKCS8_X25519_PREFIX = new Uint8Array([
|
|
6606
6603
|
48,
|
|
6607
6604
|
46,
|
|
@@ -6620,12 +6617,147 @@ var PKCS8_X25519_PREFIX = new Uint8Array([
|
|
|
6620
6617
|
4,
|
|
6621
6618
|
32
|
|
6622
6619
|
]);
|
|
6620
|
+
var SymmetricState = class _SymmetricState {
|
|
6621
|
+
constructor(h) {
|
|
6622
|
+
this.k = null;
|
|
6623
|
+
this.n = 0;
|
|
6624
|
+
this.ck = h;
|
|
6625
|
+
this.h = h;
|
|
6626
|
+
}
|
|
6627
|
+
static async initialize() {
|
|
6628
|
+
const name = new TextEncoder().encode(PROTOCOL_NAME);
|
|
6629
|
+
const h = new Uint8Array(32);
|
|
6630
|
+
h.set(name);
|
|
6631
|
+
return new _SymmetricState(h);
|
|
6632
|
+
}
|
|
6633
|
+
async mixHash(data) {
|
|
6634
|
+
this.h = await sha256(concat(this.h, data));
|
|
6635
|
+
}
|
|
6636
|
+
async mixKey(inputKeyMaterial) {
|
|
6637
|
+
const [ck, key] = await hkdf2(this.ck, inputKeyMaterial);
|
|
6638
|
+
this.ck = ck;
|
|
6639
|
+
this.k = await crypto.subtle.importKey("raw", key, "AES-GCM", false, ["encrypt", "decrypt"]);
|
|
6640
|
+
this.n = 0;
|
|
6641
|
+
}
|
|
6642
|
+
async encryptAndHash(plaintext) {
|
|
6643
|
+
if (!this.k) throw new Error("Noise: no cipher key yet");
|
|
6644
|
+
const ciphertext = new Uint8Array(
|
|
6645
|
+
await crypto.subtle.encrypt(
|
|
6646
|
+
{ name: "AES-GCM", iv: noiseNonce(this.n), additionalData: this.h },
|
|
6647
|
+
this.k,
|
|
6648
|
+
plaintext
|
|
6649
|
+
)
|
|
6650
|
+
);
|
|
6651
|
+
this.n += 1;
|
|
6652
|
+
await this.mixHash(ciphertext);
|
|
6653
|
+
return ciphertext;
|
|
6654
|
+
}
|
|
6655
|
+
async decryptAndHash(ciphertext) {
|
|
6656
|
+
if (!this.k) throw new Error("Noise: no cipher key yet");
|
|
6657
|
+
const plaintext = new Uint8Array(
|
|
6658
|
+
await crypto.subtle.decrypt(
|
|
6659
|
+
{ name: "AES-GCM", iv: noiseNonce(this.n), additionalData: this.h },
|
|
6660
|
+
this.k,
|
|
6661
|
+
ciphertext
|
|
6662
|
+
)
|
|
6663
|
+
);
|
|
6664
|
+
this.n += 1;
|
|
6665
|
+
await this.mixHash(ciphertext);
|
|
6666
|
+
return plaintext;
|
|
6667
|
+
}
|
|
6668
|
+
/** The initiator-to-responder key, then the responder-to-initiator key. */
|
|
6669
|
+
split() {
|
|
6670
|
+
return hkdf2(this.ck, new Uint8Array());
|
|
6671
|
+
}
|
|
6672
|
+
};
|
|
6673
|
+
async function initiate(remoteStatic, prologue, fixedEphemeral) {
|
|
6674
|
+
const state = await SymmetricState.initialize();
|
|
6675
|
+
await state.mixHash(prologue);
|
|
6676
|
+
await state.mixHash(remoteStatic);
|
|
6677
|
+
const ephemeral = await x25519KeyPair(fixedEphemeral);
|
|
6678
|
+
await state.mixHash(ephemeral.publicKey);
|
|
6679
|
+
await state.mixKey(await dh(ephemeral.privateKey, remoteStatic));
|
|
6680
|
+
const message1 = concat(ephemeral.publicKey, await state.encryptAndHash(new Uint8Array()));
|
|
6681
|
+
return {
|
|
6682
|
+
message1,
|
|
6683
|
+
async finish(message2) {
|
|
6684
|
+
if (message2.length < DH_LEN + TAG_LEN) throw new Error("Noise: message 2 is too short");
|
|
6685
|
+
const remoteEphemeral = message2.slice(0, DH_LEN);
|
|
6686
|
+
await state.mixHash(remoteEphemeral);
|
|
6687
|
+
await state.mixKey(await dh(ephemeral.privateKey, remoteEphemeral));
|
|
6688
|
+
const payload = await state.decryptAndHash(message2.slice(DH_LEN));
|
|
6689
|
+
return { payload, split: await state.split() };
|
|
6690
|
+
}
|
|
6691
|
+
};
|
|
6692
|
+
}
|
|
6693
|
+
async function x25519KeyPair(secret) {
|
|
6694
|
+
if (!secret) {
|
|
6695
|
+
const pair = await crypto.subtle.generateKey({ name: "X25519" }, true, [
|
|
6696
|
+
"deriveBits"
|
|
6697
|
+
]);
|
|
6698
|
+
const publicKey = new Uint8Array(await crypto.subtle.exportKey("raw", pair.publicKey));
|
|
6699
|
+
return { privateKey: pair.privateKey, publicKey };
|
|
6700
|
+
}
|
|
6701
|
+
const privateKey = await crypto.subtle.importKey(
|
|
6702
|
+
"pkcs8",
|
|
6703
|
+
concat(PKCS8_X25519_PREFIX, secret),
|
|
6704
|
+
{ name: "X25519" },
|
|
6705
|
+
true,
|
|
6706
|
+
["deriveBits"]
|
|
6707
|
+
);
|
|
6708
|
+
const jwk = await crypto.subtle.exportKey("jwk", privateKey);
|
|
6709
|
+
return { privateKey, publicKey: base64UrlDecode(jwk.x) };
|
|
6710
|
+
}
|
|
6711
|
+
async function dh(privateKey, publicKey) {
|
|
6712
|
+
const peer = await crypto.subtle.importKey("raw", publicKey, { name: "X25519" }, false, []);
|
|
6713
|
+
return new Uint8Array(
|
|
6714
|
+
await crypto.subtle.deriveBits({ name: "X25519", public: peer }, privateKey, 256)
|
|
6715
|
+
);
|
|
6716
|
+
}
|
|
6717
|
+
function noiseNonce(n) {
|
|
6718
|
+
const nonce2 = new Uint8Array(12);
|
|
6719
|
+
new DataView(nonce2.buffer).setBigUint64(4, BigInt(n), false);
|
|
6720
|
+
return nonce2;
|
|
6721
|
+
}
|
|
6722
|
+
async function sha256(data) {
|
|
6723
|
+
return new Uint8Array(await crypto.subtle.digest("SHA-256", data));
|
|
6724
|
+
}
|
|
6725
|
+
async function hmacSha256(key, data) {
|
|
6726
|
+
const hmacKey = await crypto.subtle.importKey("raw", key, { name: "HMAC", hash: "SHA-256" }, false, [
|
|
6727
|
+
"sign"
|
|
6728
|
+
]);
|
|
6729
|
+
return new Uint8Array(await crypto.subtle.sign("HMAC", hmacKey, data));
|
|
6730
|
+
}
|
|
6731
|
+
async function hkdf2(chainingKey, inputKeyMaterial) {
|
|
6732
|
+
const temp = await hmacSha256(chainingKey, inputKeyMaterial);
|
|
6733
|
+
const first = await hmacSha256(temp, new Uint8Array([1]));
|
|
6734
|
+
const second = await hmacSha256(temp, concat(first, new Uint8Array([2])));
|
|
6735
|
+
return [first, second];
|
|
6736
|
+
}
|
|
6737
|
+
function base64UrlDecode(value) {
|
|
6738
|
+
const base643 = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
6739
|
+
const binary = atob(base643 + "=".repeat((4 - base643.length % 4) % 4));
|
|
6740
|
+
return Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
6741
|
+
}
|
|
6742
|
+
|
|
6743
|
+
// src/sealed/sealed.ts
|
|
6744
|
+
var HANDSHAKE_PATH = "/sealed/v2/handshake";
|
|
6745
|
+
var SEALED_PATH = "/sealed/v2";
|
|
6746
|
+
var SEALED_CONTENT_TYPE = "application/vnd.calimero.sealed";
|
|
6747
|
+
var VERSION = 2;
|
|
6748
|
+
var PROLOGUE = new TextEncoder().encode("calimero/sealed-http/v2");
|
|
6749
|
+
var SESSION_ID_LEN = 16;
|
|
6750
|
+
var HEADER_LEN = 1 + SESSION_ID_LEN + 8;
|
|
6751
|
+
var FRAME_HEAD = 0;
|
|
6752
|
+
var FRAME_DATA = 1;
|
|
6753
|
+
var FRAME_END = 2;
|
|
6754
|
+
var RENEW_BEFORE_MS = 6e4;
|
|
6755
|
+
var TRANSPORT_BINDING_DOMAIN = "calimero.tee-attest.transport-key.v1";
|
|
6756
|
+
var NULL_BODY_STATUSES = [101, 204, 205, 304];
|
|
6623
6757
|
var utf8 = new TextEncoder();
|
|
6624
6758
|
var StaleTransportKeyError = class extends Error {
|
|
6625
6759
|
constructor() {
|
|
6626
|
-
super(
|
|
6627
|
-
"The node no longer holds the transport key this request was sealed to; attest again for the current one"
|
|
6628
|
-
);
|
|
6760
|
+
super("The node no longer holds the attested transport key; attest again for the current one");
|
|
6629
6761
|
this.name = "StaleTransportKeyError";
|
|
6630
6762
|
}
|
|
6631
6763
|
};
|
|
@@ -6648,9 +6780,9 @@ async function fetchAttestedTransportKey(admin, verify, options = {}) {
|
|
|
6648
6780
|
if (options.applicationId === void 0 !== (options.applicationHash === void 0)) {
|
|
6649
6781
|
throw new Error("applicationId and applicationHash are given together or not at all");
|
|
6650
6782
|
}
|
|
6651
|
-
const
|
|
6783
|
+
const nonce2 = hex(crypto.getRandomValues(new Uint8Array(32)));
|
|
6652
6784
|
const attested = await admin.teeAttest({
|
|
6653
|
-
nonce,
|
|
6785
|
+
nonce: nonce2,
|
|
6654
6786
|
applicationId: options.applicationId,
|
|
6655
6787
|
bindTransportKey: true
|
|
6656
6788
|
});
|
|
@@ -6660,7 +6792,7 @@ async function fetchAttestedTransportKey(admin, verify, options = {}) {
|
|
|
6660
6792
|
const transportKey = fromHex(attested.transportPublicKey, "transportPublicKey", 32);
|
|
6661
6793
|
const inner = options.applicationHash ? fromHex(options.applicationHash, "applicationHash", 32) : new Uint8Array(32);
|
|
6662
6794
|
const reportDataSuffix = hex(await transportKeyBinding(inner, transportKey));
|
|
6663
|
-
if (!await verify({ quoteB64: attested.quoteB64, nonce, reportDataSuffix })) {
|
|
6795
|
+
if (!await verify({ quoteB64: attested.quoteB64, nonce: nonce2, reportDataSuffix })) {
|
|
6664
6796
|
throw new Error("The attestation did not verify, so its transport key is not trusted");
|
|
6665
6797
|
}
|
|
6666
6798
|
return transportKey;
|
|
@@ -6668,12 +6800,37 @@ async function fetchAttestedTransportKey(admin, verify, options = {}) {
|
|
|
6668
6800
|
function createSealedFetch(options) {
|
|
6669
6801
|
const base = new URL(options.baseUrl);
|
|
6670
6802
|
const basePath = base.pathname.replace(/\/+$/, "");
|
|
6671
|
-
const
|
|
6672
|
-
const transportKey = options.transportPublicKey;
|
|
6673
|
-
if (transportKey.length !== 32) {
|
|
6674
|
-
throw new Error("transportPublicKey must be 32 bytes");
|
|
6675
|
-
}
|
|
6803
|
+
const nodeUrl = `${base.origin}${basePath}`;
|
|
6676
6804
|
const baseFetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
6805
|
+
const attest = typeof options.transportPublicKey === "function" ? options.transportPublicKey : void 0;
|
|
6806
|
+
let transportKey = attest ? Promise.resolve().then(attest) : Promise.resolve(options.transportPublicKey);
|
|
6807
|
+
let session;
|
|
6808
|
+
const openSession = async () => {
|
|
6809
|
+
try {
|
|
6810
|
+
return await handshake(baseFetch, nodeUrl, await checkedKey(transportKey));
|
|
6811
|
+
} catch (error) {
|
|
6812
|
+
if (!(error instanceof StaleTransportKeyError) || !attest) throw error;
|
|
6813
|
+
transportKey = Promise.resolve().then(attest);
|
|
6814
|
+
return handshake(baseFetch, nodeUrl, await checkedKey(transportKey));
|
|
6815
|
+
}
|
|
6816
|
+
};
|
|
6817
|
+
const currentSession = async () => {
|
|
6818
|
+
const pending = session ?? renew(void 0);
|
|
6819
|
+
const open = await pending;
|
|
6820
|
+
if (Date.now() < open.renewAt) return [open, pending];
|
|
6821
|
+
const renewed = renew(pending);
|
|
6822
|
+
return [await renewed, renewed];
|
|
6823
|
+
};
|
|
6824
|
+
const renew = (stale) => {
|
|
6825
|
+
if (session === stale) {
|
|
6826
|
+
const opening = openSession();
|
|
6827
|
+
session = opening;
|
|
6828
|
+
opening.catch(() => {
|
|
6829
|
+
if (session === opening) session = void 0;
|
|
6830
|
+
});
|
|
6831
|
+
}
|
|
6832
|
+
return session;
|
|
6833
|
+
};
|
|
6677
6834
|
return async (input, init) => {
|
|
6678
6835
|
const request = new Request(input, init);
|
|
6679
6836
|
const url = new URL(request.url);
|
|
@@ -6682,126 +6839,190 @@ function createSealedFetch(options) {
|
|
|
6682
6839
|
}
|
|
6683
6840
|
const headers = [];
|
|
6684
6841
|
request.headers.forEach((value, name) => headers.push([name, value]));
|
|
6685
|
-
const head = {
|
|
6686
|
-
method: request.method,
|
|
6687
|
-
path: url.pathname.slice(basePath.length) + url.search,
|
|
6688
|
-
headers,
|
|
6689
|
-
ts: Math.floor(Date.now() / 1e3)
|
|
6690
|
-
};
|
|
6842
|
+
const head = { method: request.method, path: url.pathname.slice(basePath.length) + url.search, headers };
|
|
6691
6843
|
const body = new Uint8Array(await request.arrayBuffer());
|
|
6692
|
-
const
|
|
6693
|
-
|
|
6694
|
-
|
|
6695
|
-
|
|
6696
|
-
|
|
6697
|
-
|
|
6698
|
-
|
|
6699
|
-
|
|
6700
|
-
|
|
6844
|
+
const send = async (open2) => {
|
|
6845
|
+
const { requestId, header, envelope } = await sealRequest(open2, head, body);
|
|
6846
|
+
const response = await baseFetch(`${nodeUrl}${SEALED_PATH}`, {
|
|
6847
|
+
method: "POST",
|
|
6848
|
+
headers: { "content-type": SEALED_CONTENT_TYPE },
|
|
6849
|
+
body: envelope,
|
|
6850
|
+
signal: request.signal
|
|
6851
|
+
});
|
|
6852
|
+
if (response.headers.get("content-type") !== SEALED_CONTENT_TYPE) {
|
|
6853
|
+
throw await envelopeRefusal(response);
|
|
6854
|
+
}
|
|
6855
|
+
return openResponse(response, open2.responseKey, header, requestId);
|
|
6856
|
+
};
|
|
6857
|
+
const [open, pending] = await currentSession();
|
|
6858
|
+
try {
|
|
6859
|
+
return await send(open);
|
|
6860
|
+
} catch (error) {
|
|
6861
|
+
if (!(error instanceof SealedTransportError) || error.code !== "unknown_session") throw error;
|
|
6862
|
+
return send(await renew(pending));
|
|
6701
6863
|
}
|
|
6702
|
-
const opened = await exchange.open(new Uint8Array(await response.arrayBuffer()));
|
|
6703
|
-
const nullBody = [101, 204, 205, 304].includes(opened.head.status);
|
|
6704
|
-
return new Response(nullBody ? null : opened.body, {
|
|
6705
|
-
status: opened.head.status,
|
|
6706
|
-
headers: opened.head.headers
|
|
6707
|
-
});
|
|
6708
6864
|
};
|
|
6709
6865
|
}
|
|
6710
|
-
async function
|
|
6711
|
-
|
|
6712
|
-
|
|
6713
|
-
|
|
6714
|
-
|
|
6715
|
-
|
|
6716
|
-
|
|
6717
|
-
|
|
6866
|
+
async function checkedKey(key) {
|
|
6867
|
+
const bytes = await key;
|
|
6868
|
+
if (bytes.length !== 32) throw new Error("transportPublicKey must be 32 bytes");
|
|
6869
|
+
return bytes;
|
|
6870
|
+
}
|
|
6871
|
+
async function handshake(baseFetch, nodeUrl, transportKey) {
|
|
6872
|
+
const noise = await initiate(transportKey, PROLOGUE);
|
|
6873
|
+
const response = await baseFetch(`${nodeUrl}${HANDSHAKE_PATH}`, {
|
|
6874
|
+
method: "POST",
|
|
6875
|
+
headers: { "content-type": SEALED_CONTENT_TYPE },
|
|
6876
|
+
body: concat(new Uint8Array([VERSION]), transportKey, noise.message1)
|
|
6877
|
+
});
|
|
6878
|
+
if (response.headers.get("content-type") !== SEALED_CONTENT_TYPE) {
|
|
6879
|
+
throw await envelopeRefusal(response);
|
|
6718
6880
|
}
|
|
6719
|
-
|
|
6720
|
-
return
|
|
6881
|
+
const reply = new Uint8Array(await response.arrayBuffer());
|
|
6882
|
+
return sessionFrom(noise, reply);
|
|
6721
6883
|
}
|
|
6722
|
-
async function
|
|
6723
|
-
|
|
6724
|
-
|
|
6725
|
-
|
|
6726
|
-
await
|
|
6727
|
-
|
|
6728
|
-
|
|
6729
|
-
|
|
6730
|
-
}
|
|
6731
|
-
|
|
6732
|
-
const
|
|
6733
|
-
const
|
|
6734
|
-
const
|
|
6735
|
-
|
|
6736
|
-
|
|
6737
|
-
|
|
6738
|
-
|
|
6739
|
-
|
|
6740
|
-
);
|
|
6741
|
-
const responseAad = concat(new Uint8Array([VERSION]), salt);
|
|
6884
|
+
async function sessionFrom(noise, reply) {
|
|
6885
|
+
if (reply[0] !== VERSION) throw new Error("The handshake reply is malformed");
|
|
6886
|
+
let finished;
|
|
6887
|
+
try {
|
|
6888
|
+
finished = await noise.finish(reply.slice(1));
|
|
6889
|
+
} catch {
|
|
6890
|
+
throw new Error("The handshake reply did not open: it did not come from the attested node");
|
|
6891
|
+
}
|
|
6892
|
+
const { payload, split } = finished;
|
|
6893
|
+
if (payload.length !== SESSION_ID_LEN + 4) throw new Error("The handshake reply is malformed");
|
|
6894
|
+
const sessionId = payload.slice(0, SESSION_ID_LEN);
|
|
6895
|
+
const lifetimeSecs = new DataView(payload.buffer, payload.byteOffset).getUint32(SESSION_ID_LEN, false);
|
|
6896
|
+
const [requestKey, responseKey] = await Promise.all([
|
|
6897
|
+
crypto.subtle.importKey("raw", split[0], "AES-GCM", false, ["encrypt"]),
|
|
6898
|
+
crypto.subtle.importKey("raw", split[1], "AES-GCM", false, ["decrypt"])
|
|
6899
|
+
]);
|
|
6900
|
+
split[0].fill(0);
|
|
6901
|
+
split[1].fill(0);
|
|
6742
6902
|
return {
|
|
6743
|
-
|
|
6744
|
-
|
|
6745
|
-
|
|
6746
|
-
|
|
6747
|
-
|
|
6748
|
-
|
|
6749
|
-
|
|
6750
|
-
|
|
6751
|
-
|
|
6752
|
-
|
|
6753
|
-
responseKey,
|
|
6754
|
-
sealed.slice(1 + NONCE_LEN)
|
|
6755
|
-
)
|
|
6756
|
-
);
|
|
6757
|
-
} catch {
|
|
6758
|
-
throw new Error("The sealed response did not open: it was not sealed by the attested node");
|
|
6759
|
-
}
|
|
6760
|
-
const view = new DataView(plaintext.buffer, plaintext.byteOffset, plaintext.byteLength);
|
|
6761
|
-
const len = view.getUint32(0, false);
|
|
6762
|
-
const head2 = JSON.parse(new TextDecoder().decode(plaintext.slice(4, 4 + len)));
|
|
6763
|
-
return { head: head2, body: plaintext.slice(4 + len) };
|
|
6903
|
+
requestKey,
|
|
6904
|
+
responseKey,
|
|
6905
|
+
nextRequestId: 1,
|
|
6906
|
+
renewAt: Date.now() + lifetimeSecs * 1e3 - RENEW_BEFORE_MS,
|
|
6907
|
+
header(requestId) {
|
|
6908
|
+
const header = new Uint8Array(HEADER_LEN);
|
|
6909
|
+
header[0] = VERSION;
|
|
6910
|
+
header.set(sessionId, 1);
|
|
6911
|
+
new DataView(header.buffer).setBigUint64(1 + SESSION_ID_LEN, BigInt(requestId), false);
|
|
6912
|
+
return header;
|
|
6764
6913
|
}
|
|
6765
6914
|
};
|
|
6766
6915
|
}
|
|
6916
|
+
async function sealRequest(session, head, body) {
|
|
6917
|
+
const requestId = session.nextRequestId;
|
|
6918
|
+
session.nextRequestId += 1;
|
|
6919
|
+
const header = session.header(requestId);
|
|
6920
|
+
const sealed = await crypto.subtle.encrypt(
|
|
6921
|
+
{ name: "AES-GCM", iv: nonce(requestId, 0), additionalData: header },
|
|
6922
|
+
session.requestKey,
|
|
6923
|
+
joinInner(head, body)
|
|
6924
|
+
);
|
|
6925
|
+
return { requestId, header, envelope: concat(header, new Uint8Array(sealed)) };
|
|
6926
|
+
}
|
|
6927
|
+
function nonce(requestId, index) {
|
|
6928
|
+
const bytes = new Uint8Array(12);
|
|
6929
|
+
const view = new DataView(bytes.buffer);
|
|
6930
|
+
view.setBigUint64(0, BigInt(requestId), false);
|
|
6931
|
+
view.setUint32(8, index, false);
|
|
6932
|
+
return bytes;
|
|
6933
|
+
}
|
|
6767
6934
|
function joinInner(head, body) {
|
|
6768
6935
|
const encoded = utf8.encode(JSON.stringify(head));
|
|
6769
6936
|
const len = new Uint8Array(4);
|
|
6770
6937
|
new DataView(len.buffer).setUint32(0, encoded.length, false);
|
|
6771
6938
|
return concat(len, encoded, body);
|
|
6772
6939
|
}
|
|
6773
|
-
async function
|
|
6774
|
-
|
|
6775
|
-
|
|
6776
|
-
|
|
6777
|
-
|
|
6778
|
-
|
|
6779
|
-
|
|
6780
|
-
|
|
6781
|
-
|
|
6940
|
+
async function openResponse(response, responseKey, header, requestId) {
|
|
6941
|
+
if (!response.body) throw new Error("The sealed response has no body");
|
|
6942
|
+
const frames = frameReader(response.body.getReader());
|
|
6943
|
+
let index = 0;
|
|
6944
|
+
const next = async () => {
|
|
6945
|
+
const sealed = await frames.next();
|
|
6946
|
+
if (!sealed) return null;
|
|
6947
|
+
let plain;
|
|
6948
|
+
try {
|
|
6949
|
+
plain = new Uint8Array(
|
|
6950
|
+
await crypto.subtle.decrypt(
|
|
6951
|
+
{ name: "AES-GCM", iv: nonce(requestId, index), additionalData: header },
|
|
6952
|
+
responseKey,
|
|
6953
|
+
sealed
|
|
6954
|
+
)
|
|
6955
|
+
);
|
|
6956
|
+
} catch {
|
|
6957
|
+
throw new Error("A sealed response frame did not open: it was not sealed by the attested node");
|
|
6958
|
+
}
|
|
6959
|
+
index += 1;
|
|
6960
|
+
return { kind: plain[0], data: plain.slice(1) };
|
|
6961
|
+
};
|
|
6962
|
+
const first = await next();
|
|
6963
|
+
if (!first || first.kind !== FRAME_HEAD) throw new Error("The sealed response is malformed");
|
|
6964
|
+
const head = JSON.parse(new TextDecoder().decode(first.data));
|
|
6965
|
+
if (NULL_BODY_STATUSES.includes(head.status)) {
|
|
6966
|
+
await frames.cancel();
|
|
6967
|
+
return new Response(null, { status: head.status, headers: head.headers });
|
|
6968
|
+
}
|
|
6969
|
+
const body = new ReadableStream({
|
|
6970
|
+
async pull(controller) {
|
|
6971
|
+
try {
|
|
6972
|
+
const frame = await next();
|
|
6973
|
+
if (!frame) {
|
|
6974
|
+
controller.error(new Error("The sealed response was cut short"));
|
|
6975
|
+
} else if (frame.kind === FRAME_DATA) {
|
|
6976
|
+
controller.enqueue(frame.data);
|
|
6977
|
+
} else if (frame.kind === FRAME_END) {
|
|
6978
|
+
controller.close();
|
|
6979
|
+
await frames.cancel();
|
|
6980
|
+
} else {
|
|
6981
|
+
controller.error(new Error("The sealed response is malformed"));
|
|
6982
|
+
}
|
|
6983
|
+
} catch (error) {
|
|
6984
|
+
controller.error(error);
|
|
6985
|
+
}
|
|
6986
|
+
},
|
|
6987
|
+
cancel() {
|
|
6988
|
+
return frames.cancel();
|
|
6989
|
+
}
|
|
6990
|
+
});
|
|
6991
|
+
return new Response(body, { status: head.status, headers: head.headers });
|
|
6782
6992
|
}
|
|
6783
|
-
|
|
6784
|
-
|
|
6785
|
-
|
|
6786
|
-
|
|
6787
|
-
|
|
6788
|
-
|
|
6789
|
-
|
|
6790
|
-
|
|
6791
|
-
|
|
6792
|
-
|
|
6793
|
-
|
|
6794
|
-
|
|
6795
|
-
|
|
6796
|
-
|
|
6797
|
-
|
|
6798
|
-
|
|
6799
|
-
|
|
6993
|
+
function frameReader(reader) {
|
|
6994
|
+
let buffer = new Uint8Array();
|
|
6995
|
+
const fill = async (length) => {
|
|
6996
|
+
while (buffer.length < length) {
|
|
6997
|
+
const { done, value } = await reader.read();
|
|
6998
|
+
if (done) return false;
|
|
6999
|
+
buffer = concat(buffer, value);
|
|
7000
|
+
}
|
|
7001
|
+
return true;
|
|
7002
|
+
};
|
|
7003
|
+
return {
|
|
7004
|
+
async next() {
|
|
7005
|
+
if (!await fill(4)) return null;
|
|
7006
|
+
const length = new DataView(buffer.buffer, buffer.byteOffset).getUint32(0, false);
|
|
7007
|
+
if (!await fill(4 + length)) return null;
|
|
7008
|
+
const frame = buffer.slice(4, 4 + length);
|
|
7009
|
+
buffer = buffer.slice(4 + length);
|
|
7010
|
+
return frame;
|
|
7011
|
+
},
|
|
7012
|
+
cancel: () => reader.cancel()
|
|
7013
|
+
};
|
|
6800
7014
|
}
|
|
6801
|
-
function
|
|
6802
|
-
|
|
6803
|
-
|
|
6804
|
-
|
|
7015
|
+
async function envelopeRefusal(response) {
|
|
7016
|
+
let code;
|
|
7017
|
+
let message = `Sealed request refused: HTTP ${response.status}`;
|
|
7018
|
+
try {
|
|
7019
|
+
const body = await response.json();
|
|
7020
|
+
code = body?.error?.code;
|
|
7021
|
+
if (body?.error?.message) message = `Sealed request refused: ${body.error.message}`;
|
|
7022
|
+
} catch {
|
|
7023
|
+
}
|
|
7024
|
+
if (code === "stale_transport_key") return new StaleTransportKeyError();
|
|
7025
|
+
return new SealedTransportError(response.status, code, message);
|
|
6805
7026
|
}
|
|
6806
7027
|
|
|
6807
7028
|
// src/namespace-op/namespace-op.ts
|