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

E2EEContext.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* global __filename, TransformStream */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. const logger = getLogger(__filename);
  4. // We use a ringbuffer of keys so we can change them and still decode packets that were
  5. // encrypted with an old key.
  6. // In the future when we dont rely on a globally shared key we will actually use it. For
  7. // now set the size to 1 which means there is only a single key. This causes some
  8. // glitches when changing the key but its ok.
  9. const keyRingSize = 1;
  10. // We use a 96 bit IV for AES GCM. This is signalled in plain together with the
  11. // packet. See https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams
  12. const ivLength = 12;
  13. // We copy the first bytes of the VP8 payload unencrypted.
  14. // For keyframes this is 10 bytes, for non-keyframes (delta) 3. See
  15. // https://tools.ietf.org/html/rfc6386#section-9.1
  16. // This allows the bridge to continue detecting keyframes (only one byte needed in the JVB)
  17. // and is also a bit easier for the VP8 decoder (i.e. it generates funny garbage pictures
  18. // instead of being unable to decode).
  19. // This is a bit for show and we might want to reduce to 1 unconditionally in the final version.
  20. //
  21. // For audio (where frame.type is not set) we do not encrypt the opus TOC byte:
  22. // https://tools.ietf.org/html/rfc6716#section-3.1
  23. const unencryptedBytes = {
  24. key: 10,
  25. delta: 3,
  26. undefined: 1 // frame.type is not set on audio
  27. };
  28. /**
  29. * Context encapsulating the cryptography bits required for E2EE.
  30. * This uses the WebRTC Insertable Streams API which is explained in
  31. * https://github.com/alvestrand/webrtc-media-streams/blob/master/explainer.md
  32. * that provides access to the encoded frames and allows them to be transformed.
  33. *
  34. * The encoded frame format is explained below in the _encodeFunction method.
  35. * High level design goals were:.
  36. * - do not require changes to existing SFUs and retain (VP8) metadata.
  37. * - allow the SFU to rewrite SSRCs, timestamp, pictureId.
  38. * - allow for the key to be rotated frequently.
  39. */
  40. export default class E2EEcontext {
  41. /**
  42. * Build a new E2EE context instance, which will be used in a given conference.
  43. *
  44. * @param {string} options.salt - Salt to be used for key deviation.
  45. * FIXME: We currently use the MUC room name for this which has the same lifetime
  46. * as this context. While not (pseudo)random as recommended in
  47. * https://developer.mozilla.org/en-US/docs/Web/API/Pbkdf2Params
  48. * this is easily available and the same for all participants.
  49. * We currently do not enforce a minimum length of 16 bytes either.
  50. */
  51. constructor(options) {
  52. this._options = options;
  53. // An array (ring) of keys that we use for sending and receiving.
  54. this._cryptoKeyRing = new Array(keyRingSize);
  55. // A pointer to the currently used key.
  56. this._currentKeyIndex = -1;
  57. // We keep track of how many frames we have sent per ssrc.
  58. // Starts with a random offset similar to the RTP sequence number.
  59. this._sendCounts = new Map();
  60. // Initialize the salt and convert it once.
  61. const encoder = new TextEncoder();
  62. this._salt = encoder.encode(options.salt);
  63. }
  64. /**
  65. * Handles the given {@code RTCRtpReceiver} by creating a {@code TransformStream} which will injecct
  66. * a frame decoder.
  67. *
  68. * @param {RTCRtpReceiver} receiver - The receiver which will get the decoding function injected.
  69. * @param {string} kind - The kind of track this receiver belongs to.
  70. */
  71. handleReceiver(receiver, kind) {
  72. const receiverStreams
  73. = kind === 'video' ? receiver.createEncodedVideoStreams() : receiver.createEncodedAudioStreams();
  74. const transform = new TransformStream({
  75. transform: this._decodeFunction.bind(this)
  76. });
  77. receiverStreams.readableStream
  78. .pipeThrough(transform)
  79. .pipeTo(receiverStreams.writableStream);
  80. }
  81. /**
  82. * Handles the given {@code RTCRtpSender} by creating a {@code TransformStream} which will injecct
  83. * a frame encoder.
  84. *
  85. * @param {RTCRtpSender} sender - The sender which will get the encoding funcction injected.
  86. * @param {string} kind - The kind of track this sender belongs to.
  87. */
  88. handleSender(sender, kind) {
  89. const senderStreams
  90. = kind === 'video' ? sender.createEncodedVideoStreams() : sender.createEncodedAudioStreams();
  91. const transform = new TransformStream({
  92. transform: this._encodeFunction.bind(this)
  93. });
  94. senderStreams.readableStream
  95. .pipeThrough(transform)
  96. .pipeTo(senderStreams.writableStream);
  97. }
  98. /**
  99. * Sets the key to be used for E2EE.
  100. *
  101. * @param {string} value - Value to be used as the new key. May be falsy to disable end-to-end encryption.
  102. */
  103. async setKey(value) {
  104. let key;
  105. if (value) {
  106. const encoder = new TextEncoder();
  107. key = await this._deriveKey(encoder.encode(value));
  108. } else {
  109. key = false;
  110. }
  111. this._currentKeyIndex++;
  112. this._cryptoKeyRing[this._currentKeyIndex % this._cryptoKeyRing.length] = key;
  113. }
  114. /**
  115. * Derives a AES-GCM key with 128 bits from the input using PBKDF2
  116. * The salt is configured in the constructor of this class.
  117. * @param {Uint8Array} keyBytes - Value to derive key from
  118. */
  119. async _deriveKey(keyBytes) {
  120. // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
  121. const material = await crypto.subtle.importKey('raw', keyBytes,
  122. 'PBKDF2', false, [ 'deriveBits', 'deriveKey' ]);
  123. // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#PBKDF2
  124. return crypto.subtle.deriveKey({
  125. name: 'PBKDF2',
  126. salt: this._salt,
  127. iterations: 100000,
  128. hash: 'SHA-256'
  129. }, material, {
  130. name: 'AES-GCM',
  131. length: 128
  132. }, false, [ 'encrypt', 'decrypt' ]);
  133. }
  134. /**
  135. * Construct the IV used for AES-GCM and sent (in plain) with the packet similar to
  136. * https://tools.ietf.org/html/rfc7714#section-8.1
  137. * It concatenates
  138. * - the 32 bit synchronization source (SSRC) given on the encoded frame,
  139. * - the 32 bit rtp timestamp given on the encoded frame,
  140. * - a send counter that is specific to the SSRC. Starts at a random number.
  141. * The send counter is essentially the pictureId but we currently have to implement this ourselves.
  142. * There is no XOR with a salt. Note that this IV leaks the SSRC to the receiver but since this is
  143. * randomly generated and SFUs may not rewrite this is considered acceptable.
  144. * The SSRC is used to allow demultiplexing multiple streams with the same key, as described in
  145. * https://tools.ietf.org/html/rfc3711#section-4.1.1
  146. * The RTP timestamp is 32 bits and advances by the codec clock rate (90khz for video, 48khz for
  147. * opus audio) every second. For video it rolls over roughly every 13 hours.
  148. * The send counter will advance at the frame rate (30fps for video, 50fps for 20ms opus audio)
  149. * every second. It will take a long time to roll over.
  150. *
  151. * See also https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams
  152. */
  153. _makeIV(synchronizationSource, timestamp) {
  154. const iv = new ArrayBuffer(ivLength);
  155. const ivView = new DataView(iv);
  156. // having to keep our own send count (similar to a picture id) is not ideal.
  157. if (!this._sendCounts.has(synchronizationSource)) {
  158. // Initialize with a random offset, similar to the RTP sequence number.
  159. this._sendCounts.set(synchronizationSource, Math.floor(Math.random() * 0xFFFF));
  160. }
  161. const sendCount = this._sendCounts.get(synchronizationSource);
  162. ivView.setUint32(0, synchronizationSource);
  163. ivView.setUint32(4, timestamp);
  164. ivView.setUint32(8, sendCount % 0xFFFF);
  165. this._sendCounts.set(synchronizationSource, sendCount + 1);
  166. return iv;
  167. }
  168. /**
  169. * Function that will be injected in a stream and will encrypt the given encoded frames.
  170. *
  171. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  172. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  173. *
  174. * The packet format is described below. One of the design goals was to not require
  175. * changes to the SFU which for video requires not encrypting the keyframe bit of VP8
  176. * as SFUs need to detect a keyframe (framemarking or the generic frame descriptor will
  177. * solve this eventually). This also "hides" that a client is using E2EE a bit.
  178. *
  179. * Note that this operates on the full frame, i.e. for VP8 the data described in
  180. * https://tools.ietf.org/html/rfc6386#section-9.1
  181. *
  182. * The VP8 payload descriptor described in
  183. * https://tools.ietf.org/html/rfc7741#section-4.2
  184. * is part of the RTP packet and not part of the frame and is not controllable by us.
  185. * This is fine as the SFU keeps having access to it for routing.
  186. *
  187. * The encrypted frame is formed as follows:
  188. * 1) Leave the first (10, 3, 1) bytes unencrypted, depending on the frame type and kind.
  189. * 2) Form the GCM IV for the frame as described above.
  190. * 3) Encrypt the rest of the frame using AES-GCM.
  191. * 4) Allocate space for the encrypted frame.
  192. * 5) Copy the unencrypted bytes to the start of the encrypted frame.
  193. * 6) Append the ciphertext to the encrypted frame.
  194. * 7) Append the IV.
  195. * 8) Append a single byte for the key identifier. TODO: we don't need all the bits.
  196. * 9) Enqueue the encrypted frame for sending.
  197. */
  198. _encodeFunction(encodedFrame, controller) {
  199. const keyIndex = this._currentKeyIndex % this._cryptoKeyRing.length;
  200. if (this._cryptoKeyRing[keyIndex]) {
  201. const iv = this._makeIV(encodedFrame.synchronizationSource, encodedFrame.timestamp);
  202. return crypto.subtle.encrypt({
  203. name: 'AES-GCM',
  204. iv,
  205. additionalData: new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type])
  206. }, this._cryptoKeyRing[keyIndex], new Uint8Array(encodedFrame.data, unencryptedBytes[encodedFrame.type]))
  207. .then(cipherText => {
  208. const newData = new ArrayBuffer(unencryptedBytes[encodedFrame.type] + cipherText.byteLength
  209. + iv.byteLength + 1);
  210. const newUint8 = new Uint8Array(newData);
  211. newUint8.set(
  212. new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrame.type])); // copy first bytes.
  213. newUint8.set(
  214. new Uint8Array(cipherText), unencryptedBytes[encodedFrame.type]); // add ciphertext.
  215. newUint8.set(
  216. new Uint8Array(iv), unencryptedBytes[encodedFrame.type] + cipherText.byteLength); // append IV.
  217. newUint8[unencryptedBytes[encodedFrame.type] + cipherText.byteLength + ivLength]
  218. = keyIndex; // set key index.
  219. encodedFrame.data = newData;
  220. return controller.enqueue(encodedFrame);
  221. }, e => {
  222. logger.error(e);
  223. // We are not enqueuing the frame here on purpose.
  224. });
  225. }
  226. /* NOTE WELL:
  227. * This will send unencrypted data (only protected by DTLS transport encryption) when no key is configured.
  228. * This is ok for demo purposes but should not be done once this becomes more relied upon.
  229. */
  230. controller.enqueue(encodedFrame);
  231. }
  232. /**
  233. * Function that will be injected in a stream and will decrypt the given encoded frames.
  234. *
  235. * @param {RTCEncodedVideoFrame|RTCEncodedAudioFrame} encodedFrame - Encoded video frame.
  236. * @param {TransformStreamDefaultController} controller - TransportStreamController.
  237. *
  238. * The decrypted frame is formed as follows:
  239. * 1) Extract the key index from the last byte of the encrypted frame.
  240. * If there is no key associated with the key index, the frame is enqueued for decoding
  241. * and these steps terminate.
  242. * 2) Determine the frame type in order to look up the number of unencrypted header bytes.
  243. * 2) Extract the 12-byte IV from its position near the end of the packet.
  244. * Note: the IV is treated as opaque and not reconstructed from the input.
  245. * 3) Decrypt the encrypted frame content after the unencrypted bytes using AES-GCM.
  246. * 4) Allocate space for the decrypted frame.
  247. * 5) Copy the unencrypted bytes from the start of the encrypted frame.
  248. * 6) Append the plaintext to the decrypted frame.
  249. * 7) Enqueue the decrypted frame for decoding.
  250. */
  251. _decodeFunction(encodedFrame, controller) {
  252. const data = new Uint8Array(encodedFrame.data);
  253. const keyIndex = data[encodedFrame.data.byteLength - 1];
  254. if (this._cryptoKeyRing[keyIndex]) {
  255. // TODO: use encodedFrame.type again, see https://bugs.chromium.org/p/chromium/issues/detail?id=1068468
  256. const encodedFrameType = encodedFrame.type
  257. ? (data[0] & 0x1) === 0 ? 'key' : 'delta' // eslint-disable-line no-bitwise
  258. : undefined;
  259. const iv = new Uint8Array(encodedFrame.data, encodedFrame.data.byteLength - ivLength - 1, ivLength);
  260. const cipherTextStart = unencryptedBytes[encodedFrameType];
  261. const cipherTextLength = encodedFrame.data.byteLength - (unencryptedBytes[encodedFrameType] + ivLength + 1);
  262. return crypto.subtle.decrypt({
  263. name: 'AES-GCM',
  264. iv,
  265. additionalData: new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrameType])
  266. }, this._cryptoKeyRing[keyIndex], new Uint8Array(encodedFrame.data, cipherTextStart, cipherTextLength))
  267. .then(plainText => {
  268. const newData = new ArrayBuffer(unencryptedBytes[encodedFrameType] + plainText.byteLength);
  269. const newUint8 = new Uint8Array(newData);
  270. newUint8.set(new Uint8Array(encodedFrame.data, 0, unencryptedBytes[encodedFrameType]));
  271. newUint8.set(new Uint8Array(plainText), unencryptedBytes[encodedFrameType]);
  272. encodedFrame.data = newData;
  273. return controller.enqueue(encodedFrame);
  274. }, e => {
  275. logger.error(e);
  276. // Just feed the (potentially encrypted) frame in case of error.
  277. // Worst case it is garbage.
  278. controller.enqueue(encodedFrame);
  279. });
  280. }
  281. // TODO: this just passes through to the decoder. Is that ok? If we don't know the key yet
  282. // we might want to buffer a bit but it is still unclear how to do that (and for how long etc).
  283. controller.enqueue(encodedFrame);
  284. }
  285. }