|
| 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 { IKeyTransport, KeyTransportEvents, KeyTransportEventsHandlerMap } from "./IKeyTransport.ts"; |
| 20 | +import { type Logger, logger } from "../logger.ts"; |
| 21 | +import { CallMembership } from "./CallMembership.ts"; |
| 22 | +import { EncryptionKeysToDeviceEventContent, Statistics } from "./types.ts"; |
| 23 | + |
| 24 | +export class ToDeviceKeyTransport |
| 25 | + extends TypedEventEmitter<KeyTransportEvents, KeyTransportEventsHandlerMap> |
| 26 | + implements IKeyTransport |
| 27 | +{ |
| 28 | + private readonly prefixedLogger: Logger; |
| 29 | + |
| 30 | + public constructor( |
| 31 | + private userId: string, |
| 32 | + private deviceId: string, |
| 33 | + private roomId: string, |
| 34 | + private client: Pick<MatrixClient, "encryptAndSendToDevice" | "on" | "off">, |
| 35 | + private statistics: Statistics, |
| 36 | + ) { |
| 37 | + super(); |
| 38 | + this.prefixedLogger = logger.getChild(`[RTC: ${roomId} ToDeviceKeyTransport]`); |
| 39 | + } |
| 40 | + |
| 41 | + start(): void { |
| 42 | + this.client.on(ClientEvent.ToDeviceEvent, (ev) => this.onToDeviceEvent(ev)); |
| 43 | + } |
| 44 | + |
| 45 | + stop(): void { |
| 46 | + this.client.off(ClientEvent.ToDeviceEvent, this.onToDeviceEvent); |
| 47 | + } |
| 48 | + |
| 49 | + public async sendKey(keyBase64Encoded: string, index: number, members: CallMembership[]): Promise<void> { |
| 50 | + const content: EncryptionKeysToDeviceEventContent = { |
| 51 | + keys: { |
| 52 | + index: index, |
| 53 | + key: keyBase64Encoded, |
| 54 | + }, |
| 55 | + roomId: this.roomId, |
| 56 | + member: { |
| 57 | + claimed_device_id: this.deviceId, |
| 58 | + }, |
| 59 | + session: { |
| 60 | + call_id: "", |
| 61 | + application: "m.call", |
| 62 | + scope: "m.room", |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + const targets = members |
| 67 | + .filter((member) => { |
| 68 | + // filter malformed call members |
| 69 | + if (member.sender == undefined || member.deviceId == undefined) { |
| 70 | + logger.warn(`Malformed call member: ${member.sender}|${member.deviceId}`); |
| 71 | + return false; |
| 72 | + } |
| 73 | + // Filter out me |
| 74 | + return !(member.sender == this.userId && member.deviceId == this.deviceId); |
| 75 | + }) |
| 76 | + .map((member) => { |
| 77 | + return { |
| 78 | + userId: member.sender!, |
| 79 | + deviceId: member.deviceId!, |
| 80 | + }; |
| 81 | + }); |
| 82 | + |
| 83 | + if (targets.length > 0) { |
| 84 | + await this.client.encryptAndSendToDevice(EventType.CallEncryptionKeysPrefix, targets, content); |
| 85 | + } else { |
| 86 | + this.prefixedLogger.warn("No targets found for sending key"); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + receiveCallKeyEvent(fromUser: string, content: EncryptionKeysToDeviceEventContent): void { |
| 91 | + // The event has already been validated at this point. |
| 92 | + |
| 93 | + this.statistics.counters.roomEventEncryptionKeysReceived += 1; |
| 94 | + |
| 95 | + // What is this, and why is it needed? |
| 96 | + // Also to device events do not have an origin server ts |
| 97 | + const now = Date.now(); |
| 98 | + const age = now - (typeof content.sent_ts === "number" ? content.sent_ts : now); |
| 99 | + this.statistics.totals.roomEventEncryptionKeysReceivedTotalAge += age; |
| 100 | + |
| 101 | + this.emit( |
| 102 | + KeyTransportEvents.ReceivedKeys, |
| 103 | + // TODO this is claimed information |
| 104 | + fromUser, |
| 105 | + // TODO: This is claimed information |
| 106 | + content.member.claimed_device_id!, |
| 107 | + content.keys.key, |
| 108 | + content.keys.index, |
| 109 | + age, |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + private onToDeviceEvent = (event: MatrixEvent): void => { |
| 114 | + if (event.getType() !== EventType.CallEncryptionKeysPrefix) { |
| 115 | + // Ignore this is not a call encryption event |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + // TODO: Not possible to check if the event is encrypted or not |
| 120 | + // see https://github.com/matrix-org/matrix-rust-sdk/issues/4883 |
| 121 | + // if (evnt.getWireType() != EventType.RoomMessageEncrypted) { |
| 122 | + // // WARN: The call keys were sent in clear. Ignore them |
| 123 | + // logger.warn(`Call encryption keys sent in clear from: ${event.getSender()}`); |
| 124 | + // return; |
| 125 | + // } |
| 126 | + |
| 127 | + const content = this.getValidEventContent(event); |
| 128 | + if (!content) return; |
| 129 | + |
| 130 | + if (!event.getSender()) return; |
| 131 | + |
| 132 | + this.receiveCallKeyEvent(event.getSender()!, content); |
| 133 | + }; |
| 134 | + |
| 135 | + private getValidEventContent(event: MatrixEvent): EncryptionKeysToDeviceEventContent | undefined { |
| 136 | + const content = event.getContent<EncryptionKeysToDeviceEventContent>(); |
| 137 | + const roomId = content.roomId; |
| 138 | + if (!roomId) { |
| 139 | + // Invalid event |
| 140 | + this.prefixedLogger.warn("Malformed Event: invalid call encryption keys event, no roomId"); |
| 141 | + return; |
| 142 | + } |
| 143 | + if (roomId !== this.roomId) { |
| 144 | + this.prefixedLogger.warn("Malformed Event: Mismatch roomId"); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + if (!content.keys || !content.keys.key || !content.keys.index) { |
| 149 | + this.prefixedLogger.warn("Malformed Event: Missing keys field"); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + if (!content.member || !content.member.claimed_device_id) { |
| 154 | + this.prefixedLogger.warn("Malformed Event: Missing claimed_device_id"); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + // TODO session is not used so far |
| 159 | + // if (!content.session || !content.session.call_id || !content.session.scope || !content.session.application) { |
| 160 | + // this.prefixedLogger.warn("Malformed Event: Missing/Malformed content.session", content.session); |
| 161 | + // return; |
| 162 | + // } |
| 163 | + return content; |
| 164 | + } |
| 165 | +} |
0 commit comments