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: Replace PgpGeneralException with NotFoundException where appropriate --- .../keychain/pgp/CanonicalizedKeyRing.java | 16 ++++++++-------- .../keychain/pgp/CanonicalizedPublicKeyRing.java | 10 +++++----- .../keychain/pgp/CanonicalizedSecretKey.java | 5 ++++- .../org/sufficientlysecure/keychain/pgp/KeyRing.java | 20 ++++++++++---------- .../keychain/pgp/OpenPgpSignatureResultBuilder.java | 3 ++- .../keychain/pgp/PgpSignEncrypt.java | 13 ++++++++----- 6 files changed, 37 insertions(+), 30 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp') 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)); } } -- cgit v1.2.3