diff --git a/js/options.js b/js/options.js index c5e72e68..c9840d87 100644 --- a/js/options.js +++ b/js/options.js @@ -135,7 +135,7 @@ $('#multi-device .status').text("Connecting..."); $('#setup-qr').html(''); - textsecure.protocol.prepareTempWebsocket().then(function(cryptoInfo) { + axolotl.protocol.createIdentityKeyRecvSocket().then(function(cryptoInfo) { var qrCode = new QRCode(document.getElementById('setup-qr')); var socket = textsecure.api.getTempWebsocket(); diff --git a/libaxolotl/protocol.js b/libaxolotl/protocol.js index 10a1d7b0..6af2ba11 100644 --- a/libaxolotl/protocol.js +++ b/libaxolotl/protocol.js @@ -16,9 +16,9 @@ ;(function() { 'use strict'; -window.textsecure = window.textsecure || {}; +window.axolotl = window.axolotl || {}; -window.textsecure.protocol = function() { +window.axolotl.protocol = function() { var self = {}; /****************************** @@ -677,7 +677,7 @@ window.textsecure.protocol = function() { refreshPreKeys(); }, 60 * 1000); - self.prepareTempWebsocket = function() { + self.createIdentityKeyRecvSocket = function() { var socketInfo = {}; var keyPair; @@ -697,9 +697,12 @@ window.textsecure.protocol = function() { return verifyMAC(ivAndCiphertext, keys[1], mac).then(function() { return window.axolotl.crypto.decrypt(keys[0], ciphertext, iv).then(function(plaintext) { - var identityKeyMsg = textsecure.protobuf.ProvisionMessage.decode(plaintext); + var identityKeyMsg = axolotl.protobuf.ProvisionMessage.decode(plaintext); return axolotl.crypto.createKeyPair(toArrayBuffer(identityKeyMsg.identityKeyPrivate)).then(function(identityKeyPair) { + if (crypto_storage.getStoredKeyPair("identityKey") !== undefined) + throw new Error("Tried to overwrite identity key"); + crypto_storage.putKeyPair("identityKey", identityKeyPair); identityKeyMsg.identityKeyPrivate = null; diff --git a/libtextsecure/axolotl_wrapper.js b/libtextsecure/axolotl_wrapper.js index 1a830d5d..9108fdf5 100644 --- a/libtextsecure/axolotl_wrapper.js +++ b/libtextsecure/axolotl_wrapper.js @@ -39,7 +39,7 @@ //TODO /*if ((finalMessage.flags & textsecure.protobuf.PushMessageContent.Flags.END_SESSION) == textsecure.protobuf.PushMessageContent.Flags.END_SESSION) - textsecure.protocol.closeSession(res[1], true);*/ + axolotl.protocol.closeSession(res[1], true);*/ return finalMessage; } @@ -47,31 +47,29 @@ window.textsecure = window.textsecure || {}; window.textsecure.protocol_wrapper = { handleIncomingPushMessageProto: function(proto) { - - switch(proto.type) { case textsecure.protobuf.IncomingPushMessageSignal.Type.PLAINTEXT: return Promise.resolve(textsecure.protobuf.PushMessageContent.decode(proto.message)); case textsecure.protobuf.IncomingPushMessageSignal.Type.CIPHERTEXT: var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); - return textsecure.protocol.decryptWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); + return axolotl.protocol.decryptWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); case textsecure.protobuf.IncomingPushMessageSignal.Type.PREKEY_BUNDLE: if (proto.message.readUint8() != ((3 << 4) | 3)) throw new Error("Bad version byte"); var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); - return textsecure.protocol.handlePreKeyWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); + return axolotl.protocol.handlePreKeyWhisperMessage(from, getString(proto.message)).then(decodeMessageContents); case textsecure.protobuf.IncomingPushMessageSignal.Type.RECEIPT: return Promise.resolve(null); case textsecure.protobuf.IncomingPushMessageSignal.Type.PREKEY_BUNDLE_DEVICE_CONTROL: if (proto.message.readUint8() != ((3 << 4) | 3)) throw new Error("Bad version byte"); var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); - return textsecure.protocol.handlePreKeyWhisperMessage(from, getString(proto.message)).then(function(res) { + return axolotl.protocol.handlePreKeyWhisperMessage(from, getString(proto.message)).then(function(res) { return textsecure.protobuf.DeviceControl.decode(res[0]); }); case textsecure.protobuf.IncomingPushMessageSignal.Type.DEVICE_CONTROL: var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); - return textsecure.protocol.decryptWhisperMessage(from, getString(proto.message)).then(function(res) { + return axolotl.protocol.decryptWhisperMessage(from, getString(proto.message)).then(function(res) { return textsecure.protobuf.DeviceControl.decode(res[0]); }); default: @@ -85,7 +83,7 @@ //TODO: Encapsuate with the rest of textsecure.storage.devices textsecure.storage.removeEncrypted("devices" + from.split('.')[0]); //TODO: Probably breaks with a devicecontrol message - return textsecure.protocol.handlePreKeyWhisperMessage(from, encodedMessage).then(decodeMessageContents).then( + return axolotl.protocol.handlePreKeyWhisperMessage(from, encodedMessage).then(decodeMessageContents).then( function(pushMessageContent) { extension.trigger('message:decrypted', { message_id : message_id, diff --git a/libtextsecure/helpers.js b/libtextsecure/helpers.js index 196daa9e..d3d3c7d7 100644 --- a/libtextsecure/helpers.js +++ b/libtextsecure/helpers.js @@ -259,7 +259,7 @@ window.textsecure.registerSingleDevice = function(number, verificationCode, step textsecure.storage.putUnencrypted("regionCode", libphonenumber.util.getRegionCodeForNumber(number)); stepDone(1); - return textsecure.protocol.generateKeys().then(function(keys) { + return axolotl.protocol.generateKeys().then(function(keys) { stepDone(2); return textsecure.api.registerKeys(keys).then(function() { stepDone(3); @@ -290,7 +290,7 @@ window.textsecure.registerSecondDevice = function(encodedProvisionEnvelope, cryp textsecure.storage.putUnencrypted("regionCode", libphonenumber.util.getRegionCodeForNumber(identityKey.number)); stepDone(2); - return textsecure.protocol.generateKeys().then(function(keys) { + return axolotl.protocol.generateKeys().then(function(keys) { stepDone(3); return textsecure.api.registerKeys(keys).then(function() { stepDone(4); diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index df7dccab..9c9922a7 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -65,7 +65,7 @@ window.textsecure.messaging = function() { return new Promise(function() { throw new Error("Mismatched relays for number " + number); }); } - return textsecure.protocol.encryptMessageFor(deviceObjectList[i], message).then(function(encryptedMsg) { + return axolotl.protocol.encryptMessageFor(deviceObjectList[i], message).then(function(encryptedMsg) { jsonData[i] = { type: encryptedMsg.type, destinationDeviceId: textsecure.utils.unencodeNumber(deviceObjectList[i].encodedNumber)[1], @@ -289,7 +289,7 @@ window.textsecure.messaging = function() { return sendIndividualProto(number, proto).then(function(res) { var devices = textsecure.storage.devices.getDeviceObjectsForNumber(number); for (var i in devices) - textsecure.protocol.closeOpenSessionForDevice(devices[i].encodedNumber); + axolotl.protocol.closeOpenSessionForDevice(devices[i].encodedNumber); return res; }); diff --git a/libtextsecure/test/protocol_test.js b/libtextsecure/test/protocol_test.js index 2371b422..36ae16f5 100644 --- a/libtextsecure/test/protocol_test.js +++ b/libtextsecure/test/protocol_test.js @@ -47,14 +47,14 @@ describe('Protocol', function() { after(function() { localStorage.clear(); }); it ('works', function(done) { localStorage.clear(); - return textsecure.protocol.generateKeys().then(function() { + return axolotl.protocol.generateKeys().then(function() { assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey")); assert.isDefined(textsecure.storage.getEncrypted("25519KeysignedKey0")); for (var i = 0; i < 100; i++) { assert.isDefined(textsecure.storage.getEncrypted("25519KeypreKey" + i)); } var origIdentityKey = getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey); - return textsecure.protocol.generateKeys().then(function() { + return axolotl.protocol.generateKeys().then(function() { assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey")); assert.equal(getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey), origIdentityKey); @@ -65,7 +65,7 @@ describe('Protocol', function() { assert.isDefined(textsecure.storage.getEncrypted("25519KeypreKey" + i)); } - return textsecure.protocol.generateKeys().then(function() { + return axolotl.protocol.generateKeys().then(function() { assert.isDefined(textsecure.storage.getEncrypted("25519KeyidentityKey")); assert.equal(getString(textsecure.storage.getEncrypted("25519KeyidentityKey").privKey), origIdentityKey);