Skip to content

Commit

Permalink
Handle other members having no e2e keys (#2383)
Browse files Browse the repository at this point in the history
Fetch the device info once at the start of the cal and cache it
rather than fetching every time, and throw if we're supposed to be
using e2e but the other end has no e2e keys.
  • Loading branch information
dbkr committed May 19, 2022
1 parent 942a28d commit aa0d3bd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
23 changes: 20 additions & 3 deletions src/webrtc/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { CallFeed } from './callFeed';
import { MatrixClient } from "../client";
import { ISendEventResponse } from "../@types/requests";
import { EventEmitterEvents, TypedEventEmitter } from "../models/typed-event-emitter";
import { DeviceInfo } from '../crypto/deviceinfo';

// events: hangup, error(err), replaced(call), state(state, oldState)

Expand Down Expand Up @@ -343,6 +344,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
private callLength = 0;

private opponentDeviceId: string;
private opponentDeviceInfo: DeviceInfo;
private opponentSessionId: string;
public groupCallId: string;

Expand Down Expand Up @@ -508,6 +510,17 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
return this.feeds.filter((feed) => !feed.isLocal());
}

private async initOpponentCrypto() {
if (!this.opponentDeviceId) return;

const userId = this.invitee || this.getOpponentMember().userId;
const deviceInfoMap = await this.client.crypto.deviceList.downloadKeys([userId], false);
this.opponentDeviceInfo = deviceInfoMap[userId][this.opponentDeviceId];
if (this.opponentDeviceInfo === undefined) {
throw new Error(`No keys found for opponent device ${this.opponentDeviceId}!`);
}
}

/**
* Generates and returns localSDPStreamMetadata
* @returns {SDPStreamMetadata} localSDPStreamMetadata
Expand Down Expand Up @@ -792,6 +805,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
// handler will start giving us more call events (eg. candidates) so if
// we haven't set the party ID, we'll ignore them.
this.chooseOpponent(event);
await this.initOpponentCrypto();
try {
await this.peerConn.setRemoteDescription(invite.offer);
await this.addBufferedIceCandidates();
Expand Down Expand Up @@ -2052,9 +2066,10 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
},
};
const userId = this.invitee || this.getOpponentMember().userId;
const deviceInfoMap = await this.client.crypto.deviceList.downloadKeys([userId], false);
const deviceInfo = deviceInfoMap[userId][this.opponentDeviceId];
return this.client.crypto.encryptAndSendToDevices([{ userId, deviceInfo }], payload);
return this.client.crypto.encryptAndSendToDevices([{
userId,
deviceInfo: this.opponentDeviceInfo,
}], payload);
} else {
this.emit(CallEvent.SendVoipEvent, {
type: "sendEvent",
Expand Down Expand Up @@ -2351,6 +2366,8 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
this.checkForErrorListener();
this.direction = CallDirection.Outbound;

await this.initOpponentCrypto();

// XXX Find a better way to do this
this.client.callEventHandler.calls.set(this.callId, this);

Expand Down
24 changes: 20 additions & 4 deletions src/webrtc/groupCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export type GroupCallEventHandlerMap = {

export enum GroupCallErrorCode {
NoUserMedia = "no_user_media",
UnknownDevice = "unknown_device"
UnknownDevice = "unknown_device",
PlaceCallFailed = "place_call_failed"
}

export class GroupCallError extends Error {
Expand Down Expand Up @@ -653,7 +654,7 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
return this.client.sendStateEvent(this.room.roomId, EventType.GroupCallMemberPrefix, content, localUserId);
}

public onMemberStateChanged = (event: MatrixEvent) => {
public onMemberStateChanged = async (event: MatrixEvent) => {
// The member events may be received for another room, which we will ignore.
if (event.getRoomId() !== this.room.roomId) {
return;
Expand Down Expand Up @@ -751,8 +752,23 @@ export class GroupCall extends TypedEventEmitter<GroupCallEvent, GroupCallEventH
const requestScreenshareFeed = opponentDevice.feeds.some(
(feed) => feed.purpose === SDPStreamMetadataPurpose.Screenshare);

// Safari can't send a MediaStream to multiple sources, so clone it
newCall.placeCallWithCallFeeds(this.getLocalFeeds().map(feed => feed.clone()), requestScreenshareFeed);
try {
// Safari can't send a MediaStream to multiple sources, so clone it
await newCall.placeCallWithCallFeeds(
this.getLocalFeeds().map(feed => feed.clone()),
requestScreenshareFeed,
);
} catch (e) {
logger.warn(`Failed to place call to ${member.userId}!`, e);
this.emit(
GroupCallEvent.Error,
new GroupCallError(
GroupCallErrorCode.PlaceCallFailed,
`Failed to place call to ${member.userId}.`,
),
);
return;
}

if (this.dataChannelsEnabled) {
newCall.createDataChannel("datachannel", this.dataChannelOptions);
Expand Down

0 comments on commit aa0d3bd

Please sign in to comment.