From d090d4d332c9b1f5174890de531e2cc6936c038b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 4 Feb 2014 19:53:50 +0100 Subject: work on lookup key, fix some illegal state exceptions with hack --- .../java/org/sufficientlysecure/keychain/Id.java | 2 +- .../keychain/pgp/PgpOperation.java | 12 +- .../keychain/service/KeychainIntentService.java | 6 +- .../service/KeychainIntentServiceHandler.java | 10 +- .../keychain/service/remote/OpenPgpService.java | 3 +- .../keychain/ui/DecryptActivity.java | 76 +++++------- .../keychain/ui/ViewKeyActivity.java | 6 +- .../ui/dialog/LookupUnknownKeyDialogFragment.java | 136 --------------------- 8 files changed, 46 insertions(+), 205 deletions(-) delete mode 100644 OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/LookupUnknownKeyDialogFragment.java (limited to 'OpenPGP-Keychain/src/main/java/org') diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/Id.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/Id.java index 22f30cb1f..a1571e491 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/Id.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/Id.java @@ -78,7 +78,7 @@ public final class Id { public static final int filename = 0x00007003; // public static final int output_filename = 0x00007004; public static final int key_server_preference = 0x00007005; - public static final int look_up_key_id = 0x00007006; +// public static final int look_up_key_id = 0x00007006; public static final int export_to_server = 0x00007007; public static final int import_from_qr_code = 0x00007008; public static final int sign_key = 0x00007009; diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpOperation.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpOperation.java index 1402be435..1b08b65a1 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpOperation.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpOperation.java @@ -789,7 +789,7 @@ public class PgpOperation { return returnData; } - public Bundle verifyText(boolean lookupUnknownKey) throws IOException, PgpGeneralException, + public Bundle verifyText() throws IOException, PgpGeneralException, PGPException, SignatureException { Bundle returnData = new Bundle(); @@ -837,16 +837,6 @@ public class PgpOperation { if (signatureKeyId == 0) { signatureKeyId = signature.getKeyID(); } - // if key is not known and we want to lookup unknown ones... - if (signatureKey == null && lookupUnknownKey) { - - returnData = new Bundle(); - returnData.putLong(KeychainIntentService.RESULT_SIGNATURE_KEY_ID, signatureKeyId); - returnData.putBoolean(KeychainIntentService.RESULT_SIGNATURE_LOOKUP_KEY, true); - - // return directly now, decrypt will be done again after importing unknown key - return returnData; - } if (signatureKey == null) { signature = null; diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index ddb53736a..870e45ea5 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -123,7 +123,6 @@ public class KeychainIntentService extends IntentService implements ProgressDial public static final String DECRYPT_RETURN_BYTES = "return_binary"; public static final String DECRYPT_CIPHERTEXT_BYTES = "ciphertext_bytes"; public static final String DECRYPT_ASSUME_SYMMETRIC = "assume_symmetric"; - public static final String DECRYPT_LOOKUP_UNKNOWN_KEY = "lookup_unknownKey"; // save keyring public static final String SAVE_KEYRING_NEW_PASSPHRASE = "new_passphrase"; @@ -188,7 +187,6 @@ public class KeychainIntentService extends IntentService implements ProgressDial public static final String RESULT_SIGNATURE_SUCCESS = "signature_success"; public static final String RESULT_SIGNATURE_UNKNOWN = "signature_unknown"; - public static final String RESULT_SIGNATURE_LOOKUP_KEY = "lookup_key"; // import public static final String RESULT_IMPORT_ADDED = "added"; @@ -395,8 +393,6 @@ public class KeychainIntentService extends IntentService implements ProgressDial boolean returnBytes = data.getBoolean(DECRYPT_RETURN_BYTES); boolean assumeSymmetricEncryption = data.getBoolean(DECRYPT_ASSUME_SYMMETRIC); - boolean lookupUnknownKey = data.getBoolean(DECRYPT_LOOKUP_UNKNOWN_KEY); - InputStream inStream = null; long inLength = -1; InputData inputData = null; @@ -472,7 +468,7 @@ public class KeychainIntentService extends IntentService implements ProgressDial // verification of signatures PgpOperation operation = new PgpOperation(this, this, inputData, outStream); if (signedOnly) { - resultData = operation.verifyText(lookupUnknownKey); + resultData = operation.verifyText(); } else { resultData = operation.decryptAndVerify( PassphraseCacheService.getCachedPassphrase(this, secretKeyId), diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java index 5a338c757..a9ec291e3 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java @@ -25,6 +25,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; import android.widget.Toast; public class KeychainIntentServiceHandler extends Handler { @@ -60,7 +61,14 @@ public class KeychainIntentServiceHandler extends Handler { } public void showProgressDialog(FragmentActivity activity) { - mProgressDialogFragment.show(activity.getSupportFragmentManager(), "progressDialog"); + // TODO: THis is a hack!, see http://stackoverflow.com/questions/10114324/show-dialogfragment-from-onactivityresult + final FragmentManager manager = activity.getSupportFragmentManager(); + Handler handler = new Handler(); + handler.post(new Runnable() { + public void run() { + mProgressDialogFragment.show(manager, "progressDialog"); + } + }); } @Override 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 575f76a22..6db091ed0 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 @@ -420,8 +420,7 @@ public class OpenPgpService extends RemoteService { Bundle outputBundle; PgpOperation operation = new PgpOperation(getContext(), null, inputData, outputStream); if (signedOnly) { - // TODO: download missing keys from keyserver? - outputBundle = operation.verifyText(false); + outputBundle = operation.verifyText(); } else { outputBundle = operation.decryptAndVerify(passphrase, false); } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java index 26657479f..ccadaf39e 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java @@ -43,7 +43,6 @@ import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.ui.dialog.DeleteFileDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.FileDialogFragment; -import org.sufficientlysecure.keychain.ui.dialog.LookupUnknownKeyDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Log; @@ -62,6 +61,7 @@ import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; +import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import android.widget.ViewFlipper; @@ -78,6 +78,9 @@ public class DecryptActivity extends DrawerActivity { /* EXTRA keys for input */ public static final String EXTRA_TEXT = "text"; + private static final int RESULT_CODE_LOOKUP_KEY = 0x00007006; + private static final int RESULT_CODE_FILE = 0x00007003; + private long mSignatureKeyId = 0; private boolean mReturnResult = false; @@ -85,7 +88,7 @@ public class DecryptActivity extends DrawerActivity { private boolean mAssumeSymmetricEncryption = false; private EditText mMessage = null; - private LinearLayout mSignatureLayout = null; + private RelativeLayout mSignatureLayout = null; private ImageView mSignatureStatusImage = null; private TextView mUserId = null; private TextView mUserIdRest = null; @@ -100,6 +103,7 @@ public class DecryptActivity extends DrawerActivity { private EditText mFilename = null; private CheckBox mDeleteAfter = null; private BootstrapButton mBrowse = null; + private BootstrapButton mLookupKey = null; private String mInputFilename = null; private String mOutputFilename = null; @@ -107,14 +111,10 @@ public class DecryptActivity extends DrawerActivity { private Uri mContentUri = null; private boolean mReturnBinary = false; - private long mUnknownSignatureKeyId = 0; - private long mSecretKeyId = Id.key.none; private FileDialogFragment mFileDialog; - private boolean mLookupUnknownKey = true; - private boolean mDecryptImmediately = false; private BootstrapButton mDecryptButton; @@ -154,7 +154,7 @@ public class DecryptActivity extends DrawerActivity { mSourceLabel.setOnClickListener(nextSourceClickListener); mMessage = (EditText) findViewById(R.id.message); - mSignatureLayout = (LinearLayout) findViewById(R.id.signature); + mSignatureLayout = (RelativeLayout) findViewById(R.id.signature); mSignatureStatusImage = (ImageView) findViewById(R.id.ic_signature_status); mUserId = (TextView) findViewById(R.id.mainUserId); mUserIdRest = (TextView) findViewById(R.id.mainUserIdRest); @@ -171,7 +171,15 @@ public class DecryptActivity extends DrawerActivity { mBrowse.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { FileHelper.openFile(DecryptActivity.this, mFilename.getText().toString(), "*/*", - Id.request.filename); + RESULT_CODE_FILE); + } + }); + + mLookupKey = (BootstrapButton) findViewById(R.id.lookup_key); + mLookupKey.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + lookupUnknownKey(mSignatureKeyId); } }); @@ -587,28 +595,10 @@ public class DecryptActivity extends DrawerActivity { } private void lookupUnknownKey(long unknownKeyId) { - // Message is received after passphrase is cached - Handler returnHandler = new Handler() { - @Override - public void handleMessage(Message message) { - if (message.what == LookupUnknownKeyDialogFragment.MESSAGE_OKAY) { - // the result is handled by onActivityResult() as LookupUnknownKeyDialogFragment - // starts a new Intent which then returns data - } else if (message.what == LookupUnknownKeyDialogFragment.MESSAGE_CANCEL) { - // decrypt again, but don't lookup unknown keys! - mLookupUnknownKey = false; - decryptStart(); - } - } - }; - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(returnHandler); - - LookupUnknownKeyDialogFragment lookupKeyDialog = LookupUnknownKeyDialogFragment - .newInstance(messenger, unknownKeyId); - - lookupKeyDialog.show(getSupportFragmentManager(), "unknownKeyDialog"); + Intent intent = new Intent(this, ImportKeysActivity.class); + intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEY_SERVER); + intent.putExtra(ImportKeysActivity.EXTRA_KEY_ID, unknownKeyId); + startActivityForResult(intent, RESULT_CODE_LOOKUP_KEY); } private void decryptStart() { @@ -645,7 +635,6 @@ public class DecryptActivity extends DrawerActivity { data.putLong(KeychainIntentService.ENCRYPT_SECRET_KEY_ID, mSecretKeyId); data.putBoolean(KeychainIntentService.DECRYPT_SIGNED_ONLY, mSignedOnly); - data.putBoolean(KeychainIntentService.DECRYPT_LOOKUP_UNKNOWN_KEY, mLookupUnknownKey); data.putBoolean(KeychainIntentService.DECRYPT_RETURN_BYTES, mReturnBinary); data.putBoolean(KeychainIntentService.DECRYPT_ASSUME_SYMMETRIC, mAssumeSymmetricEncryption); @@ -662,15 +651,6 @@ public class DecryptActivity extends DrawerActivity { // get returned data bundle Bundle returnData = message.getData(); - // if key is unknown show lookup dialog - if (returnData.getBoolean(KeychainIntentService.RESULT_SIGNATURE_LOOKUP_KEY) - && mLookupUnknownKey) { - mUnknownSignatureKeyId = returnData - .getLong(KeychainIntentService.RESULT_SIGNATURE_KEY_ID); - lookupUnknownKey(mUnknownSignatureKeyId); - return; - } - mSignatureKeyId = 0; mSignatureLayout.setVisibility(View.GONE); @@ -727,14 +707,17 @@ public class DecryptActivity extends DrawerActivity { if (returnData.getBoolean(KeychainIntentService.RESULT_SIGNATURE_SUCCESS)) { mSignatureStatusImage.setImageResource(R.drawable.overlay_ok); + mLookupKey.setVisibility(View.GONE); } else if (returnData .getBoolean(KeychainIntentService.RESULT_SIGNATURE_UNKNOWN)) { mSignatureStatusImage.setImageResource(R.drawable.overlay_error); + mLookupKey.setVisibility(View.VISIBLE); Toast.makeText(DecryptActivity.this, - R.string.unknown_signature_key_touch_to_look_up, + R.string.unknown_signature, Toast.LENGTH_LONG).show(); } else { mSignatureStatusImage.setImageResource(R.drawable.overlay_error); + mLookupKey.setVisibility(View.GONE); } mSignatureLayout.setVisibility(View.VISIBLE); } @@ -756,7 +739,7 @@ public class DecryptActivity extends DrawerActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { - case Id.request.filename: { + case RESULT_CODE_FILE: { if (resultCode == RESULT_OK && data != null) { try { String path = FileHelper.getPath(this, data.getData()); @@ -772,20 +755,19 @@ public class DecryptActivity extends DrawerActivity { // this request is returned after LookupUnknownKeyDialogFragment started // ImportKeysActivity and user looked uo key - case Id.request.look_up_key_id: { + case RESULT_CODE_LOOKUP_KEY: { Log.d(Constants.TAG, "Returning from Lookup Key..."); - // decrypt again without lookup - mLookupUnknownKey = false; + // decrypt again decryptStart(); return; } default: { + super.onActivityResult(requestCode, resultCode, data); + break; } } - - super.onActivityResult(requestCode, resultCode, data); } } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index 0bfc51e67..1f4afcd8b 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -86,6 +86,8 @@ public class ViewKeyActivity extends ActionBarActivity implements private ViewKeyUserIdsAdapter mUserIdsAdapter; private ViewKeyKeysAdapter mKeysAdapter; + private static final int RESULT_CODE_LOOKUP_KEY = 0x00007006; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -399,8 +401,8 @@ public class ViewKeyActivity extends ActionBarActivity implements queryIntent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEY_SERVER); queryIntent.putExtra(ImportKeysActivity.EXTRA_KEY_ID, updateKeyId); - // TODO: lookup?? - startActivityForResult(queryIntent, Id.request.look_up_key_id); + // TODO: lookup with onactivityresult! + startActivityForResult(queryIntent, RESULT_CODE_LOOKUP_KEY); } private void encryptToContact(Uri dataUri) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/LookupUnknownKeyDialogFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/LookupUnknownKeyDialogFragment.java deleted file mode 100644 index a0592285f..000000000 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/LookupUnknownKeyDialogFragment.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) 2012-2013 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.sufficientlysecure.keychain.ui.dialog; - -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.Id; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; -import org.sufficientlysecure.keychain.ui.ImportKeysActivity; -import org.sufficientlysecure.keychain.util.Log; - -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.DialogInterface; -import android.content.DialogInterface.OnCancelListener; -import android.content.Intent; -import android.os.Bundle; -import android.os.Message; -import android.os.Messenger; -import android.os.RemoteException; -import android.support.v4.app.DialogFragment; - -public class LookupUnknownKeyDialogFragment extends DialogFragment { - private static final String ARG_MESSENGER = "messenger"; - private static final String ARG_UNKNOWN_KEY_ID = "unknown_key_id"; - - public static final int MESSAGE_OKAY = 1; - public static final int MESSAGE_CANCEL = 2; - - private Messenger mMessenger; - - /** - * Creates new instance of this dialog fragment - * - * @param messenger - * @param unknownKeyId - * @return - */ - public static LookupUnknownKeyDialogFragment newInstance(Messenger messenger, long unknownKeyId) { - LookupUnknownKeyDialogFragment frag = new LookupUnknownKeyDialogFragment(); - Bundle args = new Bundle(); - args.putLong(ARG_UNKNOWN_KEY_ID, unknownKeyId); - args.putParcelable(ARG_MESSENGER, messenger); - - frag.setArguments(args); - - return frag; - } - - /** - * Creates dialog - */ - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - final Activity activity = getActivity(); - - final long unknownKeyId = getArguments().getLong(ARG_UNKNOWN_KEY_ID); - mMessenger = getArguments().getParcelable(ARG_MESSENGER); - - AlertDialog.Builder alert = new AlertDialog.Builder(activity); - - alert.setIcon(android.R.drawable.ic_dialog_alert); - alert.setTitle(R.string.title_unknown_signature_key); - alert.setMessage(getString(R.string.lookup_unknown_key, - PgpKeyHelper.convertKeyIdToHex(unknownKeyId))); - - alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int id) { - dismiss(); - - sendMessageToHandler(MESSAGE_OKAY); - - Intent intent = new Intent(activity, ImportKeysActivity.class); - intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEY_SERVER); - intent.putExtra(ImportKeysActivity.EXTRA_KEY_ID, unknownKeyId); - startActivityForResult(intent, Id.request.look_up_key_id); - } - }); - alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int id) { - dismiss(); - - sendMessageToHandler(MESSAGE_CANCEL); - } - }); - alert.setCancelable(true); - alert.setOnCancelListener(new OnCancelListener() { - - @Override - public void onCancel(DialogInterface dialog) { - sendMessageToHandler(MESSAGE_CANCEL); - } - }); - - return alert.create(); - } - - /** - * Send message back to handler which is initialized in a activity - * - * @param what - * Message integer you want to send - */ - private void sendMessageToHandler(Integer what) { - Message msg = Message.obtain(); - msg.what = what; - - try { - mMessenger.send(msg); - } catch (RemoteException e) { - Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); - } catch (NullPointerException e) { - Log.w(Constants.TAG, "Messenger is null!", e); - } - } -} \ No newline at end of file -- cgit v1.2.3