您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Worker.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. * Function that will be injected in a stream and will encrypt the given encoded frames.
  174. *
  175. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  176. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  177. *
  178. * The packet format is a variant of
  179. * https://tools.ietf.org/html/draft-omara-sframe-00
  180. * using a trailer instead of a header. One of the design goals was to not require
  181. * changes to the SFU which for video requires not encrypting the keyframe bit of VP8
  182. * as SFUs need to detect a keyframe (framemarking or the generic frame descriptor will
  183. * solve this eventually). This also "hides" that a client is using E2EE a bit.
  184. *
  185. * Note that this operates on the full frame, i.e. for VP8 the data described in
  186. * https://tools.ietf.org/html/rfc6386#section-9.1
  187. *
  188. * The VP8 payload descriptor described in
  189. * https://tools.ietf.org/html/rfc7741#section-4.2
  190. * is part of the RTP packet and not part of the encoded frame and is therefore not
  191. * controllable by us. This is fine as the SFU keeps having access to it for routing.
  192. */
  193. encodeFunction(encodedFrame, controller) {
  194. const keyIndex = this._currentKeyIndex;
  195. if (this._cryptoKeyRing[keyIndex]) {
  196. this._sendCount++;
  197. // Thіs is not encrypted and contains the VP8 payload descriptor or the Opus TOC byte.
  198. const frameHeader = new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type]);
  199. // Construct frame trailer. Similar to the frame header described in
  200. // https://tools.ietf.org/html/draft-omara-sframe-00#section-4.2
  201. // but we put it at the end.
  202. // 0 1 2 3 4 5 6 7
  203. // ---------+---------------------------------+-+-+-+-+-+-+-+-+
  204. // payload | CTR... (length=LEN) |S|LEN |0| KID |
  205. // ---------+---------------------------------+-+-+-+-+-+-+-+-+
  206. const counter = new Uint8Array(16);
  207. const counterView = new DataView(counter.buffer);
  208. // The counter is encoded as a variable-length field.
  209. counterView.setBigUint64(8, this._sendCount);
  210. let counterLength = 8;
  211. for (let i = 8; i < counter.byteLength; i++ && counterLength--) {
  212. if (counterView.getUint8(i) !== 0) {
  213. break;
  214. }
  215. }
  216. const frameTrailer = new Uint8Array(counterLength + 1);
  217. frameTrailer.set(new Uint8Array(counter.buffer, counter.byteLength - counterLength));
  218. // Since we never send a counter of 0 we send counterLength - 1 on the wire.
  219. // This is different from the sframe draft, increases the key space and lets us
  220. // ignore the case of a zero-length counter at the receiver.
  221. frameTrailer[frameTrailer.byteLength - 1] = keyIndex | ((counterLength - 1) << 4);
  222. // XOR the counter with the saltKey to construct the AES CTR.
  223. const saltKey = new DataView(this._cryptoKeyRing[keyIndex].saltKey);
  224. for (let i = 0; i < counter.byteLength; i++) {
  225. counterView.setUint8(i, counterView.getUint8(i) ^ saltKey.getUint8(i));
  226. }
  227. return crypto.subtle.encrypt({
  228. name: 'AES-CTR',
  229. counter,
  230. length: 64
  231. }, this._cryptoKeyRing[keyIndex].encryptionKey, new Uint8Array(encodedFrame.data,
  232. unencryptedBytes[encodedFrame.type]))
  233. .then(cipherText => {
  234. const newData = new ArrayBuffer(frameHeader.byteLength + cipherText.byteLength
  235. + digestLength[encodedFrame.type] + frameTrailer.byteLength);
  236. const newUint8 = new Uint8Array(newData);
  237. newUint8.set(frameHeader); // copy first bytes.
  238. newUint8.set(new Uint8Array(cipherText), unencryptedBytes[encodedFrame.type]); // add ciphertext.
  239. // Leave some space for the authentication tag. This is filled with 0s initially, similar to
  240. // STUN message-integrity described in https://tools.ietf.org/html/rfc5389#section-15.4
  241. newUint8.set(frameTrailer, frameHeader.byteLength + cipherText.byteLength
  242. + digestLength[encodedFrame.type]); // append trailer.
  243. return crypto.subtle.sign(authenticationTagOptions, this._cryptoKeyRing[keyIndex].authenticationKey,
  244. new Uint8Array(newData)).then(authTag => {
  245. // Set the truncated authentication tag.
  246. newUint8.set(new Uint8Array(authTag, 0, digestLength[encodedFrame.type]),
  247. unencryptedBytes[encodedFrame.type] + cipherText.byteLength);
  248. encodedFrame.data = newData;
  249. return controller.enqueue(encodedFrame);
  250. });
  251. }, e => {
  252. // TODO: surface this to the app.
  253. console.error(e);
  254. // We are not enqueuing the frame here on purpose.
  255. });
  256. }
  257. /* NOTE WELL:
  258. * This will send unencrypted data (only protected by DTLS transport encryption) when no key is configured.
  259. * This is ok for demo purposes but should not be done once this becomes more relied upon.
  260. */
  261. controller.enqueue(encodedFrame);
  262. }
  263. /**
  264. * Function that will be injected in a stream and will decrypt the given encoded frames.
  265. *
  266. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  267. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  268. */
  269. async decodeFunction(encodedFrame, controller) {
  270. const data = new Uint8Array(encodedFrame.data);
  271. const keyIndex = data[encodedFrame.data.byteLength - 1] & 0x7;
  272. if (this._cryptoKeyRing[keyIndex]) {
  273. const counterLength = 1 + ((data[encodedFrame.data.byteLength - 1] >> 4) & 0x7);
  274. const frameHeader = new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type]);
  275. // Extract the truncated authentication tag.
  276. const authTagOffset = encodedFrame.data.byteLength - (digestLength[encodedFrame.type]
  277. + counterLength + 1);
  278. const authTag = encodedFrame.data.slice(authTagOffset, authTagOffset
  279. + digestLength[encodedFrame.type]);
  280. // Set authentication tag bytes to 0.
  281. const zeros = new Uint8Array(digestLength[encodedFrame.type]);
  282. data.set(zeros, encodedFrame.data.byteLength - (digestLength[encodedFrame.type] + counterLength + 1));
  283. // Do truncated hash comparison. If the hash does not match we might have to advance the
  284. // ratchet a limited number of times. See (even though the description there is odd)
  285. // https://tools.ietf.org/html/draft-omara-sframe-00#section-4.3.5.1
  286. let { authenticationKey, material } = this._cryptoKeyRing[keyIndex];
  287. let valid = false;
  288. let newKeys = null;
  289. for (let distance = 0; distance < ratchetWindow; distance++) {
  290. const calculatedTag = await crypto.subtle.sign(authenticationTagOptions,
  291. authenticationKey, encodedFrame.data);
  292. if (isArrayEqual(new Uint8Array(authTag),
  293. new Uint8Array(calculatedTag.slice(0, digestLength[encodedFrame.type])))) {
  294. valid = true;
  295. if (distance > 0) {
  296. this._setKeys(newKeys);
  297. }
  298. break;
  299. }
  300. // Attempt to ratchet and generate the next set of keys.
  301. material = await crypto.subtle.importKey('raw', await ratchet(material),
  302. 'HKDF', false, [ 'deriveBits', 'deriveKey' ]);
  303. newKeys = await deriveKeys(material);
  304. authenticationKey = newKeys.authenticationKey;
  305. }
  306. // Check whether we found a valid signature.
  307. if (!valid) {
  308. // TODO: return an error to the app.
  309. console.error('Authentication tag mismatch');
  310. return;
  311. }
  312. // Extract the counter.
  313. const counter = new Uint8Array(16);
  314. counter.set(data.slice(encodedFrame.data.byteLength - (counterLength + 1),
  315. encodedFrame.data.byteLength - 1), 16 - counterLength);
  316. const counterView = new DataView(counter.buffer);
  317. // XOR the counter with the saltKey to construct the AES CTR.
  318. const saltKey = new DataView(this._cryptoKeyRing[keyIndex].saltKey);
  319. for (let i = 0; i < counter.byteLength; i++) {
  320. counterView.setUint8(i,
  321. counterView.getUint8(i) ^ saltKey.getUint8(i));
  322. }
  323. return crypto.subtle.decrypt({
  324. name: 'AES-CTR',
  325. counter,
  326. length: 64
  327. }, this._cryptoKeyRing[keyIndex].encryptionKey, new Uint8Array(encodedFrame.data,
  328. unencryptedBytes[encodedFrame.type],
  329. encodedFrame.data.byteLength - (unencryptedBytes[encodedFrame.type]
  330. + digestLength[encodedFrame.type] + counterLength + 1))
  331. ).then(plainText => {
  332. const newData = new ArrayBuffer(unencryptedBytes[encodedFrame.type] + plainText.byteLength);
  333. const newUint8 = new Uint8Array(newData);
  334. newUint8.set(frameHeader);
  335. newUint8.set(new Uint8Array(plainText), unencryptedBytes[encodedFrame.type]);
  336. encodedFrame.data = newData;
  337. return controller.enqueue(encodedFrame);
  338. }, e => {
  339. console.error(e);
  340. // TODO: notify the application about error status.
  341. // TODO: For video we need a better strategy since we do not want to based any
  342. // non-error frames on a garbage keyframe.
  343. if (encodedFrame.type === undefined) { // audio, replace with silence.
  344. const newData = new ArrayBuffer(3);
  345. const newUint8 = new Uint8Array(newData);
  346. newUint8.set([ 0xd8, 0xff, 0xfe ]); // opus silence frame.
  347. encodedFrame.data = newData;
  348. controller.enqueue(encodedFrame);
  349. }
  350. });
  351. } else if (keyIndex >= this._cryptoKeyRing.length && this._cryptoKeyRing[this._currentKeyIndex]) {
  352. // If we are encrypting but don't have a key for the remote drop the frame.
  353. // This is a heuristic since we don't know whether a packet is encrypted,
  354. // do not have a checksum and do not have signaling for whether a remote participant does
  355. // encrypt or not.
  356. return;
  357. }
  358. // TODO: this just passes through to the decoder. Is that ok? If we don't know the key yet
  359. // we might want to buffer a bit but it is still unclear how to do that (and for how long etc).
  360. controller.enqueue(encodedFrame);
  361. }
  362. }
  363. const contexts = new Map(); // Map participant id => context
  364. onmessage = async event => {
  365. const { operation } = event.data;
  366. if (operation === 'encode') {
  367. const { readableStream, writableStream, participantId } = event.data;
  368. if (!contexts.has(participantId)) {
  369. contexts.set(participantId, new Context(participantId));
  370. }
  371. const context = contexts.get(participantId);
  372. const transformStream = new TransformStream({
  373. transform: context.encodeFunction.bind(context)
  374. });
  375. readableStream
  376. .pipeThrough(new TransformStream({
  377. transform: polyFillEncodedFrameMetadata // M83 polyfill.
  378. }))
  379. .pipeThrough(transformStream)
  380. .pipeTo(writableStream);
  381. } else if (operation === 'decode') {
  382. const { readableStream, writableStream, participantId } = event.data;
  383. if (!contexts.has(participantId)) {
  384. contexts.set(participantId, new Context(participantId));
  385. }
  386. const context = contexts.get(participantId);
  387. const transformStream = new TransformStream({
  388. transform: context.decodeFunction.bind(context)
  389. });
  390. readableStream
  391. .pipeThrough(new TransformStream({
  392. transform: polyFillEncodedFrameMetadata // M83 polyfill.
  393. }))
  394. .pipeThrough(transformStream)
  395. .pipeTo(writableStream);
  396. } else if (operation === 'setKey') {
  397. const { participantId, key, keyIndex } = event.data;
  398. if (!contexts.has(participantId)) {
  399. contexts.set(participantId, new Context(participantId));
  400. }
  401. const context = contexts.get(participantId);
  402. if (key) {
  403. context.setKey(key, keyIndex);
  404. } else {
  405. context.setKey(false, keyIndex);
  406. }
  407. } else if (operation === 'cleanup') {
  408. const { participantId } = event.data;
  409. contexts.delete(participantId);
  410. } else {
  411. console.error('e2ee worker', operation);
  412. }
  413. };