Use NotFoundException in more places

This commit is contained in:
Dominik Schürmann 2014-04-08 23:41:21 +02:00
parent d81de8509b
commit 8ab9a0a2d0
12 changed files with 140 additions and 84 deletions

View File

@ -238,19 +238,19 @@ public class PgpDecryptVerify {
PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) obj;
// get master key id for this encryption key id
long masterKeyId = 0;
PGPSecretKeyRing secretKeyRing = null;
try {
// get master key id for this encryption key id
masterKeyId = ProviderHelper.getMasterKeyId(mContext,
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(encData.getKeyID()))
);
// get actual keyring object based on master key id
secretKeyRing = ProviderHelper.getPGPSecretKeyRing(mContext, masterKeyId);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
// continue with the next packet in the while loop
continue;
}
// get actual keyring object based on master key id
PGPSecretKeyRing secretKeyRing = ProviderHelper.getPGPSecretKeyRing(mContext, masterKeyId);
if (secretKeyRing == null) {
// continue with the next packet in the while loop
continue;
@ -390,8 +390,14 @@ public class PgpDecryptVerify {
PGPOnePassSignatureList sigList = (PGPOnePassSignatureList) dataChunk;
for (int i = 0; i < sigList.size(); ++i) {
signature = sigList.get(i);
signatureKey = ProviderHelper
.getPGPPublicKeyRingWithKeyId(mContext, signature.getKeyID()).getPublicKey();
// TODO: rework this code, seems wonky!
try {
signatureKey = ProviderHelper
.getPGPPublicKeyRingWithKeyId(mContext, signature.getKeyID()).getPublicKey();
} catch (ProviderHelper.NotFoundException e) {
Log.d(Constants.TAG, "key not found!");
}
if (signatureKeyId == 0) {
signatureKeyId = signature.getKeyID();
}
@ -401,10 +407,12 @@ public class PgpDecryptVerify {
signatureIndex = i;
signatureKeyId = signature.getKeyID();
String userId = null;
PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(
mContext, signatureKeyId);
if (signKeyRing != null) {
try {
PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(
mContext, signatureKeyId);
userId = PgpKeyHelper.getMainUserId(signKeyRing.getPublicKey());
} catch (ProviderHelper.NotFoundException e) {
Log.d(Constants.TAG, "key not found!");
}
signatureResult.setUserId(userId);
break;
@ -598,7 +606,11 @@ public class PgpDecryptVerify {
}
// this one can't fail now (yay database constraints)
signatureKey = ProviderHelper.getPGPPublicKeyRing(mContext, (Long) data.get(KeyRings.MASTER_KEY_ID)).getPublicKey();
try {
signatureKey = ProviderHelper.getPGPPublicKeyRing(mContext, (Long) data.get(KeyRings.MASTER_KEY_ID)).getPublicKey();
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
signatureResult.setUserId((String) data.get(KeyRings.USER_ID));
break;
@ -664,11 +676,13 @@ public class PgpDecryptVerify {
long signatureKeyId = signature.getKeyID();
boolean validKeyBinding = false;
PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(context,
signatureKeyId);
PGPPublicKey mKey = null;
if (signKeyRing != null) {
try {
PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(context,
signatureKeyId);
mKey = signKeyRing.getPublicKey();
} catch (ProviderHelper.NotFoundException e) {
Log.d(Constants.TAG, "key not found");
}
if (signature.getKeyID() != mKey.getKeyID()) {

View File

@ -194,11 +194,14 @@ public class PgpImportExport {
arOutStream.setHeader("Version", PgpHelper.getFullVersion(mContext));
updateProgress(progress * 100 / masterKeyIdsSize, 100);
PGPPublicKeyRing publicKeyRing =
ProviderHelper.getPGPPublicKeyRing(mContext, pubKeyMasterId);
if (publicKeyRing != null) {
try {
PGPPublicKeyRing publicKeyRing = ProviderHelper.getPGPPublicKeyRing(mContext, pubKeyMasterId);
publicKeyRing.encode(arOutStream);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
// TODO: inform user?
}
if (mKeychainServiceListener.hasServiceStopped()) {
@ -217,12 +220,15 @@ public class PgpImportExport {
arOutStream.setHeader("Version", PgpHelper.getFullVersion(mContext));
updateProgress(progress * 100 / masterKeyIdsSize, 100);
PGPSecretKeyRing secretKeyRing =
ProviderHelper.getPGPSecretKeyRing(mContext, secretKeyMasterId);
if (secretKeyRing != null) {
try {
PGPSecretKeyRing secretKeyRing = ProviderHelper.getPGPSecretKeyRing(mContext, secretKeyMasterId);
secretKeyRing.encode(arOutStream);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
// TODO: inform user?
}
if (mKeychainServiceListener.hasServiceStopped()) {
arOutStream.close();
return null;

View File

@ -201,9 +201,12 @@ public class PgpKeyHelper {
}
public static PGPPublicKey getEncryptPublicKey(Context context, long masterKeyId) {
PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRing(context, masterKeyId);
if (keyRing == null) {
Log.e(Constants.TAG, "keyRing is null!");
PGPPublicKeyRing keyRing = null;
try {
keyRing = ProviderHelper.getPGPPublicKeyRing(context, masterKeyId);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
// TODO: throw exception here!
return null;
}
Vector<PGPPublicKey> encryptKeys = getUsableEncryptKeys(keyRing);
@ -215,8 +218,12 @@ public class PgpKeyHelper {
}
public static PGPSecretKey getCertificationKey(Context context, long masterKeyId) {
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRing(context, masterKeyId);
if (keyRing == null) {
PGPSecretKeyRing keyRing = null;
try {
keyRing = ProviderHelper.getPGPSecretKeyRing(context, masterKeyId);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
// TODO: throw exception here!
return null;
}
Vector<PGPSecretKey> signingKeys = getUsableCertificationKeys(keyRing);
@ -227,8 +234,12 @@ public class PgpKeyHelper {
}
public static PGPSecretKey getSigningKey(Context context, long masterKeyId) {
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRing(context, masterKeyId);
if (keyRing == null) {
PGPSecretKeyRing keyRing = null;
try {
keyRing = ProviderHelper.getPGPSecretKeyRing(context, masterKeyId);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
// TODO: throw exception here!
return null;
}
Vector<PGPSecretKey> signingKeys = getUsableSigningKeys(keyRing);

View File

@ -235,7 +235,11 @@ public class PgpSignEncrypt {
PGPSecretKeyRing signingKeyRing = null;
PGPPrivateKey signaturePrivateKey = null;
if (enableSignature) {
signingKeyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, mSignatureKeyId);
try {
signingKeyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, mSignatureKeyId);
} catch (ProviderHelper.NotFoundException e) {
throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed));
}
signingKey = PgpKeyHelper.getSigningKey(mContext, mSignatureKeyId);
if (signingKey == null) {
throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed));
@ -464,8 +468,12 @@ public class PgpSignEncrypt {
throw new PgpGeneralException(mContext.getString(R.string.error_no_signature_key));
}
PGPSecretKeyRing signingKeyRing =
ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, mSignatureKeyId);
PGPSecretKeyRing signingKeyRing;
try {
signingKeyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, mSignatureKeyId);
} catch (ProviderHelper.NotFoundException e) {
throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed));
}
PGPSecretKey signingKey = PgpKeyHelper.getSigningKey(mContext, mSignatureKeyId);
if (signingKey == null) {
throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed));

View File

@ -159,39 +159,35 @@ public class ProviderHelper {
return result;
}
public static PGPKeyRing getPGPKeyRing(Context context, Uri queryUri) {
public static PGPKeyRing getPGPKeyRing(Context context, Uri queryUri) throws NotFoundException {
Map<Long, PGPKeyRing> result = getPGPKeyRings(context, queryUri);
if(result.isEmpty())
return null;
return result.values().iterator().next();
if(result.isEmpty()) {
throw new NotFoundException("PGPKeyRing object not found!");
} else {
return result.values().iterator().next();
}
}
public static PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(Context context, long keyId) {
public static PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(Context context, long keyId)
throws NotFoundException {
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
long masterKeyId;
try {
masterKeyId = getMasterKeyId(context, uri);
return getPGPPublicKeyRing(context, masterKeyId);
} catch (NotFoundException e) {
return null;
}
long masterKeyId = getMasterKeyId(context, uri);
return getPGPPublicKeyRing(context, masterKeyId);
}
public static PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(Context context, long keyId) {
public static PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(Context context, long keyId)
throws NotFoundException {
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
long masterKeyId;
try {
masterKeyId = getMasterKeyId(context, uri);
return getPGPSecretKeyRing(context, masterKeyId);
} catch (NotFoundException notFound) {
return null;
}
long masterKeyId = getMasterKeyId(context, uri);
return getPGPSecretKeyRing(context, masterKeyId);
}
/**
* Retrieves the actual PGPPublicKeyRing object from the database blob based on the masterKeyId
*/
public static PGPPublicKeyRing getPGPPublicKeyRing(Context context,
long masterKeyId) {
long masterKeyId) throws NotFoundException {
Uri queryUri = KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId));
return (PGPPublicKeyRing) getPGPKeyRing(context, queryUri);
}
@ -200,7 +196,7 @@ public class ProviderHelper {
* Retrieves the actual PGPSecretKeyRing object from the database blob based on the maserKeyId
*/
public static PGPSecretKeyRing getPGPSecretKeyRing(Context context,
long masterKeyId) {
long masterKeyId) throws NotFoundException {
Uri queryUri = KeyRingData.buildSecretKeyRingUri(Long.toString(masterKeyId));
return (PGPSecretKeyRing) getPGPKeyRing(context, queryUri);
}
@ -214,7 +210,12 @@ public class ProviderHelper {
long masterKeyId = masterKey.getKeyID();
// IF there is a secret key, preserve it!
PGPSecretKeyRing secretRing = ProviderHelper.getPGPSecretKeyRing(context, masterKeyId);
PGPSecretKeyRing secretRing = null;
try {
secretRing = ProviderHelper.getPGPSecretKeyRing(context, masterKeyId);
} catch (NotFoundException e) {
Log.e(Constants.TAG, "key not found!");
}
// delete old version of this keyRing, which also deletes all keys and userIds on cascade
try {

View File

@ -33,7 +33,6 @@ import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v4.util.LongSparseArray;
import android.util.Log;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPPrivateKey;
@ -48,6 +47,7 @@ import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.util.Log;
import java.util.Date;
import java.util.Iterator;
@ -231,6 +231,8 @@ public class PassphraseCacheService extends Service {
}
} catch (PGPException e) {
// silently catch
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
return true;

View File

@ -224,8 +224,9 @@ public class CertifyKeyActivity extends ActionBarActivity implements
* handles the UI bits of the signing process on the UI thread
*/
private void initiateSigning() {
PGPPublicKeyRing pubring = ProviderHelper.getPGPPublicKeyRing(this, mPubKeyId);
if (pubring != null) {
try {
PGPPublicKeyRing pubring = ProviderHelper.getPGPPublicKeyRing(this, mPubKeyId);
// if we have already signed this key, dont bother doing it again
boolean alreadySigned = false;
@ -248,14 +249,14 @@ public class CertifyKeyActivity extends ActionBarActivity implements
String passphrase = PassphraseCacheService.getCachedPassphrase(this, mMasterKeyId);
if (passphrase == null) {
PassphraseDialogFragment.show(this, mMasterKeyId,
new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) {
startSigning();
new Handler() {
@Override
public void handleMessage(Message message) {
if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) {
startSigning();
}
}
}
});
});
// bail out; need to wait until the user has entered the passphrase before trying again
return;
} else {
@ -268,6 +269,8 @@ public class CertifyKeyActivity extends ActionBarActivity implements
setResult(RESULT_CANCELED);
finish();
}
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}

View File

@ -300,15 +300,16 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
private void finallyEdit(final long masterKeyId) {
if (masterKeyId != 0) {
PGPSecretKey masterKey = null;
mKeyRing = ProviderHelper.getPGPSecretKeyRing(this, masterKeyId);
if (mKeyRing != null) {
try {
mKeyRing = ProviderHelper.getPGPSecretKeyRing(this, masterKeyId);
masterKey = mKeyRing.getSecretKey();
mMasterCanSign = PgpKeyHelper.isCertificationKey(mKeyRing.getSecretKey());
for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(mKeyRing.getSecretKeys())) {
mKeys.add(key);
mKeysUsages.add(-1); // get usage when view is created
}
} else {
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "Keyring not found with masterKeyId: " + masterKeyId);
AppMsg.makeText(this, R.string.error_no_secret_key_found, AppMsg.STYLE_ALERT).show();
// TODO

View File

@ -146,17 +146,19 @@ public class EncryptAsymmetricFragment extends Fragment {
private void preselectKeys(long preselectedSignatureKeyId, long[] preselectedEncryptionKeyIds) {
if (preselectedSignatureKeyId != 0) {
// TODO: don't use bouncy castle objects!
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(getActivity(),
preselectedSignatureKeyId);
PGPSecretKey masterKey;
if (keyRing != null) {
masterKey = keyRing.getSecretKey();
try {
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(getActivity(),
preselectedSignatureKeyId);
PGPSecretKey masterKey = keyRing.getSecretKey();
if (masterKey != null) {
Vector<PGPSecretKey> signKeys = PgpKeyHelper.getUsableSigningKeys(keyRing);
if (signKeys.size() > 0) {
setSignatureKeyId(masterKey.getKeyID());
}
}
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}

View File

@ -145,14 +145,14 @@ public class ViewCertActivity extends ActionBarActivity
}
PGPSignature sig = PgpConversionHelper.BytesToPGPSignature(data.getBlob(INDEX_DATA));
PGPKeyRing signeeRing = ProviderHelper.getPGPKeyRing(this,
KeychainContract.KeyRingData.buildPublicKeyRingUri(
Long.toString(data.getLong(INDEX_MASTER_KEY_ID))));
PGPKeyRing signerRing = ProviderHelper.getPGPKeyRing(this,
KeychainContract.KeyRingData.buildPublicKeyRingUri(
Long.toString(sig.getKeyID())));
try {
PGPKeyRing signeeRing = ProviderHelper.getPGPKeyRing(this,
KeychainContract.KeyRingData.buildPublicKeyRingUri(
Long.toString(data.getLong(INDEX_MASTER_KEY_ID))));
PGPKeyRing signerRing = ProviderHelper.getPGPKeyRing(this,
KeychainContract.KeyRingData.buildPublicKeyRingUri(
Long.toString(sig.getKeyID())));
if (signerRing != null) {
try {
sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider(
Constants.BOUNCY_CASTLE_PROVIDER_NAME), signeeRing.getPublicKey());
@ -170,7 +170,7 @@ public class ViewCertActivity extends ActionBarActivity
mStatus.setText("error!");
mStatus.setTextColor(getResources().getColor(R.color.alert));
}
} else {
} catch (ProviderHelper.NotFoundException e) {
mStatus.setText("key unavailable");
mStatus.setTextColor(getResources().getColor(R.color.black));
}

View File

@ -93,6 +93,9 @@ public class ViewKeyActivityJB extends ViewKeyActivity implements CreateNdefMess
} catch(IOException e) {
Log.e(Constants.TAG, "Error parsing keyring", e);
return null;
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
return null;
}
}

View File

@ -139,9 +139,9 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
secretKey = null;
alert.setMessage(R.string.passphrase_for_symmetric_encryption);
} else {
secretKey = ProviderHelper.getPGPSecretKeyRing(activity, secretKeyId).getSecretKey();
if (secretKey == null) {
try {
secretKey = ProviderHelper.getPGPSecretKeyRing(activity, secretKeyId).getSecretKey();
} catch (ProviderHelper.NotFoundException e) {
alert.setTitle(R.string.title_key_not_found);
alert.setMessage(getString(R.string.key_not_found, secretKeyId));
alert.setPositiveButton(android.R.string.ok, new OnClickListener() {
@ -153,6 +153,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
mCanKB = false;
return alert.create();
}
String userId = PgpKeyHelper.getMainUserIdSafe(activity, secretKey);
Log.d(Constants.TAG, "User id: '" + userId + "'");
@ -194,9 +195,13 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
sendMessageToHandler(MESSAGE_CANCEL);
return;
} else {
clickSecretKey = PgpKeyHelper.getKeyNum(ProviderHelper
.getPGPSecretKeyRingWithKeyId(activity, secretKeyId),
curKeyIndex);
try {
clickSecretKey = PgpKeyHelper.getKeyNum(ProviderHelper
.getPGPSecretKeyRingWithKeyId(activity, secretKeyId),
curKeyIndex);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
curKeyIndex++; // does post-increment work like C?
continue;
}