@private.me/xbind 1.3.5 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSES.md +212 -0
- package/README.md +388 -6
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1 -1920
- package/dist-standalone/_deps/shared/cjs/errors.js +1 -639
- package/dist-standalone/_deps/shared/cjs/index.js +1 -496
- package/dist-standalone/_deps/shared/cjs/types.js +1 -317
- package/dist-standalone/_deps/shared/errors.js +1 -255
- package/dist-standalone/_deps/shared/index.js +1 -74
- package/dist-standalone/_deps/shared/types.js +1 -90
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
- package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/search.js +1 -1
- package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
- package/dist-standalone/_deps/xchange/errors.js +1 -1
- package/dist-standalone/_deps/xchange/index.js +1 -1
- package/dist-standalone/_deps/xchange/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/xchange.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
- package/dist-standalone/_deps/xregistry/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/index.js +1 -1
- package/dist-standalone/_deps/xregistry/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/types.js +1 -1
- package/dist-standalone/agent-call.js +1 -642
- package/dist-standalone/agent-sdk.js +1 -328
- package/dist-standalone/agent.d.ts +95 -5
- package/dist-standalone/agent.js +1 -1545
- package/dist-standalone/approval.js +1 -193
- package/dist-standalone/async-iterators.d.ts +275 -0
- package/dist-standalone/async-iterators.js +1 -0
- package/dist-standalone/auth.js +1 -219
- package/dist-standalone/auto-accept.js +1 -229
- package/dist-standalone/backup-config.js +1 -201
- package/dist-standalone/backup.d.ts +114 -0
- package/dist-standalone/backup.js +1 -0
- package/dist-standalone/batch-operations.d.ts +297 -0
- package/dist-standalone/batch-operations.js +1 -0
- package/dist-standalone/cancellation.d.ts +301 -0
- package/dist-standalone/cancellation.js +1 -0
- package/dist-standalone/checkpoint.js +1 -186
- package/dist-standalone/circuit-breaker.d.ts +351 -0
- package/dist-standalone/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/agent-call.js +1 -651
- package/dist-standalone/cjs/agent-sdk.js +1 -332
- package/dist-standalone/cjs/agent.js +1 -1582
- package/dist-standalone/cjs/approval.js +1 -199
- package/dist-standalone/cjs/async-iterators.js +1 -0
- package/dist-standalone/cjs/auth.js +1 -225
- package/dist-standalone/cjs/auto-accept.js +1 -233
- package/dist-standalone/cjs/backup-config.js +1 -207
- package/dist-standalone/cjs/backup.js +1 -0
- package/dist-standalone/cjs/batch-operations.js +1 -0
- package/dist-standalone/cjs/cancellation.js +1 -0
- package/dist-standalone/cjs/checkpoint.js +1 -193
- package/dist-standalone/cjs/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/cli/init.js +1 -486
- package/dist-standalone/cjs/config-validation.js +1 -0
- package/dist-standalone/cjs/connect.js +1 -312
- package/dist-standalone/cjs/connection-pool.js +1 -0
- package/dist-standalone/cjs/correlation-id.js +1 -339
- package/dist-standalone/cjs/crypto-utils.js +1 -0
- package/dist-standalone/cjs/debug-mode.js +1 -0
- package/dist-standalone/cjs/did-document.js +1 -101
- package/dist-standalone/cjs/did-privateme.js +1 -130
- package/dist-standalone/cjs/did-web.js +1 -201
- package/dist-standalone/cjs/discovery.js +1 -462
- package/dist-standalone/cjs/dual-mode.js +1 -251
- package/dist-standalone/cjs/email-templates.js +1 -313
- package/dist-standalone/cjs/email-transport.js +1 -239
- package/dist-standalone/cjs/envelope.js +1 -510
- package/dist-standalone/cjs/errors.js +1 -826
- package/dist-standalone/cjs/event-emitter.js +1 -0
- package/dist-standalone/cjs/gateway-state.js +1 -55
- package/dist-standalone/cjs/gateway-transport.js +1 -120
- package/dist-standalone/cjs/graceful-degradation.js +1 -0
- package/dist-standalone/cjs/guardrails.js +1 -223
- package/dist-standalone/cjs/health-check.js +1 -0
- package/dist-standalone/cjs/http-compat.js +1 -272
- package/dist-standalone/cjs/http-status-map.js +1 -571
- package/dist-standalone/cjs/identity.js +1 -540
- package/dist-standalone/cjs/index.js +1 -237
- package/dist-standalone/cjs/invitation.js +1 -421
- package/dist-standalone/cjs/invite.js +1 -328
- package/dist-standalone/cjs/key-agreement.js +1 -246
- package/dist-standalone/cjs/lazy-init.js +1 -300
- package/dist-standalone/cjs/logger.js +1 -0
- package/dist-standalone/cjs/mdns-discovery.js +1 -202
- package/dist-standalone/cjs/nonce-store.js +1 -66
- package/dist-standalone/cjs/pairing-manager.js +1 -223
- package/dist-standalone/cjs/plugin-system.js +1 -0
- package/dist-standalone/cjs/plugins/logging.js +1 -0
- package/dist-standalone/cjs/plugins/metrics.js +1 -0
- package/dist-standalone/cjs/plugins/validation.js +1 -0
- package/dist-standalone/cjs/policy.js +1 -320
- package/dist-standalone/cjs/progress-callbacks.js +1 -0
- package/dist-standalone/cjs/redis-nonce-store.js +1 -76
- package/dist-standalone/cjs/registry-middleware.js +1 -50
- package/dist-standalone/cjs/retry-strategies.js +1 -0
- package/dist-standalone/cjs/retry-transport.js +1 -102
- package/dist-standalone/cjs/runtime/browser.js +1 -0
- package/dist-standalone/cjs/runtime/edge.js +1 -0
- package/dist-standalone/cjs/runtime/react-native.js +1 -0
- package/dist-standalone/cjs/security-policy.js +1 -245
- package/dist-standalone/cjs/serialization.js +1 -0
- package/dist-standalone/cjs/split-channel.js +1 -177
- package/dist-standalone/cjs/subscription-proof.js +1 -230
- package/dist-standalone/cjs/succession.js +1 -148
- package/dist-standalone/cjs/timeouts.js +1 -0
- package/dist-standalone/cjs/trace-context.js +1 -0
- package/dist-standalone/cjs/trace-spans.js +1 -0
- package/dist-standalone/cjs/transport.js +1 -63
- package/dist-standalone/cjs/trust-registry.js +1 -742
- package/dist-standalone/cjs/types/error-response.js +1 -56
- package/dist-standalone/cjs/vault-auth.js +1 -0
- package/dist-standalone/cjs/vault-store-loader.js +1 -0
- package/dist-standalone/cjs/verify.js +1 -25
- package/dist-standalone/cjs/version-info.js +1 -0
- package/dist-standalone/cjs/xfetch.js +1 -252
- package/dist-standalone/cli/init.js +1 -449
- package/dist-standalone/cli/setup.js +1 -514
- package/dist-standalone/cli/types.js +1 -27
- package/dist-standalone/cli/xbind.js +1 -148
- package/dist-standalone/config-validation.d.ts +185 -0
- package/dist-standalone/config-validation.js +1 -0
- package/dist-standalone/connect.js +1 -274
- package/dist-standalone/connection-pool.d.ts +251 -0
- package/dist-standalone/connection-pool.js +1 -0
- package/dist-standalone/correlation-id.js +1 -326
- package/dist-standalone/crypto-utils.d.ts +60 -0
- package/dist-standalone/crypto-utils.js +1 -0
- package/dist-standalone/debug-mode.d.ts +286 -0
- package/dist-standalone/debug-mode.js +1 -0
- package/dist-standalone/did-document.js +1 -96
- package/dist-standalone/did-privateme.js +1 -121
- package/dist-standalone/did-web.js +1 -196
- package/dist-standalone/discovery.js +1 -458
- package/dist-standalone/dual-mode.js +1 -247
- package/dist-standalone/email-templates.js +1 -309
- package/dist-standalone/email-transport.js +1 -232
- package/dist-standalone/envelope.d.ts +29 -1
- package/dist-standalone/envelope.js +1 -497
- package/dist-standalone/errors.d.ts +10 -0
- package/dist-standalone/errors.js +1 -811
- package/dist-standalone/event-emitter.d.ts +395 -0
- package/dist-standalone/event-emitter.js +1 -0
- package/dist-standalone/gateway-state.js +1 -51
- package/dist-standalone/gateway-transport.js +1 -116
- package/dist-standalone/graceful-degradation.d.ts +246 -0
- package/dist-standalone/graceful-degradation.js +1 -0
- package/dist-standalone/guardrails.js +1 -216
- package/dist-standalone/health-check.d.ts +150 -0
- package/dist-standalone/health-check.js +1 -0
- package/dist-standalone/http-compat.js +1 -267
- package/dist-standalone/http-status-map.js +1 -561
- package/dist-standalone/identity.d.ts +64 -1
- package/dist-standalone/identity.js +1 -515
- package/dist-standalone/index.d.ts +45 -3
- package/dist-standalone/index.js +1 -52
- package/dist-standalone/invitation.js +1 -415
- package/dist-standalone/invite.js +1 -324
- package/dist-standalone/key-agreement.d.ts +61 -13
- package/dist-standalone/key-agreement.js +1 -236
- package/dist-standalone/lazy-init.js +1 -295
- package/dist-standalone/logger.d.ts +77 -0
- package/dist-standalone/logger.js +1 -0
- package/dist-standalone/mdns-discovery.js +1 -195
- package/dist-standalone/nonce-store.d.ts +16 -3
- package/dist-standalone/nonce-store.js +1 -62
- package/dist-standalone/package.json +0 -1
- package/dist-standalone/pairing-manager.js +1 -219
- package/dist-standalone/plugin-system.d.ts +145 -0
- package/dist-standalone/plugin-system.js +1 -0
- package/dist-standalone/policy.js +1 -315
- package/dist-standalone/progress-callbacks.d.ts +394 -0
- package/dist-standalone/progress-callbacks.js +1 -0
- package/dist-standalone/redis-nonce-store.js +1 -72
- package/dist-standalone/registry-middleware.js +1 -47
- package/dist-standalone/retry-strategies.d.ts +382 -0
- package/dist-standalone/retry-strategies.js +1 -0
- package/dist-standalone/retry-transport.js +1 -98
- package/dist-standalone/security-policy.js +1 -239
- package/dist-standalone/serialization.d.ts +244 -0
- package/dist-standalone/serialization.js +1 -0
- package/dist-standalone/split-channel.d.ts +49 -1
- package/dist-standalone/split-channel.js +1 -171
- package/dist-standalone/subscription-proof.js +1 -224
- package/dist-standalone/succession.js +1 -142
- package/dist-standalone/timeouts.d.ts +275 -0
- package/dist-standalone/timeouts.js +1 -0
- package/dist-standalone/trace-context.d.ts +252 -0
- package/dist-standalone/trace-context.js +1 -0
- package/dist-standalone/trace-spans.d.ts +360 -0
- package/dist-standalone/trace-spans.js +1 -0
- package/dist-standalone/transport.js +1 -59
- package/dist-standalone/trust-registry.d.ts +106 -5
- package/dist-standalone/trust-registry.js +1 -702
- package/dist-standalone/vault-auth.d.ts +91 -0
- package/dist-standalone/vault-auth.js +1 -0
- package/dist-standalone/vault-store-loader.d.ts +110 -0
- package/dist-standalone/vault-store-loader.js +1 -0
- package/dist-standalone/verify.js +1 -16
- package/dist-standalone/version-info.d.ts +259 -0
- package/dist-standalone/version-info.js +1 -0
- package/dist-standalone/xfetch.js +1 -247
- package/llms.txt +1 -0
- package/package.json +65 -5
- package/share1.dat +0 -0
- package/dist-standalone/_deps/crypto/base64.d.ts +0 -29
- package/dist-standalone/_deps/crypto/base64.js +0 -222
- package/dist-standalone/_deps/crypto/cjs/base64.js +0 -665
- package/dist-standalone/_deps/crypto/cjs/errors.js +0 -675
- package/dist-standalone/_deps/crypto/cjs/hmac.js +0 -473
- package/dist-standalone/_deps/crypto/cjs/index.js +0 -852
- package/dist-standalone/_deps/crypto/cjs/package.json +0 -1
- package/dist-standalone/_deps/crypto/cjs/padding.js +0 -511
- package/dist-standalone/_deps/crypto/cjs/share-header.js +0 -372
- package/dist-standalone/_deps/crypto/cjs/shares.js +0 -874
- package/dist-standalone/_deps/crypto/cjs/tlv.js +0 -1021
- package/dist-standalone/_deps/crypto/cjs/uuid.js +0 -443
- package/dist-standalone/_deps/crypto/cjs/verify.js +0 -414
- package/dist-standalone/_deps/crypto/cjs/xorida.js +0 -923
- package/dist-standalone/_deps/crypto/errors.d.ts +0 -51
- package/dist-standalone/_deps/crypto/errors.js +0 -199
- package/dist-standalone/_deps/crypto/hmac.d.ts +0 -39
- package/dist-standalone/_deps/crypto/hmac.js +0 -134
- package/dist-standalone/_deps/crypto/index.d.ts +0 -20
- package/dist-standalone/_deps/crypto/index.js +0 -145
- package/dist-standalone/_deps/crypto/padding.d.ts +0 -19
- package/dist-standalone/_deps/crypto/padding.js +0 -159
- package/dist-standalone/_deps/crypto/share-header.d.ts +0 -44
- package/dist-standalone/_deps/crypto/share-header.js +0 -92
- package/dist-standalone/_deps/crypto/shares.d.ts +0 -27
- package/dist-standalone/_deps/crypto/shares.js +0 -295
- package/dist-standalone/_deps/crypto/tlv.d.ts +0 -26
- package/dist-standalone/_deps/crypto/tlv.js +0 -364
- package/dist-standalone/_deps/crypto/uuid.d.ts +0 -22
- package/dist-standalone/_deps/crypto/uuid.js +0 -136
- package/dist-standalone/_deps/crypto/verify.d.ts +0 -15
- package/dist-standalone/_deps/crypto/verify.js +0 -71
- package/dist-standalone/_deps/crypto/xorida.d.ts +0 -44
- package/dist-standalone/_deps/crypto/xorida.js +0 -366
- package/dist-standalone/_deps/shared/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/errors.js.map +0 -1
- package/dist-standalone/_deps/shared/index.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/index.js.map +0 -1
- package/dist-standalone/_deps/shared/types.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.js.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.js.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.js.map +0 -1
- package/dist-standalone/_deps/xregistry/index.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/index.js.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.js.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.js.map +0 -1
- package/dist-standalone/_deps/xregistry/types.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/types.js.map +0 -1
|
@@ -1,497 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { toBase64, fromBase64 } from"./_deps/crypto/index.js";
|
|
3
|
-
import { sign as ed25519Sign, verify as ed25519Verify, signMlDsa65 } from './identity.js';
|
|
4
|
-
/* ── Constants ── */
|
|
5
|
-
const NONCE_BYTES = 16;
|
|
6
|
-
const AES_IV_BYTES = 12;
|
|
7
|
-
const AES_ALGO = 'AES-GCM';
|
|
8
|
-
/** ML-KEM-768 ciphertext length in bytes. */
|
|
9
|
-
const KEM_CIPHERTEXT_BYTES = 1088;
|
|
10
|
-
/** ML-DSA-65 signature length in bytes. */
|
|
11
|
-
const ML_DSA65_SIG_BYTES = 3309;
|
|
12
|
-
/* ── Helpers ── */
|
|
13
|
-
/** Copy Uint8Array to fresh ArrayBuffer. */
|
|
14
|
-
function toArrayBuffer(data) {
|
|
15
|
-
const buf = new ArrayBuffer(data.byteLength);
|
|
16
|
-
new Uint8Array(buf).set(data);
|
|
17
|
-
return buf;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Create canonical representation of envelope fields for signing.
|
|
21
|
-
*
|
|
22
|
-
* SECURITY FIX (v1.1.3): Sign ALL envelope metadata (nonce, timestamp, sender,
|
|
23
|
-
* recipient, scope, payload) to prevent replay attacks via nonce substitution.
|
|
24
|
-
*
|
|
25
|
-
* Previous versions only signed the payload, allowing attackers to:
|
|
26
|
-
* 1. Capture an envelope
|
|
27
|
-
* 2. Change the nonce to a fresh value
|
|
28
|
-
* 3. Replay the message (signature still verified)
|
|
29
|
-
*
|
|
30
|
-
* Canonical format: UTF-8 JSON with sorted keys, no whitespace.
|
|
31
|
-
* Example: {"alg":"Ed25519","nonce":"...","payload":"...","recipient":"...","scope":"...","sender":"...","timestamp":1234567890,"v":1}
|
|
32
|
-
*/
|
|
33
|
-
function createSignedData(envelope) {
|
|
34
|
-
// Create canonical JSON representation (sorted keys)
|
|
35
|
-
const canonical = JSON.stringify({
|
|
36
|
-
v: envelope.v,
|
|
37
|
-
alg: envelope.alg,
|
|
38
|
-
sender: envelope.sender,
|
|
39
|
-
recipient: envelope.recipient,
|
|
40
|
-
timestamp: envelope.timestamp,
|
|
41
|
-
nonce: envelope.nonce,
|
|
42
|
-
scope: envelope.scope,
|
|
43
|
-
payload: envelope.payload,
|
|
44
|
-
});
|
|
45
|
-
return new TextEncoder().encode(canonical);
|
|
46
|
-
}
|
|
47
|
-
/* ── Envelope Creation ── */
|
|
48
|
-
/**
|
|
49
|
-
* Create a signed, encrypted TransportEnvelope.
|
|
50
|
-
*
|
|
51
|
-
* Encrypt-then-sign: AES-256-GCM encrypts payload, Ed25519 signs ciphertext.
|
|
52
|
-
*/
|
|
53
|
-
export async function createEnvelope(opts) {
|
|
54
|
-
const nonce = new Uint8Array(NONCE_BYTES);
|
|
55
|
-
crypto.getRandomValues(nonce);
|
|
56
|
-
const iv = new Uint8Array(AES_IV_BYTES);
|
|
57
|
-
crypto.getRandomValues(iv);
|
|
58
|
-
let ciphertext;
|
|
59
|
-
try {
|
|
60
|
-
const encrypted = await crypto.subtle.encrypt({ name: AES_ALGO, iv: toArrayBuffer(iv) }, opts.sharedKey, toArrayBuffer(opts.plaintext));
|
|
61
|
-
const ivAndCipher = new Uint8Array(iv.length + encrypted.byteLength);
|
|
62
|
-
ivAndCipher.set(iv);
|
|
63
|
-
ivAndCipher.set(new Uint8Array(encrypted), iv.length);
|
|
64
|
-
ciphertext = ivAndCipher;
|
|
65
|
-
}
|
|
66
|
-
catch {
|
|
67
|
-
return err('ENCRYPT_FAILED');
|
|
68
|
-
}
|
|
69
|
-
// SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
|
|
70
|
-
// (not just payload) to prevent replay attacks via nonce substitution
|
|
71
|
-
const timestamp = Date.now();
|
|
72
|
-
const envelopeFields = {
|
|
73
|
-
v: 1,
|
|
74
|
-
alg: 'Ed25519',
|
|
75
|
-
sender: opts.senderDid,
|
|
76
|
-
recipient: opts.recipientDid,
|
|
77
|
-
timestamp,
|
|
78
|
-
nonce: toBase64(nonce),
|
|
79
|
-
scope: opts.scope,
|
|
80
|
-
payload: toBase64(ciphertext),
|
|
81
|
-
};
|
|
82
|
-
const signedData = createSignedData(envelopeFields);
|
|
83
|
-
const sigResult = await ed25519Sign(opts.privateKey, signedData);
|
|
84
|
-
if (!sigResult.ok)
|
|
85
|
-
return err('SIGN_FAILED');
|
|
86
|
-
const envelope = {
|
|
87
|
-
...envelopeFields,
|
|
88
|
-
signature: toBase64(sigResult.value),
|
|
89
|
-
...(opts.ephemeralPublicKey
|
|
90
|
-
? { ephemeralPub: toBase64(opts.ephemeralPublicKey) }
|
|
91
|
-
: {}),
|
|
92
|
-
...(opts.shareIndex !== undefined
|
|
93
|
-
? {
|
|
94
|
-
shareIndex: opts.shareIndex,
|
|
95
|
-
shareTotal: opts.shareTotal,
|
|
96
|
-
shareThreshold: opts.shareThreshold,
|
|
97
|
-
shareGroupId: opts.shareGroupId,
|
|
98
|
-
shareHmacKey: opts.shareHmacKey,
|
|
99
|
-
shareHmacSig: opts.shareHmacSig,
|
|
100
|
-
}
|
|
101
|
-
: {}),
|
|
102
|
-
protocol: 'PRIVATE.ME/xBind/v3',
|
|
103
|
-
documentationUrl: 'https://private.me/docs/xbind.html',
|
|
104
|
-
};
|
|
105
|
-
return ok(envelope);
|
|
106
|
-
}
|
|
107
|
-
/* ── V2 Envelope Creation ── */
|
|
108
|
-
/**
|
|
109
|
-
* Create a signed, encrypted TransportEnvelopeV2 with hybrid KEM.
|
|
110
|
-
*
|
|
111
|
-
* Same encrypt-then-sign as v1, but includes ML-KEM-768 ciphertext.
|
|
112
|
-
*/
|
|
113
|
-
export async function createEnvelopeV2(opts) {
|
|
114
|
-
const nonce = new Uint8Array(NONCE_BYTES);
|
|
115
|
-
crypto.getRandomValues(nonce);
|
|
116
|
-
const iv = new Uint8Array(AES_IV_BYTES);
|
|
117
|
-
crypto.getRandomValues(iv);
|
|
118
|
-
let ciphertext;
|
|
119
|
-
try {
|
|
120
|
-
const encrypted = await crypto.subtle.encrypt({ name: AES_ALGO, iv: toArrayBuffer(iv) }, opts.sharedKey, toArrayBuffer(opts.plaintext));
|
|
121
|
-
const ivAndCipher = new Uint8Array(iv.length + encrypted.byteLength);
|
|
122
|
-
ivAndCipher.set(iv);
|
|
123
|
-
ivAndCipher.set(new Uint8Array(encrypted), iv.length);
|
|
124
|
-
ciphertext = ivAndCipher;
|
|
125
|
-
}
|
|
126
|
-
catch {
|
|
127
|
-
return err('ENCRYPT_FAILED');
|
|
128
|
-
}
|
|
129
|
-
// SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
|
|
130
|
-
const timestamp = Date.now();
|
|
131
|
-
const envelopeFields = {
|
|
132
|
-
v: 2,
|
|
133
|
-
alg: 'Ed25519',
|
|
134
|
-
sender: opts.senderDid,
|
|
135
|
-
recipient: opts.recipientDid,
|
|
136
|
-
timestamp,
|
|
137
|
-
nonce: toBase64(nonce),
|
|
138
|
-
scope: opts.scope,
|
|
139
|
-
payload: toBase64(ciphertext),
|
|
140
|
-
};
|
|
141
|
-
const signedData = createSignedData(envelopeFields);
|
|
142
|
-
const sigResult = await ed25519Sign(opts.privateKey, signedData);
|
|
143
|
-
if (!sigResult.ok)
|
|
144
|
-
return err('SIGN_FAILED');
|
|
145
|
-
const envelope = {
|
|
146
|
-
...envelopeFields,
|
|
147
|
-
kem: 'X25519-MLKEM768',
|
|
148
|
-
signature: toBase64(sigResult.value),
|
|
149
|
-
ephemeralPub: toBase64(opts.ephemeralPublicKey),
|
|
150
|
-
kemCiphertext: toBase64(opts.kemCiphertext),
|
|
151
|
-
...(opts.shareIndex !== undefined
|
|
152
|
-
? {
|
|
153
|
-
shareIndex: opts.shareIndex,
|
|
154
|
-
shareTotal: opts.shareTotal,
|
|
155
|
-
shareThreshold: opts.shareThreshold,
|
|
156
|
-
shareGroupId: opts.shareGroupId,
|
|
157
|
-
shareHmacKey: opts.shareHmacKey,
|
|
158
|
-
shareHmacSig: opts.shareHmacSig,
|
|
159
|
-
}
|
|
160
|
-
: {}),
|
|
161
|
-
protocol: 'PRIVATE.ME/xBind/v3',
|
|
162
|
-
documentationUrl: 'https://private.me/docs/xbind.html',
|
|
163
|
-
};
|
|
164
|
-
return ok(envelope);
|
|
165
|
-
}
|
|
166
|
-
/* ── V3 Envelope Creation ── */
|
|
167
|
-
/**
|
|
168
|
-
* Create a signed, encrypted TransportEnvelopeV3 with hybrid KEM + dual signatures.
|
|
169
|
-
*
|
|
170
|
-
* Encrypt-then-sign: AES-256-GCM encrypts payload, Ed25519 AND ML-DSA-65 sign ciphertext.
|
|
171
|
-
* Both signatures must verify for the envelope to be accepted.
|
|
172
|
-
*/
|
|
173
|
-
export async function createEnvelopeV3(opts) {
|
|
174
|
-
const nonce = new Uint8Array(NONCE_BYTES);
|
|
175
|
-
crypto.getRandomValues(nonce);
|
|
176
|
-
const iv = new Uint8Array(AES_IV_BYTES);
|
|
177
|
-
crypto.getRandomValues(iv);
|
|
178
|
-
let ciphertext;
|
|
179
|
-
try {
|
|
180
|
-
const encrypted = await crypto.subtle.encrypt({ name: AES_ALGO, iv: toArrayBuffer(iv) }, opts.sharedKey, toArrayBuffer(opts.plaintext));
|
|
181
|
-
const ivAndCipher = new Uint8Array(iv.length + encrypted.byteLength);
|
|
182
|
-
ivAndCipher.set(iv);
|
|
183
|
-
ivAndCipher.set(new Uint8Array(encrypted), iv.length);
|
|
184
|
-
ciphertext = ivAndCipher;
|
|
185
|
-
}
|
|
186
|
-
catch {
|
|
187
|
-
return err('ENCRYPT_FAILED');
|
|
188
|
-
}
|
|
189
|
-
// SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
|
|
190
|
-
const timestamp = Date.now();
|
|
191
|
-
const envelopeFields = {
|
|
192
|
-
v: 3,
|
|
193
|
-
alg: 'Ed25519',
|
|
194
|
-
sender: opts.senderDid,
|
|
195
|
-
recipient: opts.recipientDid,
|
|
196
|
-
timestamp,
|
|
197
|
-
nonce: toBase64(nonce),
|
|
198
|
-
scope: opts.scope,
|
|
199
|
-
payload: toBase64(ciphertext),
|
|
200
|
-
};
|
|
201
|
-
const signedData = createSignedData(envelopeFields);
|
|
202
|
-
const sigResult = await ed25519Sign(opts.privateKey, signedData);
|
|
203
|
-
if (!sigResult.ok)
|
|
204
|
-
return err('SIGN_FAILED');
|
|
205
|
-
const pqSigResult = await signMlDsa65(opts.mlDsaSecretKey, signedData);
|
|
206
|
-
if (!pqSigResult.ok)
|
|
207
|
-
return err('PQ_SIGN_FAILED');
|
|
208
|
-
const envelope = {
|
|
209
|
-
...envelopeFields,
|
|
210
|
-
sig: 'Ed25519+MLDSA65',
|
|
211
|
-
kem: 'X25519-MLKEM768',
|
|
212
|
-
signature: toBase64(sigResult.value),
|
|
213
|
-
pqSignature: toBase64(pqSigResult.value),
|
|
214
|
-
ephemeralPub: toBase64(opts.ephemeralPublicKey),
|
|
215
|
-
kemCiphertext: toBase64(opts.kemCiphertext),
|
|
216
|
-
...(opts.shareIndex !== undefined
|
|
217
|
-
? {
|
|
218
|
-
shareIndex: opts.shareIndex,
|
|
219
|
-
shareTotal: opts.shareTotal,
|
|
220
|
-
shareThreshold: opts.shareThreshold,
|
|
221
|
-
shareGroupId: opts.shareGroupId,
|
|
222
|
-
shareHmacKey: opts.shareHmacKey,
|
|
223
|
-
shareHmacSig: opts.shareHmacSig,
|
|
224
|
-
}
|
|
225
|
-
: {}),
|
|
226
|
-
protocol: 'PRIVATE.ME/xBind/v3',
|
|
227
|
-
documentationUrl: 'https://private.me/docs/xbind.html',
|
|
228
|
-
};
|
|
229
|
-
return ok(envelope);
|
|
230
|
-
}
|
|
231
|
-
/* ── Xchange Mode Envelope Creation ── */
|
|
232
|
-
/**
|
|
233
|
-
* Create a signed Xchange mode envelope (v4 wire format).
|
|
234
|
-
*
|
|
235
|
-
* Opt-in performance mode. No encryption — the share IS the payload.
|
|
236
|
-
* Ed25519 signs the raw share data for sender authentication.
|
|
237
|
-
* The XorIDA split provides confidentiality (information-theoretic).
|
|
238
|
-
*/
|
|
239
|
-
export async function createEnvelopeV4(opts) {
|
|
240
|
-
const nonce = new Uint8Array(NONCE_BYTES);
|
|
241
|
-
crypto.getRandomValues(nonce);
|
|
242
|
-
// SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
|
|
243
|
-
const timestamp = Date.now();
|
|
244
|
-
const envelopeFields = {
|
|
245
|
-
v: 4,
|
|
246
|
-
alg: 'Ed25519',
|
|
247
|
-
sender: opts.senderDid,
|
|
248
|
-
recipient: opts.recipientDid,
|
|
249
|
-
timestamp,
|
|
250
|
-
nonce: toBase64(nonce),
|
|
251
|
-
scope: opts.scope,
|
|
252
|
-
payload: toBase64(opts.shareData),
|
|
253
|
-
};
|
|
254
|
-
const signedData = createSignedData(envelopeFields);
|
|
255
|
-
const sigResult = await ed25519Sign(opts.privateKey, signedData);
|
|
256
|
-
if (!sigResult.ok)
|
|
257
|
-
return err('SIGN_FAILED');
|
|
258
|
-
const envelope = {
|
|
259
|
-
...envelopeFields,
|
|
260
|
-
kem: 'Xchange',
|
|
261
|
-
signature: toBase64(sigResult.value),
|
|
262
|
-
shareIndex: opts.shareIndex,
|
|
263
|
-
shareTotal: opts.shareTotal,
|
|
264
|
-
shareThreshold: opts.shareThreshold,
|
|
265
|
-
shareGroupId: opts.shareGroupId,
|
|
266
|
-
shareHmacKey: opts.shareHmacKey,
|
|
267
|
-
shareHmacSig: opts.shareHmacSig,
|
|
268
|
-
protocol: 'PRIVATE.ME/xBind/v3',
|
|
269
|
-
documentationUrl: 'https://private.me/docs/xbind.html',
|
|
270
|
-
};
|
|
271
|
-
return ok(envelope);
|
|
272
|
-
}
|
|
273
|
-
/* ── Decryption ── */
|
|
274
|
-
/**
|
|
275
|
-
* Decrypt the payload from a v1, v2, or v3 TransportEnvelope.
|
|
276
|
-
* Caller must verify signature before calling this.
|
|
277
|
-
*/
|
|
278
|
-
export async function decryptPayload(envelope, sharedKey) {
|
|
279
|
-
try {
|
|
280
|
-
const ciphertext = fromBase64(envelope.payload);
|
|
281
|
-
const iv = ciphertext.slice(0, AES_IV_BYTES);
|
|
282
|
-
const data = ciphertext.slice(AES_IV_BYTES);
|
|
283
|
-
const decrypted = await crypto.subtle.decrypt({ name: AES_ALGO, iv: toArrayBuffer(iv) }, sharedKey, toArrayBuffer(data));
|
|
284
|
-
return ok(new Uint8Array(decrypted));
|
|
285
|
-
}
|
|
286
|
-
catch {
|
|
287
|
-
return err('DECRYPT_FAILED');
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
/* ── Serialization ── */
|
|
291
|
-
/** Serialize envelope to UTF-8 bytes. */
|
|
292
|
-
export function serializeEnvelope(envelope) {
|
|
293
|
-
return new TextEncoder().encode(JSON.stringify(envelope));
|
|
294
|
-
}
|
|
295
|
-
/** Deserialize bytes to a validated v1 or v2 TransportEnvelope. */
|
|
296
|
-
export function deserializeEnvelope(bytes) {
|
|
297
|
-
let parsed;
|
|
298
|
-
try {
|
|
299
|
-
parsed = JSON.parse(new TextDecoder().decode(bytes));
|
|
300
|
-
}
|
|
301
|
-
catch {
|
|
302
|
-
return err('PARSE_FAILED');
|
|
303
|
-
}
|
|
304
|
-
return validateEnvelope(parsed);
|
|
305
|
-
}
|
|
306
|
-
/* ── Validation ── */
|
|
307
|
-
/** Validate a parsed object is a valid v1 or v2 TransportEnvelope. */
|
|
308
|
-
export function validateEnvelope(obj) {
|
|
309
|
-
if (typeof obj !== 'object' || obj === null)
|
|
310
|
-
return err('PARSE_FAILED');
|
|
311
|
-
const e = obj;
|
|
312
|
-
if (e['v'] !== 1 && e['v'] !== 2 && e['v'] !== 3 && e['v'] !== 4)
|
|
313
|
-
return err('INVALID_VERSION');
|
|
314
|
-
if (e['alg'] !== 'Ed25519')
|
|
315
|
-
return err('INVALID_ALG');
|
|
316
|
-
if (typeof e['sender'] !== 'string' || !e['sender'].startsWith('did:'))
|
|
317
|
-
return err('INVALID_DID');
|
|
318
|
-
if (typeof e['recipient'] !== 'string' || !e['recipient'].startsWith('did:'))
|
|
319
|
-
return err('INVALID_DID');
|
|
320
|
-
if (typeof e['timestamp'] !== 'number')
|
|
321
|
-
return err('INVALID_FIELDS');
|
|
322
|
-
if (typeof e['scope'] !== 'string')
|
|
323
|
-
return err('INVALID_FIELDS');
|
|
324
|
-
if (typeof e['payload'] !== 'string')
|
|
325
|
-
return err('INVALID_FIELDS');
|
|
326
|
-
if (typeof e['signature'] !== 'string')
|
|
327
|
-
return err('INVALID_FIELDS');
|
|
328
|
-
if (typeof e['nonce'] !== 'string')
|
|
329
|
-
return err('INVALID_NONCE');
|
|
330
|
-
try {
|
|
331
|
-
const nonceBytes = fromBase64(e['nonce']);
|
|
332
|
-
if (nonceBytes.length !== NONCE_BYTES)
|
|
333
|
-
return err('INVALID_NONCE');
|
|
334
|
-
}
|
|
335
|
-
catch {
|
|
336
|
-
return err('INVALID_NONCE');
|
|
337
|
-
}
|
|
338
|
-
if (e['ephemeralPub'] !== undefined && typeof e['ephemeralPub'] !== 'string') {
|
|
339
|
-
return err('INVALID_FIELDS');
|
|
340
|
-
}
|
|
341
|
-
// V2-specific validation
|
|
342
|
-
if (e['v'] === 2) {
|
|
343
|
-
if (e['kem'] !== 'X25519-MLKEM768')
|
|
344
|
-
return err('INVALID_KEM');
|
|
345
|
-
if (typeof e['ephemeralPub'] !== 'string')
|
|
346
|
-
return err('INVALID_FIELDS');
|
|
347
|
-
if (typeof e['kemCiphertext'] !== 'string')
|
|
348
|
-
return err('INVALID_FIELDS');
|
|
349
|
-
try {
|
|
350
|
-
const kemBytes = fromBase64(e['kemCiphertext']);
|
|
351
|
-
if (kemBytes.length !== KEM_CIPHERTEXT_BYTES)
|
|
352
|
-
return err('INVALID_FIELDS');
|
|
353
|
-
}
|
|
354
|
-
catch {
|
|
355
|
-
return err('INVALID_FIELDS');
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
// V3-specific validation (hybrid KEM + dual signatures)
|
|
359
|
-
if (e['v'] === 3) {
|
|
360
|
-
if (e['sig'] !== 'Ed25519+MLDSA65')
|
|
361
|
-
return err('INVALID_ALG');
|
|
362
|
-
if (e['kem'] !== 'X25519-MLKEM768')
|
|
363
|
-
return err('INVALID_KEM');
|
|
364
|
-
if (typeof e['ephemeralPub'] !== 'string')
|
|
365
|
-
return err('INVALID_FIELDS');
|
|
366
|
-
if (typeof e['kemCiphertext'] !== 'string')
|
|
367
|
-
return err('INVALID_FIELDS');
|
|
368
|
-
if (typeof e['pqSignature'] !== 'string')
|
|
369
|
-
return err('INVALID_FIELDS');
|
|
370
|
-
try {
|
|
371
|
-
const kemBytes = fromBase64(e['kemCiphertext']);
|
|
372
|
-
if (kemBytes.length !== KEM_CIPHERTEXT_BYTES)
|
|
373
|
-
return err('INVALID_FIELDS');
|
|
374
|
-
}
|
|
375
|
-
catch {
|
|
376
|
-
return err('INVALID_FIELDS');
|
|
377
|
-
}
|
|
378
|
-
try {
|
|
379
|
-
const pqSigBytes = fromBase64(e['pqSignature']);
|
|
380
|
-
if (pqSigBytes.length !== ML_DSA65_SIG_BYTES)
|
|
381
|
-
return err('INVALID_FIELDS');
|
|
382
|
-
}
|
|
383
|
-
catch {
|
|
384
|
-
return err('INVALID_FIELDS');
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
// V4-specific validation (Xchange — no encryption, share metadata required)
|
|
388
|
-
if (e['v'] === 4) {
|
|
389
|
-
if (e['kem'] !== 'Xchange')
|
|
390
|
-
return err('INVALID_KEM');
|
|
391
|
-
if (typeof e['shareIndex'] !== 'number')
|
|
392
|
-
return err('INVALID_FIELDS');
|
|
393
|
-
if (typeof e['shareTotal'] !== 'number')
|
|
394
|
-
return err('INVALID_FIELDS');
|
|
395
|
-
if (typeof e['shareThreshold'] !== 'number')
|
|
396
|
-
return err('INVALID_FIELDS');
|
|
397
|
-
if (typeof e['shareGroupId'] !== 'string')
|
|
398
|
-
return err('INVALID_FIELDS');
|
|
399
|
-
if (typeof e['shareHmacKey'] !== 'string')
|
|
400
|
-
return err('INVALID_FIELDS');
|
|
401
|
-
if (typeof e['shareHmacSig'] !== 'string')
|
|
402
|
-
return err('INVALID_FIELDS');
|
|
403
|
-
return ok(e);
|
|
404
|
-
}
|
|
405
|
-
if (e['shareIndex'] !== undefined && typeof e['shareIndex'] !== 'number') {
|
|
406
|
-
return err('INVALID_FIELDS');
|
|
407
|
-
}
|
|
408
|
-
if (e['shareTotal'] !== undefined && typeof e['shareTotal'] !== 'number') {
|
|
409
|
-
return err('INVALID_FIELDS');
|
|
410
|
-
}
|
|
411
|
-
if (e['shareThreshold'] !== undefined && typeof e['shareThreshold'] !== 'number') {
|
|
412
|
-
return err('INVALID_FIELDS');
|
|
413
|
-
}
|
|
414
|
-
if (e['shareGroupId'] !== undefined && typeof e['shareGroupId'] !== 'string') {
|
|
415
|
-
return err('INVALID_FIELDS');
|
|
416
|
-
}
|
|
417
|
-
if (e['shareHmacKey'] !== undefined && typeof e['shareHmacKey'] !== 'string') {
|
|
418
|
-
return err('INVALID_FIELDS');
|
|
419
|
-
}
|
|
420
|
-
if (e['shareHmacSig'] !== undefined && typeof e['shareHmacSig'] !== 'string') {
|
|
421
|
-
return err('INVALID_FIELDS');
|
|
422
|
-
}
|
|
423
|
-
return ok(e);
|
|
424
|
-
}
|
|
425
|
-
/** Generate an AES-256-GCM key for envelope encryption. */
|
|
426
|
-
export async function generateSharedKey() {
|
|
427
|
-
return crypto.subtle.generateKey({ name: AES_ALGO, length: 256 }, true, ['encrypt', 'decrypt']);
|
|
428
|
-
}
|
|
429
|
-
/* ── Signed (Unencrypted) Envelopes ── */
|
|
430
|
-
/**
|
|
431
|
-
* Create a signed but unencrypted TransportEnvelope.
|
|
432
|
-
*
|
|
433
|
-
* Payload is base64-encoded plaintext (NOT encrypted).
|
|
434
|
-
* Signature covers the raw plaintext bytes.
|
|
435
|
-
* Use for public announcements, audit logs, or telemetry where
|
|
436
|
-
* confidentiality is not required but integrity is.
|
|
437
|
-
*
|
|
438
|
-
* @param opts - Sender, recipient, scope, plaintext, and signing key.
|
|
439
|
-
* @returns Signed envelope or error.
|
|
440
|
-
*/
|
|
441
|
-
export async function createSignedEnvelope(opts) {
|
|
442
|
-
const nonce = new Uint8Array(NONCE_BYTES);
|
|
443
|
-
crypto.getRandomValues(nonce);
|
|
444
|
-
const timestamp = Date.now();
|
|
445
|
-
const payloadBase64 = toBase64(opts.plaintext);
|
|
446
|
-
const nonceBase64 = toBase64(nonce);
|
|
447
|
-
// SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
|
|
448
|
-
// (not just payload) to prevent replay attacks via nonce/timestamp mutation
|
|
449
|
-
const envelopeFields = {
|
|
450
|
-
v: 1,
|
|
451
|
-
alg: 'Ed25519',
|
|
452
|
-
sender: opts.senderDid,
|
|
453
|
-
recipient: opts.recipientDid,
|
|
454
|
-
timestamp,
|
|
455
|
-
nonce: nonceBase64,
|
|
456
|
-
scope: opts.scope,
|
|
457
|
-
payload: payloadBase64,
|
|
458
|
-
};
|
|
459
|
-
const signedData = createSignedData(envelopeFields);
|
|
460
|
-
const sigResult = await ed25519Sign(opts.privateKey, signedData);
|
|
461
|
-
if (!sigResult.ok)
|
|
462
|
-
return err('SIGN_FAILED');
|
|
463
|
-
const envelope = {
|
|
464
|
-
...envelopeFields,
|
|
465
|
-
signature: toBase64(sigResult.value),
|
|
466
|
-
};
|
|
467
|
-
return ok(envelope);
|
|
468
|
-
}
|
|
469
|
-
/**
|
|
470
|
-
* Verify signature on a signed (unencrypted) envelope and return plaintext.
|
|
471
|
-
*
|
|
472
|
-
* @param envelope - The signed envelope to verify.
|
|
473
|
-
* @param senderPublicKey - The sender's Ed25519 public key.
|
|
474
|
-
* @returns Plaintext bytes if signature is valid, error otherwise.
|
|
475
|
-
*/
|
|
476
|
-
export async function openSignedEnvelope(envelope, senderPublicKey) {
|
|
477
|
-
const payloadBytes = fromBase64(envelope.payload);
|
|
478
|
-
const sigBytes = fromBase64(envelope.signature);
|
|
479
|
-
// SECURITY FIX (v1.1.3): Verify canonical representation (not just payload)
|
|
480
|
-
const canonicalData = JSON.stringify({
|
|
481
|
-
v: envelope.v,
|
|
482
|
-
alg: envelope.alg,
|
|
483
|
-
sender: envelope.sender,
|
|
484
|
-
recipient: envelope.recipient,
|
|
485
|
-
timestamp: envelope.timestamp,
|
|
486
|
-
nonce: envelope.nonce,
|
|
487
|
-
scope: envelope.scope,
|
|
488
|
-
payload: envelope.payload,
|
|
489
|
-
});
|
|
490
|
-
const canonicalBytes = new TextEncoder().encode(canonicalData);
|
|
491
|
-
const valid = await ed25519Verify(senderPublicKey, sigBytes, canonicalBytes);
|
|
492
|
-
if (!valid.ok)
|
|
493
|
-
return err('VERIFY_FAILED');
|
|
494
|
-
if (!valid.value)
|
|
495
|
-
return err('VERIFY_FAILED');
|
|
496
|
-
return ok(payloadBytes);
|
|
497
|
-
}
|
|
1
|
+
import{ok,err}from"./_deps/shared/index.js";import{toBase64,fromBase64}from"./crypto-utils.js";import{sign as ed25519Sign,verify as ed25519Verify,signMlDsa65}from"./identity.js";const NONCE_BYTES=16,AES_IV_BYTES=12,AES_ALGO="AES-GCM",KEM_CIPHERTEXT_BYTES=1088,ML_DSA65_SIG_BYTES=3309;function toArrayBuffer(e){const r=new ArrayBuffer(e.byteLength);return new Uint8Array(r).set(e),r}function createSignedData(e){const r=JSON.stringify({v:e.v,alg:e.alg,sender:e.sender,recipient:e.recipient,timestamp:e.timestamp,nonce:e.nonce,scope:e.scope,payload:e.payload});return(new TextEncoder).encode(r)}export async function createEnvelope(e){const r=new Uint8Array(16);crypto.getRandomValues(r);const t=new Uint8Array(12);let a;crypto.getRandomValues(t);try{const r=await crypto.subtle.encrypt({name:AES_ALGO,iv:toArrayBuffer(t)},e.sharedKey,toArrayBuffer(e.plaintext)),n=new Uint8Array(t.length+r.byteLength);n.set(t),n.set(new Uint8Array(r),t.length),a=n}catch{return err("ENCRYPT_FAILED")}const n=Date.now(),o={v:1,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:n,nonce:toBase64(r),scope:e.scope,payload:toBase64(a)},s=createSignedData(o),i=await ed25519Sign(e.privateKey,s);if(!i.ok)return err("SIGN_FAILED");const c={...o,signature:toBase64(i.value),...e.ephemeralPublicKey?{ephemeralPub:toBase64(e.ephemeralPublicKey)}:{},...void 0!==e.shareIndex?{shareIndex:e.shareIndex,shareTotal:e.shareTotal,shareThreshold:e.shareThreshold,shareGroupId:e.shareGroupId,shareHmacKey:e.shareHmacKey,shareHmacSig:e.shareHmacSig}:{},protocol:"PRIVATE.ME/xBind/v3",documentationUrl:"https://private.me/docs/xbind.html"};return ok(c)}export async function createEnvelopeV2(e){const r=new Uint8Array(16);crypto.getRandomValues(r);const t=new Uint8Array(12);let a;crypto.getRandomValues(t);try{const r=await crypto.subtle.encrypt({name:AES_ALGO,iv:toArrayBuffer(t)},e.sharedKey,toArrayBuffer(e.plaintext)),n=new Uint8Array(t.length+r.byteLength);n.set(t),n.set(new Uint8Array(r),t.length),a=n}catch{return err("ENCRYPT_FAILED")}const n=Date.now(),o={v:2,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:n,nonce:toBase64(r),scope:e.scope,payload:toBase64(a)},s=createSignedData(o),i=await ed25519Sign(e.privateKey,s);if(!i.ok)return err("SIGN_FAILED");const c={...o,kem:"X25519-MLKEM768",signature:toBase64(i.value),ephemeralPub:toBase64(e.ephemeralPublicKey),kemCiphertext:toBase64(e.kemCiphertext),...void 0!==e.shareIndex?{shareIndex:e.shareIndex,shareTotal:e.shareTotal,shareThreshold:e.shareThreshold,shareGroupId:e.shareGroupId,shareHmacKey:e.shareHmacKey,shareHmacSig:e.shareHmacSig}:{},protocol:"PRIVATE.ME/xBind/v3",documentationUrl:"https://private.me/docs/xbind.html"};return ok(c)}export async function createEnvelopeV3(e){const r=new Uint8Array(16);crypto.getRandomValues(r);const t=new Uint8Array(12);let a;crypto.getRandomValues(t);try{const r=await crypto.subtle.encrypt({name:AES_ALGO,iv:toArrayBuffer(t)},e.sharedKey,toArrayBuffer(e.plaintext)),n=new Uint8Array(t.length+r.byteLength);n.set(t),n.set(new Uint8Array(r),t.length),a=n}catch{return err("ENCRYPT_FAILED")}const n=Date.now(),o={v:3,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:n,nonce:toBase64(r),scope:e.scope,payload:toBase64(a)},s=createSignedData(o),i=await ed25519Sign(e.privateKey,s);if(!i.ok)return err("SIGN_FAILED");const c=await signMlDsa65(e.mlDsaSecretKey,s);if(!c.ok)return err("PQ_SIGN_FAILED");const p={...o,sig:"Ed25519+MLDSA65",kem:"X25519-MLKEM768",signature:toBase64(i.value),pqSignature:toBase64(c.value),ephemeralPub:toBase64(e.ephemeralPublicKey),kemCiphertext:toBase64(e.kemCiphertext),...void 0!==e.shareIndex?{shareIndex:e.shareIndex,shareTotal:e.shareTotal,shareThreshold:e.shareThreshold,shareGroupId:e.shareGroupId,shareHmacKey:e.shareHmacKey,shareHmacSig:e.shareHmacSig}:{},protocol:"PRIVATE.ME/xBind/v3",documentationUrl:"https://private.me/docs/xbind.html"};return ok(p)}export async function createEnvelopeV4(e){const r=new Uint8Array(16);crypto.getRandomValues(r);const t=Date.now(),a={v:4,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:t,nonce:toBase64(r),scope:e.scope,payload:toBase64(e.shareData)},n=createSignedData(a),o=await ed25519Sign(e.privateKey,n);if(!o.ok)return err("SIGN_FAILED");const s={...a,kem:"Xchange",signature:toBase64(o.value),shareIndex:e.shareIndex,shareTotal:e.shareTotal,shareThreshold:e.shareThreshold,shareGroupId:e.shareGroupId,shareHmacKey:e.shareHmacKey,shareHmacSig:e.shareHmacSig,protocol:"PRIVATE.ME/xBind/v3",documentationUrl:"https://private.me/docs/xbind.html"};return ok(s)}export async function decryptPayload(e,r){try{const t=fromBase64(e.payload),a=t.slice(0,12),n=t.slice(12),o=await crypto.subtle.decrypt({name:AES_ALGO,iv:toArrayBuffer(a)},r,toArrayBuffer(n));return ok(new Uint8Array(o))}catch{return err("DECRYPT_FAILED")}}export function serializeEnvelope(e){return(new TextEncoder).encode(JSON.stringify(e))}export function deserializeEnvelope(e){let r;try{r=JSON.parse((new TextDecoder).decode(e))}catch{return err("PARSE_FAILED")}return validateEnvelope(r)}export function validateEnvelope(e){if("object"!=typeof e||null===e)return err("PARSE_FAILED");const r=e;if(1!==r.v&&2!==r.v&&3!==r.v&&4!==r.v)return err("INVALID_VERSION");if("Ed25519"!==r.alg)return err("INVALID_ALG");if("string"!=typeof r.sender||!r.sender.startsWith("did:"))return err("INVALID_DID");if("string"!=typeof r.recipient||!r.recipient.startsWith("did:"))return err("INVALID_DID");if("number"!=typeof r.timestamp)return err("INVALID_FIELDS");if("string"!=typeof r.scope)return err("INVALID_FIELDS");if("string"!=typeof r.payload)return err("INVALID_FIELDS");if("string"!=typeof r.signature)return err("INVALID_FIELDS");if("string"!=typeof r.nonce)return err("INVALID_NONCE");try{if(16!==fromBase64(r.nonce).length)return err("INVALID_NONCE")}catch{return err("INVALID_NONCE")}if(void 0!==r.ephemeralPub&&"string"!=typeof r.ephemeralPub)return err("INVALID_FIELDS");if(2===r.v){if("X25519-MLKEM768"!==r.kem)return err("INVALID_KEM");if("string"!=typeof r.ephemeralPub)return err("INVALID_FIELDS");if("string"!=typeof r.kemCiphertext)return err("INVALID_FIELDS");try{if(1088!==fromBase64(r.kemCiphertext).length)return err("INVALID_FIELDS")}catch{return err("INVALID_FIELDS")}}if(3===r.v){if("Ed25519+MLDSA65"!==r.sig)return err("INVALID_ALG");if("X25519-MLKEM768"!==r.kem)return err("INVALID_KEM");if("string"!=typeof r.ephemeralPub)return err("INVALID_FIELDS");if("string"!=typeof r.kemCiphertext)return err("INVALID_FIELDS");if("string"!=typeof r.pqSignature)return err("INVALID_FIELDS");try{if(1088!==fromBase64(r.kemCiphertext).length)return err("INVALID_FIELDS")}catch{return err("INVALID_FIELDS")}try{if(3309!==fromBase64(r.pqSignature).length)return err("INVALID_FIELDS")}catch{return err("INVALID_FIELDS")}}return 4===r.v?"Xchange"!==r.kem?err("INVALID_KEM"):"number"!=typeof r.shareIndex||"number"!=typeof r.shareTotal||"number"!=typeof r.shareThreshold||"string"!=typeof r.shareGroupId||"string"!=typeof r.shareHmacKey||"string"!=typeof r.shareHmacSig?err("INVALID_FIELDS"):ok(r):void 0!==r.shareIndex&&"number"!=typeof r.shareIndex||void 0!==r.shareTotal&&"number"!=typeof r.shareTotal||void 0!==r.shareThreshold&&"number"!=typeof r.shareThreshold||void 0!==r.shareGroupId&&"string"!=typeof r.shareGroupId||void 0!==r.shareHmacKey&&"string"!=typeof r.shareHmacKey||void 0!==r.shareHmacSig&&"string"!=typeof r.shareHmacSig?err("INVALID_FIELDS"):ok(r)}export async function generateSharedKey(){return crypto.subtle.generateKey({name:AES_ALGO,length:256},!0,["encrypt","decrypt"])}export async function createSignedEnvelope(e){const r=new Uint8Array(16);crypto.getRandomValues(r);const t=Date.now(),a=toBase64(e.plaintext),n=toBase64(r),o={v:1,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:t,nonce:n,scope:e.scope,payload:a},s=createSignedData(o),i=await ed25519Sign(e.privateKey,s);if(!i.ok)return err("SIGN_FAILED");const c={...o,signature:toBase64(i.value)};return ok(c)}export async function openSignedEnvelope(e,r){const t=fromBase64(e.payload),a=fromBase64(e.signature),n=JSON.stringify({v:e.v,alg:e.alg,sender:e.sender,recipient:e.recipient,timestamp:e.timestamp,nonce:e.nonce,scope:e.scope,payload:e.payload}),o=(new TextEncoder).encode(n),s=await ed25519Verify(r,a,o);return s.ok&&s.value?ok(t):err("VERIFY_FAILED")}
|
|
@@ -49,6 +49,16 @@ export declare class XBindAgentError extends XBindError {
|
|
|
49
49
|
export declare class XBindBillingError extends XBindError {
|
|
50
50
|
constructor(code: string, message: string);
|
|
51
51
|
}
|
|
52
|
+
/** Vault Store errors (Full Control IP protection, crypto loading). */
|
|
53
|
+
export declare class VaultStoreError extends XBindError {
|
|
54
|
+
constructor(code: string, message: string);
|
|
55
|
+
}
|
|
56
|
+
/** Quota exceeded error (Free tier >120K hard cap). */
|
|
57
|
+
export declare class QuotaExceededError extends XBindBillingError {
|
|
58
|
+
/** Upgrade URL for Pro tier */
|
|
59
|
+
readonly upgradeUrl: string;
|
|
60
|
+
constructor(message: string, upgradeUrl?: string);
|
|
61
|
+
}
|
|
52
62
|
/**
|
|
53
63
|
* Create detailed error information for a given error code.
|
|
54
64
|
*
|