2012-07-17 02:49:37 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-17 02:49:37 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-07-17 02:49:37 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.service;
|
|
|
|
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.app.Service;
|
2013-02-11 01:30:51 +00:00
|
|
|
import android.content.Context;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Binder;
|
2012-09-09 16:18:17 +00:00
|
|
|
import android.os.Build;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.os.IBinder;
|
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.util.Log;
|
2012-09-09 16:18:17 +00:00
|
|
|
import android.widget.RemoteViews;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-07-17 02:49:37 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2013-02-17 19:42:30 +00:00
|
|
|
import org.thoughtcrime.securesms.RoutingActivity;
|
|
|
|
import org.thoughtcrime.securesms.crypto.DecryptingQueue;
|
2012-07-17 02:49:37 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2013-02-08 19:57:54 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/**
|
|
|
|
* Small service that stays running to keep a key cached in memory.
|
2012-07-17 02:49:37 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class KeyCachingService extends Service {
|
|
|
|
|
2013-02-11 01:30:51 +00:00
|
|
|
public static final int SERVICE_RUNNING_ID = 4141;
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String KEY_PERMISSION = "org.thoughtcrime.securesms.ACCESS_SECRETS";
|
|
|
|
public static final String NEW_KEY_EVENT = "org.thoughtcrime.securesms.service.action.NEW_KEY_EVENT";
|
2013-02-08 19:57:54 +00:00
|
|
|
public static final String CLEAR_KEY_EVENT = "org.thoughtcrime.securesms.service.action.CLEAR_KEY_EVENT";
|
|
|
|
private static final String PASSPHRASE_EXPIRED_EVENT = "org.thoughtcrime.securesms.service.action.PASSPHRASE_EXPIRED_EVENT";
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String CLEAR_KEY_ACTION = "org.thoughtcrime.securesms.service.action.CLEAR_KEY";
|
|
|
|
public static final String ACTIVITY_START_EVENT = "org.thoughtcrime.securesms.service.action.ACTIVITY_START_EVENT";
|
|
|
|
public static final String ACTIVITY_STOP_EVENT = "org.thoughtcrime.securesms.service.action.ACTIVITY_STOP_EVENT";
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private PendingIntent pending;
|
2012-07-17 02:49:37 +00:00
|
|
|
private int activitiesRunning = 0;
|
|
|
|
private final IBinder binder = new KeyCachingBinder();
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private MasterSecret masterSecret;
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public KeyCachingService() {}
|
|
|
|
|
|
|
|
public synchronized MasterSecret getMasterSecret() {
|
|
|
|
return masterSecret;
|
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2013-02-08 19:57:54 +00:00
|
|
|
public synchronized void setMasterSecret(final MasterSecret masterSecret) {
|
2011-12-20 18:20:44 +00:00
|
|
|
this.masterSecret = masterSecret;
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
foregroundService();
|
|
|
|
broadcastNewSecret();
|
|
|
|
startTimeoutIfAppropriate();
|
2013-02-08 19:57:54 +00:00
|
|
|
|
|
|
|
new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
DecryptingQueue.schedulePendingDecrypts(KeyCachingService.this, masterSecret);
|
2013-02-08 19:57:54 +00:00
|
|
|
MessageNotifier.updateNotification(KeyCachingService.this, masterSecret);
|
|
|
|
}
|
|
|
|
}.start();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public void onStart(Intent intent, int startId) {
|
2012-08-05 19:41:31 +00:00
|
|
|
if (intent == null) return;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (intent.getAction() != null && intent.getAction().equals(CLEAR_KEY_ACTION))
|
|
|
|
handleClearKey();
|
|
|
|
else if (intent.getAction() != null && intent.getAction().equals(ACTIVITY_START_EVENT))
|
|
|
|
handleActivityStarted();
|
|
|
|
else if (intent.getAction() != null && intent.getAction().equals(ACTIVITY_STOP_EVENT))
|
|
|
|
handleActivityStopped();
|
|
|
|
else if (intent.getAction() != null && intent.getAction().equals(PASSPHRASE_EXPIRED_EVENT))
|
2013-02-08 19:57:54 +00:00
|
|
|
handleClearKey();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2013-02-08 19:57:54 +00:00
|
|
|
super.onCreate();
|
2012-07-17 02:49:37 +00:00
|
|
|
pending = PendingIntent.getService(this, 0, new Intent(PASSPHRASE_EXPIRED_EVENT, null, this, KeyCachingService.class), 0);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2013-02-08 19:57:54 +00:00
|
|
|
super.onDestroy();
|
|
|
|
Log.w("KeyCachingService", "KCS Is Being Destroyed!");
|
|
|
|
handleClearKey();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void handleActivityStarted() {
|
|
|
|
Log.w("KeyCachingService", "Incrementing activity count...");
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE);
|
2012-07-17 02:49:37 +00:00
|
|
|
alarmManager.cancel(pending);
|
2011-12-20 18:20:44 +00:00
|
|
|
activitiesRunning++;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleActivityStopped() {
|
|
|
|
Log.w("KeyCachingService", "Decrementing activity count...");
|
|
|
|
|
|
|
|
activitiesRunning--;
|
|
|
|
startTimeoutIfAppropriate();
|
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void handleClearKey() {
|
|
|
|
this.masterSecret = null;
|
2012-07-17 02:49:37 +00:00
|
|
|
stopForeground(true);
|
|
|
|
|
2013-02-08 19:57:54 +00:00
|
|
|
Intent intent = new Intent(CLEAR_KEY_EVENT);
|
2011-12-20 18:20:44 +00:00
|
|
|
intent.setPackage(getApplicationContext().getPackageName());
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
sendBroadcast(intent, KEY_PERMISSION);
|
2013-02-08 19:57:54 +00:00
|
|
|
|
|
|
|
new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
MessageNotifier.updateNotification(KeyCachingService.this, null);
|
|
|
|
}
|
|
|
|
}.start();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void startTimeoutIfAppropriate() {
|
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
boolean timeoutEnabled = sharedPreferences.getBoolean(ApplicationPreferencesActivity.PASSPHRASE_TIMEOUT_PREF, false);
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if ((activitiesRunning == 0) && (this.masterSecret != null) && timeoutEnabled) {
|
|
|
|
long timeoutMinutes = sharedPreferences.getInt(ApplicationPreferencesActivity.PASSPHRASE_TIMEOUT_INTERVAL_PREF, 60 * 5);
|
|
|
|
long timeoutMillis = timeoutMinutes * 60 * 1000;
|
|
|
|
|
|
|
|
Log.w("KeyCachingService", "Starting timeout: " + timeoutMillis);
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE);
|
|
|
|
alarmManager.cancel(pending);
|
|
|
|
alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeoutMillis, pending);
|
2012-07-17 02:49:37 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2012-09-09 16:18:17 +00:00
|
|
|
private void foregroundServiceModern() {
|
|
|
|
Notification notification = new Notification(R.drawable.icon_cached, null, System.currentTimeMillis());
|
|
|
|
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.key_caching_notification);
|
|
|
|
|
|
|
|
Intent intent = new Intent(this, KeyCachingService.class);
|
|
|
|
intent.setAction(PASSPHRASE_EXPIRED_EVENT);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
|
|
|
|
remoteViews.setOnClickPendingIntent(R.id.lock_cache_icon, pendingIntent);
|
|
|
|
|
|
|
|
notification.contentView = remoteViews;
|
|
|
|
|
|
|
|
stopForeground(true);
|
|
|
|
startForeground(SERVICE_RUNNING_ID, notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void foregroundServiceLegacy() {
|
2012-09-08 03:03:23 +00:00
|
|
|
Notification notification = new Notification(R.drawable.icon_cached,
|
2012-09-20 02:56:04 +00:00
|
|
|
getString(R.string.KeyCachingService_textsecure_passphrase_cached),
|
2012-09-08 03:03:23 +00:00
|
|
|
System.currentTimeMillis());
|
2013-02-17 19:42:30 +00:00
|
|
|
Intent intent = new Intent(this, RoutingActivity.class);
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
PendingIntent launchIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
|
2012-09-20 02:56:04 +00:00
|
|
|
notification.setLatestEventInfo(getApplicationContext(),
|
|
|
|
getString(R.string.KeyCachingService_passphrase_cached),
|
|
|
|
getString(R.string.KeyCachingService_textsecure_passphrase_cached),
|
|
|
|
launchIntent);
|
2012-07-17 02:49:37 +00:00
|
|
|
|
|
|
|
stopForeground(true);
|
|
|
|
startForeground(SERVICE_RUNNING_ID, notification);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2012-09-09 16:18:17 +00:00
|
|
|
private void foregroundService() {
|
|
|
|
if (Build.VERSION.SDK_INT >= 11) foregroundServiceModern();
|
|
|
|
else foregroundServiceLegacy();
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void broadcastNewSecret() {
|
|
|
|
Log.w("service", "Broadcasting new secret...");
|
2013-02-08 19:57:54 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
Intent intent = new Intent(NEW_KEY_EVENT);
|
|
|
|
intent.putExtra("master_secret", masterSecret);
|
|
|
|
intent.setPackage(getApplicationContext().getPackageName());
|
|
|
|
|
|
|
|
sendBroadcast(intent, KEY_PERMISSION);
|
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent arg0) {
|
|
|
|
return binder;
|
|
|
|
}
|
2012-07-17 02:49:37 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public class KeyCachingBinder extends Binder {
|
|
|
|
public KeyCachingService getService() {
|
|
|
|
return KeyCachingService.this;
|
|
|
|
}
|
|
|
|
}
|
2013-02-11 01:30:51 +00:00
|
|
|
|
|
|
|
public static void registerPassphraseActivityStarted(Context activity) {
|
|
|
|
Intent intent = new Intent(activity, KeyCachingService.class);
|
|
|
|
intent.setAction(KeyCachingService.ACTIVITY_START_EVENT);
|
|
|
|
activity.startService(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerPassphraseActivityStopped(Context activity) {
|
|
|
|
Intent intent = new Intent(activity, KeyCachingService.class);
|
|
|
|
intent.setAction(KeyCachingService.ACTIVITY_STOP_EVENT);
|
|
|
|
activity.startService(intent);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|