Refactor ProviderHelper to be non-static using a constructor based on context (first commit to get context out of pgp classes)

This commit is contained in:
Dominik Schürmann
2014-04-11 17:45:52 +02:00
parent bbd97cf800
commit 094fb698de
23 changed files with 266 additions and 228 deletions

View File

@@ -79,6 +79,7 @@ import java.util.Set;
*/
public class PgpDecryptVerify {
private Context mContext;
private ProviderHelper mProviderHelper;
private InputData mData;
private OutputStream mOutStream;
@@ -90,6 +91,7 @@ public class PgpDecryptVerify {
private PgpDecryptVerify(Builder builder) {
// private Constructor can only be called from Builder
this.mContext = builder.mContext;
this.mProviderHelper = new ProviderHelper(mContext);
this.mData = builder.mData;
this.mOutStream = builder.mOutStream;
@@ -243,11 +245,11 @@ public class PgpDecryptVerify {
PGPSecretKeyRing secretKeyRing = null;
try {
// get master key id for this encryption key id
masterKeyId = ProviderHelper.getMasterKeyId(mContext,
masterKeyId = mProviderHelper.getMasterKeyId(
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(encData.getKeyID()))
);
// get actual keyring object based on master key id
secretKeyRing = ProviderHelper.getPGPSecretKeyRing(mContext, masterKeyId);
secretKeyRing = mProviderHelper.getPGPSecretKeyRing(masterKeyId);
} catch (ProviderHelper.NotFoundException e) {
// continue with the next packet in the while loop
continue;
@@ -393,17 +395,17 @@ public class PgpDecryptVerify {
try {
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(
Long.toString(sigList.get(i).getKeyID()));
masterKeyId = ProviderHelper.getMasterKeyId(mContext, uri);
masterKeyId = mProviderHelper.getMasterKeyId(uri);
signatureIndex = i;
} catch (ProviderHelper.NotFoundException e) {
Log.d(Constants.TAG, "key not found!");
}
}
if(masterKeyId == null) {
if (masterKeyId == null) {
try {
signatureKey = ProviderHelper
.getPGPPublicKeyRing(mContext, masterKeyId).getPublicKey();
signatureKey = mProviderHelper
.getPGPPublicKeyRing(masterKeyId).getPublicKey();
} catch (ProviderHelper.NotFoundException e) {
// can't happen
}
@@ -417,7 +419,7 @@ public class PgpDecryptVerify {
signature.init(contentVerifierBuilderProvider, signatureKey);
} else {
if(!sigList.isEmpty()) {
if (!sigList.isEmpty()) {
signatureResult.setKeyId(sigList.get(0).getKeyID());
}
@@ -489,7 +491,7 @@ public class PgpDecryptVerify {
signatureResult.setSignatureOnly(false);
//Now check binding signatures
boolean validKeyBinding = verifyKeyBinding(mContext, messageSignature, signatureKey);
boolean validKeyBinding = verifyKeyBinding(messageSignature, signatureKey);
boolean validSignature = signature.verify(messageSignature);
// TODO: implement CERTIFIED!
@@ -587,7 +589,7 @@ public class PgpDecryptVerify {
signatureKeyId = signature.getKeyID();
// find data about this subkey
HashMap<String, Object> data = ProviderHelper.getGenericData(mContext,
HashMap<String, Object> data = mProviderHelper.getGenericData(
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(signature.getKeyID())),
new String[]{KeyRings.MASTER_KEY_ID, KeyRings.USER_ID},
new int[]{ProviderHelper.FIELD_TYPE_INTEGER, ProviderHelper.FIELD_TYPE_STRING});
@@ -600,7 +602,7 @@ public class PgpDecryptVerify {
// this one can't fail now (yay database constraints)
try {
signatureKey = ProviderHelper.getPGPPublicKeyRing(mContext, (Long) data.get(KeyRings.MASTER_KEY_ID)).getPublicKey();
signatureKey = mProviderHelper.getPGPPublicKeyRing((Long) data.get(KeyRings.MASTER_KEY_ID)).getPublicKey();
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
@@ -644,7 +646,7 @@ public class PgpDecryptVerify {
}
//Now check binding signatures
boolean validKeyBinding = verifyKeyBinding(mContext, signature, signatureKey);
boolean validKeyBinding = verifyKeyBinding(signature, signatureKey);
boolean validSignature = signature.verify();
if (validKeyBinding && validSignature) {
@@ -664,14 +666,13 @@ public class PgpDecryptVerify {
return result;
}
private static boolean verifyKeyBinding(Context context,
PGPSignature signature, PGPPublicKey signatureKey) {
private boolean verifyKeyBinding(PGPSignature signature, PGPPublicKey signatureKey) {
long signatureKeyId = signature.getKeyID();
boolean validKeyBinding = false;
PGPPublicKey mKey = null;
try {
PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(context,
PGPPublicKeyRing signKeyRing = mProviderHelper.getPGPPublicKeyRingWithKeyId(
signatureKeyId);
mKey = signKeyRing.getPublicKey();
} catch (ProviderHelper.NotFoundException e) {
@@ -686,7 +687,7 @@ public class PgpDecryptVerify {
return validKeyBinding;
}
private static boolean verifyKeyBinding(PGPPublicKey masterPublicKey, PGPPublicKey signingPublicKey) {
private boolean verifyKeyBinding(PGPPublicKey masterPublicKey, PGPPublicKey signingPublicKey) {
boolean validSubkeyBinding = false;
boolean validTempSubkeyBinding = false;
boolean validPrimaryKeyBinding = false;
@@ -734,9 +735,9 @@ public class PgpDecryptVerify {
return (validSubkeyBinding & validPrimaryKeyBinding);
}
private static boolean verifyPrimaryKeyBinding(PGPSignatureSubpacketVector pkts,
PGPPublicKey masterPublicKey,
PGPPublicKey signingPublicKey) {
private boolean verifyPrimaryKeyBinding(PGPSignatureSubpacketVector pkts,
PGPPublicKey masterPublicKey,
PGPPublicKey signingPublicKey) {
boolean validPrimaryKeyBinding = false;
JcaPGPContentVerifierBuilderProvider contentVerifierBuilderProvider =
new JcaPGPContentVerifierBuilderProvider()

View File

@@ -57,10 +57,13 @@ public class PgpImportExport {
private KeychainServiceListener mKeychainServiceListener;
private ProviderHelper mProviderHelper;
public PgpImportExport(Context context, ProgressDialogUpdater progress) {
super();
this.mContext = context;
this.mProgress = progress;
this.mProviderHelper = new ProviderHelper(context);
}
public PgpImportExport(Context context,
@@ -68,6 +71,7 @@ public class PgpImportExport {
super();
this.mContext = context;
this.mProgress = progress;
this.mProviderHelper = new ProviderHelper(context);
this.mKeychainServiceListener = keychainListener;
}
@@ -196,7 +200,7 @@ public class PgpImportExport {
updateProgress(progress * 100 / masterKeyIdsSize, 100);
try {
PGPPublicKeyRing publicKeyRing = ProviderHelper.getPGPPublicKeyRing(mContext, pubKeyMasterId);
PGPPublicKeyRing publicKeyRing = mProviderHelper.getPGPPublicKeyRing(pubKeyMasterId);
publicKeyRing.encode(arOutStream);
} catch (ProviderHelper.NotFoundException e) {
@@ -222,7 +226,7 @@ public class PgpImportExport {
updateProgress(progress * 100 / masterKeyIdsSize, 100);
try {
PGPSecretKeyRing secretKeyRing = ProviderHelper.getPGPSecretKeyRing(mContext, secretKeyMasterId);
PGPSecretKeyRing secretKeyRing = mProviderHelper.getPGPSecretKeyRing(secretKeyMasterId);
secretKeyRing.encode(arOutStream);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
@@ -279,15 +283,15 @@ public class PgpImportExport {
newPubRing = PGPPublicKeyRing.insertPublicKey(newPubRing, key);
}
if (newPubRing != null) {
ProviderHelper.saveKeyRing(mContext, newPubRing);
mProviderHelper.saveKeyRing(newPubRing);
}
ProviderHelper.saveKeyRing(mContext, secretKeyRing);
mProviderHelper.saveKeyRing(secretKeyRing);
// TODO: remove status returns, use exceptions!
status = Id.return_value.ok;
}
} else if (keyring instanceof PGPPublicKeyRing) {
PGPPublicKeyRing publicKeyRing = (PGPPublicKeyRing) keyring;
ProviderHelper.saveKeyRing(mContext, publicKeyRing);
mProviderHelper.saveKeyRing(publicKeyRing);
// TODO: remove status returns, use exceptions!
status = Id.return_value.ok;
}

View File

@@ -67,6 +67,7 @@ import java.util.Date;
*/
public class PgpSignEncrypt {
private Context mContext;
private ProviderHelper mProviderHelper;
private InputData mData;
private OutputStream mOutStream;
@@ -85,6 +86,7 @@ public class PgpSignEncrypt {
private PgpSignEncrypt(Builder builder) {
// private Constructor can only be called from Builder
this.mContext = builder.mContext;
this.mProviderHelper = new ProviderHelper(mContext);
this.mData = builder.mData;
this.mOutStream = builder.mOutStream;
@@ -252,7 +254,7 @@ public class PgpSignEncrypt {
PGPPrivateKey signaturePrivateKey = null;
if (enableSignature) {
try {
signingKeyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, mSignatureMasterKeyId);
signingKeyRing = mProviderHelper.getPGPSecretKeyRingWithKeyId(mSignatureMasterKeyId);
} catch (ProviderHelper.NotFoundException e) {
throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed));
}
@@ -300,7 +302,7 @@ public class PgpSignEncrypt {
// Asymmetric encryption
for (long id : mEncryptionMasterKeyIds) {
try {
PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRing(mContext, id);
PGPPublicKeyRing keyRing = mProviderHelper.getPGPPublicKeyRing(id);
PGPPublicKey key = PgpKeyHelper.getEncryptPublicKey(keyRing);
if (key != null) {
JcePublicKeyKeyEncryptionMethodGenerator pubKeyEncryptionGenerator =
@@ -491,7 +493,7 @@ public class PgpSignEncrypt {
PGPSecretKeyRing signingKeyRing;
try {
signingKeyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, mSignatureMasterKeyId);
signingKeyRing = mProviderHelper.getPGPSecretKeyRingWithKeyId(mSignatureMasterKeyId);
} catch (ProviderHelper.NotFoundException e) {
throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed));
}