work on lookup key, fix some illegal state exceptions with hack

This commit is contained in:
Dominik Schürmann 2014-02-04 19:53:50 +01:00
parent b78a564de3
commit d090d4d332
10 changed files with 85 additions and 231 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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),

View File

@ -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

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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) {

View File

@ -1,136 +0,0 @@
/*
* Copyright (C) 2012-2013 Dominik Schürmann <dominik@dominikschuermann.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}
}
}

View File

@ -20,10 +20,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="4dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
<RelativeLayout
android:id="@+id/signature"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -35,7 +36,8 @@
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:id="@+id/relativeLayout">
<ImageView
android:id="@+id/ic_signature"
@ -50,29 +52,41 @@
android:src="@drawable/overlay_error" />
</RelativeLayout>
<LinearLayout
<com.beardedhen.androidbootstrap.BootstrapButton
android:id="@+id/lookup_key"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:padding="4dp"
android:text="@string/btn_lookup_key"
bootstrapbutton:bb_icon_left="fa-download"
bootstrapbutton:bb_type="info"
bootstrapbutton:bb_size="small"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/mainUserId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dip">
android:layout_gravity="left"
android:text="Main User Id"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignTop="@+id/linearLayout"
android:layout_toRightOf="@+id/relativeLayout" />
<TextView
android:id="@+id/mainUserId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Main User Id"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/mainUserIdRest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Main User Id Rest"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/mainUserIdRest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Main User Id Rest"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignBottom="@+id/relativeLayout"
android:layout_alignLeft="@+id/mainUserId"
android:layout_alignStart="@+id/mainUserId" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"

View File

@ -65,7 +65,7 @@
<string name="btn_back">Back</string>
<string name="btn_clipboard">Clipboard</string>
<string name="btn_share">Share with…</string>
<string name="btn_lookup_key">Lookup key</string>
<!-- menu -->
<string name="menu_preferences">Settings</string>
@ -233,15 +233,14 @@
<item quantity="other">Found %d keys.</item>
</plurals>
<string name="unknown_signature_key_touch_to_look_up">Unknown signature, touch to look up key.</string>
<string name="unknown_signature">Unknown signature, click button to lookup the missing key.</string>
<plurals name="bad_keys_encountered">
<item quantity="one">%d bad secret key ignored. Perhaps you exported with the option\n --export-secret-subkeys\nMake sure you export with\n --export-secret-keys\ninstead.</item>
<item quantity="other">%d bad secret keys ignored. Perhaps you exported with the option\n --export-secret-subkeys\nMake sure you export with\n --export-secret-keys\ninstead.</item>
</plurals>
<string name="lookup_unknown_key">Unknown key %s, do you want to try finding it on a keyserver?</string>
<string name="key_send_success">Successfully sent key to server</string>
<string name="key_send_success">Successfully uploaded key to server</string>
<string name="key_sign_success">Successfully signed key</string>
<string name="list_empty">This list is empty!</string>
<string name="nfc_successfull">Successfully sent key with NFC Beam!</string>