From 8ea4db03db5d77cd237a4c50b5dccbe00edac0f9 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Fri, 28 Feb 2014 15:58:30 -0800 Subject: [PATCH] Better support for local storage with passphrase disabled. 1) Never get into a state where messages aren't displayed unencrypted. 2) Fix bug where locked DB required launching twice. --- .../securesms/RoutingActivity.java | 23 ++++++++++++------- .../securesms/service/PushReceiver.java | 4 ++-- .../securesms/service/SendReceiveService.java | 20 ++++++++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/org/thoughtcrime/securesms/RoutingActivity.java b/src/org/thoughtcrime/securesms/RoutingActivity.java index 70e8ff8b72..046898f6fb 100644 --- a/src/org/thoughtcrime/securesms/RoutingActivity.java +++ b/src/org/thoughtcrime/securesms/RoutingActivity.java @@ -9,9 +9,6 @@ import org.thoughtcrime.securesms.recipients.RecipientFactory; import org.thoughtcrime.securesms.recipients.RecipientFormattingException; import org.thoughtcrime.securesms.recipients.Recipients; import org.thoughtcrime.securesms.service.ApplicationMigrationService; -import org.thoughtcrime.securesms.service.DirectoryRefreshListener; -import org.thoughtcrime.securesms.service.DirectoryRefreshService; -import org.thoughtcrime.securesms.util.DirectoryHelper; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.whispersystems.textsecure.crypto.MasterSecret; @@ -24,18 +21,27 @@ public class RoutingActivity extends PassphraseRequiredSherlockActivity { private static final int STATE_UPGRADE_DATABASE = 5; private static final int STATE_PROMPT_PUSH_REGISTRATION = 6; - private MasterSecret masterSecret = null; - private boolean isVisible = false; + private MasterSecret masterSecret = null; + private boolean isVisible = false; + private boolean canceledResult = false; + private boolean newIntent = false; @Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); + this.newIntent = true; } @Override public void onResume() { - this.isVisible = true; + if (this.canceledResult && !this.newIntent) { + finish(); + } + + this.newIntent = false; + this.canceledResult = false; + this.isVisible = true; super.onResume(); } @@ -65,8 +71,9 @@ public class RoutingActivity extends PassphraseRequiredSherlockActivity { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (resultCode == RESULT_CANCELED) - finish(); + if (resultCode == RESULT_CANCELED) { + canceledResult = true; + } } private void routeApplicationState() { diff --git a/src/org/thoughtcrime/securesms/service/PushReceiver.java b/src/org/thoughtcrime/securesms/service/PushReceiver.java index 0f517fa0fb..51d46315b1 100644 --- a/src/org/thoughtcrime/securesms/service/PushReceiver.java +++ b/src/org/thoughtcrime/securesms/service/PushReceiver.java @@ -281,8 +281,8 @@ public class PushReceiver { } private Pair insertMessagePlaceholder(MasterSecret masterSecret, - IncomingPushMessage message, - boolean secure) + IncomingPushMessage message, + boolean secure) { IncomingTextMessage placeholder = new IncomingTextMessage(message, "", null); diff --git a/src/org/thoughtcrime/securesms/service/SendReceiveService.java b/src/org/thoughtcrime/securesms/service/SendReceiveService.java index 56912d16fe..25f3ebb89b 100644 --- a/src/org/thoughtcrime/securesms/service/SendReceiveService.java +++ b/src/org/thoughtcrime/securesms/service/SendReceiveService.java @@ -29,7 +29,10 @@ import android.os.Message; import android.util.Log; import android.widget.Toast; +import org.thoughtcrime.securesms.crypto.InvalidPassphraseException; +import org.thoughtcrime.securesms.crypto.MasterSecretUtil; import org.thoughtcrime.securesms.database.CanonicalSessionMigrator; +import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.WorkerThread; import org.whispersystems.textsecure.crypto.MasterSecret; @@ -185,6 +188,7 @@ public class SendReceiveService extends Service { registerReceiver(clearKeyReceiver, clearKeyFilter, KeyCachingService.KEY_PERMISSION, null); Intent bindIntent = new Intent(this, KeyCachingService.class); + startService(bindIntent); bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE); } @@ -242,6 +246,12 @@ public class SendReceiveService extends Service { @Override public void run() { + MasterSecret masterSecret = SendReceiveService.this.masterSecret; + + if (masterSecret == null && TextSecurePreferences.isPasswordDisabled(SendReceiveService.this)) { + masterSecret = getPlaceholderSecret(); + } + switch (what) { case RECEIVE_SMS: smsReceiver.process(masterSecret, intent); return; case SEND_SMS: smsSender.process(masterSecret, intent); return; @@ -254,6 +264,16 @@ public class SendReceiveService extends Service { case DOWNLOAD_AVATAR: avatarDownloader.process(masterSecret, intent); return; } } + + private MasterSecret getPlaceholderSecret() { + try { + return MasterSecretUtil.getMasterSecret(SendReceiveService.this, + MasterSecretUtil.UNENCRYPTED_PASSPHRASE); + } catch (InvalidPassphraseException e) { + Log.w("SendReceiveService", e); + return null; + } + } } public class ToastHandler extends Handler {