From 78cdc0cb522c93c189e78bd4a44a3f2179567962 Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 27 Apr 2016 15:47:24 -0700 Subject: [PATCH] Remove tempKeys Now that we simply establish and save a session via SessionBuilder rather than saving the keys to pass in during encrypt, we can stop caching them in memory in between the identity key check and the encryption step. // FREEBIE --- js/libtextsecure.js | 46 ++++++++----------------------- libtextsecure/outgoing_message.js | 5 +--- libtextsecure/storage/devices.js | 41 +++++++-------------------- 3 files changed, 22 insertions(+), 70 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 7ff41142..58c68cef 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -35674,18 +35674,9 @@ libsignal.SessionBuilder = SessionBuilder; window.textsecure = window.textsecure || {}; window.textsecure.storage = window.textsecure.storage || {}; - var tempKeys = {}; - window.textsecure.storage.devices = { saveKeysToDeviceObject: function(deviceObject) { - return textsecure.protocol_wrapper.processPreKey(deviceObject).then(function() { - tempKeys[deviceObject.encodedNumber] = deviceObject; - }); - }, - - removeTempKeysFromDevice: function(encodedNumber) { - delete tempKeys[encodedNumber]; - return Promise.resolve(); + return textsecure.protocol_wrapper.processPreKey(deviceObject); }, getStaleDeviceIdsForNumber: function(number) { @@ -35695,9 +35686,9 @@ libsignal.SessionBuilder = SessionBuilder; } var updateDevices = []; return Promise.all(deviceIds.map(function(deviceId) { - var encodedNumber = number + '.' + deviceId; - return textsecure.protocol_wrapper.hasOpenSession(encodedNumber).then(function(hasSession) { - if (!hasSession && !tempKeys[encodedNumber]) { + var address = new libsignal.SignalProtocolAddress(number, deviceId).toString(); + return textsecure.protocol_wrapper.hasOpenSession(address).then(function(hasSession) { + if (!hasSession) { updateDevices.push(deviceId); } }); @@ -35712,24 +35703,13 @@ libsignal.SessionBuilder = SessionBuilder; return []; } return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) { - // Add pending devices from tempKeys - for (var encodedNumber in tempKeys) { - var deviceNumber = textsecure.utils.unencodeNumber(encodedNumber)[0]; - var deviceId = parseInt(textsecure.utils.unencodeNumber(encodedNumber)[1]); - if (deviceNumber === number && deviceIds.indexOf(deviceId) < 0) { - deviceIds.push(deviceId); - } - } return Promise.all(deviceIds.map(function(deviceId) { - var encodedNumber = number + '.' + deviceId; - var deviceObject = tempKeys[encodedNumber] || {}; - deviceObject.encodedNumber = encodedNumber; - deviceObject.identityKey = identityKey; - return textsecure.protocol_wrapper.getRegistrationId(encodedNumber).then(function(registrationId) { - if (deviceObject.registrationId === undefined) { - deviceObject.registrationId = registrationId; - } - return deviceObject; + var address = new libsignal.SignalProtocolAddress(number, deviceId).toString(); + return textsecure.protocol_wrapper.getRegistrationId(address).then(function(registrationId) { + return { + encodedNumber : address, + registrationId : registrationId + }; }); })); }); @@ -35741,7 +35721,6 @@ libsignal.SessionBuilder = SessionBuilder; for (var j in deviceIdsToRemove) { promise = promise.then(function() { var encodedNumber = number + "." + deviceIdsToRemove[j]; - delete tempKeys[encodedNumber]; return textsecure.storage.protocol.removeSession(encodedNumber); }); } @@ -37522,10 +37501,7 @@ OutgoingMessage.prototype = { var plaintext = this.message.toArrayBuffer(); return Promise.all(deviceObjectList.map(function(device) { return textsecure.protocol_wrapper.encryptMessageFor(device, plaintext).then(function(encryptedMsg) { - var json = this.toJSON(device, encryptedMsg); - return textsecure.storage.devices.removeTempKeysFromDevice(device.encodedNumber).then(function() { - return json; - }); + return this.toJSON(device, encryptedMsg); }.bind(this)); }.bind(this))); }, diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 07bc33c5..c4bc6137 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -131,10 +131,7 @@ OutgoingMessage.prototype = { var plaintext = this.message.toArrayBuffer(); return Promise.all(deviceObjectList.map(function(device) { return textsecure.protocol_wrapper.encryptMessageFor(device, plaintext).then(function(encryptedMsg) { - var json = this.toJSON(device, encryptedMsg); - return textsecure.storage.devices.removeTempKeysFromDevice(device.encodedNumber).then(function() { - return json; - }); + return this.toJSON(device, encryptedMsg); }.bind(this)); }.bind(this))); }, diff --git a/libtextsecure/storage/devices.js b/libtextsecure/storage/devices.js index 23f1ec31..ba437351 100644 --- a/libtextsecure/storage/devices.js +++ b/libtextsecure/storage/devices.js @@ -11,18 +11,9 @@ window.textsecure = window.textsecure || {}; window.textsecure.storage = window.textsecure.storage || {}; - var tempKeys = {}; - window.textsecure.storage.devices = { saveKeysToDeviceObject: function(deviceObject) { - return textsecure.protocol_wrapper.processPreKey(deviceObject).then(function() { - tempKeys[deviceObject.encodedNumber] = deviceObject; - }); - }, - - removeTempKeysFromDevice: function(encodedNumber) { - delete tempKeys[encodedNumber]; - return Promise.resolve(); + return textsecure.protocol_wrapper.processPreKey(deviceObject); }, getStaleDeviceIdsForNumber: function(number) { @@ -32,9 +23,9 @@ } var updateDevices = []; return Promise.all(deviceIds.map(function(deviceId) { - var encodedNumber = number + '.' + deviceId; - return textsecure.protocol_wrapper.hasOpenSession(encodedNumber).then(function(hasSession) { - if (!hasSession && !tempKeys[encodedNumber]) { + var address = new libsignal.SignalProtocolAddress(number, deviceId).toString(); + return textsecure.protocol_wrapper.hasOpenSession(address).then(function(hasSession) { + if (!hasSession) { updateDevices.push(deviceId); } }); @@ -49,24 +40,13 @@ return []; } return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) { - // Add pending devices from tempKeys - for (var encodedNumber in tempKeys) { - var deviceNumber = textsecure.utils.unencodeNumber(encodedNumber)[0]; - var deviceId = parseInt(textsecure.utils.unencodeNumber(encodedNumber)[1]); - if (deviceNumber === number && deviceIds.indexOf(deviceId) < 0) { - deviceIds.push(deviceId); - } - } return Promise.all(deviceIds.map(function(deviceId) { - var encodedNumber = number + '.' + deviceId; - var deviceObject = tempKeys[encodedNumber] || {}; - deviceObject.encodedNumber = encodedNumber; - deviceObject.identityKey = identityKey; - return textsecure.protocol_wrapper.getRegistrationId(encodedNumber).then(function(registrationId) { - if (deviceObject.registrationId === undefined) { - deviceObject.registrationId = registrationId; - } - return deviceObject; + var address = new libsignal.SignalProtocolAddress(number, deviceId).toString(); + return textsecure.protocol_wrapper.getRegistrationId(address).then(function(registrationId) { + return { + encodedNumber : address, + registrationId : registrationId + }; }); })); }); @@ -78,7 +58,6 @@ for (var j in deviceIdsToRemove) { promise = promise.then(function() { var encodedNumber = number + "." + deviceIdsToRemove[j]; - delete tempKeys[encodedNumber]; return textsecure.storage.protocol.removeSession(encodedNumber); }); }