fix Encrypt* with RequiredInputParcel

This commit is contained in:
Vincent Breitmoser
2015-03-30 23:35:32 +02:00
parent d7b79e55fb
commit 39b131c7e5
21 changed files with 184 additions and 559 deletions

View File

@@ -66,7 +66,7 @@ public abstract class CryptoOperationFragment extends Fragment {
case REQUEST_CODE_PASSPHRASE: {
if (resultCode == Activity.RESULT_OK && data != null) {
CryptoInputParcel cryptoInput =
data.getParcelableExtra(PassphraseDialogActivity.RESULT_DATA);
data.getParcelableExtra(PassphraseDialogActivity.RESULT_CRYPTO_INPUT);
cryptoOperation(cryptoInput);
return;
}
@@ -110,6 +110,10 @@ public abstract class CryptoOperationFragment extends Fragment {
return false;
}
protected void cryptoOperation() {
cryptoOperation(new CryptoInputParcel());
}
protected abstract void cryptoOperation(CryptoInputParcel cryptoInput);
}

View File

@@ -198,7 +198,7 @@ public class DecryptFilesFragment extends DecryptFragment {
// data.putParcelable(KeychainIntentService.DECRYPT_PASSPHRASE, mPassphrase);
// data.putByteArray(KeychainIntentService.DECRYPT_NFC_DECRYPTED_SESSION_KEY, mNfcDecryptedSessionKey);
//
// intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
// intent.putExtra(KeychainIntentService.EXTRA_SERVICE_INTENT, data);
//
// // Message is received after decrypting is done in KeychainIntentService
// ServiceProgressHandler saveHandler = new ServiceProgressHandler(

View File

@@ -105,7 +105,7 @@ public abstract class DecryptFragment extends CryptoOperationFragment {
// // build PendingIntent for Yubikey NFC operations
// Intent intent = new Intent(getActivity(), NfcActivity.class);
// intent.setAction(NfcActivity.ACTION_DECRYPT_SESSION_KEY);
// intent.putExtra(NfcActivity.EXTRA_DATA, new Intent()); // not used, only relevant to OpenPgpService
// intent.putExtra(NfcActivity.EXTRA_SERVICE_INTENT, new Intent()); // not used, only relevant to OpenPgpService
// intent.putExtra(NfcActivity.EXTRA_KEY_ID, subKeyId);
// intent.putExtra(NfcActivity.EXTRA_PIN, pin);
//

View File

@@ -1,189 +0,0 @@
/*
* Copyright (C) 2014 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;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.view.View;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.PgpSignEncryptResult;
import org.sufficientlysecure.keychain.operations.results.SignEncryptResult;
import org.sufficientlysecure.keychain.pgp.SignEncryptParcel;
import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
import org.sufficientlysecure.keychain.ui.base.BaseActivity;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
import org.sufficientlysecure.keychain.util.Passphrase;
public abstract class EncryptActivity extends BaseActivity {
public static final int REQUEST_CODE_PASSPHRASE = 0x00008001;
public static final int REQUEST_CODE_NFC = 0x00008002;
// For NFC data
protected Passphrase mSigningKeyPassphrase = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setFullScreenDialogClose(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(Activity.RESULT_CANCELED);
finish();
}
}, false);
}
protected void startPassphraseDialog(long subkeyId) {
Intent intent = new Intent(this, PassphraseDialogActivity.class);
intent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID, subkeyId);
startActivityForResult(intent, REQUEST_CODE_PASSPHRASE);
}
protected void startNfcSign(long keyId, RequiredInputParcel nfcOps) {
Intent intent = new Intent(this, NfcOperationActivity.class);
intent.putExtra(NfcOperationActivity.EXTRA_REQUIRED_INPUT, nfcOps);
// TODO respect keyid(?)
startActivityForResult(intent, REQUEST_CODE_NFC);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_PASSPHRASE: {
if (resultCode == RESULT_OK && data != null) {
mSigningKeyPassphrase = data.getParcelableExtra(PassphraseDialogActivity.MESSAGE_DATA_PASSPHRASE);
startEncrypt();
return;
}
break;
}
case REQUEST_CODE_NFC: {
if (resultCode == RESULT_OK && data != null) {
CryptoInputParcel cryptoInput =
data.getParcelableExtra(NfcOperationActivity.RESULT_DATA);
startEncrypt(cryptoInput);
return;
}
break;
}
default: {
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
}
public void startEncrypt() {
startEncrypt(null);
}
public void startEncrypt(final CryptoInputParcel cryptoInput) {
if (!inputIsValid()) {
// Notify was created by inputIsValid.
return;
}
// Send all information needed to service to edit key in other thread
Intent intent = new Intent(this, KeychainIntentService.class);
intent.setAction(KeychainIntentService.ACTION_SIGN_ENCRYPT);
final SignEncryptParcel input = createEncryptBundle();
Bundle data = new Bundle();
data.putParcelable(KeychainIntentService.SIGN_ENCRYPT_PARCEL, input);
data.putParcelable(KeychainIntentService.EXTRA_CRYPTO_INPUT, cryptoInput);
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
// Message is received after encrypting is done in KeychainIntentService
ServiceProgressHandler serviceHandler = new ServiceProgressHandler(
this,
getString(R.string.progress_encrypting),
ProgressDialog.STYLE_HORIZONTAL,
ProgressDialogFragment.ServiceType.KEYCHAIN_INTENT) {
public void handleMessage(Message message) {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
SignEncryptResult result =
message.getData().getParcelable(SignEncryptResult.EXTRA_RESULT);
PgpSignEncryptResult pgpResult = result.getPending();
if (pgpResult != null && pgpResult.isPending()) {
if ((pgpResult.getResult() & PgpSignEncryptResult.RESULT_PENDING_PASSPHRASE) ==
PgpSignEncryptResult.RESULT_PENDING_PASSPHRASE) {
startPassphraseDialog(pgpResult.getKeyIdPassphraseNeeded());
} else if ((pgpResult.getResult() & PgpSignEncryptResult.RESULT_PENDING_NFC) ==
PgpSignEncryptResult.RESULT_PENDING_NFC) {
RequiredInputParcel parcel = RequiredInputParcel.createNfcSignOperation(
pgpResult.getNfcHash(),
pgpResult.getNfcAlgo(),
cryptoInput.getSignatureTime());
startNfcSign(pgpResult.getNfcKeyId(), parcel);
} else {
throw new RuntimeException("Unhandled pending result!");
}
return;
}
if (result.success()) {
onEncryptSuccess(result);
} else {
result.createNotify(EncryptActivity.this).show();
}
// no matter the result, reset parameters
mSigningKeyPassphrase = null;
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(serviceHandler);
intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);
// show progress dialog
serviceHandler.showProgressDialog(this);
// start service with intent
startService(intent);
}
protected abstract boolean inputIsValid();
protected abstract void onEncryptSuccess(SignEncryptResult result);
protected abstract SignEncryptParcel createEncryptBundle();
}

View File

@@ -363,7 +363,7 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
public void startEncrypt(boolean share) {
mShareAfterEncrypt = share;
cryptoOperation(new CryptoInputParcel());
cryptoOperation();
}
public void onEncryptSuccess(final SignEncryptResult result) {
@@ -512,36 +512,11 @@ public class EncryptFilesFragment extends CryptoOperationFragment {
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
SignEncryptResult result =
message.getData().getParcelable(SignEncryptResult.EXTRA_RESULT);
// PgpSignEncryptResult pgpResult = result.getPending();
//
// if (pgpResult != null && pgpResult.isPending()) {
// if ((pgpResult.getResult() & PgpSignEncryptResult.RESULT_PENDING_PASSPHRASE) ==
// PgpSignEncryptResult.RESULT_PENDING_PASSPHRASE) {
// startPassphraseDialog(pgpResult.getKeyIdPassphraseNeeded());
// } else if ((pgpResult.getResult() & PgpSignEncryptResult.RESULT_PENDING_NFC) ==
// PgpSignEncryptResult.RESULT_PENDING_NFC) {
//
// RequiredInputParcel parcel = RequiredInputParcel.createNfcSignOperation(
// pgpResult.getNfcHash(),
// pgpResult.getNfcAlgo(),
// input.getSignatureTime());
// startNfcSign(pgpResult.getNfcKeyId(), parcel);
//
// } else {
// throw new RuntimeException("Unhandled pending result!");
// }
// return;
// }
if (result.success()) {
onEncryptSuccess(result);
} else {
result.createNotify(getActivity()).show();
}
// no matter the result, reset parameters
// mSigningKeyPassphrase = null;
}
}
};

View File

@@ -160,6 +160,6 @@ public class EncryptTextActivity extends BaseActivity implements
@Override
public void onPassphraseChanged(Passphrase passphrase) {
mEncryptFragment.setPassphrase(passphrase);
mEncryptFragment.setSymmetricPassphrase(passphrase);
}
}

View File

@@ -37,7 +37,6 @@ import org.spongycastle.bcpg.CompressionAlgorithmTags;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.ClipboardReflection;
import org.sufficientlysecure.keychain.operations.results.PgpSignEncryptResult;
import org.sufficientlysecure.keychain.operations.results.SignEncryptResult;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.PgpConstants;
@@ -73,7 +72,7 @@ public class EncryptTextFragment extends CryptoOperationFragment {
private String mEncryptionUserIds[] = null;
// TODO Constants.key.none? What's wrong with a null value?
private long mSigningKeyId = Constants.key.none;
private Passphrase mPassphrase = new Passphrase();
private Passphrase mSymmetricPassphrase = new Passphrase();
private String mMessage = "";
private TextView mText;
@@ -90,8 +89,8 @@ public class EncryptTextFragment extends CryptoOperationFragment {
mSigningKeyId = signingKeyId;
}
public void setPassphrase(Passphrase passphrase) {
mPassphrase = passphrase;
public void setSymmetricPassphrase(Passphrase passphrase) {
mSymmetricPassphrase = passphrase;
}
/**
@@ -233,7 +232,7 @@ public class EncryptTextFragment extends CryptoOperationFragment {
if (mSymmetricMode) {
Log.d(Constants.TAG, "Symmetric encryption enabled!");
Passphrase passphrase = mPassphrase;
Passphrase passphrase = mSymmetricPassphrase;
if (passphrase.isEmpty()) {
passphrase = null;
}
@@ -241,7 +240,6 @@ public class EncryptTextFragment extends CryptoOperationFragment {
} else {
data.setEncryptionMasterKeyIds(mEncryptionKeyIds);
data.setSignatureMasterKeyId(mSigningKeyId);
// data.setSignaturePassphrase(mSigningKeyPassphrase);
}
return data;
}
@@ -296,12 +294,12 @@ public class EncryptTextFragment extends CryptoOperationFragment {
if (mSymmetricMode) {
// symmetric encryption checks
if (mPassphrase == null) {
if (mSymmetricPassphrase == null) {
Notify.create(getActivity(), R.string.passphrases_do_not_match, Notify.Style.ERROR)
.show();
return false;
}
if (mPassphrase.isEmpty()) {
if (mSymmetricPassphrase.isEmpty()) {
Notify.create(getActivity(), R.string.passphrase_must_not_be_empty, Notify.Style.ERROR)
.show();
return false;
@@ -325,11 +323,7 @@ public class EncryptTextFragment extends CryptoOperationFragment {
public void startEncrypt(boolean share) {
mShareAfterEncrypt = share;
startEncrypt();
}
public void startEncrypt() {
cryptoOperation(null);
cryptoOperation();
}
@Override
@@ -359,45 +353,19 @@ public class EncryptTextFragment extends CryptoOperationFragment {
// handle messages by standard KeychainIntentServiceHandler first
super.handleMessage(message);
// TODO: We need a InputPendingResult!
// // handle pending messages
// if (handlePendingMessage(message)) {
// return;
// }
if (handlePendingMessage(message)) {
return;
}
if (message.arg1 == MessageStatus.OKAY.ordinal()) {
SignEncryptResult result =
message.getData().getParcelable(SignEncryptResult.EXTRA_RESULT);
PgpSignEncryptResult pgpResult = result.getPending();
// if (pgpResult != null && pgpResult.isPending()) {
// if ((pgpResult.getResult() & PgpSignEncryptResult.RESULT_PENDING_PASSPHRASE) ==
// PgpSignEncryptResult.RESULT_PENDING_PASSPHRASE) {
// startPassphraseDialog(pgpResult.getKeyIdPassphraseNeeded());
// } else if ((pgpResult.getResult() & PgpSignEncryptResult.RESULT_PENDING_NFC) ==
// PgpSignEncryptResult.RESULT_PENDING_NFC) {
//
// RequiredInputParcel parcel = RequiredInputParcel.createNfcSignOperation(
// pgpResult.getNfcHash(),
// pgpResult.getNfcAlgo(),
// input.getSignatureTime());
// startNfcSign(pgpResult.getNfcKeyId(), parcel);
//
// } else {
// throw new RuntimeException("Unhandled pending result!");
// }
// return;
// }
if (result.success()) {
onEncryptSuccess(result);
} else {
result.createNotify(getActivity()).show();
}
// no matter the result, reset parameters
// mSigningKeyPassphrase = null;
}
}
};

View File

@@ -100,7 +100,7 @@ public class NfcActivity extends BaseActivity {
Log.d(Constants.TAG, "NfcActivity mAction: " + mAction);
Log.d(Constants.TAG, "NfcActivity mPin: " + mPin);
Log.d(Constants.TAG, "NfcActivity mHashToSign as hex: " + getHex(mHashToSign));
Log.d(Constants.TAG, "NfcActivity mServiceIntent: " + mServiceIntent.toString());
Log.d(Constants.TAG, "NfcActivity mServiceIntent: " + mServiceIntent);
break;
case ACTION_DECRYPT_SESSION_KEY:
mAction = action;
@@ -111,7 +111,7 @@ public class NfcActivity extends BaseActivity {
Log.d(Constants.TAG, "NfcActivity mAction: " + mAction);
Log.d(Constants.TAG, "NfcActivity mPin: " + mPin);
Log.d(Constants.TAG, "NfcActivity mEncryptedSessionKey as hex: " + getHex(mEncryptedSessionKey));
Log.d(Constants.TAG, "NfcActivity mServiceIntent: " + mServiceIntent.toString());
Log.d(Constants.TAG, "NfcActivity mServiceIntent: " + mServiceIntent);
break;
case NfcAdapter.ACTION_TAG_DISCOVERED:
Log.e(Constants.TAG, "This should not happen! NfcActivity.onCreate() is being called instead of onNewIntent()!");

View File

@@ -10,8 +10,10 @@ import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.WindowManager;
import org.openintents.openpgp.util.OpenPgpApi;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
@@ -34,9 +36,13 @@ public class NfcOperationActivity extends BaseNfcActivity {
public static final String EXTRA_REQUIRED_INPUT = "required_input";
// passthrough for OpenPgpService
public static final String EXTRA_SERVICE_INTENT = "data";
public static final String RESULT_DATA = "result_data";
RequiredInputParcel mRequiredInput;
private RequiredInputParcel mRequiredInput;
private Intent mServiceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -49,6 +55,7 @@ public class NfcOperationActivity extends BaseNfcActivity {
Bundle data = intent.getExtras();
mRequiredInput = data.getParcelable(EXTRA_REQUIRED_INPUT);
mServiceIntent = data.getParcelable(EXTRA_SERVICE_INTENT);
// obtain passphrase for this subkey
obtainYubikeyPin(RequiredInputParcel.createRequiredPassphrase(mRequiredInput));
@@ -84,10 +91,15 @@ public class NfcOperationActivity extends BaseNfcActivity {
break;
}
// give data through for new service call
Intent result = new Intent();
result.putExtra(NfcOperationActivity.RESULT_DATA, resultData);
setResult(RESULT_OK, result);
if (mServiceIntent != null) {
mServiceIntent.putExtra(OpenPgpApi.EXTRA_CRYPTO_INPUT, resultData);
setResult(RESULT_OK, mServiceIntent);
} else {
Intent result = new Intent();
result.putExtra(NfcOperationActivity.RESULT_DATA, resultData);
setResult(RESULT_OK, result);
}
finish();
}

View File

@@ -41,7 +41,7 @@ import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import junit.framework.Assert;
import org.openintents.openpgp.util.OpenPgpApi;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround;
@@ -67,14 +67,13 @@ import org.sufficientlysecure.keychain.util.Preferences;
* This activity encapsulates a DialogFragment to emulate a dialog.
*/
public class PassphraseDialogActivity extends FragmentActivity {
public static final String MESSAGE_DATA_PASSPHRASE = "passphrase";
public static final String RESULT_DATA = "result_data";
public static final String RESULT_CRYPTO_INPUT = "result_data";
public static final String EXTRA_REQUIRED_INPUT = "required_input";
public static final String EXTRA_SUBKEY_ID = "secret_key_id";
// special extra for OpenPgpService
public static final String EXTRA_DATA = "data";
public static final String EXTRA_SERVICE_INTENT = "data";
private static final int REQUEST_CODE_ENTER_PATTERN = 2;
@@ -104,7 +103,7 @@ public class PassphraseDialogActivity extends FragmentActivity {
keyId = requiredInput.getSubKeyId();
}
Intent serviceIntent = getIntent().getParcelableExtra(EXTRA_DATA);
Intent serviceIntent = getIntent().getParcelableExtra(EXTRA_SERVICE_INTENT);
show(this, keyId, serviceIntent);
}
@@ -155,7 +154,7 @@ public class PassphraseDialogActivity extends FragmentActivity {
PassphraseDialogFragment frag = new PassphraseDialogFragment();
Bundle args = new Bundle();
args.putLong(EXTRA_SUBKEY_ID, keyId);
args.putParcelable(EXTRA_DATA, serviceIntent);
args.putParcelable(EXTRA_SERVICE_INTENT, serviceIntent);
frag.setArguments(args);
@@ -188,7 +187,7 @@ public class PassphraseDialogActivity extends FragmentActivity {
R.style.Theme_AppCompat_Light_Dialog);
mSubKeyId = getArguments().getLong(EXTRA_SUBKEY_ID);
mServiceIntent = getArguments().getParcelable(EXTRA_DATA);
mServiceIntent = getArguments().getParcelable(EXTRA_SERVICE_INTENT);
CustomAlertDialogBuilder alert = new CustomAlertDialogBuilder(theme);
@@ -418,15 +417,13 @@ public class PassphraseDialogActivity extends FragmentActivity {
}
if (mServiceIntent != null) {
// TODO: Not routing passphrase through OpenPGP API currently
// due to security concerns...
// BUT this means you need to _cache_ passphrases!
// TODO really pass this through the PendingIntent?
mServiceIntent.putExtra(OpenPgpApi.EXTRA_CRYPTO_INPUT, new CryptoInputParcel(null, passphrase));
getActivity().setResult(RESULT_OK, mServiceIntent);
} else {
// also return passphrase back to activity
Intent returnIntent = new Intent();
returnIntent.putExtra(MESSAGE_DATA_PASSPHRASE, passphrase);
returnIntent.putExtra(RESULT_DATA, new CryptoInputParcel(null, passphrase));
returnIntent.putExtra(RESULT_CRYPTO_INPUT, new CryptoInputParcel(null, passphrase));
getActivity().setResult(RESULT_OK, returnIntent);
}

View File

@@ -130,7 +130,7 @@ public abstract class BaseNfcActivity extends BaseActivity {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_PASSPHRASE:
CryptoInputParcel input = data.getParcelableExtra(PassphraseDialogActivity.RESULT_DATA);
CryptoInputParcel input = data.getParcelableExtra(PassphraseDialogActivity.RESULT_CRYPTO_INPUT);
mPin = input.getPassphrase();
break;