hookup megolm encryption in session

This commit is contained in:
Bruno Windels 2020-09-03 17:50:28 +02:00
parent be4d887178
commit c5c9505ce2
2 changed files with 29 additions and 13 deletions

View file

@ -23,6 +23,7 @@ import {DeviceMessageHandler} from "./DeviceMessageHandler.js";
import {Decryption as OlmDecryption} from "./e2ee/olm/Decryption.js"; import {Decryption as OlmDecryption} from "./e2ee/olm/Decryption.js";
import {Encryption as OlmEncryption} from "./e2ee/olm/Encryption.js"; import {Encryption as OlmEncryption} from "./e2ee/olm/Encryption.js";
import {Decryption as MegOlmDecryption} from "./e2ee/megolm/Decryption.js"; import {Decryption as MegOlmDecryption} from "./e2ee/megolm/Decryption.js";
import {Encryption as MegOlmEncryption} from "./e2ee/megolm/Encryption.js";
import {RoomEncryption} from "./e2ee/RoomEncryption.js"; import {RoomEncryption} from "./e2ee/RoomEncryption.js";
import {DeviceTracker} from "./e2ee/DeviceTracker.js"; import {DeviceTracker} from "./e2ee/DeviceTracker.js";
import {LockMap} from "../utils/LockMap.js"; import {LockMap} from "../utils/LockMap.js";
@ -83,11 +84,19 @@ export class Session {
olmUtil: this._olmUtil, olmUtil: this._olmUtil,
senderKeyLock senderKeyLock
}); });
this._megolmEncryption = new MegOlmEncryption({
account: this._e2eeAccount,
pickleKey: PICKLE_KEY,
olm: this._olm,
storage: this._storage,
now: this._clock.now,
ownDeviceId: this._sessionInfo.deviceId,
})
const megolmDecryption = new MegOlmDecryption({pickleKey: PICKLE_KEY, olm: this._olm}); const megolmDecryption = new MegOlmDecryption({pickleKey: PICKLE_KEY, olm: this._olm});
this._deviceMessageHandler.enableEncryption({olmDecryption, megolmDecryption}); this._deviceMessageHandler.enableEncryption({olmDecryption, megolmDecryption});
} }
_createRoomEncryption(room, encryptionEventContent) { _createRoomEncryption(room, encryptionParams) {
// TODO: this will actually happen when users start using the e2ee version for the first time // TODO: this will actually happen when users start using the e2ee version for the first time
// this should never happen because either a session was already synced once // this should never happen because either a session was already synced once
@ -103,7 +112,8 @@ export class Session {
room, room,
deviceTracker: this._deviceTracker, deviceTracker: this._deviceTracker,
olmEncryption: this._olmEncryption, olmEncryption: this._olmEncryption,
encryptionEventContent megolmEncryption: this._megolmEncryption,
encryptionParams
}); });
} }

View file

@ -17,14 +17,16 @@ limitations under the License.
import {groupBy} from "../../utils/groupBy.js"; import {groupBy} from "../../utils/groupBy.js";
import {makeTxnId} from "../common.js"; import {makeTxnId} from "../common.js";
const ENCRYPTED_TYPE = "m.room.encrypted";
export class RoomEncryption { export class RoomEncryption {
constructor({room, deviceTracker, olmEncryption, encryptionEventContent}) { constructor({room, deviceTracker, olmEncryption, megolmEncryption, encryptionParams}) {
this._room = room; this._room = room;
this._deviceTracker = deviceTracker; this._deviceTracker = deviceTracker;
this._olmEncryption = olmEncryption; this._olmEncryption = olmEncryption;
this._megolmEncryption = megolmEncryption;
// content of the m.room.encryption event // content of the m.room.encryption event
this._encryptionEventContent = encryptionEventContent; this._encryptionParams = encryptionParams;
} }
async writeMemberChanges(memberChanges, txn) { async writeMemberChanges(memberChanges, txn) {
@ -32,15 +34,19 @@ export class RoomEncryption {
} }
async encrypt(type, content, hsApi) { async encrypt(type, content, hsApi) {
await this._deviceTracker.trackRoom(this._room); const megolmResult = await this._megolmEncryption.encrypt(this._room.id, type, content, this._encryptionParams);
const devices = await this._deviceTracker.deviceIdentitiesForTrackedRoom(this._room.id, hsApi); // share the new megolm session if needed
const messages = await this._olmEncryption.encrypt("m.foo", {body: "hello at " + new Date()}, devices, hsApi); if (megolmResult.roomKeyMessage) {
await this._sendMessagesToDevices("m.room.encrypted", messages, hsApi); await this._deviceTracker.trackRoom(this._room);
return {type, content}; const devices = await this._deviceTracker.deviceIdentitiesForTrackedRoom(this._room.id, hsApi);
// return { const messages = await this._olmEncryption.encrypt(
// type: "m.room.encrypted", "m.room_key", megolmResult.roomKeyMessage, devices, hsApi);
// content: encryptedContent, await this._sendMessagesToDevices(ENCRYPTED_TYPE, messages, hsApi);
// } }
return {
type: ENCRYPTED_TYPE,
content: megolmResult.content
};
} }
async _sendMessagesToDevices(type, messages, hsApi) { async _sendMessagesToDevices(type, messages, hsApi) {