Skip to content

Commit

Permalink
fix: Try to decrypt even if keyIDs don't match (#33615)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Oct 17, 2024
1 parent 415ddca commit 45d9b8e
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions apps/meteor/app/e2e/client/rocketchat.e2e.room.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,29 +653,38 @@ export class E2ERoom extends Emitter {
};
}

async doDecrypt(vector, key, cipherText) {
const result = await decryptAES(vector, key, cipherText);
return EJSON.parse(new TextDecoder('UTF-8').decode(new Uint8Array(result)));
}

async decrypt(message) {
const keyID = message.slice(0, 12);
message = message.slice(12);

const [vector, cipherText] = splitVectorAndEcryptedData(Base64.decode(message));

let oldKey = '';
if (keyID !== this.keyID) {
const oldRoomKey = this.oldKeys?.find((key) => key.e2eKeyId === keyID);
// Messages already contain a keyID stored with them
// That means that if we cannot find a keyID for the key the message has preppended to
// The message is indecipherable.
// In these cases, we'll give a last shot using the current session key, which may not work
// but will be enough to help with some mobile issues.
if (!oldRoomKey) {
this.error(`Message is indecipherable. Message KeyID ${keyID} not found in old room keys`);
return { msg: t('E2E_indecipherable') };
try {
return await this.doDecrypt(vector, this.groupSessionKey, cipherText);
} catch (error) {
this.error('Error decrypting message: ', error, message);
return { msg: t('E2E_indecipherable') };
}
}
oldKey = oldRoomKey.E2EKey;
}

message = message.slice(12);

const [vector, cipherText] = splitVectorAndEcryptedData(Base64.decode(message));

try {
const result = await decryptAES(vector, oldKey || this.groupSessionKey, cipherText);
return EJSON.parse(new TextDecoder('UTF-8').decode(new Uint8Array(result)));
return await this.doDecrypt(vector, oldKey || this.groupSessionKey, cipherText);
} catch (error) {
this.error('Error decrypting message: ', error, message);
return { msg: t('E2E_Key_Error') };
Expand Down

0 comments on commit 45d9b8e

Please sign in to comment.