From 49b4ff63122988dc1587844e6b4b2ee5d0855385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 9 Oct 2014 00:58:07 +0200 Subject: [PATCH 1/5] Replace PgpGeneralException with NotFoundException where appropriate --- .../keychain/pgp/CanonicalizedKeyRing.java | 16 +- .../pgp/CanonicalizedPublicKeyRing.java | 10 +- .../keychain/pgp/CanonicalizedSecretKey.java | 5 +- .../keychain/pgp/KeyRing.java | 20 +-- .../pgp/OpenPgpSignatureResultBuilder.java | 3 +- .../keychain/pgp/PgpSignEncrypt.java | 13 +- .../provider/CachedPublicKeyRing.java | 146 +++++++----------- .../keychain/remote/OpenPgpService.java | 23 +-- .../remote/ui/AccountSettingsActivity.java | 3 +- .../remote/ui/AccountSettingsFragment.java | 2 +- .../service/KeychainIntentService.java | 2 +- .../keychain/ui/EditKeyFragment.java | 3 - .../ui/EncryptAsymmetricFragment.java | 4 +- .../keychain/ui/MultiCertifyKeyFragment.java | 2 +- .../keychain/ui/PassphraseDialogActivity.java | 4 +- .../keychain/ui/ViewCertActivity.java | 2 +- .../keychain/ui/ViewKeyMainFragment.java | 2 +- .../ui/dialog/PassphraseDialogFragment.java | 4 +- .../ui/widget/EncryptKeyCompletionView.java | 3 +- .../keychain/util/ExportHelper.java | 2 +- 20 files changed, 119 insertions(+), 150 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java index 08b7316aa..cc0de1e34 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java @@ -19,7 +19,7 @@ package org.sufficientlysecure.keychain.pgp; import org.spongycastle.openpgp.PGPKeyRing; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import org.sufficientlysecure.keychain.util.IterableIterator; import java.io.IOException; @@ -56,11 +56,11 @@ public abstract class CanonicalizedKeyRing extends KeyRing { return getRing().getPublicKey().getFingerprint(); } - public String getPrimaryUserId() throws PgpGeneralException { + public String getPrimaryUserId() throws NotFoundException { return getPublicKey().getPrimaryUserId(); } - public String getPrimaryUserIdWithFallback() throws PgpGeneralException { + public String getPrimaryUserIdWithFallback() throws NotFoundException { return getPublicKey().getPrimaryUserIdWithFallback(); } @@ -87,24 +87,24 @@ public abstract class CanonicalizedKeyRing extends KeyRing { return creationDate.after(now) || (expiryDate != null && expiryDate.before(now)); } - public boolean canCertify() throws PgpGeneralException { + public boolean canCertify() throws NotFoundException { return getRing().getPublicKey().isEncryptionKey(); } - public long getEncryptId() throws PgpGeneralException { + public long getEncryptId() throws NotFoundException { for(CanonicalizedPublicKey key : publicKeyIterator()) { if (key.canEncrypt() && key.isValid()) { return key.getKeyId(); } } - throw new PgpGeneralException("No valid encryption key found!"); + throw new NotFoundException("No valid encryption key found!"); } - public boolean hasEncrypt() throws PgpGeneralException { + public boolean hasEncrypt() throws NotFoundException { try { getEncryptId(); return true; - } catch(PgpGeneralException e) { + } catch(NotFoundException e) { return false; } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java index 77c967c65..68ac7c133 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java @@ -21,7 +21,7 @@ package org.sufficientlysecure.keychain.pgp; import org.spongycastle.openpgp.PGPObjectFactory; import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKeyRing; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import org.sufficientlysecure.keychain.util.IterableIterator; import java.io.IOException; @@ -61,16 +61,16 @@ public class CanonicalizedPublicKeyRing extends CanonicalizedKeyRing { } /** Getter that returns the subkey that should be used for signing. */ - CanonicalizedPublicKey getEncryptionSubKey() throws PgpGeneralException { + CanonicalizedPublicKey getEncryptionSubKey() throws NotFoundException { PGPPublicKey key = getRing().getPublicKey(getEncryptId()); - if(key != null) { + if (key != null) { CanonicalizedPublicKey cKey = new CanonicalizedPublicKey(this, key); if(!cKey.canEncrypt()) { - throw new PgpGeneralException("key error"); + throw new NotFoundException("key error"); } return cKey; } - throw new PgpGeneralException("no encryption key available"); + throw new NotFoundException("no encryption key available"); } public IterableIterator publicKeyIterator() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java index 595f37872..48d8dbeb9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java @@ -41,6 +41,7 @@ import org.spongycastle.openpgp.operator.jcajce.NfcSyncPGPContentSignerBuilder; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; +import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; @@ -254,9 +255,11 @@ public class CanonicalizedSecretKey extends CanonicalizedPublicKey { spGen.setSignatureCreationTime(false, nfcCreationTimestamp); signatureGenerator.setHashedSubpackets(spGen.generate()); return signatureGenerator; - } catch (PGPException e) { + } catch (ProviderHelper.NotFoundException e) { // TODO: simply throw PGPException! throw new PgpGeneralException("Error initializing signature!", e); + } catch (PGPException e) { + throw new PgpGeneralException("Error initializing signature!", e); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java index b682378e9..17d20a326 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java @@ -20,7 +20,7 @@ package org.sufficientlysecure.keychain.pgp; import android.text.TextUtils; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -38,25 +38,25 @@ import java.util.regex.Pattern; */ public abstract class KeyRing { - abstract public long getMasterKeyId() throws PgpGeneralException; + abstract public long getMasterKeyId() throws NotFoundException; - abstract public String getPrimaryUserId() throws PgpGeneralException; + abstract public String getPrimaryUserId() throws NotFoundException; - abstract public String getPrimaryUserIdWithFallback() throws PgpGeneralException; + abstract public String getPrimaryUserIdWithFallback() throws NotFoundException; - public String[] getSplitPrimaryUserIdWithFallback() throws PgpGeneralException { + public String[] getSplitPrimaryUserIdWithFallback() throws NotFoundException { return splitUserId(getPrimaryUserIdWithFallback()); } - abstract public boolean isRevoked() throws PgpGeneralException; + abstract public boolean isRevoked() throws NotFoundException; - abstract public boolean canCertify() throws PgpGeneralException; + abstract public boolean canCertify() throws NotFoundException; - abstract public long getEncryptId() throws PgpGeneralException; + abstract public long getEncryptId() throws NotFoundException; - abstract public boolean hasEncrypt() throws PgpGeneralException; + abstract public boolean hasEncrypt() throws NotFoundException; - abstract public int getVerified() throws PgpGeneralException; + abstract public int getVerified() throws NotFoundException; private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$"); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java index bd7606194..f0bf97018 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.pgp; import org.openintents.openpgp.OpenPgpSignatureResult; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; @@ -96,7 +97,7 @@ public class OpenPgpSignatureResultBuilder { setKeyId(signingRing.getMasterKeyId()); try { setPrimaryUserId(signingRing.getPrimaryUserIdWithFallback()); - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { Log.d(Constants.TAG, "No primary user id in keyring with master key id " + signingRing.getMasterKeyId()); } setSignatureKeyCertified(signingRing.getVerified() > 0); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java index 40e265253..00696d150 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java @@ -385,18 +385,21 @@ public class PgpSignEncrypt { // Asymmetric encryption for (long id : mEncryptionMasterKeyIds) { + CanonicalizedPublicKeyRing keyRing = null; try { - CanonicalizedPublicKeyRing keyRing = mProviderHelper.getCanonicalizedPublicKeyRing( + keyRing = mProviderHelper.getCanonicalizedPublicKeyRing( KeyRings.buildUnifiedKeyRingUri(id)); + } catch (ProviderHelper.NotFoundException e) { + log.add(LogType.MSG_SE_KEY_UNKNOWN, indent + 1, + KeyFormattingUtils.convertKeyIdToHex(id)); + } + try { CanonicalizedPublicKey key = keyRing.getEncryptionSubKey(); cPk.addMethod(key.getPubKeyEncryptionGenerator()); log.add(LogType.MSG_SE_KEY_OK, indent + 1, KeyFormattingUtils.convertKeyIdToHex(id)); - } catch (PgpGeneralException e) { - log.add(LogType.MSG_SE_KEY_WARN, indent + 1, - KeyFormattingUtils.convertKeyIdToHex(id)); } catch (ProviderHelper.NotFoundException e) { - log.add(LogType.MSG_SE_KEY_UNKNOWN, indent + 1, + log.add(LogType.MSG_SE_KEY_WARN, indent + 1, KeyFormattingUtils.convertKeyIdToHex(id)); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java index 5a3770f2d..7be8cdd3b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java @@ -30,21 +30,21 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.Keys; import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import org.sufficientlysecure.keychain.util.Log; -/** This implementation of KeyRing provides a cached view of PublicKeyRing +/** + * This implementation of KeyRing provides a cached view of PublicKeyRing * objects based on database queries exclusively. - * + *

* This class should be used where only few points of data but no actual * cryptographic operations are required about a PublicKeyRing which is already * in the database. This happens commonly in UI code, where parsing of a PGP * key for examination would be a very expensive operation. - * + *

* Each getter method is implemented using a more or less expensive database * query, while object construction is (almost) free. A common pattern is * mProviderHelper.getCachedKeyRing(uri).getterMethod() - * + *

* TODO Ensure that the values returned here always match the ones returned by * the parsed KeyRing! - * */ public class CachedPublicKeyRing extends KeyRing { @@ -57,21 +57,17 @@ public class CachedPublicKeyRing extends KeyRing { } @Override - public long getMasterKeyId() throws PgpGeneralException { - try { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.MASTER_KEY_ID, ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data; - } catch (ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); - } + public long getMasterKeyId() throws NotFoundException { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.MASTER_KEY_ID, ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data; } /** * Find the master key id related to a given query. The id will either be extracted from the * query, which should work for all specific /key_rings/ queries, or will be queried if it can't. */ - public long extractOrGetMasterKeyId() throws PgpGeneralException { + public long extractOrGetMasterKeyId() throws NotFoundException { // try extracting from the uri first String firstSegment = mUri.getPathSegments().get(1); if (!firstSegment.equals("find")) try { @@ -83,114 +79,82 @@ public class CachedPublicKeyRing extends KeyRing { return getMasterKeyId(); } - public byte[] getFingerprint() throws PgpGeneralException { - try { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); - return (byte[]) data; - } catch (ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); - } + public byte[] getFingerprint() throws NotFoundException { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); + return (byte[]) data; } @Override - public String getPrimaryUserId() throws PgpGeneralException { - try { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.USER_ID, - ProviderHelper.FIELD_TYPE_STRING); - return (String) data; - } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); - } + public String getPrimaryUserId() throws NotFoundException { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.USER_ID, + ProviderHelper.FIELD_TYPE_STRING); + return (String) data; } - public String getPrimaryUserIdWithFallback() throws PgpGeneralException { + public String getPrimaryUserIdWithFallback() throws NotFoundException { return getPrimaryUserId(); } @Override - public boolean isRevoked() throws PgpGeneralException { - try { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.IS_REVOKED, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data > 0; - } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); - } + public boolean isRevoked() throws NotFoundException { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.IS_REVOKED, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data > 0; } @Override - public boolean canCertify() throws PgpGeneralException { - try { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.HAS_CERTIFY, - ProviderHelper.FIELD_TYPE_NULL); - return !((Boolean) data); - } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); - } + public boolean canCertify() throws NotFoundException { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.HAS_CERTIFY, + ProviderHelper.FIELD_TYPE_NULL); + return !((Boolean) data); } @Override - public long getEncryptId() throws PgpGeneralException { - try { - Object data = mProviderHelper.getGenericData(mUri, - KeyRings.HAS_ENCRYPT, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data; - } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); - } + public long getEncryptId() throws NotFoundException { + Object data = mProviderHelper.getGenericData(mUri, + KeyRings.HAS_ENCRYPT, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data; } @Override - public boolean hasEncrypt() throws PgpGeneralException { + public boolean hasEncrypt() throws NotFoundException { return getEncryptId() != 0; } - /** Returns the key id which should be used for signing. - * + /** + * Returns the key id which should be used for signing. + *

* This method returns keys which are actually available (ie. secret available, and not stripped, * revoked, or expired), hence only works on keyrings where a secret key is available! - * */ - public long getSecretSignId() throws PgpGeneralException { - try { - Object data = mProviderHelper.getGenericData(mUri, - KeyRings.HAS_SIGN, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data; - } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); - } + public long getSecretSignId() throws NotFoundException { + Object data = mProviderHelper.getGenericData(mUri, + KeyRings.HAS_SIGN, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data; } @Override - public int getVerified() throws PgpGeneralException { - try { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.VERIFIED, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Integer) data; - } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); - } + public int getVerified() throws NotFoundException { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.VERIFIED, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Integer) data; } - public boolean hasAnySecret() throws PgpGeneralException { - try { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.HAS_ANY_SECRET, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data > 0; - } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); - } + public boolean hasAnySecret() throws NotFoundException { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.HAS_ANY_SECRET, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data > 0; } - private Cursor getSubkeys() throws PgpGeneralException { + private Cursor getSubkeys() throws NotFoundException { Uri keysUri = KeychainContract.Keys.buildKeysUri(extractOrGetMasterKeyId()); return mProviderHelper.getContentResolver().query(keysUri, null, null, null, null); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index b650147cf..80199d00c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -272,23 +272,24 @@ public class OpenPgpService extends RemoteService { InputData inputData = new InputData(is, inputLength); // Find the appropriate subkey to sign with - CachedPublicKeyRing signingRing = - new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId()); - final long sigSubKeyId = signingRing.getSecretSignId(); + final long sigSubKeyId; + try { + CachedPublicKeyRing signingRing = + new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId()); + sigSubKeyId = signingRing.getSecretSignId(); + } catch (NotFoundException e) { + // secret key that is set for this account is deleted? + // show account config again! + return getCreateAccountIntent(data, getAccountName(data)); + } // get passphrase from cache, if key has "no" passphrase, this returns an empty String String passphrase; if (data.hasExtra(OpenPgpApi.EXTRA_PASSPHRASE)) { passphrase = data.getStringExtra(OpenPgpApi.EXTRA_PASSPHRASE); } else { - try { - passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), - accSettings.getKeyId(), sigSubKeyId); - } catch (PassphraseCacheService.KeyNotFoundException e) { - // secret key that is set for this account is deleted? - // show account config again! - return getCreateAccountIntent(data, getAccountName(data)); - } + passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), + accSettings.getKeyId(), sigSubKeyId); } if (passphrase == null) { // get PendingIntent for passphrase input, add it to given params and return to client diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java index b43dec2f1..421231fa2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java @@ -27,13 +27,12 @@ import android.view.View; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.ui.util.ActionBarHelper; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.remote.AccountSettings; import org.sufficientlysecure.keychain.service.results.OperationResult; -import org.sufficientlysecure.keychain.service.results.OperationResult.LogLevel; import org.sufficientlysecure.keychain.service.results.OperationResult.LogType; import org.sufficientlysecure.keychain.service.results.SingletonResult; +import org.sufficientlysecure.keychain.ui.util.ActionBarHelper; import org.sufficientlysecure.keychain.util.Log; public class AccountSettingsActivity extends ActionBarActivity { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java index a7eb58377..fe56d5ead 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java @@ -67,7 +67,7 @@ public class AccountSettingsFragment extends Fragment { } public void setAccSettings(AccountSettings accountSettings) { - this.mAccSettings = accountSettings; + mAccSettings = accountSettings; mAccNameView.setText(accountSettings.getAccountName()); mSelectKeySpinner.setSelectedKeyId(accountSettings.getKeyId()); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index b9c42db3f..21240c8dc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -719,7 +719,7 @@ public class KeychainIntentService extends IntentService implements Progressable builder.setNfcState(nfcHash, nfcTimestamp); } - } catch (PgpGeneralException e) { + } catch (NotFoundException e) { // encrypt-only // TODO Just silently drop the requested signature? Shouldn't we throw here? } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index 57c2cac5c..d90b29b8d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -229,9 +229,6 @@ public class EditKeyFragment extends LoaderFragment implements } catch (NotFoundException e) { finishWithError(LogType.MSG_EK_ERROR_NOT_FOUND); return; - } catch (PgpGeneralException e) { - finishWithError(LogType.MSG_EK_ERROR_NOT_FOUND); - return; } try { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java index 54877f676..0aaa03636 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java @@ -139,7 +139,7 @@ public class EncryptAsymmetricFragment extends Fragment implements EncryptActivi setSignatureKeyId(keyring.getMasterKeyId()); mSign.setSelectedKeyId(mEncryptInterface.getSignatureKey()); } - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } @@ -151,7 +151,7 @@ public class EncryptAsymmetricFragment extends Fragment implements EncryptActivi CachedPublicKeyRing ring = mProviderHelper.getCachedPublicKeyRing( KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(preselectedId)); mEncryptKeyView.addObject(mEncryptKeyView.new EncryptionKey(ring)); - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java index ddfbac03c..497ac48eb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java @@ -115,7 +115,7 @@ public class MultiCertifyKeyFragment extends LoaderFragment if (key.canCertify()) { mCertifyKeySpinner.setSelectedKeyId(certifyKeyId); } - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "certify certify check failed", e); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java index 4bfca9e1d..dcf96c66a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java @@ -151,7 +151,7 @@ public class PassphraseDialogActivity extends FragmentActivity { // the catch clause doesn't return. try { userId = mSecretRing.getPrimaryUserIdWithFallback(); - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { userId = null; } @@ -312,7 +312,7 @@ public class PassphraseDialogActivity extends FragmentActivity { PassphraseCacheService.addCachedPassphrase(getActivity(), mSecretRing.getMasterKeyId(), mSubKeyId, passphrase, mSecretRing.getPrimaryUserIdWithFallback()); - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "adding of a passphrase failed", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java index b0e641778..a894c519a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java @@ -185,7 +185,7 @@ public class ViewCertActivity extends ActionBarActivity KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(mCertifierKeyId)).getMasterKeyId(); viewIntent.setData(KeyRings.buildGenericKeyRingUri(signerMasterKeyId)); startActivity(viewIntent); - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { // TODO notify user of this, maybe offer download? Log.e(Constants.TAG, "key not found!", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java index 2d7bb07cf..a1a79b82e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java @@ -308,7 +308,7 @@ public class ViewKeyMainFragment extends LoaderFragment implements } // used instead of startActivity set actionbar based on callingPackage startActivityForResult(intent, 0); - } catch (PgpGeneralException e) { + } catch (NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } 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 43f869f02..b5aa2a3d2 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 @@ -145,7 +145,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // the catch clause doesn't return. try { userId = mSecretRing.getPrimaryUserIdWithFallback(); - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { userId = null; } @@ -308,7 +308,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor PassphraseCacheService.addCachedPassphrase(getActivity(), mSecretRing.getMasterKeyId(), mSubKeyId, passphrase, mSecretRing.getPrimaryUserIdWithFallback()); - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "adding of a passphrase failed", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java index 08599333a..44a3938f0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java @@ -42,6 +42,7 @@ import com.tokenautocomplete.TokenCompleteTextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.ContactHelper; import org.sufficientlysecure.keychain.pgp.KeyRing; @@ -198,7 +199,7 @@ public class EncryptKeyCompletionView extends TokenCompleteTextView { } - public EncryptionKey(CachedPublicKeyRing ring) throws PgpGeneralException { + public EncryptionKey(CachedPublicKeyRing ring) throws ProviderHelper.NotFoundException { this(ring.getPrimaryUserId(), ring.extractOrGetMasterKeyId(), KeyFormattingUtils.convertFingerprintToHex(ring.getFingerprint())); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java index 3c100e272..a8b4d029a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java @@ -57,7 +57,7 @@ public class ExportHelper { DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger, new long[]{ masterKeyId }); deleteKeyDialog.show(mActivity.getSupportFragmentManager(), "deleteKeyDialog"); - } catch (PgpGeneralException e) { + } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } From b3f56c927b47565bdaa7e3b4ea2a8a214aa56652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 9 Oct 2014 00:59:45 +0200 Subject: [PATCH 2/5] Revert "Replace PgpGeneralException with NotFoundException where appropriate" This reverts commit 49b4ff63122988dc1587844e6b4b2ee5d0855385. --- .../keychain/pgp/CanonicalizedKeyRing.java | 16 +- .../pgp/CanonicalizedPublicKeyRing.java | 10 +- .../keychain/pgp/CanonicalizedSecretKey.java | 5 +- .../keychain/pgp/KeyRing.java | 20 +-- .../pgp/OpenPgpSignatureResultBuilder.java | 3 +- .../keychain/pgp/PgpSignEncrypt.java | 13 +- .../provider/CachedPublicKeyRing.java | 146 +++++++++++------- .../keychain/remote/OpenPgpService.java | 23 ++- .../remote/ui/AccountSettingsActivity.java | 3 +- .../remote/ui/AccountSettingsFragment.java | 2 +- .../service/KeychainIntentService.java | 2 +- .../keychain/ui/EditKeyFragment.java | 3 + .../ui/EncryptAsymmetricFragment.java | 4 +- .../keychain/ui/MultiCertifyKeyFragment.java | 2 +- .../keychain/ui/PassphraseDialogActivity.java | 4 +- .../keychain/ui/ViewCertActivity.java | 2 +- .../keychain/ui/ViewKeyMainFragment.java | 2 +- .../ui/dialog/PassphraseDialogFragment.java | 4 +- .../ui/widget/EncryptKeyCompletionView.java | 3 +- .../keychain/util/ExportHelper.java | 2 +- 20 files changed, 150 insertions(+), 119 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java index cc0de1e34..08b7316aa 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java @@ -19,7 +19,7 @@ package org.sufficientlysecure.keychain.pgp; import org.spongycastle.openpgp.PGPKeyRing; -import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; +import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.util.IterableIterator; import java.io.IOException; @@ -56,11 +56,11 @@ public abstract class CanonicalizedKeyRing extends KeyRing { return getRing().getPublicKey().getFingerprint(); } - public String getPrimaryUserId() throws NotFoundException { + public String getPrimaryUserId() throws PgpGeneralException { return getPublicKey().getPrimaryUserId(); } - public String getPrimaryUserIdWithFallback() throws NotFoundException { + public String getPrimaryUserIdWithFallback() throws PgpGeneralException { return getPublicKey().getPrimaryUserIdWithFallback(); } @@ -87,24 +87,24 @@ public abstract class CanonicalizedKeyRing extends KeyRing { return creationDate.after(now) || (expiryDate != null && expiryDate.before(now)); } - public boolean canCertify() throws NotFoundException { + public boolean canCertify() throws PgpGeneralException { return getRing().getPublicKey().isEncryptionKey(); } - public long getEncryptId() throws NotFoundException { + public long getEncryptId() throws PgpGeneralException { for(CanonicalizedPublicKey key : publicKeyIterator()) { if (key.canEncrypt() && key.isValid()) { return key.getKeyId(); } } - throw new NotFoundException("No valid encryption key found!"); + throw new PgpGeneralException("No valid encryption key found!"); } - public boolean hasEncrypt() throws NotFoundException { + public boolean hasEncrypt() throws PgpGeneralException { try { getEncryptId(); return true; - } catch(NotFoundException e) { + } catch(PgpGeneralException e) { return false; } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java index 68ac7c133..77c967c65 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java @@ -21,7 +21,7 @@ package org.sufficientlysecure.keychain.pgp; import org.spongycastle.openpgp.PGPObjectFactory; import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKeyRing; -import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; +import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.util.IterableIterator; import java.io.IOException; @@ -61,16 +61,16 @@ public class CanonicalizedPublicKeyRing extends CanonicalizedKeyRing { } /** Getter that returns the subkey that should be used for signing. */ - CanonicalizedPublicKey getEncryptionSubKey() throws NotFoundException { + CanonicalizedPublicKey getEncryptionSubKey() throws PgpGeneralException { PGPPublicKey key = getRing().getPublicKey(getEncryptId()); - if (key != null) { + if(key != null) { CanonicalizedPublicKey cKey = new CanonicalizedPublicKey(this, key); if(!cKey.canEncrypt()) { - throw new NotFoundException("key error"); + throw new PgpGeneralException("key error"); } return cKey; } - throw new NotFoundException("no encryption key available"); + throw new PgpGeneralException("no encryption key available"); } public IterableIterator publicKeyIterator() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java index 48d8dbeb9..595f37872 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java @@ -41,7 +41,6 @@ import org.spongycastle.openpgp.operator.jcajce.NfcSyncPGPContentSignerBuilder; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; -import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; @@ -255,10 +254,8 @@ public class CanonicalizedSecretKey extends CanonicalizedPublicKey { spGen.setSignatureCreationTime(false, nfcCreationTimestamp); signatureGenerator.setHashedSubpackets(spGen.generate()); return signatureGenerator; - } catch (ProviderHelper.NotFoundException e) { - // TODO: simply throw PGPException! - throw new PgpGeneralException("Error initializing signature!", e); } catch (PGPException e) { + // TODO: simply throw PGPException! throw new PgpGeneralException("Error initializing signature!", e); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java index 17d20a326..b682378e9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java @@ -20,7 +20,7 @@ package org.sufficientlysecure.keychain.pgp; import android.text.TextUtils; -import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; +import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -38,25 +38,25 @@ import java.util.regex.Pattern; */ public abstract class KeyRing { - abstract public long getMasterKeyId() throws NotFoundException; + abstract public long getMasterKeyId() throws PgpGeneralException; - abstract public String getPrimaryUserId() throws NotFoundException; + abstract public String getPrimaryUserId() throws PgpGeneralException; - abstract public String getPrimaryUserIdWithFallback() throws NotFoundException; + abstract public String getPrimaryUserIdWithFallback() throws PgpGeneralException; - public String[] getSplitPrimaryUserIdWithFallback() throws NotFoundException { + public String[] getSplitPrimaryUserIdWithFallback() throws PgpGeneralException { return splitUserId(getPrimaryUserIdWithFallback()); } - abstract public boolean isRevoked() throws NotFoundException; + abstract public boolean isRevoked() throws PgpGeneralException; - abstract public boolean canCertify() throws NotFoundException; + abstract public boolean canCertify() throws PgpGeneralException; - abstract public long getEncryptId() throws NotFoundException; + abstract public long getEncryptId() throws PgpGeneralException; - abstract public boolean hasEncrypt() throws NotFoundException; + abstract public boolean hasEncrypt() throws PgpGeneralException; - abstract public int getVerified() throws NotFoundException; + abstract public int getVerified() throws PgpGeneralException; private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$"); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java index f0bf97018..bd7606194 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java @@ -20,7 +20,6 @@ package org.sufficientlysecure.keychain.pgp; import org.openintents.openpgp.OpenPgpSignatureResult; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; -import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; @@ -97,7 +96,7 @@ public class OpenPgpSignatureResultBuilder { setKeyId(signingRing.getMasterKeyId()); try { setPrimaryUserId(signingRing.getPrimaryUserIdWithFallback()); - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { Log.d(Constants.TAG, "No primary user id in keyring with master key id " + signingRing.getMasterKeyId()); } setSignatureKeyCertified(signingRing.getVerified() > 0); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java index 00696d150..40e265253 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java @@ -385,22 +385,19 @@ public class PgpSignEncrypt { // Asymmetric encryption for (long id : mEncryptionMasterKeyIds) { - CanonicalizedPublicKeyRing keyRing = null; try { - keyRing = mProviderHelper.getCanonicalizedPublicKeyRing( + CanonicalizedPublicKeyRing keyRing = mProviderHelper.getCanonicalizedPublicKeyRing( KeyRings.buildUnifiedKeyRingUri(id)); - } catch (ProviderHelper.NotFoundException e) { - log.add(LogType.MSG_SE_KEY_UNKNOWN, indent + 1, - KeyFormattingUtils.convertKeyIdToHex(id)); - } - try { CanonicalizedPublicKey key = keyRing.getEncryptionSubKey(); cPk.addMethod(key.getPubKeyEncryptionGenerator()); log.add(LogType.MSG_SE_KEY_OK, indent + 1, KeyFormattingUtils.convertKeyIdToHex(id)); - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { log.add(LogType.MSG_SE_KEY_WARN, indent + 1, KeyFormattingUtils.convertKeyIdToHex(id)); + } catch (ProviderHelper.NotFoundException e) { + log.add(LogType.MSG_SE_KEY_UNKNOWN, indent + 1, + KeyFormattingUtils.convertKeyIdToHex(id)); } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java index 7be8cdd3b..5a3770f2d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java @@ -30,21 +30,21 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.Keys; import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import org.sufficientlysecure.keychain.util.Log; -/** - * This implementation of KeyRing provides a cached view of PublicKeyRing +/** This implementation of KeyRing provides a cached view of PublicKeyRing * objects based on database queries exclusively. - *

+ * * This class should be used where only few points of data but no actual * cryptographic operations are required about a PublicKeyRing which is already * in the database. This happens commonly in UI code, where parsing of a PGP * key for examination would be a very expensive operation. - *

+ * * Each getter method is implemented using a more or less expensive database * query, while object construction is (almost) free. A common pattern is * mProviderHelper.getCachedKeyRing(uri).getterMethod() - *

+ * * TODO Ensure that the values returned here always match the ones returned by * the parsed KeyRing! + * */ public class CachedPublicKeyRing extends KeyRing { @@ -57,17 +57,21 @@ public class CachedPublicKeyRing extends KeyRing { } @Override - public long getMasterKeyId() throws NotFoundException { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.MASTER_KEY_ID, ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data; + public long getMasterKeyId() throws PgpGeneralException { + try { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.MASTER_KEY_ID, ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data; + } catch (ProviderHelper.NotFoundException e) { + throw new PgpGeneralException(e); + } } /** * Find the master key id related to a given query. The id will either be extracted from the * query, which should work for all specific /key_rings/ queries, or will be queried if it can't. */ - public long extractOrGetMasterKeyId() throws NotFoundException { + public long extractOrGetMasterKeyId() throws PgpGeneralException { // try extracting from the uri first String firstSegment = mUri.getPathSegments().get(1); if (!firstSegment.equals("find")) try { @@ -79,82 +83,114 @@ public class CachedPublicKeyRing extends KeyRing { return getMasterKeyId(); } - public byte[] getFingerprint() throws NotFoundException { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); - return (byte[]) data; + public byte[] getFingerprint() throws PgpGeneralException { + try { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); + return (byte[]) data; + } catch (ProviderHelper.NotFoundException e) { + throw new PgpGeneralException(e); + } } @Override - public String getPrimaryUserId() throws NotFoundException { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.USER_ID, - ProviderHelper.FIELD_TYPE_STRING); - return (String) data; + public String getPrimaryUserId() throws PgpGeneralException { + try { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.USER_ID, + ProviderHelper.FIELD_TYPE_STRING); + return (String) data; + } catch(ProviderHelper.NotFoundException e) { + throw new PgpGeneralException(e); + } } - public String getPrimaryUserIdWithFallback() throws NotFoundException { + public String getPrimaryUserIdWithFallback() throws PgpGeneralException { return getPrimaryUserId(); } @Override - public boolean isRevoked() throws NotFoundException { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.IS_REVOKED, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data > 0; + public boolean isRevoked() throws PgpGeneralException { + try { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.IS_REVOKED, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data > 0; + } catch(ProviderHelper.NotFoundException e) { + throw new PgpGeneralException(e); + } } @Override - public boolean canCertify() throws NotFoundException { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.HAS_CERTIFY, - ProviderHelper.FIELD_TYPE_NULL); - return !((Boolean) data); + public boolean canCertify() throws PgpGeneralException { + try { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.HAS_CERTIFY, + ProviderHelper.FIELD_TYPE_NULL); + return !((Boolean) data); + } catch(ProviderHelper.NotFoundException e) { + throw new PgpGeneralException(e); + } } @Override - public long getEncryptId() throws NotFoundException { - Object data = mProviderHelper.getGenericData(mUri, - KeyRings.HAS_ENCRYPT, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data; + public long getEncryptId() throws PgpGeneralException { + try { + Object data = mProviderHelper.getGenericData(mUri, + KeyRings.HAS_ENCRYPT, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data; + } catch(ProviderHelper.NotFoundException e) { + throw new PgpGeneralException(e); + } } @Override - public boolean hasEncrypt() throws NotFoundException { + public boolean hasEncrypt() throws PgpGeneralException { return getEncryptId() != 0; } - /** - * Returns the key id which should be used for signing. - *

+ /** Returns the key id which should be used for signing. + * * This method returns keys which are actually available (ie. secret available, and not stripped, * revoked, or expired), hence only works on keyrings where a secret key is available! + * */ - public long getSecretSignId() throws NotFoundException { - Object data = mProviderHelper.getGenericData(mUri, - KeyRings.HAS_SIGN, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data; + public long getSecretSignId() throws PgpGeneralException { + try { + Object data = mProviderHelper.getGenericData(mUri, + KeyRings.HAS_SIGN, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data; + } catch(ProviderHelper.NotFoundException e) { + throw new PgpGeneralException(e); + } } @Override - public int getVerified() throws NotFoundException { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.VERIFIED, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Integer) data; + public int getVerified() throws PgpGeneralException { + try { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.VERIFIED, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Integer) data; + } catch(ProviderHelper.NotFoundException e) { + throw new PgpGeneralException(e); + } } - public boolean hasAnySecret() throws NotFoundException { - Object data = mProviderHelper.getGenericData(mUri, - KeychainContract.KeyRings.HAS_ANY_SECRET, - ProviderHelper.FIELD_TYPE_INTEGER); - return (Long) data > 0; + public boolean hasAnySecret() throws PgpGeneralException { + try { + Object data = mProviderHelper.getGenericData(mUri, + KeychainContract.KeyRings.HAS_ANY_SECRET, + ProviderHelper.FIELD_TYPE_INTEGER); + return (Long) data > 0; + } catch(ProviderHelper.NotFoundException e) { + throw new PgpGeneralException(e); + } } - private Cursor getSubkeys() throws NotFoundException { + private Cursor getSubkeys() throws PgpGeneralException { Uri keysUri = KeychainContract.Keys.buildKeysUri(extractOrGetMasterKeyId()); return mProviderHelper.getContentResolver().query(keysUri, null, null, null, null); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index 80199d00c..b650147cf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -272,24 +272,23 @@ public class OpenPgpService extends RemoteService { InputData inputData = new InputData(is, inputLength); // Find the appropriate subkey to sign with - final long sigSubKeyId; - try { - CachedPublicKeyRing signingRing = - new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId()); - sigSubKeyId = signingRing.getSecretSignId(); - } catch (NotFoundException e) { - // secret key that is set for this account is deleted? - // show account config again! - return getCreateAccountIntent(data, getAccountName(data)); - } + CachedPublicKeyRing signingRing = + new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId()); + final long sigSubKeyId = signingRing.getSecretSignId(); // get passphrase from cache, if key has "no" passphrase, this returns an empty String String passphrase; if (data.hasExtra(OpenPgpApi.EXTRA_PASSPHRASE)) { passphrase = data.getStringExtra(OpenPgpApi.EXTRA_PASSPHRASE); } else { - passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), - accSettings.getKeyId(), sigSubKeyId); + try { + passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), + accSettings.getKeyId(), sigSubKeyId); + } catch (PassphraseCacheService.KeyNotFoundException e) { + // secret key that is set for this account is deleted? + // show account config again! + return getCreateAccountIntent(data, getAccountName(data)); + } } if (passphrase == null) { // get PendingIntent for passphrase input, add it to given params and return to client diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java index 421231fa2..b43dec2f1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java @@ -27,12 +27,13 @@ import android.view.View; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.util.ActionBarHelper; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.remote.AccountSettings; import org.sufficientlysecure.keychain.service.results.OperationResult; +import org.sufficientlysecure.keychain.service.results.OperationResult.LogLevel; import org.sufficientlysecure.keychain.service.results.OperationResult.LogType; import org.sufficientlysecure.keychain.service.results.SingletonResult; -import org.sufficientlysecure.keychain.ui.util.ActionBarHelper; import org.sufficientlysecure.keychain.util.Log; public class AccountSettingsActivity extends ActionBarActivity { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java index fe56d5ead..a7eb58377 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java @@ -67,7 +67,7 @@ public class AccountSettingsFragment extends Fragment { } public void setAccSettings(AccountSettings accountSettings) { - mAccSettings = accountSettings; + this.mAccSettings = accountSettings; mAccNameView.setText(accountSettings.getAccountName()); mSelectKeySpinner.setSelectedKeyId(accountSettings.getKeyId()); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 21240c8dc..b9c42db3f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -719,7 +719,7 @@ public class KeychainIntentService extends IntentService implements Progressable builder.setNfcState(nfcHash, nfcTimestamp); } - } catch (NotFoundException e) { + } catch (PgpGeneralException e) { // encrypt-only // TODO Just silently drop the requested signature? Shouldn't we throw here? } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index d90b29b8d..57c2cac5c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -229,6 +229,9 @@ public class EditKeyFragment extends LoaderFragment implements } catch (NotFoundException e) { finishWithError(LogType.MSG_EK_ERROR_NOT_FOUND); return; + } catch (PgpGeneralException e) { + finishWithError(LogType.MSG_EK_ERROR_NOT_FOUND); + return; } try { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java index 0aaa03636..54877f676 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java @@ -139,7 +139,7 @@ public class EncryptAsymmetricFragment extends Fragment implements EncryptActivi setSignatureKeyId(keyring.getMasterKeyId()); mSign.setSelectedKeyId(mEncryptInterface.getSignatureKey()); } - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { Log.e(Constants.TAG, "key not found!", e); } } @@ -151,7 +151,7 @@ public class EncryptAsymmetricFragment extends Fragment implements EncryptActivi CachedPublicKeyRing ring = mProviderHelper.getCachedPublicKeyRing( KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(preselectedId)); mEncryptKeyView.addObject(mEncryptKeyView.new EncryptionKey(ring)); - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { Log.e(Constants.TAG, "key not found!", e); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java index 497ac48eb..ddfbac03c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java @@ -115,7 +115,7 @@ public class MultiCertifyKeyFragment extends LoaderFragment if (key.canCertify()) { mCertifyKeySpinner.setSelectedKeyId(certifyKeyId); } - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { Log.e(Constants.TAG, "certify certify check failed", e); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java index dcf96c66a..4bfca9e1d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java @@ -151,7 +151,7 @@ public class PassphraseDialogActivity extends FragmentActivity { // the catch clause doesn't return. try { userId = mSecretRing.getPrimaryUserIdWithFallback(); - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { userId = null; } @@ -312,7 +312,7 @@ public class PassphraseDialogActivity extends FragmentActivity { PassphraseCacheService.addCachedPassphrase(getActivity(), mSecretRing.getMasterKeyId(), mSubKeyId, passphrase, mSecretRing.getPrimaryUserIdWithFallback()); - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { Log.e(Constants.TAG, "adding of a passphrase failed", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java index a894c519a..b0e641778 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java @@ -185,7 +185,7 @@ public class ViewCertActivity extends ActionBarActivity KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(mCertifierKeyId)).getMasterKeyId(); viewIntent.setData(KeyRings.buildGenericKeyRingUri(signerMasterKeyId)); startActivity(viewIntent); - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { // TODO notify user of this, maybe offer download? Log.e(Constants.TAG, "key not found!", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java index a1a79b82e..2d7bb07cf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java @@ -308,7 +308,7 @@ public class ViewKeyMainFragment extends LoaderFragment implements } // used instead of startActivity set actionbar based on callingPackage startActivityForResult(intent, 0); - } catch (NotFoundException e) { + } catch (PgpGeneralException e) { Log.e(Constants.TAG, "key not found!", e); } } 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 b5aa2a3d2..43f869f02 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 @@ -145,7 +145,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // the catch clause doesn't return. try { userId = mSecretRing.getPrimaryUserIdWithFallback(); - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { userId = null; } @@ -308,7 +308,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor PassphraseCacheService.addCachedPassphrase(getActivity(), mSecretRing.getMasterKeyId(), mSubKeyId, passphrase, mSecretRing.getPrimaryUserIdWithFallback()); - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { Log.e(Constants.TAG, "adding of a passphrase failed", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java index 44a3938f0..08599333a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java @@ -42,7 +42,6 @@ import com.tokenautocomplete.TokenCompleteTextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.ContactHelper; import org.sufficientlysecure.keychain.pgp.KeyRing; @@ -199,7 +198,7 @@ public class EncryptKeyCompletionView extends TokenCompleteTextView { } - public EncryptionKey(CachedPublicKeyRing ring) throws ProviderHelper.NotFoundException { + public EncryptionKey(CachedPublicKeyRing ring) throws PgpGeneralException { this(ring.getPrimaryUserId(), ring.extractOrGetMasterKeyId(), KeyFormattingUtils.convertFingerprintToHex(ring.getFingerprint())); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java index a8b4d029a..3c100e272 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java @@ -57,7 +57,7 @@ public class ExportHelper { DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger, new long[]{ masterKeyId }); deleteKeyDialog.show(mActivity.getSupportFragmentManager(), "deleteKeyDialog"); - } catch (ProviderHelper.NotFoundException e) { + } catch (PgpGeneralException e) { Log.e(Constants.TAG, "key not found!", e); } } From 45b02008fba52eaba833009c517afa697d2443f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 9 Oct 2014 01:37:44 +0200 Subject: [PATCH 3/5] Replace many PgpGeneralExceptions with PgpKeyNotFoundException --- .../keychain/pgp/CanonicalizedKeyRing.java | 15 +++--- .../pgp/CanonicalizedPublicKeyRing.java | 8 ++-- .../keychain/pgp/CanonicalizedSecretKey.java | 5 +- .../keychain/pgp/KeyRing.java | 20 ++++---- .../pgp/OpenPgpSignatureResultBuilder.java | 3 +- .../keychain/pgp/PgpSignEncrypt.java | 3 +- .../exception/PgpKeyNotFoundException.java | 33 +++++++++++++ .../provider/CachedPublicKeyRing.java | 46 +++++++++---------- .../keychain/remote/OpenPgpService.java | 17 +++++-- .../service/KeychainIntentService.java | 3 +- .../service/PassphraseCacheService.java | 6 +-- .../keychain/ui/EditKeyFragment.java | 5 +- .../ui/EncryptAsymmetricFragment.java | 5 +- .../keychain/ui/MultiCertifyKeyFragment.java | 3 +- .../keychain/ui/PassphraseDialogActivity.java | 7 +-- .../keychain/ui/ViewCertActivity.java | 3 +- .../keychain/ui/ViewKeyMainFragment.java | 3 +- .../ui/dialog/PassphraseDialogFragment.java | 5 +- .../ui/widget/EncryptKeyCompletionView.java | 3 +- .../keychain/util/ExportHelper.java | 3 +- 20 files changed, 126 insertions(+), 70 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/exception/PgpKeyNotFoundException.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java index 08b7316aa..db0a9b137 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.pgp; import org.spongycastle.openpgp.PGPKeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.util.IterableIterator; import java.io.IOException; @@ -56,11 +57,11 @@ public abstract class CanonicalizedKeyRing extends KeyRing { return getRing().getPublicKey().getFingerprint(); } - public String getPrimaryUserId() throws PgpGeneralException { + public String getPrimaryUserId() throws PgpKeyNotFoundException { return getPublicKey().getPrimaryUserId(); } - public String getPrimaryUserIdWithFallback() throws PgpGeneralException { + public String getPrimaryUserIdWithFallback() throws PgpKeyNotFoundException { return getPublicKey().getPrimaryUserIdWithFallback(); } @@ -87,24 +88,24 @@ public abstract class CanonicalizedKeyRing extends KeyRing { return creationDate.after(now) || (expiryDate != null && expiryDate.before(now)); } - public boolean canCertify() throws PgpGeneralException { + public boolean canCertify() throws PgpKeyNotFoundException { return getRing().getPublicKey().isEncryptionKey(); } - public long getEncryptId() throws PgpGeneralException { + public long getEncryptId() throws PgpKeyNotFoundException { for(CanonicalizedPublicKey key : publicKeyIterator()) { if (key.canEncrypt() && key.isValid()) { return key.getKeyId(); } } - throw new PgpGeneralException("No valid encryption key found!"); + throw new PgpKeyNotFoundException("No valid encryption key found!"); } - public boolean hasEncrypt() throws PgpGeneralException { + public boolean hasEncrypt() throws PgpKeyNotFoundException { try { getEncryptId(); return true; - } catch(PgpGeneralException e) { + } catch(PgpKeyNotFoundException e) { return false; } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java index 77c967c65..85ef3eaa4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java @@ -21,7 +21,7 @@ package org.sufficientlysecure.keychain.pgp; import org.spongycastle.openpgp.PGPObjectFactory; import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKeyRing; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.util.IterableIterator; import java.io.IOException; @@ -61,16 +61,16 @@ public class CanonicalizedPublicKeyRing extends CanonicalizedKeyRing { } /** Getter that returns the subkey that should be used for signing. */ - CanonicalizedPublicKey getEncryptionSubKey() throws PgpGeneralException { + CanonicalizedPublicKey getEncryptionSubKey() throws PgpKeyNotFoundException { PGPPublicKey key = getRing().getPublicKey(getEncryptId()); if(key != null) { CanonicalizedPublicKey cKey = new CanonicalizedPublicKey(this, key); if(!cKey.canEncrypt()) { - throw new PgpGeneralException("key error"); + throw new PgpKeyNotFoundException("key error"); } return cKey; } - throw new PgpGeneralException("no encryption key available"); + throw new PgpKeyNotFoundException("no encryption key available"); } public IterableIterator publicKeyIterator() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java index 595f37872..a65759a3b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java @@ -41,6 +41,7 @@ import org.spongycastle.openpgp.operator.jcajce.NfcSyncPGPContentSignerBuilder; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; @@ -254,9 +255,11 @@ public class CanonicalizedSecretKey extends CanonicalizedPublicKey { spGen.setSignatureCreationTime(false, nfcCreationTimestamp); signatureGenerator.setHashedSubpackets(spGen.generate()); return signatureGenerator; - } catch (PGPException e) { + } catch (PgpKeyNotFoundException e) { // TODO: simply throw PGPException! throw new PgpGeneralException("Error initializing signature!", e); + } catch (PGPException e) { + throw new PgpGeneralException("Error initializing signature!", e); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java index b682378e9..26375219b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java @@ -20,7 +20,7 @@ package org.sufficientlysecure.keychain.pgp; import android.text.TextUtils; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -38,25 +38,25 @@ import java.util.regex.Pattern; */ public abstract class KeyRing { - abstract public long getMasterKeyId() throws PgpGeneralException; + abstract public long getMasterKeyId() throws PgpKeyNotFoundException; - abstract public String getPrimaryUserId() throws PgpGeneralException; + abstract public String getPrimaryUserId() throws PgpKeyNotFoundException; - abstract public String getPrimaryUserIdWithFallback() throws PgpGeneralException; + abstract public String getPrimaryUserIdWithFallback() throws PgpKeyNotFoundException; - public String[] getSplitPrimaryUserIdWithFallback() throws PgpGeneralException { + public String[] getSplitPrimaryUserIdWithFallback() throws PgpKeyNotFoundException { return splitUserId(getPrimaryUserIdWithFallback()); } - abstract public boolean isRevoked() throws PgpGeneralException; + abstract public boolean isRevoked() throws PgpKeyNotFoundException; - abstract public boolean canCertify() throws PgpGeneralException; + abstract public boolean canCertify() throws PgpKeyNotFoundException; - abstract public long getEncryptId() throws PgpGeneralException; + abstract public long getEncryptId() throws PgpKeyNotFoundException; - abstract public boolean hasEncrypt() throws PgpGeneralException; + abstract public boolean hasEncrypt() throws PgpKeyNotFoundException; - abstract public int getVerified() throws PgpGeneralException; + abstract public int getVerified() throws PgpKeyNotFoundException; private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$"); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java index bd7606194..aa324c7ed 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OpenPgpSignatureResultBuilder.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.pgp; import org.openintents.openpgp.OpenPgpSignatureResult; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; @@ -96,7 +97,7 @@ public class OpenPgpSignatureResultBuilder { setKeyId(signingRing.getMasterKeyId()); try { setPrimaryUserId(signingRing.getPrimaryUserIdWithFallback()); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { Log.d(Constants.TAG, "No primary user id in keyring with master key id " + signingRing.getMasterKeyId()); } setSignatureKeyCertified(signingRing.getVerified() > 0); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java index 40e265253..739a92b3b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java @@ -35,6 +35,7 @@ import org.spongycastle.util.encoders.Hex; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.results.OperationResult.LogType; @@ -392,7 +393,7 @@ public class PgpSignEncrypt { cPk.addMethod(key.getPubKeyEncryptionGenerator()); log.add(LogType.MSG_SE_KEY_OK, indent + 1, KeyFormattingUtils.convertKeyIdToHex(id)); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { log.add(LogType.MSG_SE_KEY_WARN, indent + 1, KeyFormattingUtils.convertKeyIdToHex(id)); } catch (ProviderHelper.NotFoundException e) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/exception/PgpKeyNotFoundException.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/exception/PgpKeyNotFoundException.java new file mode 100644 index 000000000..21a7c0cf0 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/exception/PgpKeyNotFoundException.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2012-2014 Dominik Schürmann + * Copyright (C) 2010-2014 Thialfihar + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.pgp.exception; + +public class PgpKeyNotFoundException extends Exception { + static final long serialVersionUID = 0xf812773342L; + + public PgpKeyNotFoundException(String message) { + super(message); + } + public PgpKeyNotFoundException(String message, Throwable cause) { + super(message, cause); + } + public PgpKeyNotFoundException(Throwable cause) { + super(cause); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java index 5a3770f2d..7c9ef719e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java @@ -24,7 +24,7 @@ import android.net.Uri; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType; import org.sufficientlysecure.keychain.pgp.KeyRing; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainContract.Keys; import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; @@ -57,13 +57,13 @@ public class CachedPublicKeyRing extends KeyRing { } @Override - public long getMasterKeyId() throws PgpGeneralException { + public long getMasterKeyId() throws PgpKeyNotFoundException { try { Object data = mProviderHelper.getGenericData(mUri, KeychainContract.KeyRings.MASTER_KEY_ID, ProviderHelper.FIELD_TYPE_INTEGER); return (Long) data; } catch (ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); + throw new PgpKeyNotFoundException(e); } } @@ -71,7 +71,7 @@ public class CachedPublicKeyRing extends KeyRing { * Find the master key id related to a given query. The id will either be extracted from the * query, which should work for all specific /key_rings/ queries, or will be queried if it can't. */ - public long extractOrGetMasterKeyId() throws PgpGeneralException { + public long extractOrGetMasterKeyId() throws PgpKeyNotFoundException { // try extracting from the uri first String firstSegment = mUri.getPathSegments().get(1); if (!firstSegment.equals("find")) try { @@ -83,70 +83,70 @@ public class CachedPublicKeyRing extends KeyRing { return getMasterKeyId(); } - public byte[] getFingerprint() throws PgpGeneralException { + public byte[] getFingerprint() throws PgpKeyNotFoundException { try { Object data = mProviderHelper.getGenericData(mUri, KeychainContract.KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); return (byte[]) data; } catch (ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); + throw new PgpKeyNotFoundException(e); } } @Override - public String getPrimaryUserId() throws PgpGeneralException { + public String getPrimaryUserId() throws PgpKeyNotFoundException { try { Object data = mProviderHelper.getGenericData(mUri, KeychainContract.KeyRings.USER_ID, ProviderHelper.FIELD_TYPE_STRING); return (String) data; } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); + throw new PgpKeyNotFoundException(e); } } - public String getPrimaryUserIdWithFallback() throws PgpGeneralException { + public String getPrimaryUserIdWithFallback() throws PgpKeyNotFoundException { return getPrimaryUserId(); } @Override - public boolean isRevoked() throws PgpGeneralException { + public boolean isRevoked() throws PgpKeyNotFoundException { try { Object data = mProviderHelper.getGenericData(mUri, KeychainContract.KeyRings.IS_REVOKED, ProviderHelper.FIELD_TYPE_INTEGER); return (Long) data > 0; } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); + throw new PgpKeyNotFoundException(e); } } @Override - public boolean canCertify() throws PgpGeneralException { + public boolean canCertify() throws PgpKeyNotFoundException { try { Object data = mProviderHelper.getGenericData(mUri, KeychainContract.KeyRings.HAS_CERTIFY, ProviderHelper.FIELD_TYPE_NULL); return !((Boolean) data); } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); + throw new PgpKeyNotFoundException(e); } } @Override - public long getEncryptId() throws PgpGeneralException { + public long getEncryptId() throws PgpKeyNotFoundException { try { Object data = mProviderHelper.getGenericData(mUri, KeyRings.HAS_ENCRYPT, ProviderHelper.FIELD_TYPE_INTEGER); return (Long) data; } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); + throw new PgpKeyNotFoundException(e); } } @Override - public boolean hasEncrypt() throws PgpGeneralException { + public boolean hasEncrypt() throws PgpKeyNotFoundException { return getEncryptId() != 0; } @@ -156,41 +156,41 @@ public class CachedPublicKeyRing extends KeyRing { * revoked, or expired), hence only works on keyrings where a secret key is available! * */ - public long getSecretSignId() throws PgpGeneralException { + public long getSecretSignId() throws PgpKeyNotFoundException { try { Object data = mProviderHelper.getGenericData(mUri, KeyRings.HAS_SIGN, ProviderHelper.FIELD_TYPE_INTEGER); return (Long) data; } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); + throw new PgpKeyNotFoundException(e); } } @Override - public int getVerified() throws PgpGeneralException { + public int getVerified() throws PgpKeyNotFoundException { try { Object data = mProviderHelper.getGenericData(mUri, KeychainContract.KeyRings.VERIFIED, ProviderHelper.FIELD_TYPE_INTEGER); return (Integer) data; } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); + throw new PgpKeyNotFoundException(e); } } - public boolean hasAnySecret() throws PgpGeneralException { + public boolean hasAnySecret() throws PgpKeyNotFoundException { try { Object data = mProviderHelper.getGenericData(mUri, KeychainContract.KeyRings.HAS_ANY_SECRET, ProviderHelper.FIELD_TYPE_INTEGER); return (Long) data > 0; } catch(ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(e); + throw new PgpKeyNotFoundException(e); } } - private Cursor getSubkeys() throws PgpGeneralException { + private Cursor getSubkeys() throws PgpKeyNotFoundException { Uri keysUri = KeychainContract.Keys.buildKeysUri(extractOrGetMasterKeyId()); return mProviderHelper.getContentResolver().query(keysUri, null, null, null, null); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index b650147cf..eb016529d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -31,6 +31,7 @@ import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.util.OpenPgpApi; import org.spongycastle.util.encoders.Hex; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.ui.NfcActivity; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.PassphraseCacheInterface; @@ -272,9 +273,16 @@ public class OpenPgpService extends RemoteService { InputData inputData = new InputData(is, inputLength); // Find the appropriate subkey to sign with - CachedPublicKeyRing signingRing = - new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId()); - final long sigSubKeyId = signingRing.getSecretSignId(); + long sigSubKeyId; + try { + CachedPublicKeyRing signingRing = + new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId()); + sigSubKeyId = signingRing.getSecretSignId(); + } catch (PgpKeyNotFoundException e) { + // secret key that is set for this account is deleted? + // show account config again! + return getCreateAccountIntent(data, getAccountName(data)); + } // get passphrase from cache, if key has "no" passphrase, this returns an empty String String passphrase; @@ -285,8 +293,7 @@ public class OpenPgpService extends RemoteService { passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), accSettings.getKeyId(), sigSubKeyId); } catch (PassphraseCacheService.KeyNotFoundException e) { - // secret key that is set for this account is deleted? - // show account config again! + // should happen earlier, but return again here if it happens return getCreateAccountIntent(data, getAccountName(data)); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index b9c42db3f..90091ce35 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -30,6 +30,7 @@ import android.os.RemoteException; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.PgpCertifyOperation; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import org.sufficientlysecure.keychain.service.results.CertifyResult; import org.sufficientlysecure.keychain.util.FileHelper; @@ -719,7 +720,7 @@ public class KeychainIntentService extends IntentService implements Progressable builder.setNfcState(nfcHash, nfcTimestamp); } - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { // encrypt-only // TODO Just silently drop the requested signature? Shouldn't we throw here? } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 9638fb7bf..97fb7e7b4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -39,11 +39,11 @@ import android.support.v4.util.LongSparseArray; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.ProviderHelper; -import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import org.sufficientlysecure.keychain.util.Log; import java.util.Date; @@ -239,9 +239,9 @@ public class PassphraseCacheService extends Service { case PASSPHRASE_EMPTY: return ""; case UNAVAILABLE: - throw new NotFoundException("secret key for this subkey is not available"); + throw new ProviderHelper.NotFoundException("secret key for this subkey is not available"); case GNU_DUMMY: - throw new NotFoundException("secret key for stripped subkey is not available"); + throw new ProviderHelper.NotFoundException("secret key for stripped subkey is not available"); } // get cached passphrase diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index 57c2cac5c..dce86abd2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -40,6 +40,7 @@ import android.widget.ListView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.ui.util.ActionBarHelper; import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; @@ -226,10 +227,10 @@ public class EditKeyFragment extends LoaderFragment implements mSaveKeyringParcel = new SaveKeyringParcel(masterKeyId, keyRing.getFingerprint()); mPrimaryUserId = keyRing.getPrimaryUserIdWithFallback(); - } catch (NotFoundException e) { + } catch (PgpKeyNotFoundException e) { finishWithError(LogType.MSG_EK_ERROR_NOT_FOUND); return; - } catch (PgpGeneralException e) { + } catch (NotFoundException e) { finishWithError(LogType.MSG_EK_ERROR_NOT_FOUND); return; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java index 54877f676..2d1b66daa 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java @@ -29,6 +29,7 @@ import com.tokenautocomplete.TokenCompleteTextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.ProviderHelper; @@ -139,7 +140,7 @@ public class EncryptAsymmetricFragment extends Fragment implements EncryptActivi setSignatureKeyId(keyring.getMasterKeyId()); mSign.setSelectedKeyId(mEncryptInterface.getSignatureKey()); } - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } @@ -151,7 +152,7 @@ public class EncryptAsymmetricFragment extends Fragment implements EncryptActivi CachedPublicKeyRing ring = mProviderHelper.getCachedPublicKeyRing( KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(preselectedId)); mEncryptKeyView.addObject(mEncryptKeyView.new EncryptionKey(ring)); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java index ddfbac03c..34589c8e7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/MultiCertifyKeyFragment.java @@ -45,6 +45,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds; import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables; @@ -115,7 +116,7 @@ public class MultiCertifyKeyFragment extends LoaderFragment if (key.canCertify()) { mCertifyKeySpinner.setSelectedKeyId(certifyKeyId); } - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { Log.e(Constants.TAG, "certify certify check failed", e); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java index 4bfca9e1d..b9761fbf3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java @@ -45,6 +45,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.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; @@ -151,11 +152,11 @@ public class PassphraseDialogActivity extends FragmentActivity { // the catch clause doesn't return. try { userId = mSecretRing.getPrimaryUserIdWithFallback(); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { userId = null; } - /* Get key type for message */ + /* Get key type for message */ // find a master key id for our key long masterKeyId = new ProviderHelper(activity).getMasterKeyId(mSubKeyId); CachedPublicKeyRing keyRing = new ProviderHelper(activity).getCachedPublicKeyRing(masterKeyId); @@ -312,7 +313,7 @@ public class PassphraseDialogActivity extends FragmentActivity { PassphraseCacheService.addCachedPassphrase(getActivity(), mSecretRing.getMasterKeyId(), mSubKeyId, passphrase, mSecretRing.getPrimaryUserIdWithFallback()); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { Log.e(Constants.TAG, "adding of a passphrase failed", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java index b0e641778..34c08a6c7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java @@ -38,6 +38,7 @@ import android.widget.TextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.pgp.WrappedSignature; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; @@ -185,7 +186,7 @@ public class ViewCertActivity extends ActionBarActivity KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(mCertifierKeyId)).getMasterKeyId(); viewIntent.setData(KeyRings.buildGenericKeyRingUri(signerMasterKeyId)); startActivity(viewIntent); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { // TODO notify user of this, maybe offer download? Log.e(Constants.TAG, "key not found!", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java index 2d7bb07cf..7623f9a7b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java @@ -36,6 +36,7 @@ import android.widget.ListView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.KeychainContract; @@ -308,7 +309,7 @@ public class ViewKeyMainFragment extends LoaderFragment implements } // used instead of startActivity set actionbar based on callingPackage startActivityForResult(intent, 0); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } 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 43f869f02..8560bccc0 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 @@ -48,6 +48,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.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.ProviderHelper; @@ -145,7 +146,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // the catch clause doesn't return. try { userId = mSecretRing.getPrimaryUserIdWithFallback(); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { userId = null; } @@ -308,7 +309,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor PassphraseCacheService.addCachedPassphrase(getActivity(), mSecretRing.getMasterKeyId(), mSubKeyId, passphrase, mSecretRing.getPrimaryUserIdWithFallback()); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { Log.e(Constants.TAG, "adding of a passphrase failed", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java index 08599333a..e03a14989 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/EncryptKeyCompletionView.java @@ -42,6 +42,7 @@ import com.tokenautocomplete.TokenCompleteTextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.ContactHelper; import org.sufficientlysecure.keychain.pgp.KeyRing; @@ -198,7 +199,7 @@ public class EncryptKeyCompletionView extends TokenCompleteTextView { } - public EncryptionKey(CachedPublicKeyRing ring) throws PgpGeneralException { + public EncryptionKey(CachedPublicKeyRing ring) throws PgpKeyNotFoundException { this(ring.getPrimaryUserId(), ring.extractOrGetMasterKeyId(), KeyFormattingUtils.convertFingerprintToHex(ring.getFingerprint())); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java index 3c100e272..649a74641 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ExportHelper.java @@ -30,6 +30,7 @@ import android.widget.Toast; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; @@ -57,7 +58,7 @@ public class ExportHelper { DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger, new long[]{ masterKeyId }); deleteKeyDialog.show(mActivity.getSupportFragmentManager(), "deleteKeyDialog"); - } catch (PgpGeneralException e) { + } catch (PgpKeyNotFoundException e) { Log.e(Constants.TAG, "key not found!", e); } } From 7646baf486527b174e3a1103411a6c7c13cc4d09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 9 Oct 2014 01:51:35 +0200 Subject: [PATCH 4/5] Better handling if no api key is selected --- .../keychain/remote/OpenPgpService.java | 13 +++-- .../remote/ui/RemoteServiceActivity.java | 9 ++-- .../res/layout/api_remote_create_account.xml | 48 +++++++++++-------- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index eb016529d..b78ab8a37 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -410,9 +410,16 @@ public class OpenPgpService extends RemoteService { if (sign) { // Find the appropriate subkey to sign with - CachedPublicKeyRing signingRing = - new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId()); - final long sigSubKeyId = signingRing.getSecretSignId(); + long sigSubKeyId; + try { + CachedPublicKeyRing signingRing = + new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId()); + sigSubKeyId = signingRing.getSecretSignId(); + } catch (PgpKeyNotFoundException e) { + // secret key that is set for this account is deleted? + // show account config again! + return getCreateAccountIntent(data, getAccountName(data)); + } String passphrase; if (data.hasExtra(OpenPgpApi.EXTRA_PASSPHRASE)) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java index ed5e1f4cc..3e1c67dfa 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java @@ -31,7 +31,6 @@ import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.view.View; import android.widget.TextView; -import android.widget.Toast; import org.openintents.openpgp.util.OpenPgpApi; import org.sufficientlysecure.keychain.Constants; @@ -42,6 +41,7 @@ import org.sufficientlysecure.keychain.remote.AccountSettings; import org.sufficientlysecure.keychain.remote.AppSettings; import org.sufficientlysecure.keychain.ui.SelectPublicKeyFragment; import org.sufficientlysecure.keychain.ui.util.ActionBarHelper; +import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; @@ -176,10 +176,9 @@ public class RemoteServiceActivity extends ActionBarActivity { public void onClick(View v) { // Save - // user needs to select a key! - if (mAccSettingsFragment.getAccSettings().getKeyId() == Constants.key.none) { - // TODO - Toast.makeText(RemoteServiceActivity.this, getString(R.string.api_register_error_select_key), Toast.LENGTH_LONG).show(); + // user needs to select a key, but also allow None for mUpdateExistingAccount + if (mUpdateExistingAccount && mAccSettingsFragment.getAccSettings().getKeyId() == Constants.key.none) { + Notify.showNotify(RemoteServiceActivity.this, getString(R.string.api_register_error_select_key), Notify.Style.ERROR); } else { if (mUpdateExistingAccount) { Uri baseUri = KeychainContract.ApiAccounts.buildBaseUri(packageName); diff --git a/OpenKeychain/src/main/res/layout/api_remote_create_account.xml b/OpenKeychain/src/main/res/layout/api_remote_create_account.xml index 3aee9094f..a6a39b1ad 100644 --- a/OpenKeychain/src/main/res/layout/api_remote_create_account.xml +++ b/OpenKeychain/src/main/res/layout/api_remote_create_account.xml @@ -1,29 +1,37 @@ - + android:layout_height="match_parent" + android:orientation="vertical"> - + + + android:layout_height="wrap_content"> - - - + android:padding="16dp" + android:orientation="vertical"> - - + + + + + + + \ No newline at end of file From 134e1cb764656051c73bf332eabc3cdc9cedc87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 9 Oct 2014 01:52:44 +0200 Subject: [PATCH 5/5] Better handling if no api key is selected --- .../keychain/remote/ui/RemoteServiceActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java index 3e1c67dfa..d7b723eb0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RemoteServiceActivity.java @@ -176,7 +176,7 @@ public class RemoteServiceActivity extends ActionBarActivity { public void onClick(View v) { // Save - // user needs to select a key, but also allow None for mUpdateExistingAccount + // user needs to select a key if it has explicitly requested (None is only allowed for new accounts) if (mUpdateExistingAccount && mAccSettingsFragment.getAccSettings().getKeyId() == Constants.key.none) { Notify.showNotify(RemoteServiceActivity.this, getString(R.string.api_register_error_select_key), Notify.Style.ERROR); } else {