From 076a7ec4a16a6db5df0bbe5490d44cbd514f10c1 Mon Sep 17 00:00:00 2001 From: Ashley Hughes Date: Thu, 13 Feb 2014 22:05:36 +0000 Subject: [PATCH] save work, this code doesn't work... --- .../keychain/pgp/PgpKeyOperation.java | 34 +++++++++---------- .../service/KeychainIntentService.java | 11 ++++-- .../keychain/ui/EditKeyActivity.java | 15 ++++++++ .../keychain/ui/widget/SectionView.java | 19 +++++++++++ 4 files changed, 58 insertions(+), 21 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 7ab5df613..505d3ba55 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -23,7 +23,6 @@ import java.security.InvalidAlgorithmParameterException; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; -import java.security.PublicKey; import java.security.SecureRandom; import java.security.SignatureException; import java.util.ArrayList; @@ -35,7 +34,6 @@ import org.spongycastle.bcpg.CompressionAlgorithmTags; import org.spongycastle.bcpg.HashAlgorithmTags; import org.spongycastle.bcpg.SymmetricKeyAlgorithmTags; import org.spongycastle.bcpg.sig.KeyFlags; -import org.spongycastle.jce.provider.BouncyCastleProvider; import org.spongycastle.jce.spec.ElGamalParameterSpec; import org.spongycastle.openpgp.PGPEncryptedData; import org.spongycastle.openpgp.PGPException; @@ -57,7 +55,6 @@ import org.spongycastle.openpgp.operator.PGPContentSignerBuilder; import org.spongycastle.openpgp.operator.PGPDigestCalculator; import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; import org.spongycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; -import org.spongycastle.openpgp.operator.jcajce.JcaPGPKeyConverter; import org.spongycastle.openpgp.operator.jcajce.JcaPGPKeyPair; import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; @@ -201,9 +198,7 @@ public class PgpKeyOperation { } - public void buildSecretKey(ArrayList userIds, ArrayList keys, - ArrayList keysUsages, ArrayList keysExpiryDates, - String oldPassPhrase, String newPassPhrase) throws PgpGeneralException, + public void buildSecretKey(ArrayList userIds, ArrayList OriginalIDs, ArrayList deletedIDs, ArrayList keys, boolean[] modded_keys, String newPassPhrase, ArrayList keysExpiryDates, String oldPassPhrase, ArrayList keysUsages) throws PgpGeneralException, PGPException, SignatureException, IOException { Log.d(Constants.TAG, "userIds: " + userIds.toString()); @@ -226,10 +221,7 @@ public class PgpKeyOperation { PGPSecretKey masterKey = keys.get(0); // this removes all userIds and certifications previously attached to the masterPublicKey - PGPPublicKey tmpKey = masterKey.getPublicKey(); - PublicKey tmpPuK = new JcaPGPKeyConverter().setProvider(new BouncyCastleProvider()).getPublicKey(tmpKey); - PGPPublicKey masterPublicKey = new JcaPGPKeyConverter().getPGPPublicKey(tmpKey.getAlgorithm(), - tmpPuK, tmpKey.getCreationTime()); + PGPPublicKey masterPublicKey = masterKey.getPublicKey(); // already done by code above: // PGPPublicKey masterPublicKey = masterKey.getPublicKey(); @@ -243,6 +235,8 @@ public class PgpKeyOperation { // masterPublicKey = masterPublicKeyRmCert; // } + + PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(oldPassPhrase.toCharArray()); PGPPrivateKey masterPrivateKey = masterKey.extractPrivateKey(keyDecryptor); @@ -250,18 +244,22 @@ public class PgpKeyOperation { updateProgress(R.string.progress_certifying_master_key, 20, 100); // TODO: if we are editing a key, keep old certs, don't remake certs we don't have to. - + int user_id_index = 0; for (String userId : userIds) { - PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder( - masterPublicKey.getAlgorithm(), HashAlgorithmTags.SHA1) - .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME); - PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder); + if (OriginalIDs[user_id_index]) { + PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder( + masterPublicKey.getAlgorithm(), HashAlgorithmTags.SHA1) + .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME); + PGPSignatureGenerator sGen = new PGPSignatureGenerator(signerBuilder); - sGen.init(PGPSignature.POSITIVE_CERTIFICATION, masterPrivateKey); + sGen.init(PGPSignature.POSITIVE_CERTIFICATION, masterPrivateKey); - PGPSignature certification = sGen.generateCertification(userId, masterPublicKey); + PGPSignature certification = sGen.generateCertification(userId, masterPublicKey); - masterPublicKey = PGPPublicKey.addCertification(masterPublicKey, userId, certification); + masterPublicKey = PGPPublicKey.removeCertification(); + masterPublicKey = PGPPublicKey.addCertification(masterPublicKey, userId, certification); + } + user_id_index++; } PGPKeyPair masterKeyPair = new PGPKeyPair(masterPublicKey, masterPrivateKey); diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index a31ddc53e..4cace2658 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -54,7 +54,6 @@ import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry; import org.sufficientlysecure.keychain.util.HkpKeyServer; import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.Log; -import org.sufficientlysecure.keychain.util.PositionAwareInputStream; import org.sufficientlysecure.keychain.util.ProgressDialogUpdater; import android.app.IntentService; @@ -135,6 +134,9 @@ public class KeychainIntentService extends IntentService implements ProgressDial public static final String SAVE_KEYRING_KEYS_EXPIRY_DATES = "keys_expiry_dates"; public static final String SAVE_KEYRING_MASTER_KEY_ID = "master_key_id"; public static final String SAVE_KEYRING_CAN_SIGN = "can_sign"; + public static final String SAVE_KEYRING_ORIGINAL_IDS = "original_ids"; + public static final String SAVE_KEYRING_DELETED_IDS = "deleted_ids"; + public static final String SAVE_KEYRING_MODDED_KEYS = "modified_keys"; // generate key public static final String GENERATE_KEY_ALGORITHM = "algorithm"; @@ -532,6 +534,9 @@ public class KeychainIntentService extends IntentService implements ProgressDial .getByteArray(SAVE_KEYRING_KEYS)); ArrayList keysUsages = data.getIntegerArrayList(SAVE_KEYRING_KEYS_USAGES); ArrayList keysExpiryDates = (ArrayList) data.getSerializable(SAVE_KEYRING_KEYS_EXPIRY_DATES); + ArrayList original_ids = data.getStringArrayList(SAVE_KEYRING_ORIGINAL_IDS); + ArrayList deleted_ids = data.getStringArrayList(SAVE_KEYRING_DELETED_IDS); + boolean[] modded_keys = data.getBooleanArray(SAVE_KEYRING_MODDED_KEYS); long masterKeyId = data.getLong(SAVE_KEYRING_MASTER_KEY_ID); @@ -542,8 +547,8 @@ public class KeychainIntentService extends IntentService implements ProgressDial ProviderHelper.getPGPSecretKeyRingByKeyId(this, masterKeyId), oldPassPhrase, newPassPhrase); } else { - keyOperations.buildSecretKey(userIds, keys, keysUsages, keysExpiryDates, - oldPassPhrase, newPassPhrase); + keyOperations.buildSecretKey(userIds, original_ids, deleted_ids, keys, modded_keys, + newPassPhrase, keysExpiryDates, oldPassPhrase, keysUsages); } PassphraseCacheService.addCachedPassphrase(this, masterKeyId, newPassPhrase); diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java index 132141bc5..5adb65342 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java @@ -19,6 +19,7 @@ package org.sufficientlysecure.keychain.ui; import java.util.ArrayList; import java.util.GregorianCalendar; +import java.util.List; import java.util.Vector; import org.spongycastle.bcpg.sig.KeyFlags; @@ -553,6 +554,15 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener } } + private boolean[] toPrimitiveArray(final List booleanList) { + final boolean[] primitives = new boolean[booleanList.size()]; + int index = 0; + for (Boolean object : booleanList) { + primitives[index++] = object; + } + return primitives; + } + private void finallySaveClicked() { try { // Send all information needed to service to edit key in other thread @@ -576,6 +586,11 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener getKeysExpiryDates(mKeysView)); data.putLong(KeychainIntentService.SAVE_KEYRING_MASTER_KEY_ID, getMasterKeyId()); data.putBoolean(KeychainIntentService.SAVE_KEYRING_CAN_SIGN, masterCanSign); + data.putStringArrayList(KeychainIntentService.SAVE_KEYRING_ORIGINAL_IDS, ); + data.putBooleanArray(KeychainIntentService.SAVE_KEYRING_ORIGINAL_IDS, + toPrimitiveArray(mUserIdsView.getNeedsSavingArray())); + data.putBooleanArray(KeychainIntentService.SAVE_KEYRING_MODDED_KEYS, + toPrimitiveArray(mKeysView.getNeedsSavingArray())); intent.putExtra(KeychainIntentService.EXTRA_DATA, data); diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java index 315a8faba..5eaf54841 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java @@ -16,6 +16,8 @@ package org.sufficientlysecure.keychain.ui.widget; +import java.util.ArrayList; +import java.util.List; import java.util.Vector; import org.spongycastle.openpgp.PGPKeyFlags; @@ -162,6 +164,23 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor return ret; } + public List getNeedsSavingArray() + { + ArrayList mList = new ArrayList(); + for (int i = 0; i < mEditors.getChildCount(); ++i) { + Editor editor = (Editor) mEditors.getChildAt(i); + if (mType == Id.type.user_id) { + try { + if (((UserIdEditor)editor).getValue().equals("")) //other code ignores empty user id + continue; + } catch (UserIdEditor.InvalidEmailException e) { + } + } + mList.add(editor.needsSaving()); + } + return mList; + } + /** {@inheritDoc} */ public void onClick(View v) { if (canEdit) {