intent to create default rsa key pair

This commit is contained in:
Dominik 2012-04-25 17:54:02 +02:00
parent 340e0289ef
commit 73a2957d95
4 changed files with 127 additions and 52 deletions

View File

@ -399,29 +399,26 @@ public class Apg {
PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder() PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
.setProvider("SC").build(passPhrase.toCharArray()); .setProvider("SC").build(passPhrase.toCharArray());
PGPSecretKeyRing secKeyRing = null; PGPKeyRingGenerator ringGen = null;
if (masterSecretKey == null) { if (masterSecretKey == null) {
// build keyRing with only this one master key in it! // build keyRing with only this one master key in it!
PGPKeyRingGenerator ringGen = new PGPKeyRingGenerator( ringGen = new PGPKeyRingGenerator(PGPSignature.DEFAULT_CERTIFICATION, keyPair, "",
PGPSignature.DEFAULT_CERTIFICATION, keyPair, "", sha1Calc, null, null, sha1Calc, null, null, certificationSignerBuilder, keyEncryptor);
certificationSignerBuilder, keyEncryptor);
secKeyRing = ringGen.generateSecretKeyRing();
} else { } else {
PGPPublicKey masterPublicKey = masterSecretKey.getPublicKey(); PGPPublicKey masterPublicKey = masterSecretKey.getPublicKey();
PGPPrivateKey masterPrivateKey = masterSecretKey.extractPrivateKey(keyDecryptor); PGPPrivateKey masterPrivateKey = masterSecretKey.extractPrivateKey(keyDecryptor);
PGPKeyPair masterKeyPair = new PGPKeyPair(masterPublicKey, masterPrivateKey); PGPKeyPair masterKeyPair = new PGPKeyPair(masterPublicKey, masterPrivateKey);
// build keyRing with master key and new key as subkey (certified by masterkey) // build keyRing with master key and new key as subkey (certified by masterkey)
PGPKeyRingGenerator ringGen = new PGPKeyRingGenerator( ringGen = new PGPKeyRingGenerator(PGPSignature.DEFAULT_CERTIFICATION, masterKeyPair,
PGPSignature.DEFAULT_CERTIFICATION, masterKeyPair, "", sha1Calc, null, null, "", sha1Calc, null, null, certificationSignerBuilder, keyEncryptor);
certificationSignerBuilder, keyEncryptor);
ringGen.addSubKey(keyPair); ringGen.addSubKey(keyPair);
secKeyRing = ringGen.generateSecretKeyRing();
} }
PGPSecretKeyRing secKeyRing = ringGen.generateSecretKeyRing();
return secKeyRing; return secKeyRing;
} }

View File

