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

E2EEncryption.js 9.4KB

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