Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MatrixRTC key distribution using to-device messaging #4452

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ca07b18
Add CryptoApi. encryptToDeviceMessages
hughns Aug 31, 2024
6a0d8e2
Overload MatrixClient. encryptAndSendToDevices instead of deprecating
hughns Sep 2, 2024
354f5b9
Revert "Overload MatrixClient. encryptAndSendToDevices instead of dep…
hughns Sep 2, 2024
28456ce
Feedback from code review
hughns Sep 2, 2024
94d68e8
Use temporary pre-release build of @matrix-org/matrix-sdk-crypto-wasm
hughns Sep 3, 2024
3266a69
Deduplicate user IDs
hughns Sep 4, 2024
8afcc05
Test for RustCrypto implementation
hughns Sep 4, 2024
7bb6a1b
Use ensureSessionsForUsers()
hughns Sep 4, 2024
687ce0d
Encrypt to-device messages in parallel
hughns Sep 6, 2024
5e978b9
Use release version of matrix-sdk-crypto-wasm
hughns Sep 6, 2024
e8bc1f5
Merge branch 'develop' into hughns/rust-send-to-device
hughns Sep 9, 2024
0253e5d
Basic implementation of unencrypted to-device MatrixRTC sender key di…
hughns Aug 16, 2024
084685d
Use encrypted to-device events
hughns Aug 16, 2024
362bf2a
Workaround for support of widget API
hughns Aug 19, 2024
c87a8a3
Add note about missing support for Rust Crypto
hughns Aug 19, 2024
752112d
Capture key send and receive stats
hughns Aug 23, 2024
65c295b
Add CryptoApi. encryptToDeviceMessages
hughns Aug 31, 2024
aa6f63d
Add embedded. sendToDeviceViaWidgetApi
hughns Sep 2, 2024
6525ef7
Use RoomWidgetClient.sendToDeviceViaWidgetApi() when running embedded
hughns Sep 3, 2024
03b7a6e
Fix error handling and payload contents
hughns Sep 4, 2024
92beeab
Log age of keys
hughns Sep 4, 2024
9f12284
Only send the latest key via to-device message
hughns Sep 4, 2024
5a6012b
Send correct key on rotation
hughns Sep 4, 2024
faa1987
Don't incorrectly track other people's key index as our own
hughns Sep 4, 2024
f1974b2
Use custom build of matrix-sdk-crypto-wasm to workaround breaking cha…
hughns Sep 9, 2024
b0174ec
FIx NPE
hughns Sep 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
],
"dependencies": {
"@babel/runtime": "^7.12.5",
"@matrix-org/matrix-sdk-crypto-wasm": "^7.0.0",
"@matrix-org/matrix-sdk-crypto-wasm": "https://floofy.netlify.app/matrix-org-matrix-sdk-crypto-wasm-v7.0.0-to-device.tgz",
"@matrix-org/olm": "3.2.15",
"another-json": "^0.2.0",
"bs58": "^6.0.0",
Expand Down
118 changes: 118 additions & 0 deletions spec/unit/rust-crypto/rust-crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,124 @@ describe("RustCrypto", () => {
await expect(rustCrypto.exportSecretsBundle()).resolves.toEqual(expect.objectContaining(bundle));
});
});

