fix passtrough of params

This commit is contained in:
Dominik Schürmann
2014-02-15 02:08:27 +01:00
parent 494a5fa414
commit 5f39cb3ec0
7 changed files with 75 additions and 94 deletions

View File

@@ -167,8 +167,7 @@ public class OpenPgpProviderActivity extends Activity {
PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT); PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
try { try {
OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(), OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
REQUEST_CODE_SIGN, null, REQUEST_CODE_SIGN, null, 0, 0, 0);
0, 0, 0);
} catch (IntentSender.SendIntentException e) { } catch (IntentSender.SendIntentException e) {
Log.e(Constants.TAG, "SendIntentException", e); Log.e(Constants.TAG, "SendIntentException", e);
} }
@@ -206,8 +205,7 @@ public class OpenPgpProviderActivity extends Activity {
PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT); PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
try { try {
OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(), OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
REQUEST_CODE_ENCRYPT, null, REQUEST_CODE_ENCRYPT, null, 0, 0, 0);
0, 0, 0);
} catch (IntentSender.SendIntentException e) { } catch (IntentSender.SendIntentException e) {
Log.e(Constants.TAG, "SendIntentException", e); Log.e(Constants.TAG, "SendIntentException", e);
} }
@@ -245,8 +243,7 @@ public class OpenPgpProviderActivity extends Activity {
PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT); PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
try { try {
OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(), OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
REQUEST_CODE_SIGN_AND_ENCRYPT, null, REQUEST_CODE_SIGN_AND_ENCRYPT, null, 0, 0, 0);
0, 0, 0);
} catch (IntentSender.SendIntentException e) { } catch (IntentSender.SendIntentException e) {
Log.e(Constants.TAG, "SendIntentException", e); Log.e(Constants.TAG, "SendIntentException", e);
} }
@@ -283,8 +280,7 @@ public class OpenPgpProviderActivity extends Activity {
PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT); PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
try { try {
OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(), OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
REQUEST_CODE_DECRYPT_AND_VERIFY, null, REQUEST_CODE_DECRYPT_AND_VERIFY, null, 0, 0, 0);
0, 0, 0);
} catch (IntentSender.SendIntentException e) { } catch (IntentSender.SendIntentException e) {
Log.e(Constants.TAG, "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) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
Log.d(Constants.TAG, "onActivityResult"); Log.d(Constants.TAG, "onActivityResult resultCode: " + resultCode);
switch (requestCode) {
case REQUEST_CODE_SIGN: {
Log.d(Constants.TAG, "resultCode: " + resultCode);
// try to sign again after password caching // try again after user interaction
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
sign(data.getExtras()); Bundle params = data.getBundleExtra(OpenPgpConstants.PI_RESULT_PARAMS);
}
break;
}
case REQUEST_CODE_ENCRYPT: {
Log.d(Constants.TAG, "resultCode: " + resultCode);
// try to sign again after password caching switch (requestCode) {
if (resultCode == RESULT_OK) { case REQUEST_CODE_SIGN: {
// use data extras now as params for call (they now include key ids! sign(params);
encrypt(data.getExtras()); break;
} }
break; case REQUEST_CODE_ENCRYPT: {
} encrypt(params);
case REQUEST_CODE_SIGN_AND_ENCRYPT: { break;
Log.d(Constants.TAG, "resultCode: " + resultCode);
// try to sign again after password caching
if (resultCode == RESULT_OK) {
signAndEncrypt(data.getExtras());
} }
break; case REQUEST_CODE_SIGN_AND_ENCRYPT: {
} signAndEncrypt(params);
case REQUEST_CODE_DECRYPT_AND_VERIFY: { break;
Log.d(Constants.TAG, "resultCode: " + resultCode); }
case REQUEST_CODE_DECRYPT_AND_VERIFY: {
// try to sign again after password caching decryptAndVerify(params);
if (resultCode == RESULT_OK) { break;
decryptAndVerify(new Bundle());
} }
break;
} }
} }
} }

