|
|
|
@ -92,6 +92,8 @@ function base64EncArr (aBytes) {
|
|
|
|
|
|
|
|
|
|
/* END CRAP TO BE DELETED */
|
|
|
|
|
|
|
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
|
|
|
|
|
|
/*********************************
|
|
|
|
|
*** Type conversion utilities ***
|
|
|
|
|
*********************************/
|
|
|
|
@ -176,7 +178,7 @@ function base64ToArrayBuffer(string) {
|
|
|
|
|
return base64DecToArr(string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Protobuf decodingA
|
|
|
|
|
// Protobuf decoding
|
|
|
|
|
//TODO: throw on missing fields everywhere
|
|
|
|
|
var IncomingPushMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/IncomingPushMessageSignal.proto").build("textsecure.IncomingPushMessageSignal");
|
|
|
|
|
function decodeIncomingPushMessageProtobuf(string) {
|
|
|
|
@ -224,13 +226,6 @@ function verifyNumber(string) {
|
|
|
|
|
return getEncodedNumber(string.trim());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDeviceId(encodedNumber) {
|
|
|
|
|
var split = encodedNumber.split(".");
|
|
|
|
|
if (split.length > 1)
|
|
|
|
|
return split[1];
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Other
|
|
|
|
|
|
|
|
|
|
function timestampToHumanReadable(timestamp) {
|
|
|
|
@ -251,18 +246,21 @@ function objectContainsKeys(object) {
|
|
|
|
|
/************************************************
|
|
|
|
|
*** Utilities to store data in local storage ***
|
|
|
|
|
************************************************/
|
|
|
|
|
var storage = new function() {
|
|
|
|
|
//TODO: textsecure.storage
|
|
|
|
|
window.storage = function() {
|
|
|
|
|
var self = {};
|
|
|
|
|
|
|
|
|
|
/*****************************
|
|
|
|
|
*** Base Storage Routines ***
|
|
|
|
|
*****************************/
|
|
|
|
|
this.putEncrypted = function(key, value) {
|
|
|
|
|
self.putEncrypted = function(key, value) {
|
|
|
|
|
//TODO
|
|
|
|
|
if (value === undefined)
|
|
|
|
|
throw new Error("Tried to store undefined");
|
|
|
|
|
localStorage.setItem("e" + key, jsonThing(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.getEncrypted = function(key, defaultValue) {
|
|
|
|
|
self.getEncrypted = function(key, defaultValue) {
|
|
|
|
|
//TODO
|
|
|
|
|
var value = localStorage.getItem("e" + key);
|
|
|
|
|
if (value === null)
|
|
|
|
@ -270,40 +268,42 @@ var storage = new function() {
|
|
|
|
|
return JSON.parse(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.removeEncrypted = function(key) {
|
|
|
|
|
self.removeEncrypted = function(key) {
|
|
|
|
|
localStorage.removeItem("e" + key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.putUnencrypted = function(key, value) {
|
|
|
|
|
self.putUnencrypted = function(key, value) {
|
|
|
|
|
if (value === undefined)
|
|
|
|
|
throw new Error("Tried to store undefined");
|
|
|
|
|
localStorage.setItem("u" + key, jsonThing(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.getUnencrypted = function(key, defaultValue) {
|
|
|
|
|
self.getUnencrypted = function(key, defaultValue) {
|
|
|
|
|
var value = localStorage.getItem("u" + key);
|
|
|
|
|
if (value === null)
|
|
|
|
|
return defaultValue;
|
|
|
|
|
return JSON.parse(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.removeUnencrypted = function(key) {
|
|
|
|
|
self.removeUnencrypted = function(key) {
|
|
|
|
|
localStorage.removeItem("u" + key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**********************
|
|
|
|
|
*** Device Storage ***
|
|
|
|
|
**********************/
|
|
|
|
|
this.devices = new function() {
|
|
|
|
|
this.getDeviceObject = function(encodedNumber) {
|
|
|
|
|
self.devices = function() {
|
|
|
|
|
var self = {};
|
|
|
|
|
|
|
|
|
|
self.getDeviceObject = function(encodedNumber) {
|
|
|
|
|
return storage.getEncrypted("deviceObject" + getEncodedNumber(encodedNumber));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.getDeviceIdListFromNumber = function(number) {
|
|
|
|
|
self.getDeviceIdListFromNumber = function(number) {
|
|
|
|
|
return storage.getEncrypted("deviceIdList" + getNumberFromString(number), []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.addDeviceIdForNumber = function(number, deviceId) {
|
|
|
|
|
self.addDeviceIdForNumber = function(number, deviceId) {
|
|
|
|
|
var deviceIdList = this.getDeviceIdListFromNumber(getNumberFromString(number));
|
|
|
|
|
for (var i = 0; i < deviceIdList.length; i++) {
|
|
|
|
|
if (deviceIdList[i] == deviceId)
|
|
|
|
@ -313,8 +313,15 @@ var storage = new function() {
|
|
|
|
|
storage.putEncrypted("deviceIdList" + getNumberFromString(number), deviceIdList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var getDeviceId = function(encodedNumber) {
|
|
|
|
|
var split = encodedNumber.split(".");
|
|
|
|
|
if (split.length > 1)
|
|
|
|
|
return split[1];
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// throws "Identity key mismatch"
|
|
|
|
|
this.saveDeviceObject = function(deviceObject) {
|
|
|
|
|
self.saveDeviceObject = function(deviceObject) {
|
|
|
|
|
var existing = this.getDeviceObject(deviceObject.encodedNumber);
|
|
|
|
|
if (existing === undefined)
|
|
|
|
|
existing = {encodedNumber: getEncodedNumber(deviceObject.encodedNumber)};
|
|
|
|
@ -331,15 +338,19 @@ var storage = new function() {
|
|
|
|
|
this.addDeviceIdForNumber(deviceObject.encodedNumber, getDeviceId(deviceObject.encodedNumber));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.getDeviceObjectListFromNumber = function(number) {
|
|
|
|
|
self.getDeviceObjectListFromNumber = function(number) {
|
|
|
|
|
var deviceObjectList = [];
|
|
|
|
|
var deviceIdList = this.getDeviceIdListFromNumber(number);
|
|
|
|
|
for (var i = 0; i < deviceIdList.length; i++)
|
|
|
|
|
deviceObjectList[deviceObjectList.length] = this.getDeviceObject(getNumberFromString(number) + "." + deviceIdList[i]);
|
|
|
|
|
return deviceObjectList;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
function registrationDone() {
|
|
|
|
|
storage.putUnencrypted("registration_done", "");
|
|
|
|
@ -374,34 +385,37 @@ function storeMessage(messageObject) {
|
|
|
|
|
/**********************
|
|
|
|
|
*** NaCL Interface ***
|
|
|
|
|
**********************/
|
|
|
|
|
var USE_NACL = false;
|
|
|
|
|
window.textsecure.nacl = function() {
|
|
|
|
|
var self = {};
|
|
|
|
|
|
|
|
|
|
self.USE_NACL = false;
|
|
|
|
|
|
|
|
|
|
var onLoadCallbacks = [];
|
|
|
|
|
var naclLoaded = 0;
|
|
|
|
|
function registerOnLoadFunction(func) {
|
|
|
|
|
if (naclLoaded || !USE_NACL) {
|
|
|
|
|
var onLoadCallbacks = [];
|
|
|
|
|
var naclLoaded = 0;
|
|
|
|
|
self.registerOnLoadFunction = function(func) {
|
|
|
|
|
if (naclLoaded || !self.USE_NACL) {
|
|
|
|
|
func();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
onLoadCallbacks[onLoadCallbacks.length] = func;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var naclMessageNextId = 0;
|
|
|
|
|
var naclMessageIdCallbackMap = {};
|
|
|
|
|
function moduleDidLoad() {
|
|
|
|
|
var naclMessageNextId = 0;
|
|
|
|
|
var naclMessageIdCallbackMap = {};
|
|
|
|
|
window.moduleDidLoad = function() {
|
|
|
|
|
common.hideModule();
|
|
|
|
|
naclLoaded = 1;
|
|
|
|
|
for (var i = 0; i < onLoadCallbacks.length; i++)
|
|
|
|
|
onLoadCallbacks[i]();
|
|
|
|
|
onLoadCallbacks = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleMessage(message) {
|
|
|
|
|
window.handleMessage = function(message) {
|
|
|
|
|
naclMessageIdCallbackMap[message.data.call_id](message.data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function postNaclMessage(message) {
|
|
|
|
|
if (!USE_NACL)
|
|
|
|
|
self.postNaclMessage = function(message) {
|
|
|
|
|
if (!self.USE_NACL)
|
|
|
|
|
throw new Error("Attempted to make NaCL call with !USE_NACL?");
|
|
|
|
|
|
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
@ -410,11 +424,18 @@ function postNaclMessage(message) {
|
|
|
|
|
|
|
|
|
|
common.naclModule.postMessage(message);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
// message_callback(decoded_protobuf) (use decodeMessage(proto))
|
|
|
|
|
var subscribeToPushMessageSemaphore = 0;
|
|
|
|
|
function subscribeToPush(message_callback) {
|
|
|
|
|
//TODO: Some kind of textsecure.init(use_nacl)
|
|
|
|
|
window.textsecure.registerOnLoadFunction = window.textsecure.nacl.registerOnLoadFunction;
|
|
|
|
|
|
|
|
|
|
// message_callback({message: decryptedMessage, pushMessage: server-providedPushMessage})
|
|
|
|
|
window.textsecure.subscribeToPush = function() {
|
|
|
|
|
var subscribeToPushMessageSemaphore = 0;
|
|
|
|
|
return function(message_callback) {
|
|
|
|
|
subscribeToPushMessageSemaphore++;
|
|
|
|
|
if (subscribeToPushMessageSemaphore <= 0)
|
|
|
|
|
return;
|
|
|
|
@ -463,7 +484,7 @@ function subscribeToPush(message_callback) {
|
|
|
|
|
socket.send(JSON.stringify({type: 1, id: message.id}));
|
|
|
|
|
return textsecure.crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) {
|
|
|
|
|
var handleAttachment = function(attachment) {
|
|
|
|
|
return API.getAttachment(attachment.id).then(function(encryptedBin) {
|
|
|
|
|
return textsecure.api.getAttachment(attachment.id).then(function(encryptedBin) {
|
|
|
|
|
return textsecure.crypto.decryptAttachment(encryptedBin, toArrayBuffer(attachment.key)).then(function(decryptedBin) {
|
|
|
|
|
attachment.decrypted = decryptedBin;
|
|
|
|
|
});
|
|
|
|
@ -485,11 +506,13 @@ function subscribeToPush(message_callback) {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
// success_callback(identity_key), error_callback(error_msg)
|
|
|
|
|
function getKeysForNumber(number) {
|
|
|
|
|
return API.getKeysForNumber(number).then(function(response) {
|
|
|
|
|
// sendMessage(numbers = [], message = PushMessageContentProto, callback(success/failure map))
|
|
|
|
|
window.textsecure.sendMessage = function() {
|
|
|
|
|
function getKeysForNumber(number) {
|
|
|
|
|
return textsecure.api.getKeysForNumber(number).then(function(response) {
|
|
|
|
|
for (var i = 0; i < response.length; i++) {
|
|
|
|
|
storage.devices.saveDeviceObject({
|
|
|
|
|
encodedNumber: number + "." + response[i].deviceId,
|
|
|
|
@ -501,11 +524,11 @@ function getKeysForNumber(number) {
|
|
|
|
|
}
|
|
|
|
|
return response[0].identityKey;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// success_callback(server success/failure map), error_callback(error_msg)
|
|
|
|
|
// message == PushMessageContentProto (NOT STRING)
|
|
|
|
|
function sendMessageToDevices(number, deviceObjectList, message, success_callback, error_callback) {
|
|
|
|
|
// success_callback(server success/failure map), error_callback(error_msg)
|
|
|
|
|
// message == PushMessageContentProto (NOT STRING)
|
|
|
|
|
function sendMessageToDevices(number, deviceObjectList, message, success_callback, error_callback) {
|
|
|
|
|
var jsonData = [];
|
|
|
|
|
var relay = undefined;
|
|
|
|
|
var promises = [];
|
|
|
|
@ -537,13 +560,11 @@ function sendMessageToDevices(number, deviceObjectList, message, success_callbac
|
|
|
|
|
for (var i = 0; i < deviceObjectList.length; i++)
|
|
|
|
|
promises[i] = addEncryptionFor(i);
|
|
|
|
|
return Promise.all(promises).then(function() {
|
|
|
|
|
return API.sendMessages(number, jsonData);
|
|
|
|
|
return textsecure.api.sendMessages(number, jsonData);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// callback(success/failure map, see code)
|
|
|
|
|
// message == PushMessageContentProto (NOT STRING)
|
|
|
|
|
function sendMessageToNumbers(numbers, message, callback) {
|
|
|
|
|
return function(numbers, message, callback) {
|
|
|
|
|
var numbersCompleted = 0;
|
|
|
|
|
var errors = [];
|
|
|
|
|
var successfulNumbers = [];
|
|
|
|
@ -588,7 +609,8 @@ function sendMessageToNumbers(numbers, message, callback) {
|
|
|
|
|
} else
|
|
|
|
|
doSendMessage(number, devicesForNumber, message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
function requestIdentityPrivKeyFromMasterDevice(number, identityKey) {
|
|
|
|
|
sendMessageToDevices([storage.devices.getDeviceObject(getNumberFromString(number)) + ".1"],
|
|
|
|
|