From a0f95c033a1030e70003897090de9bac0cab240c Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 5 Jun 2019 09:29:18 +1000 Subject: [PATCH] Pass PreKeyBundleStore into message sender. --- .../SignalCommunicationModule.java | 5 ++++ .../securesms/jobs/PushDecryptJob.java | 10 +++++++- .../loki/LokiPreKeyBundleDatabase.kt | 5 ++-- .../loki/LokiPreKeyBundleStoreImpl.kt | 24 +++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleStoreImpl.kt diff --git a/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java b/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java index 9fecccd64f..7d21962283 100644 --- a/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java +++ b/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java @@ -41,6 +41,7 @@ import org.thoughtcrime.securesms.jobs.RotateCertificateJob; import org.thoughtcrime.securesms.jobs.RotateSignedPreKeyJob; import org.thoughtcrime.securesms.jobs.SendDeliveryReceiptJob; import org.thoughtcrime.securesms.jobs.SendReadReceiptJob; +import org.thoughtcrime.securesms.loki.LokiPreKeyBundleStoreImpl; import org.thoughtcrime.securesms.preferences.AppProtectionPreferenceFragment; import org.thoughtcrime.securesms.push.SecurityEventListener; import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess; @@ -134,6 +135,10 @@ public class SignalCommunicationModule { Optional.fromNullable(IncomingMessageObserver.getPipe()), Optional.fromNullable(IncomingMessageObserver.getUnidentifiedPipe()), Optional.of(new SecurityEventListener(context))); + + // LOKI - Set the prekey bundle store + // This is something that we MUST have otherwise our loki logic will fail + this.messageSender.setPreKeyBundleStore(new LokiPreKeyBundleStoreImpl(context)); } else { this.messageSender.setMessagePipe(IncomingMessageObserver.getPipe(), IncomingMessageObserver.getUnidentifiedPipe()); this.messageSender.setIsMultiDevice(TextSecurePreferences.isMultiDevice(context)); diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index ebfc17d8a7..83ddabbc08 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -61,6 +61,7 @@ import org.thoughtcrime.securesms.linkpreview.Link; import org.thoughtcrime.securesms.linkpreview.LinkPreview; import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil; import org.thoughtcrime.securesms.logging.Log; +import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase; import org.thoughtcrime.securesms.mms.IncomingMediaMessage; import org.thoughtcrime.securesms.mms.MmsException; import org.thoughtcrime.securesms.mms.OutgoingExpirationUpdateMessage; @@ -81,6 +82,7 @@ import org.thoughtcrime.securesms.sms.OutgoingTextMessage; import org.thoughtcrime.securesms.util.GroupUtil; import org.thoughtcrime.securesms.util.IdentityUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.whispersystems.libsignal.state.PreKeyBundle; import org.whispersystems.libsignal.state.SessionStore; import org.whispersystems.libsignal.state.SignalProtocolStore; import org.whispersystems.libsignal.util.guava.Optional; @@ -243,7 +245,13 @@ public class PushDecryptJob extends BaseJob { if (content.lokiMessage.isPresent()) { LokiServiceMessage lokiMessage = content.lokiMessage.get(); if (lokiMessage.getPreKeyBundleMessage() != null) { - // TODO: Loki - Handle pre key bundle + Log.i(TAG, "[Loki] Received a prekey bundle from: " + envelope.getSource()); + int registrationId = TextSecurePreferences.getLocalRegistrationId(context); + if (registrationId > 0) { + LokiPreKeyBundleDatabase preKeyBundleDatabase = DatabaseFactory.getLokiPreKeyBundleDatabase(context); + PreKeyBundle preKeyBundle = lokiMessage.getPreKeyBundleMessage().getPreKeyBundle(registrationId); + preKeyBundleDatabase.setPreKeyBundle(envelope.getSource(), preKeyBundle); + } } if (lokiMessage.getAddressMessage() != null) { diff --git a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt index f6de9e0a2f..28480a90c0 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt @@ -13,6 +13,7 @@ import org.thoughtcrime.securesms.util.TextSecurePreferences import org.whispersystems.libsignal.IdentityKey import org.whispersystems.libsignal.ecc.Curve import org.whispersystems.libsignal.state.PreKeyBundle +import org.whispersystems.signalservice.api.push.SignalServiceAddress class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { @@ -57,9 +58,7 @@ class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : val registrationId = TextSecurePreferences.getLocalRegistrationId(context) if (registrationId == 0) return null - // TODO: This is the primary device id, we may want to change it to support multiple devices? - val deviceId = 1 - + val deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID return PreKeyBundle(registrationId, deviceId,preKeyRecord.id, preKeyRecord.keyPair.publicKey, signedPreKey.id, signedPreKey.keyPair.publicKey, signedPreKey.signature, identityKeyPair.publicKey) } diff --git a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleStoreImpl.kt b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleStoreImpl.kt new file mode 100644 index 0000000000..1bba0760a8 --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleStoreImpl.kt @@ -0,0 +1,24 @@ +package org.thoughtcrime.securesms.loki + +import android.content.Context +import org.thoughtcrime.securesms.database.DatabaseFactory +import org.whispersystems.libsignal.state.PreKeyBundle +import org.whispersystems.signalservice.loki.utilities.LokiPreKeyBundleStore + +class LokiPreKeyBundleStoreImpl(val context: Context): LokiPreKeyBundleStore { + companion object { + val FILE_LOCK = Object() + } + + override fun getPreKeyBundle(pubKey: String): PreKeyBundle? { + synchronized (FILE_LOCK) { + return DatabaseFactory.getLokiPreKeyBundleDatabase(context).getPreKeyBundle(pubKey) + } + } + + override fun removePreKeyBundle(pubKey: String) { + synchronized (FILE_LOCK) { + DatabaseFactory.getLokiPreKeyBundleDatabase(context).removePreKeyBundle(pubKey) + } + } +} \ No newline at end of file