|
| 1 | +/* |
| 2 | +Copyright 2025 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import {ClientEvent, EventType, type MatrixClient, MatrixEvent } from "../matrix.ts"; |
| 18 | +import { TypedEventEmitter } from "../models/typed-event-emitter.ts"; |
| 19 | +import {Statistics} from "./EncryptionManager.ts"; |
| 20 | +import {IKeyTransport, KeyTransportEvents, KeyTransportEventsHandlerMap} from "./IKeyTransport.ts"; |
| 21 | +import {type Logger, logger} from "../logger.ts"; |
| 22 | +import {CallMembership} from "./CallMembership.ts"; |
| 23 | + |
| 24 | +export type ParticipantId = string |
| 25 | + |
| 26 | +export interface EncryptionKeysToDeviceEventContent { |
| 27 | + keys: { index: number; key: string }; |
| 28 | + // member: { |
| 29 | + // id: ParticipantId, |
| 30 | + // device_id: string, |
| 31 | + // user_id: string |
| 32 | + // }; |
| 33 | + roomId: string; |
| 34 | + session: { |
| 35 | + application: string, |
| 36 | + call_id: string, |
| 37 | + scope: string |
| 38 | + }, |
| 39 | +} |
| 40 | + |
| 41 | +export class ToDeviceKeyTransport extends TypedEventEmitter<KeyTransportEvents, KeyTransportEventsHandlerMap> |
| 42 | + implements IKeyTransport { |
| 43 | + private readonly prefixedLogger: Logger; |
| 44 | + |
| 45 | + public constructor( |
| 46 | + private userId: string, |
| 47 | + private deviceId: string, |
| 48 | + private roomId: string, |
| 49 | + private client: Pick<MatrixClient, "encryptAndSendToDevice" | "on" | "off">, |
| 50 | + // private statistics: Statistics, |
| 51 | + ) { |
| 52 | + super(); |
| 53 | + this.prefixedLogger = logger.getChild(`[RTC: ${roomId} ToDeviceKeyTransport]`); |
| 54 | + } |
| 55 | + |
| 56 | + start(): void { |
| 57 | + this.client.on(ClientEvent.ToDeviceEvent, this.onToDeviceEvent); |
| 58 | + } |
| 59 | + |
| 60 | + stop(): void { |
| 61 | + this.client.off(ClientEvent.ToDeviceEvent, this.onToDeviceEvent); |
| 62 | + } |
| 63 | + |
| 64 | + public async sendKey(keyBase64Encoded: string, index: number, members: CallMembership[]): Promise<void> { |
| 65 | + const content: EncryptionKeysToDeviceEventContent = { |
| 66 | + keys: { |
| 67 | + index: index, |
| 68 | + key: keyBase64Encoded, |
| 69 | + }, |
| 70 | + roomId: this.roomId, |
| 71 | + // member: { |
| 72 | + // id: getParticipantId(this.userId, this.deviceId), |
| 73 | + // // device_id: this.deviceId, |
| 74 | + // // user_id: this.userId |
| 75 | + // }, |
| 76 | + session: { |
| 77 | + call_id: "", |
| 78 | + application: "m.call", |
| 79 | + scope: "m.room", |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | + const targets = members.filter(member => { |
| 84 | + // filter malformed call members |
| 85 | + if (member.sender == undefined || member.deviceId == undefined) { |
| 86 | + logger.warn(`Malformed call member: ${member.sender}|${member.deviceId}`); |
| 87 | + return false; |
| 88 | + } |
| 89 | + // Filter out me |
| 90 | + return !(member.sender == this.userId && member.deviceId == this.deviceId); |
| 91 | + }) |
| 92 | + .map(member => { |
| 93 | + return { |
| 94 | + userId: member.sender!, |
| 95 | + deviceId: member.deviceId!, |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + if (targets.length > 0) { |
| 100 | + await this.client.encryptAndSendToDevice(EventType.CallEncryptionKeysPrefix, targets, content); |
| 101 | + } else { |
| 102 | + this.prefixedLogger.warn("No targets found for sending key"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + receiveRoomEvent(event: MatrixEvent /**, statistics: Statistics*/): void { |
| 107 | + // The event has already been validated at this point. |
| 108 | + const content = event.getContent<EncryptionKeysToDeviceEventContent>(); |
| 109 | + |
| 110 | + // statistics.counters.roomEventEncryptionKeysReceived += 1; |
| 111 | + |
| 112 | + // WTF is this? |
| 113 | + // const age = Date.now() - (typeof content.sent_ts === "number" ? content.sent_ts : event.getTs()); |
| 114 | + // statistics.totals.roomEventEncryptionKeysReceivedTotalAge += age; |
| 115 | + |
| 116 | + this.emit( |
| 117 | + KeyTransportEvents.ReceivedKeys, |
| 118 | + event.getSender()!, |
| 119 | + // TODO: Check that this is correctly passed from the to device and is not claimed info |
| 120 | + (event.event as any)["sender_device"]!, |
| 121 | + content.keys.key, |
| 122 | + content.keys.index, |
| 123 | + event.getTs(), |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + private onToDeviceEvent = (event: MatrixEvent): void => { |
| 129 | + if (event.getType() !== EventType.CallEncryptionKeysPrefix) { |
| 130 | + // Ignore this is not a call encryption event |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + // if (event.getWireType() != EventType.RoomMessageEncrypted) { |
| 135 | + // // WARN: The call keys were sent in clear. Ignore them |
| 136 | + // logger.warn(`Call encryption keys sent in clear from: ${event.getSender()}`); |
| 137 | + // return; |
| 138 | + // } |
| 139 | + |
| 140 | + const content = event.getContent<EncryptionKeysToDeviceEventContent>(); |
| 141 | + const roomId = content.roomId; |
| 142 | + if (!roomId) { |
| 143 | + // Invalid event |
| 144 | + logger.warn("onToDeviceEvent: invalid call encryption keys event, no roomId"); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + if (roomId !== this.roomId) { |
| 149 | + return; |
| 150 | + } |
| 151 | + // const rtcSession = this.roomSessions.get(roomId); |
| 152 | + // if (!rtcSession) { |
| 153 | + // logger.warn(`onToDeviceEvent: call encryption keys event for unknown session for room ${roomId}`); |
| 154 | + // return; |
| 155 | + // } |
| 156 | + |
| 157 | + this.receiveRoomEvent(event |
| 158 | + // , this.statistics |
| 159 | + ); |
| 160 | + }; |
| 161 | +} |
0 commit comments