@ -41,6 +41,7 @@ public class ApgHandler extends Handler {
// generate key results // generate key results
public static final String NEW_KEY = "new_key"; public static final String NEW_KEY = "new_key";
public static final String NEW_KEY2 = "new_key2";
Activity mActivity; Activity mActivity;

View File

@ -22,6 +22,7 @@ import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing; import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.thialfihar.android.apg.Apg; import org.thialfihar.android.apg.Apg;
import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.ProgressDialogUpdater; import org.thialfihar.android.apg.ProgressDialogUpdater;
import org.thialfihar.android.apg.util.Utils; import org.thialfihar.android.apg.util.Utils;
@ -64,6 +65,7 @@ public class ApgService extends IntentService implements ProgressDialogUpdater {
// possible ints for EXTRA_ACTION // possible ints for EXTRA_ACTION
public static final int ACTION_SAVE_KEYRING = 1; public static final int ACTION_SAVE_KEYRING = 1;
public static final int ACTION_GENERATE_KEY = 2; public static final int ACTION_GENERATE_KEY = 2;
public static final int ACTION_GENERATE_DEFAULT_RSA_KEYS = 3;
Messenger mMessenger; Messenger mMessenger;
@ -162,6 +164,31 @@ public class ApgService extends IntentService implements ProgressDialogUpdater {
break; break;
case ACTION_GENERATE_DEFAULT_RSA_KEYS:
// generate one RSA 2048 key for signing and one subkey for encrypting!
try {
String passphrase = data.getString(PASSPHRASE);
// Operation
PGPSecretKeyRing masterKeyRing = Apg.createKey(this, Id.choice.algorithm.rsa, 2048,
passphrase, null);
PGPSecretKeyRing subKeyRing = Apg.createKey(this, Id.choice.algorithm.rsa, 2048,
passphrase, masterKeyRing.getSecretKey());
// Output
Bundle resultData = new Bundle();
resultData.putByteArray(ApgHandler.NEW_KEY,
Utils.PGPSecretKeyRingToBytes(masterKeyRing));
resultData.putByteArray(ApgHandler.NEW_KEY2,
Utils.PGPSecretKeyRingToBytes(subKeyRing));
sendMessageToHandler(ApgHandler.MESSAGE_OKAY, null, resultData);
} catch (Exception e) {
Log.e(Constants.TAG, "Creating initial key failed: +" + e);
}
break;
default: default:
break; break;
} }

View File

@ -58,6 +58,7 @@ import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.CompoundButton.OnCheckedChangeListener;
import java.util.Iterator;
import java.util.Vector; import java.util.Vector;
public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseActivity { public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseActivity {
@ -66,8 +67,8 @@ public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseA
private PGPSecretKeyRing mKeyRing = null; private PGPSecretKeyRing mKeyRing = null;
private SectionView mUserIds; private SectionView mUserIdsView;
private SectionView mKeys; private SectionView mKeysView;
private String mCurrentPassPhrase = null; private String mCurrentPassPhrase = null;
private String mNewPassPhrase = null; private String mNewPassPhrase = null;
@ -77,6 +78,11 @@ public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseA
private CheckBox mNoPassphrase; private CheckBox mNoPassphrase;
private ProgressDialogFragment mSavingDialog; private ProgressDialogFragment mSavingDialog;
private ProgressDialogFragment mGeneratingDialog;
Vector<String> mUserIds;
Vector<PGPSecretKey> mKeys;
Vector<Integer> mKeysUsages;
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
@ -131,9 +137,9 @@ public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseA
mChangePassPhrase = (Button) findViewById(R.id.edit_key_btn_change_pass_phrase); mChangePassPhrase = (Button) findViewById(R.id.edit_key_btn_change_pass_phrase);
mNoPassphrase = (CheckBox) findViewById(R.id.edit_key_no_passphrase); mNoPassphrase = (CheckBox) findViewById(R.id.edit_key_no_passphrase);
Vector<String> userIds = new Vector<String>(); mUserIds = new Vector<String>();
Vector<PGPSecretKey> keys = new Vector<PGPSecretKey>(); mKeys = new Vector<PGPSecretKey>();
Vector<Integer> keysUsages = new Vector<Integer>(); mKeysUsages = new Vector<Integer>();
// Catch Intents opened from other apps // Catch Intents opened from other apps
mIntent = getIntent(); mIntent = getIntent();
@ -150,7 +156,7 @@ public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseA
// if userId is given, prefill the fields // if userId is given, prefill the fields
if (extras.containsKey(Apg.EXTRA_USER_IDS)) { if (extras.containsKey(Apg.EXTRA_USER_IDS)) {
Log.d(Constants.TAG, "UserIds are given!"); Log.d(Constants.TAG, "UserIds are given!");
userIds.add(extras.getString(Apg.EXTRA_USER_IDS)); mUserIds.add(extras.getString(Apg.EXTRA_USER_IDS));
} }
// if no passphrase is given // if no passphrase is given
@ -169,25 +175,62 @@ public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseA
.getBoolean(Apg.EXTRA_GENERATE_DEFAULT_KEYS); .getBoolean(Apg.EXTRA_GENERATE_DEFAULT_KEYS);
if (generateDefaultKeys) { if (generateDefaultKeys) {
// // generate a RSA 2048 key for encryption and signing! // Send all information needed to service generate keys in other thread
// try { Intent intent = new Intent(this, ApgService.class);
// PGPSecretKey masterKey = Apg.createKey(this, Id.choice.algorithm.rsa, intent.putExtra(ApgService.EXTRA_ACTION,
// 2048, mCurrentPassPhrase, null); ApgService.ACTION_GENERATE_DEFAULT_RSA_KEYS);
//
// // add new masterKey to keys array, which is then added to view
// keys.add(masterKey);
// keysUsages.add(Id.choice.usage.sign_only);
//
// PGPSecretKey subKey = Apg.createKey(this, Id.choice.algorithm.rsa,
// 2048, mCurrentPassPhrase, masterKey);
//
// keys.add(subKey);
// keysUsages.add(Id.choice.usage.encrypt_only);
// } catch (Exception e) {
// Log.e(Constants.TAG, "Creating initial key failed: +" + e);
// }
}
// fill values for this action
Bundle data = new Bundle();
data.putString(ApgService.PASSPHRASE, mCurrentPassPhrase);
intent.putExtra(ApgService.EXTRA_DATA, data);
// show progress dialog
mGeneratingDialog = ProgressDialogFragment.newInstance(
R.string.progress_generating, ProgressDialog.STYLE_SPINNER);
// Message is received after generating is done in ApgService
ApgHandler saveHandler = new ApgHandler(this, mGeneratingDialog) {
public void handleMessage(Message message) {
// handle messages by standard ApgHandler first
super.handleMessage(message);
if (message.arg1 == ApgHandler.MESSAGE_OKAY) {
// get new key from data bundle returned from service
Bundle data = message.getData();
PGPSecretKeyRing masterKeyRing = Utils
.BytesToPGPSecretKeyRing(data
.getByteArray(ApgHandler.NEW_KEY));
PGPSecretKeyRing subKeyRing = Utils
.BytesToPGPSecretKeyRing(data
.getByteArray(ApgHandler.NEW_KEY2));
// add master key
Iterator<PGPSecretKey> masterIt = masterKeyRing.getSecretKeys();
mKeys.add(masterIt.next());
mKeysUsages.add(Id.choice.usage.sign_only);
// add sub key
Iterator<PGPSecretKey> subIt = subKeyRing.getSecretKeys();
subIt.next(); // masterkey
mKeys.add(subIt.next());
mKeysUsages.add(Id.choice.usage.encrypt_only);
buildLayout();
}
};
};
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
intent.putExtra(ApgService.EXTRA_MESSENGER, messenger);
mGeneratingDialog.show(getSupportFragmentManager(), "dialog");
// start service with intent
startService(intent);
}
} }
} }
} else if (Apg.Intent.EDIT_KEY.equals(mIntent.getAction())) { } else if (Apg.Intent.EDIT_KEY.equals(mIntent.getAction())) {
@ -217,14 +260,14 @@ public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseA
masterKey = Apg.getMasterKey(mKeyRing); masterKey = Apg.getMasterKey(mKeyRing);
for (PGPSecretKey key : new IterableIterator<PGPSecretKey>( for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(
mKeyRing.getSecretKeys())) { mKeyRing.getSecretKeys())) {
keys.add(key); mKeys.add(key);
keysUsages.add(-1); // get usage when view is created mKeysUsages.add(-1); // get usage when view is created
} }
} }
if (masterKey != null) { if (masterKey != null) {
for (String userId : new IterableIterator<String>( for (String userId : new IterableIterator<String>(
masterKey.getUserIDs())) { masterKey.getUserIDs())) {
userIds.add(userId); mUserIds.add(userId);
} }
} }
} }
@ -255,27 +298,35 @@ public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseA
} }
}); });
buildLayout();
}
/**
* Build layout based on mUserId, mKeys and mKeysUsages Vectors. It creates Views for every user
* id and key.
*/
private void buildLayout() {
// Build layout based on given userIds and keys // Build layout based on given userIds and keys
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = (LinearLayout) findViewById(R.id.edit_key_container); LinearLayout container = (LinearLayout) findViewById(R.id.edit_key_container);
mUserIds = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false); mUserIdsView = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false);
mUserIds.setType(Id.type.user_id); mUserIdsView.setType(Id.type.user_id);
mUserIds.setUserIds(userIds); mUserIdsView.setUserIds(mUserIds);
container.addView(mUserIds); container.addView(mUserIdsView);
mKeys = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false); mKeysView = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false);
mKeys.setType(Id.type.key); mKeysView.setType(Id.type.key);
mKeys.setKeys(keys, keysUsages); mKeysView.setKeys(mKeys, mKeysUsages);
container.addView(mKeys); container.addView(mKeysView);
updatePassPhraseButtonText(); updatePassPhraseButtonText();
} }
private long getMasterKeyId() { private long getMasterKeyId() {
if (mKeys.getEditors().getChildCount() == 0) { if (mKeysView.getEditors().getChildCount() == 0) {
return 0; return 0;
} }
return ((KeyEditor) mKeys.getEditors().getChildAt(0)).getValue().getKeyID(); return ((KeyEditor) mKeysView.getEditors().getChildAt(0)).getValue().getKeyID();
} }
public boolean isPassphraseSet() { public boolean isPassphraseSet() {
@ -362,13 +413,12 @@ public class EditKeyActivity extends SherlockFragmentActivity { // extends BaseA
data.putString(ApgService.CURRENT_PASSPHRASE, mCurrentPassPhrase); data.putString(ApgService.CURRENT_PASSPHRASE, mCurrentPassPhrase);
data.putString(ApgService.NEW_PASSPHRASE, mNewPassPhrase); data.putString(ApgService.NEW_PASSPHRASE, mNewPassPhrase);
data.putSerializable(ApgService.USER_IDS, getUserIds(mUserIds)); data.putSerializable(ApgService.USER_IDS, getUserIds(mUserIdsView));
Vector<PGPSecretKey> keys = getKeys(mKeys); Vector<PGPSecretKey> keys = getKeys(mKeysView);
byte[] keysBytes = Utils.PGPSecretKeyListToBytes(keys); data.putByteArray(ApgService.KEYS, Utils.PGPSecretKeyListToBytes(keys));
data.putByteArray(ApgService.KEYS, keysBytes);
data.putSerializable(ApgService.KEYS_USAGES, getKeysUsages(mKeys)); data.putSerializable(ApgService.KEYS_USAGES, getKeysUsages(mKeysView));
data.putLong(ApgService.MASTER_KEY_ID, getMasterKeyId()); data.putLong(ApgService.MASTER_KEY_ID, getMasterKeyId());