Fix missing MasterSecret in PartProvider

Fixes #2706
Closes #2910

// FREEBIE
This commit is contained in:
Jake McGinty 2015-04-06 13:49:04 -07:00 committed by Moxie Marlinspike
parent 39e0639b4b
commit 01020c1c09

View File

@ -16,18 +16,11 @@
*/ */
package org.thoughtcrime.securesms.providers; package org.thoughtcrime.securesms.providers;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentProvider; import android.content.ContentProvider;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.UriMatcher; import android.content.UriMatcher;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.IBinder;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.util.Log; import android.util.Log;
@ -42,6 +35,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
public class PartProvider extends ContentProvider { public class PartProvider extends ContentProvider {
private static final String TAG = PartProvider.class.getSimpleName();
private static final String CONTENT_URI_STRING = "content://org.thoughtcrime.provider.securesms/part"; private static final String CONTENT_URI_STRING = "content://org.thoughtcrime.provider.securesms/part";
public static final Uri CONTENT_URI = Uri.parse(CONTENT_URI_STRING); public static final Uri CONTENT_URI = Uri.parse(CONTENT_URI_STRING);
@ -54,19 +48,12 @@ public class PartProvider extends ContentProvider {
uriMatcher.addURI("org.thoughtcrime.provider.securesms", "part/#", SINGLE_ROW); uriMatcher.addURI("org.thoughtcrime.provider.securesms", "part/#", SINGLE_ROW);
} }
private MasterSecret masterSecret;
private NewKeyReceiver receiver;
@Override @Override
public boolean onCreate() { public boolean onCreate() {
initializeMasterSecret(); Log.w(TAG, "onCreate()");
return true; return true;
} }
public static boolean isAuthority(Uri uri) {
return uriMatcher.match(uri) != -1;
}
private File copyPartToTemporaryFile(MasterSecret masterSecret, long partId) throws IOException { private File copyPartToTemporaryFile(MasterSecret masterSecret, long partId) throws IOException {
InputStream in = DatabaseFactory.getPartDatabase(getContext()).getPartStream(masterSecret, partId); InputStream in = DatabaseFactory.getPartDatabase(getContext()).getPartStream(masterSecret, partId);
File tmpDir = getContext().getDir("tmp", 0); File tmpDir = getContext().getDir("tmp", 0);
@ -86,22 +73,27 @@ public class PartProvider extends ContentProvider {
@Override @Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Log.w("PartProvider", "openFile() called!"); MasterSecret masterSecret = KeyCachingService.getMasterSecret(getContext());
Log.w(TAG, "openFile() called!");
if (this.masterSecret == null) if (masterSecret == null) {
Log.w(TAG, "masterSecret was null, abandoning.");
return null; return null;
}
switch (uriMatcher.match(uri)) { switch (uriMatcher.match(uri)) {
case SINGLE_ROW: case SINGLE_ROW:
Log.w("PartProvider", "Parting out a single row..."); Log.w(TAG, "Parting out a single row...");
try { try {
int partId = Integer.parseInt(uri.getPathSegments().get(1)); int partId = Integer.parseInt(uri.getPathSegments().get(1));
File tmpFile = copyPartToTemporaryFile(masterSecret, partId); File tmpFile = copyPartToTemporaryFile(masterSecret, partId);
ParcelFileDescriptor pdf = ParcelFileDescriptor.open(tmpFile, ParcelFileDescriptor.MODE_READ_ONLY); ParcelFileDescriptor pdf = ParcelFileDescriptor.open(tmpFile, ParcelFileDescriptor.MODE_READ_ONLY);
tmpFile.delete(); if (!tmpFile.delete()) {
Log.w(TAG, "Failed to delete temp file.");
}
return pdf; return pdf;
} catch (IOException ioe) { } catch (IOException ioe) {
Log.w("PartProvider", ioe); Log.w(TAG, ioe);
throw new FileNotFoundException("Error opening file"); throw new FileNotFoundException("Error opening file");
} }
} }
@ -133,26 +125,4 @@ public class PartProvider extends ContentProvider {
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) { public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
return 0; return 0;
} }
private void initializeWithMasterSecret(MasterSecret masterSecret) {
Log.w("PartProvider", "Got master secret: " + masterSecret);
this.masterSecret = masterSecret;
}
private void initializeMasterSecret() {
receiver = new NewKeyReceiver();
IntentFilter filter = new IntentFilter(KeyCachingService.NEW_KEY_EVENT);
getContext().registerReceiver(receiver, filter, KeyCachingService.KEY_PERMISSION, null);
initializeWithMasterSecret(KeyCachingService.getMasterSecret(getContext()));
}
private class NewKeyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("SendReceiveService", "Got a MasterSecret broadcast...");
initializeWithMasterSecret((MasterSecret)intent.getParcelableExtra("master_secret"));
}
};
} }