added secure file deletion, overwriting the file with random data before actual deletion

Update issue 56
Added a method for secure file deletion. We'll possibly have to test this a bit and make sure it actually *is* secure on the filesystems used (vfat for SD card, yaffs2 for phone flash memory by default). This sort of overwrite might be absolutely useless on some filesystems.
I also wanted to add an option at first, but this seems reasonably fast and in general it should be what the user wants, so for now it will ALWAYS securely delete. If there is a demand for plain deletion, then options can be added.

Update issue 39
A new string:
<string name="progress_deletingSecurely">deleting \'%s\' securely...</string>
This commit is contained in:
Thialfihar 2010-07-24 14:24:42 +00:00
parent 2407f3b989
commit b650b30a11
6 changed files with 83 additions and 19 deletions

View File

@ -242,6 +242,7 @@
<string name="progress_findingKey">finding key...</string> <string name="progress_findingKey">finding key...</string>
<string name="progress_decompressingData">decompressing data...</string> <string name="progress_decompressingData">decompressing data...</string>
<string name="progress_verifyingIntegrity">verifying integrity...</string> <string name="progress_verifyingIntegrity">verifying integrity...</string>
<string name="progress_deletingSecurely">deleting \'%s\' securely...</string>
<!-- permission strings --> <!-- permission strings -->
<string name="permission_read_key_details_label">Read key details from APG.</string> <string name="permission_read_key_details_label">Read key details from APG.</string>

View File

@ -20,10 +20,12 @@ import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.EOFException; import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPairGenerator; import java.security.KeyPairGenerator;
@ -1895,4 +1897,24 @@ public class Apg {
} }
return size; return size;
} }
static void deleteFileSecurely(Context context, File file, ProgressDialogUpdater progress)
throws FileNotFoundException, IOException {
long length = file.length();
SecureRandom random = new SecureRandom();
RandomAccessFile raf = new RandomAccessFile(file, "rws");
raf.seek(0);
raf.getFilePointer();
byte[] data = new byte[1 << 16];
int pos = 0;
String msg = context.getString(R.string.progress_deletingSecurely, file.getName());
while (pos < length) {
progress.setProgress(msg, (int)(100 * pos / length), 100);
random.nextBytes(data);
raf.write(data);
pos += data.length;
}
raf.close();
file.delete();
}
} }

View File

