Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

E2EEncryption.js 9.6KB

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