From e92971a6c0a3d9a5a8578b6fe671dcbfdd0b3656 Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 8 Sep 2012 22:45:12 +0200 Subject: [PATCH] lookup key fixes --- .../android/apg/helper/PGPMain.java | 14 ++- .../android/apg/service/ApgHandler.java | 4 - .../android/apg/service/ApgService.java | 12 ++- .../android/apg/ui/DecryptActivity.java | 95 ++++++++----------- 4 files changed, 62 insertions(+), 63 deletions(-) diff --git a/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java b/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java index 180716ef5..fbbe9199f 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java @@ -1636,8 +1636,8 @@ public class PGPMain { } public static Bundle verifyText(Context context, InputData data, OutputStream outStream, - ProgressDialogUpdater progress) throws IOException, GeneralException, PGPException, - SignatureException { + boolean lookupUnknownKey, ProgressDialogUpdater progress) throws IOException, + GeneralException, PGPException, SignatureException { Bundle returnData = new Bundle(); ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -1686,7 +1686,15 @@ public class PGPMain { if (signatureKeyId == 0) { signatureKeyId = signature.getKeyID(); } - if (signatureKey == null) { + // if key is not known and we want to lookup unknown ones... + if (signatureKey == null && lookupUnknownKey) { + + returnData = new Bundle(); + returnData.putLong(ApgService.RESULT_SIGNATURE_KEY_ID, signatureKeyId); + returnData.putBoolean(ApgService.RESULT_SIGNATURE_LOOKUP_KEY, true); + + return returnData; + // TODO: reimplement! // Bundle pauseData = new Bundle(); // pauseData.putInt(Constants.extras.STATUS, Id.message.unknown_signature_key); diff --git a/org_apg/src/org/thialfihar/android/apg/service/ApgHandler.java b/org_apg/src/org/thialfihar/android/apg/service/ApgHandler.java index d69d3fd4b..bb9e94938 100644 --- a/org_apg/src/org/thialfihar/android/apg/service/ApgHandler.java +++ b/org_apg/src/org/thialfihar/android/apg/service/ApgHandler.java @@ -31,10 +31,6 @@ public class ApgHandler extends Handler { public static final int MESSAGE_OKAY = 1; public static final int MESSAGE_EXCEPTION = 2; public static final int MESSAGE_UPDATE_PROGRESS = 3; - - // used in decrypt - public static final int MESSAGE_UNKOWN_KEY = 4; - // possible data keys for messages public static final String DATA_ERROR = "error"; diff --git a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java index 5e7daa48c..b6811d2ee 100644 --- a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java +++ b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java @@ -86,11 +86,12 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { public static final String OUTPUT_FILE = "outputFile"; public static final String PROVIDER_URI = "providerUri"; - // decrypt + // decrypt/verify public static final String SIGNED_ONLY = "signedOnly"; public static final String RETURN_BYTES = "returnBinary"; public static final String CIPHERTEXT_BYTES = "ciphertextBytes"; public static final String ASSUME_SYMMETRIC = "assumeSymmetric"; + public static final String LOOKUP_UNKNOWN_KEY = "lookup_unknown_key"; // edit keys public static final String NEW_PASSPHRASE = "newPassphrase"; @@ -132,14 +133,16 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { public static final String RESULT_ENCRYPTED_DATA = "encryptedData"; public static final String RESULT_URI = "resultUri"; - // decrypt + // decrypt/verify public static final String RESULT_DECRYPTED_MESSAGE = "decryptedMessage"; public static final String RESULT_DECRYPTED_DATA = "decryptedData"; public static final String RESULT_SIGNATURE = "signature"; public static final String RESULT_SIGNATURE_KEY_ID = "signatureKeyId"; public static final String RESULT_SIGNATURE_USER_ID = "signatureUserId"; + public static final String RESULT_SIGNATURE_SUCCESS = "signatureSuccess"; public static final String RESULT_SIGNATURE_UNKNOWN = "signatureUnknown"; + public static final String RESULT_SIGNATURE_LOOKUP_KEY = "lookupKey"; Messenger mMessenger; @@ -340,6 +343,8 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { boolean returnBytes = data.getBoolean(RETURN_BYTES); boolean assumeSymmetricEncryption = data.getBoolean(ASSUME_SYMMETRIC); + boolean lookupUnknownKey = data.getBoolean(LOOKUP_UNKNOWN_KEY); + InputStream inStream = null; long inLength = -1; InputData inputData = null; @@ -416,7 +421,8 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { // verifyText and decrypt returning additional resultData values for the // verification of signatures if (signedOnly) { - resultData = PGPMain.verifyText(this, inputData, outStream, this); + resultData = PGPMain.verifyText(this, inputData, outStream, lookupUnknownKey, + this); } else { resultData = PGPMain.decrypt(this, inputData, outStream, PGPMain.getCachedPassPhrase(secretKeyId), this, diff --git a/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java index 0c8ef4738..71bb3d30a 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java @@ -26,6 +26,7 @@ import org.thialfihar.android.apg.service.ApgHandler; import org.thialfihar.android.apg.service.ApgService; import org.thialfihar.android.apg.ui.dialog.DeleteFileDialogFragment; import org.thialfihar.android.apg.ui.dialog.FileDialogFragment; +import org.thialfihar.android.apg.ui.dialog.LookupUnknownKeyDialogFragment; import org.thialfihar.android.apg.ui.dialog.PassphraseDialogFragment; import org.thialfihar.android.apg.ui.dialog.ProgressDialogFragment; import org.thialfihar.android.apg.util.Compatibility; @@ -129,6 +130,8 @@ public class DecryptActivity extends SherlockFragmentActivity { private ProgressDialogFragment mDecryptingDialog; private FileDialogFragment mFileDialog; + private boolean mLookupUnknownKey = true; + public void setSecretKeyId(long id) { mSecretKeyId = id; } @@ -603,7 +606,6 @@ public class DecryptActivity extends SherlockFragmentActivity { inStream = new ByteArrayInputStream(mData); } else { inStream = new ByteArrayInputStream(mMessage.getText().toString().getBytes()); - } } @@ -666,6 +668,31 @@ public class DecryptActivity extends SherlockFragmentActivity { mFileDialog.show(getSupportFragmentManager(), "fileDialog"); } + 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"); + } + private void decryptStart() { Log.d(Constants.TAG, "decryptStart"); @@ -682,7 +709,6 @@ public class DecryptActivity extends SherlockFragmentActivity { data.putInt(ApgService.TARGET, ApgService.TARGET_STREAM); data.putString(ApgService.PROVIDER_URI, mContentUri.toString()); - } else if (mDecryptTarget == Id.target.file) { data.putInt(ApgService.TARGET, ApgService.TARGET_FILE); @@ -691,7 +717,6 @@ public class DecryptActivity extends SherlockFragmentActivity { data.putString(ApgService.INPUT_FILE, mInputFilename); data.putString(ApgService.OUTPUT_FILE, mOutputFilename); - } else { data.putInt(ApgService.TARGET, ApgService.TARGET_BYTES); @@ -706,6 +731,7 @@ public class DecryptActivity extends SherlockFragmentActivity { data.putLong(ApgService.SECRET_KEY_ID, getSecretKeyId()); data.putBoolean(ApgService.SIGNED_ONLY, mSignedOnly); + data.putBoolean(ApgService.LOOKUP_UNKNOWN_KEY, mLookupUnknownKey); data.putBoolean(ApgService.RETURN_BYTES, mReturnBinary); data.putBoolean(ApgService.ASSUME_SYMMETRIC, mAssumeSymmetricEncryption); @@ -725,6 +751,14 @@ public class DecryptActivity extends SherlockFragmentActivity { // get returned data bundle Bundle returnData = message.getData(); + // if key is unknown show lookup dialog + if (returnData.getBoolean(ApgService.RESULT_SIGNATURE_LOOKUP_KEY) && mLookupUnknownKey) { + mUnknownSignatureKeyId = returnData + .getLong(ApgService.RESULT_SIGNATURE_KEY_ID); + lookupUnknownKey(mUnknownSignatureKeyId); + return; + } + mSignatureKeyId = 0; mSignatureLayout.setVisibility(View.GONE); mReplyEnabled = false; @@ -842,14 +876,12 @@ public class DecryptActivity extends SherlockFragmentActivity { return; } - // this request is returned after the LookupUnknownKeyDialogFragment was displayed and the - // user choose okay + // this request is returned after LookupUnknownKeyDialogFragment started + // KeyServerQueryActivity and user looked uo key case Id.request.look_up_key_id: { - // TODO - // PausableThread thread = getRunningThread(); - // if (thread != null && thread.isPaused()) { - // thread.unpause(); - // } + // decrypt again without lookup + mLookupUnknownKey = false; + decryptStart(); return; } @@ -861,47 +893,4 @@ public class DecryptActivity extends SherlockFragmentActivity { super.onActivityResult(requestCode, resultCode, data); } - @Override - protected Dialog onCreateDialog(int id) { - switch (id) { - - case Id.dialog.lookup_unknown_key: { - AlertDialog.Builder alert = new AlertDialog.Builder(this); - - alert.setIcon(android.R.drawable.ic_dialog_alert); - alert.setTitle(R.string.title_unknownSignatureKey); - alert.setMessage(getString(R.string.lookupUnknownKey, - PGPHelper.getSmallFingerPrint(mUnknownSignatureKeyId))); - - alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - removeDialog(Id.dialog.lookup_unknown_key); - Intent intent = new Intent(DecryptActivity.this, KeyServerQueryActivity.class); - intent.setAction(KeyServerQueryActivity.ACTION_LOOK_UP_KEY_ID); - intent.putExtra(KeyServerQueryActivity.EXTRA_KEY_ID, mUnknownSignatureKeyId); - startActivityForResult(intent, Id.request.look_up_key_id); - } - }); - alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - removeDialog(Id.dialog.lookup_unknown_key); - // TODO - // PausableThread thread = getRunningThread(); - // if (thread != null && thread.isPaused()) { - // thread.unpause(); - // } - } - }); - alert.setCancelable(true); - - return alert.create(); - } - - default: { - break; - } - } - - return super.onCreateDialog(id); - } }