From bd6aeea6db7d5af5b3751453db95b6c968d54100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 11 Apr 2014 19:14:39 +0200 Subject: PgpSignEncrypt is now context-free --- .../sufficientlysecure/keychain/pgp/PgpHelper.java | 5 ++- .../keychain/pgp/PgpSignEncrypt.java | 52 +++++++++++++++------- .../keychain/remote/OpenPgpService.java | 11 ++++- .../keychain/service/KeychainIntentService.java | 5 ++- 4 files changed, 51 insertions(+), 22 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java index 2b518c7e6..ec2860593 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java @@ -27,6 +27,7 @@ import org.spongycastle.openpgp.PGPObjectFactory; import org.spongycastle.openpgp.PGPPublicKeyRing; import org.spongycastle.openpgp.PGPSecretKeyRing; import org.spongycastle.openpgp.PGPUtil; +import org.sufficientlysecure.keychain.BuildConfig; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Id; import org.sufficientlysecure.keychain.R; @@ -55,14 +56,14 @@ public class PgpHelper { Pattern.DOTALL); public static String getVersion(Context context) { - String version = null; + String version; try { PackageInfo pi = context.getPackageManager().getPackageInfo(Constants.PACKAGE_NAME, 0); version = pi.versionName; return version; } catch (NameNotFoundException e) { Log.e(Constants.TAG, "Version could not be retrieved!", e); - return "0.0.0"; + return "0.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 aa3e2e6bd..589b06274 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java @@ -18,8 +18,6 @@ package org.sufficientlysecure.keychain.pgp; -import android.content.Context; - import org.spongycastle.bcpg.ArmoredOutputStream; import org.spongycastle.bcpg.BCPGOutputStream; import org.spongycastle.openpgp.PGPCompressedDataGenerator; @@ -67,8 +65,8 @@ import java.util.Date; * This class uses a Builder pattern! */ public class PgpSignEncrypt { - private Context mContext; private ProviderHelper mProviderHelper; + private String mVersionHeader; private InputData mData; private OutputStream mOutStream; @@ -95,11 +93,10 @@ 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.mProviderHelper = builder.mProviderHelper; + this.mVersionHeader = builder.mVersionHeader; this.mData = builder.mData; this.mOutStream = builder.mOutStream; @@ -119,7 +116,8 @@ public class PgpSignEncrypt { public static class Builder { // mandatory parameter - private Context mContext; + private ProviderHelper mProviderHelper; + private String mVersionHeader; private InputData mData; private OutputStream mOutStream; @@ -137,8 +135,9 @@ public class PgpSignEncrypt { private boolean mEncryptToSigner = false; private boolean mBinaryInput = false; - public Builder(Context context, InputData data, OutputStream outStream) { - this.mContext = context; + public Builder(ProviderHelper providerHelper, String versionHeader, InputData data, OutputStream outStream) { + this.mProviderHelper = providerHelper; + this.mVersionHeader = versionHeader; this.mData = data; this.mOutStream = outStream; } @@ -232,6 +231,21 @@ public class PgpSignEncrypt { } } + public static class KeyExtractionException extends Exception { + public KeyExtractionException() { + } + } + + public static class NoPassphraseException extends Exception { + public NoPassphraseException() { + } + } + + public static class NoSigningKeyException extends Exception { + public NoSigningKeyException() { + } + } + /** * Signs and/or encrypts data based on parameters of class * @@ -244,7 +258,7 @@ public class PgpSignEncrypt { */ public void execute() throws IOException, PgpGeneralException, PGPException, NoSuchProviderException, - NoSuchAlgorithmException, SignatureException { + NoSuchAlgorithmException, SignatureException, KeyExtractionException, NoSigningKeyException, NoPassphraseException { boolean enableSignature = mSignatureMasterKeyId != Id.key.none; boolean enableEncryption = ((mEncryptionMasterKeyIds != null && mEncryptionMasterKeyIds.length > 0) @@ -274,7 +288,7 @@ public class PgpSignEncrypt { OutputStream out; if (mEnableAsciiArmorOutput) { armorOut = new ArmoredOutputStream(mOutStream); - armorOut.setHeader("Version", PgpHelper.getFullVersion(mContext)); + armorOut.setHeader("Version", mVersionHeader); out = armorOut; } else { out = mOutStream; @@ -288,16 +302,19 @@ public class PgpSignEncrypt { try { signingKeyRing = mProviderHelper.getPGPSecretKeyRing(mSignatureMasterKeyId); } catch (ProviderHelper.NotFoundException e) { - throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed)); + throw new NoSigningKeyException(); +// throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed)); } signingKey = PgpKeyHelper.getSigningKey(signingKeyRing); if (signingKey == null) { - throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed)); + throw new NoSigningKeyException(); +// throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed)); } if (mSignaturePassphrase == null) { - throw new PgpGeneralException( - mContext.getString(R.string.error_no_signature_passphrase)); +// throw new PgpGeneralException( +// mContext.getString(R.string.error_no_signature_passphrase)); + throw new NoPassphraseException(); } updateProgress(R.string.progress_extracting_signature_key, 0, 100); @@ -306,8 +323,9 @@ public class PgpSignEncrypt { Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(mSignaturePassphrase.toCharArray()); signaturePrivateKey = signingKey.extractPrivateKey(keyDecryptor); if (signaturePrivateKey == null) { - throw new PgpGeneralException( - mContext.getString(R.string.error_could_not_extract_private_key)); +// throw new PgpGeneralException( +// mContext.getString(R.string.error_could_not_extract_private_key)); + throw new KeyExtractionException(); } } updateProgress(R.string.progress_preparing_streams, 5, 100); 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 48cc8502f..91df79104 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -33,6 +33,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Id; import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify; import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyResult; +import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; @@ -165,7 +166,10 @@ public class OpenPgpService extends RemoteService { InputData inputData = new InputData(is, inputLength); // sign-only - PgpSignEncrypt.Builder builder = new PgpSignEncrypt.Builder(getContext(), inputData, os); + PgpSignEncrypt.Builder builder = new PgpSignEncrypt.Builder( + new ProviderHelper(getContext()), + PgpHelper.getFullVersion(getContext()), + inputData, os); builder.enableAsciiArmorOutput(asciiArmor) .signatureHashAlgorithm(accSettings.getHashAlgorithm()) .signatureForceV3(false) @@ -231,7 +235,10 @@ public class OpenPgpService extends RemoteService { long inputLength = is.available(); InputData inputData = new InputData(is, inputLength); - PgpSignEncrypt.Builder builder = new PgpSignEncrypt.Builder(getContext(), inputData, os); + PgpSignEncrypt.Builder builder = new PgpSignEncrypt.Builder( + new ProviderHelper(getContext()), + PgpHelper.getFullVersion(getContext()), + inputData, os); builder.enableAsciiArmorOutput(asciiArmor) .compressionId(accSettings.getCompression()) .symmetricEncryptionAlgorithm(accSettings.getEncryptionAlgorithm()) 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 c0d9321a4..38664f996 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -311,7 +311,10 @@ public class KeychainIntentService extends IntentService /* Operation */ PgpSignEncrypt.Builder builder = - new PgpSignEncrypt.Builder(this, inputData, outStream); + new PgpSignEncrypt.Builder( + new ProviderHelper(this), + PgpHelper.getFullVersion(this), + inputData, outStream); builder.progress(this); builder.enableAsciiArmorOutput(useAsciiArmor) -- cgit v1.2.3