View File

@@ -131,11 +131,7 @@ public class OpenPgpApi {
try { try {
params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION);
// default result is error
Bundle result = new Bundle(); 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) { if (operationId == OPERATION_GET_KEY_IDS) {
result = mService.getKeyIds(params); result = mService.getKeyIds(params);

View File

@@ -33,7 +33,7 @@ public class OpenPgpConstants {
public static final String PARAMS_USER_IDS = "user_ids"; public static final String PARAMS_USER_IDS = "user_ids";
public static final String PARAMS_KEY_IDS = "key_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_CODE = "result_code";
public static final String RESULT_SIGNATURE = "signature"; public static final String RESULT_SIGNATURE = "signature";
public static final String RESULT_ERRORS = "error"; public static final String RESULT_ERRORS = "error";
@@ -46,4 +46,7 @@ public class OpenPgpConstants {
// executeServiceMethod intent and do it again with params from intent // executeServiceMethod intent and do it again with params from intent
public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2;
/* PendingIntent returns */
public static final String PI_RESULT_PARAMS = "params";
} }

View File

@@ -56,7 +56,7 @@ public class OpenPgpService extends RemoteService {
* @param encryptionUserIds * @param encryptionUserIds
* @return * @return
*/ */
private Bundle getKeyIdsFromEmails(String[] encryptionUserIds) { private Bundle getKeyIdsFromEmails(Bundle params, String[] encryptionUserIds) {
// find key ids to given emails in database // find key ids to given emails in database
ArrayList<Long> keyIds = new ArrayList<Long>(); ArrayList<Long> keyIds = new ArrayList<Long>();
@@ -97,6 +97,7 @@ public class OpenPgpService extends RemoteService {
intent.putExtra(RemoteServiceActivity.EXTRA_SELECTED_MASTER_KEY_IDS, keyIdsArray); intent.putExtra(RemoteServiceActivity.EXTRA_SELECTED_MASTER_KEY_IDS, keyIdsArray);
intent.putExtra(RemoteServiceActivity.EXTRA_MISSING_USER_IDS, missingUserIds); intent.putExtra(RemoteServiceActivity.EXTRA_MISSING_USER_IDS, missingUserIds);
intent.putExtra(RemoteServiceActivity.EXTRA_DUBLICATE_USER_IDS, dublicateUserIds); 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); PendingIntent pi = PendingIntent.getActivity(getBaseContext(), PRIVATE_REQUEST_CODE_USER_IDS, intent, 0);
@@ -118,11 +119,13 @@ public class OpenPgpService extends RemoteService {
return result; return result;
} }
private Bundle getPassphraseBundleIntent(long keyId) { private Bundle getPassphraseBundleIntent(Bundle params, long keyId) {
// build PendingIntent for passphrase input // build PendingIntent for passphrase input
Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class); Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class);
intent.setAction(RemoteServiceActivity.ACTION_CACHE_PASSPHRASE); intent.setAction(RemoteServiceActivity.ACTION_CACHE_PASSPHRASE);
intent.putExtra(RemoteServiceActivity.EXTRA_SECRET_KEY_ID, keyId); 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); PendingIntent pi = PendingIntent.getActivity(getBaseContext(), PRIVATE_REQUEST_CODE_PASSPHRASE, intent, 0);
// return PendingIntent to be executed by client // return PendingIntent to be executed by client
@@ -141,9 +144,8 @@ public class OpenPgpService extends RemoteService {
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId()); String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId());
if (passphrase == null) { if (passphrase == null) {
// get PendingIntent for passphrase input, add it to given params and return to client // get PendingIntent for passphrase input, add it to given params and return to client
Bundle passphraseBundle = getPassphraseBundleIntent(appSettings.getKeyId()); Bundle passphraseBundle = getPassphraseBundleIntent(params, appSettings.getKeyId());
params.putAll(passphraseBundle); return passphraseBundle;
return params;
} }
// Get Input- and OutputStream from ParcelFileDescriptor // Get Input- and OutputStream from ParcelFileDescriptor
@@ -185,15 +187,14 @@ public class OpenPgpService extends RemoteService {
} else { } else {
// get key ids based on given user ids // get key ids based on given user ids
String[] userIds = params.getStringArray(OpenPgpConstants.PARAMS_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) { if (result.getInt(OpenPgpConstants.RESULT_CODE, 0) == OpenPgpConstants.RESULT_CODE_SUCCESS) {
keyIds = result.getLongArray(OpenPgpConstants.PARAMS_KEY_IDS); keyIds = result.getLongArray(OpenPgpConstants.PARAMS_KEY_IDS);
} else { } else {
// if not success -> result contains a PendingIntent for user interaction // if not success -> result contains a PendingIntent for user interaction
// return all old params with the new PendingIntent to client! return result;
params.putAll(result);
return params;
} }
} }
@@ -215,9 +216,8 @@ public class OpenPgpService extends RemoteService {
appSettings.getKeyId()); appSettings.getKeyId());
if (passphrase == null) { if (passphrase == null) {
// get PendingIntent for passphrase input, add it to given params and return to client // get PendingIntent for passphrase input, add it to given params and return to client
Bundle passphraseBundle = getPassphraseBundleIntent(appSettings.getKeyId()); Bundle passphraseBundle = getPassphraseBundleIntent(params, appSettings.getKeyId());
params.putAll(passphraseBundle); return passphraseBundle;
return params;
} }
operation.signAndEncrypt(asciiArmor, appSettings.getCompression(), keyIds, null, operation.signAndEncrypt(asciiArmor, appSettings.getCompression(), keyIds, null,
@@ -338,9 +338,8 @@ public class OpenPgpService extends RemoteService {
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId()); String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId());
if (passphrase == null) { if (passphrase == null) {
// get PendingIntent for passphrase input, add it to given params and return to client // get PendingIntent for passphrase input, add it to given params and return to client
Bundle passphraseBundle = getPassphraseBundleIntent(appSettings.getKeyId()); Bundle passphraseBundle = getPassphraseBundleIntent(params, appSettings.getKeyId());
params.putAll(passphraseBundle); return passphraseBundle;
return params;
} }
// } // }
@@ -407,10 +406,8 @@ public class OpenPgpService extends RemoteService {
private Bundle getKeyIdsImpl(Bundle params) { private Bundle getKeyIdsImpl(Bundle params) {
// get key ids based on given user ids // get key ids based on given user ids
String[] userIds = params.getStringArray(OpenPgpConstants.PARAMS_USER_IDS); String[] userIds = params.getStringArray(OpenPgpConstants.PARAMS_USER_IDS);
Bundle result = getKeyIdsFromEmails(userIds); Bundle result = getKeyIdsFromEmails(params, userIds);
return result;
params.putAll(result);
return params;
} }
/** /**

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de> * Copyright (C) 2013-2014 Dominik Schürmann <dominik@dominikschuermann.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -17,7 +17,14 @@
package org.sufficientlysecure.keychain.service.remote; 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.openintents.openpgp.util.OpenPgpConstants;
import org.sufficientlysecure.htmltextview.HtmlTextView; 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.ui.dialog.PassphraseDialogFragment;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import android.content.Intent; import java.util.ArrayList;
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;
public class RemoteServiceActivity extends ActionBarActivity { public class RemoteServiceActivity extends ActionBarActivity {
@@ -102,11 +101,7 @@ public class RemoteServiceActivity extends ActionBarActivity {
finishHandled = false; finishHandled = false;
String action = intent.getAction(); String action = intent.getAction();
Bundle extras = intent.getExtras(); final Bundle extras = intent.getExtras();
if (extras == null) {
extras = new Bundle();
}
mMessenger = extras.getParcelable(EXTRA_MESSENGER); mMessenger = extras.getParcelable(EXTRA_MESSENGER);
@@ -176,8 +171,9 @@ public class RemoteServiceActivity extends ActionBarActivity {
mSettingsFragment.setAppSettings(settings); mSettingsFragment.setAppSettings(settings);
} else if (ACTION_CACHE_PASSPHRASE.equals(action)) { } else if (ACTION_CACHE_PASSPHRASE.equals(action)) {
long secretKeyId = extras.getLong(EXTRA_SECRET_KEY_ID); 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)) { } else if (ACTION_SELECT_PUB_KEYS.equals(action)) {
long[] selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS); long[] selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS);
ArrayList<String> missingUserIds = intent ArrayList<String> missingUserIds = intent
@@ -213,10 +209,13 @@ public class RemoteServiceActivity extends ActionBarActivity {
new View.OnClickListener() { new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// return key ids to requesting activity // sdd key ids to params Bundle for new request
Intent finishIntent = new Intent(); Bundle params = extras.getBundle(OpenPgpConstants.PI_RESULT_PARAMS);
finishIntent.putExtra(OpenPgpConstants.PARAMS_KEY_IDS, params.putLongArray(OpenPgpConstants.PARAMS_KEY_IDS,
mSelectFragment.getSelectedMasterKeyIds()); mSelectFragment.getSelectedMasterKeyIds());
Intent finishIntent = new Intent();
finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params);
setResult(RESULT_OK, finishIntent); setResult(RESULT_OK, finishIntent);
finish(); 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 * encryption. Based on mSecretKeyId it asks for a passphrase to open a private key or it asks
* for a symmetric passphrase * for a symmetric passphrase
*/ */
private void showPassphraseDialog(long secretKeyId) { private void showPassphraseDialog(final Bundle params, long secretKeyId) {
// Message is received after passphrase is cached // Message is received after passphrase is cached
Handler returnHandler = new Handler() { Handler returnHandler = new Handler() {
@Override @Override
public void handleMessage(Message message) { public void handleMessage(Message message) {
if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { 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 { } else {
RemoteServiceActivity.this.setResult(RESULT_CANCELED); RemoteServiceActivity.this.setResult(RESULT_CANCELED);
} }
@@ -314,8 +316,11 @@ public class RemoteServiceActivity extends ActionBarActivity {
passphraseDialog.show(getSupportFragmentManager(), "passphraseDialog"); passphraseDialog.show(getSupportFragmentManager(), "passphraseDialog");
} catch (PgpGeneralException e) { } catch (PgpGeneralException e) {
Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!"); Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!");
RemoteServiceActivity.this.setResult(RESULT_OK); // return given params again, for calling the service method again
RemoteServiceActivity.this.finish(); Intent finishIntent = new Intent();
finishIntent.putExtras(params);
setResult(RESULT_OK, finishIntent);
finish();
} }
} }
} }

View File

@@ -131,11 +131,7 @@ public class OpenPgpApi {
try { try {
params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION); params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION);
// default result is error
Bundle result = new Bundle(); 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) { if (operationId == OPERATION_GET_KEY_IDS) {
result = mService.getKeyIds(params); result = mService.getKeyIds(params);

View File

@@ -33,7 +33,7 @@ public class OpenPgpConstants {
public static final String PARAMS_USER_IDS = "user_ids"; public static final String PARAMS_USER_IDS = "user_ids";
public static final String PARAMS_KEY_IDS = "key_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_CODE = "result_code";
public static final String RESULT_SIGNATURE = "signature"; public static final String RESULT_SIGNATURE = "signature";
public static final String RESULT_ERRORS = "error"; public static final String RESULT_ERRORS = "error";
@@ -46,4 +46,7 @@ public class OpenPgpConstants {
// executeServiceMethod intent and do it again with params from intent // executeServiceMethod intent and do it again with params from intent
public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2; public static final int RESULT_CODE_USER_INTERACTION_REQUIRED = 2;
/* PendingIntent returns */
public static final String PI_RESULT_PARAMS = "params";
} }