From 5f39cb3ec0d1d3f77d48fdbb428a2147dfe11d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 15 Feb 2014 02:08:27 +0100 Subject: [PATCH] fix passtrough of params --- .../demo/OpenPgpProviderActivity.java | 63 +++++++------------ .../openintents/openpgp/util/OpenPgpApi.java | 4 -- .../openpgp/util/OpenPgpConstants.java | 5 +- .../service/remote/OpenPgpService.java | 35 +++++------ .../service/remote/RemoteServiceActivity.java | 53 +++++++++------- .../openintents/openpgp/util/OpenPgpApi.java | 4 -- .../openpgp/util/OpenPgpConstants.java | 5 +- 7 files changed, 75 insertions(+), 94 deletions(-) diff --git a/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java b/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java index e21b1c2a1..e578d0660 100644 --- a/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java +++ b/OpenPGP-Keychain-API/example-app/src/main/java/org/sufficientlysecure/keychain/demo/OpenPgpProviderActivity.java @@ -167,8 +167,7 @@ public class OpenPgpProviderActivity extends Activity { PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT); try { OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(), - REQUEST_CODE_SIGN, null, - 0, 0, 0); + REQUEST_CODE_SIGN, null, 0, 0, 0); } catch (IntentSender.SendIntentException e) { Log.e(Constants.TAG, "SendIntentException", e); } @@ -206,8 +205,7 @@ public class OpenPgpProviderActivity extends Activity { PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT); try { OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(), - REQUEST_CODE_ENCRYPT, null, - 0, 0, 0); + REQUEST_CODE_ENCRYPT, null, 0, 0, 0); } catch (IntentSender.SendIntentException e) { Log.e(Constants.TAG, "SendIntentException", e); } @@ -245,8 +243,7 @@ public class OpenPgpProviderActivity extends Activity { PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT); try { OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(), - REQUEST_CODE_SIGN_AND_ENCRYPT, null, - 0, 0, 0); + REQUEST_CODE_SIGN_AND_ENCRYPT, null, 0, 0, 0); } catch (IntentSender.SendIntentException e) { Log.e(Constants.TAG, "SendIntentException", e); } @@ -283,8 +280,7 @@ public class OpenPgpProviderActivity extends Activity { PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT); try { OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(), - REQUEST_CODE_DECRYPT_AND_VERIFY, null, - 0, 0, 0); + REQUEST_CODE_DECRYPT_AND_VERIFY, null, 0, 0, 0); } catch (IntentSender.SendIntentException e) { Log.e(Constants.TAG, "SendIntentException", e); } @@ -299,44 +295,29 @@ public class OpenPgpProviderActivity extends Activity { protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - Log.d(Constants.TAG, "onActivityResult"); - switch (requestCode) { - case REQUEST_CODE_SIGN: { - Log.d(Constants.TAG, "resultCode: " + resultCode); + Log.d(Constants.TAG, "onActivityResult resultCode: " + resultCode); - // try to sign again after password caching - if (resultCode == RESULT_OK) { - sign(data.getExtras()); - } - break; - } - case REQUEST_CODE_ENCRYPT: { - Log.d(Constants.TAG, "resultCode: " + resultCode); + // try again after user interaction + if (resultCode == RESULT_OK) { + Bundle params = data.getBundleExtra(OpenPgpConstants.PI_RESULT_PARAMS); - // try to sign again after password caching - if (resultCode == RESULT_OK) { - // use data extras now as params for call (they now include key ids! - encrypt(data.getExtras()); + switch (requestCode) { + case REQUEST_CODE_SIGN: { + sign(params); + break; } - break; - } - case REQUEST_CODE_SIGN_AND_ENCRYPT: { - Log.d(Constants.TAG, "resultCode: " + resultCode); - - // try to sign again after password caching - if (resultCode == RESULT_OK) { - signAndEncrypt(data.getExtras()); + case REQUEST_CODE_ENCRYPT: { + encrypt(params); + break; } - break; - } - case REQUEST_CODE_DECRYPT_AND_VERIFY: { - Log.d(Constants.TAG, "resultCode: " + resultCode); - - // try to sign again after password caching - if (resultCode == RESULT_OK) { - decryptAndVerify(new Bundle()); + case REQUEST_CODE_SIGN_AND_ENCRYPT: { + signAndEncrypt(params); + break; + } + case REQUEST_CODE_DECRYPT_AND_VERIFY: { + decryptAndVerify(params); + break; } - break; } } } diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java index 5f67ddf88..5c38d3304 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -131,11 +131,7 @@ public class OpenPgpApi { try { params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); - // default result is error Bundle result = new Bundle(); - result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); - result.putParcelable(OpenPgpConstants.RESULT_ERRORS, - new OpenPgpError(OpenPgpError.GENERIC_ERROR, "This should never happen!")); if (operationId == OPERATION_GET_KEY_IDS) { result = mService.getKeyIds(params); diff --git a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java index 64bc92fdd..229c8d42a 100644 --- a/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java +++ b/OpenPGP-Keychain-API/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java @@ -33,7 +33,7 @@ public class OpenPgpConstants { public static final String PARAMS_USER_IDS = "user_ids"; public static final String PARAMS_KEY_IDS = "key_ids"; - /* Bundle return */ + /* Service Bundle returns */ public static final String RESULT_CODE = "result_code"; public static final String RESULT_SIGNATURE = "signature"; public static final String RESULT_ERRORS = "error"; @@ -46,4 +46,7 @@ public class OpenPgpConstants { // executeServiceMethod intent and do it again with params from intent public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; + /* PendingIntent returns */ + public static final String PI_RESULT_PARAMS = "params"; + } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java index 4c65f6b70..0f5aa9e3b 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java @@ -56,7 +56,7 @@ public class OpenPgpService extends RemoteService { * @param encryptionUserIds * @return */ - private Bundle getKeyIdsFromEmails(String[] encryptionUserIds) { + private Bundle getKeyIdsFromEmails(Bundle params, String[] encryptionUserIds) { // find key ids to given emails in database ArrayList keyIds = new ArrayList(); @@ -97,6 +97,7 @@ public class OpenPgpService extends RemoteService { intent.putExtra(RemoteServiceActivity.EXTRA_SELECTED_MASTER_KEY_IDS, keyIdsArray); intent.putExtra(RemoteServiceActivity.EXTRA_MISSING_USER_IDS, missingUserIds); intent.putExtra(RemoteServiceActivity.EXTRA_DUBLICATE_USER_IDS, dublicateUserIds); + intent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params); PendingIntent pi = PendingIntent.getActivity(getBaseContext(), PRIVATE_REQUEST_CODE_USER_IDS, intent, 0); @@ -118,11 +119,13 @@ public class OpenPgpService extends RemoteService { return result; } - private Bundle getPassphraseBundleIntent(long keyId) { + private Bundle getPassphraseBundleIntent(Bundle params, 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); + // pass params through to activity that it can be returned again later to repeat pgp operation + intent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params); PendingIntent pi = PendingIntent.getActivity(getBaseContext(), PRIVATE_REQUEST_CODE_PASSPHRASE, intent, 0); // return PendingIntent to be executed by client @@ -141,9 +144,8 @@ public class OpenPgpService extends RemoteService { String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId()); if (passphrase == null) { // get PendingIntent for passphrase input, add it to given params and return to client - Bundle passphraseBundle = getPassphraseBundleIntent(appSettings.getKeyId()); - params.putAll(passphraseBundle); - return params; + Bundle passphraseBundle = getPassphraseBundleIntent(params, appSettings.getKeyId()); + return passphraseBundle; } // Get Input- and OutputStream from ParcelFileDescriptor @@ -185,15 +187,14 @@ public class OpenPgpService extends RemoteService { } else { // get key ids based on given user ids String[] userIds = params.getStringArray(OpenPgpConstants.PARAMS_USER_IDS); - Bundle result = getKeyIdsFromEmails(userIds); + // give params through to activity... + Bundle result = getKeyIdsFromEmails(params, userIds); if (result.getInt(OpenPgpConstants.RESULT_CODE, 0) == OpenPgpConstants.RESULT_CODE_SUCCESS) { keyIds = result.getLongArray(OpenPgpConstants.PARAMS_KEY_IDS); } else { // if not success -> result contains a PendingIntent for user interaction - // return all old params with the new PendingIntent to client! - params.putAll(result); - return params; + return result; } } @@ -215,9 +216,8 @@ public class OpenPgpService extends RemoteService { appSettings.getKeyId()); if (passphrase == null) { // get PendingIntent for passphrase input, add it to given params and return to client - Bundle passphraseBundle = getPassphraseBundleIntent(appSettings.getKeyId()); - params.putAll(passphraseBundle); - return params; + Bundle passphraseBundle = getPassphraseBundleIntent(params, appSettings.getKeyId()); + return passphraseBundle; } operation.signAndEncrypt(asciiArmor, appSettings.getCompression(), keyIds, null, @@ -338,9 +338,8 @@ public class OpenPgpService extends RemoteService { String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId()); if (passphrase == null) { // get PendingIntent for passphrase input, add it to given params and return to client - Bundle passphraseBundle = getPassphraseBundleIntent(appSettings.getKeyId()); - params.putAll(passphraseBundle); - return params; + Bundle passphraseBundle = getPassphraseBundleIntent(params, appSettings.getKeyId()); + return passphraseBundle; } // } @@ -407,10 +406,8 @@ public class OpenPgpService extends RemoteService { private Bundle getKeyIdsImpl(Bundle params) { // get key ids based on given user ids String[] userIds = params.getStringArray(OpenPgpConstants.PARAMS_USER_IDS); - Bundle result = getKeyIdsFromEmails(userIds); - - params.putAll(result); - return params; + Bundle result = getKeyIdsFromEmails(params, userIds); + return result; } /** diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/RemoteServiceActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/RemoteServiceActivity.java index 4ca713c1d..8f619f37e 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/RemoteServiceActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/RemoteServiceActivity.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Dominik Schürmann + * Copyright (C) 2013-2014 Dominik Schürmann * * 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 @@ -17,7 +17,14 @@ package org.sufficientlysecure.keychain.service.remote; -import java.util.ArrayList; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.support.v7.app.ActionBarActivity; +import android.view.View; import org.openintents.openpgp.util.OpenPgpConstants; import org.sufficientlysecure.htmltextview.HtmlTextView; @@ -31,15 +38,7 @@ import org.sufficientlysecure.keychain.ui.SelectPublicKeyFragment; import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Log; -import android.content.Intent; -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.os.Messenger; -import android.os.RemoteException; -import android.support.v7.app.ActionBarActivity; -import android.view.View; -import android.widget.Toast; +import java.util.ArrayList; public class RemoteServiceActivity extends ActionBarActivity { @@ -102,11 +101,7 @@ public class RemoteServiceActivity extends ActionBarActivity { finishHandled = false; String action = intent.getAction(); - Bundle extras = intent.getExtras(); - - if (extras == null) { - extras = new Bundle(); - } + final Bundle extras = intent.getExtras(); mMessenger = extras.getParcelable(EXTRA_MESSENGER); @@ -176,8 +171,9 @@ public class RemoteServiceActivity extends ActionBarActivity { mSettingsFragment.setAppSettings(settings); } else if (ACTION_CACHE_PASSPHRASE.equals(action)) { long secretKeyId = extras.getLong(EXTRA_SECRET_KEY_ID); + Bundle oldParams = extras.getBundle(OpenPgpConstants.PI_RESULT_PARAMS); - showPassphraseDialog(secretKeyId); + showPassphraseDialog(oldParams, secretKeyId); } else if (ACTION_SELECT_PUB_KEYS.equals(action)) { long[] selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS); ArrayList missingUserIds = intent @@ -213,10 +209,13 @@ public class RemoteServiceActivity extends ActionBarActivity { new View.OnClickListener() { @Override public void onClick(View v) { - // return key ids to requesting activity - Intent finishIntent = new Intent(); - finishIntent.putExtra(OpenPgpConstants.PARAMS_KEY_IDS, + // sdd key ids to params Bundle for new request + Bundle params = extras.getBundle(OpenPgpConstants.PI_RESULT_PARAMS); + params.putLongArray(OpenPgpConstants.PARAMS_KEY_IDS, mSelectFragment.getSelectedMasterKeyIds()); + + Intent finishIntent = new Intent(); + finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params); setResult(RESULT_OK, finishIntent); finish(); } @@ -289,13 +288,16 @@ public class RemoteServiceActivity extends ActionBarActivity { * encryption. Based on mSecretKeyId it asks for a passphrase to open a private key or it asks * for a symmetric passphrase */ - private void showPassphraseDialog(long secretKeyId) { + private void showPassphraseDialog(final Bundle params, long secretKeyId) { // Message is received after passphrase is cached Handler returnHandler = new Handler() { @Override public void handleMessage(Message message) { if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { - RemoteServiceActivity.this.setResult(RESULT_OK); + // return given params again, for calling the service method again + Intent finishIntent = new Intent(); + finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params); + RemoteServiceActivity.this.setResult(RESULT_OK, finishIntent); } else { RemoteServiceActivity.this.setResult(RESULT_CANCELED); } @@ -314,8 +316,11 @@ public class RemoteServiceActivity extends ActionBarActivity { passphraseDialog.show(getSupportFragmentManager(), "passphraseDialog"); } catch (PgpGeneralException e) { Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!"); - RemoteServiceActivity.this.setResult(RESULT_OK); - RemoteServiceActivity.this.finish(); + // return given params again, for calling the service method again + Intent finishIntent = new Intent(); + finishIntent.putExtras(params); + setResult(RESULT_OK, finishIntent); + finish(); } } } diff --git a/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java b/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java index 5f67ddf88..5c38d3304 100644 --- a/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java +++ b/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpApi.java @@ -131,11 +131,7 @@ public class OpenPgpApi { try { params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); - // default result is error Bundle result = new Bundle(); - result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR); - result.putParcelable(OpenPgpConstants.RESULT_ERRORS, - new OpenPgpError(OpenPgpError.GENERIC_ERROR, "This should never happen!")); if (operationId == OPERATION_GET_KEY_IDS) { result = mService.getKeyIds(params); diff --git a/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java b/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java index 64bc92fdd..229c8d42a 100644 --- a/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java +++ b/libraries/keychain-api-library/src/main/java/org/openintents/openpgp/util/OpenPgpConstants.java @@ -33,7 +33,7 @@ public class OpenPgpConstants { public static final String PARAMS_USER_IDS = "user_ids"; public static final String PARAMS_KEY_IDS = "key_ids"; - /* Bundle return */ + /* Service Bundle returns */ public static final String RESULT_CODE = "result_code"; public static final String RESULT_SIGNATURE = "signature"; public static final String RESULT_ERRORS = "error"; @@ -46,4 +46,7 @@ public class OpenPgpConstants { // executeServiceMethod intent and do it again with params from intent public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; + /* PendingIntent returns */ + public static final String PI_RESULT_PARAMS = "params"; + }