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.

Context.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* eslint-disable no-bitwise */
  2. import { deriveKeys, importKey, ratchet } from './crypto-utils';
  3. // We use a ringbuffer of keys so we can change them and still decode packets that were
  4. // encrypted with an old key. We use a size of 16 which corresponds to the four bits
  5. // in the frame trailer.
  6. const KEYRING_SIZE = 16;
  7. // We copy the first bytes of the VP8 payload unencrypted.
  8. // For keyframes this is 10 bytes, for non-keyframes (delta) 3. See
  9. // https://tools.ietf.org/html/rfc6386#section-9.1
  10. // This allows the bridge to continue detecting keyframes (only one byte needed in the JVB)
  11. // and is also a bit easier for the VP8 decoder (i.e. it generates funny garbage pictures
  12. // instead of being unable to decode).
  13. // This is a bit for show and we might want to reduce to 1 unconditionally in the final version.
  14. //
  15. // For audio (where frame.type is not set) we do not encrypt the opus TOC byte:
  16. // https://tools.ietf.org/html/rfc6716#section-3.1
  17. const UNENCRYPTED_BYTES = {
  18. key: 10,
  19. delta: 3,
  20. undefined: 1 // frame.type is not set on audio
  21. };
  22. const ENCRYPTION_ALGORITHM = 'AES-GCM';
  23. /* We use a 96 bit IV for AES GCM. This is signalled in plain together with the
  24. packet. See https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams */
  25. const IV_LENGTH = 12;
  26. const RATCHET_WINDOW_SIZE = 8;
  27. /**
  28. * Per-participant context holding the cryptographic keys and
  29. * encode/decode functions
  30. */
  31. export class Context {
  32. /**
  33. * @param {Object} options
  34. */
  35. constructor({ sharedKey = false } = {}) {
  36. // An array (ring) of keys that we use for sending and receiving.
  37. this._cryptoKeyRing = new Array(KEYRING_SIZE);
  38. // A pointer to the currently used key.
  39. this._currentKeyIndex = -1;
  40. this._sendCounts = new Map();
  41. this._sharedKey = sharedKey;
  42. this._enabled = false;
  43. }
  44. /**
  45. * Enables or disables the E2EE context. When disabled packets are passed through.
  46. * @param {boolean} enabled True if E2EE is enabled, false otherwise.
  47. */
  48. setEnabled(enabled) {
  49. this._enabled = enabled;
  50. }
  51. /**
  52. * Derives the different subkeys and starts using them for encryption or
  53. * decryption.
  54. * @param {Uint8Array|false} key bytes. Pass false to disable.
  55. * @param {Number} keyIndex
  56. */
  57. async setKey(key, keyIndex = -1) {
  58. let newKey = false;
  59. if (key) {
  60. if (this._sharedKey) {
  61. newKey = key;
  62. } else {
  63. const material = await importKey(key);
  64. newKey = await deriveKeys(material);
  65. }
  66. }
  67. this._setKeys(newKey, keyIndex);
  68. }
  69. /**
  70. * Sets a set of keys and resets the sendCount.
  71. * decryption.
  72. * @param {Object} keys set of keys.
  73. * @param {Number} keyIndex optional
  74. * @private
  75. */
  76. _setKeys(keys, keyIndex = -1) {
  77. if (keyIndex >= 0) {
  78. this._currentKeyIndex = keyIndex % this._cryptoKeyRing.length;
  79. }
  80. this._cryptoKeyRing[this._currentKeyIndex] = keys;
  81. }
  82. /**
  83. * Function that will be injected in a stream and will encrypt the given encoded frames.
  84. *
  85. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  86. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  87. *
  88. * The VP8 payload descriptor described in
  89. * https://tools.ietf.org/html/rfc7741#section-4.2
  90. * is part of the RTP packet and not part of the frame and is not controllable by us.
  91. * This is fine as the SFU keeps having access to it for routing.
  92. *
  93. * The encrypted frame is formed as follows:
  94. * 1) Leave the first (10, 3, 1) bytes unencrypted, depending on the frame type and kind.
  95. * 2) Form the GCM IV for the frame as described above.
  96. * 3) Encrypt the rest of the frame using AES-GCM.
  97. * 4) Allocate space for the encrypted frame.
  98. * 5) Copy the unencrypted bytes to the start of the encrypted frame.
  99. * 6) Append the ciphertext to the encrypted frame.
  100. * 7) Append the IV.
  101. * 8) Append a single byte for the key identifier.
  102. * 9) Enqueue the encrypted frame for sending.
  103. */
  104. encodeFunction(encodedFrame, controller) {
  105. if (!this._enabled) {
  106. return controller.enqueue(encodedFrame);
  107. }
  108. const keyIndex = this._currentKeyIndex;
  109. const currentKey = this._cryptoKeyRing[keyIndex];
  110. if (currentKey) {
  111. const iv = this._makeIV(encodedFrame.getMetadata().synchronizationSource, encodedFrame.timestamp);
  112. // This is not encrypted and contains the VP8 payload descriptor or the Opus TOC byte.
  113. const frameHeader = new Uint8Array(encodedFrame.data, 0, UNENCRYPTED_BYTES[encodedFrame.type]);
  114. // Frame trailer contains the R|IV_LENGTH and key index
  115. const frameTrailer = new Uint8Array(2);
  116. frameTrailer[0] = IV_LENGTH;
  117. frameTrailer[1] = keyIndex;
  118. // Construct frame trailer. Similar to the frame header described in
  119. // https://tools.ietf.org/html/draft-omara-sframe-00#section-4.2
  120. // but we put it at the end.
  121. //
  122. // ---------+-------------------------+-+---------+----
  123. // payload |IV...(length = IV_LENGTH)|R|IV_LENGTH|KID |
  124. // ---------+-------------------------+-+---------+----
  125. return crypto.subtle.encrypt({
  126. name: ENCRYPTION_ALGORITHM,
  127. iv,
  128. additionalData: new Uint8Array(encodedFrame.data, 0, frameHeader.byteLength)
  129. }, currentKey.encryptionKey, new Uint8Array(encodedFrame.data,
  130. UNENCRYPTED_BYTES[encodedFrame.type]))
  131. .then(cipherText => {
  132. const newData = new ArrayBuffer(frameHeader.byteLength + cipherText.byteLength
  133. + iv.byteLength + frameTrailer.byteLength);
  134. const newUint8 = new Uint8Array(newData);
  135. newUint8.set(frameHeader); // copy first bytes.
  136. newUint8.set(
  137. new Uint8Array(cipherText), frameHeader.byteLength); // add ciphertext.
  138. newUint8.set(
  139. new Uint8Array(iv), frameHeader.byteLength + cipherText.byteLength); // append IV.
  140. newUint8.set(
  141. frameTrailer,
  142. frameHeader.byteLength + cipherText.byteLength + iv.byteLength); // append frame trailer.
  143. encodedFrame.data = newData;
  144. return controller.enqueue(encodedFrame);
  145. }, e => {
  146. // TODO: surface this to the app.
  147. console.error(e);
  148. // We are not enqueuing the frame here on purpose.
  149. });
  150. }
  151. }
  152. /**
  153. * Function that will be injected in a stream and will decrypt the given encoded frames.
  154. *
  155. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  156. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  157. */
  158. async decodeFunction(encodedFrame, controller) {
  159. if (!this._enabled) {
  160. return controller.enqueue(encodedFrame);
  161. }
  162. const data = new Uint8Array(encodedFrame.data);
  163. const keyIndex = data[encodedFrame.data.byteLength - 1];
  164. if (this._cryptoKeyRing[keyIndex]) {
  165. const decodedFrame = await this._decryptFrame(
  166. encodedFrame,
  167. keyIndex);
  168. if (decodedFrame) {
  169. controller.enqueue(decodedFrame);
  170. }
  171. }
  172. }
  173. /**
  174. * Function that will decrypt the given encoded frame. If the decryption fails, it will
  175. * ratchet the key for up to RATCHET_WINDOW_SIZE times.
  176. *
  177. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  178. * @param {number} keyIndex - the index of the decryption data in _cryptoKeyRing array.
  179. * @param {number} ratchetCount - the number of retries after ratcheting the key.
  180. * @returns {Promise<RTCEncodedVideoFrame|RTCEncodedAudioFrame>} - The decrypted frame.
  181. * @private
  182. */
  183. async _decryptFrame(
  184. encodedFrame,
  185. keyIndex,
  186. initialKey = undefined,
  187. ratchetCount = 0) {
  188. const { encryptionKey } = this._cryptoKeyRing[keyIndex];
  189. let { material } = this._cryptoKeyRing[keyIndex];
  190. // Construct frame trailer. Similar to the frame header described in
  191. // https://tools.ietf.org/html/draft-omara-sframe-00#section-4.2
  192. // but we put it at the end.
  193. //
  194. // ---------+-------------------------+-+---------+----
  195. // payload |IV...(length = IV_LENGTH)|R|IV_LENGTH|KID |
  196. // ---------+-------------------------+-+---------+----
  197. try {
  198. const frameHeader = new Uint8Array(encodedFrame.data, 0, UNENCRYPTED_BYTES[encodedFrame.type]);
  199. const frameTrailer = new Uint8Array(encodedFrame.data, encodedFrame.data.byteLength - 2, 2);
  200. const ivLength = frameTrailer[0];
  201. const iv = new Uint8Array(
  202. encodedFrame.data,
  203. encodedFrame.data.byteLength - ivLength - frameTrailer.byteLength,
  204. ivLength);
  205. const cipherTextStart = frameHeader.byteLength;
  206. const cipherTextLength = encodedFrame.data.byteLength
  207. - (frameHeader.byteLength + ivLength + frameTrailer.byteLength);
  208. const plainText = await crypto.subtle.decrypt({
  209. name: 'AES-GCM',
  210. iv,
  211. additionalData: new Uint8Array(encodedFrame.data, 0, frameHeader.byteLength)
  212. },
  213. encryptionKey,
  214. new Uint8Array(encodedFrame.data, cipherTextStart, cipherTextLength));
  215. const newData = new ArrayBuffer(frameHeader.byteLength + plainText.byteLength);
  216. const newUint8 = new Uint8Array(newData);
  217. newUint8.set(new Uint8Array(encodedFrame.data, 0, frameHeader.byteLength));
  218. newUint8.set(new Uint8Array(plainText), frameHeader.byteLength);
  219. encodedFrame.data = newData;
  220. return encodedFrame;
  221. } catch (error) {
  222. if (this._sharedKey) {
  223. return;
  224. }
  225. if (ratchetCount < RATCHET_WINDOW_SIZE) {
  226. const currentKey = this._cryptoKeyRing[this._currentKeyIndex];
  227. material = await importKey(await ratchet(material));
  228. const newKey = await deriveKeys(material);
  229. this._setKeys(newKey);
  230. return await this._decryptFrame(
  231. encodedFrame,
  232. keyIndex,
  233. initialKey || currentKey,
  234. ratchetCount + 1);
  235. }
  236. /**
  237. * Since the key it is first send and only afterwards actually used for encrypting, there were
  238. * situations when the decrypting failed due to the fact that the received frame was not encrypted
  239. * yet and ratcheting, of course, did not solve the problem. So if we fail RATCHET_WINDOW_SIZE times,
  240. * we come back to the initial key.
  241. */
  242. this._setKeys(initialKey);
  243. // TODO: notify the application about error status.
  244. }
  245. }
  246. /**
  247. * Construct the IV used for AES-GCM and sent (in plain) with the packet similar to
  248. * https://tools.ietf.org/html/rfc7714#section-8.1
  249. * It concatenates
  250. * - the 32 bit synchronization source (SSRC) given on the encoded frame,
  251. * - the 32 bit rtp timestamp given on the encoded frame,
  252. * - a send counter that is specific to the SSRC. Starts at a random number.
  253. * The send counter is essentially the pictureId but we currently have to implement this ourselves.
  254. * There is no XOR with a salt. Note that this IV leaks the SSRC to the receiver but since this is
  255. * randomly generated and SFUs may not rewrite this is considered acceptable.
  256. * The SSRC is used to allow demultiplexing multiple streams with the same key, as described in
  257. * https://tools.ietf.org/html/rfc3711#section-4.1.1
  258. * The RTP timestamp is 32 bits and advances by the codec clock rate (90khz for video, 48khz for
  259. * opus audio) every second. For video it rolls over roughly every 13 hours.
  260. * The send counter will advance at the frame rate (30fps for video, 50fps for 20ms opus audio)
  261. * every second. It will take a long time to roll over.
  262. *
  263. * See also https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams
  264. */
  265. _makeIV(synchronizationSource, timestamp) {
  266. const iv = new ArrayBuffer(IV_LENGTH);
  267. const ivView = new DataView(iv);
  268. // having to keep our own send count (similar to a picture id) is not ideal.
  269. if (!this._sendCounts.has(synchronizationSource)) {
  270. // Initialize with a random offset, similar to the RTP sequence number.
  271. this._sendCounts.set(synchronizationSource, Math.floor(Math.random() * 0xFFFF));
  272. }
  273. const sendCount = this._sendCounts.get(synchronizationSource);
  274. ivView.setUint32(0, synchronizationSource);
  275. ivView.setUint32(4, timestamp);
  276. ivView.setUint32(8, sendCount % 0xFFFF);
  277. this._sendCounts.set(synchronizationSource, sendCount + 1);
  278. return iv;
  279. }
  280. }