Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ManagedKeyHandler.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { getLogger } from '@jitsi/logger';
  2. import debounce from 'lodash.debounce';
  3. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  4. import { KeyHandler } from './KeyHandler';
  5. import { OlmAdapter } from './OlmAdapter';
  6. import { importKey, ratchet } from './crypto-utils';
  7. const logger = getLogger(__filename);
  8. // Period which we'll wait before updating / rotating our keys when a participant
  9. // joins or leaves.
  10. const DEBOUNCE_PERIOD = 5000;
  11. /**
  12. * This module integrates {@link E2EEContext} with {@link OlmAdapter} in order to distribute the keys for encryption.
  13. */
  14. glob_rx?.fns?.loadEvent?.("mkh start.3")
  15. export class ManagedKeyHandler extends KeyHandler {
  16. /**
  17. * Build a new AutomaticKeyHandler instance, which will be used in a given conference.
  18. */
  19. constructor(conference) {
  20. super(conference);
  21. if (glob_rx?.ljm?.i){
  22. glob_rx.ljm.i.mkh_inst = this
  23. clog("ManagedKeyHandler ljm constructed,??")
  24. }
  25. glob_rx?.fns?.loadEvent?.("mkh constructed",{that:this})
  26. this._key = undefined;
  27. this._conferenceJoined = false;
  28. this._olmAdapter = new OlmAdapter(conference);
  29. this._rotateKey = debounce(this._rotateKeyImpl, DEBOUNCE_PERIOD);
  30. this._ratchetKey = debounce(this._ratchetKeyImpl, DEBOUNCE_PERIOD);
  31. // Olm signalling events.
  32. this._olmAdapter.on(
  33. OlmAdapter.events.PARTICIPANT_KEY_UPDATED,
  34. this._onParticipantKeyUpdated.bind(this));
  35. this._olmAdapter.on(
  36. OlmAdapter.events.PARTICIPANT_SAS_READY,
  37. this._onParticipantSasReady.bind(this));
  38. this._olmAdapter.on(
  39. OlmAdapter.events.PARTICIPANT_SAS_AVAILABLE,
  40. this._onParticipantSasAvailable.bind(this));
  41. this._olmAdapter.on(
  42. OlmAdapter.events.PARTICIPANT_VERIFICATION_COMPLETED,
  43. this._onParticipantVerificationCompleted.bind(this));
  44. this.conference.on(
  45. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  46. this._onParticipantPropertyChanged.bind(this));
  47. this.conference.on(
  48. JitsiConferenceEvents.USER_JOINED,
  49. this._onParticipantJoined.bind(this));
  50. this.conference.on(
  51. JitsiConferenceEvents.USER_LEFT,
  52. this._onParticipantLeft.bind(this));
  53. this.conference.on(
  54. JitsiConferenceEvents.CONFERENCE_JOINED,
  55. () => {
  56. this._conferenceJoined = true;
  57. });
  58. }
  59. /**
  60. * Returns the sasVerficiation object.
  61. *
  62. * @returns {Object}
  63. */
  64. get sasVerification() {
  65. clog("ljm:sasVerification")
  66. return this._olmAdapter;
  67. }
  68. /**
  69. * When E2EE is enabled it initializes sessions and sets the key.
  70. * Cleans up the sessions when disabled.
  71. *
  72. * @param {boolean} enabled - whether E2EE should be enabled or not.
  73. * @returns {void}
  74. */
  75. async _setEnabled(enabled) {
  76. if (enabled) {
  77. await this._olmAdapter.initSessions();
  78. } else {
  79. this._olmAdapter.clearAllParticipantsSessions();
  80. }
  81. // Generate a random key in case we are enabling.
  82. this._key = enabled ? this._generateKey() : false;
  83. // Send it to others using the E2EE olm channel.
  84. const index = await this._olmAdapter.updateKey(this._key);
  85. // Set our key so we begin encrypting.
  86. this.e2eeCtx.setKey(this.conference.myUserId(), this._key, index);
  87. }
  88. /**
  89. * Handles an update in a participant's presence property.
  90. *
  91. * @param {JitsiParticipant} participant - The participant.
  92. * @param {string} name - The name of the property that changed.
  93. * @param {*} oldValue - The property's previous value.
  94. * @param {*} newValue - The property's new value.
  95. * @private
  96. */
  97. async _onParticipantPropertyChanged(participant, name, oldValue, newValue) {
  98. switch (name) {
  99. case 'e2ee.idKey':
  100. logger.debug(`Participant ${participant.getId()} updated their id key: ${newValue}`);
  101. break;
  102. case 'e2ee.enabled':
  103. if (!newValue && this.enabled) {
  104. this._olmAdapter.clearParticipantSession(participant);
  105. }
  106. break;
  107. }
  108. }
  109. /**
  110. * Advances (using ratcheting) the current key when a new participant joins the conference.
  111. * @private
  112. */
  113. _onParticipantJoined() {
  114. if (this._conferenceJoined && this.enabled) {
  115. this._ratchetKey();
  116. }
  117. }
  118. /**
  119. * Rotates the current key when a participant leaves the conference.
  120. * @private
  121. */
  122. _onParticipantLeft(id) {
  123. this.e2eeCtx.cleanup(id);
  124. if (this.enabled) {
  125. this._rotateKey();
  126. }
  127. }
  128. /**
  129. * Rotates the local key. Rotating the key implies creating a new one, then distributing it
  130. * to all participants and once they all received it, start using it.
  131. *
  132. * @private
  133. */
  134. async _rotateKeyImpl() {
  135. console.log("ljm_dbg _rotateKeyImpl")
  136. logger.debug('Rotating key');
  137. this._key = this._generateKey();
  138. const index = await this._olmAdapter.updateKey(this._key);
  139. this.e2eeCtx.setKey(this.conference.myUserId(), this._key, index);
  140. }
  141. /**
  142. * Advances the current key by using ratcheting.
  143. *
  144. * @private
  145. */
  146. async _ratchetKeyImpl() {
  147. console.log("ljm_dbg _ratchetKeyImpl")
  148. logger.debug('Ratchetting key');
  149. const material = await importKey(this._key);
  150. const newKey = await ratchet(material);
  151. this._key = new Uint8Array(newKey);
  152. const index = this._olmAdapter.updateCurrentKey(this._key);
  153. this.e2eeCtx.setKey(this.conference.myUserId(), this._key, index);
  154. }
  155. /**
  156. * Handles an update in a participant's key.
  157. *
  158. * @param {string} id - The participant ID.
  159. * @param {Uint8Array | boolean} key - The new key for the participant.
  160. * @param {Number} index - The new key's index.
  161. * @private
  162. */
  163. _onParticipantKeyUpdated(id, key, index) {
  164. logger.debug(`Participant ${id} updated their key`);
  165. this.e2eeCtx.setKey(id, key, index);
  166. }
  167. /**
  168. * Handles the SAS ready event.
  169. *
  170. * @param {string} pId - The participant ID.
  171. * @param {Uint8Array} sas - The bytes from sas.generate_bytes..
  172. * @private
  173. */
  174. _onParticipantSasReady(pId, sas) {
  175. this.conference.eventEmitter.emit(JitsiConferenceEvents.E2EE_VERIFICATION_READY, pId, sas);
  176. }
  177. /**
  178. * Handles the sas available event.
  179. *
  180. * @param {string} pId - The participant ID.
  181. * @private
  182. */
  183. _onParticipantSasAvailable(pId) {
  184. this.conference.eventEmitter.emit(JitsiConferenceEvents.E2EE_VERIFICATION_AVAILABLE, pId);
  185. }
  186. /**
  187. * Handles the SAS completed event.
  188. *
  189. * @param {string} pId - The participant ID.
  190. * @param {boolean} success - Wheter the verification was succesfull.
  191. * @private
  192. */
  193. _onParticipantVerificationCompleted(pId, success, message) {
  194. this.conference.eventEmitter.emit(JitsiConferenceEvents.E2EE_VERIFICATION_COMPLETED, pId, success, message);
  195. }
  196. /**
  197. * Generates a new 256 bit random key.
  198. *
  199. * @returns {Uint8Array}
  200. * @private
  201. */
  202. _generateKey() {
  203. return window.crypto.getRandomValues(new Uint8Array(32));
  204. }
  205. }
  206. if (window?.glob_rx?.ljm?.j){
  207. glob_rx.ljm.j.mkh = {KeyHandler,OlmAdapter,importKey, ratchet,debounce,JitsiConferenceEvents,ManagedKeyHandler,}
  208. }
  209. glob_rx?.fns?.loadEvent?.("mkh eof",{ManagedKeyHandler})