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

fix: Try to decrypt even if keyIDs don't match #33615

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Changes from 1 commit
Commits
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
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_Key_Error') };
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
Loading