Skip to content

Commit

Permalink
Patch bad mac issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsandriesse committed Aug 21, 2020
1 parent 1a8ced9 commit 1a013e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public void onSuccess(Boolean loadedDraft) {
});

sessionRestoreBannerView.setOnRestore(() -> {
SessionManagementProtocol.startSessionReset(this, recipient, threadId);
SessionManagementProtocol.startSessionReset(this, recipient.getAddress().serialize());
updateSessionRestoreBanner();
return Unit.INSTANCE;
});
Expand Down
25 changes: 21 additions & 4 deletions src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
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;
Expand Down Expand Up @@ -354,14 +355,14 @@ private void handleMessage(@NonNull SignalServiceEnvelope envelope, @NonNull Opt
} 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);
Expand Down Expand Up @@ -1069,7 +1070,7 @@ private void handleInvalidVersionMessage(@NonNull String sender, int senderDevic
}

private void handleCorruptMessage(@NonNull String sender, int senderDevice, long timestamp,
@NonNull Optional<Long> smsMessageId)
@NonNull Optional<Long> smsMessageId, @NonNull Throwable e)
{
SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context);

Expand All @@ -1083,6 +1084,22 @@ private void handleCorruptMessage(@NonNull String sender, int senderDevice, long
} 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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 1a013e9

Please sign in to comment.