|
@@ -1,36 +1,10 @@
|
1
|
|
-/* global __filename, TransformStream */
|
|
1
|
+/* global __filename */
|
2
|
2
|
|
|
3
|
+import { e2eeWorkerScript } from './Worker';
|
3
|
4
|
import { getLogger } from 'jitsi-meet-logger';
|
4
|
5
|
|
5
|
6
|
const logger = getLogger(__filename);
|
6
|
7
|
|
7
|
|
-// We use a ringbuffer of keys so we can change them and still decode packets that were
|
8
|
|
-// encrypted with an old key.
|
9
|
|
-// In the future when we dont rely on a globally shared key we will actually use it. For
|
10
|
|
-// now set the size to 1 which means there is only a single key. This causes some
|
11
|
|
-// glitches when changing the key but its ok.
|
12
|
|
-const keyRingSize = 1;
|
13
|
|
-
|
14
|
|
-// We use a 96 bit IV for AES GCM. This is signalled in plain together with the
|
15
|
|
-// packet. See https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams
|
16
|
|
-const ivLength = 12;
|
17
|
|
-
|
18
|
|
-// We copy the first bytes of the VP8 payload unencrypted.
|
19
|
|
-// For keyframes this is 10 bytes, for non-keyframes (delta) 3. See
|
20
|
|
-// https://tools.ietf.org/html/rfc6386#section-9.1
|
21
|
|
-// This allows the bridge to continue detecting keyframes (only one byte needed in the JVB)
|
22
|
|
-// and is also a bit easier for the VP8 decoder (i.e. it generates funny garbage pictures
|
23
|
|
-// instead of being unable to decode).
|
24
|
|
-// This is a bit for show and we might want to reduce to 1 unconditionally in the final version.
|
25
|
|
-//
|
26
|
|
-// For audio (where frame.type is not set) we do not encrypt the opus TOC byte:
|
27
|
|
-// https://tools.ietf.org/html/rfc6716#section-3.1
|
28
|
|
-const unencryptedBytes = {
|
29
|
|
- key: 10,
|
30
|
|
- delta: 3,
|
31
|
|
- undefined: 1 // frame.type is not set on audio
|
32
|
|
-};
|
33
|
|
-
|
34
|
8
|
// Flag to set on senders / receivers to avoid setting up the encryption transform
|
35
|
9
|
// more than once.
|
36
|
10
|
const kJitsiE2EE = Symbol('kJitsiE2EE');
|
|
@@ -62,20 +36,20 @@ export default class E2EEcontext {
|
62
|
36
|
constructor(options) {
|
63
|
37
|
this._options = options;
|
64
|
38
|
|
65
|
|
- // An array (ring) of keys that we use for sending and receiving.
|
66
|
|
- this._cryptoKeyRing = new Array(keyRingSize);
|
67
|
|
-
|
68
|
|
- // A pointer to the currently used key.
|
69
|
|
- this._currentKeyIndex = -1;
|
70
|
|
-
|
71
|
|
- // We keep track of how many frames we have sent per ssrc.
|
72
|
|
- // Starts with a random offset similar to the RTP sequence number.
|
73
|
|
- this._sendCounts = new Map();
|
|
39
|
+ // Initialize the E2EE worker.
|
|
40
|
+ this._worker = new Worker(e2eeWorkerScript, {
|
|
41
|
+ name: 'E2EE Worker'
|
|
42
|
+ });
|
|
43
|
+ this._worker.onerror = e => logger.onerror(e);
|
74
|
44
|
|
75
|
45
|
// Initialize the salt and convert it once.
|
76
|
46
|
const encoder = new TextEncoder();
|
77
|
47
|
|
78
|
|
- this._salt = encoder.encode(options.salt);
|
|
48
|
+ // Send initial options to worker.
|
|
49
|
+ this._worker.postMessage({
|
|
50
|
+ operation: 'initialize',
|
|
51
|
+ salt: encoder.encode(options.salt)
|
|
52
|
+ });
|
79
|
53
|
}
|
80
|
54
|
|
81
|
55
|
/**
|
|
@@ -89,18 +63,16 @@ export default class E2EEcontext {
|
89
|
63
|
if (receiver[kJitsiE2EE]) {
|
90
|
64
|
return;
|
91
|
65
|
}
|
|
66
|
+ receiver[kJitsiE2EE] = true;
|
92
|
67
|
|
93
|
68
|
const receiverStreams
|
94
|
69
|
= kind === 'video' ? receiver.createEncodedVideoStreams() : receiver.createEncodedAudioStreams();
|
95
|
|
- const transform = new TransformStream({
|
96
|
|
- transform: this._decodeFunction.bind(this)
|
97
|
|
- });
|
98
|
|
-
|
99
|
|
- receiverStreams.readableStream
|
100
|
|
- .pipeThrough(transform)
|
101
|
|
- .pipeTo(receiverStreams.writableStream);
|
102
|
70
|
|
103
|
|
- receiver[kJitsiE2EE] = true;
|
|
71
|
+ this._worker.postMessage({
|
|
72
|
+ operation: 'decode',
|
|
73
|
+ readableStream: receiverStreams.readableStream,
|
|
74
|
+ writableStream: receiverStreams.writableStream
|
|
75
|
+ }, [ receiverStreams.readableStream, receiverStreams.writableStream ]);
|
104
|
76
|
}
|
105
|
77
|
|
106
|
78
|
/**
|
|
@@ -114,18 +86,16 @@ export default class E2EEcontext {
|
114
|
86
|
if (sender[kJitsiE2EE]) {
|
115
|
87
|
return;
|
116
|
88
|
}
|
|
89
|
+ sender[kJitsiE2EE] = true;
|
117
|
90
|
|
118
|
91
|
const senderStreams
|
119
|
92
|
= kind === 'video' ? sender.createEncodedVideoStreams() : sender.createEncodedAudioStreams();
|
120
|
|
- const transform = new TransformStream({
|
121
|
|
- transform: this._encodeFunction.bind(this)
|
122
|
|
- });
|
123
|
|
-
|
124
|
|
- senderStreams.readableStream
|
125
|
|
- .pipeThrough(transform)
|
126
|
|
- .pipeTo(senderStreams.writableStream);
|
127
|
93
|
|
128
|
|
- sender[kJitsiE2EE] = true;
|
|
94
|
+ this._worker.postMessage({
|
|
95
|
+ operation: 'encode',
|
|
96
|
+ readableStream: senderStreams.readableStream,
|
|
97
|
+ writableStream: senderStreams.writableStream
|
|
98
|
+ }, [ senderStreams.readableStream, senderStreams.writableStream ]);
|
129
|
99
|
}
|
130
|
100
|
|
131
|
101
|
/**
|
|
@@ -133,225 +103,20 @@ export default class E2EEcontext {
|
133
|
103
|
*
|
134
|
104
|
* @param {string} value - Value to be used as the new key. May be falsy to disable end-to-end encryption.
|
135
|
105
|
*/
|
136
|
|
- async setKey(value) {
|
|
106
|
+ setKey(value) {
|
137
|
107
|
let key;
|
138
|
108
|
|
139
|
109
|
if (value) {
|
140
|
110
|
const encoder = new TextEncoder();
|
141
|
111
|
|
142
|
|
- key = await this._deriveKey(encoder.encode(value));
|
|
112
|
+ key = encoder.encode(value);
|
143
|
113
|
} else {
|
144
|
114
|
key = false;
|
145
|
115
|
}
|
146
|
|
- this._currentKeyIndex++;
|
147
|
|
- this._cryptoKeyRing[this._currentKeyIndex % this._cryptoKeyRing.length] = key;
|
148
|
|
- }
|
149
|
|
-
|
150
|
|
- /**
|
151
|
|
- * Derives a AES-GCM key with 128 bits from the input using PBKDF2
|
152
|
|
- * The salt is configured in the constructor of this class.
|
153
|
|
- * @param {Uint8Array} keyBytes - Value to derive key from
|
154
|
|
- */
|
155
|
|
- async _deriveKey(keyBytes) {
|
156
|
|
- // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
|
157
|
|
- const material = await crypto.subtle.importKey('raw', keyBytes,
|
158
|
|
- 'PBKDF2', false, [ 'deriveBits', 'deriveKey' ]);
|
159
|
|
-
|
160
|
|
- // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#PBKDF2
|
161
|
|
- return crypto.subtle.deriveKey({
|
162
|
|
- name: 'PBKDF2',
|
163
|
|
- salt: this._salt,
|
164
|
|
- iterations: 100000,
|
165
|
|
- hash: 'SHA-256'
|
166
|
|
- }, material, {
|
167
|
|
- name: 'AES-GCM',
|
168
|
|
- length: 128
|
169
|
|
- }, false, [ 'encrypt', 'decrypt' ]);
|
170
|
|
- }
|
171
|
|
-
|
172
|
|
- /**
|
173
|
|
- * Construct the IV used for AES-GCM and sent (in plain) with the packet similar to
|
174
|
|
- * https://tools.ietf.org/html/rfc7714#section-8.1
|
175
|
|
- * It concatenates
|
176
|
|
- * - the 32 bit synchronization source (SSRC) given on the encoded frame,
|
177
|
|
- * - the 32 bit rtp timestamp given on the encoded frame,
|
178
|
|
- * - a send counter that is specific to the SSRC. Starts at a random number.
|
179
|
|
- * The send counter is essentially the pictureId but we currently have to implement this ourselves.
|
180
|
|
- * There is no XOR with a salt. Note that this IV leaks the SSRC to the receiver but since this is
|
181
|
|
- * randomly generated and SFUs may not rewrite this is considered acceptable.
|
182
|
|
- * The SSRC is used to allow demultiplexing multiple streams with the same key, as described in
|
183
|
|
- * https://tools.ietf.org/html/rfc3711#section-4.1.1
|
184
|
|
- * The RTP timestamp is 32 bits and advances by the codec clock rate (90khz for video, 48khz for
|
185
|
|
- * opus audio) every second. For video it rolls over roughly every 13 hours.
|
186
|
|
- * The send counter will advance at the frame rate (30fps for video, 50fps for 20ms opus audio)
|
187
|
|
- * every second. It will take a long time to roll over.
|
188
|
|
- *
|
189
|
|
- * See also https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams
|
190
|
|
- */
|
191
|
|
- _makeIV(synchronizationSource, timestamp) {
|
192
|
|
- const iv = new ArrayBuffer(ivLength);
|
193
|
|
- const ivView = new DataView(iv);
|
194
|
|
-
|
195
|
|
- // having to keep our own send count (similar to a picture id) is not ideal.
|
196
|
|
- if (!this._sendCounts.has(synchronizationSource)) {
|
197
|
|
- // Initialize with a random offset, similar to the RTP sequence number.
|
198
|
|
- this._sendCounts.set(synchronizationSource, Math.floor(Math.random() * 0xFFFF));
|
199
|
|
- }
|
200
|
|
- const sendCount = this._sendCounts.get(synchronizationSource);
|
201
|
|
-
|
202
|
|
- ivView.setUint32(0, synchronizationSource);
|
203
|
|
- ivView.setUint32(4, timestamp);
|
204
|
|
- ivView.setUint32(8, sendCount % 0xFFFF);
|
205
|
|
-
|
206
|
|
- this._sendCounts.set(synchronizationSource, sendCount + 1);
|
207
|
|
-
|
208
|
|
- return iv;
|
209
|
|
- }
|
210
|
|
-
|
211
|
|
- /**
|
212
|
|
- * Function that will be injected in a stream and will encrypt the given encoded frames.
|
213
|
|
- *
|
214
|
|
- * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
|
215
|
|
- * @param {TransformStreamDefaultController} controller - TransportStreamController.
|
216
|
|
- *
|
217
|
|
- * The packet format is described below. One of the design goals was to not require
|
218
|
|
- * changes to the SFU which for video requires not encrypting the keyframe bit of VP8
|
219
|
|
- * as SFUs need to detect a keyframe (framemarking or the generic frame descriptor will
|
220
|
|
- * solve this eventually). This also "hides" that a client is using E2EE a bit.
|
221
|
|
- *
|
222
|
|
- * Note that this operates on the full frame, i.e. for VP8 the data described in
|
223
|
|
- * https://tools.ietf.org/html/rfc6386#section-9.1
|
224
|
|
- *
|
225
|
|
- * The VP8 payload descriptor described in
|
226
|
|
- * https://tools.ietf.org/html/rfc7741#section-4.2
|
227
|
|
- * is part of the RTP packet and not part of the frame and is not controllable by us.
|
228
|
|
- * This is fine as the SFU keeps having access to it for routing.
|
229
|
|
- *
|
230
|
|
- * The encrypted frame is formed as follows:
|
231
|
|
- * 1) Leave the first (10, 3, 1) bytes unencrypted, depending on the frame type and kind.
|
232
|
|
- * 2) Form the GCM IV for the frame as described above.
|
233
|
|
- * 3) Encrypt the rest of the frame using AES-GCM.
|
234
|
|
- * 4) Allocate space for the encrypted frame.
|
235
|
|
- * 5) Copy the unencrypted bytes to the start of the encrypted frame.
|
236
|
|
- * 6) Append the ciphertext to the encrypted frame.
|
237
|
|
- * 7) Append the IV.
|
238
|
|
- * 8) Append a single byte for the key identifier. TODO: we don't need all the bits.
|
239
|
|
- * 9) Enqueue the encrypted frame for sending.
|
240
|
|
- */
|
241
|
|
- _encodeFunction(encodedFrame, controller) {
|
242
|
|
- const keyIndex = this._currentKeyIndex % this._cryptoKeyRing.length;
|
243
|
|
-
|
244
|
|
- if (this._cryptoKeyRing[keyIndex]) {
|
245
|
|
- const iv = this._makeIV(encodedFrame.synchronizationSource, encodedFrame.timestamp);
|
246
|
|
-
|
247
|
|
- return crypto.subtle.encrypt({
|
248
|
|
- name: 'AES-GCM',
|
249
|
|
- iv,
|
250
|
|
- additionalData: new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type])
|
251
|
|
- }, this._cryptoKeyRing[keyIndex], new Uint8Array(encodedFrame.data, unencryptedBytes[encodedFrame.type]))
|
252
|
|
- .then(cipherText => {
|
253
|
|
- const newData = new ArrayBuffer(unencryptedBytes[encodedFrame.type] + cipherText.byteLength
|
254
|
|
- + iv.byteLength + 1);
|
255
|
|
- const newUint8 = new Uint8Array(newData);
|
256
|
|
-
|
257
|
|
- newUint8.set(
|
258
|
|
- new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type])); // copy first bytes.
|
259
|
|
- newUint8.set(
|
260
|
|
- new Uint8Array(cipherText), unencryptedBytes[encodedFrame.type]); // add ciphertext.
|
261
|
|
- newUint8.set(
|
262
|
|
- new Uint8Array(iv), unencryptedBytes[encodedFrame.type] + cipherText.byteLength); // append IV.
|
263
|
|
- newUint8[unencryptedBytes[encodedFrame.type] + cipherText.byteLength + ivLength]
|
264
|
|
- = keyIndex; // set key index.
|
265
|
|
-
|
266
|
|
- encodedFrame.data = newData;
|
267
|
|
-
|
268
|
|
- return controller.enqueue(encodedFrame);
|
269
|
|
- }, e => {
|
270
|
|
- logger.error(e);
|
271
|
|
-
|
272
|
|
- // We are not enqueuing the frame here on purpose.
|
273
|
|
- });
|
274
|
|
- }
|
275
|
|
-
|
276
|
|
- /* NOTE WELL:
|
277
|
|
- * This will send unencrypted data (only protected by DTLS transport encryption) when no key is configured.
|
278
|
|
- * This is ok for demo purposes but should not be done once this becomes more relied upon.
|
279
|
|
- */
|
280
|
|
- controller.enqueue(encodedFrame);
|
281
|
|
- }
|
282
|
|
-
|
283
|
|
- /**
|
284
|
|
- * Function that will be injected in a stream and will decrypt the given encoded frames.
|
285
|
|
- *
|
286
|
|
- * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
|
287
|
|
- * @param {TransformStreamDefaultController} controller - TransportStreamController.
|
288
|
|
- *
|
289
|
|
- * The decrypted frame is formed as follows:
|
290
|
|
- * 1) Extract the key index from the last byte of the encrypted frame.
|
291
|
|
- * If there is no key associated with the key index, the frame is enqueued for decoding
|
292
|
|
- * and these steps terminate.
|
293
|
|
- * 2) Determine the frame type in order to look up the number of unencrypted header bytes.
|
294
|
|
- * 2) Extract the 12-byte IV from its position near the end of the packet.
|
295
|
|
- * Note: the IV is treated as opaque and not reconstructed from the input.
|
296
|
|
- * 3) Decrypt the encrypted frame content after the unencrypted bytes using AES-GCM.
|
297
|
|
- * 4) Allocate space for the decrypted frame.
|
298
|
|
- * 5) Copy the unencrypted bytes from the start of the encrypted frame.
|
299
|
|
- * 6) Append the plaintext to the decrypted frame.
|
300
|
|
- * 7) Enqueue the decrypted frame for decoding.
|
301
|
|
- */
|
302
|
|
- _decodeFunction(encodedFrame, controller) {
|
303
|
|
- const data = new Uint8Array(encodedFrame.data);
|
304
|
|
- const keyIndex = data[encodedFrame.data.byteLength - 1];
|
305
|
|
-
|
306
|
|
- if (this._cryptoKeyRing[keyIndex]) {
|
307
|
|
- const iv = new Uint8Array(encodedFrame.data, encodedFrame.data.byteLength - ivLength - 1, ivLength);
|
308
|
|
- const cipherTextStart = unencryptedBytes[encodedFrame.type];
|
309
|
|
- const cipherTextLength = encodedFrame.data.byteLength - (unencryptedBytes[encodedFrame.type]
|
310
|
|
- + ivLength + 1);
|
311
|
|
-
|
312
|
|
- return crypto.subtle.decrypt({
|
313
|
|
- name: 'AES-GCM',
|
314
|
|
- iv,
|
315
|
|
- additionalData: new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type])
|
316
|
|
- }, this._cryptoKeyRing[keyIndex], new Uint8Array(encodedFrame.data, cipherTextStart, cipherTextLength))
|
317
|
|
- .then(plainText => {
|
318
|
|
- const newData = new ArrayBuffer(unencryptedBytes[encodedFrame.type] + plainText.byteLength);
|
319
|
|
- const newUint8 = new Uint8Array(newData);
|
320
|
|
-
|
321
|
|
- newUint8.set(new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type]));
|
322
|
|
- newUint8.set(new Uint8Array(plainText), unencryptedBytes[encodedFrame.type]);
|
323
|
|
-
|
324
|
|
- encodedFrame.data = newData;
|
325
|
|
-
|
326
|
|
- return controller.enqueue(encodedFrame);
|
327
|
|
- }, e => {
|
328
|
|
- logger.error(e, encodedFrame.type);
|
329
|
|
-
|
330
|
|
- // TODO: notify the application about error status.
|
331
|
|
-
|
332
|
|
- // TODO: For video we need a better strategy since we do not want to based any
|
333
|
|
- // non-error frames on a garbage keyframe.
|
334
|
|
- if (encodedFrame.type === undefined) { // audio, replace with silence.
|
335
|
|
- // audio, replace with silence.
|
336
|
|
- const newData = new ArrayBuffer(3);
|
337
|
|
- const newUint8 = new Uint8Array(newData);
|
338
|
|
-
|
339
|
|
- newUint8.set([ 0xd8, 0xff, 0xfe ]); // opus silence frame.
|
340
|
|
- encodedFrame.data = newData;
|
341
|
|
- controller.enqueue(encodedFrame);
|
342
|
|
- }
|
343
|
|
- });
|
344
|
|
- } else if (keyIndex >= this._cryptoKeyRing.length
|
345
|
|
- && this._cryptoKeyRing[this._currentKeyIndex % this._cryptoKeyRing.length]) {
|
346
|
|
- // If we are encrypting but don't have a key for the remote drop the frame.
|
347
|
|
- // This is a heuristic since we don't know whether a packet is encrypted,
|
348
|
|
- // do not have a checksum and do not have signaling for whether a remote participant does
|
349
|
|
- // encrypt or not.
|
350
|
|
- return;
|
351
|
|
- }
|
352
|
116
|
|
353
|
|
- // TODO: this just passes through to the decoder. Is that ok? If we don't know the key yet
|
354
|
|
- // we might want to buffer a bit but it is still unclear how to do that (and for how long etc).
|
355
|
|
- controller.enqueue(encodedFrame);
|
|
117
|
+ this._worker.postMessage({
|
|
118
|
+ operation: 'setKey',
|
|
119
|
+ key
|
|
120
|
+ });
|
356
|
121
|
}
|
357
|
122
|
}
|