@towns-protocol/encryption 0.0.191

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.
Files changed (99) hide show
  1. package/LICENSE.txt +21 -0
  2. package/README.md +3 -0
  3. package/dist/base.d.ts +64 -0
  4. package/dist/base.d.ts.map +1 -0
  5. package/dist/base.js +44 -0
  6. package/dist/base.js.map +1 -0
  7. package/dist/cryptoAesGcm.d.ts +9 -0
  8. package/dist/cryptoAesGcm.d.ts.map +1 -0
  9. package/dist/cryptoAesGcm.js +30 -0
  10. package/dist/cryptoAesGcm.js.map +1 -0
  11. package/dist/cryptoStore.d.ts +52 -0
  12. package/dist/cryptoStore.d.ts.map +1 -0
  13. package/dist/cryptoStore.js +131 -0
  14. package/dist/cryptoStore.js.map +1 -0
  15. package/dist/decryptionExtensions.d.ts +200 -0
  16. package/dist/decryptionExtensions.d.ts.map +1 -0
  17. package/dist/decryptionExtensions.js +687 -0
  18. package/dist/decryptionExtensions.js.map +1 -0
  19. package/dist/derivedEncryption.d.ts +2 -0
  20. package/dist/derivedEncryption.d.ts.map +1 -0
  21. package/dist/derivedEncryption.js +2 -0
  22. package/dist/derivedEncryption.js.map +1 -0
  23. package/dist/encryptionDelegate.d.ts +20 -0
  24. package/dist/encryptionDelegate.d.ts.map +1 -0
  25. package/dist/encryptionDelegate.js +86 -0
  26. package/dist/encryptionDelegate.js.map +1 -0
  27. package/dist/encryptionDevice.d.ts +264 -0
  28. package/dist/encryptionDevice.d.ts.map +1 -0
  29. package/dist/encryptionDevice.js +742 -0
  30. package/dist/encryptionDevice.js.map +1 -0
  31. package/dist/encryptionTypes.d.ts +20 -0
  32. package/dist/encryptionTypes.d.ts.map +1 -0
  33. package/dist/encryptionTypes.js +2 -0
  34. package/dist/encryptionTypes.js.map +1 -0
  35. package/dist/groupDecryption.d.ts +34 -0
  36. package/dist/groupDecryption.d.ts.map +1 -0
  37. package/dist/groupDecryption.js +84 -0
  38. package/dist/groupDecryption.js.map +1 -0
  39. package/dist/groupEncryption.d.ts +36 -0
  40. package/dist/groupEncryption.d.ts.map +1 -0
  41. package/dist/groupEncryption.js +90 -0
  42. package/dist/groupEncryption.js.map +1 -0
  43. package/dist/groupEncryptionCrypto.d.ts +119 -0
  44. package/dist/groupEncryptionCrypto.d.ts.map +1 -0
  45. package/dist/groupEncryptionCrypto.js +256 -0
  46. package/dist/groupEncryptionCrypto.js.map +1 -0
  47. package/dist/hybridGroupDecryption.d.ts +33 -0
  48. package/dist/hybridGroupDecryption.d.ts.map +1 -0
  49. package/dist/hybridGroupDecryption.js +84 -0
  50. package/dist/hybridGroupDecryption.js.map +1 -0
  51. package/dist/hybridGroupEncryption.d.ts +30 -0
  52. package/dist/hybridGroupEncryption.d.ts.map +1 -0
  53. package/dist/hybridGroupEncryption.js +92 -0
  54. package/dist/hybridGroupEncryption.js.map +1 -0
  55. package/dist/index.d.ts +19 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +19 -0
  58. package/dist/index.js.map +1 -0
  59. package/dist/olmLib.d.ts +35 -0
  60. package/dist/olmLib.d.ts.map +1 -0
  61. package/dist/olmLib.js +37 -0
  62. package/dist/olmLib.js.map +1 -0
  63. package/dist/storeTypes.d.ts +27 -0
  64. package/dist/storeTypes.d.ts.map +1 -0
  65. package/dist/storeTypes.js +2 -0
  66. package/dist/storeTypes.js.map +1 -0
  67. package/dist/tests/cryptoAesGcm.test.d.ts +2 -0
  68. package/dist/tests/cryptoAesGcm.test.d.ts.map +1 -0
  69. package/dist/tests/cryptoAesGcm.test.js +71 -0
  70. package/dist/tests/cryptoAesGcm.test.js.map +1 -0
  71. package/dist/tests/cryptoStore.test.d.ts +5 -0
  72. package/dist/tests/cryptoStore.test.d.ts.map +1 -0
  73. package/dist/tests/cryptoStore.test.js +114 -0
  74. package/dist/tests/cryptoStore.test.js.map +1 -0
  75. package/dist/tests/decryptionExtensions.test.d.ts +2 -0
  76. package/dist/tests/decryptionExtensions.test.d.ts.map +1 -0
  77. package/dist/tests/decryptionExtensions.test.js +355 -0
  78. package/dist/tests/decryptionExtensions.test.js.map +1 -0
  79. package/dist/tests/encryption-protocol.test.d.ts +2 -0
  80. package/dist/tests/encryption-protocol.test.d.ts.map +1 -0
  81. package/dist/tests/encryption-protocol.test.js +150 -0
  82. package/dist/tests/encryption-protocol.test.js.map +1 -0
  83. package/dist/tests/encryptionDelegate.test.d.ts +2 -0
  84. package/dist/tests/encryptionDelegate.test.d.ts.map +1 -0
  85. package/dist/tests/encryptionDelegate.test.js +78 -0
  86. package/dist/tests/encryptionDelegate.test.js.map +1 -0
  87. package/dist/tests/group-encryption-protocol.test.d.ts +2 -0
  88. package/dist/tests/group-encryption-protocol.test.d.ts.map +1 -0
  89. package/dist/tests/group-encryption-protocol.test.js +103 -0
  90. package/dist/tests/group-encryption-protocol.test.js.map +1 -0
  91. package/dist/tests/group-encryptionDelegate.test.d.ts +2 -0
  92. package/dist/tests/group-encryptionDelegate.test.d.ts.map +1 -0
  93. package/dist/tests/group-encryptionDelegate.test.js +23 -0
  94. package/dist/tests/group-encryptionDelegate.test.js.map +1 -0
  95. package/dist/tests/pk.test.d.ts +2 -0
  96. package/dist/tests/pk.test.d.ts.map +1 -0
  97. package/dist/tests/pk.test.js +103 -0
  98. package/dist/tests/pk.test.js.map +1 -0
  99. package/package.json +54 -0
