diff --git a/js/crypto.js b/js/crypto.js index a6adaba867..ac3b2fe155 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 5b2665168d..f72776b40d 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);