backup: only ask for passphrase for secret keys which have one
This commit is contained in:
@@ -17,24 +17,28 @@
|
|||||||
|
|
||||||
package org.sufficientlysecure.keychain.ui;
|
package org.sufficientlysecure.keychain.ui;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
|
import android.support.v4.app.FragmentActivity;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.Constants;
|
import org.sufficientlysecure.keychain.Constants;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.provider.KeychainContract;
|
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
|
||||||
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
|
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
|
||||||
import org.sufficientlysecure.keychain.util.ExportHelper;
|
import org.sufficientlysecure.keychain.util.ExportHelper;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class BackupFragment extends Fragment {
|
public class BackupFragment extends Fragment {
|
||||||
|
|
||||||
// This ids for multiple key export.
|
// This ids for multiple key export.
|
||||||
@@ -49,17 +53,17 @@ public class BackupFragment extends Fragment {
|
|||||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||||
View view = inflater.inflate(R.layout.backup_fragment, container, false);
|
View view = inflater.inflate(R.layout.backup_fragment, container, false);
|
||||||
|
|
||||||
View mBackupAll = view.findViewById(R.id.backup_all);
|
View backupAll = view.findViewById(R.id.backup_all);
|
||||||
View mBackupPublicKeys = view.findViewById(R.id.backup_public_keys);
|
View backupPublicKeys = view.findViewById(R.id.backup_public_keys);
|
||||||
|
|
||||||
mBackupAll.setOnClickListener(new View.OnClickListener() {
|
backupAll.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
exportToFile(true);
|
exportToFile(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
mBackupPublicKeys.setOnClickListener(new View.OnClickListener() {
|
backupPublicKeys.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
exportToFile(false);
|
exportToFile(false);
|
||||||
@@ -70,49 +74,83 @@ public class BackupFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void exportToFile(boolean includeSecretKeys) {
|
private void exportToFile(boolean includeSecretKeys) {
|
||||||
if (includeSecretKeys) {
|
FragmentActivity activity = getActivity();
|
||||||
mIdsForRepeatAskPassphrase = new ArrayList<>();
|
if (activity == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Cursor cursor = getActivity().getContentResolver().query(
|
if (!includeSecretKeys) {
|
||||||
KeychainContract.KeyRingData.buildSecretKeyRingUri(), null, null, null, null);
|
ExportHelper exportHelper = new ExportHelper(activity);
|
||||||
try {
|
exportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, false);
|
||||||
if (cursor != null) {
|
return;
|
||||||
int keyIdColumn = cursor.getColumnIndex(KeychainContract.KeyRingData.MASTER_KEY_ID);
|
}
|
||||||
while (cursor.moveToNext()) {
|
|
||||||
long keyId = cursor.getLong(keyIdColumn);
|
new AsyncTask<ContentResolver,Void,ArrayList<Long>>() {
|
||||||
try {
|
@Override
|
||||||
if (PassphraseCacheService.getCachedPassphrase(
|
protected ArrayList<Long> doInBackground(ContentResolver... resolver) {
|
||||||
getActivity(), keyId, keyId) == null) {
|
ArrayList<Long> askPassphraseIds = new ArrayList<>();
|
||||||
mIdsForRepeatAskPassphrase.add(keyId);
|
Cursor cursor = resolver[0].query(
|
||||||
|
KeyRings.buildUnifiedKeyRingsUri(), new String[] {
|
||||||
|
KeyRings.MASTER_KEY_ID,
|
||||||
|
KeyRings.HAS_SECRET,
|
||||||
|
}, KeyRings.HAS_SECRET + " != 0", null, null);
|
||||||
|
try {
|
||||||
|
if (cursor != null) {
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
SecretKeyType secretKeyType = SecretKeyType.fromNum(cursor.getInt(1));
|
||||||
|
switch (secretKeyType) {
|
||||||
|
// all of these make no sense to ask
|
||||||
|
case PASSPHRASE_EMPTY:
|
||||||
|
case GNU_DUMMY:
|
||||||
|
case DIVERT_TO_CARD:
|
||||||
|
case UNAVAILABLE:
|
||||||
|
continue;
|
||||||
|
default: {
|
||||||
|
long keyId = cursor.getLong(0);
|
||||||
|
askPassphraseIds.add(keyId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (PassphraseCacheService.KeyNotFoundException e) {
|
|
||||||
// This happens when the master key is stripped
|
|
||||||
// and ignore this key.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
return askPassphraseIds;
|
||||||
if (cursor != null) {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mIndex = 0;
|
@Override
|
||||||
if (mIdsForRepeatAskPassphrase.size() != 0) {
|
protected void onPostExecute(ArrayList<Long> askPassphraseIds) {
|
||||||
startPassphraseActivity();
|
super.onPostExecute(askPassphraseIds);
|
||||||
return;
|
FragmentActivity activity = getActivity();
|
||||||
|
if (activity == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mIdsForRepeatAskPassphrase = askPassphraseIds;
|
||||||
|
mIndex = 0;
|
||||||
|
|
||||||
|
if (mIdsForRepeatAskPassphrase.size() != 0) {
|
||||||
|
startPassphraseActivity();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ExportHelper exportHelper = new ExportHelper(activity);
|
||||||
|
exportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
ExportHelper exportHelper = new ExportHelper(getActivity());
|
}.execute(activity.getContentResolver());
|
||||||
exportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true);
|
|
||||||
} else {
|
|
||||||
ExportHelper exportHelper = new ExportHelper(getActivity());
|
|
||||||
exportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startPassphraseActivity() {
|
private void startPassphraseActivity() {
|
||||||
Intent intent = new Intent(getActivity(), PassphraseDialogActivity.class);
|
Activity activity = getActivity();
|
||||||
|
if (activity == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent intent = new Intent(activity, PassphraseDialogActivity.class);
|
||||||
long masterKeyId = mIdsForRepeatAskPassphrase.get(mIndex++);
|
long masterKeyId = mIdsForRepeatAskPassphrase.get(mIndex++);
|
||||||
intent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID, masterKeyId);
|
intent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID, masterKeyId);
|
||||||
startActivityForResult(intent, REQUEST_REPEAT_PASSPHRASE);
|
startActivityForResult(intent, REQUEST_REPEAT_PASSPHRASE);
|
||||||
|
|||||||
Reference in New Issue
Block a user