|
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+/* global __filename, Olm */
|
|
|
2
|
+
|
|
|
3
|
+import base64js from 'base64-js';
|
|
|
4
|
+import { getLogger } from 'jitsi-meet-logger';
|
|
|
5
|
+import isEqual from 'lodash.isequal';
|
|
|
6
|
+import { v4 as uuidv4 } from 'uuid';
|
|
|
7
|
+
|
|
|
8
|
+import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
|
|
|
9
|
+import Deferred from '../util/Deferred';
|
|
|
10
|
+import Listenable from '../util/Listenable';
|
|
|
11
|
+import { JITSI_MEET_MUC_TYPE } from '../xmpp/xmpp';
|
|
|
12
|
+
|
|
|
13
|
+const logger = getLogger(__filename);
|
|
|
14
|
+
|
|
|
15
|
+const REQ_TIMEOUT = 5 * 1000;
|
|
|
16
|
+const OLM_MESSAGE_TYPE = 'olm';
|
|
|
17
|
+const OLM_MESSAGE_TYPES = {
|
|
|
18
|
+ ERROR: 'error',
|
|
|
19
|
+ KEY_INFO: 'key-info',
|
|
|
20
|
+ KEY_INFO_ACK: 'key-info-ack',
|
|
|
21
|
+ SESSION_ACK: 'session-ack',
|
|
|
22
|
+ SESSION_INIT: 'session-init'
|
|
|
23
|
+};
|
|
|
24
|
+
|
|
|
25
|
+const kOlmData = Symbol('OlmData');
|
|
|
26
|
+
|
|
|
27
|
+const OlmAdapterEvents = {
|
|
|
28
|
+ PARTICIPANT_E2EE_CHANNEL_READY: 'olm.participant_e2ee_channel_ready',
|
|
|
29
|
+ PARTICIPANT_KEY_UPDATED: 'olm.partitipant_key_updated'
|
|
|
30
|
+};
|
|
|
31
|
+
|
|
|
32
|
+/**
|
|
|
33
|
+ * This class implements an End-to-End Encrypted communication channel between every two peers
|
|
|
34
|
+ * in the conference. This channel uses libolm to achieve E2EE.
|
|
|
35
|
+ *
|
|
|
36
|
+ * The created channel is then used to exchange the secret key that each participant will use
|
|
|
37
|
+ * to encrypt the actual media (see {@link E2EEContext}).
|
|
|
38
|
+ *
|
|
|
39
|
+ * A simple JSON message based protocol is implemented, which follows a request - response model:
|
|
|
40
|
+ * - session-init: Initiates an olm session establishment procedure. This message will be sent
|
|
|
41
|
+ * by the participant who just joined, to everyone else.
|
|
|
42
|
+ * - session-ack: Completes the olm session etablishment. This messsage may contain ancilliary
|
|
|
43
|
+ * encrypted data, more specifically the sender's current key.
|
|
|
44
|
+ * - key-info: Includes the sender's most up to date key information.
|
|
|
45
|
+ * - key-info-ack: Acknowledges the reception of a key-info request. In addition, it may contain
|
|
|
46
|
+ * the sender's key information, if available.
|
|
|
47
|
+ * - error: Indicates a request processing error has occurred.
|
|
|
48
|
+ *
|
|
|
49
|
+ * These requessts and responses are transport independent. Currently they are sent using XMPP
|
|
|
50
|
+ * MUC private messages.
|
|
|
51
|
+ */
|
|
|
52
|
+export class OlmAdapter extends Listenable {
|
|
|
53
|
+ /**
|
|
|
54
|
+ * Creates an adapter instance for the given conference.
|
|
|
55
|
+ */
|
|
|
56
|
+ constructor(conference) {
|
|
|
57
|
+ super();
|
|
|
58
|
+
|
|
|
59
|
+ this._conf = conference;
|
|
|
60
|
+ this._init = new Deferred();
|
|
|
61
|
+ this._key = undefined;
|
|
|
62
|
+ this._keyIndex = -1;
|
|
|
63
|
+ this._reqs = new Map();
|
|
|
64
|
+
|
|
|
65
|
+ if (OlmAdapter.isSupported()) {
|
|
|
66
|
+ this._bootstrapOlm();
|
|
|
67
|
+
|
|
|
68
|
+ this._conf.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, this._onEndpointMessageReceived.bind(this));
|
|
|
69
|
+ this._conf.on(JitsiConferenceEvents.CONFERENCE_JOINED, this._onConferenceJoined.bind(this));
|
|
|
70
|
+ this._conf.on(JitsiConferenceEvents.CONFERENCE_LEFT, this._onConferenceLeft.bind(this));
|
|
|
71
|
+ this._conf.on(JitsiConferenceEvents.USER_LEFT, this._onParticipantLeft.bind(this));
|
|
|
72
|
+ } else {
|
|
|
73
|
+ this._init.reject(new Error('Olm not supported'));
|
|
|
74
|
+ }
|
|
|
75
|
+ }
|
|
|
76
|
+
|
|
|
77
|
+ /**
|
|
|
78
|
+ * Indicates if olm is supported on the current platform.
|
|
|
79
|
+ *
|
|
|
80
|
+ * @returns {boolean}
|
|
|
81
|
+ */
|
|
|
82
|
+ static isSupported() {
|
|
|
83
|
+ return typeof window.Olm !== 'undefined';
|
|
|
84
|
+ }
|
|
|
85
|
+
|
|
|
86
|
+ /**
|
|
|
87
|
+ * Updates the current participant key and distributes it to all participants in the conference
|
|
|
88
|
+ * by sending a key-info message.
|
|
|
89
|
+ *
|
|
|
90
|
+ * @param {Uint8Array|boolean} key - The new key.
|
|
|
91
|
+ * @retrns {Promise<Number>}
|
|
|
92
|
+ */
|
|
|
93
|
+ async updateKey(key) {
|
|
|
94
|
+ // Store it locally for new sessions.
|
|
|
95
|
+ this._key = key;
|
|
|
96
|
+ this._keyIndex++;
|
|
|
97
|
+
|
|
|
98
|
+ const promises = [];
|
|
|
99
|
+
|
|
|
100
|
+ // Broadcast it.
|
|
|
101
|
+ for (const participant of this._conf.getParticipants()) {
|
|
|
102
|
+ const pId = participant.getId();
|
|
|
103
|
+ const olmData = this._getParticipantOlmData(participant);
|
|
|
104
|
+
|
|
|
105
|
+ // TODO: skip those who don't support E2EE.
|
|
|
106
|
+
|
|
|
107
|
+ if (!olmData.session) {
|
|
|
108
|
+ logger.warn(`Tried to send key to participant ${pId} but we have no session`);
|
|
|
109
|
+
|
|
|
110
|
+ // eslint-disable-next-line no-continue
|
|
|
111
|
+ continue;
|
|
|
112
|
+ }
|
|
|
113
|
+
|
|
|
114
|
+ const uuid = uuidv4();
|
|
|
115
|
+ const data = {
|
|
|
116
|
+ [JITSI_MEET_MUC_TYPE]: OLM_MESSAGE_TYPE,
|
|
|
117
|
+ olm: {
|
|
|
118
|
+ type: OLM_MESSAGE_TYPES.KEY_INFO,
|
|
|
119
|
+ data: {
|
|
|
120
|
+ ciphertext: this._encryptKeyInfo(olmData.session),
|
|
|
121
|
+ uuid
|
|
|
122
|
+ }
|
|
|
123
|
+ }
|
|
|
124
|
+ };
|
|
|
125
|
+ const d = new Deferred();
|
|
|
126
|
+
|
|
|
127
|
+ d.setRejectTimeout(REQ_TIMEOUT);
|
|
|
128
|
+ d.catch(() => {
|
|
|
129
|
+ this._reqs.delete(uuid);
|
|
|
130
|
+ });
|
|
|
131
|
+ this._reqs.set(uuid, d);
|
|
|
132
|
+ promises.push(d);
|
|
|
133
|
+
|
|
|
134
|
+ this._sendMessage(data, pId);
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ await Promise.allSettled(promises);
|
|
|
138
|
+
|
|
|
139
|
+ // TODO: retry failed ones?
|
|
|
140
|
+
|
|
|
141
|
+ return this._keyIndex;
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ /**
|
|
|
145
|
+ * Internal helper to bootstrap the olm library.
|
|
|
146
|
+ *
|
|
|
147
|
+ * @returns {Promise<void>}
|
|
|
148
|
+ * @private
|
|
|
149
|
+ */
|
|
|
150
|
+ async _bootstrapOlm() {
|
|
|
151
|
+ logger.debug('Initializing Olm...');
|
|
|
152
|
+
|
|
|
153
|
+ try {
|
|
|
154
|
+ await Olm.init();
|
|
|
155
|
+
|
|
|
156
|
+ this._olmAccount = new Olm.Account();
|
|
|
157
|
+ this._olmAccount.create();
|
|
|
158
|
+
|
|
|
159
|
+ const idKeys = JSON.parse(this._olmAccount.identity_keys());
|
|
|
160
|
+
|
|
|
161
|
+ this._idKey = idKeys.curve25519;
|
|
|
162
|
+
|
|
|
163
|
+ logger.debug('Olm initialized!');
|
|
|
164
|
+ this._init.resolve();
|
|
|
165
|
+ } catch (e) {
|
|
|
166
|
+ logger.error('Failed to initialize Olm', e);
|
|
|
167
|
+ this._init.reject(e);
|
|
|
168
|
+ }
|
|
|
169
|
+
|
|
|
170
|
+ }
|
|
|
171
|
+
|
|
|
172
|
+ /**
|
|
|
173
|
+ * Internal helper for encrypting the current key information for a given participant.
|
|
|
174
|
+ *
|
|
|
175
|
+ * @param {Olm.Session} session - Participant's session.
|
|
|
176
|
+ * @returns {string} - The encrypted text with the key information.
|
|
|
177
|
+ * @private
|
|
|
178
|
+ */
|
|
|
179
|
+ _encryptKeyInfo(session) {
|
|
|
180
|
+ const keyInfo = {};
|
|
|
181
|
+
|
|
|
182
|
+ if (this._key !== undefined) {
|
|
|
183
|
+ keyInfo.key = this._key ? base64js.fromByteArray(this._key) : false;
|
|
|
184
|
+ keyInfo.keyIndex = this._keyIndex;
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+ return session.encrypt(JSON.stringify(keyInfo));
|
|
|
188
|
+ }
|
|
|
189
|
+
|
|
|
190
|
+ /**
|
|
|
191
|
+ * Internal helper for getting the olm related data associated with a participant.
|
|
|
192
|
+ *
|
|
|
193
|
+ * @param {JitsiParticipant} participant - Participant whose data wants to be extracted.
|
|
|
194
|
+ * @returns {Object}
|
|
|
195
|
+ * @private
|
|
|
196
|
+ */
|
|
|
197
|
+ _getParticipantOlmData(participant) {
|
|
|
198
|
+ participant[kOlmData] = participant[kOlmData] || {};
|
|
|
199
|
+
|
|
|
200
|
+ return participant[kOlmData];
|
|
|
201
|
+ }
|
|
|
202
|
+
|
|
|
203
|
+ /**
|
|
|
204
|
+ * Handles the conference joined event. Upon joining a conference, the participant
|
|
|
205
|
+ * who just joined will start new olm sessions with every other participant.
|
|
|
206
|
+ *
|
|
|
207
|
+ * @private
|
|
|
208
|
+ */
|
|
|
209
|
+ async _onConferenceJoined() {
|
|
|
210
|
+ logger.debug('Conference joined');
|
|
|
211
|
+
|
|
|
212
|
+ await this._init;
|
|
|
213
|
+
|
|
|
214
|
+ const promises = [];
|
|
|
215
|
+
|
|
|
216
|
+ // Establish a 1-to-1 Olm session with every participant in the conference.
|
|
|
217
|
+ // We are forcing the last user to join the conference to start the exchange
|
|
|
218
|
+ // so we can send some pre-established secrets in the ACK.
|
|
|
219
|
+ for (const participant of this._conf.getParticipants()) {
|
|
|
220
|
+ promises.push(this._sendSessionInit(participant));
|
|
|
221
|
+ }
|
|
|
222
|
+
|
|
|
223
|
+ await Promise.allSettled(promises);
|
|
|
224
|
+
|
|
|
225
|
+ // TODO: retry failed ones.
|
|
|
226
|
+ // TODO: skip participants which don't support E2EE.
|
|
|
227
|
+ }
|
|
|
228
|
+
|
|
|
229
|
+ /**
|
|
|
230
|
+ * Handles leaving the conference, cleaning up olm sessions.
|
|
|
231
|
+ *
|
|
|
232
|
+ * @private
|
|
|
233
|
+ */
|
|
|
234
|
+ async _onConferenceLeft() {
|
|
|
235
|
+ logger.debug('Conference left');
|
|
|
236
|
+
|
|
|
237
|
+ await this._init;
|
|
|
238
|
+
|
|
|
239
|
+ for (const participant of this._conf.getParticipants()) {
|
|
|
240
|
+ this._onParticipantLeft(participant.getId(), participant);
|
|
|
241
|
+ }
|
|
|
242
|
+
|
|
|
243
|
+ if (this._olmAccount) {
|
|
|
244
|
+ this._olmAccount.free();
|
|
|
245
|
+ this._olmAccount = undefined;
|
|
|
246
|
+ }
|
|
|
247
|
+ }
|
|
|
248
|
+
|
|
|
249
|
+ /**
|
|
|
250
|
+ * Main message handler. Handles 1-to-1 messages received from other participants
|
|
|
251
|
+ * and send the appropriate replies.
|
|
|
252
|
+ *
|
|
|
253
|
+ * @private
|
|
|
254
|
+ */
|
|
|
255
|
+ async _onEndpointMessageReceived(participant, payload) {
|
|
|
256
|
+ if (payload[JITSI_MEET_MUC_TYPE] !== OLM_MESSAGE_TYPE) {
|
|
|
257
|
+ return;
|
|
|
258
|
+ }
|
|
|
259
|
+
|
|
|
260
|
+ if (!payload.olm) {
|
|
|
261
|
+ logger.warn('Incorrectly formatted message');
|
|
|
262
|
+
|
|
|
263
|
+ return;
|
|
|
264
|
+ }
|
|
|
265
|
+
|
|
|
266
|
+ await this._init;
|
|
|
267
|
+
|
|
|
268
|
+ const msg = payload.olm;
|
|
|
269
|
+ const pId = participant.getId();
|
|
|
270
|
+ const olmData = this._getParticipantOlmData(participant);
|
|
|
271
|
+
|
|
|
272
|
+ switch (msg.type) {
|
|
|
273
|
+ case OLM_MESSAGE_TYPES.SESSION_INIT: {
|
|
|
274
|
+ if (olmData.session) {
|
|
|
275
|
+ logger.warn(`Participant ${pId} already has a session`);
|
|
|
276
|
+
|
|
|
277
|
+ this._sendError(participant, 'Session already established');
|
|
|
278
|
+ } else {
|
|
|
279
|
+ // Create a session for communicating with this participant.
|
|
|
280
|
+
|
|
|
281
|
+ const session = new Olm.Session();
|
|
|
282
|
+
|
|
|
283
|
+ session.create_outbound(this._olmAccount, msg.data.idKey, msg.data.otKey);
|
|
|
284
|
+ olmData.session = session;
|
|
|
285
|
+
|
|
|
286
|
+ // Send ACK
|
|
|
287
|
+ const ack = {
|
|
|
288
|
+ [JITSI_MEET_MUC_TYPE]: OLM_MESSAGE_TYPE,
|
|
|
289
|
+ olm: {
|
|
|
290
|
+ type: OLM_MESSAGE_TYPES.SESSION_ACK,
|
|
|
291
|
+ data: {
|
|
|
292
|
+ ciphertext: this._encryptKeyInfo(session),
|
|
|
293
|
+ uuid: msg.data.uuid
|
|
|
294
|
+ }
|
|
|
295
|
+ }
|
|
|
296
|
+ };
|
|
|
297
|
+
|
|
|
298
|
+ this._sendMessage(ack, pId);
|
|
|
299
|
+ }
|
|
|
300
|
+ break;
|
|
|
301
|
+ }
|
|
|
302
|
+ case OLM_MESSAGE_TYPES.SESSION_ACK: {
|
|
|
303
|
+ if (olmData.session) {
|
|
|
304
|
+ logger.warn(`Participant ${pId} already has a session`);
|
|
|
305
|
+
|
|
|
306
|
+ this._sendError(participant, 'No session found');
|
|
|
307
|
+ } else if (msg.data.uuid === olmData.pendingSessionUuid) {
|
|
|
308
|
+ const { ciphertext } = msg.data;
|
|
|
309
|
+ const d = this._reqs.get(msg.data.uuid);
|
|
|
310
|
+ const session = new Olm.Session();
|
|
|
311
|
+
|
|
|
312
|
+ session.create_inbound(this._olmAccount, ciphertext.body);
|
|
|
313
|
+
|
|
|
314
|
+ // Remove OT keys that have been used to setup this session.
|
|
|
315
|
+ this._olmAccount.remove_one_time_keys(session);
|
|
|
316
|
+
|
|
|
317
|
+ // Decrypt first message.
|
|
|
318
|
+ const data = session.decrypt(ciphertext.type, ciphertext.body);
|
|
|
319
|
+
|
|
|
320
|
+ olmData.session = session;
|
|
|
321
|
+ olmData.pendingSessionUuid = undefined;
|
|
|
322
|
+
|
|
|
323
|
+ logger.debug(`Olm session established with ${pId}`);
|
|
|
324
|
+ this.eventEmitter.emit(OlmAdapterEvents.PARTICIPANT_E2EE_CHANNEL_READY, pId);
|
|
|
325
|
+
|
|
|
326
|
+ this._reqs.delete(msg.data.uuid);
|
|
|
327
|
+ d.resolve();
|
|
|
328
|
+
|
|
|
329
|
+ const json = safeJsonParse(data);
|
|
|
330
|
+
|
|
|
331
|
+ if (json.key) {
|
|
|
332
|
+ const key = base64js.toByteArray(json.key);
|
|
|
333
|
+ const keyIndex = json.keyIndex;
|
|
|
334
|
+
|
|
|
335
|
+ olmData.lastKey = key;
|
|
|
336
|
+ this.eventEmitter.emit(OlmAdapterEvents.PARTICIPANT_KEY_UPDATED, pId, key, keyIndex);
|
|
|
337
|
+ }
|
|
|
338
|
+ } else {
|
|
|
339
|
+ logger.warn('Received ACK with the wrong UUID');
|
|
|
340
|
+
|
|
|
341
|
+ this._sendError(participant, 'Invalid UUID');
|
|
|
342
|
+ }
|
|
|
343
|
+ break;
|
|
|
344
|
+ }
|
|
|
345
|
+ case OLM_MESSAGE_TYPES.ERROR: {
|
|
|
346
|
+ logger.error(msg.data.error);
|
|
|
347
|
+
|
|
|
348
|
+ break;
|
|
|
349
|
+ }
|
|
|
350
|
+ case OLM_MESSAGE_TYPES.KEY_INFO: {
|
|
|
351
|
+ if (olmData.session) {
|
|
|
352
|
+ const { ciphertext } = msg.data;
|
|
|
353
|
+ const data = olmData.session.decrypt(ciphertext.type, ciphertext.body);
|
|
|
354
|
+ const json = safeJsonParse(data);
|
|
|
355
|
+
|
|
|
356
|
+ if (json.key !== undefined && json.keyIndex !== undefined) {
|
|
|
357
|
+ const key = json.key ? base64js.toByteArray(json.key) : false;
|
|
|
358
|
+ const keyIndex = json.keyIndex;
|
|
|
359
|
+
|
|
|
360
|
+ if (!isEqual(olmData.lastKey, key)) {
|
|
|
361
|
+ olmData.lastKey = key;
|
|
|
362
|
+ this.eventEmitter.emit(OlmAdapterEvents.PARTICIPANT_KEY_UPDATED, pId, key, keyIndex);
|
|
|
363
|
+ }
|
|
|
364
|
+
|
|
|
365
|
+ // Send ACK.
|
|
|
366
|
+ const ack = {
|
|
|
367
|
+ [JITSI_MEET_MUC_TYPE]: OLM_MESSAGE_TYPE,
|
|
|
368
|
+ olm: {
|
|
|
369
|
+ type: OLM_MESSAGE_TYPES.KEY_INFO_ACK,
|
|
|
370
|
+ data: {
|
|
|
371
|
+ ciphertext: this._encryptKeyInfo(olmData.session),
|
|
|
372
|
+ uuid: msg.data.uuid
|
|
|
373
|
+ }
|
|
|
374
|
+ }
|
|
|
375
|
+ };
|
|
|
376
|
+
|
|
|
377
|
+ this._sendMessage(ack, pId);
|
|
|
378
|
+ }
|
|
|
379
|
+ } else {
|
|
|
380
|
+ logger.debug(`Received key info message from ${pId} but we have no session for them!`);
|
|
|
381
|
+
|
|
|
382
|
+ this._sendError(participant, 'No session found while processing key-info');
|
|
|
383
|
+ }
|
|
|
384
|
+ break;
|
|
|
385
|
+ }
|
|
|
386
|
+ case OLM_MESSAGE_TYPES.KEY_INFO_ACK: {
|
|
|
387
|
+ if (olmData.session) {
|
|
|
388
|
+ const { ciphertext } = msg.data;
|
|
|
389
|
+ const data = olmData.session.decrypt(ciphertext.type, ciphertext.body);
|
|
|
390
|
+ const json = safeJsonParse(data);
|
|
|
391
|
+
|
|
|
392
|
+ if (json.key !== undefined && json.keyIndex !== undefined) {
|
|
|
393
|
+ const key = json.key ? base64js.toByteArray(json.key) : false;
|
|
|
394
|
+ const keyIndex = json.keyIndex;
|
|
|
395
|
+
|
|
|
396
|
+ if (!isEqual(olmData.lastKey, key)) {
|
|
|
397
|
+ olmData.lastKey = key;
|
|
|
398
|
+ this.eventEmitter.emit(OlmAdapterEvents.PARTICIPANT_KEY_UPDATED, pId, key, keyIndex);
|
|
|
399
|
+ }
|
|
|
400
|
+ }
|
|
|
401
|
+
|
|
|
402
|
+ const d = this._reqs.get(msg.data.uuid);
|
|
|
403
|
+
|
|
|
404
|
+ this._reqs.delete(msg.data.uuid);
|
|
|
405
|
+ d.resolve();
|
|
|
406
|
+ } else {
|
|
|
407
|
+ logger.debug(`Received key info ack message from ${pId} but we have no session for them!`);
|
|
|
408
|
+
|
|
|
409
|
+ this._sendError(participant, 'No session found while processing key-info-ack');
|
|
|
410
|
+ }
|
|
|
411
|
+ break;
|
|
|
412
|
+ }
|
|
|
413
|
+ }
|
|
|
414
|
+
|
|
|
415
|
+ }
|
|
|
416
|
+
|
|
|
417
|
+ /**
|
|
|
418
|
+ * Handles a participant leaving. When a participant leaves their olm session is destroyed.
|
|
|
419
|
+ *
|
|
|
420
|
+ * @private
|
|
|
421
|
+ */
|
|
|
422
|
+ _onParticipantLeft(id, participant) {
|
|
|
423
|
+ logger.debug(`Participant ${id} left`);
|
|
|
424
|
+
|
|
|
425
|
+ const olmData = this._getParticipantOlmData(participant);
|
|
|
426
|
+
|
|
|
427
|
+ if (olmData.session) {
|
|
|
428
|
+ olmData.session.free();
|
|
|
429
|
+ olmData.session = undefined;
|
|
|
430
|
+ }
|
|
|
431
|
+ }
|
|
|
432
|
+
|
|
|
433
|
+ /**
|
|
|
434
|
+ * Builds and sends an error message to the target participant.
|
|
|
435
|
+ *
|
|
|
436
|
+ * @param {JitsiParticipant} participant - The target participant.
|
|
|
437
|
+ * @param {string} error - The error message.
|
|
|
438
|
+ * @returns {void}
|
|
|
439
|
+ */
|
|
|
440
|
+ _sendError(participant, error) {
|
|
|
441
|
+ const pId = participant.getId();
|
|
|
442
|
+ const err = {
|
|
|
443
|
+ [JITSI_MEET_MUC_TYPE]: OLM_MESSAGE_TYPE,
|
|
|
444
|
+ olm: {
|
|
|
445
|
+ type: OLM_MESSAGE_TYPES.ERROR,
|
|
|
446
|
+ data: {
|
|
|
447
|
+ error
|
|
|
448
|
+ }
|
|
|
449
|
+ }
|
|
|
450
|
+ };
|
|
|
451
|
+
|
|
|
452
|
+ this._sendMessage(err, pId);
|
|
|
453
|
+ }
|
|
|
454
|
+
|
|
|
455
|
+ /**
|
|
|
456
|
+ * Internal helper to send the given object to the given participant ID.
|
|
|
457
|
+ * This function merely exists so the transport can be easily swapped.
|
|
|
458
|
+ * Currently messages are transmitted via XMPP MUC private messages.
|
|
|
459
|
+ *
|
|
|
460
|
+ * @param {object} data - The data that will be sent to the target participant.
|
|
|
461
|
+ * @param {string} participantId - ID of the target participant.
|
|
|
462
|
+ */
|
|
|
463
|
+ _sendMessage(data, participantId) {
|
|
|
464
|
+ this._conf.sendMessage(data, participantId);
|
|
|
465
|
+ }
|
|
|
466
|
+
|
|
|
467
|
+ /**
|
|
|
468
|
+ * Builds and sends the session-init request to the target participant.
|
|
|
469
|
+ *
|
|
|
470
|
+ * @param {JitsiParticipant} participant - Participant to whom we'll send the request.
|
|
|
471
|
+ * @returns {Promise} - The promise will be resolved when the session-ack is received.
|
|
|
472
|
+ * @private
|
|
|
473
|
+ */
|
|
|
474
|
+ _sendSessionInit(participant) {
|
|
|
475
|
+ const pId = participant.getId();
|
|
|
476
|
+ const olmData = this._getParticipantOlmData(participant);
|
|
|
477
|
+
|
|
|
478
|
+ if (olmData.session) {
|
|
|
479
|
+ logger.warn(`Tried to send session-init to ${pId} but we already have a session`);
|
|
|
480
|
+
|
|
|
481
|
+ return Promise.reject();
|
|
|
482
|
+ }
|
|
|
483
|
+
|
|
|
484
|
+ if (olmData.pendingSessionUuid !== undefined) {
|
|
|
485
|
+ logger.warn(`Tried to send session-init to ${pId} but we already have a pending session`);
|
|
|
486
|
+
|
|
|
487
|
+ return Promise.reject();
|
|
|
488
|
+ }
|
|
|
489
|
+
|
|
|
490
|
+ // Generate a One Time Key.
|
|
|
491
|
+ this._olmAccount.generate_one_time_keys(1);
|
|
|
492
|
+
|
|
|
493
|
+ const otKeys = JSON.parse(this._olmAccount.one_time_keys());
|
|
|
494
|
+ const otKey = Object.values(otKeys.curve25519)[0];
|
|
|
495
|
+
|
|
|
496
|
+ if (!otKey) {
|
|
|
497
|
+ return Promise.reject(new Error('No one-time-keys generated'));
|
|
|
498
|
+ }
|
|
|
499
|
+
|
|
|
500
|
+ // Mark the OT keys (one really) as published so they are not reused.
|
|
|
501
|
+ this._olmAccount.mark_keys_as_published();
|
|
|
502
|
+
|
|
|
503
|
+ const uuid = uuidv4();
|
|
|
504
|
+ const init = {
|
|
|
505
|
+ [JITSI_MEET_MUC_TYPE]: OLM_MESSAGE_TYPE,
|
|
|
506
|
+ olm: {
|
|
|
507
|
+ type: OLM_MESSAGE_TYPES.SESSION_INIT,
|
|
|
508
|
+ data: {
|
|
|
509
|
+ idKey: this._idKey,
|
|
|
510
|
+ otKey,
|
|
|
511
|
+ uuid
|
|
|
512
|
+ }
|
|
|
513
|
+ }
|
|
|
514
|
+ };
|
|
|
515
|
+
|
|
|
516
|
+ const d = new Deferred();
|
|
|
517
|
+
|
|
|
518
|
+ d.setRejectTimeout(REQ_TIMEOUT);
|
|
|
519
|
+ d.catch(() => {
|
|
|
520
|
+ this._reqs.delete(uuid);
|
|
|
521
|
+ olmData.pendingSessionUuid = undefined;
|
|
|
522
|
+ });
|
|
|
523
|
+ this._reqs.set(uuid, d);
|
|
|
524
|
+
|
|
|
525
|
+ this._sendMessage(init, pId);
|
|
|
526
|
+
|
|
|
527
|
+ // Store the UUID for matching with the ACK.
|
|
|
528
|
+ olmData.pendingSessionUuid = uuid;
|
|
|
529
|
+
|
|
|
530
|
+ return d;
|
|
|
531
|
+ }
|
|
|
532
|
+}
|
|
|
533
|
+
|
|
|
534
|
+OlmAdapter.events = OlmAdapterEvents;
|
|
|
535
|
+
|
|
|
536
|
+/**
|
|
|
537
|
+ * Helper to ensure JSON parsing always returns an object.
|
|
|
538
|
+ *
|
|
|
539
|
+ * @param {string} data - The data that needs to be parsed.
|
|
|
540
|
+ * @returns {object} - Parsed data or empty object in case of failure.
|
|
|
541
|
+ */
|
|
|
542
|
+function safeJsonParse(data) {
|
|
|
543
|
+ try {
|
|
|
544
|
+ return JSON.parse(data);
|
|
|
545
|
+ } catch (e) {
|
|
|
546
|
+ return {};
|
|
|
547
|
+ }
|
|
|
548
|
+}
|