Files
open-keychain/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/ui/SignKeyActivity.java

309 lines
11 KiB
Java
Raw Normal View History

2012-03-12 14:28:35 +01:00
/*
2014-01-18 19:54:27 +01:00
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2011 Senecaso
*
2012-03-12 14:28:35 +01:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2013-01-16 14:31:16 +01:00
package org.sufficientlysecure.keychain.ui;
import java.util.Iterator;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSignature;
2013-01-16 14:31:16 +01:00
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
2013-01-16 14:31:16 +01:00
import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
2013-01-16 14:31:16 +01:00
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler;
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment;
import org.sufficientlysecure.keychain.util.Log;
2012-03-12 14:28:35 +01:00
2012-09-10 19:57:39 +02:00
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
2012-09-10 19:57:39 +02:00
import android.os.Handler;
import android.os.Message;
2012-09-10 19:57:39 +02:00
import android.os.Messenger;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.beardedhen.androidbootstrap.BootstrapButton;
/**
* gpg --sign-key
*
* signs the specified public key with the specified secret master key
*/
2014-01-18 19:54:27 +01:00
public class SignKeyActivity extends SherlockFragmentActivity implements
SelectSecretKeyLayoutFragment.SelectSecretKeyCallback {
2014-01-18 19:54:27 +01:00
public static final String EXTRA_KEY_ID = "key_id";
2012-09-10 23:34:14 +02:00
private long mPubKeyId = 0;
private long mMasterKeyId = 0;
2012-03-12 14:28:35 +01:00
private BootstrapButton mSignButton;
private CheckBox mUploadKeyCheckbox;
private Spinner mSelectKeyserverSpinner;
2014-01-18 19:54:27 +01:00
private SelectSecretKeyLayoutFragment mSelectKeyFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_key_activity);
2012-09-10 23:34:14 +02:00
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
2014-01-18 19:54:27 +01:00
mSelectKeyFragment = (SelectSecretKeyLayoutFragment) getSupportFragmentManager()
.findFragmentById(R.id.sign_key_select_key_fragment);
mSelectKeyFragment.setCallback(this);
mSelectKeyserverSpinner = (Spinner) findViewById(R.id.sign_key_keyserver);
2012-03-12 14:28:35 +01:00
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
2012-09-10 19:57:39 +02:00
android.R.layout.simple_spinner_item, Preferences.getPreferences(this)
.getKeyServers());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSelectKeyserverSpinner.setAdapter(adapter);
mUploadKeyCheckbox = (CheckBox) findViewById(R.id.sign_key_upload_checkbox);
if (!mUploadKeyCheckbox.isChecked()) {
mSelectKeyserverSpinner.setEnabled(false);
} else {
mSelectKeyserverSpinner.setEnabled(true);
}
mUploadKeyCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
mSelectKeyserverSpinner.setEnabled(false);
} else {
mSelectKeyserverSpinner.setEnabled(true);
}
}
});
mSignButton = (BootstrapButton) findViewById(R.id.sign_key_sign_button);
mSignButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
2012-09-10 23:34:14 +02:00
if (mPubKeyId != 0) {
2014-01-18 19:54:27 +01:00
if (mMasterKeyId == 0) {
mSelectKeyFragment.setError(getString(R.string.select_key_to_sign));
} else {
initiateSigning();
}
}
}
});
2012-03-12 14:28:35 +01:00
2012-09-10 23:34:14 +02:00
mPubKeyId = getIntent().getLongExtra(EXTRA_KEY_ID, 0);
if (mPubKeyId == 0) {
2014-01-18 19:54:27 +01:00
Log.e(Constants.TAG, "No pub key id given!");
finish();
}
}
2012-09-10 19:57:39 +02:00
private void showPassphraseDialog(final long secretKeyId) {
// Message is received after passphrase is cached
Handler returnHandler = new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) {
startSigning();
}
}
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(returnHandler);
try {
PassphraseDialogFragment passphraseDialog = PassphraseDialogFragment.newInstance(this,
2012-09-10 19:57:39 +02:00
messenger, secretKeyId);
passphraseDialog.show(getSupportFragmentManager(), "passphraseDialog");
} catch (PgpGeneralException e) {
2012-09-10 19:57:39 +02:00
Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!");
// send message to handler to start encryption directly
returnHandler.sendEmptyMessage(PassphraseDialogFragment.MESSAGE_OKAY);
}
}
/**
* handles the UI bits of the signing process on the UI thread
*/
private void initiateSigning() {
2012-10-25 14:52:13 +02:00
PGPPublicKeyRing pubring = ProviderHelper.getPGPPublicKeyRingByMasterKeyId(this, mPubKeyId);
if (pubring != null) {
// if we have already signed this key, dont bother doing it again
boolean alreadySigned = false;
2012-03-12 14:28:35 +01:00
@SuppressWarnings("unchecked")
2012-09-10 23:34:14 +02:00
Iterator<PGPSignature> itr = pubring.getPublicKey(mPubKeyId).getSignatures();
while (itr.hasNext()) {
PGPSignature sig = itr.next();
2012-09-10 23:34:14 +02:00
if (sig.getKeyID() == mMasterKeyId) {
alreadySigned = true;
break;
}
}
2012-03-12 14:28:35 +01:00
if (!alreadySigned) {
/*
* get the user's passphrase for this key (if required)
*/
String passphrase = PassphraseCacheService.getCachedPassphrase(this, mMasterKeyId);
if (passphrase == null) {
2012-09-10 23:34:14 +02:00
showPassphraseDialog(mMasterKeyId);
2012-03-12 14:28:35 +01:00
return; // bail out; need to wait until the user has entered the passphrase
// before trying again
} else {
startSigning();
}
} else {
2014-01-18 19:54:27 +01:00
Toast.makeText(this, R.string.key_has_already_been_signed, Toast.LENGTH_SHORT)
.show();
2012-03-12 14:28:35 +01:00
2012-09-13 18:37:31 +02:00
setResult(RESULT_CANCELED);
finish();
}
}
}
2012-03-12 14:28:35 +01:00
/**
* kicks off the actual signing process on a background thread
*/
private void startSigning() {
2012-09-10 19:57:39 +02:00
// Send all information needed to service to sign key in other thread
2013-01-16 14:31:16 +01:00
Intent intent = new Intent(this, KeychainIntentService.class);
2012-03-12 14:28:35 +01:00
intent.setAction(KeychainIntentService.ACTION_SIGN_KEYRING);
2012-09-10 19:57:39 +02:00
// fill values for this action
Bundle data = new Bundle();
2012-03-12 14:28:35 +01:00
2013-01-16 14:31:16 +01:00
data.putLong(KeychainIntentService.SIGN_KEY_MASTER_KEY_ID, mMasterKeyId);
data.putLong(KeychainIntentService.SIGN_KEY_PUB_KEY_ID, mPubKeyId);
2012-03-12 14:28:35 +01:00
2013-01-16 14:31:16 +01:00
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
2012-03-12 14:28:35 +01:00
2012-09-10 19:57:39 +02:00
// Message is received after signing is done in ApgService
KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this,
R.string.progress_signing, ProgressDialog.STYLE_SPINNER) {
2012-09-10 19:57:39 +02:00
public void handleMessage(Message message) {
// handle messages by standard ApgHandler first
super.handleMessage(message);
2012-03-12 14:28:35 +01:00
2013-01-16 14:31:16 +01:00
if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) {
2012-03-12 14:28:35 +01:00
Toast.makeText(SignKeyActivity.this, R.string.key_sign_success,
2012-09-10 19:57:39 +02:00
Toast.LENGTH_SHORT).show();
2012-03-12 14:28:35 +01:00
2012-09-10 19:57:39 +02:00
// check if we need to send the key to the server or not
2014-01-18 19:54:27 +01:00
if (mUploadKeyCheckbox.isChecked()) {
2012-09-10 19:57:39 +02:00
/*
* upload the newly signed key to the key server
*/
uploadKey();
} else {
finish();
}
}
2012-09-10 19:57:39 +02:00
};
};
2012-03-12 14:28:35 +01:00
2012-09-10 19:57:39 +02:00
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
2013-01-16 14:31:16 +01:00
intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);
2012-03-12 14:28:35 +01:00
2012-09-10 19:57:39 +02:00
// show progress dialog
saveHandler.showProgressDialog(this);
2012-03-12 14:28:35 +01:00
2012-09-10 19:57:39 +02:00
// start service with intent
startService(intent);
}
2012-03-12 14:28:35 +01:00
2012-09-10 19:57:39 +02:00
private void uploadKey() {
// Send all information needed to service to upload key in other thread
2013-01-16 14:31:16 +01:00
Intent intent = new Intent(this, KeychainIntentService.class);
2012-09-10 19:57:39 +02:00
intent.setAction(KeychainIntentService.ACTION_UPLOAD_KEYRING);
2012-09-10 19:57:39 +02:00
// fill values for this action
Bundle data = new Bundle();
2013-01-16 14:31:16 +01:00
data.putLong(KeychainIntentService.UPLOAD_KEY_KEYRING_ROW_ID, mPubKeyId);
2012-09-10 19:57:39 +02:00
Spinner keyServer = (Spinner) findViewById(R.id.sign_key_keyserver);
2012-09-10 19:57:39 +02:00
String server = (String) keyServer.getSelectedItem();
2013-01-16 14:31:16 +01:00
data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, server);
2012-09-10 19:57:39 +02:00
2013-01-16 14:31:16 +01:00
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
2012-09-10 19:57:39 +02:00
// Message is received after uploading is done in ApgService
KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this,
R.string.progress_exporting, ProgressDialog.STYLE_HORIZONTAL) {
2012-09-10 19:57:39 +02:00
public void handleMessage(Message message) {
// handle messages by standard ApgHandler first
super.handleMessage(message);
2013-01-16 14:31:16 +01:00
if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) {
2012-09-10 19:57:39 +02:00
Toast.makeText(SignKeyActivity.this, R.string.key_send_success,
2012-09-10 19:57:39 +02:00
Toast.LENGTH_SHORT).show();
2012-03-12 14:28:35 +01:00
2012-09-10 19:57:39 +02:00
finish();
}
};
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
2013-01-16 14:31:16 +01:00
intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);
2012-09-10 19:57:39 +02:00
// show progress dialog
saveHandler.showProgressDialog(this);
// start service with intent
startService(intent);
}
2012-03-12 14:28:35 +01:00
2014-01-18 19:54:27 +01:00
/**
* callback from select key fragment
*/
@Override
2014-01-18 19:54:27 +01:00
public void onKeySelected(long secretKeyId) {
mMasterKeyId = secretKeyId;
}
}