From a139be29ba556468ca282c96e5c985762c466b5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 17 Sep 2014 21:36:38 +0200 Subject: [PATCH] PassphraseDialogActivity usage in Remote API --- .../keychain/remote/OpenPgpService.java | 8 ++-- .../remote/ui/RemoteServiceActivity.java | 22 --------- .../keychain/ui/PassphraseDialogActivity.java | 48 +++++++++++-------- 3 files changed, 33 insertions(+), 45 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index c11f283be..a8751bac0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -48,6 +48,7 @@ import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.service.results.OperationResult.LogEntryParcel; import org.sufficientlysecure.keychain.service.results.SignEncryptResult; import org.sufficientlysecure.keychain.ui.ImportKeysActivity; +import org.sufficientlysecure.keychain.ui.PassphraseDialogActivity; import org.sufficientlysecure.keychain.ui.ViewKeyActivity; import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.Log; @@ -203,11 +204,10 @@ public class OpenPgpService extends RemoteService { private Intent getPassphraseIntent(Intent data, long keyId) { // build PendingIntent for passphrase input - Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class); - intent.setAction(RemoteServiceActivity.ACTION_CACHE_PASSPHRASE); - intent.putExtra(RemoteServiceActivity.EXTRA_SECRET_KEY_ID, keyId); + Intent intent = new Intent(getBaseContext(), PassphraseDialogActivity.class); + intent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID, keyId); // pass params through to activity that it can be returned again later to repeat pgp operation - intent.putExtra(RemoteServiceActivity.EXTRA_DATA, data); + intent.putExtra(PassphraseDialogActivity.EXTRA_DATA, data); PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java index ce5534cf4..6a96a831c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java @@ -54,8 +54,6 @@ public class RemoteServiceActivity extends ActionBarActivity { public static final String ACTION_REGISTER = Constants.INTENT_PREFIX + "API_ACTIVITY_REGISTER"; public static final String ACTION_CREATE_ACCOUNT = Constants.INTENT_PREFIX + "API_ACTIVITY_CREATE_ACCOUNT"; - public static final String ACTION_CACHE_PASSPHRASE = Constants.INTENT_PREFIX - + "API_ACTIVITY_CACHE_PASSPHRASE"; public static final String ACTION_SELECT_PUB_KEYS = Constants.INTENT_PREFIX + "API_ACTIVITY_SELECT_PUB_KEYS"; public static final String ACTION_ERROR_MESSAGE = Constants.INTENT_PREFIX @@ -215,26 +213,6 @@ public class RemoteServiceActivity extends ActionBarActivity { } ); - } else if (ACTION_CACHE_PASSPHRASE.equals(action)) { - long secretKeyId = extras.getLong(EXTRA_SECRET_KEY_ID); - final Intent resultData = extras.getParcelable(EXTRA_DATA); - - PassphraseDialogFragment.show(this, secretKeyId, - new Handler() { - @Override - public void handleMessage(Message message) { - if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { - // return given params again, for calling the service method again - RemoteServiceActivity.this.setResult(RESULT_OK, resultData); - } else { - RemoteServiceActivity.this.setResult(RESULT_CANCELED); - } - - RemoteServiceActivity.this.finish(); - } - } - ); - } else if (ACTION_SELECT_PUB_KEYS.equals(action)) { long[] selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS); boolean noUserIdsCheck = intent.getBooleanExtra(EXTRA_NO_USER_IDS_CHECK, true); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java index e2dcb2ba5..ddd5fb5f0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java @@ -64,7 +64,6 @@ public class PassphraseDialogActivity extends FragmentActivity { // special extra for OpenPgpService public static final String EXTRA_DATA = "data"; - private Intent mServiceIntent; @Override protected void onCreate(Bundle savedInstanceState) { @@ -83,9 +82,9 @@ public class PassphraseDialogActivity extends FragmentActivity { long keyId = getIntent().getLongExtra(EXTRA_SUBKEY_ID, 0); - mServiceIntent = getIntent().getParcelableExtra(EXTRA_DATA); + Intent serviceIntent = getIntent().getParcelableExtra(EXTRA_DATA); - show(this, keyId); + show(this, keyId, serviceIntent); } /** @@ -93,13 +92,14 @@ public class PassphraseDialogActivity extends FragmentActivity { * encryption. Based on mSecretKeyId it asks for a passphrase to open a private key or it asks * for a symmetric passphrase */ - public static void show(final FragmentActivity context, final long keyId) { + public static void show(final FragmentActivity context, final long keyId, final Intent serviceIntent) { DialogFragmentWorkaround.INTERFACE.runnableRunDelayed(new Runnable() { public void run() { // do NOT check if the key even needs a passphrase. that's not our job here. PassphraseDialogFragment frag = new PassphraseDialogFragment(); Bundle args = new Bundle(); args.putLong(EXTRA_SUBKEY_ID, keyId); + args.putParcelable(EXTRA_DATA, serviceIntent); frag.setArguments(args); @@ -112,9 +112,11 @@ public class PassphraseDialogActivity extends FragmentActivity { private EditText mPassphraseEditText; private View mInput, mProgress; - CanonicalizedSecretKeyRing mSecretRing = null; - boolean mIsCancelled = false; - long mSubKeyId; + private CanonicalizedSecretKeyRing mSecretRing = null; + private boolean mIsCancelled = false; + private long mSubKeyId; + + private Intent mServiceIntent; /** * Creates dialog @@ -129,6 +131,7 @@ public class PassphraseDialogActivity extends FragmentActivity { R.style.Theme_AppCompat_Light); mSubKeyId = getArguments().getLong(EXTRA_SUBKEY_ID); + mServiceIntent = getArguments().getParcelable(EXTRA_DATA); CustomAlertDialogBuilder alert = new CustomAlertDialogBuilder(theme); @@ -250,12 +253,8 @@ public class PassphraseDialogActivity extends FragmentActivity { if (mSecretRing == null) { PassphraseCacheService.addCachedPassphrase(getActivity(), Constants.key.symmetric, passphrase, getString(R.string.passp_cache_notif_pwd)); - // also return passphrase back to activity - Intent returnIntent = new Intent(); - returnIntent.putExtra(MESSAGE_DATA_PASSPHRASE, passphrase); - getActivity().setResult(RESULT_OK, returnIntent); - dismiss(); - getActivity().finish(); + + finishCaching(passphrase); return; } @@ -316,17 +315,28 @@ public class PassphraseDialogActivity extends FragmentActivity { Log.e(Constants.TAG, "adding of a passphrase failed", e); } - // also return passphrase back to activity - Intent returnIntent = new Intent(); - returnIntent.putExtra(MESSAGE_DATA_PASSPHRASE, passphrase); - getActivity().setResult(RESULT_OK, returnIntent); - dismiss(); - getActivity().finish(); + finishCaching(passphrase); } }.execute(); } }); + } + private void finishCaching(String passphrase) { + if (mServiceIntent != null) { + // TODO: Not routing passphrase through OpenPGP API currently + // due to security concerns... + // BUT this means you need to _cache_ passphrases! + getActivity().setResult(RESULT_OK, mServiceIntent); + } else { + // also return passphrase back to activity + Intent returnIntent = new Intent(); + returnIntent.putExtra(MESSAGE_DATA_PASSPHRASE, passphrase); + getActivity().setResult(RESULT_OK, returnIntent); + } + + dismiss(); + getActivity().finish(); } @Override