From 3357672516ac25c3e1da0de85350e28fb7ee15cc Mon Sep 17 00:00:00 2001 From: Brice Date: Thu, 14 Jan 2021 13:20:18 +1100 Subject: [PATCH] MessaageSender: use of session protocol for closed groups --- .../org/thoughtcrime/securesms/database/Storage.kt | 4 ++++ .../session/libsession/messaging/StorageProtocol.kt | 1 + .../messaging/sending_receiving/MessageSender.kt | 13 ++++++++++--- .../sending_receiving/MessageSenderEncryption.kt | 4 ++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt index 160f25fa58..6b5e6c9b47 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -272,6 +272,10 @@ class Storage(val context: Context): StorageProtocol { TODO("Not yet implemented") } + override fun getLatestClosedGroupEncryptionKeyPair(groupPublicKey: String): ECKeyPair { + TODO("Not yet implemented") + } + override fun setProfileSharing(address: Address, value: Boolean) { val recipient = Recipient.from(context, address, false) DatabaseFactory.getRecipientDatabase(context).setProfileSharing(recipient, value) diff --git a/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt b/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt index 52c1cbf090..998a84390c 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/StorageProtocol.kt @@ -104,6 +104,7 @@ interface StorageProtocol { members: Collection, admins: Collection, threadID: Long) fun isClosedGroup(publicKey: String): Boolean //TODO fun getClosedGroupEncryptionKeyPairs(groupPublicKey: String): MutableList //TODO + fun getLatestClosedGroupEncryptionKeyPair(groupPublicKey: String): ECKeyPair //TODO // Settings fun setProfileSharing(address: Address, value: Boolean) diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt index 47c564be13..19af7d570b 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt @@ -23,6 +23,7 @@ import org.session.libsignal.service.api.messages.SignalServiceAttachment import org.session.libsignal.service.internal.push.SignalServiceProtos import org.session.libsignal.service.internal.util.Base64 import org.session.libsignal.service.loki.api.crypto.ProofOfWork +import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey object MessageSender { @@ -32,7 +33,10 @@ object MessageSender { object InvalidMessage : Error("Invalid message.") object ProtoConversionFailed : Error("Couldn't convert message to proto.") object ProofOfWorkCalculationFailed : Error("Proof of work calculation failed.") - object NoUserPublicKey : Error("Couldn't find user key pair.") + object NoUserX25519KeyPair : Error("Couldn't find user X25519 key pair.") + object NoUserED25519KeyPair : Error("Couldn't find user ED25519 key pair.") + object SigningFailed : Error("Couldn't sign message.") + object EncryptionFailed : Error("Couldn't encrypt message.") // Closed groups object NoThread : Error("Couldn't find a thread associated with the given group public key.") @@ -71,7 +75,7 @@ object MessageSender { var snodeMessage: SnodeMessage? = null // Set the timestamp, sender and recipient message.sentTimestamp ?: run { message.sentTimestamp = System.currentTimeMillis() } /* Visible messages will already have their sent timestamp set */ - message.sender = storage.getUserPublicKey() + message.sender = userPublicKey try { when (destination) { is Destination.Contact -> message.recipient = destination.publicKey @@ -117,7 +121,10 @@ object MessageSender { val ciphertext: ByteArray when (destination) { is Destination.Contact -> ciphertext = MessageSenderEncryption.encryptWithSessionProtocol(plaintext, destination.publicKey) - is Destination.ClosedGroup -> ciphertext = MessageSenderEncryption.encryptWithSharedSenderKeys(plaintext, destination.groupPublicKey) + is Destination.ClosedGroup -> { + val encryptionKeyPair = MessagingConfiguration.shared.storage.getLatestClosedGroupEncryptionKeyPair(destination.groupPublicKey) + ciphertext = MessageSenderEncryption.encryptWithSessionProtocol(plaintext, encryptionKeyPair.hexEncodedPublicKey) + } is Destination.OpenGroup -> throw preconditionFailure } // Wrap the result diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderEncryption.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderEncryption.kt index 2b0f390a45..7ee8a47f8a 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderEncryption.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderEncryption.kt @@ -38,7 +38,7 @@ object MessageSenderEncryption { return MessagingConfiguration.shared.sessionProtocol.encrypt(plaintext, recipientPublicKey) } - internal fun encryptWithSharedSenderKeys(plaintext: ByteArray, groupPublicKey: String): ByteArray { + /*internal fun encryptWithSharedSenderKeys(plaintext: ByteArray, groupPublicKey: String): ByteArray { // 1. ) Encrypt the data with the user's sender key val userPublicKey = MessagingConfiguration.shared.storage.getUserPublicKey() ?: throw Error.NoUserPublicKey val ciphertextAndKeyIndex = SharedSenderKeysImplementation.shared.encrypt(plaintext, groupPublicKey, userPublicKey) @@ -52,5 +52,5 @@ object MessageSenderEncryption { .setCiphertext(ByteString.copyFrom(intermediate.ciphertext)) .setEphemeralPublicKey(ByteString.copyFrom(intermediate.ephemeralPublicKey)) .build().toByteArray() - } + }*/ } \ No newline at end of file