2014-10-20 00:53:17 +00:00
|
|
|
/* vim: ts=4:sw=4:expandtab
|
2014-05-14 18:58:12 +00:00
|
|
|
*
|
2014-05-04 06:34:13 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-05-17 05:53:58 +00:00
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
|
2014-01-12 07:32:13 +00:00
|
|
|
/*********************************
|
|
|
|
*** Type conversion utilities ***
|
|
|
|
*********************************/
|
2014-01-15 07:46:05 +00:00
|
|
|
// Strings/arrays
|
2014-03-06 21:44:59 +00:00
|
|
|
//TODO: Throw all this shit in favor of consistent types
|
2014-07-21 02:06:04 +00:00
|
|
|
//TODO: Namespace
|
2014-01-12 14:07:13 +00:00
|
|
|
var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__;
|
2014-01-22 06:23:41 +00:00
|
|
|
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
2014-03-06 18:18:11 +00:00
|
|
|
var StaticUint8ArrayProto = new Uint8Array().__proto__;
|
2014-01-12 07:32:13 +00:00
|
|
|
function getString(thing) {
|
2014-10-20 00:53:17 +00:00
|
|
|
if (thing === Object(thing)) {
|
|
|
|
if (thing.__proto__ == StaticUint8ArrayProto)
|
|
|
|
return String.fromCharCode.apply(null, thing);
|
|
|
|
if (thing.__proto__ == StaticArrayBufferProto)
|
|
|
|
return getString(new Uint8Array(thing));
|
|
|
|
if (thing.__proto__ == StaticByteBufferProto)
|
|
|
|
return thing.toString("binary");
|
|
|
|
}
|
|
|
|
return thing;
|
2014-01-12 07:32:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 01:31:15 +00:00
|
|
|
function getStringable(thing) {
|
2014-10-20 00:53:17 +00:00
|
|
|
return (typeof thing == "string" || typeof thing == "number" || typeof thing == "boolean" ||
|
|
|
|
(thing === Object(thing) &&
|
|
|
|
(thing.__proto__ == StaticArrayBufferProto ||
|
|
|
|
thing.__proto__ == StaticUint8ArrayProto ||
|
2014-11-10 04:03:08 +00:00
|
|
|
thing.__proto__ == StaticByteBufferProto)));
|
2014-03-05 01:31:15 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 05:49:13 +00:00
|
|
|
function isEqual(a, b, mayBeShort) {
|
2014-10-20 00:53:17 +00:00
|
|
|
// TODO: Special-case arraybuffers, etc
|
|
|
|
if (a === undefined || b === undefined)
|
|
|
|
return false;
|
|
|
|
a = getString(a);
|
|
|
|
b = getString(b);
|
|
|
|
var maxLength = mayBeShort ? Math.min(a.length, b.length) : Math.max(a.length, b.length);
|
|
|
|
if (maxLength < 5)
|
|
|
|
throw new Error("a/b compare too short");
|
|
|
|
return a.substring(0, Math.min(maxLength, a.length)) == b.substring(0, Math.min(maxLength, b.length));
|
2014-07-21 02:06:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 01:31:15 +00:00
|
|
|
function toArrayBuffer(thing) {
|
2014-10-20 00:53:17 +00:00
|
|
|
//TODO: Optimize this for specific cases
|
|
|
|
if (thing === undefined)
|
|
|
|
return undefined;
|
|
|
|
if (thing === Object(thing) && thing.__proto__ == StaticArrayBufferProto)
|
|
|
|
return thing;
|
|
|
|
|
|
|
|
if (thing instanceof Array) {
|
|
|
|
// Assuming Uint16Array from curve25519
|
|
|
|
var res = new ArrayBuffer(thing.length * 2);
|
|
|
|
var uint = new Uint16Array(res);
|
|
|
|
for (var i = 0; i < thing.length; i++)
|
|
|
|
uint[i] = thing[i];
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!getStringable(thing))
|
|
|
|
throw new Error("Tried to convert a non-stringable thing of type " + typeof thing + " to an array buffer");
|
|
|
|
var str = getString(thing);
|
|
|
|
var res = new ArrayBuffer(str.length);
|
|
|
|
var uint = new Uint8Array(res);
|
|
|
|
for (var i = 0; i < str.length; i++)
|
|
|
|
uint[i] = str.charCodeAt(i);
|
|
|
|
return res;
|
2014-03-05 01:31:15 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 22:09:07 +00:00
|
|
|
// Number formatting utils
|
|
|
|
window.textsecure.utils = function() {
|
2014-10-20 00:53:17 +00:00
|
|
|
var self = {};
|
|
|
|
self.unencodeNumber = function(number) {
|
|
|
|
return number.split(".");
|
|
|
|
};
|
|
|
|
|
2015-01-13 21:33:11 +00:00
|
|
|
self.isNumberSane = function(number) {
|
|
|
|
return number[0] == "+" &&
|
|
|
|
/^[0-9]+$/.test(number.substring(1));
|
|
|
|
}
|
|
|
|
|
2014-10-20 00:53:17 +00:00
|
|
|
/**************************
|
|
|
|
*** JSON'ing Utilities ***
|
|
|
|
**************************/
|
|
|
|
function ensureStringed(thing) {
|
|
|
|
if (getStringable(thing))
|
|
|
|
return getString(thing);
|
|
|
|
else if (thing instanceof Array) {
|
|
|
|
var res = [];
|
|
|
|
for (var i = 0; i < thing.length; i++)
|
|
|
|
res[i] = ensureStringed(thing[i]);
|
|
|
|
return res;
|
|
|
|
} else if (thing === Object(thing)) {
|
|
|
|
var res = {};
|
|
|
|
for (var key in thing)
|
|
|
|
res[key] = ensureStringed(thing[key]);
|
|
|
|
return res;
|
2015-04-01 20:08:09 +00:00
|
|
|
} else if (thing === null) {
|
|
|
|
return null;
|
2014-10-20 00:53:17 +00:00
|
|
|
}
|
|
|
|
throw new Error("unsure of how to jsonify object of type " + typeof thing);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jsonThing = function(thing) {
|
|
|
|
return JSON.stringify(ensureStringed(thing));
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2014-05-25 22:45:22 +00:00
|
|
|
}();
|
|
|
|
|
2015-06-01 21:08:21 +00:00
|
|
|
function handleAttachment(attachment) {
|
2014-12-20 00:49:18 +00:00
|
|
|
function getAttachment() {
|
2015-04-30 21:10:04 +00:00
|
|
|
return TextSecureServer.getAttachment(attachment.id.toString());
|
2014-12-20 00:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function decryptAttachment(encrypted) {
|
2015-01-16 04:11:08 +00:00
|
|
|
return textsecure.crypto.decryptAttachment(
|
2014-12-20 00:49:18 +00:00
|
|
|
encrypted,
|
|
|
|
attachment.key.toArrayBuffer()
|
|
|
|
);
|
|
|
|
}
|
2014-10-20 00:53:17 +00:00
|
|
|
|
2014-12-20 00:49:18 +00:00
|
|
|
function updateAttachment(data) {
|
|
|
|
attachment.data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getAttachment().
|
2015-06-01 21:08:21 +00:00
|
|
|
then(decryptAttachment).
|
|
|
|
then(updateAttachment);
|
|
|
|
}
|
2014-10-20 00:53:17 +00:00
|
|
|
|
2015-06-01 21:08:21 +00:00
|
|
|
function processDecrypted(decrypted, source) {
|
2014-12-20 00:49:18 +00:00
|
|
|
|
|
|
|
// Now that its decrypted, validate the message and clean it up for consumer processing
|
|
|
|
// Note that messages may (generally) only perform one action and we ignore remaining fields
|
|
|
|
// after the first action.
|
|
|
|
|
|
|
|
if (decrypted.flags == null)
|
|
|
|
decrypted.flags = 0;
|
|
|
|
|
2015-06-01 21:08:21 +00:00
|
|
|
if ((decrypted.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION)
|
|
|
|
== textsecure.protobuf.DataMessage.Flags.END_SESSION) {
|
2015-02-26 01:42:47 +00:00
|
|
|
decrypted.body = null;
|
|
|
|
decrypted.attachments = [];
|
|
|
|
decrypted.group = null;
|
2015-02-14 00:23:06 +00:00
|
|
|
return Promise.resolve(decrypted);
|
2015-02-26 01:42:47 +00:00
|
|
|
}
|
2014-12-20 00:49:18 +00:00
|
|
|
if (decrypted.flags != 0) {
|
|
|
|
throw new Error("Unknown flags in message");
|
|
|
|
}
|
|
|
|
|
|
|
|
var promises = [];
|
|
|
|
|
|
|
|
if (decrypted.group !== null) {
|
|
|
|
decrypted.group.id = getString(decrypted.group.id);
|
2015-06-26 19:23:57 +00:00
|
|
|
|
|
|
|
if (decrypted.group.type == textsecure.protobuf.GroupContext.Type.UPDATE) {
|
|
|
|
if (decrypted.group.avatar !== null) {
|
|
|
|
promises.push(handleAttachment(decrypted.group.avatar));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 20:11:12 +00:00
|
|
|
promises.push(textsecure.storage.groups.getNumbers(decrypted.group.id).then(function(existingGroup) {
|
|
|
|
if (existingGroup === undefined) {
|
2015-06-01 21:08:21 +00:00
|
|
|
if (decrypted.group.type != textsecure.protobuf.GroupContext.Type.UPDATE) {
|
2015-05-06 20:11:12 +00:00
|
|
|
throw new Error("Got message for unknown group");
|
|
|
|
}
|
|
|
|
return textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
|
|
|
|
} else {
|
|
|
|
var fromIndex = existingGroup.indexOf(source);
|
2014-12-20 00:49:18 +00:00
|
|
|
|
2015-05-06 20:11:12 +00:00
|
|
|
if (fromIndex < 0) {
|
|
|
|
//TODO: This could be indication of a race...
|
|
|
|
throw new Error("Sender was not a member of the group they were sending from");
|
2014-10-20 00:53:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 20:11:12 +00:00
|
|
|
switch(decrypted.group.type) {
|
2015-06-01 21:08:21 +00:00
|
|
|
case textsecure.protobuf.GroupContext.Type.UPDATE:
|
2015-06-23 00:55:15 +00:00
|
|
|
return textsecure.storage.groups.updateNumbers(
|
|
|
|
decrypted.group.id, decrypted.group.members
|
|
|
|
).then(function(added) {
|
|
|
|
decrypted.group.added = added;
|
|
|
|
|
|
|
|
if (decrypted.group.avatar === null &&
|
|
|
|
decrypted.group.added.length == 0 &&
|
|
|
|
decrypted.group.name === null) {
|
2015-05-06 20:11:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypted.body = null;
|
|
|
|
decrypted.attachments = [];
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
2015-06-01 21:08:21 +00:00
|
|
|
case textsecure.protobuf.GroupContext.Type.QUIT:
|
2015-05-06 20:11:12 +00:00
|
|
|
decrypted.body = null;
|
|
|
|
decrypted.attachments = [];
|
|
|
|
return textsecure.storage.groups.removeNumber(decrypted.group.id, source);
|
2015-06-01 21:08:21 +00:00
|
|
|
case textsecure.protobuf.GroupContext.Type.DELIVER:
|
2015-05-06 20:11:12 +00:00
|
|
|
decrypted.group.name = null;
|
|
|
|
decrypted.group.members = [];
|
|
|
|
decrypted.group.avatar = null;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Unknown group message type");
|
|
|
|
}
|
2014-12-20 00:49:18 +00:00
|
|
|
}
|
2015-05-06 20:11:12 +00:00
|
|
|
}));
|
2014-12-20 00:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i in decrypted.attachments) {
|
|
|
|
promises.push(handleAttachment(decrypted.attachments[i]));
|
|
|
|
}
|
|
|
|
return Promise.all(promises).then(function() {
|
|
|
|
return decrypted;
|
2014-11-28 06:03:42 +00:00
|
|
|
});
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|