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.

ManagedKeyHandler.js 7.7KB

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