From 9f871db48a6673c53f41723ee7df101e6697d6b7 Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 27 Apr 2016 15:21:44 -0700 Subject: [PATCH] Update libsignal-protocol / Update prekey format Integrates change in prekey object formatting, which now matches more conveniently with the representation rendered by the server. // FREEBIE --- js/libtextsecure.js | 61 +++++++---------------- libtextsecure/libsignal-protocol.js | 14 +----- libtextsecure/outgoing_message.js | 20 +++----- libtextsecure/protocol_wrapper.js | 7 +++ libtextsecure/storage/devices.js | 20 +------- libtextsecure/test/device_storage_test.js | 32 +++++------- 6 files changed, 49 insertions(+), 105 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 3b4a40b2..db99ca17 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -34627,19 +34627,7 @@ window.libsignal.protocol = function(storage_interface) { var address = SignalProtocolAddress.fromString(deviceObject.encodedNumber); var builder = new SessionBuilder(storage_interface, address); - return builder.processPreKey({ - identityKey: toArrayBuffer(deviceObject.identityKey), - preKey: { - keyId: deviceObject.preKeyId, - publicKey: toArrayBuffer(deviceObject.preKey), - }, - signedPreKey: { - keyId: deviceObject.signedKeyId, - publicKey: toArrayBuffer(deviceObject.signedKey), - signature: toArrayBuffer(deviceObject.signedKeySignature), - }, - registrationId: deviceObject.registrationId - }); + return builder.processPreKey(deviceObject); } }).then(function() { return getRecord(deviceObject.encodedNumber).then(function(refreshed) { @@ -35458,6 +35446,13 @@ libsignal.SessionBuilder = SessionBuilder; throw e; }); }); + }, + processPreKey: function(preKeyBundle) { + return queueJobForNumber(preKeyBundle.encodedNumber, function() { + var address = libsignal.SignalProtocolAddress.fromString(preKeyBundle.encodedNumber); + var builder = new libsignal.SessionBuilder(textsecure.storage.protocol, address); + return builder.processPreKey(preKeyBundle); + }); } }; })(); @@ -35699,24 +35694,8 @@ libsignal.SessionBuilder = SessionBuilder; window.textsecure.storage.devices = { saveKeysToDeviceObject: function(deviceObject) { - var number = textsecure.utils.unencodeNumber(deviceObject.encodedNumber)[0]; - return textsecure.storage.protocol.loadIdentityKey(number).then(function(identityKey) { - if (identityKey !== undefined && deviceObject.identityKey !== undefined && getString(identityKey) != getString(deviceObject.identityKey)) { - var error = new Error("Identity key changed"); - error.identityKey = deviceObject.identityKey; - throw error; - } - - return textsecure.storage.protocol.putIdentityKey(number, deviceObject.identityKey).then(function() { - tempKeys[deviceObject.encodedNumber] = { - preKey: deviceObject.preKey, - preKeyId: deviceObject.preKeyId, - signedKey: deviceObject.signedKey, - signedKeyId: deviceObject.signedKeyId, - signedKeySignature: deviceObject.signedKeySignature, - registrationId: deviceObject.registrationId - }; - }); + return textsecure.protocol_wrapper.processPreKey(deviceObject).then(function() { + tempKeys[deviceObject.encodedNumber] = deviceObject; }); }, @@ -37480,23 +37459,19 @@ OutgoingMessage.prototype = { getKeysForNumber: function(number, updateDevices) { var handleResult = function(response) { return Promise.all(response.devices.map(function(device) { - if (updateDevices === undefined || updateDevices.indexOf(device.deviceId) > -1) - return textsecure.storage.devices.saveKeysToDeviceObject({ - encodedNumber: number + "." + device.deviceId, - identityKey: response.identityKey, - preKey: device.preKey.publicKey, - preKeyId: device.preKey.keyId, - signedKey: device.signedPreKey.publicKey, - signedKeyId: device.signedPreKey.keyId, - signedKeySignature: device.signedPreKey.signature, - registrationId: device.registrationId - }).catch(function(error) { + device.identityKey = response.identityKey; + device.encodedNumber = number + "." + device.deviceId; + if (updateDevices === undefined || updateDevices.indexOf(device.deviceId) > -1) { + return textsecure.storage.devices.saveKeysToDeviceObject(device).catch(function(error) { if (error.message === "Identity key changed") { - error = new textsecure.OutgoingIdentityKeyError(number, this.message.toArrayBuffer(), this.timestamp, error.identityKey); + error = new textsecure.OutgoingIdentityKeyError( + number, this.message.toArrayBuffer(), + this.timestamp, device.identityKey); this.registerError(number, "Identity key changed", error); } throw error; }.bind(this)); + } }.bind(this))); }.bind(this); diff --git a/libtextsecure/libsignal-protocol.js b/libtextsecure/libsignal-protocol.js index cb88f2f0..c2c40087 100644 --- a/libtextsecure/libsignal-protocol.js +++ b/libtextsecure/libsignal-protocol.js @@ -34513,19 +34513,7 @@ window.libsignal.protocol = function(storage_interface) { var address = SignalProtocolAddress.fromString(deviceObject.encodedNumber); var builder = new SessionBuilder(storage_interface, address); - return builder.processPreKey({ - identityKey: toArrayBuffer(deviceObject.identityKey), - preKey: { - keyId: deviceObject.preKeyId, - publicKey: toArrayBuffer(deviceObject.preKey), - }, - signedPreKey: { - keyId: deviceObject.signedKeyId, - publicKey: toArrayBuffer(deviceObject.signedKey), - signature: toArrayBuffer(deviceObject.signedKeySignature), - }, - registrationId: deviceObject.registrationId - }); + return builder.processPreKey(deviceObject); } }).then(function() { return getRecord(deviceObject.encodedNumber).then(function(refreshed) { diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 194cb7b9..07bc33c5 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -52,23 +52,19 @@ OutgoingMessage.prototype = { getKeysForNumber: function(number, updateDevices) { var handleResult = function(response) { return Promise.all(response.devices.map(function(device) { - if (updateDevices === undefined || updateDevices.indexOf(device.deviceId) > -1) - return textsecure.storage.devices.saveKeysToDeviceObject({ - encodedNumber: number + "." + device.deviceId, - identityKey: response.identityKey, - preKey: device.preKey.publicKey, - preKeyId: device.preKey.keyId, - signedKey: device.signedPreKey.publicKey, - signedKeyId: device.signedPreKey.keyId, - signedKeySignature: device.signedPreKey.signature, - registrationId: device.registrationId - }).catch(function(error) { + device.identityKey = response.identityKey; + device.encodedNumber = number + "." + device.deviceId; + if (updateDevices === undefined || updateDevices.indexOf(device.deviceId) > -1) { + return textsecure.storage.devices.saveKeysToDeviceObject(device).catch(function(error) { if (error.message === "Identity key changed") { - error = new textsecure.OutgoingIdentityKeyError(number, this.message.toArrayBuffer(), this.timestamp, error.identityKey); + error = new textsecure.OutgoingIdentityKeyError( + number, this.message.toArrayBuffer(), + this.timestamp, device.identityKey); this.registerError(number, "Identity key changed", error); } throw error; }.bind(this)); + } }.bind(this))); }.bind(this); diff --git a/libtextsecure/protocol_wrapper.js b/libtextsecure/protocol_wrapper.js index 447d0b4c..b6ec5599 100644 --- a/libtextsecure/protocol_wrapper.js +++ b/libtextsecure/protocol_wrapper.js @@ -82,6 +82,13 @@ throw e; }); }); + }, + processPreKey: function(preKeyBundle) { + return queueJobForNumber(preKeyBundle.encodedNumber, function() { + var address = libsignal.SignalProtocolAddress.fromString(preKeyBundle.encodedNumber); + var builder = new libsignal.SessionBuilder(textsecure.storage.protocol, address); + return builder.processPreKey(preKeyBundle); + }); } }; })(); diff --git a/libtextsecure/storage/devices.js b/libtextsecure/storage/devices.js index 6237ccee..23f1ec31 100644 --- a/libtextsecure/storage/devices.js +++ b/libtextsecure/storage/devices.js @@ -15,24 +15,8 @@ window.textsecure.storage.devices = { saveKeysToDeviceObject: function(deviceObject) { - var number = textsecure.utils.unencodeNumber(deviceObject.encodedNumber)[0]; - return textsecure.storage.protocol.loadIdentityKey(number).then(function(identityKey) { - if (identityKey !== undefined && deviceObject.identityKey !== undefined && getString(identityKey) != getString(deviceObject.identityKey)) { - var error = new Error("Identity key changed"); - error.identityKey = deviceObject.identityKey; - throw error; - } - - return textsecure.storage.protocol.putIdentityKey(number, deviceObject.identityKey).then(function() { - tempKeys[deviceObject.encodedNumber] = { - preKey: deviceObject.preKey, - preKeyId: deviceObject.preKeyId, - signedKey: deviceObject.signedKey, - signedKeyId: deviceObject.signedKeyId, - signedKeySignature: deviceObject.signedKeySignature, - registrationId: deviceObject.registrationId - }; - }); + return textsecure.protocol_wrapper.processPreKey(deviceObject).then(function() { + tempKeys[deviceObject.encodedNumber] = deviceObject; }); }, diff --git a/libtextsecure/test/device_storage_test.js b/libtextsecure/test/device_storage_test.js index adedeedc..1727f146 100644 --- a/libtextsecure/test/device_storage_test.js +++ b/libtextsecure/test/device_storage_test.js @@ -5,33 +5,27 @@ 'use strict'; describe('Device storage', function() { - before(function() { localStorage.clear(); }); var store = textsecure.storage.protocol; var identifier = '+5558675309'; var another_identifier = '+5555590210'; - var identityKey = { - pubKey: textsecure.crypto.getRandomBytes(33), - privKey: textsecure.crypto.getRandomBytes(32), - }; - var testKey = { - pubKey: textsecure.crypto.getRandomBytes(33), - privKey: textsecure.crypto.getRandomBytes(32), - }; + var prekeys, identityKey, testKey; + this.timeout(5000); + before(function(done) { + localStorage.clear(); + libsignal.util.generateIdentityKeyPair().then(function(identityKey) { + return textsecure.storage.protocol.putIdentityKey(identifier, identityKey); + }).then(done); + }); describe('saveKeysToDeviceObject', function() { it('rejects if the identity key changes', function(done) { return textsecure.storage.devices.saveKeysToDeviceObject({ - identityKey: identityKey.pubKey, + identityKey: textsecure.crypto.getRandomBytes(33), encodedNumber: identifier + '.1' }).then(function() { - textsecure.storage.devices.saveKeysToDeviceObject({ - identityKey: testKey.pubKey, - encodedNumber: identifier + '.1' - }).then(function() { - done(new Error('Allowed to overwrite identity key')); - }).catch(function(e) { - assert.strictEqual(e.message, 'Identity key changed'); - done(); - }); + done(new Error('Allowed to overwrite identity key')); + }).catch(function(e) { + assert.strictEqual(e.message, 'Identity key changed'); + done(); }); }); });