From a5e33097a6cb3d8240ea475bfd32f0dbda02b4a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 19 Feb 2014 10:47:13 +0100 Subject: [PATCH] cleanup --- .../keychain/pgp/PgpOperationOutgoing.java | 22 +++++++++---------- .../service/remote/OpenPgpService.java | 14 ++++++------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpOperationOutgoing.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpOperationOutgoing.java index 346638fa7..f86d83547 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpOperationOutgoing.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpOperationOutgoing.java @@ -191,13 +191,9 @@ public class PgpOperationOutgoing { throws IOException, PgpGeneralException, PGPException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException { - if (encryptionKeyIds == null) { - encryptionKeyIds = new long[0]; - } - boolean enableSignature = signatureKeyId != Id.key.none; - boolean enableCompression = compressionId != Id.choice.compression.none; - boolean enableEncryption = encryptionKeyIds.length != 0 || encryptionPassphrase != null; + boolean enableEncryption = (encryptionKeyIds.length != 0 || encryptionPassphrase != null); + boolean enableCompression = (enableEncryption && compressionId != Id.choice.compression.none); int signatureType; if (enableAsciiArmorOutput && enableSignature && !enableEncryption && !enableCompression) { @@ -208,7 +204,6 @@ public class PgpOperationOutgoing { ArmoredOutputStream armorOut = null; OutputStream out; - OutputStream encryptionOut = null; if (enableAsciiArmorOutput) { armorOut = new ArmoredOutputStream(outStream); armorOut.setHeader("Version", PgpHelper.getFullVersion(context)); @@ -217,7 +212,7 @@ public class PgpOperationOutgoing { out = outStream; } - + /* Get keys for signature generation for later usage */ PGPSecretKey signingKey = null; PGPSecretKeyRing signingKeyRing = null; PGPPrivateKey signaturePrivateKey = null; @@ -245,7 +240,8 @@ public class PgpOperationOutgoing { } updateProgress(R.string.progress_preparing_streams, 5, 100); - // encrypt and compress input file content + /* Initialize PGPEncryptedDataGenerator for later usage */ + PGPEncryptedDataGenerator cPk = null; if (enableEncryption) { // has Integrity packet enabled! JcePGPDataEncryptorBuilder encryptorBuilder = @@ -253,7 +249,7 @@ public class PgpOperationOutgoing { .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME) .setWithIntegrityPacket(true); - PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(encryptorBuilder); + cPk = new PGPEncryptedDataGenerator(encryptorBuilder); if (encryptionKeyIds.length == 0) { // Symmetric encryption @@ -267,16 +263,15 @@ public class PgpOperationOutgoing { for (long id : encryptionKeyIds) { PGPPublicKey key = PgpKeyHelper.getEncryptPublicKey(context, id); if (key != null) { - JcePublicKeyKeyEncryptionMethodGenerator pubKeyEncryptionGenerator = new JcePublicKeyKeyEncryptionMethodGenerator(key); cPk.addMethod(pubKeyEncryptionGenerator); } } } - encryptionOut = cPk.open(out, new byte[1 << 16]); } + /* Initialize signature generator object for later usage */ PGPSignatureGenerator signatureGenerator = null; PGPV3SignatureGenerator signatureV3Generator = null; if (enableSignature) { @@ -303,7 +298,10 @@ public class PgpOperationOutgoing { PGPCompressedDataGenerator compressGen = null; OutputStream pOut; + OutputStream encryptionOut = null; if (enableEncryption) { + encryptionOut = cPk.open(out, new byte[1 << 16]); + BCPGOutputStream bcpgOut; if (enableCompression) { compressGen = new PGPCompressedDataGenerator(compressionId); diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java index da42be2e2..ec3f40d23 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java @@ -136,11 +136,11 @@ public class OpenPgpService extends RemoteService { return result; } - - // TODO: asciiArmor?! private Bundle signImpl(Bundle params, ParcelFileDescriptor input, ParcelFileDescriptor output, AppSettings appSettings) { try { + boolean asciiArmor = params.getBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true); + // get passphrase from cache, if key has "no" passphrase, this returns an empty String String passphrase; if (params.containsKey(OpenPgpConstants.PARAMS_PASSPHRASE)) { @@ -163,7 +163,7 @@ public class OpenPgpService extends RemoteService { // sign-only PgpOperationOutgoing.Builder builder = new PgpOperationOutgoing.Builder(getContext(), inputData, os); - builder.enableAsciiArmorOutput(true) + builder.enableAsciiArmorOutput(asciiArmor) .signatureHashAlgorithm(appSettings.getHashAlgorithm()) .signatureForceV3(false) .signatureKeyId(appSettings.getKeyId()) @@ -190,7 +190,7 @@ public class OpenPgpService extends RemoteService { ParcelFileDescriptor output, AppSettings appSettings, boolean sign) { try { - boolean asciiArmor = params.getBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, false); + boolean asciiArmor = params.getBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true); long[] keyIds; if (params.containsKey(OpenPgpConstants.PARAMS_KEY_IDS)) { @@ -231,8 +231,6 @@ public class OpenPgpService extends RemoteService { builder.enableAsciiArmorOutput(asciiArmor) .compressionId(appSettings.getCompression()) .symmetricEncryptionAlgorithm(appSettings.getEncryptionAlgorithm()) - .signatureHashAlgorithm(appSettings.getHashAlgorithm()) - .signatureForceV3(false) .encryptionKeyIds(keyIds); if (sign) { @@ -250,7 +248,9 @@ public class OpenPgpService extends RemoteService { } // sign and encrypt - builder.signatureKeyId(appSettings.getKeyId()) + builder.signatureHashAlgorithm(appSettings.getHashAlgorithm()) + .signatureForceV3(false) + .signatureKeyId(appSettings.getKeyId()) .signaturePassphrase(passphrase); } else { // encrypt only