From ebcfd4736e60bfd1b0c874e99d91ba6b6ebe5c4e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 23 Jul 2014 03:36:11 -0400 Subject: [PATCH] Fix session lookup in duplicate prekeymessage case --- js/crypto.js | 12 +++++++++--- js/helpers.js | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/js/crypto.js b/js/crypto.js index a6adaba86..ac3b2fe15 100644 --- a/js/crypto.js +++ b/js/crypto.js @@ -536,15 +536,21 @@ window.textsecure.crypto = function() { var session = crypto_storage.getSessionOrIdentityKeyByBaseKey(encodedNumber, toArrayBuffer(message.baseKey)); var open_session = crypto_storage.getOpenSession(encodedNumber); - if (signedPreKeyPair === undefined) { - // Session may or may not be the correct one, but if its not, we can't do anything about it + if (preKeyPair === undefined || signedPreKeyPair === undefined) { + // Session may or may not be the right one, but if its not, we can't do anything about it // ...fall through and let decryptWhisperMessage handle that case if (session !== undefined && session.currentRatchet !== undefined) return Promise.resolve([session, undefined]); + else if (preKeyPair === undefined) + throw new Error("Missing PreKey for PreKeyWhisperMessage"); else - throw new Error("Missing signedPreKey for PreKeyWhisperMessage"); + throw new Error("Missing Signed PreKey for PreKeyWhisperMessage"); } if (session !== undefined) { + // Duplicate PreKeyMessage for session: + if (isEqual(session.indexInfo.baseKey, message.baseKey, false)) + return Promise.resolve([session, undefined]); + // We already had a session/known identity key: if (isEqual(session.indexInfo.remoteIdentityKey, message.identityKey, false)) { // If the identity key matches the previous one, close the previous one and use the new one diff --git a/js/helpers.js b/js/helpers.js index 5b2665168..f72776b40 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -129,6 +129,8 @@ function getStringable(thing) { function isEqual(a, b, mayBeShort) { // TODO: Special-case arraybuffers, etc + if (a === undefined || b === undefined) + return false; a = getString(a); b = getString(b); var maxLength = mayBeShort ? Math.min(a.length, b.length) : Math.max(a.length, b.length);