diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 153a7e6a..3810ce9a 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -35493,11 +35493,6 @@ Internal.SessionLock.queueJobForNumber = function queueJobForNumber(number, runJ window.textsecure = window.textsecure || {}; window.textsecure.protocol_wrapper = { - closeOpenSessionForDevice: function(encodedNumber) { - return queueJobForNumber(encodedNumber, function() { - return protocolInstance.closeOpenSessionForDevice(encodedNumber); - }); - }, startWorker: function() { protocolInstance.startWorker('/js/libsignal-protocol-worker.js'); }, @@ -37338,8 +37333,11 @@ MessageReceiver.prototype.extend({ console.log('got end session'); return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) { return Promise.all(devices.map(function(device) { - console.log('closing session for', device.encodedNumber); - return textsecure.protocol_wrapper.closeOpenSessionForDevice(device.encodedNumber); + var address = new libsignal.SignalProtocolAddress(number, device.deviceId); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + + console.log('closing session for', address.toString()); + return sessionCipher.closeOpenSessionForDevice(); })); }); }, @@ -37552,7 +37550,14 @@ OutgoingMessage.prototype = { }, doSendMessage: function(number, devicesForNumber, recurse) { - return this.encryptToDevices(devicesForNumber).then(function(jsonData) { + var ciphers = {}; + var plaintext = this.message.toArrayBuffer(); + return Promise.all(devicesForNumber.map(function(device) { + var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + ciphers[address.getDeviceId()] = sessionCipher; + return this.encryptToDevice(device, plaintext, sessionCipher); + }.bind(this))).then(function(jsonData) { return this.transmitMessage(number, jsonData, this.timestamp).then(function() { this.successfulNumbers[this.successfulNumbers.length] = number; this.numberCompleted(); @@ -37567,7 +37572,7 @@ OutgoingMessage.prototype = { p = textsecure.storage.devices.removeDeviceIdsForNumber(number, error.response.extraDevices); } else { p = Promise.all(error.response.staleDevices.map(function(deviceId) { - return textsecure.protocol_wrapper.closeOpenSessionForDevice(number + '.' + deviceId); + return ciphers[deviceId].closeOpenSessionForDevice(); })); } @@ -37585,17 +37590,13 @@ OutgoingMessage.prototype = { }.bind(this)); }, - encryptToDevices: function(deviceObjectList) { - var plaintext = this.message.toArrayBuffer(); - return Promise.all(deviceObjectList.map(function(device) { - var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); - var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); - return sessionCipher.encrypt(plaintext).then(function(encryptedMsg) { - return sessionCipher.getRemoteRegistrationId().then(function(registrationId) { - return this.toJSON(device, encryptedMsg, registrationId); - }.bind(this)); - }.bind(this)); - }.bind(this))); + encryptToDevice: function(device, plaintext, sessionCipher) { + return Promise.all([ + sessionCipher.encrypt(plaintext), + sessionCipher.getRemoteRegistrationId() + ]).then(function(result) { + return this.toJSON(device, result[0], result[1]); + }.bind(this)); }, toJSON: function(device, encryptedMsg, registrationId) { @@ -37930,7 +37931,10 @@ MessageSender.prototype = { return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) { return Promise.all(devices.map(function(device) { console.log('closing session for', device.encodedNumber); - return textsecure.protocol_wrapper.closeOpenSessionForDevice(device.encodedNumber); + + var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + return sessionCipher.closeOpenSessionForDevice(); })).then(function() { return res; }); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 9aa90c55..a7676313 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -345,8 +345,11 @@ MessageReceiver.prototype.extend({ console.log('got end session'); return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) { return Promise.all(devices.map(function(device) { - console.log('closing session for', device.encodedNumber); - return textsecure.protocol_wrapper.closeOpenSessionForDevice(device.encodedNumber); + var address = new libsignal.SignalProtocolAddress(number, device.deviceId); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + + console.log('closing session for', address.toString()); + return sessionCipher.closeOpenSessionForDevice(); })); }); }, diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index aa152165..dc88368a 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -96,7 +96,14 @@ OutgoingMessage.prototype = { }, doSendMessage: function(number, devicesForNumber, recurse) { - return this.encryptToDevices(devicesForNumber).then(function(jsonData) { + var ciphers = {}; + var plaintext = this.message.toArrayBuffer(); + return Promise.all(devicesForNumber.map(function(device) { + var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + ciphers[address.getDeviceId()] = sessionCipher; + return this.encryptToDevice(device, plaintext, sessionCipher); + }.bind(this))).then(function(jsonData) { return this.transmitMessage(number, jsonData, this.timestamp).then(function() { this.successfulNumbers[this.successfulNumbers.length] = number; this.numberCompleted(); @@ -111,7 +118,7 @@ OutgoingMessage.prototype = { p = textsecure.storage.devices.removeDeviceIdsForNumber(number, error.response.extraDevices); } else { p = Promise.all(error.response.staleDevices.map(function(deviceId) { - return textsecure.protocol_wrapper.closeOpenSessionForDevice(number + '.' + deviceId); + return ciphers[deviceId].closeOpenSessionForDevice(); })); } @@ -129,17 +136,13 @@ OutgoingMessage.prototype = { }.bind(this)); }, - encryptToDevices: function(deviceObjectList) { - var plaintext = this.message.toArrayBuffer(); - return Promise.all(deviceObjectList.map(function(device) { - var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); - var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); - return sessionCipher.encrypt(plaintext).then(function(encryptedMsg) { - return sessionCipher.getRemoteRegistrationId().then(function(registrationId) { - return this.toJSON(device, encryptedMsg, registrationId); - }.bind(this)); - }.bind(this)); - }.bind(this))); + encryptToDevice: function(device, plaintext, sessionCipher) { + return Promise.all([ + sessionCipher.encrypt(plaintext), + sessionCipher.getRemoteRegistrationId() + ]).then(function(result) { + return this.toJSON(device, result[0], result[1]); + }.bind(this)); }, toJSON: function(device, encryptedMsg, registrationId) { diff --git a/libtextsecure/protocol_wrapper.js b/libtextsecure/protocol_wrapper.js index b8c3e50d..d82a3296 100644 --- a/libtextsecure/protocol_wrapper.js +++ b/libtextsecure/protocol_wrapper.js @@ -28,11 +28,6 @@ window.textsecure = window.textsecure || {}; window.textsecure.protocol_wrapper = { - closeOpenSessionForDevice: function(encodedNumber) { - return queueJobForNumber(encodedNumber, function() { - return protocolInstance.closeOpenSessionForDevice(encodedNumber); - }); - }, startWorker: function() { protocolInstance.startWorker('/js/libsignal-protocol-worker.js'); }, diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index d062665c..babc0cbd 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -298,7 +298,10 @@ MessageSender.prototype = { return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) { return Promise.all(devices.map(function(device) { console.log('closing session for', device.encodedNumber); - return textsecure.protocol_wrapper.closeOpenSessionForDevice(device.encodedNumber); + + var address = libsignal.SignalProtocolAddress.fromString(device.encodedNumber); + var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address); + return sessionCipher.closeOpenSessionForDevice(); })).then(function() { return res; });