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.

ManagedKeyHandler.js 7.7KB

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