modified lib-jitsi-meet dev repo
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 11KB

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