diff --git a/apps/meteor/app/e2e/client/rocketchat.e2e.room.js b/apps/meteor/app/e2e/client/rocketchat.e2e.room.js index fea71e381182..739a939b8c35 100644 --- a/apps/meteor/app/e2e/client/rocketchat.e2e.room.js +++ b/apps/meteor/app/e2e/client/rocketchat.e2e.room.js @@ -264,7 +264,8 @@ export class E2ERoom extends Emitter { const decryptedKey = await decryptRSA(e2e.privateKey, groupKey); this.sessionKeyExportedString = toString(decryptedKey); } catch (error) { - return this.error('Error decrypting group key: ', error); + this.error('Error decrypting group key: ', error); + return false; } this.keyID = Base64.encode(this.sessionKeyExportedString).slice(0, 12); @@ -275,8 +276,11 @@ export class E2ERoom extends Emitter { // Key has been obtained. E2E is now in session. this.groupSessionKey = key; } catch (error) { - return this.error('Error importing group key: ', error); + this.error('Error importing group key: ', error); + return false; } + + return true; } async createGroupKey() { diff --git a/apps/meteor/app/e2e/client/rocketchat.e2e.ts b/apps/meteor/app/e2e/client/rocketchat.e2e.ts index c770c4a23ebc..71e4c8244777 100644 --- a/apps/meteor/app/e2e/client/rocketchat.e2e.ts +++ b/apps/meteor/app/e2e/client/rocketchat.e2e.ts @@ -125,6 +125,12 @@ class E2E extends Emitter { }); } + async acceptSuggestedKey(rid: string): Promise { + await APIClient.post('/v1/e2e.acceptSuggestedGroupKey', { + rid, + }); + } + getKeysFromLocalStorage(): [public_key: string | null, private_key: string | null] { return [Meteor._localStorage.getItem('public_key'), Meteor._localStorage.getItem('private_key')]; } diff --git a/apps/meteor/client/startup/e2e.ts b/apps/meteor/client/startup/e2e.ts index 8ed28694d22d..77ba0eb89313 100644 --- a/apps/meteor/client/startup/e2e.ts +++ b/apps/meteor/client/startup/e2e.ts @@ -53,26 +53,34 @@ Meteor.startup(() => { Notifications.onUser('e2ekeyRequest', handle); observable = Subscriptions.find().observe({ - changed: async (doc: ISubscription) => { - if (!doc.encrypted && !doc.E2EKey) { - e2e.removeInstanceByRoomId(doc.rid); + changed: async (sub: ISubscription) => { + if (!sub.encrypted && !sub.E2EKey) { + e2e.removeInstanceByRoomId(sub.rid); return; } - const e2eRoom = await e2e.getInstanceByRoomId(doc.rid); + const e2eRoom = await e2e.getInstanceByRoomId(sub.rid); if (!e2eRoom) { return; } - doc.encrypted ? e2eRoom.resume() : e2eRoom.pause(); + if (sub.E2ESuggestedKey) { + if (await e2eRoom.importGroupKey(sub.E2ESuggestedKey)) { + e2e.acceptSuggestedKey(sub.rid); + } else { + console.log('Invalid E2ESuggestedKey', sub.E2ESuggestedKey); + } + } + + sub.encrypted ? e2eRoom.resume() : e2eRoom.pause(); // Cover private groups and direct messages - if (!e2eRoom.isSupportedRoomType(doc.t)) { + if (!e2eRoom.isSupportedRoomType(sub.t)) { e2eRoom.disable(); return; } - if (doc.E2EKey && e2eRoom.isWaitingKeys()) { + if (sub.E2EKey && e2eRoom.isWaitingKeys()) { e2eRoom.keyReceived(); return; } @@ -83,14 +91,14 @@ Meteor.startup(() => { e2eRoom.decryptSubscription(); }, - added: async (doc: ISubscription) => { - if (!doc.encrypted && !doc.E2EKey) { + added: async (sub: ISubscription) => { + if (!sub.encrypted && !sub.E2EKey) { return; } - return e2e.getInstanceByRoomId(doc.rid); + return e2e.getInstanceByRoomId(sub.rid); }, - removed: (doc: ISubscription) => { - e2e.removeInstanceByRoomId(doc.rid); + removed: (sub: ISubscription) => { + e2e.removeInstanceByRoomId(sub.rid); }, }); diff --git a/packages/rest-typings/src/v1/e2e.ts b/packages/rest-typings/src/v1/e2e.ts index 4f52d9c4230b..8f0cc2b2dd3b 100644 --- a/packages/rest-typings/src/v1/e2e.ts +++ b/packages/rest-typings/src/v1/e2e.ts @@ -100,7 +100,7 @@ export type E2eEndpoints = { POST: (params: E2eUpdateGroupKeyProps) => void; }; '/v1/e2e.acceptSuggestedGroupKey': { - POST: (params: E2eSetUserPublicAndPrivateKeysProps) => void; + POST: (params: E2eGetUsersOfRoomWithoutKeyProps) => void; }; '/v1/e2e.setRoomKeyID': { POST: (params: E2eSetRoomKeyIdProps) => void;