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.

E2EEncryption.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import debounce from 'lodash.debounce';
  4. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  5. import RTCEvents from '../../service/RTC/RTCEvents';
  6. import browser from '../browser';
  7. import E2EEContext from './E2EEContext';
  8. import { OlmAdapter } from './OlmAdapter';
  9. const logger = getLogger(__filename);
  10. // Period which we'll wait before updating / rotating our keys when a participant
  11. // joins or leaves.
  12. const DEBOUNCE_PERIOD = 5000;
  13. /**
  14. * This module integrates {@link E2EEContext} with {@link JitsiConference} in order to enable E2E encryption.
  15. */
  16. export class E2EEncryption {
  17. /**
  18. * A constructor.
  19. * @param {JitsiConference} conference - The conference instance for which E2E encryption is to be enabled.
  20. */
  21. constructor(conference) {
  22. this.conference = conference;
  23. this._conferenceJoined = false;
  24. this._enabled = false;
  25. this._initialized = false;
  26. this._key = undefined;
  27. this._e2eeCtx = new E2EEContext();
  28. this._olmAdapter = new OlmAdapter(conference);
  29. // Debounce key rotation / ratcheting to avoid a storm of messages.
  30. this._ratchetKey = debounce(this._ratchetKeyImpl, DEBOUNCE_PERIOD);
  31. this._rotateKey = debounce(this._rotateKeyImpl, DEBOUNCE_PERIOD);
  32. // Participant join / leave operations. Used for key advancement / rotation.
  33. //
  34. this.conference.on(
  35. JitsiConferenceEvents.USER_JOINED,
  36. this._onParticipantJoined.bind(this));
  37. this.conference.on(
  38. JitsiConferenceEvents.USER_LEFT,
  39. this._onParticipantLeft.bind(this));
  40. this.conference.on(
  41. JitsiConferenceEvents.CONFERENCE_JOINED,
  42. () => {
  43. this._conferenceJoined = true;
  44. });
  45. // Conference media events in order to attach the encryptor / decryptor.
  46. // FIXME add events to TraceablePeerConnection which will allow to see when there's new receiver or sender
  47. // added instead of shenanigans around conference track events and track muted.
  48. //
  49. this.conference.on(
  50. JitsiConferenceEvents._MEDIA_SESSION_STARTED,
  51. this._onMediaSessionStarted.bind(this));
  52. this.conference.on(
  53. JitsiConferenceEvents.TRACK_ADDED,
  54. track => track.isLocal() && this._onLocalTrackAdded(track));
  55. this.conference.rtc.on(
  56. RTCEvents.REMOTE_TRACK_ADDED,
  57. (track, tpc) => this._setupReceiverE2EEForTrack(tpc, track));
  58. this.conference.on(
  59. JitsiConferenceEvents.TRACK_MUTE_CHANGED,
  60. this._trackMuteChanged.bind(this));
  61. // Olm signalling events.
  62. this._olmAdapter.on(
  63. OlmAdapter.events.PARTICIPANT_E2EE_CHANNEL_READY,
  64. this._onParticipantE2EEChannelReady.bind(this));
  65. this._olmAdapter.on(
  66. OlmAdapter.events.PARTICIPANT_KEY_UPDATED,
  67. this._onParticipantKeyUpdated.bind(this));
  68. }
  69. /**
  70. * Indicates if E2EE is supported in the current platform.
  71. *
  72. * @param {object} config - Global configuration.
  73. * @returns {boolean}
  74. */
  75. static isSupported(config) {
  76. return browser.supportsInsertableStreams()
  77. && OlmAdapter.isSupported()
  78. && !(config.testing && config.testing.disableE2EE);
  79. }
  80. /**
  81. * Indicates whether E2EE is currently enabled or not.
  82. *
  83. * @returns {boolean}
  84. */
  85. isEnabled() {
  86. return this._enabled;
  87. }
  88. /**
  89. * Enables / disables End-To-End encryption.
  90. *
  91. * @param {boolean} enabled - whether E2EE should be enabled or not.
  92. * @returns {void}
  93. */
  94. setEnabled(enabled) {
  95. if (enabled === this._enabled) {
  96. return;
  97. }
  98. this._enabled = enabled;
  99. if (!this._initialized && enabled) {
  100. // Need to re-create the peerconnections in order to apply the insertable streams constraint.
  101. // TODO: this was necessary due to some audio issues when indertable streams are used
  102. // even though encryption is not performed. This should be fixed in the browser eventually.
  103. // https://bugs.chromium.org/p/chromium/issues/detail?id=1103280
  104. this.conference._restartMediaSessions();
  105. this._initialized = true;
  106. }
  107. // Generate a random key in case we are enabling.
  108. this._key = enabled ? this._generateKey() : false;
  109. // Send it to others using the E2EE olm channel.
  110. this._olmAdapter.updateKey(this._key).then(index => {
  111. // Set our key so we begin encrypting.
  112. this._e2eeCtx.setKey(this.conference.myUserId(), this._key, index);
  113. });
  114. }
  115. /**
  116. * Generates a new 256 bit random key.
  117. *
  118. * @returns {Uint8Array}
  119. * @private
  120. */
  121. _generateKey() {
  122. return window.crypto.getRandomValues(new Uint8Array(32));
  123. }
  124. /**
  125. * Setup E2EE on the new track that has been added to the conference, apply it on all the open peerconnections.
  126. * @param {JitsiLocalTrack} track - the new track that's being added to the conference.
  127. * @private
  128. */
  129. _onLocalTrackAdded(track) {
  130. for (const session of this.conference._getMediaSessions()) {
  131. this._setupSenderE2EEForTrack(session, track);
  132. }
  133. }
  134. /**
  135. * Setups E2E encryption for the new session.
  136. * @param {JingleSessionPC} session - the new media session.
  137. * @private
  138. */
  139. _onMediaSessionStarted(session) {
  140. const localTracks = this.conference.getLocalTracks();
  141. for (const track of localTracks) {
  142. this._setupSenderE2EEForTrack(session, track);
  143. }
  144. }
  145. /**
  146. * Advances (using ratcheting) the current key when a new participant joins the conference.
  147. * @private
  148. */
  149. _onParticipantJoined(id) {
  150. logger.debug(`Participant ${id} joined`);
  151. if (this._conferenceJoined && this._enabled) {
  152. this._ratchetKey();
  153. }
  154. }
  155. /**
  156. * Rotates the current key when a participant leaves the conference.
  157. * @private
  158. */
  159. _onParticipantLeft(id) {
  160. logger.debug(`Participant ${id} left`);
  161. this._e2eeCtx.cleanup(id);
  162. if (this._enabled) {
  163. this._rotateKey();
  164. }
  165. }
  166. /**
  167. * Event posted when the E2EE signalling channel has been established with the given participant.
  168. * @private
  169. */
  170. _onParticipantE2EEChannelReady(id) {
  171. logger.debug(`E2EE channel with participant ${id} is ready`);
  172. }
  173. /**
  174. * Handles an update in a participant's key.
  175. *
  176. * @param {string} id - The participant ID.
  177. * @param {Uint8Array | boolean} key - The new key for the participant.
  178. * @param {Number} index - The new key's index.
  179. * @private
  180. */
  181. _onParticipantKeyUpdated(id, key, index) {
  182. logger.debug(`Participant ${id} updated their key`);
  183. this._e2eeCtx.setKey(id, key, index);
  184. }
  185. /**
  186. * Advances the current key by using ratcheting.
  187. *
  188. * @private
  189. */
  190. async _ratchetKeyImpl() {
  191. logger.debug('Ratchetting key');
  192. const material = await crypto.subtle.importKey('raw', this._key, 'HKDF', false, [ 'deriveBits' ]);
  193. const newKey = await crypto.subtle.deriveBits({
  194. name: 'HKDF',
  195. salt: new TextEncoder().encode('JFrameRatchetKey'),
  196. hash: 'SHA-256',
  197. info: new ArrayBuffer()
  198. }, material, 256);
  199. this._key = new Uint8Array(newKey);
  200. const index = await this._olmAdapter.updateCurrentKey(this._key);
  201. this._e2eeCtx.setKey(this.conference.myUserId(), this._key, index);
  202. }
  203. /**
  204. * Rotates the local key. Rotating the key implies creating a new one, then distributing it
  205. * to all participants and once they all received it, start using it.
  206. *
  207. * @private
  208. */
  209. async _rotateKeyImpl() {
  210. logger.debug('Rotating key');
  211. this._key = this._generateKey();
  212. const index = await this._olmAdapter.updateKey(this._key);
  213. this._e2eeCtx.setKey(this.conference.myUserId(), this._key, index);
  214. }
  215. /**
  216. * Setup E2EE for the receiving side.
  217. *
  218. * @private
  219. */
  220. _setupReceiverE2EEForTrack(tpc, track) {
  221. if (!this._enabled) {
  222. return;
  223. }
  224. const receiver = tpc.findReceiverForTrack(track.track);
  225. if (receiver) {
  226. this._e2eeCtx.handleReceiver(receiver, track.getType(), track.getParticipantId());
  227. } else {
  228. logger.warn(`Could not handle E2EE for ${track}: receiver not found in: ${tpc}`);
  229. }
  230. }
  231. /**
  232. * Setup E2EE for the sending side.
  233. *
  234. * @param {JingleSessionPC} session - the session which sends the media produced by the track.
  235. * @param {JitsiLocalTrack} track - the local track for which e2e encoder will be configured.
  236. * @private
  237. */
  238. _setupSenderE2EEForTrack(session, track) {
  239. if (!this._enabled) {
  240. return;
  241. }
  242. const pc = session.peerconnection;
  243. const sender = pc && pc.findSenderForTrack(track.track);
  244. if (sender) {
  245. this._e2eeCtx.handleSender(sender, track.getType(), track.getParticipantId());
  246. } else {
  247. logger.warn(`Could not handle E2EE for ${track}: sender not found in ${pc}`);
  248. }
  249. }
  250. /**
  251. * Setup E2EE on the sender that is created for the unmuted local video track.
  252. * @param {JitsiLocalTrack} track - the track for which muted status has changed.
  253. * @private
  254. */
  255. _trackMuteChanged(track) {
  256. if (browser.doesVideoMuteByStreamRemove() && track.isLocal() && track.isVideoTrack() && !track.isMuted()) {
  257. for (const session of this.conference._getMediaSessions()) {
  258. this._setupSenderE2EEForTrack(session, track);
  259. }
  260. }
  261. }
  262. }