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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /* global TransformStream */
  2. /* eslint-disable no-bitwise */
  3. // Worker for E2EE/Insertable streams.
  4. //
  5. /**
  6. * Polyfill RTCEncoded(Audio|Video)Frame.getMetadata() (not available in M83, available M84+).
  7. * The polyfill can not be done on the prototype since its not exposed in workers. Instead,
  8. * it is done as another transformation to keep it separate.
  9. */
  10. function polyFillEncodedFrameMetadata(encodedFrame, controller) {
  11. if (!encodedFrame.getMetadata) {
  12. encodedFrame.getMetadata = function() {
  13. return {
  14. // TODO: provide a more complete polyfill based on additionalData for video.
  15. synchronizationSource: this.synchronizationSource,
  16. contributingSources: this.contributingSources
  17. };
  18. };
  19. }
  20. controller.enqueue(encodedFrame);
  21. }
  22. /**
  23. * Compares two byteArrays for equality.
  24. */
  25. function isArrayEqual(a1, a2) {
  26. if (a1.byteLength !== a2.byteLength) {
  27. return false;
  28. }
  29. for (let i = 0; i < a1.byteLength; i++) {
  30. if (a1[i] !== a2[i]) {
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. // We use a ringbuffer of keys so we can change them and still decode packets that were
  37. // encrypted with an old key.
  38. const keyRingSize = 3;
  39. // We copy the first bytes of the VP8 payload unencrypted.
  40. // For keyframes this is 10 bytes, for non-keyframes (delta) 3. See
  41. // https://tools.ietf.org/html/rfc6386#section-9.1
  42. // This allows the bridge to continue detecting keyframes (only one byte needed in the JVB)
  43. // and is also a bit easier for the VP8 decoder (i.e. it generates funny garbage pictures
  44. // instead of being unable to decode).
  45. // This is a bit for show and we might want to reduce to 1 unconditionally in the final version.
  46. //
  47. // For audio (where frame.type is not set) we do not encrypt the opus TOC byte:
  48. // https://tools.ietf.org/html/rfc6716#section-3.1
  49. const unencryptedBytes = {
  50. key: 10,
  51. delta: 3,
  52. undefined: 1 // frame.type is not set on audio
  53. };
  54. // Use truncated SHA-256 hashes, 80 bіts for video, 32 bits for audio.
  55. // This follows the same principles as DTLS-SRTP.
  56. const authenticationTagOptions = {
  57. name: 'HMAC',
  58. hash: 'SHA-256'
  59. };
  60. const digestLength = {
  61. key: 10,
  62. delta: 10,
  63. undefined: 4 // frame.type is not set on audio
  64. };
  65. // Maximum number of forward ratchets to attempt when the authentication
  66. // tag on a remote packet does not match the current key.
  67. const ratchetWindow = 8;
  68. /**
  69. * Derives a set of keys from the master key.
  70. * @param {CryptoKey} material - master key to derive from
  71. *
  72. * See https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.1
  73. */
  74. async function deriveKeys(material) {
  75. const info = new ArrayBuffer();
  76. const textEncoder = new TextEncoder();
  77. // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#HKDF
  78. // https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams
  79. const encryptionKey = await crypto.subtle.deriveKey({
  80. name: 'HKDF',
  81. salt: textEncoder.encode('JFrameEncryptionKey'),
  82. hash: 'SHA-256',
  83. info
  84. }, material, {
  85. name: 'AES-CTR',
  86. length: 128
  87. }, false, [ 'encrypt', 'decrypt' ]);
  88. const authenticationKey = await crypto.subtle.deriveKey({
  89. name: 'HKDF',
  90. salt: textEncoder.encode('JFrameAuthenticationKey'),
  91. hash: 'SHA-256',
  92. info
  93. }, material, {
  94. name: 'HMAC',
  95. hash: 'SHA-256'
  96. }, false, [ 'sign' ]);
  97. const saltKey = await crypto.subtle.deriveBits({
  98. name: 'HKDF',
  99. salt: textEncoder.encode('JFrameSaltKey'),
  100. hash: 'SHA-256',
  101. info
  102. }, material, 128);
  103. return {
  104. material,
  105. encryptionKey,
  106. authenticationKey,
  107. saltKey
  108. };
  109. }
  110. /**
  111. * Ratchets a key. See
  112. * https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1
  113. * @param {CryptoKey} material - base key material
  114. * @returns {ArrayBuffer} - ratcheted key material
  115. */
  116. async function ratchet(material) {
  117. const textEncoder = new TextEncoder();
  118. return crypto.subtle.deriveBits({
  119. name: 'HKDF',
  120. salt: textEncoder.encode('JFrameRatchetKey'),
  121. hash: 'SHA-256',
  122. info: new ArrayBuffer()
  123. }, material, 256);
  124. }
  125. /**
  126. * Per-participant context holding the cryptographic keys and
  127. * encode/decode functions
  128. */
  129. class Context {
  130. /**
  131. * @param {string} id - local muc resourcepart
  132. */
  133. constructor(id) {
  134. // An array (ring) of keys that we use for sending and receiving.
  135. this._cryptoKeyRing = new Array(keyRingSize);
  136. // A pointer to the currently used key.
  137. this._currentKeyIndex = -1;
  138. // A per-sender counter that is used create the AES CTR.
  139. // Must be incremented on every frame that is sent, can be reset on
  140. // key changes.
  141. this._sendCount = 0n;
  142. this._id = id;
  143. }
  144. /**
  145. * Derives the different subkeys and starts using them for encryption or
  146. * decryption.
  147. * @param {Uint8Array|false} key bytes. Pass false to disable.
  148. * @param {Number} keyIndex
  149. */
  150. async setKey(keyBytes, keyIndex) {
  151. let newKey;
  152. if (keyBytes) {
  153. // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
  154. const material = await crypto.subtle.importKey('raw', keyBytes,
  155. 'HKDF', false, [ 'deriveBits', 'deriveKey' ]);
  156. newKey = await deriveKeys(material);
  157. } else {
  158. newKey = false;
  159. }
  160. this._currentKeyIndex = keyIndex % this._cryptoKeyRing.length;
  161. this._setKeys(newKey);
  162. }
  163. /**
  164. * Sets a set of keys and resets the sendCount.
  165. * decryption.
  166. * @param {Object} keys set of keys.
  167. */
  168. _setKeys(keys) {
  169. this._cryptoKeyRing[this._currentKeyIndex] = keys;
  170. this._sendCount = 0n; // Reset the send count (bigint).
  171. }
  172. /**
  173. * Ratchets a key forward one step.
  174. */
  175. async ratchet() {
  176. const keys = this._cryptoKeyRing[this._currentKeyIndex];
  177. const material = await ratchet(keys.material);
  178. this.setKey(material, this._currentKeyIndex);
  179. }
  180. /**
  181. * Function that will be injected in a stream and will encrypt the given encoded frames.
  182. *
  183. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  184. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  185. *
  186. * The packet format is a variant of
  187. * https://tools.ietf.org/html/draft-omara-sframe-00
  188. * using a trailer instead of a header. One of the design goals was to not require
  189. * changes to the SFU which for video requires not encrypting the keyframe bit of VP8
  190. * as SFUs need to detect a keyframe (framemarking or the generic frame descriptor will
  191. * solve this eventually). This also "hides" that a client is using E2EE a bit.
  192. *
  193. * Note that this operates on the full frame, i.e. for VP8 the data described in
  194. * https://tools.ietf.org/html/rfc6386#section-9.1
  195. *
  196. * The VP8 payload descriptor described in
  197. * https://tools.ietf.org/html/rfc7741#section-4.2
  198. * is part of the RTP packet and not part of the encoded frame and is therefore not
  199. * controllable by us. This is fine as the SFU keeps having access to it for routing.
  200. */
  201. encodeFunction(encodedFrame, controller) {
  202. const keyIndex = this._currentKeyIndex;
  203. if (this._cryptoKeyRing[keyIndex]) {
  204. this._sendCount++;
  205. // Thіs is not encrypted and contains the VP8 payload descriptor or the Opus TOC byte.
  206. const frameHeader = new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type]);
  207. // Construct frame trailer. Similar to the frame header described in
  208. // https://tools.ietf.org/html/draft-omara-sframe-00#section-4.2
  209. // but we put it at the end.
  210. // 0 1 2 3 4 5 6 7
  211. // ---------+---------------------------------+-+-+-+-+-+-+-+-+
  212. // payload | CTR... (length=LEN) |S|LEN |0| KID |
  213. // ---------+---------------------------------+-+-+-+-+-+-+-+-+
  214. const counter = new Uint8Array(16);
  215. const counterView = new DataView(counter.buffer);
  216. // The counter is encoded as a variable-length field.
  217. counterView.setBigUint64(8, this._sendCount);
  218. let counterLength = 8;
  219. for (let i = 8; i < counter.byteLength; i++ && counterLength--) {
  220. if (counterView.getUint8(i) !== 0) {
  221. break;
  222. }
  223. }
  224. const frameTrailer = new Uint8Array(counterLength + 1);
  225. frameTrailer.set(new Uint8Array(counter.buffer, counter.byteLength - counterLength));
  226. // Since we never send a counter of 0 we send counterLength - 1 on the wire.
  227. // This is different from the sframe draft, increases the key space and lets us
  228. // ignore the case of a zero-length counter at the receiver.
  229. frameTrailer[frameTrailer.byteLength - 1] = keyIndex | ((counterLength - 1) << 4);
  230. // XOR the counter with the saltKey to construct the AES CTR.
  231. const saltKey = new DataView(this._cryptoKeyRing[keyIndex].saltKey);
  232. for (let i = 0; i < counter.byteLength; i++) {
  233. counterView.setUint8(i, counterView.getUint8(i) ^ saltKey.getUint8(i));
  234. }
  235. return crypto.subtle.encrypt({
  236. name: 'AES-CTR',
  237. counter,
  238. length: 64
  239. }, this._cryptoKeyRing[keyIndex].encryptionKey, new Uint8Array(encodedFrame.data,
  240. unencryptedBytes[encodedFrame.type]))
  241. .then(cipherText => {
  242. const newData = new ArrayBuffer(frameHeader.byteLength + cipherText.byteLength
  243. + digestLength[encodedFrame.type] + frameTrailer.byteLength);
  244. const newUint8 = new Uint8Array(newData);
  245. newUint8.set(frameHeader); // copy first bytes.
  246. newUint8.set(new Uint8Array(cipherText), unencryptedBytes[encodedFrame.type]); // add ciphertext.
  247. // Leave some space for the authentication tag. This is filled with 0s initially, similar to
  248. // STUN message-integrity described in https://tools.ietf.org/html/rfc5389#section-15.4
  249. newUint8.set(frameTrailer, frameHeader.byteLength + cipherText.byteLength
  250. + digestLength[encodedFrame.type]); // append trailer.
  251. return crypto.subtle.sign(authenticationTagOptions, this._cryptoKeyRing[keyIndex].authenticationKey,
  252. new Uint8Array(newData)).then(authTag => {
  253. // Set the truncated authentication tag.
  254. newUint8.set(new Uint8Array(authTag, 0, digestLength[encodedFrame.type]),
  255. unencryptedBytes[encodedFrame.type] + cipherText.byteLength);
  256. encodedFrame.data = newData;
  257. return controller.enqueue(encodedFrame);
  258. });
  259. }, e => {
  260. // TODO: surface this to the app.
  261. console.error(e);
  262. // We are not enqueuing the frame here on purpose.
  263. });
  264. }
  265. /* NOTE WELL:
  266. * This will send unencrypted data (only protected by DTLS transport encryption) when no key is configured.
  267. * This is ok for demo purposes but should not be done once this becomes more relied upon.
  268. */
  269. controller.enqueue(encodedFrame);
  270. }
  271. /**
  272. * Function that will be injected in a stream and will decrypt the given encoded frames.
  273. *
  274. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  275. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  276. */
  277. async decodeFunction(encodedFrame, controller) {
  278. const data = new Uint8Array(encodedFrame.data);
  279. const keyIndex = data[encodedFrame.data.byteLength - 1] & 0x7;
  280. if (this._cryptoKeyRing[keyIndex]) {
  281. const counterLength = 1 + ((data[encodedFrame.data.byteLength - 1] >> 4) & 0x7);
  282. const frameHeader = new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type]);
  283. // Extract the truncated authentication tag.
  284. const authTagOffset = encodedFrame.data.byteLength - (digestLength[encodedFrame.type]
  285. + counterLength + 1);
  286. const authTag = encodedFrame.data.slice(authTagOffset, authTagOffset
  287. + digestLength[encodedFrame.type]);
  288. // Set authentication tag bytes to 0.
  289. const zeros = new Uint8Array(digestLength[encodedFrame.type]);
  290. data.set(zeros, encodedFrame.data.byteLength - (digestLength[encodedFrame.type] + counterLength + 1));
  291. // Do truncated hash comparison. If the hash does not match we might have to advance the
  292. // ratchet a limited number of times. See (even though the description there is odd)
  293. // https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1
  294. let { authenticationKey, material } = this._cryptoKeyRing[keyIndex];
  295. let valid = false;
  296. let newKeys = null;
  297. for (let distance = 0; distance < ratchetWindow; distance++) {
  298. const calculatedTag = await crypto.subtle.sign(authenticationTagOptions,
  299. authenticationKey, encodedFrame.data);
  300. if (isArrayEqual(new Uint8Array(authTag),
  301. new Uint8Array(calculatedTag.slice(0, digestLength[encodedFrame.type])))) {
  302. valid = true;
  303. if (distance > 0) {
  304. this._setKeys(newKeys);
  305. }
  306. break;
  307. }
  308. // Attempt to ratchet and generate the next set of keys.
  309. material = await crypto.subtle.importKey('raw', await ratchet(material),
  310. 'HKDF', false, [ 'deriveBits', 'deriveKey' ]);
  311. newKeys = await deriveKeys(material);
  312. authenticationKey = newKeys.authenticationKey;
  313. }
  314. // Check whether we found a valid signature.
  315. if (!valid) {
  316. // TODO: return an error to the app.
  317. console.error('Authentication tag mismatch');
  318. return;
  319. }
  320. // Extract the counter.
  321. const counter = new Uint8Array(16);
  322. counter.set(data.slice(encodedFrame.data.byteLength - (counterLength + 1),
  323. encodedFrame.data.byteLength - 1), 16 - counterLength);
  324. const counterView = new DataView(counter.buffer);
  325. // XOR the counter with the saltKey to construct the AES CTR.
  326. const saltKey = new DataView(this._cryptoKeyRing[keyIndex].saltKey);
  327. for (let i = 0; i < counter.byteLength; i++) {
  328. counterView.setUint8(i,
  329. counterView.getUint8(i) ^ saltKey.getUint8(i));
  330. }
  331. return crypto.subtle.decrypt({
  332. name: 'AES-CTR',
  333. counter,
  334. length: 64
  335. }, this._cryptoKeyRing[keyIndex].encryptionKey, new Uint8Array(encodedFrame.data,
  336. unencryptedBytes[encodedFrame.type],
  337. encodedFrame.data.byteLength - (unencryptedBytes[encodedFrame.type]
  338. + digestLength[encodedFrame.type] + counterLength + 1))
  339. ).then(plainText => {
  340. const newData = new ArrayBuffer(unencryptedBytes[encodedFrame.type] + plainText.byteLength);
  341. const newUint8 = new Uint8Array(newData);
  342. newUint8.set(frameHeader);
  343. newUint8.set(new Uint8Array(plainText), unencryptedBytes[encodedFrame.type]);
  344. encodedFrame.data = newData;
  345. return controller.enqueue(encodedFrame);
  346. }, e => {
  347. console.error(e);
  348. // TODO: notify the application about error status.
  349. // TODO: For video we need a better strategy since we do not want to based any
  350. // non-error frames on a garbage keyframe.
  351. if (encodedFrame.type === undefined) { // audio, replace with silence.
  352. const newData = new ArrayBuffer(3);
  353. const newUint8 = new Uint8Array(newData);
  354. newUint8.set([ 0xd8, 0xff, 0xfe ]); // opus silence frame.
  355. encodedFrame.data = newData;
  356. controller.enqueue(encodedFrame);
  357. }
  358. });
  359. } else if (keyIndex >= this._cryptoKeyRing.length && this._cryptoKeyRing[this._currentKeyIndex]) {
  360. // If we are encrypting but don't have a key for the remote drop the frame.
  361. // This is a heuristic since we don't know whether a packet is encrypted,
  362. // do not have a checksum and do not have signaling for whether a remote participant does
  363. // encrypt or not.
  364. return;
  365. }
  366. // TODO: this just passes through to the decoder. Is that ok? If we don't know the key yet
  367. // we might want to buffer a bit but it is still unclear how to do that (and for how long etc).
  368. controller.enqueue(encodedFrame);
  369. }
  370. }
  371. const contexts = new Map(); // Map participant id => context
  372. onmessage = async event => {
  373. const { operation } = event.data;
  374. if (operation === 'encode') {
  375. const { readableStream, writableStream, participantId } = event.data;
  376. if (!contexts.has(participantId)) {
  377. contexts.set(participantId, new Context(participantId));
  378. }
  379. const context = contexts.get(participantId);
  380. const transformStream = new TransformStream({
  381. transform: context.encodeFunction.bind(context)
  382. });
  383. readableStream
  384. .pipeThrough(new TransformStream({
  385. transform: polyFillEncodedFrameMetadata // M83 polyfill.
  386. }))
  387. .pipeThrough(transformStream)
  388. .pipeTo(writableStream);
  389. } else if (operation === 'decode') {
  390. const { readableStream, writableStream, participantId } = event.data;
  391. if (!contexts.has(participantId)) {
  392. contexts.set(participantId, new Context(participantId));
  393. }
  394. const context = contexts.get(participantId);
  395. const transformStream = new TransformStream({
  396. transform: context.decodeFunction.bind(context)
  397. });
  398. readableStream
  399. .pipeThrough(new TransformStream({
  400. transform: polyFillEncodedFrameMetadata // M83 polyfill.
  401. }))
  402. .pipeThrough(transformStream)
  403. .pipeTo(writableStream);
  404. } else if (operation === 'setKey') {
  405. const { participantId, key, keyIndex } = event.data;
  406. if (!contexts.has(participantId)) {
  407. contexts.set(participantId, new Context(participantId));
  408. }
  409. const context = contexts.get(participantId);
  410. if (key) {
  411. context.setKey(key, keyIndex);
  412. } else {
  413. context.setKey(false, keyIndex);
  414. }
  415. } else if (operation === 'ratchet') {
  416. const { participantId } = event.data;
  417. // TODO: can we ensure this is for our own sender key?
  418. if (!contexts.has(participantId)) {
  419. console.error('Could not find context for', participantId);
  420. return;
  421. }
  422. const context = contexts.get(participantId);
  423. context.ratchet();
  424. } else if (operation === 'cleanup') {
  425. const { participantId } = event.data;
  426. contexts.delete(participantId);
  427. } else {
  428. console.error('e2ee worker', operation);
  429. }
  430. };