Fix session lookup in duplicate prekeymessage case

This commit is contained in:
Matt Corallo 2014-07-23 03:36:11 -04:00
parent 5040bfbe44
commit ebcfd4736e
2 changed files with 11 additions and 3 deletions

View file

@ -536,15 +536,21 @@ window.textsecure.crypto = function() {
var session = crypto_storage.getSessionOrIdentityKeyByBaseKey(encodedNumber, toArrayBuffer(message.baseKey)); var session = crypto_storage.getSessionOrIdentityKeyByBaseKey(encodedNumber, toArrayBuffer(message.baseKey));
var open_session = crypto_storage.getOpenSession(encodedNumber); var open_session = crypto_storage.getOpenSession(encodedNumber);
if (signedPreKeyPair === undefined) { if (preKeyPair === undefined || signedPreKeyPair === undefined) {
// Session may or may not be the correct one, but if its not, we can't do anything about it // 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 // ...fall through and let decryptWhisperMessage handle that case
if (session !== undefined && session.currentRatchet !== undefined) if (session !== undefined && session.currentRatchet !== undefined)
return Promise.resolve([session, undefined]); return Promise.resolve([session, undefined]);
else if (preKeyPair === undefined)
throw new Error("Missing PreKey for PreKeyWhisperMessage");
else else
throw new Error("Missing signedPreKey for PreKeyWhisperMessage"); throw new Error("Missing Signed PreKey for PreKeyWhisperMessage");
} }
if (session !== undefined) { 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: // We already had a session/known identity key:
if (isEqual(session.indexInfo.remoteIdentityKey, message.identityKey, false)) { 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 // If the identity key matches the previous one, close the previous one and use the new one

View file

@ -129,6 +129,8 @@ function getStringable(thing) {
function isEqual(a, b, mayBeShort) { function isEqual(a, b, mayBeShort) {
// TODO: Special-case arraybuffers, etc // TODO: Special-case arraybuffers, etc
if (a === undefined || b === undefined)
return false;
a = getString(a); a = getString(a);
b = getString(b); b = getString(b);
var maxLength = mayBeShort ? Math.min(a.length, b.length) : Math.max(a.length, b.length); var maxLength = mayBeShort ? Math.min(a.length, b.length) : Math.max(a.length, b.length);