Skip to content

Commit

Permalink
Merge pull request #3202 from matrix-org/rav/element-r/encryption_fixes
Browse files Browse the repository at this point in the history
Fixes to event encryption in the Rust Crypto implementation
  • Loading branch information
richvdh authored Mar 10, 2023
2 parents e79ef1f + 686216f commit 69f7789
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/rust-crypto/KeyClaimManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ export class KeyClaimManager {
// The Rust-SDK requires that we only have one getMissingSessions process in flight at once. This little dance
// ensures that, by only having one call to ensureSessionsForUsersInner active at once (and making them
// queue up in order).
const prom = this.currentClaimPromise.finally(() => this.ensureSessionsForUsersInner(userList));
const prom = this.currentClaimPromise
.catch(() => {
// any errors in the previous claim will have been reported already, so there is nothing to do here.
// we just throw away the error and start anew.
})
.then(() => this.ensureSessionsForUsersInner(userList));
this.currentClaimPromise = prom;
return prom;
}
Expand Down
15 changes: 14 additions & 1 deletion src/rust-crypto/RoomEncryptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Room } from "../models/room";
import { logger, PrefixedLogger } from "../logger";
import { KeyClaimManager } from "./KeyClaimManager";
import { RoomMember } from "../models/room-member";
import { OutgoingRequestProcessor } from "./OutgoingRequestProcessor";

/**
* RoomEncryptor: responsible for encrypting messages to a given room
Expand All @@ -38,6 +39,7 @@ export class RoomEncryptor {
public constructor(
private readonly olmMachine: OlmMachine,
private readonly keyClaimManager: KeyClaimManager,
private readonly outgoingRequestProcessor: OutgoingRequestProcessor,
private readonly room: Room,
private encryptionSettings: IContent,
) {
Expand Down Expand Up @@ -97,10 +99,21 @@ export class RoomEncryptor {
const userList = members.map((u) => new UserId(u.userId));
await this.keyClaimManager.ensureSessionsForUsers(userList);

this.prefixedLogger.debug("Sessions for users are ready; now sharing room key");

const rustEncryptionSettings = new EncryptionSettings();
/* FIXME historyVisibility, rotation, etc */

await this.olmMachine.shareRoomKey(new RoomId(this.room.roomId), userList, rustEncryptionSettings);
const shareMessages = await this.olmMachine.shareRoomKey(
new RoomId(this.room.roomId),
userList,
rustEncryptionSettings,
);
if (shareMessages) {
for (const m of shareMessages) {
await this.outgoingRequestProcessor.makeOutgoingRequest(m);
}
}
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,13 @@ export class RustCrypto implements CryptoBackend {
if (existingEncryptor) {
existingEncryptor.onCryptoEvent(config);
} else {
this.roomEncryptors[room.roomId] = new RoomEncryptor(this.olmMachine, this.keyClaimManager, room, config);
this.roomEncryptors[room.roomId] = new RoomEncryptor(
this.olmMachine,
this.keyClaimManager,
this.outgoingRequestProcessor,
room,
config,
);
}

// start tracking devices for any users already known to be in this room.
Expand Down

0 comments on commit 69f7789

Please sign in to comment.