diff options
author | Vincent Breitmoser <valodim@mugenguild.com> | 2014-09-03 02:42:05 +0200 |
---|---|---|
committer | Vincent Breitmoser <valodim@mugenguild.com> | 2014-09-03 02:43:06 +0200 |
commit | 7bc424a8cb1c3d4d4c77778f27bd18fe61da0736 (patch) | |
tree | 0350e5ec8fab966159963572224d2151f0b0451f /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui | |
parent | e9b14585f5eecddf8c22b2e68aa1a0500682ba3a (diff) | |
download | open-keychain-7bc424a8cb1c3d4d4c77778f27bd18fe61da0736.tar.gz open-keychain-7bc424a8cb1c3d4d4c77778f27bd18fe61da0736.tar.bz2 open-keychain-7bc424a8cb1c3d4d4c77778f27bd18fe61da0736.zip |
work on passphrase caching, make use of cached SecretKeyType data (WIP!)
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui')
-rw-r--r-- | OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java | 55 |
1 files changed, 12 insertions, 43 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index ef2659cf8..f19309b51 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -47,6 +47,7 @@ import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.util.Log; @@ -98,17 +99,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor */ public static PassphraseDialogFragment newInstance(Context context, Messenger messenger, long secretKeyId) throws PgpGeneralException { - // check if secret key has a passphrase - if (!(secretKeyId == Constants.key.symmetric || secretKeyId == Constants.key.none)) { - try { - if (!new ProviderHelper(context).getCanonicalizedSecretKeyRing(secretKeyId).hasPassphrase()) { - throw new PgpGeneralException("No passphrase! No passphrase dialog needed!"); - } - } catch (ProviderHelper.NotFoundException e) { - throw new PgpGeneralException("Error: Key not found!", e); - } - } - + // do NOT check if the key even needs a passphrase. that's not our job here. PassphraseDialogFragment frag = new PassphraseDialogFragment(); Bundle args = new Bundle(); args.putLong(ARG_SECRET_KEY_ID, secretKeyId); @@ -141,7 +132,8 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor } else { try { ProviderHelper helper = new ProviderHelper(activity); - secretRing = helper.getCanonicalizedSecretKeyRing(secretKeyId); + secretRing = helper.getCanonicalizedSecretKeyRing( + KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(secretKeyId)); // yes the inner try/catch block is necessary, otherwise the final variable // above can't be statically verified to have been set in all cases because // the catch clause doesn't return. @@ -191,51 +183,28 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor return; } - CanonicalizedSecretKey unlockedSecretKey = null; - - for (CanonicalizedSecretKey clickSecretKey : secretRing.secretKeyIterator()) { - try { - boolean unlocked = clickSecretKey.unlock(passphrase); - if (unlocked) { - unlockedSecretKey = clickSecretKey; - break; - } - } catch (PgpGeneralException e) { - Toast.makeText(activity, R.string.error_could_not_extract_private_key, - Toast.LENGTH_SHORT).show(); - - sendMessageToHandler(MESSAGE_CANCEL); - return; // ran out of keys to try - } - } - - // Means we got an exception every time - if (unlockedSecretKey == null) { - Toast.makeText(activity, R.string.wrong_passphrase, + try { + // make sure this unlocks + // TODO this is a very costly operation, we should not be doing this here! + secretRing.getSecretKey(secretKeyId).unlock(passphrase); + } catch (PgpGeneralException e) { + Toast.makeText(activity, R.string.error_could_not_extract_private_key, Toast.LENGTH_SHORT).show(); sendMessageToHandler(MESSAGE_CANCEL); - return; + return; // ran out of keys to try } - long masterKeyId = secretRing.getMasterKeyId(); - // cache the new passphrase Log.d(Constants.TAG, "Everything okay! Caching entered passphrase"); try { - PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase, + PassphraseCacheService.addCachedPassphrase(activity, secretKeyId, passphrase, secretRing.getPrimaryUserIdWithFallback()); } catch (PgpGeneralException e) { Log.e(Constants.TAG, "adding of a passphrase failed", e); } - if (unlockedSecretKey.getKeyId() != masterKeyId) { - PassphraseCacheService.addCachedPassphrase( - activity, unlockedSecretKey.getKeyId(), passphrase, - unlockedSecretKey.getPrimaryUserIdWithFallback()); - } - // also return passphrase back to activity Bundle data = new Bundle(); data.putString(MESSAGE_DATA_PASSPHRASE, passphrase); |