describe("encryptToDeviceMessages", () => {
let rustCrypto: RustCrypto;
let testOlmMachine: RustSdkCryptoJs.OlmMachine;

beforeEach(async () => {
testOlmMachine = await OlmMachine.initialize(
new RustSdkCryptoJs.UserId(testData.TEST_USER_ID),
new RustSdkCryptoJs.DeviceId(testData.TEST_DEVICE_ID),
);
jest.spyOn(OlmMachine, "initFromStore").mockResolvedValue(testOlmMachine);
rustCrypto = await makeTestRustCrypto();
expect(OlmMachine.initFromStore).toHaveBeenCalled();
});

afterEach(() => {
testOlmMachine?.free();
});

const payload = { hello: "world" };

it("returns empty batch if devices not known", async () => {
const getMissingSessions = jest.spyOn(testOlmMachine, "getMissingSessions");
const getDevice = jest.spyOn(testOlmMachine, "getDevice");
const batch = await rustCrypto.encryptToDeviceMessages(
"m.test.type",
[
{ deviceId: "AAA", userId: "@user1:domain" },
{ deviceId: "BBB", userId: "@user1:domain" },
{ deviceId: "CCC", userId: "@user2:domain" },
],
payload,
);
expect(getMissingSessions.mock.calls[0][0].length).toBe(2);
expect(getDevice).toHaveBeenCalledTimes(3);
expect(batch?.eventType).toEqual("m.room.encrypted");
expect(batch?.batch).toEqual([]);
}, 120000);

it("returns encrypted batch for known devices", async () => {
// Make m aware of another device, and get some OTK to be able to establish a session.
await testOlmMachine.markRequestAsSent(
"foo",
RustSdkCryptoJs.RequestType.KeysQuery,
JSON.stringify({
device_keys: {
"@example:localhost": {
AFGUOBTZWM: {
algorithms: ["m.olm.v1.curve25519-aes-sha2", "m.megolm.v1.aes-sha2"],
device_id: "AFGUOBTZWM",
keys: {
"curve25519:AFGUOBTZWM": "boYjDpaC+7NkECQEeMh5dC+I1+AfriX0VXG2UV7EUQo",
"ed25519:AFGUOBTZWM": "NayrMQ33ObqMRqz6R9GosmHdT6HQ6b/RX/3QlZ2yiec",
},
signatures: {
"@example:localhost": {
"ed25519:AFGUOBTZWM":
"RoSWvru1jj6fs2arnTedWsyIyBmKHMdOu7r9gDi0BZ61h9SbCK2zLXzuJ9ZFLao2VvA0yEd7CASCmDHDLYpXCA",
},
},
user_id: "@example:localhost",
unsigned: {
device_display_name: "rust-sdk",
},
},
},
},
failures: {},
}),
);

await testOlmMachine.markRequestAsSent(
"bar",
RustSdkCryptoJs.RequestType.KeysClaim,
JSON.stringify({
one_time_keys: {
"@example:localhost": {
AFGUOBTZWM: {
"signed_curve25519:AAAABQ": {
key: "9IGouMnkB6c6HOd4xUsNv4i3Dulb4IS96TzDordzOws",
signatures: {
"@example:localhost": {
"ed25519:AFGUOBTZWM":
"2bvUbbmJegrV0eVP/vcJKuIWC3kud+V8+C0dZtg4dVovOSJdTP/iF36tQn2bh5+rb9xLlSeztXBdhy4c+LiOAg",
},
},
},
},
},
},
failures: {},
}),
);

const batch = await rustCrypto.encryptToDeviceMessages(
"m.test.type",
[
{ deviceId: "AAA", userId: "@user1:domain" },
{ deviceId: "BBB", userId: "@user1:domain" },
{ deviceId: "CCC", userId: "@user2:domain" },
{ deviceId: "AFGUOBTZWM", userId: "@example:localhost" },
],
payload,
);
expect(batch?.eventType).toEqual("m.room.encrypted");
expect(batch?.batch.length).toEqual(1);
expect(batch?.batch[0].deviceId).toEqual("AFGUOBTZWM");
expect(batch?.batch[0].userId).toEqual("@example:localhost");
expect(batch?.batch[0].payload).toEqual(
expect.objectContaining({
"algorithm": "m.olm.v1.curve25519-aes-sha2",
"ciphertext": expect.any(Object),
"org.matrix.msgid": expect.any(String),
"sender_key": expect.any(String),
}),
);
});
});
});

