Files
open-keychain/org_apg/src/org/apg/ui/SignKeyActivity.java

328 lines
11 KiB
Java
Raw Normal View History

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.
*/
package org.apg.ui;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.util.Iterator;
import org.apg.Apg;
import org.apg.Constants;
import org.apg.HkpKeyServer;
import org.apg.Id;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPPrivateKey;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSignature;
import org.spongycastle.openpgp.PGPSignatureGenerator;
import org.spongycastle.openpgp.PGPSignatureSubpacketGenerator;
import org.spongycastle.openpgp.PGPSignatureSubpacketVector;
import org.spongycastle.openpgp.PGPUtil;
import org.apg.R;
2012-03-12 14:28:35 +01:00
import com.actionbarsherlock.view.MenuItem;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.Toast;
/**
* gpg --sign-key
*
* signs the specified public key with the specified secret master key
*/
public class SignKeyActivity extends BaseActivity {
private static final String TAG = "SignKeyActivity";
private long pubKeyId = 0;
private long masterKeyId = 0;
2012-03-12 14:28:35 +01:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
startActivity(new Intent(this, PublicKeyListActivity.class));
return true;
default:
break;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// check we havent already signed it
setContentView(R.layout.sign_key_layout);
final Spinner keyServer = (Spinner) findViewById(R.id.keyServer);
2012-03-12 14:28:35 +01:00
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, mPreferences.getKeyServers());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
keyServer.setAdapter(adapter);
final CheckBox sendKey = (CheckBox) findViewById(R.id.sendKey);
if (!sendKey.isChecked()) {
keyServer.setEnabled(false);
} else {
keyServer.setEnabled(true);
}
sendKey.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
keyServer.setEnabled(false);
} else {
keyServer.setEnabled(true);
}
}
});
Button sign = (Button) findViewById(R.id.sign);
sign.setEnabled(false); // disabled until the user selects a key to sign with
sign.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (pubKeyId != 0) {
initiateSigning();
}
}
});
2012-03-12 14:28:35 +01:00
pubKeyId = getIntent().getLongExtra(Apg.EXTRA_KEY_ID, 0);
if (pubKeyId == 0) {
finish(); // nothing to do if we dont know what key to sign
} else {
2012-03-12 14:28:35 +01:00
// kick off the SecretKey selection activity so the user chooses which key to sign with
// first
Intent intent = new Intent(this, SelectSecretKeyListActivity.class);
startActivityForResult(intent, Id.request.secret_keys);
}
}
/**
* handles the UI bits of the signing process on the UI thread
*/
private void initiateSigning() {
PGPPublicKeyRing pubring = Apg.getPublicKeyRing(pubKeyId);
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")
Iterator<PGPSignature> itr = pubring.getPublicKey(pubKeyId).getSignatures();
while (itr.hasNext()) {
PGPSignature sig = itr.next();
if (sig.getKeyID() == masterKeyId) {
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 = Apg.getCachedPassPhrase(masterKeyId);
if (passphrase == null) {
showDialog(Id.dialog.pass_phrase);
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 {
final Bundle status = new Bundle();
Message msg = new Message();
status.putString(Apg.EXTRA_ERROR, "Key has already been signed");
status.putInt(Constants.extras.STATUS, Id.message.done);
2012-03-12 14:28:35 +01:00
msg.setData(status);
sendMessage(msg);
2012-03-12 14:28:35 +01:00
setResult(Id.return_value.error);
finish();
}
}
}
2012-03-12 14:28:35 +01:00
@Override
public long getSecretKeyId() {
return masterKeyId;
}
2012-03-12 14:28:35 +01:00
@Override
public void passPhraseCallback(long keyId, String passPhrase) {
super.passPhraseCallback(keyId, passPhrase);
startSigning();
}
2012-03-12 14:28:35 +01:00
/**
* kicks off the actual signing process on a background thread
*/
private void startSigning() {
showDialog(Id.dialog.signing);
startThread();
}
2012-03-12 14:28:35 +01:00
@Override
public void run() {
final Bundle status = new Bundle();
Message msg = new Message();
try {
String passphrase = Apg.getCachedPassPhrase(masterKeyId);
if (passphrase == null || passphrase.length() <= 0) {
status.putString(Apg.EXTRA_ERROR, "Unable to obtain passphrase");
} else {
PGPPublicKeyRing pubring = Apg.getPublicKeyRing(pubKeyId);
2012-03-12 14:28:35 +01:00
/*
* sign the incoming key
*/
PGPSecretKey secretKey = Apg.getSecretKey(masterKeyId);
2012-03-12 14:28:35 +01:00
PGPPrivateKey signingKey = secretKey.extractPrivateKey(passphrase.toCharArray(),
BouncyCastleProvider.PROVIDER_NAME);
PGPSignatureGenerator sGen = new PGPSignatureGenerator(secretKey.getPublicKey()
.getAlgorithm(), PGPUtil.SHA256, BouncyCastleProvider.PROVIDER_NAME);
sGen.initSign(PGPSignature.DIRECT_KEY, signingKey);
2012-03-12 14:28:35 +01:00
PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
2012-03-12 14:28:35 +01:00
PGPSignatureSubpacketVector packetVector = spGen.generate();
sGen.setHashedSubpackets(packetVector);
2012-03-12 14:28:35 +01:00
PGPPublicKey signedKey = PGPPublicKey.addCertification(
pubring.getPublicKey(pubKeyId), sGen.generate());
pubring = PGPPublicKeyRing.insertPublicKey(pubring, signedKey);
2012-03-12 14:28:35 +01:00
// check if we need to send the key to the server or not
CheckBox sendKey = (CheckBox) findViewById(R.id.sendKey);
if (sendKey.isChecked()) {
Spinner keyServer = (Spinner) findViewById(R.id.keyServer);
HkpKeyServer server = new HkpKeyServer((String) keyServer.getSelectedItem());
2012-03-12 14:28:35 +01:00
/*
* upload the newly signed key to the key server
*/
2012-03-12 14:28:35 +01:00
Apg.uploadKeyRingToServer(server, pubring);
}
2012-03-12 14:28:35 +01:00
// store the signed key in our local cache
int retval = Apg.storeKeyRingInCache(pubring);
if (retval != Id.return_value.ok && retval != Id.return_value.updated) {
status.putString(Apg.EXTRA_ERROR, "Failed to store signed key in local cache");
}
}
} catch (PGPException e) {
Log.e(TAG, "Failed to sign key", e);
status.putString(Apg.EXTRA_ERROR, "Failed to sign key");
status.putInt(Constants.extras.STATUS, Id.message.done);
return;
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "Failed to sign key", e);
status.putString(Apg.EXTRA_ERROR, "Failed to sign key");
status.putInt(Constants.extras.STATUS, Id.message.done);
return;
} catch (NoSuchProviderException e) {
Log.e(TAG, "Failed to sign key", e);
status.putString(Apg.EXTRA_ERROR, "Failed to sign key");
status.putInt(Constants.extras.STATUS, Id.message.done);
return;
} catch (SignatureException e) {
Log.e(TAG, "Failed to sign key", e);
status.putString(Apg.EXTRA_ERROR, "Failed to sign key");
status.putInt(Constants.extras.STATUS, Id.message.done);
return;
}
2012-03-12 14:28:35 +01:00
status.putInt(Constants.extras.STATUS, Id.message.done);
2012-03-12 14:28:35 +01:00
msg.setData(status);
sendMessage(msg);
2012-03-12 14:28:35 +01:00
if (status.containsKey(Apg.EXTRA_ERROR)) {
setResult(Id.return_value.error);
} else {
setResult(Id.return_value.ok);
}
2012-03-12 14:28:35 +01:00
finish();
}
2012-03-12 14:28:35 +01:00
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
2012-03-12 14:28:35 +01:00
case Id.request.secret_keys: {
if (resultCode == RESULT_OK) {
masterKeyId = data.getLongExtra(Apg.EXTRA_KEY_ID, 0);
// re-enable the sign button so the user can initiate the sign process
Button sign = (Button) findViewById(R.id.sign);
sign.setEnabled(true);
}
2012-03-12 14:28:35 +01:00
break;
}
default: {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
2012-03-12 14:28:35 +01:00
@Override
public void doneCallback(Message msg) {
super.doneCallback(msg);
2012-03-12 14:28:35 +01:00
removeDialog(Id.dialog.signing);
Bundle data = msg.getData();
String error = data.getString(Apg.EXTRA_ERROR);
if (error != null) {
2012-03-12 14:28:35 +01:00
Toast.makeText(this, getString(R.string.errorMessage, error), Toast.LENGTH_SHORT)
.show();
return;
}
Toast.makeText(this, R.string.keySignSuccess, Toast.LENGTH_SHORT).show();
finish();
}
}