@ -17,6 +17,8 @@
package org.thialfihar.android.apg; package org.thialfihar.android.apg;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import android.app.Activity; import android.app.Activity;
@ -44,6 +46,7 @@ public class BaseActivity extends Activity
private ProgressDialog mProgressDialog = null; private ProgressDialog mProgressDialog = null;
private Thread mRunningThread = null; private Thread mRunningThread = null;
private Thread mDeletingThread = null;
private long mSecretKeyId = 0; private long mSecretKeyId = 0;
private String mDeleteFile = null; private String mDeleteFile = null;
@ -149,6 +152,11 @@ public class BaseActivity extends Activity
return mProgressDialog; return mProgressDialog;
} }
case Id.dialog.deleting: {
mProgressDialog.setMessage(this.getString(R.string.progress_initializing));
return mProgressDialog;
}
default: { default: {
break; break;
} }
@ -235,19 +243,30 @@ public class BaseActivity extends Activity
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
removeDialog(Id.dialog.delete_file); removeDialog(Id.dialog.delete_file);
File file = new File(getDeleteFile()); final File file = new File(getDeleteFile());
String msg = ""; showDialog(Id.dialog.deleting);
if (file.delete()) { mDeletingThread = new Thread(new Runnable() {
msg = BaseActivity.this.getString( @Override
R.string.fileDeleteSuccessful); public void run() {
} else { Bundle data = new Bundle();
msg = BaseActivity.this.getString( data.putInt(Apg.EXTRA_STATUS, Id.message.delete_done);
R.string.errorMessage, try {
Apg.deleteFileSecurely(BaseActivity.this, file, BaseActivity.this);
} catch (FileNotFoundException e) {
data.putString(Apg.EXTRA_ERROR,
BaseActivity.this.getString(
R.string.error_fileNotFound, file));
} catch (IOException e) {
data.putString(Apg.EXTRA_ERROR,
BaseActivity.this.getString( BaseActivity.this.getString(
R.string.error_fileDeleteFailed, file)); R.string.error_fileDeleteFailed, file));
} }
Toast.makeText(BaseActivity.this, Message msg = new Message();
msg, Toast.LENGTH_SHORT).show(); msg.setData(data);
sendMessage(msg);
}
});
mDeletingThread.start();
} }
}); });
alert.setNegativeButton(android.R.string.cancel, alert.setNegativeButton(android.R.string.cancel,
@ -335,8 +354,14 @@ public class BaseActivity extends Activity
break; break;
} }
case Id.message.import_done: // intentionall no break case Id.message.delete_done: {
case Id.message.export_done: // intentionall no break mProgressDialog = null;
deleteDoneCallback(msg);
break;
}
case Id.message.import_done: // intentionally no break
case Id.message.export_done: // intentionally no break
case Id.message.done: { case Id.message.done: {
mProgressDialog = null; mProgressDialog = null;
doneCallback(msg); doneCallback(msg);
@ -349,6 +374,22 @@ public class BaseActivity extends Activity
} }
public void deleteDoneCallback(Message msg) {
removeDialog(Id.dialog.deleting);
mDeletingThread = null;
Bundle data = msg.getData();
String error = data.getString(Apg.EXTRA_ERROR);
String message;
if (error != null) {
message = getString(R.string.errorMessage, error);
} else {
message = getString(R.string.fileDeleteSuccessful);
}
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
public void passPhraseCallback(long keyId, String passPhrase) { public void passPhraseCallback(long keyId, String passPhrase) {
Apg.setCachedPassPhrase(keyId, passPhrase); Apg.setCachedPassPhrase(keyId, passPhrase);
} }

View File

@ -545,8 +545,7 @@ public class DecryptActivity extends BaseActivity {
String error = data.getString(Apg.EXTRA_ERROR); String error = data.getString(Apg.EXTRA_ERROR);
if (error != null) { if (error != null) {
Toast.makeText(DecryptActivity.this, Toast.makeText(this, getString(R.string.errorMessage, error), Toast.LENGTH_SHORT).show();
getString(R.string.errorMessage, error), Toast.LENGTH_SHORT).show();
return; return;
} }

View File

@ -779,8 +779,7 @@ public class EncryptActivity extends BaseActivity {
Bundle data = msg.getData(); Bundle data = msg.getData();
String error = data.getString(Apg.EXTRA_ERROR); String error = data.getString(Apg.EXTRA_ERROR);
if (error != null) { if (error != null) {
Toast.makeText(EncryptActivity.this, Toast.makeText(this, getString(R.string.errorMessage, error), Toast.LENGTH_SHORT).show();
getString(R.string.errorMessage, error), Toast.LENGTH_SHORT).show();
return; return;
} }
switch (mEncryptTarget) { switch (mEncryptTarget) {

View File

@ -46,6 +46,7 @@ public final class Id {
public static final int export_done = 0x21070006; public static final int export_done = 0x21070006;
public static final int create_key = 0x21070007; public static final int create_key = 0x21070007;
public static final int edit_key = 0x21070008; public static final int edit_key = 0x21070008;
public static final int delete_done = 0x21070009;
} }
public static final class request { public static final class request {
@ -74,6 +75,7 @@ public final class Id {
public static final int change_log = 0x21070010; public static final int change_log = 0x21070010;
public static final int output_filename = 0x21070011; public static final int output_filename = 0x21070011;
public static final int delete_file = 0x21070012; public static final int delete_file = 0x21070012;
public static final int deleting = 0x21070013;
} }
public static final class task { public static final class task {