Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. /**
  66. * Derives a set of keys from the master key.
  67. * @param {Uint8Array} keyBytes - Value to derive key from
  68. * @param {Uint8Array} salt - Salt used in key derivation
  69. *
  70. * See https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.1
  71. */
  72. async function deriveKeys(keyBytes) {
  73. // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
  74. const material = await crypto.subtle.importKey('raw', keyBytes,
  75. 'HKDF', false, [ 'deriveBits', 'deriveKey' ]);
  76. const info = new ArrayBuffer();
  77. const textEncoder = new TextEncoder();
  78. // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#HKDF
  79. // https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams
  80. const encryptionKey = await crypto.subtle.deriveKey({
  81. name: 'HKDF',
  82. salt: textEncoder.encode('JFrameEncryptionKey'),
  83. hash: 'SHA-256',
  84. info
  85. }, material, {
  86. name: 'AES-CTR',
  87. length: 128
  88. }, false, [ 'encrypt', 'decrypt' ]);
  89. const authenticationKey = await crypto.subtle.deriveKey({
  90. name: 'HKDF',
  91. salt: textEncoder.encode('JFrameAuthenticationKey'),
  92. hash: 'SHA-256',
  93. info
  94. }, material, {
  95. name: 'HMAC',
  96. hash: 'SHA-256'
  97. }, false, [ 'sign' ]);
  98. const saltKey = await crypto.subtle.deriveBits({
  99. name: 'HKDF',
  100. salt: textEncoder.encode('JFrameSaltKey'),
  101. hash: 'SHA-256',
  102. info
  103. }, material, 128);
  104. return {
  105. encryptionKey,
  106. authenticationKey,
  107. saltKey
  108. };
  109. }
  110. /**
  111. * Per-participant context holding the cryptographic keys and
  112. * encode/decode functions
  113. */
  114. class Context {
  115. /**
  116. * @param {string} id - local muc resourcepart
  117. */
  118. constructor(id) {
  119. // An array (ring) of keys that we use for sending and receiving.
  120. this._cryptoKeyRing = new Array(keyRingSize);
  121. // A pointer to the currently used key.
  122. this._currentKeyIndex = -1;
  123. // A per-sender counter that is used create the AES CTR.
  124. // Must be incremented on every frame that is sent, can be reset on
  125. // key changes.
  126. this._sendCount = 0n;
  127. this._id = id;
  128. }
  129. /**
  130. * Sets a key, derives the different subkeys and starts using them for encryption or
  131. * decryption.
  132. * @param {CryptoKey} key
  133. * @param {Number} keyIndex
  134. */
  135. async setKey(key, keyIndex) {
  136. this._currentKeyIndex = keyIndex % this._cryptoKeyRing.length;
  137. if (key) {
  138. this._cryptoKeyRing[this._currentKeyIndex] = await deriveKeys(key);
  139. } else {
  140. this._cryptoKeyRing[this._currentKeyIndex] = false;
  141. }
  142. this._sendCount = 0n; // Reset the send count (bigint).
  143. }
  144. /**
  145. * Function that will be injected in a stream and will encrypt the given encoded frames.
  146. *
  147. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  148. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  149. *
  150. * The packet format is a variant of
  151. * https://tools.ietf.org/html/draft-omara-sframe-00
  152. * using a trailer instead of a header. One of the design goals was to not require
  153. * changes to the SFU which for video requires not encrypting the keyframe bit of VP8
  154. * as SFUs need to detect a keyframe (framemarking or the generic frame descriptor will
  155. * solve this eventually). This also "hides" that a client is using E2EE a bit.
  156. *
  157. * Note that this operates on the full frame, i.e. for VP8 the data described in
  158. * https://tools.ietf.org/html/rfc6386#section-9.1
  159. *
  160. * The VP8 payload descriptor described in
  161. * https://tools.ietf.org/html/rfc7741#section-4.2
  162. * is part of the RTP packet and not part of the encoded frame and is therefore not
  163. * controllable by us. This is fine as the SFU keeps having access to it for routing.
  164. */
  165. encodeFunction(encodedFrame, controller) {
  166. const keyIndex = this._currentKeyIndex;
  167. if (this._cryptoKeyRing[keyIndex]) {
  168. this._sendCount++;
  169. // Thіs is not encrypted and contains the VP8 payload descriptor or the Opus TOC byte.
  170. const frameHeader = new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type]);
  171. // Construct frame trailer. Similar to the frame header described in
  172. // https://tools.ietf.org/html/draft-omara-sframe-00#section-4.2
  173. // but we put it at the end.
  174. // 0 1 2 3 4 5 6 7
  175. // ---------+---------------------------------+-+-+-+-+-+-+-+-+
  176. // payload | CTR... (length=LEN) |S|LEN |0| KID |
  177. // ---------+---------------------------------+-+-+-+-+-+-+-+-+
  178. const counter = new Uint8Array(16);
  179. const counterView = new DataView(counter.buffer);
  180. // The counter is encoded as a variable-length field.
  181. counterView.setBigUint64(8, this._sendCount);
  182. let counterLength = 8;
  183. for (let i = 8; i < counter.byteLength; i++ && counterLength--) {
  184. if (counterView.getUint8(i) !== 0) {
  185. break;
  186. }
  187. }
  188. const frameTrailer = new Uint8Array(counterLength + 1);
  189. frameTrailer.set(new Uint8Array(counter.buffer, counter.byteLength - counterLength));
  190. // Since we never send a counter of 0 we send counterLength - 1 on the wire.
  191. // This is different from the sframe draft, increases the key space and lets us
  192. // ignore the case of a zero-length counter at the receiver.
  193. frameTrailer[frameTrailer.byteLength - 1] = keyIndex | ((counterLength - 1) << 4);
  194. // XOR the counter with the saltKey to construct the AES CTR.
  195. const saltKey = new DataView(this._cryptoKeyRing[keyIndex].saltKey);
  196. for (let i = 0; i < counter.byteLength; i++) {
  197. counterView.setUint8(i, counterView.getUint8(i) ^ saltKey.getUint8(i));
  198. }
  199. return crypto.subtle.encrypt({
  200. name: 'AES-CTR',
  201. counter,
  202. length: 64
  203. }, this._cryptoKeyRing[keyIndex].encryptionKey, new Uint8Array(encodedFrame.data,
  204. unencryptedBytes[encodedFrame.type]))
  205. .then(cipherText => {
  206. const newData = new ArrayBuffer(frameHeader.byteLength + cipherText.byteLength
  207. + digestLength[encodedFrame.type] + frameTrailer.byteLength);
  208. const newUint8 = new Uint8Array(newData);
  209. newUint8.set(frameHeader); // copy first bytes.
  210. newUint8.set(new Uint8Array(cipherText), unencryptedBytes[encodedFrame.type]); // add ciphertext.
  211. // Leave some space for the authentication tag. This is filled with 0s initially, similar to
  212. // STUN message-integrity described in https://tools.ietf.org/html/rfc5389#section-15.4
  213. newUint8.set(frameTrailer, frameHeader.byteLength + cipherText.byteLength
  214. + digestLength[encodedFrame.type]); // append trailer.
  215. return crypto.subtle.sign(authenticationTagOptions, this._cryptoKeyRing[keyIndex].authenticationKey,
  216. new Uint8Array(newData)).then(authTag => {
  217. // Set the truncated authentication tag.
  218. newUint8.set(new Uint8Array(authTag, 0, digestLength[encodedFrame.type]),
  219. unencryptedBytes[encodedFrame.type] + cipherText.byteLength);
  220. encodedFrame.data = newData;
  221. return controller.enqueue(encodedFrame);
  222. });
  223. }, e => {
  224. // TODO: surface this to the app.
  225. console.error(e);
  226. // We are not enqueuing the frame here on purpose.
  227. });
  228. }
  229. /* NOTE WELL:
  230. * This will send unencrypted data (only protected by DTLS transport encryption) when no key is configured.
  231. * This is ok for demo purposes but should not be done once this becomes more relied upon.
  232. */
  233. controller.enqueue(encodedFrame);
  234. }
  235. /**
  236. * Function that will be injected in a stream and will decrypt the given encoded frames.
  237. *
  238. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  239. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  240. */
  241. async decodeFunction(encodedFrame, controller) {
  242. const data = new Uint8Array(encodedFrame.data);
  243. const keyIndex = data[encodedFrame.data.byteLength - 1] & 0x7;
  244. if (this._cryptoKeyRing[keyIndex]) {
  245. const counterLength = 1 + ((data[encodedFrame.data.byteLength - 1] >> 4) & 0x7);
  246. const frameHeader = new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type]);
  247. // Extract the truncated authentication tag.
  248. const authTagOffset = encodedFrame.data.byteLength - (digestLength[encodedFrame.type]
  249. + counterLength + 1);
  250. const authTag = encodedFrame.data.slice(authTagOffset, authTagOffset
  251. + digestLength[encodedFrame.type]);
  252. // Set authentication tag bytes to 0.
  253. const zeros = new Uint8Array(digestLength[encodedFrame.type]);
  254. data.set(zeros, encodedFrame.data.byteLength - (digestLength[encodedFrame.type] + counterLength + 1));
  255. const calculatedTag = await crypto.subtle.sign(authenticationTagOptions,
  256. this._cryptoKeyRing[keyIndex].authenticationKey, encodedFrame.data);
  257. // Do truncated hash comparison.
  258. if (!isArrayEqual(new Uint8Array(authTag),
  259. new Uint8Array(calculatedTag.slice(0, digestLength[encodedFrame.type])))) {
  260. // TODO: at this point we need to ratchet until we get a key that works. If we ratchet too often
  261. // we need to return an error to the app.
  262. console.error('Authentication tag mismatch', new Uint8Array(authTag), new Uint8Array(calculatedTag,
  263. 0, digestLength[encodedFrame.type]));
  264. return;
  265. }
  266. // Extract the counter.
  267. const counter = new Uint8Array(16);
  268. counter.set(data.slice(encodedFrame.data.byteLength - (counterLength + 1),
  269. encodedFrame.data.byteLength - 1), 16 - counterLength);
  270. const counterView = new DataView(counter.buffer);
  271. // XOR the counter with the saltKey to construct the AES CTR.
  272. const saltKey = new DataView(this._cryptoKeyRing[keyIndex].saltKey);
  273. for (let i = 0; i < counter.byteLength; i++) {
  274. counterView.setUint8(i,
  275. counterView.getUint8(i) ^ saltKey.getUint8(i));
  276. }
  277. return crypto.subtle.decrypt({
  278. name: 'AES-CTR',
  279. counter,
  280. length: 64
  281. }, this._cryptoKeyRing[keyIndex].encryptionKey, new Uint8Array(encodedFrame.data,
  282. unencryptedBytes[encodedFrame.type],
  283. encodedFrame.data.byteLength - (unencryptedBytes[encodedFrame.type]
  284. + digestLength[encodedFrame.type] + counterLength + 1))
  285. ).then(plainText => {
  286. const newData = new ArrayBuffer(unencryptedBytes[encodedFrame.type] + plainText.byteLength);
  287. const newUint8 = new Uint8Array(newData);
  288. newUint8.set(frameHeader);
  289. newUint8.set(new Uint8Array(plainText), unencryptedBytes[encodedFrame.type]);
  290. encodedFrame.data = newData;
  291. return controller.enqueue(encodedFrame);
  292. }, e => {
  293. console.error(e);
  294. // TODO: notify the application about error status.
  295. // TODO: For video we need a better strategy since we do not want to based any
  296. // non-error frames on a garbage keyframe.
  297. if (encodedFrame.type === undefined) { // audio, replace with silence.
  298. const newData = new ArrayBuffer(3);
  299. const newUint8 = new Uint8Array(newData);
  300. newUint8.set([ 0xd8, 0xff, 0xfe ]); // opus silence frame.
  301. encodedFrame.data = newData;
  302. controller.enqueue(encodedFrame);
  303. }
  304. });
  305. } else if (keyIndex >= this._cryptoKeyRing.length && this._cryptoKeyRing[this._currentKeyIndex]) {
  306. // If we are encrypting but don't have a key for the remote drop the frame.
  307. // This is a heuristic since we don't know whether a packet is encrypted,
  308. // do not have a checksum and do not have signaling for whether a remote participant does
  309. // encrypt or not.
  310. return;
  311. }
  312. // TODO: this just passes through to the decoder. Is that ok? If we don't know the key yet
  313. // we might want to buffer a bit but it is still unclear how to do that (and for how long etc).
  314. controller.enqueue(encodedFrame);
  315. }
  316. }
  317. const contexts = new Map(); // Map participant id => context
  318. onmessage = async event => {
  319. const { operation } = event.data;
  320. if (operation === 'encode') {
  321. const { readableStream, writableStream, participantId } = event.data;
  322. if (!contexts.has(participantId)) {
  323. contexts.set(participantId, new Context(participantId));
  324. }
  325. const context = contexts.get(participantId);
  326. const transformStream = new TransformStream({
  327. transform: context.encodeFunction.bind(context)
  328. });
  329. readableStream
  330. .pipeThrough(new TransformStream({
  331. transform: polyFillEncodedFrameMetadata // M83 polyfill.
  332. }))
  333. .pipeThrough(transformStream)
  334. .pipeTo(writableStream);
  335. } else if (operation === 'decode') {
  336. const { readableStream, writableStream, participantId } = event.data;
  337. if (!contexts.has(participantId)) {
  338. contexts.set(participantId, new Context(participantId));
  339. }
  340. const context = contexts.get(participantId);
  341. const transformStream = new TransformStream({
  342. transform: context.decodeFunction.bind(context)
  343. });
  344. readableStream
  345. .pipeThrough(new TransformStream({
  346. transform: polyFillEncodedFrameMetadata // M83 polyfill.
  347. }))
  348. .pipeThrough(transformStream)
  349. .pipeTo(writableStream);
  350. } else if (operation === 'setKey') {
  351. const { participantId, key, keyIndex } = event.data;
  352. if (!contexts.has(participantId)) {
  353. contexts.set(participantId, new Context(participantId));
  354. }
  355. const context = contexts.get(participantId);
  356. if (key) {
  357. context.setKey(key, keyIndex);
  358. } else {
  359. context.setKey(false, keyIndex);
  360. }
  361. } else if (operation === 'cleanup') {
  362. const { participantId } = event.data;
  363. contexts.delete(participantId);
  364. } else {
  365. console.error('e2ee worker', operation);
  366. }
  367. };