From 3da35725d4b733796b7ca2a57dec16b362d7d26d Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 13 Nov 2014 21:31:57 +0200 Subject: [PATCH] Fix initial connection not working (DeviceInfoMessage should not be encrypted). --- .../ensichat/bluetooth/TransferThread.scala | 40 ++++++++++++++----- .../nutomic/ensichat/messages/Message.scala | 2 +- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala index a020111..05f35f2 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/TransferThread.scala @@ -22,6 +22,16 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi private val Tag: String = "TransferThread" + /** + * First value in a message, indicates that content is not encrypted. + */ + private val MessageUnencrypted = false + + /** + * First value in a message, indicates that content is encrypted. + */ + private val MessageEncrypted = true + val InStream: InputStream = try { socket.getInputStream @@ -46,9 +56,14 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi while (socket.isConnected) { try { val up = new ScalaMessagePack().createUnpacker(InStream) - val encrypted = up.readByteArray() - val key = up.readByteArray() - val plain = crypto.decrypt(encrypted, key) + val plain = up.readBoolean() match { + case MessageEncrypted => + val encrypted = up.readByteArray() + val key = up.readByteArray() + crypto.decrypt(encrypted, key) + case MessageUnencrypted => + up.readByteArray() + } val (message, signature) = Message.read(plain) var messageValid = true @@ -97,12 +112,19 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi def send(message: Message): Unit = { try { - val sig = crypto.calculateSignature(message) - val plain = message.write(sig) - val (encrypted, key) = crypto.encrypt(message.receiver, plain) - new ScalaMessagePack().createPacker(OutStream) - .write(encrypted) - .write(key) + val plain = message.write(crypto.calculateSignature(message)) + val packer = new ScalaMessagePack().createPacker(OutStream) + + message.messageType match { + case Message.Type.Text => + val (encrypted, key) = crypto.encrypt(message.receiver, plain) + packer.write(MessageEncrypted) + .write(encrypted) + .write(key) + case Message.Type.DeviceInfo => + packer.write(MessageUnencrypted) + .write(plain) + } } catch { case e: IOException => Log.e(Tag, "Failed to write message", e) } diff --git a/app/src/main/scala/com/nutomic/ensichat/messages/Message.scala b/app/src/main/scala/com/nutomic/ensichat/messages/Message.scala index a012ff2..409f496 100644 --- a/app/src/main/scala/com/nutomic/ensichat/messages/Message.scala +++ b/app/src/main/scala/com/nutomic/ensichat/messages/Message.scala @@ -52,7 +52,7 @@ object Message { * * @param messageType One of [[Message.Type]]. */ -abstract class Message(messageType: Int) { +abstract class Message(val messageType: Int) { /** * Device where the message was sent from.