From 1a013e9a19168fd137b148f1b4ad8a14d4362bea Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 21 Aug 2020 14:23:23 +1000 Subject: [PATCH] Patch bad mac issues --- .../conversation/ConversationActivity.java | 2 +- .../securesms/jobs/PushDecryptJob.java | 25 ++++++++++++++++--- .../protocol/SessionManagementProtocol.kt | 8 ++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java index e6cbd46077..5beff5770f 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -446,7 +446,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity }); sessionRestoreBannerView.setOnRestore(() -> { - SessionManagementProtocol.startSessionReset(this, recipient, threadId); + SessionManagementProtocol.startSessionReset(this, recipient.getAddress().serialize()); updateSessionRestoreBanner(); return Unit.INSTANCE; }); diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 52bfb5541f..993fb49d96 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -98,6 +98,7 @@ import org.thoughtcrime.securesms.util.Hex; import org.thoughtcrime.securesms.util.IdentityUtil; import org.thoughtcrime.securesms.util.MediaUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.whispersystems.libsignal.InvalidMessageException; import org.whispersystems.libsignal.loki.SessionResetProtocol; import org.whispersystems.libsignal.state.SignalProtocolStore; import org.whispersystems.libsignal.util.guava.Optional; @@ -354,14 +355,14 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } catch (ProtocolInvalidMessageException e) { Log.w(TAG, e); if (!isPushNotification) { // This can be triggered if a PN encrypted with an old session comes in after the user performed a session reset - handleCorruptMessage(e.getSender(), e.getSenderDevice(), envelope.getTimestamp(), smsMessageId); + handleCorruptMessage(e.getSender(), e.getSenderDevice(), envelope.getTimestamp(), smsMessageId, e); } } catch (ProtocolInvalidKeyIdException | ProtocolInvalidKeyException | ProtocolUntrustedIdentityException e) { Log.w(TAG, e); - handleCorruptMessage(e.getSender(), e.getSenderDevice(), envelope.getTimestamp(), smsMessageId); + handleCorruptMessage(e.getSender(), e.getSenderDevice(), envelope.getTimestamp(), smsMessageId, e); } catch (StorageFailedException e) { Log.w(TAG, e); - handleCorruptMessage(e.getSender(), e.getSenderDevice(), envelope.getTimestamp(), smsMessageId); + handleCorruptMessage(e.getSender(), e.getSenderDevice(), envelope.getTimestamp(), smsMessageId, e); } catch (ProtocolNoSessionException e) { Log.w(TAG, e); handleNoSessionMessage(e.getSender(), e.getSenderDevice(), envelope.getTimestamp(), smsMessageId); @@ -1069,7 +1070,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } private void handleCorruptMessage(@NonNull String sender, int senderDevice, long timestamp, - @NonNull Optional smsMessageId) + @NonNull Optional smsMessageId, @NonNull Throwable e) { SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context); @@ -1083,6 +1084,22 @@ public class PushDecryptJob extends BaseJob implements InjectableType { } else { smsDatabase.markAsDecryptFailed(smsMessageId.get()); } + + // FIXME: This is a temporary patch for bad mac issues. At least with this people will be able to message again. We have to figure out the root cause of the issue though. + if (e.getCause() != null) { + Throwable e2 = e.getCause(); + if (e2.getCause() != null) { + Throwable e3 = e2.getCause(); + if (e3 instanceof InvalidMessageException) { + String message = e3.getMessage(); + if (message != null && message.startsWith("Bad Mac!")) { + SessionManagementProtocol.startSessionReset(context, sender); + return; // Don't trigger the session restoration UI + } + } + } + } + SessionManagementProtocol.triggerSessionRestorationUI(context, sender); } diff --git a/src/org/thoughtcrime/securesms/loki/protocol/SessionManagementProtocol.kt b/src/org/thoughtcrime/securesms/loki/protocol/SessionManagementProtocol.kt index fe9e07344c..c0143a3042 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/SessionManagementProtocol.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/SessionManagementProtocol.kt @@ -1,6 +1,7 @@ package org.thoughtcrime.securesms.loki.protocol import android.content.Context +import android.os.AsyncTask import android.util.Log import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.crypto.IdentityKeyUtil @@ -23,15 +24,18 @@ import java.util.* object SessionManagementProtocol { @JvmStatic - fun startSessionReset(context: Context, recipient: Recipient, threadID: Long) { + fun startSessionReset(context: Context, publicKey: String) { + val recipient = recipient(context, publicKey) if (recipient.isGroupRecipient) { return } val lokiThreadDB = DatabaseFactory.getLokiThreadDatabase(context) - val smsDB = DatabaseFactory.getSmsDatabase(context) + if (lokiThreadDB.getSessionResetStatus(publicKey) != SessionResetStatus.NONE) { return } + val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient) val devices = lokiThreadDB.getSessionRestoreDevices(threadID) for (device in devices) { val endSessionMessage = OutgoingEndSessionMessage(OutgoingTextMessage(recipient, "TERMINATE", 0, -1)) MessageSender.send(context, endSessionMessage, threadID, false, null) } + val smsDB = DatabaseFactory.getSmsDatabase(context) val infoMessage = OutgoingTextMessage(recipient, "", 0, 0) val infoMessageID = smsDB.insertMessageOutbox(threadID, infoMessage, false, System.currentTimeMillis(), null) if (infoMessageID > -1) {