/** Build a MatrixHttpApi instance */
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3245,7 +3245,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* userDeviceMap, and returns the `{ contentMap, deviceInfoByDeviceId }`
* of the successfully sent messages.
*
* @deprecated Not supported for Rust Cryptography.
* @deprecated Instead use {@link CryptoApi.encryptToDeviceMessages} followed by {@link queueToDevice}.
*/
public encryptAndSendToDevices(userDeviceInfoArr: IOlmDevice<DeviceInfo>[], payload: object): Promise<void> {
if (!this.crypto) {
Expand Down
33 changes: 33 additions & 0 deletions src/crypto-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

import type { SecretsBundle } from "@matrix-org/matrix-sdk-crypto-wasm";
import type { IMegolmSessionData } from "../@types/crypto.ts";
import type { ToDeviceBatch, ToDevicePayload } from "../models/ToDeviceMessage.ts";
import { Room } from "../models/room.ts";
import { DeviceMap } from "../models/device.ts";
import { UIAuthCallback } from "../interactive-auth.ts";
Expand Down Expand Up @@ -340,6 +341,22 @@ export interface CryptoApi {
*/
getEncryptionInfoForEvent(event: MatrixEvent): Promise<EventEncryptionInfo | null>;

/**
* Encrypts a given payload object via Olm to-device messages to a given
* set of devices.
*
* @param eventType the type of the event to send
* @param devices an array of (user ID, device ID) pairs to encrypt the payload for
* @param payload the payload to encrypt
*
* @returns the batch of encrypted payloads which can then be sent via {@link MatrixClient#queueToDevice}
*/
encryptToDeviceMessages(
eventType: string,
devices: { userId: string; deviceId: string }[],
payload: ToDevicePayload,
): Promise<ToDeviceBatch>;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Device/User verification
Expand Down Expand Up @@ -550,6 +567,22 @@ export interface CryptoApi {
* @param secrets - The secrets bundle received from the other device
*/
importSecretsBundle?(secrets: Awaited<ReturnType<SecretsBundle["to_json"]>>): Promise<void>;

/**
* Encrypts a given payload object via Olm to-device messages to a given
* set of devices.
*
* @param eventType the type of the event to send
* @param devices an array of (user ID, device ID) pairs to encrypt the payload for
* @param payload the payload to encrypt
*
* @returns a promise which resolves to the batch of encrypted payloads which can then be sent via {@link MatrixClient#queueToDevice}
*/
encryptToDeviceMessages(
eventType: string,
devices: { userId: string; deviceId: string }[],
payload: ToDevicePayload,
): Promise<ToDeviceBatch>;
}

/** A reason code for a failure to decrypt an event. */
Expand Down
141 changes: 90 additions & 51 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { IStore } from "../store/index.ts";
import { Room, RoomEvent } from "../models/room.ts";
import { RoomMember, RoomMemberEvent } from "../models/room-member.ts";
import { EventStatus, IContent, IEvent, MatrixEvent, MatrixEventEvent } from "../models/event.ts";
import { ToDeviceBatch } from "../models/ToDeviceMessage.ts";
import { ToDeviceBatch, ToDevicePayload } from "../models/ToDeviceMessage.ts";
import { ClientEvent, IKeysUploadResponse, ISignedKey, IUploadKeySignaturesResponse, MatrixClient } from "../client.ts";
import { IRoomEncryption, RoomList } from "./RoomList.ts";
import { IKeyBackupInfo } from "./keybackup.ts";
Expand Down Expand Up @@ -3522,59 +3522,12 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
* resolves once the message has been encrypted and sent to the given
* userDeviceMap, and returns the `{ contentMap, deviceInfoByDeviceId }`
* of the successfully sent messages.
*
* @deprecated Instead use {@link encryptToDeviceMessages} followed by {@link MatrixClient.queueToDevice}.
*/
public async encryptAndSendToDevices(userDeviceInfoArr: IOlmDevice<DeviceInfo>[], payload: object): Promise<void> {
const toDeviceBatch: ToDeviceBatch = {
eventType: EventType.RoomMessageEncrypted,
batch: [],
};

try {
await Promise.all(
userDeviceInfoArr.map(async ({ userId, deviceInfo }) => {
const deviceId = deviceInfo.deviceId;
const encryptedContent: IEncryptedContent = {
algorithm: olmlib.OLM_ALGORITHM,
sender_key: this.olmDevice.deviceCurve25519Key!,
ciphertext: {},
[ToDeviceMessageId]: uuidv4(),
};

toDeviceBatch.batch.push({
userId,
deviceId,
payload: encryptedContent,
});

await olmlib.ensureOlmSessionsForDevices(
this.olmDevice,
this.baseApis,
new Map([[userId, [deviceInfo]]]),
);
await olmlib.encryptMessageForDevice(
encryptedContent.ciphertext,
this.userId,
this.deviceId,
this.olmDevice,
userId,
deviceInfo,
payload,
);
}),
);

// prune out any devices that encryptMessageForDevice could not encrypt for,
// in which case it will have just not added anything to the ciphertext object.
// There's no point sending messages to devices if we couldn't encrypt to them,
// since that's effectively a blank message.
toDeviceBatch.batch = toDeviceBatch.batch.filter((msg) => {
if (Object.keys(msg.payload.ciphertext).length > 0) {
return true;
} else {
logger.log(`No ciphertext for device ${msg.userId}:${msg.deviceId}: pruning`);
return false;
}
});
const toDeviceBatch = await this.prepareToDeviceBatch(userDeviceInfoArr, payload);

try {
await this.baseApis.queueToDevice(toDeviceBatch);
Expand Down Expand Up @@ -4305,6 +4258,92 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
public async startDehydration(createNewKey?: boolean): Promise<void> {
throw new Error("Not implemented");
}

private async prepareToDeviceBatch(
userDeviceInfoArr: IOlmDevice<DeviceInfo>[],
payload: object,
): Promise<ToDeviceBatch> {
const toDeviceBatch: ToDeviceBatch = {
eventType: EventType.RoomMessageEncrypted,
batch: [],
};

await Promise.all(
userDeviceInfoArr.map(async ({ userId, deviceInfo }) => {
const deviceId = deviceInfo.deviceId;
const encryptedContent: IEncryptedContent = {
algorithm: olmlib.OLM_ALGORITHM,
sender_key: this.olmDevice.deviceCurve25519Key!,
ciphertext: {},
[ToDeviceMessageId]: uuidv4(),
};

toDeviceBatch.batch.push({
userId,
deviceId,
payload: encryptedContent,
});

await olmlib.ensureOlmSessionsForDevices(
this.olmDevice,
this.baseApis,
new Map([[userId, [deviceInfo]]]),
);
await olmlib.encryptMessageForDevice(
encryptedContent.ciphertext,
this.userId,
this.deviceId,
this.olmDevice,
userId,
deviceInfo,
payload,
);
}),
);

// prune out any devices that encryptMessageForDevice could not encrypt for,
// in which case it will have just not added anything to the ciphertext object.
// There's no point sending messages to devices if we couldn't encrypt to them,
// since that's effectively a blank message.
toDeviceBatch.batch = toDeviceBatch.batch.filter((msg) => {
if (Object.keys(msg.payload.ciphertext).length > 0) {
return true;
} else {
logger.log(`No ciphertext for device ${msg.userId}:${msg.deviceId}: pruning`);
return false;
}
});

return toDeviceBatch;
}

public async encryptToDeviceMessages(
eventType: string,
devices: { userId: string; deviceId: string }[],
payload: ToDevicePayload,
): Promise<ToDeviceBatch> {
const userIds = new Set(devices.map(({ userId }) => userId));
const deviceInfoMap = await this.downloadKeys(Array.from(userIds), false);

const userDeviceInfoArr: IOlmDevice<DeviceInfo>[] = [];

devices.forEach(({ userId, deviceId }) => {
const devices = deviceInfoMap.get(userId);
if (!devices) {
logger.warn(`No devices found for user ${userId}`);
return;
}

if (devices.has(deviceId)) {
// Send the message to a specific device
userDeviceInfoArr.push({ userId, deviceInfo: devices.get(deviceId)! });
} else {
logger.warn(`No device found for user ${userId} with id ${deviceId}`);
}
});

return this.prepareToDeviceBatch(userDeviceInfoArr, payload);
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,22 @@ export class RoomWidgetClient extends MatrixClient {
await this.widgetApi.sendToDevice((payload as { type: string }).type, true, recursiveMapToObject(contentMap));
}

/**
* Send an event to a specific list of devices via the widget API.
* Optionally encrypts the event.
*
* @param eventType
* @param encrypted - Whether the event should be encrypted.
* @param contentMap content to send. Map from user_id to device_id to content object.
*/
public async sendToDeviceViaWidgetApi(
eventType: string,
encrypted: boolean,
contentMap: SendToDeviceContentMap,
): Promise<void> {
await this.widgetApi.sendToDevice(eventType, encrypted, recursiveMapToObject(contentMap));
}

// Overridden since we get TURN servers automatically over the widget API,
// and this method would otherwise complain about missing an access token
public async checkTurnServers(): Promise<boolean> {
Expand Down
Loading
Loading