@abraca/dabra 2.90.0 → 2.92.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/package.json +5 -11
- package/src/AbracadabraBaseProvider.ts +0 -963
- package/src/AbracadabraClient.ts +0 -1960
- package/src/AbracadabraProvider.ts +0 -915
- package/src/AbracadabraWS.ts +0 -695
- package/src/BackgroundSyncManager.ts +0 -856
- package/src/BackgroundSyncPersistence.ts +0 -140
- package/src/ChatClient.ts +0 -314
- package/src/CloseEvents.ts +0 -49
- package/src/ContentManager.ts +0 -246
- package/src/CoverReconcile.ts +0 -168
- package/src/CryptoIdentityKeystore.ts +0 -754
- package/src/DeviceRegistrationService.ts +0 -257
- package/src/DocConverters.ts +0 -1859
- package/src/DocKeyManager.ts +0 -163
- package/src/DocTypes.ts +0 -628
- package/src/DocUtils.ts +0 -251
- package/src/DocumentCache.ts +0 -210
- package/src/DocumentManager.ts +0 -410
- package/src/E2EAbracadabraProvider.ts +0 -426
- package/src/E2EOfflineStore.ts +0 -55
- package/src/EncryptedChatClient.ts +0 -173
- package/src/EncryptedY.ts +0 -145
- package/src/EventEmitter.ts +0 -59
- package/src/FileBlobStore.ts +0 -569
- package/src/IdentityDoc.ts +0 -711
- package/src/IncomingMessage.ts +0 -63
- package/src/MessageReceiver.ts +0 -151
- package/src/MessageSender.ts +0 -22
- package/src/MetaManager.ts +0 -194
- package/src/MnemonicKeyDerivation.ts +0 -171
- package/src/NodeConnect.ts +0 -63
- package/src/NodeIdentity.ts +0 -378
- package/src/NotificationsClient.ts +0 -207
- package/src/OfflineStore.ts +0 -294
- package/src/OutgoingMessage.ts +0 -25
- package/src/OutgoingMessages/AuthenticationMessage.ts +0 -23
- package/src/OutgoingMessages/AwarenessMessage.ts +0 -41
- package/src/OutgoingMessages/CloseMessage.ts +0 -17
- package/src/OutgoingMessages/QueryAwarenessMessage.ts +0 -17
- package/src/OutgoingMessages/StatelessMessage.ts +0 -18
- package/src/OutgoingMessages/SubdocMessage.ts +0 -35
- package/src/OutgoingMessages/SyncStepOneMessage.ts +0 -25
- package/src/OutgoingMessages/SyncStepTwoMessage.ts +0 -25
- package/src/OutgoingMessages/UpdateMessage.ts +0 -20
- package/src/QueryClient.ts +0 -209
- package/src/RpcClient.ts +0 -659
- package/src/SchemaTypes.ts +0 -179
- package/src/SearchIndex.ts +0 -250
- package/src/ServerRegistry.ts +0 -220
- package/src/ServerReset.ts +0 -222
- package/src/TokenManager.ts +0 -337
- package/src/TreeManager.ts +0 -894
- package/src/TreeTimestamps.ts +0 -101
- package/src/auth.ts +0 -62
- package/src/awarenessStatesToArray.ts +0 -10
- package/src/index.ts +0 -219
- package/src/messageRecord.ts +0 -121
- package/src/sync/BroadcastChannelSync.ts +0 -235
- package/src/types.ts +0 -790
- package/src/webrtc/AbracadabraWebRTC.ts +0 -683
- package/src/webrtc/DataChannelRouter.ts +0 -179
- package/src/webrtc/DevicePairingChannel.ts +0 -496
- package/src/webrtc/E2EEChannel.ts +0 -195
- package/src/webrtc/FileTransferChannel.ts +0 -367
- package/src/webrtc/ManualSignaling.ts +0 -204
- package/src/webrtc/PeerConnection.ts +0 -133
- package/src/webrtc/SignalingSocket.ts +0 -378
- package/src/webrtc/YjsDataChannel.ts +0 -203
- package/src/webrtc/index.ts +0 -26
- package/src/webrtc/types.ts +0 -177
package/src/DocKeyManager.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DocKeyManager
|
|
3
|
-
*
|
|
4
|
-
* Manages AES-256-GCM document keys for CSE and E2E encrypted documents.
|
|
5
|
-
* Keys are wrapped per-user using X25519 ECDH + HKDF-SHA256 + AES-256-GCM.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { x25519 } from "@noble/curves/ed25519.js";
|
|
9
|
-
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
10
|
-
import { sha256 } from "@noble/hashes/sha2.js";
|
|
11
|
-
import type { AbracadabraClient } from "./AbracadabraClient.ts";
|
|
12
|
-
import type { CryptoIdentityKeystore } from "./CryptoIdentityKeystore.ts";
|
|
13
|
-
|
|
14
|
-
const HKDF_INFO = new TextEncoder().encode("abracadabra-dockey-v1");
|
|
15
|
-
|
|
16
|
-
function fromBase64(b64: string): Uint8Array {
|
|
17
|
-
// Accept both standard base64 and base64url, padded or not — server
|
|
18
|
-
// payloads mix the two alphabets depending on the field.
|
|
19
|
-
let normalized = b64.replace(/-/g, "+").replace(/_/g, "/");
|
|
20
|
-
const pad = normalized.length % 4;
|
|
21
|
-
if (pad) normalized += "=".repeat(4 - pad);
|
|
22
|
-
return Uint8Array.from(atob(normalized), (c) => c.charCodeAt(0));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class DocKeyManager {
|
|
26
|
-
private cache = new Map<string, { key: CryptoKey; epoch: number; fetchedAt: number }>();
|
|
27
|
-
private static readonly CACHE_TTL = 10 * 60 * 1000; // 10 minutes
|
|
28
|
-
|
|
29
|
-
/** Generate a new random AES-256-GCM document key. */
|
|
30
|
-
static async generateDocKey(): Promise<CryptoKey> {
|
|
31
|
-
return crypto.subtle.generateKey(
|
|
32
|
-
{ name: "AES-GCM", length: 256 },
|
|
33
|
-
true,
|
|
34
|
-
["encrypt", "decrypt"],
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Get (or fetch) the DocKey for a document.
|
|
40
|
-
* Returns null if no envelope exists (user not provisioned).
|
|
41
|
-
*/
|
|
42
|
-
async getDocKey(
|
|
43
|
-
docId: string,
|
|
44
|
-
client: AbracadabraClient,
|
|
45
|
-
keystore: CryptoIdentityKeystore,
|
|
46
|
-
): Promise<CryptoKey | null> {
|
|
47
|
-
const cached = this.cache.get(docId);
|
|
48
|
-
if (cached && Date.now() - cached.fetchedAt < DocKeyManager.CACHE_TTL) {
|
|
49
|
-
return cached.key;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const envelope = await client.getMyKeyEnvelope(docId);
|
|
53
|
-
if (envelope) {
|
|
54
|
-
const x25519PrivKey = await keystore.getX25519PrivateKey();
|
|
55
|
-
try {
|
|
56
|
-
const wrapped = fromBase64(envelope.encrypted_key);
|
|
57
|
-
const docKey = await this._unwrapKey(wrapped, x25519PrivKey, docId);
|
|
58
|
-
this.cache.set(docId, { key: docKey, epoch: envelope.key_epoch, fetchedAt: Date.now() });
|
|
59
|
-
return docKey;
|
|
60
|
-
} finally {
|
|
61
|
-
x25519PrivKey.fill(0);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Inheritance fallback. The doc has no envelope of our own — but it
|
|
66
|
-
// might inherit its encryption mode from an ancestor (e.g. a child
|
|
67
|
-
// page under an E2E space). The ancestor IS provisioned for us
|
|
68
|
-
// (otherwise we couldn't open the parent either). Resolve the
|
|
69
|
-
// encryption-source via /docs/:id/encryption.inherited_from, fetch
|
|
70
|
-
// OUR envelope on that source, unwrap with the source's salt,
|
|
71
|
-
// re-wrap for the current docId with its own salt, upload the new
|
|
72
|
-
// envelope so subsequent calls hit the direct-envelope path.
|
|
73
|
-
try {
|
|
74
|
-
const enc = await client.getDocEncryption(docId);
|
|
75
|
-
if (enc.effective_mode !== "e2e" || !enc.inherited_from || enc.inherited_from === docId) {
|
|
76
|
-
return null;
|
|
77
|
-
}
|
|
78
|
-
const sourceEnv = await client.getMyKeyEnvelope(enc.inherited_from);
|
|
79
|
-
if (!sourceEnv) return null;
|
|
80
|
-
const x25519PrivKey = await keystore.getX25519PrivateKey();
|
|
81
|
-
try {
|
|
82
|
-
const wrappedSrc = fromBase64(sourceEnv.encrypted_key);
|
|
83
|
-
const docKey = await this._unwrapKey(wrappedSrc, x25519PrivKey, enc.inherited_from);
|
|
84
|
-
this.cache.set(docId, { key: docKey, epoch: sourceEnv.key_epoch, fetchedAt: Date.now() });
|
|
85
|
-
// Re-wrap for the current doc + upload so future opens take
|
|
86
|
-
// the direct path. Best-effort — caller already has the key.
|
|
87
|
-
const me = await client.getMe();
|
|
88
|
-
if (me.publicKey) {
|
|
89
|
-
const myKeys = await client.listUserKeys(me.id);
|
|
90
|
-
const primaryKey = myKeys[0];
|
|
91
|
-
if (primaryKey?.x25519Key) {
|
|
92
|
-
const x25519Pub = fromBase64(primaryKey.x25519Key);
|
|
93
|
-
const rewrapped = await this.wrapKeyForRecipient(docKey, x25519Pub, docId);
|
|
94
|
-
const b64 = (() => {
|
|
95
|
-
let s = "";
|
|
96
|
-
for (let i = 0; i < rewrapped.length; i++) s += String.fromCharCode(rewrapped[i]!);
|
|
97
|
-
return btoa(s);
|
|
98
|
-
})();
|
|
99
|
-
await client.uploadKeyEnvelopes(docId, {
|
|
100
|
-
key_epoch: sourceEnv.key_epoch,
|
|
101
|
-
envelopes: [{ recipient_key_id: primaryKey.id, encrypted_key: b64 }],
|
|
102
|
-
}).catch(() => null);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return docKey;
|
|
106
|
-
} finally {
|
|
107
|
-
x25519PrivKey.fill(0);
|
|
108
|
-
}
|
|
109
|
-
} catch {
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Wrap a DocKey for a recipient.
|
|
116
|
-
* Output: [ephemeralPub(32) || nonce(12) || ciphertext(~48)] bytes
|
|
117
|
-
*/
|
|
118
|
-
async wrapKeyForRecipient(
|
|
119
|
-
docKey: CryptoKey,
|
|
120
|
-
recipientX25519PubKey: Uint8Array,
|
|
121
|
-
docId: string,
|
|
122
|
-
): Promise<Uint8Array> {
|
|
123
|
-
const ephemeralPriv = crypto.getRandomValues(new Uint8Array(32));
|
|
124
|
-
const ephemeralPub = x25519.getPublicKey(ephemeralPriv);
|
|
125
|
-
const sharedSecret = x25519.getSharedSecret(ephemeralPriv, recipientX25519PubKey);
|
|
126
|
-
|
|
127
|
-
const salt = new TextEncoder().encode(docId);
|
|
128
|
-
const keyBytes = hkdf(sha256, sharedSecret, salt, HKDF_INFO, 32);
|
|
129
|
-
const wrapKey = await crypto.subtle.importKey("raw", keyBytes as BufferSource, { name: "AES-GCM" }, false, ["encrypt"]);
|
|
130
|
-
|
|
131
|
-
const rawDocKey = await crypto.subtle.exportKey("raw", docKey);
|
|
132
|
-
const nonce = crypto.getRandomValues(new Uint8Array(12));
|
|
133
|
-
const ciphertext = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv: nonce as BufferSource }, wrapKey, rawDocKey));
|
|
134
|
-
|
|
135
|
-
const result = new Uint8Array(32 + 12 + ciphertext.length);
|
|
136
|
-
result.set(ephemeralPub, 0);
|
|
137
|
-
result.set(nonce, 32);
|
|
138
|
-
result.set(ciphertext, 44);
|
|
139
|
-
return result;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private async _unwrapKey(wrapped: Uint8Array, recipientX25519PrivKey: Uint8Array, docId: string): Promise<CryptoKey> {
|
|
143
|
-
const ephemeralPub = wrapped.slice(0, 32);
|
|
144
|
-
const nonce = wrapped.slice(32, 44);
|
|
145
|
-
const ciphertext = wrapped.slice(44);
|
|
146
|
-
|
|
147
|
-
const sharedSecret = x25519.getSharedSecret(recipientX25519PrivKey, ephemeralPub);
|
|
148
|
-
const salt = new TextEncoder().encode(docId);
|
|
149
|
-
const keyBytes = hkdf(sha256, sharedSecret, salt, HKDF_INFO, 32);
|
|
150
|
-
const wrapKey = await crypto.subtle.importKey("raw", keyBytes as BufferSource, { name: "AES-GCM" }, false, ["decrypt"]);
|
|
151
|
-
const rawDocKey = await crypto.subtle.decrypt({ name: "AES-GCM", iv: nonce as BufferSource }, wrapKey, ciphertext as BufferSource);
|
|
152
|
-
return crypto.subtle.importKey("raw", rawDocKey, { name: "AES-GCM" }, true, ["encrypt", "decrypt"]);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Clear the cached key for a document (or all if docId omitted). */
|
|
156
|
-
clearCache(docId?: string): void {
|
|
157
|
-
if (docId) {
|
|
158
|
-
this.cache.delete(docId);
|
|
159
|
-
} else {
|
|
160
|
-
this.cache.clear();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|