encrypt call signalling message only for given device

This commit is contained in:
Bruno Windels 2022-06-01 15:28:49 +02:00
parent 6f0ebeacb7
commit 50ae51e893
3 changed files with 23 additions and 23 deletions

View File

@ -79,18 +79,18 @@ export class Session {
this._callHandler = new CallHandler({ this._callHandler = new CallHandler({
clock: this._platform.clock, clock: this._platform.clock,
hsApi: this._hsApi, hsApi: this._hsApi,
encryptDeviceMessage: async (roomId, userId, message, log) => { encryptDeviceMessage: async (roomId, userId, deviceId, message, log) => {
if (!this._deviceTracker || !this._olmEncryption) { if (!this._deviceTracker || !this._olmEncryption) {
throw new Error("encryption is not enabled"); throw new Error("encryption is not enabled");
} }
// TODO: just get the devices we're sending the message to, not all the room devices const device = await log.wrap("get device key", async log => {
// although we probably already fetched all devices to send messages in the likely e2ee room return this._deviceTracker.deviceForId(userId, deviceId, this._hsApi, log);
const devices = await log.wrap("get device keys", async log => {
await this._deviceTracker.trackRoom(this.rooms.get(roomId), log);
return this._deviceTracker.devicesForRoomMembers(roomId, [userId], this._hsApi, log);
}); });
const encryptedMessage = await this._olmEncryption.encrypt(message.type, message.content, devices, this._hsApi, log); if (!device) {
return encryptedMessage; throw new Error(`Could not find device key ${deviceId} for ${userId} in ${roomId}`);
}
const encryptedMessages = await this._olmEncryption.encrypt(message.type, message.content, [device], this._hsApi, log);
return encryptedMessages;
}, },
storage: this._storage, storage: this._storage,
webRTC: this._platform.webRTC, webRTC: this._platform.webRTC,

View File

@ -55,7 +55,7 @@ function getDeviceFromMemberKey(key: string): string {
export type Options = Omit<MemberOptions, "emitUpdate" | "confId" | "encryptDeviceMessage"> & { export type Options = Omit<MemberOptions, "emitUpdate" | "confId" | "encryptDeviceMessage"> & {
emitUpdate: (call: GroupCall, params?: any) => void; emitUpdate: (call: GroupCall, params?: any) => void;
encryptDeviceMessage: (roomId: string, userId: string, message: SignallingMessage<MGroupCallBase>, log: ILogItem) => Promise<EncryptedMessage>, encryptDeviceMessage: (roomId: string, userId: string, deviceId: string, message: SignallingMessage<MGroupCallBase>, log: ILogItem) => Promise<EncryptedMessage>,
storage: Storage, storage: Storage,
logger: ILogger, logger: ILogger,
}; };
@ -93,8 +93,8 @@ export class GroupCall extends EventEmitter<{change: never}> {
this._memberOptions = Object.assign({}, options, { this._memberOptions = Object.assign({}, options, {
confId: this.id, confId: this.id,
emitUpdate: member => this._members.update(getMemberKey(member.userId, member.deviceId), member), emitUpdate: member => this._members.update(getMemberKey(member.userId, member.deviceId), member),
encryptDeviceMessage: (userId: string, message: SignallingMessage<MGroupCallBase>, log) => { encryptDeviceMessage: (userId: string, deviceId: string, message: SignallingMessage<MGroupCallBase>, log) => {
return this.options.encryptDeviceMessage(this.roomId, userId, message, log); return this.options.encryptDeviceMessage(this.roomId, userId, deviceId, message, log);
} }
}); });
} }

View File

@ -36,7 +36,7 @@ export type Options = Omit<PeerCallOptions, "emitUpdate" | "sendSignallingMessag
// local session id of our client // local session id of our client
sessionId: string, sessionId: string,
hsApi: HomeServerApi, hsApi: HomeServerApi,
encryptDeviceMessage: (userId: string, message: SignallingMessage<MGroupCallBase>, log: ILogItem) => Promise<EncryptedMessage>, encryptDeviceMessage: (userId: string, deviceId: string, message: SignallingMessage<MGroupCallBase>, log: ILogItem) => Promise<EncryptedMessage>,
emitUpdate: (participant: Member, params?: any) => void, emitUpdate: (participant: Member, params?: any) => void,
} }
@ -217,20 +217,20 @@ export class Member {
groupMessage.content.party_id = this.options.ownDeviceId; groupMessage.content.party_id = this.options.ownDeviceId;
groupMessage.content.sender_session_id = this.options.sessionId; groupMessage.content.sender_session_id = this.options.sessionId;
groupMessage.content.dest_session_id = this.sessionId; groupMessage.content.dest_session_id = this.sessionId;
// const encryptedMessages = await this.options.encryptDeviceMessage(this.member.userId, groupMessage, log); let payload;
// const payload = formatToDeviceMessagesPayload(encryptedMessages); let type: string = message.type;
const payload = { const encryptedMessages = await this.options.encryptDeviceMessage(this.member.userId, this.deviceId, groupMessage, log);
messages: { if (encryptedMessages) {
[this.member.userId]: { payload = formatToDeviceMessagesPayload(encryptedMessages);
[this.deviceId]: groupMessage.content type = "m.room.encrypted";
} else {
// device needs deviceId and userId
payload = formatToDeviceMessagesPayload([{content: groupMessage.content, device: this}]);
} }
}
};
// TODO: remove this for release // TODO: remove this for release
log.set("payload", groupMessage.content); log.set("payload", groupMessage.content);
const request = this.options.hsApi.sendToDevice( const request = this.options.hsApi.sendToDevice(
message.type, type,
//"m.room.encrypted",
payload, payload,
makeTxnId(), makeTxnId(),
{log} {log}