Replace many PgpGeneralExceptions with PgpKeyNotFoundException

This commit is contained in:
Dominik Schürmann
2014-10-09 01:37:44 +02:00
parent b3f56c927b
commit 45b02008fb
20 changed files with 126 additions and 70 deletions

View File

@@ -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;
}
}

View File

@@ -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<CanonicalizedPublicKey> publicKeyIterator() {

View File

@@ -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);
}
}

View File

@@ -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("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$");

View File

@@ -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);

View File

@@ -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) {

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2010-2014 Thialfihar <thi@thialfihar.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}
}