From 2d12a33ead6a795d7adea469cbe163a324df6495 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 26 May 2014 01:48:41 +0200 Subject: [PATCH] Fix decoding/protos/tests --- js/crypto.js | 9 +++++---- js/helpers.js | 10 +++++----- js/test.js | 24 ++++++++++++------------ 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/js/crypto.js b/js/crypto.js index 8016bea7a..c6aaeccc4 100644 --- a/js/crypto.js +++ b/js/crypto.js @@ -603,14 +603,15 @@ window.textsecure.crypto = new function() { case 0: //TYPE_MESSAGE_PLAINTEXT return Promise.resolve(textsecure.protos.decodePushMessageContentProtobuf(getString(proto.message))); case 1: //TYPE_MESSAGE_CIPHERTEXT - return decryptWhisperMessage(proto.source + "." + proto.sourceDevice, getString(proto.message)); + var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); + return decryptWhisperMessage(from, getString(proto.message)); case 3: //TYPE_MESSAGE_PREKEY_BUNDLE if (proto.message.readUint8() != (2 << 4 | 2)) throw new Error("Bad version byte"); + var from = proto.source + "." + (proto.sourceDevice == null ? 0 : proto.sourceDevice); var preKeyProto = textsecure.protos.decodePreKeyWhisperMessageProtobuf(getString(proto.message)); -//XXX: proto.sourceDevice == jsNumber??? (and above) - return initSessionFromPreKeyWhisperMessage(proto.source + "." + proto.sourceDevice, preKeyProto).then(function(sessions) { - return decryptWhisperMessage(proto.source, getString(preKeyProto.message), sessions[0]).then(function(result) { + return initSessionFromPreKeyWhisperMessage(from, preKeyProto).then(function(sessions) { + return decryptWhisperMessage(from, getString(preKeyProto.message), sessions[0]).then(function(result) { if (sessions[1] !== undefined) crypto_storage.saveSession(proto.source, sessions[1]); return result; diff --git a/js/helpers.js b/js/helpers.js index c755d088c..4e5381ab1 100644 --- a/js/helpers.js +++ b/js/helpers.js @@ -164,27 +164,27 @@ window.textsecure.protos = function() { self.IncomingPushMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/IncomingPushMessageSignal.proto").build("textsecure.IncomingPushMessageSignal"); self.decodeIncomingPushMessageProtobuf = function(string) { - return IncomingPushMessageProtobuf.decode(btoa(string)); + return self.IncomingPushMessageProtobuf.decode(btoa(string)); } self.PushMessageContentProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/IncomingPushMessageSignal.proto").build("textsecure.PushMessageContent"); self.decodePushMessageContentProtobuf = function(string) { - return PushMessageContentProtobuf.decode(btoa(string)); + return self.PushMessageContentProtobuf.decode(btoa(string)); } self.WhisperMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/WhisperTextProtocol.proto").build("textsecure.WhisperMessage"); self.decodeWhisperMessageProtobuf = function(string) { - return WhisperMessageProtobuf.decode(btoa(string)); + return self.WhisperMessageProtobuf.decode(btoa(string)); } self.PreKeyWhisperMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/WhisperTextProtocol.proto").build("textsecure.PreKeyWhisperMessage"); self.decodePreKeyWhisperMessageProtobuf = function(string) { - return PreKeyWhisperMessageProtobuf.decode(btoa(string)); + return self.PreKeyWhisperMessageProtobuf.decode(btoa(string)); } self.KeyExchangeMessageProtobuf = dcodeIO.ProtoBuf.loadProtoFile("protos/WhisperTextProtocol.proto").build("textsecure.KeyExchangeMessage"); self.decodeKeyExchangeMessageProtobuf = function(string) { - return KeyExchangeMessageProtobuf.decode(btoa(string)); + return self.KeyExchangeMessageProtobuf.decode(btoa(string)); } return self; diff --git a/js/test.js b/js/test.js index 2a4faff26..180890058 100644 --- a/js/test.js +++ b/js/test.js @@ -118,8 +118,8 @@ textsecure.registerOnLoadFunction(function() { source: "+19999999999", timestamp: 42, message: text_message.encode() }; return textsecure.crypto.handleIncomingPushMessageProto(server_message).then(function(message) { - return (message.message.body == text_message.body && - message.message.attachments.length == text_message.attachments.length && + return (message.body == text_message.body && + message.attachments.length == text_message.attachments.length && text_message.attachments.length == 0); }); }, 'Unencrypted PushMessageProto "decrypt"', true); @@ -338,10 +338,10 @@ textsecure.registerOnLoadFunction(function() { var message = new textsecure.protos.IncomingPushMessageProtobuf(); message.type = data.type; - message.source = remoteDevice.encodedNumber; + message.source = textsecure.utils.unencodeNumber(remoteDevice.encodedNumber); message.message = data.message; return textsecure.crypto.handleIncomingPushMessageProto(textsecure.protos.decodeIncomingPushMessageProtobuf(getString(message.encode()))).then(function(res) { - return res.message.body == data.expectedSmsText; + return res.body == data.expectedSmsText; }); } @@ -366,7 +366,7 @@ textsecure.registerOnLoadFunction(function() { textsecure.storage.putUnencrypted("registrationId", data.registrationId); } - var message = new PushMessageContentProtobuf(); + var message = new textsecure.protos.PushMessageContentProtobuf(); message.body = data.smsText; return textsecure.crypto.encryptMessageFor(remoteDevice, message).then(function(res) { @@ -405,7 +405,7 @@ textsecure.registerOnLoadFunction(function() { } TEST(function() { - return axolotlTestVectors(axolotlTwoPartyTestVectorsAlice, { encodedNumber: "BOB" }); + return axolotlTestVectors(axolotlTwoPartyTestVectorsAlice, { encodedNumber: "BOB.0" }); }, "Standard Axolotl Test Vectors as Alice", true); TEST(function() { @@ -414,11 +414,11 @@ textsecure.registerOnLoadFunction(function() { axolotlTwoPartyTestVectorsAlice[2][1].newEphemeralKey = t.newEphemeralKey; axolotlTwoPartyTestVectorsAlice[3][1] = t; delete axolotlTwoPartyTestVectorsAlice[3][1]['newEphemeralKey']; - return axolotlTestVectors(axolotlTwoPartyTestVectorsAlice, { encodedNumber: "BOB" }); + return axolotlTestVectors(axolotlTwoPartyTestVectorsAlice, { encodedNumber: "BOB.0" }); }, "Shuffled Axolotl Test Vectors as Alice", true); TEST(function() { - return axolotlTestVectors(axolotlTwoPartyTestVectorsBob, { encodedNumber: "ALICE" }); + return axolotlTestVectors(axolotlTwoPartyTestVectorsBob, { encodedNumber: "ALICE.0" }); }, "Standard Axolotl Test Vectors as Bob", true); TEST(function() { @@ -440,7 +440,7 @@ textsecure.registerOnLoadFunction(function() { v[0][1].newEphemeralKey = orig[0][1].newEphemeralKey; v[1][1] = { message: orig[0][1].message, type: orig[0][1].type, expectedSmsText: orig[0][1].expectedSmsText }; - return axolotlTestVectors(v, { encodedNumber: "ALICE" }); + return axolotlTestVectors(v, { encodedNumber: "ALICE.0" }); }, "Shuffled Axolotl Test Vectors as Bob I", true); TEST(function() { @@ -457,7 +457,7 @@ textsecure.registerOnLoadFunction(function() { v[1] = orig[2]; v[2] = orig[1]; - return axolotlTestVectors(v, { encodedNumber: "ALICE" }); + return axolotlTestVectors(v, { encodedNumber: "ALICE.0" }); }, "Shuffled Axolotl Test Vectors as Bob II", true); TEST(function() { @@ -476,7 +476,7 @@ textsecure.registerOnLoadFunction(function() { v[2] = orig[3]; v[3] = orig[4]; - return axolotlTestVectors(v, { encodedNumber: "ALICE" }); + return axolotlTestVectors(v, { encodedNumber: "ALICE.0" }); }, "Shuffled Axolotl Test Vectors as Bob III", true); TEST(function() { @@ -506,7 +506,7 @@ textsecure.registerOnLoadFunction(function() { v[2] = orig[3]; v[3] = orig[4]; - return axolotlTestVectors(v, { encodedNumber: "ALICE" }); + return axolotlTestVectors(v, { encodedNumber: "ALICE.0" }); }, "Shuffled Axolotl Test Vectors as Bob IV", true); TEST(function() {