Skip to content

Commit

Permalink
Handle key acceptance
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Dec 15, 2022
1 parent f324468 commit fb5daa7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
8 changes: 6 additions & 2 deletions apps/meteor/app/e2e/client/rocketchat.e2e.room.js
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions apps/meteor/app/e2e/client/rocketchat.e2e.ts
Expand Up @@ -125,6 +125,12 @@ class E2E extends Emitter {
});
}

async acceptSuggestedKey(rid: string): Promise<void> {
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')];
}
Expand Down
32 changes: 20 additions & 12 deletions apps/meteor/client/startup/e2e.ts
Expand Up @@ -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;
}
Expand All @@ -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);
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/rest-typings/src/v1/e2e.ts
Expand Up @@ -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;
Expand Down

0 comments on commit fb5daa7

Please sign in to comment.