Implement sync protocol changes

Update protobuf definitions and refactor message receive and decrypt
codepath to support new protocol, including various flavors of sync
messages (sent messages, contacts, and groups).

Also cleans up background.js and lets libtextsecure internalize
textsecure.processDecrypted and ensure that it is called before handing
DataMessages off to the application.

The Envelope structure now has a generic content field and a
legacyMessage field for backwards compatibility. We'll send outgoing
messages as legacy messages, and sync messages as "content" while
continuing to support both legacy and non-legacy messages on the receive
side until old clients have a chance to transition.
This commit is contained in:
lilia 2015-06-01 14:08:21 -07:00
parent 757bcd4e50
commit a833d62a71
13 changed files with 756 additions and 379 deletions

View file

@ -125,8 +125,7 @@ window.textsecure.utils = function() {
return self;
}();
var handleAttachment = function(attachment) {
function handleAttachment(attachment) {
function getAttachment() {
return TextSecureServer.getAttachment(attachment.id.toString());
}
@ -143,11 +142,11 @@ var handleAttachment = function(attachment) {
}
return getAttachment().
then(decryptAttachment).
then(updateAttachment);
};
then(decryptAttachment).
then(updateAttachment);
}
textsecure.processDecrypted = function(decrypted, source) {
function processDecrypted(decrypted, source) {
// 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
@ -156,21 +155,11 @@ textsecure.processDecrypted = function(decrypted, source) {
if (decrypted.flags == null)
decrypted.flags = 0;
if (decrypted.sync !== null && textsecure.storage.user.getNumber() != source) {
// Ignore erroneous or malicious sync context from different number
decrypted.sync = null;
}
if ((decrypted.flags & textsecure.protobuf.PushMessageContent.Flags.END_SESSION)
== textsecure.protobuf.PushMessageContent.Flags.END_SESSION) {
if ((decrypted.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION)
== textsecure.protobuf.DataMessage.Flags.END_SESSION) {
decrypted.body = null;
decrypted.attachments = [];
decrypted.group = null;
if (decrypted.sync !== null) {
// We didn't actually close the session - see axolotl_wrapper
// so just throw an error since this message makes no sense
throw new Error("Got a sync END_SESSION message");
}
return Promise.resolve(decrypted);
}
if (decrypted.flags != 0) {
@ -183,7 +172,7 @@ textsecure.processDecrypted = function(decrypted, source) {
decrypted.group.id = getString(decrypted.group.id);
promises.push(textsecure.storage.groups.getNumbers(decrypted.group.id).then(function(existingGroup) {
if (existingGroup === undefined) {
if (decrypted.group.type != textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE) {
if (decrypted.group.type != textsecure.protobuf.GroupContext.Type.UPDATE) {
throw new Error("Got message for unknown group");
}
if (decrypted.group.avatar !== null) {
@ -199,7 +188,7 @@ textsecure.processDecrypted = function(decrypted, source) {
}
switch(decrypted.group.type) {
case textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE:
case textsecure.protobuf.GroupContext.Type.UPDATE:
if (decrypted.group.avatar !== null)
promises.push(handleAttachment(decrypted.group.avatar));
@ -226,11 +215,11 @@ textsecure.processDecrypted = function(decrypted, source) {
});
break;
case textsecure.protobuf.PushMessageContent.GroupContext.Type.QUIT:
case textsecure.protobuf.GroupContext.Type.QUIT:
decrypted.body = null;
decrypted.attachments = [];
return textsecure.storage.groups.removeNumber(decrypted.group.id, source);
case textsecure.protobuf.PushMessageContent.GroupContext.Type.DELIVER:
case textsecure.protobuf.GroupContext.Type.DELIVER:
decrypted.group.name = null;
decrypted.group.members = [];
decrypted.group.avatar = null;
@ -249,4 +238,4 @@ textsecure.processDecrypted = function(decrypted, source) {
return Promise.all(promises).then(function() {
return decrypted;
});
};
}