Fix initial connection not working (DeviceInfoMessage should not be
encrypted).
This commit is contained in:
parent
11361c83ab
commit
3da35725d4
2 changed files with 32 additions and 10 deletions
|
@ -22,6 +22,16 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi
|
||||||
|
|
||||||
private val Tag: String = "TransferThread"
|
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 =
|
val InStream: InputStream =
|
||||||
try {
|
try {
|
||||||
socket.getInputStream
|
socket.getInputStream
|
||||||
|
@ -46,9 +56,14 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi
|
||||||
while (socket.isConnected) {
|
while (socket.isConnected) {
|
||||||
try {
|
try {
|
||||||
val up = new ScalaMessagePack().createUnpacker(InStream)
|
val up = new ScalaMessagePack().createUnpacker(InStream)
|
||||||
|
val plain = up.readBoolean() match {
|
||||||
|
case MessageEncrypted =>
|
||||||
val encrypted = up.readByteArray()
|
val encrypted = up.readByteArray()
|
||||||
val key = up.readByteArray()
|
val key = up.readByteArray()
|
||||||
val plain = crypto.decrypt(encrypted, key)
|
crypto.decrypt(encrypted, key)
|
||||||
|
case MessageUnencrypted =>
|
||||||
|
up.readByteArray()
|
||||||
|
}
|
||||||
val (message, signature) = Message.read(plain)
|
val (message, signature) = Message.read(plain)
|
||||||
var messageValid = true
|
var messageValid = true
|
||||||
|
|
||||||
|
@ -97,12 +112,19 @@ class TransferThread(device: Device, socket: BluetoothSocket, service: ChatServi
|
||||||
|
|
||||||
def send(message: Message): Unit = {
|
def send(message: Message): Unit = {
|
||||||
try {
|
try {
|
||||||
val sig = crypto.calculateSignature(message)
|
val plain = message.write(crypto.calculateSignature(message))
|
||||||
val plain = message.write(sig)
|
val packer = new ScalaMessagePack().createPacker(OutStream)
|
||||||
|
|
||||||
|
message.messageType match {
|
||||||
|
case Message.Type.Text =>
|
||||||
val (encrypted, key) = crypto.encrypt(message.receiver, plain)
|
val (encrypted, key) = crypto.encrypt(message.receiver, plain)
|
||||||
new ScalaMessagePack().createPacker(OutStream)
|
packer.write(MessageEncrypted)
|
||||||
.write(encrypted)
|
.write(encrypted)
|
||||||
.write(key)
|
.write(key)
|
||||||
|
case Message.Type.DeviceInfo =>
|
||||||
|
packer.write(MessageUnencrypted)
|
||||||
|
.write(plain)
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
case e: IOException => Log.e(Tag, "Failed to write message", e)
|
case e: IOException => Log.e(Tag, "Failed to write message", e)
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ object Message {
|
||||||
*
|
*
|
||||||
* @param messageType One of [[Message.Type]].
|
* @param messageType One of [[Message.Type]].
|
||||||
*/
|
*/
|
||||||
abstract class Message(messageType: Int) {
|
abstract class Message(val messageType: Int) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Device where the message was sent from.
|
* Device where the message was sent from.
|
||||||
|
|
Reference in a new issue