@@ -0,0 +1,742 @@
1
+ import { GroupEncryptionAlgorithmId } from './olmLib';
2
+ import { bin_equal, bin_fromHexString, bin_toHexString, dlog } from '@towns-protocol/dlog';
3
+ import { ExportedDeviceSchema, ExportedDevice_GroupSessionSchema, ExportedDevice_HybridGroupSessionSchema, HybridGroupSessionKeySchema, } from '@towns-protocol/proto';
4
+ import { exportAesGsmKeyBytes, generateNewAesGcmKey } from './cryptoAesGcm';
5
+ import { Dexie } from 'dexie';
6
+ import { create, fromBinary, toBinary } from '@bufbuild/protobuf';
7
+ const log = dlog('csb:encryption:encryptionDevice');
8
+ // The maximum size of an event is 65K, and we base64 the content, so this is a
9
+ // reasonable approximation to the biggest plaintext we can encrypt.
10
+ const MAX_PLAINTEXT_LENGTH = (65536 * 3) / 4;
11
+ function checkPayloadLength(payloadString, opts) {
12
+ if (payloadString === undefined) {
13
+ throw new Error('payloadString undefined');
14
+ }
15
+ if (payloadString.length > MAX_PLAINTEXT_LENGTH) {
16
+ // might as well fail early here rather than letting the olm library throw
17
+ // a cryptic memory allocation error.
18
+ throw new Error(`Message too long (${payloadString.length} bytes). ` +
19
+ `The maximum for an encrypted message is ${MAX_PLAINTEXT_LENGTH} bytes.` +
20
+ `streamId: ${opts.streamId}, source: ${opts.source}`);
21
+ }
22
+ }
23
+ export class EncryptionDevice {
24
+ delegate;
25
+ cryptoStore;
26
+ // https://linear.app/hnt-labs/issue/HNT-4273/pick-a-better-pickle-key-in-olmdevice
27
+ pickleKey = 'DEFAULT_KEY'; // set by consumers
28
+ /** Curve25519 key for the account, unknown until we load the account from storage in init() */
29
+ deviceCurve25519Key = null;
30
+ /** Ed25519 key for the account, unknown until we load the account from storage in init() */
31
+ deviceDoNotUseKey = null;
32
+ // keyId: base64(key)
33
+ fallbackKey = { keyId: '', key: '' };
34
+ // Keep track of sessions that we're starting, so that we don't start
35
+ // multiple sessions for the same device at the same time.
36
+ sessionsInProgress = {}; // set by consumers
37
+ // Used by olm to serialise prekey message decryptions
38
+ // todo: ensure we need this to serialize prekey message given we're using fallback keys
39
+ // not one time keys, which suffer a race condition and expire once used.
40
+ olmPrekeyPromise = Promise.resolve(); // set by consumers
41
+ // Store a set of decrypted message indexes for each group session.
42
+ // This partially mitigates a replay attack where a MITM resends a group
43
+ // message into the room.
44
+ //
45
+ // Keys are strings of form "<senderKey>|<session_id>|<message_index>"
46
+ // Values are objects of the form "{id: <event id>, timestamp: <ts>}"
47
+ inboundGroupSessionMessageIndexes = {};
48
+ constructor(delegate, cryptoStore) {
49
+ this.delegate = delegate;
50
+ this.cryptoStore = cryptoStore;
51
+ }
52
+ /**
53
+ * Iniitialize the Account. Must be called prior to any other operation
54
+ * on the device.
55
+ *
56
+ * Data from an exported device can be provided in order to recreate this device.
57
+ *
58
+ * Attempts to load the Account from the crypto store, or create one otherwise
59
+ * storing the account in storage.
60
+ *
61
+ * Reads the device keys from the Account object.
62
+ *
63
+ * @param fromExportedDevice - data from exported device
64
+ * that must be re-created.
65
+ * If present, opts.pickleKey is ignored
66
+ * (exported data already provides a pickle key)
67
+ * @param pickleKey - pickle key to set instead of default one
68
+ *
69
+ *
70
+ */
71
+ async init(opts) {
72
+ const { fromExportedDevice, pickleKey } = opts ?? {};
73
+ let e2eKeys;
74
+ if (!this.delegate.initialized) {
75
+ await this.delegate.init();
76
+ }
77
+ const account = this.delegate.createAccount();
78
+ try {
79
+ if (fromExportedDevice) {
80
+ this.pickleKey = fromExportedDevice.pickleKey;
81
+ await this.initializeFromExportedDevice(fromExportedDevice, account);
82
+ }
83
+ else {
84
+ if (pickleKey) {
85
+ this.pickleKey = pickleKey;
86
+ }
87
+ await this.initializeAccount(account);
88
+ }
89
+ await this.generateFallbackKeyIfNeeded();
90
+ e2eKeys = JSON.parse(account.identity_keys());
91
+ this.fallbackKey = await this.getFallbackKey();
92
+ }
93
+ finally {
94
+ account.free();
95
+ }
96
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
97
+ this.deviceCurve25519Key = e2eKeys.curve25519;
98
+ // note jterzis 07/19/23: deprecating ed25519 key in favor of TDK
99
+ // see: https://linear.app/hnt-labs/issue/HNT-1796/tdk-signature-storage-curve25519-key
100
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
101
+ this.deviceDoNotUseKey = e2eKeys.ed25519;
102
+ log(`init: deviceCurve25519Key: ${this.deviceCurve25519Key}, fallbackKey ${JSON.stringify(this.fallbackKey)}`);
103
+ }
104
+ async initializeFromExportedDevice(exportedData, account) {
105
+ await this.cryptoStore.withAccountTx(() => this.cryptoStore.storeAccount(exportedData.pickledAccount));
106
+ await this.cryptoStore.withGroupSessions(() => {
107
+ return Promise.all([
108
+ ...exportedData.outboundSessions.map((session) => this.cryptoStore.storeEndToEndOutboundGroupSession(session.sessionId, session.session, session.streamId)),
109
+ ...exportedData.inboundSessions.map((session) => this.cryptoStore.storeEndToEndInboundGroupSession(session.streamId, session.sessionId, {
110
+ stream_id: session.streamId,
111
+ session: session.session,
112
+ keysClaimed: {},
113
+ })),
114
+ ...exportedData.hybridGroupSessions.map((session) => this.cryptoStore.storeHybridGroupSession(session)),
115
+ ]);
116
+ });
117
+ account.unpickle(this.pickleKey, exportedData.pickledAccount);
118
+ }
119
+ async initializeAccount(account) {
120
+ try {
121
+ const pickledAccount = await this.cryptoStore.getAccount();
122
+ account.unpickle(this.pickleKey, pickledAccount);
123
+ }
124
+ catch {
125
+ account.create();
126
+ const pickledAccount = account.pickle(this.pickleKey);
127
+ await this.cryptoStore.storeAccount(pickledAccount);
128
+ }
129
+ }
130
+ /**
131
+ * Export the current device state
132
+ * @returns ExportedDevice object containing the device state
133
+ */
134
+ async exportDevice() {
135
+ const account = await this.getAccount();
136
+ const pickledAccount = account.pickle(this.pickleKey);
137
+ account.free();
138
+ const [inboundSessions, outboundSessions, hybridGroupSessions] = await Promise.all([
139
+ this.cryptoStore.getAllEndToEndInboundGroupSessions(),
140
+ this.cryptoStore.getAllEndToEndOutboundGroupSessions(),
141
+ this.cryptoStore.getAllHybridGroupSessions(),
142
+ ]);
143
+ return create(ExportedDeviceSchema, {
144
+ pickleKey: this.pickleKey,
145
+ pickledAccount,
146
+ inboundSessions: inboundSessions.map((session) => create(ExportedDevice_GroupSessionSchema, {
147
+ sessionId: session.sessionId,
148
+ streamId: session.streamId,
149
+ session: session.session,
150
+ })),
151
+ outboundSessions: outboundSessions.map((session) => create(ExportedDevice_GroupSessionSchema, {
152
+ sessionId: session.sessionId,
153
+ streamId: session.streamId,
154
+ session: session.session,
155
+ })),
156
+ hybridGroupSessions: hybridGroupSessions.map((session) => create(ExportedDevice_HybridGroupSessionSchema, {
157
+ sessionId: session.sessionId,
158
+ streamId: session.streamId,
159
+ sessionKey: session.sessionKey,
160
+ miniblockNum: session.miniblockNum,
161
+ })),
162
+ });
163
+ }
164
+ /**
165
+ * Extract our Account from the crypto store and call the given function
166
+ * with the account object
167
+ * The `account` object is usable only within the callback passed to this
168
+ * function and will be freed as soon the callback returns. It is *not*
169
+ * usable for the rest of the lifetime of the transaction.
170
+ * This function requires a live transaction object from cryptoStore.doTxn()
171
+ * and therefore may only be called in a doTxn() callback.
172
+ *
173
+ * @param txn - Opaque transaction object from cryptoStore.doTxn()
174
+ * @internal
175
+ */
176
+ async getAccount() {
177
+ const pickledAccount = await this.cryptoStore.getAccount();
178
+ const account = this.delegate.createAccount();
179
+ account.unpickle(this.pickleKey, pickledAccount);
180
+ return account;
181
+ }
182
+ /**
183
+ * Saves an account to the crypto store.
184
+ * This function requires a live transaction object from cryptoStore.doTxn()
185
+ * and therefore may only be called in a doTxn() callback.
186
+ *
187
+ * @param txn - Opaque transaction object from cryptoStore.doTxn()
188
+ * @param Account object
189
+ * @internal
190
+ */
191
+ async storeAccount(account) {
192
+ await this.cryptoStore.storeAccount(account.pickle(this.pickleKey));
193
+ }
194
+ /**
195
+ * get an OlmUtility and call the given function
196
+ *
197
+ * @returns result of func
198
+ * @internal
199
+ */
200
+ getUtility(func) {
201
+ const utility = this.delegate.createUtility();
202
+ try {
203
+ return func(utility);
204
+ }
205
+ finally {
206
+ utility.free();
207
+ }
208
+ }
209
+ /**
210
+ * Signs a message with the ed25519 key for this account.
211
+ *
212
+ * @param message - message to be signed
213
+ * @returns base64-encoded signature
214
+ */
215
+ async sign(message) {
216
+ const account = await this.getAccount();
217
+ return account.sign(message);
218
+ }
219
+ /**
220
+ * Marks all of the fallback keys as published.
221
+ */
222
+ async markKeysAsPublished() {
223
+ const account = await this.getAccount();
224
+ account.mark_keys_as_published();
225
+ await this.storeAccount(account);
226
+ }
227
+ /**
228
+ * Generate a new fallback keys
229
+ *
230
+ * @returns Resolved once the account is saved back having generated the key
231
+ */
232
+ async generateFallbackKeyIfNeeded() {
233
+ try {
234
+ await this.getFallbackKey();
235
+ }
236
+ catch {
237
+ const account = await this.getAccount();
238
+ account.generate_fallback_key();
239
+ await this.storeAccount(account);
240
+ }
241
+ }
242
+ async getFallbackKey() {
243
+ const account = await this.getAccount();
244
+ const record = JSON.parse(account.unpublished_fallback_key());
245
+ const key = Object.values(record.curve25519)[0];
246
+ const keyId = Object.keys(record.curve25519)[0];
247
+ if (!key || !keyId) {
248
+ throw new Error('No fallback key');
249
+ }
250
+ return { key, keyId };
251
+ }
252
+ async forgetOldFallbackKey() {
253
+ const account = await this.getAccount();
254
+ account.forget_old_fallback_key();
255
+ await this.storeAccount(account);
256
+ }
257
+ // Outbound group session
258
+ // ======================
259
+ /**
260
+ * Store an OutboundGroupSession in outboundSessionStore
261
+ *
262
+ */
263
+ async saveOutboundGroupSession(session, streamId) {
264
+ return this.cryptoStore.withGroupSessions(async () => {
265
+ await this.cryptoStore.storeEndToEndOutboundGroupSession(session.session_id(), session.pickle(this.pickleKey), streamId);
266
+ });
267
+ }
268
+ /**
269
+ * Extract OutboundGroupSession from the session store and call given fn.
270
+ */
271
+ async getOutboundGroupSession(streamId) {
272
+ return this.cryptoStore.withGroupSessions(async () => {
273
+ const pickled = await this.cryptoStore.getEndToEndOutboundGroupSession(streamId);
274
+ if (!pickled) {
275
+ throw new Error(`Unknown outbound group session ${streamId}`);
276
+ }
277
+ const session = this.delegate.createOutboundGroupSession();
278
+ session.unpickle(this.pickleKey, pickled);
279
+ return session;
280
+ });
281
+ }
282
+ /**
283
+ * Get the session keys for an outbound group session
284
+ *
285
+ * @param sessionId - the id of the outbound group session
286
+ *
287
+ * @returns current chain index, and
288
+ * base64-encoded secret key.
289
+ */
290
+ async getOutboundGroupSessionKey(streamId) {
291
+ const session = await this.getOutboundGroupSession(streamId);
292
+ const chain_index = session.message_index();
293
+ const key = session.session_key();
294
+ session.free();
295
+ return { chain_index, key };
296
+ }
297
+ /** */
298
+ async getHybridGroupSessionKeyForStream(streamId) {
299
+ return this.cryptoStore.withGroupSessions(async () => {
300
+ const sessionRecords = await this.cryptoStore.getHybridGroupSessionsForStream(streamId);
301
+ if (sessionRecords.length === 0) {
302
+ throw new Error(`hybrid group session not found for stream ${streamId}`);
303
+ }
304
+ // sort on session.miniblockNum decending
305
+ const sessionRecord = sessionRecords.reduce((max, current) => (current.miniblockNum > max.miniblockNum ? current : max), sessionRecords[0]);
306
+ return fromBinary(HybridGroupSessionKeySchema, sessionRecord.sessionKey);
307
+ });
308
+ }
309
+ /** */
310
+ async getHybridGroupSessionKey(streamId, sessionId) {
311
+ return this.cryptoStore.withGroupSessions(async () => {
312
+ const sessionRecord = await this.cryptoStore.getHybridGroupSession(streamId, sessionId);
313
+ if (!sessionRecord) {
314
+ throw new Error(`hybrid group session not found for stream ${streamId}`);
315
+ }
316
+ return fromBinary(HybridGroupSessionKeySchema, sessionRecord.sessionKey);
317
+ });
318
+ }
319
+ /**
320
+ * Generate a new outbound group session
321
+ *
322
+ */
323
+ async createOutboundGroupSession(streamId) {
324
+ return await this.cryptoStore.withGroupSessions(async () => {
325
+ // Create an outbound group session
326
+ const session = this.delegate.createOutboundGroupSession();
327
+ const inboundSession = this.delegate.createInboundGroupSession();
328
+ try {
329
+ session.create();
330
+ const sessionId = session.session_id();
331
+ await this.saveOutboundGroupSession(session, streamId);
332
+ // While still inside the transaction, create an inbound counterpart session
333
+ // to make sure that the session is exported at message index 0.
334
+ const key = session.session_key();
335
+ inboundSession.create(key);
336
+ const pickled = inboundSession.pickle(this.pickleKey);
337
+ await this.cryptoStore.storeEndToEndInboundGroupSession(streamId, sessionId, {
338
+ session: pickled,
339
+ stream_id: streamId,
340
+ keysClaimed: {},
341
+ });
342
+ return sessionId;
343
+ }
344
+ catch (e) {
345
+ log('Error creating outbound group session', e);
346
+ throw e;
347
+ }
348
+ finally {
349
+ session.free();
350
+ inboundSession.free();
351
+ }
352
+ });
353
+ }
354
+ /** */
355
+ async createHybridGroupSession(streamId, miniblockNum, miniblockHash) {
356
+ const streamIdBytes = bin_fromHexString(streamId);
357
+ const aesKey = await generateNewAesGcmKey();
358
+ const aesKeyBytes = await exportAesGsmKeyBytes(aesKey);
359
+ const sessionIdBytes = await hybridSessionKeyHash(streamIdBytes, aesKeyBytes, miniblockNum, miniblockHash);
360
+ const sessionKey = create(HybridGroupSessionKeySchema, {
361
+ sessionId: sessionIdBytes,
362
+ streamId: streamIdBytes,
363
+ key: aesKeyBytes,
364
+ miniblockNum,
365
+ miniblockHash,
366
+ });
367
+ const sessionId = bin_toHexString(sessionIdBytes);
368
+ const sessionRecord = {
369
+ sessionId,
370
+ streamId: streamId,
371
+ sessionKey: toBinary(HybridGroupSessionKeySchema, sessionKey),
372
+ miniblockNum,
373
+ };
374
+ return this.cryptoStore.withGroupSessions(async () => {
375
+ await this.cryptoStore.storeHybridGroupSession(sessionRecord);
376
+ return { sessionId, sessionRecord, sessionKey };
377
+ });
378
+ }
379
+ // Inbound group session
380
+ // =====================
381
+ /**
382
+ * Unpickle a session from a sessionData object and invoke the given function.
383
+ * The session is valid only until func returns.
384
+ *
385
+ * @param sessionData - Object describing the session.
386
+ * @param func - Invoked with the unpickled session
387
+ * @returns result of func
388
+ */
389
+ unpickleInboundGroupSession(sessionData) {
390
+ const session = this.delegate.createInboundGroupSession();
391
+ session.unpickle(this.pickleKey, sessionData.session);
392
+ return session;
393
+ }
394
+ /**
395
+ * Extract an InboundGroupSession from the crypto store and call the given function
396
+ *
397
+ * @param streamId - The stream ID to extract the session for, or null to fetch
398
+ * sessions for any room.
399
+ * @param txn - Opaque transaction object from cryptoStore.doTxn()
400
+ * @param func - function to call.
401
+ *
402
+ * @internal
403
+ */
404
+ async getInboundGroupSession(streamId, sessionId) {
405
+ const sessionInfo = await this.cryptoStore.getEndToEndInboundGroupSession(streamId, sessionId);
406
+ const session = sessionInfo ? this.unpickleInboundGroupSession(sessionInfo) : undefined;
407
+ return {
408
+ session: session,
409
+ data: sessionInfo,
410
+ };
411
+ }
412
+ /**
413
+ * Add an inbound group session to the session store
414
+ *
415
+ * @param streamId - room in which this session will be used
416
+ * @param senderKey - base64-encoded curve25519 key of the sender
417
+ * @param sessionId - session identifier
418
+ * @param sessionKey - base64-encoded secret key
419
+ * @param keysClaimed - Other keys the sender claims.
420
+ * @param exportFormat - true if the group keys are in export format
421
+ * (ie, they lack an ed25519 signature)
422
+ * @param extraSessionData - any other data to be include with the session
423
+ */
424
+ async addInboundGroupSession(streamId, sessionId, sessionKey, keysClaimed, _exportFormat, extraSessionData = {}) {
425
+ const { session: existingSession, data: existingSessionData } = await this.getInboundGroupSession(streamId, sessionId);
426
+ const session = this.delegate.createInboundGroupSession();
427
+ try {
428
+ log(`Adding group session ${streamId}|${sessionId}`);
429
+ try {
430
+ session.import_session(sessionKey);
431
+ }
432
+ catch {
433
+ session.create(sessionKey);
434
+ }
435
+ if (sessionId != session.session_id()) {
436
+ throw new Error('Mismatched group session ID from streamId: ' + streamId);
437
+ }
438
+ if (existingSession && existingSessionData) {
439
+ log(`Update for group session ${streamId}|${sessionId}`);
440
+ if (existingSession.first_known_index() <= session.first_known_index()) {
441
+ if (!existingSessionData.untrusted || extraSessionData.untrusted) {
442
+ // existing session has less-than-or-equal index
443
+ // (i.e. can decrypt at least as much), and the
444
+ // new session's trust does not win over the old
445
+ // session's trust, so keep it
446
+ log(`Keeping existing group session ${streamId}|${sessionId}`);
447
+ return;
448
+ }
449
+ if (existingSession.first_known_index() < session.first_known_index()) {
450
+ // We want to upgrade the existing session's trust,
451
+ // but we can't just use the new session because we'll
452
+ // lose the lower index. Check that the sessions connect
453
+ // properly, and then manually set the existing session
454
+ // as trusted.
455
+ if (existingSession.export_session(session.first_known_index()) ===
456
+ session.export_session(session.first_known_index())) {
457
+ log('Upgrading trust of existing group session ' +
458
+ `${streamId}|${sessionId} based on newly-received trusted session`);
459
+ existingSessionData.untrusted = false;
460
+ await this.cryptoStore.storeEndToEndInboundGroupSession(streamId, sessionId, existingSessionData);
461
+ }
462
+ else {
463
+ log(`Newly-received group session ${streamId}|$sessionId}` +
464
+ ' does not match existing session! Keeping existing session');
465
+ }
466
+ return;
467
+ }
468
+ // If the sessions have the same index, go ahead and store the new trusted one.
469
+ }
470
+ }
471
+ log(`Storing group session ${streamId}|${sessionId} with first index ${session.first_known_index()}`);
472
+ const sessionData = Object.assign({}, extraSessionData, {
473
+ stream_id: streamId,
474
+ session: session.pickle(this.pickleKey),
475
+ keysClaimed: keysClaimed,
476
+ });
477
+ await this.cryptoStore.withGroupSessions(async () => {
478
+ await this.cryptoStore.storeEndToEndInboundGroupSession(streamId, sessionId, sessionData);
479
+ });
480
+ }
481
+ finally {
482
+ session.free();
483
+ }
484
+ }
485
+ /** */
486
+ async addHybridGroupSession(streamId, sessionId, sessionKey) {
487
+ const sessionKeyBytes = bin_fromHexString(sessionKey);
488
+ const session = fromBinary(HybridGroupSessionKeySchema, sessionKeyBytes);
489
+ if (bin_toHexString(session.streamId) !== streamId) {
490
+ throw new Error(`Stream ID mismatch for hybrid group session ${streamId}`);
491
+ }
492
+ if (bin_toHexString(session.sessionId) !== sessionId) {
493
+ throw new Error(`Session ID mismatch for hybrid group session ${sessionId}`);
494
+ }
495
+ const expectedSessionPromise = hybridSessionKeyHash(session.streamId, session.key, session.miniblockNum, session.miniblockHash);
496
+ const expectedSessionId = await Dexie.waitFor(expectedSessionPromise);
497
+ if (!bin_equal(expectedSessionId, bin_fromHexString(sessionId))) {
498
+ throw new Error(`Session ID mismatch for hybrid group session ${sessionId} expected ${bin_toHexString(expectedSessionId)}`);
499
+ }
500
+ await this.cryptoStore.withGroupSessions(async () => {
501
+ await this.cryptoStore.storeHybridGroupSession({
502
+ sessionId,
503
+ streamId,
504
+ sessionKey: sessionKeyBytes,
505
+ miniblockNum: session.miniblockNum,
506
+ });
507
+ });
508
+ }
509
+ /**
510
+ * Encrypt an outgoing message with an outbound group session
511
+ *
512
+ * @param sessionId - this id of the session
513
+ * @param payloadString - payload to be encrypted
514
+ *
515
+ * @returns ciphertext
516
+ */
517
+ async encryptGroupMessage(payloadString, streamId) {
518
+ return await this.cryptoStore.withGroupSessions(async () => {
519
+ log(`encrypting msg with group session for stream id ${streamId}`);
520
+ checkPayloadLength(payloadString, { streamId, source: 'encryptGroupMessage' });
521
+ const session = await this.getOutboundGroupSession(streamId);
522
+ const ciphertext = session.encrypt(payloadString);
523
+ const sessionId = session.session_id();
524
+ await this.saveOutboundGroupSession(session, streamId);
525
+ session.free();
526
+ return { ciphertext, sessionId };
527
+ });
528
+ }
529
+ async encryptUsingFallbackKey(theirIdentityKey, fallbackKey, payload) {
530
+ checkPayloadLength(payload, { source: 'encryptUsingFallbackKey' });
531
+ return this.cryptoStore.withAccountTx(async () => {
532
+ const session = this.delegate.createSession();
533
+ try {
534
+ const account = await this.getAccount();
535
+ session.create_outbound(account, theirIdentityKey, fallbackKey);
536
+ const result = session.encrypt(payload);
537
+ return result;
538
+ }
539
+ catch (error) {
540
+ log('Error encrypting message with fallback key', error);
541
+ throw error;
542
+ }
543
+ finally {
544
+ session.free();
545
+ }
546
+ });
547
+ }
548
+ /**
549
+ * Decrypt an incoming message using an existing session
550
+ *
551
+ * @param theirDeviceIdentityKey - Curve25519 identity key for the
552
+ * remote device
553
+ * @param messageType - messageType field from the received message
554
+ * @param ciphertext - base64-encoded body from the received message
555
+ *
556
+ * @returns decrypted payload.
557
+ */
558
+ async decryptMessage(ciphertext, theirDeviceIdentityKey, messageType = 0) {
559
+ if (messageType !== 0) {
560
+ throw new Error('Only pre-key messages supported');
561
+ }
562
+ checkPayloadLength(ciphertext, { source: 'decryptMessage' });
563
+ return await this.cryptoStore.withAccountTx(async () => {
564
+ const account = await this.getAccount();
565
+ const session = this.delegate.createSession();
566
+ const sessionDesc = session.describe();
567
+ log('Session ID ' +
568
+ session.session_id() +
569
+ ' from ' +
570
+ theirDeviceIdentityKey +
571
+ ': ' +
572
+ sessionDesc);
573
+ try {
574
+ session.create_inbound_from(account, theirDeviceIdentityKey, ciphertext);
575
+ await this.storeAccount(account);
576
+ return session.decrypt(messageType, ciphertext);
577
+ }
578
+ catch (e) {
579
+ throw new Error('Error decrypting prekey message: ' + JSON.stringify(e.message));
580
+ }
581
+ finally {
582
+ session.free();
583
+ }
584
+ });
585
+ }
586
+ // Utilities
587
+ // =========
588
+ /**
589
+ * Verify an ed25519 signature.
590
+ *
591
+ * @param key - ed25519 key
592
+ * @param message - message which was signed
593
+ * @param signature - base64-encoded signature to be checked
594
+ *
595
+ * @throws Error if there is a problem with the verification. If the key was
596
+ * too small then the message will be "OLM.INVALID_BASE64". If the signature
597
+ * was invalid then the message will be "OLM.BAD_MESSAGE_MAC".
598
+ */
599
+ verifySignature(key, message, signature) {
600
+ this.getUtility(function (util) {
601
+ util.ed25519_verify(key, message, signature);
602
+ });
603
+ }
604
+ // Group Sessions
605
+ async getInboundGroupSessionIds(streamId) {
606
+ return await this.cryptoStore.getInboundGroupSessionIds(streamId);
607
+ }
608
+ async getHybridGroupSessionIds(streamId) {
609
+ return await this.cryptoStore.getHybridGroupSessionIds(streamId);
610
+ }
611
+ /**
612
+ * Determine if we have the keys for a given group session
613
+ *
614
+ * @param streamId - stream in which the message was received
615
+ * @param senderKey - base64-encoded curve25519 key of the sender
616
+ * @param sessionId - session identifier
617
+ */
618
+ async hasInboundSessionKeys(streamId, sessionId) {
619
+ const sessionData = await this.cryptoStore.withGroupSessions(async () => {
620
+ return this.cryptoStore.getEndToEndInboundGroupSession(streamId, sessionId);
621
+ });
622
+ if (!sessionData) {
623
+ return false;
624
+ }
625
+ if (streamId !== sessionData.stream_id) {
626
+ log(`[hasInboundSessionKey]: requested keys for inbound group session` +
627
+ `${sessionId}, with incorrect stream id ` +
628
+ `(expected ${sessionData.stream_id}, ` +
629
+ `was ${streamId})`);
630
+ return false;
631
+ }
632
+ else {
633
+ return true;
634
+ }
635
+ }
636
+ /** */
637
+ async hasHybridGroupSessionKey(streamId, sessionId) {
638
+ const key = await this.cryptoStore.getHybridGroupSession(streamId, sessionId);
639
+ return key !== undefined;
640
+ }
641
+ /**
642
+ * Export an inbound group session
643
+ *
644
+ * @param streamId - streamId of session
645
+ * @param sessionId - session identifier
646
+ */
647
+ async exportInboundGroupSession(streamId, sessionId) {
648
+ const sessionData = await this.cryptoStore.getEndToEndInboundGroupSession(streamId, sessionId);
649
+ if (!sessionData) {
650
+ return undefined;
651
+ }
652
+ const session = this.unpickleInboundGroupSession(sessionData);
653
+ const messageIndex = session.first_known_index();
654
+ const sessionKey = session.export_session(messageIndex);
655
+ session.free();
656
+ return {
657
+ streamId: streamId,
658
+ sessionId: sessionId,
659
+ sessionKey: sessionKey,
660
+ algorithm: GroupEncryptionAlgorithmId.GroupEncryption,
661
+ };
662
+ }
663
+ /** */
664
+ async exportHybridGroupSession(streamId, sessionId) {
665
+ const sessionData = await this.cryptoStore.getHybridGroupSession(streamId, sessionId);
666
+ if (!sessionData) {
667
+ return undefined;
668
+ }
669
+ return {
670
+ streamId: streamId,
671
+ sessionId: sessionId,
672
+ sessionKey: bin_toHexString(sessionData.sessionKey),
673
+ algorithm: GroupEncryptionAlgorithmId.HybridGroupEncryption,
674
+ };
675
+ }
676
+ /**
677
+ * Get a list containing all of the room keys
678
+ *
679
+ * @returns a list of session export objects
680
+ */
681
+ async exportInboundGroupSessions() {
682
+ const exportedSessions = [];
683
+ await this.cryptoStore.withGroupSessions(async () => {
684
+ const sessions = await this.cryptoStore.getAllEndToEndInboundGroupSessions();
685
+ for (const sessionData of sessions) {
686
+ if (!sessionData) {
687
+ continue;
688
+ }
689
+ const session = this.unpickleInboundGroupSession(sessionData);
690
+ const messageIndex = session.first_known_index();
691
+ const sessionKey = session.export_session(messageIndex);
692
+ session.free();
693
+ exportedSessions.push({
694
+ streamId: sessionData.streamId,
695
+ sessionId: sessionData.sessionId,
696
+ sessionKey: sessionKey,
697
+ algorithm: GroupEncryptionAlgorithmId.GroupEncryption,
698
+ });
699
+ }
700
+ });
701
+ return exportedSessions;
702
+ }
703
+ async exportHybridGroupSessions() {
704
+ const sessions = await this.cryptoStore.getAllHybridGroupSessions();
705
+ return sessions.map((session) => {
706
+ return {
707
+ streamId: session.streamId,
708
+ sessionId: session.sessionId,
709
+ sessionKey: bin_toHexString(session.sessionKey),
710
+ algorithm: GroupEncryptionAlgorithmId.HybridGroupEncryption,
711
+ };
712
+ });
713
+ }
714
+ }
715
+ const hybridSessionKeyHashPrefixBytes = new TextEncoder().encode('RVR_HSK:');
716
+ // TODO: needs unit tests
717
+ export async function hybridSessionKeyHash(streamId, key, miniblockNum, miniblockHash) {
718
+ const length = hybridSessionKeyHashPrefixBytes.length +
719
+ streamId.length +
720
+ key.length +
721
+ 8 +
722
+ miniblockHash.length;
723
+ const bytes = new ArrayBuffer(length);
724
+ const dataView = new DataView(bytes);
725
+ const arrayView = new Uint8Array(bytes);
726
+ arrayView.set(hybridSessionKeyHashPrefixBytes);
727
+ let offset = hybridSessionKeyHashPrefixBytes.length;
728
+ arrayView.set(streamId, offset);
729
+ offset += streamId.length;
730
+ arrayView.set(key, offset);
731
+ offset += key.length;
732
+ dataView.setBigUint64(offset, miniblockNum);
733
+ offset += 8;
734
+ arrayView.set(miniblockHash, offset);
735
+ offset += miniblockHash.length;
736
+ if (offset !== length) {
737
+ throw new Error(`Final offset ${offset} does not match expected length ${length}`);
738
+ }
739
+ const hashBytes = await crypto.subtle.digest('SHA-256', bytes);
740
+ return new Uint8Array(hashBytes);
741
+ }
742
+ //# sourceMappingURL=encryptionDevice.js.map