You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Worker.js 17KB

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