2015-01-15 00:42:01 +01:00
|
|
|
;(function() {
|
2015-04-01 22:08:09 +02:00
|
|
|
'use strict';
|
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
window.textsecure.storage = window.textsecure.storage || {};
|
|
|
|
textsecure.storage.axolotl = new AxolotlStore();
|
|
|
|
var axolotlInstance = axolotl.protocol(textsecure.storage.axolotl);
|
2015-01-15 21:42:32 +01:00
|
|
|
|
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
window.textsecure.protocol_wrapper = {
|
2015-06-01 23:08:21 +02:00
|
|
|
decrypt: function(source, sourceDevice, type, blob) {
|
|
|
|
if (sourceDevice === null) { sourceDevice = 0; }
|
|
|
|
var fromAddress = [source, sourceDevice].join('.');
|
|
|
|
switch(type) {
|
|
|
|
case textsecure.protobuf.Envelope.Type.CIPHERTEXT:
|
|
|
|
return axolotlInstance.decryptWhisperMessage(fromAddress, getString(blob));
|
|
|
|
case textsecure.protobuf.Envelope.Type.PREKEY_BUNDLE:
|
|
|
|
if (blob.readUint8() != ((3 << 4) | 3))
|
2015-01-15 21:42:32 +01:00
|
|
|
throw new Error("Bad version byte");
|
2015-07-20 21:22:33 +02:00
|
|
|
var blob = blob.toArrayBuffer();
|
|
|
|
return axolotlInstance.handlePreKeyWhisperMessage(fromAddress, blob).catch(function(e) {
|
2015-07-31 21:11:17 +02:00
|
|
|
handleIdentityKeyError(fromAddress, blob, e);
|
2015-07-20 21:22:33 +02:00
|
|
|
});
|
2015-01-15 21:42:32 +01:00
|
|
|
default:
|
2015-06-01 23:08:21 +02:00
|
|
|
return new Promise.reject(new Error("Unknown message type"));
|
2015-01-15 21:42:32 +01:00
|
|
|
}
|
2015-03-17 22:31:33 +01:00
|
|
|
},
|
|
|
|
closeOpenSessionForDevice: function(encodedNumber) {
|
|
|
|
return axolotlInstance.closeOpenSessionForDevice(encodedNumber)
|
|
|
|
},
|
|
|
|
encryptMessageFor: function(deviceObject, pushMessageContent) {
|
|
|
|
return axolotlInstance.encryptMessageFor(deviceObject, pushMessageContent);
|
|
|
|
},
|
2015-05-04 20:55:47 +02:00
|
|
|
startWorker: function() {
|
|
|
|
axolotlInstance.startWorker('/js/libaxolotl-worker.js');
|
|
|
|
},
|
|
|
|
stopWorker: function() {
|
|
|
|
axolotlInstance.stopWorker();
|
2015-03-17 22:31:33 +01:00
|
|
|
},
|
|
|
|
createIdentityKeyRecvSocket: function() {
|
|
|
|
return axolotlInstance.createIdentityKeyRecvSocket();
|
2015-04-01 22:08:09 +02:00
|
|
|
},
|
|
|
|
hasOpenSession: function(encodedNumber) {
|
|
|
|
return axolotlInstance.hasOpenSession(encodedNumber);
|
|
|
|
},
|
|
|
|
getRegistrationId: function(encodedNumber) {
|
|
|
|
return axolotlInstance.getRegistrationId(encodedNumber);
|
2015-01-15 21:42:32 +01:00
|
|
|
}
|
|
|
|
};
|
2015-01-16 01:30:55 +01:00
|
|
|
|
2015-07-31 21:11:17 +02:00
|
|
|
function handleIdentityKeyError(from, blob, e) {
|
|
|
|
if (e.message === 'Unknown identity key') {
|
|
|
|
// create an error that the UI will pick up and ask the
|
|
|
|
// user if they want to re-negotiate
|
|
|
|
throw new textsecure.IncomingIdentityKeyError(from, blob, e.identityKey);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2015-02-24 01:23:22 +01:00
|
|
|
var tryMessageAgain = function(from, encodedMessage) {
|
2015-06-01 23:08:21 +02:00
|
|
|
return axolotlInstance.handlePreKeyWhisperMessage(from, encodedMessage).then(function(res) {
|
|
|
|
var finalMessage = textsecure.protobuf.DataMessage.decode(res[0]);
|
|
|
|
|
|
|
|
if ((finalMessage.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION)
|
|
|
|
== textsecure.protobuf.DataMessage.Flags.END_SESSION &&
|
|
|
|
finalMessage.sync !== null)
|
|
|
|
res[1]();
|
|
|
|
|
|
|
|
return processDecrypted(finalMessage);
|
2015-07-31 21:11:17 +02:00
|
|
|
}).catch(function(e) {
|
|
|
|
handleIdentityKeyError(from, encodedMessage, e);
|
2015-06-01 23:08:21 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-24 01:23:22 +01:00
|
|
|
textsecure.replay.registerFunction(tryMessageAgain, textsecure.replay.Type.INIT_SESSION);
|
2015-04-01 22:08:09 +02:00
|
|
|
|
2015-01-15 00:42:01 +01:00
|
|
|
})();
|