More changes to PgpSignEncrypt

This commit is contained in:
Dominik Schürmann 2014-04-12 20:33:25 +02:00
parent a65a9ac5f0
commit 44a7bda7dc
3 changed files with 48 additions and 37 deletions

View File

@ -80,7 +80,7 @@ public class PgpSignEncrypt {
private boolean mSignatureForceV3; private boolean mSignatureForceV3;
private String mSignaturePassphrase; private String mSignaturePassphrase;
private boolean mEncryptToSigner; private boolean mEncryptToSigner;
private boolean mBinaryInput; private boolean mCleartextInput;
private static byte[] NEW_LINE; private static byte[] NEW_LINE;
@ -110,7 +110,7 @@ public class PgpSignEncrypt {
this.mSignatureForceV3 = builder.mSignatureForceV3; this.mSignatureForceV3 = builder.mSignatureForceV3;
this.mSignaturePassphrase = builder.mSignaturePassphrase; this.mSignaturePassphrase = builder.mSignaturePassphrase;
this.mEncryptToSigner = builder.mEncryptToSigner; this.mEncryptToSigner = builder.mEncryptToSigner;
this.mBinaryInput = builder.mBinaryInput; this.mCleartextInput = builder.mCleartextInput;
} }
public static class Builder { public static class Builder {
@ -132,7 +132,7 @@ public class PgpSignEncrypt {
private boolean mSignatureForceV3 = false; private boolean mSignatureForceV3 = false;
private String mSignaturePassphrase = null; private String mSignaturePassphrase = null;
private boolean mEncryptToSigner = false; private boolean mEncryptToSigner = false;
private boolean mBinaryInput = false; private boolean mCleartextInput = false;
public Builder(ProviderHelper providerHelper, String versionHeader, InputData data, OutputStream outStream) { public Builder(ProviderHelper providerHelper, String versionHeader, InputData data, OutputStream outStream) {
this.mProviderHelper = providerHelper; this.mProviderHelper = providerHelper;
@ -205,11 +205,11 @@ public class PgpSignEncrypt {
/** /**
* TODO: test this option! * TODO: test this option!
* *
* @param binaryInput * @param cleartextInput
* @return * @return
*/ */
public Builder binaryInput(boolean binaryInput) { public Builder cleartextInput(boolean cleartextInput) {
this.mBinaryInput = binaryInput; this.mCleartextInput = cleartextInput;
return this; return this;
} }
@ -255,7 +255,7 @@ public class PgpSignEncrypt {
boolean enableSignature = mSignatureMasterKeyId != Id.key.none; boolean enableSignature = mSignatureMasterKeyId != Id.key.none;
boolean enableEncryption = ((mEncryptionMasterKeyIds != null && mEncryptionMasterKeyIds.length > 0) boolean enableEncryption = ((mEncryptionMasterKeyIds != null && mEncryptionMasterKeyIds.length > 0)
|| mSymmetricPassphrase != null); || mSymmetricPassphrase != null);
boolean enableCompression = (enableEncryption && mCompressionId != Id.choice.compression.none); boolean enableCompression = (mCompressionId != Id.choice.compression.none);
Log.d(Constants.TAG, "enableSignature:" + enableSignature Log.d(Constants.TAG, "enableSignature:" + enableSignature
+ "\nenableEncryption:" + enableEncryption + "\nenableEncryption:" + enableEncryption
@ -362,8 +362,8 @@ public class PgpSignEncrypt {
.setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME); .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);
int signatureType; int signatureType;
if (mEnableAsciiArmorOutput && !enableEncryption) { if (mCleartextInput && mEnableAsciiArmorOutput && !enableEncryption) {
// for sign-only ascii text and sign-only binary input (files) // for sign-only ascii text
signatureType = PGPSignature.CANONICAL_TEXT_DOCUMENT; signatureType = PGPSignature.CANONICAL_TEXT_DOCUMENT;
} else { } else {
signatureType = PGPSignature.BINARY_DOCUMENT; signatureType = PGPSignature.BINARY_DOCUMENT;
@ -436,7 +436,7 @@ public class PgpSignEncrypt {
} }
literalGen.close(); literalGen.close();
} else if (!mBinaryInput && mEnableAsciiArmorOutput && enableSignature && !enableEncryption && !enableCompression) { } else if (enableSignature && mCleartextInput && mEnableAsciiArmorOutput) {
/* cleartext signature: sign-only of ascii text */ /* cleartext signature: sign-only of ascii text */
updateProgress(R.string.progress_signing, 40, 100); updateProgress(R.string.progress_signing, 40, 100);
@ -477,43 +477,45 @@ public class PgpSignEncrypt {
armorOut.endClearText(); armorOut.endClearText();
pOut = new BCPGOutputStream(armorOut); pOut = new BCPGOutputStream(armorOut);
} else if (mBinaryInput && enableSignature && !enableEncryption && !enableCompression) { } else if (enableSignature && !mCleartextInput) {
// TODO: This part of the code is not tested!!! /* sign-only binary (files/data stream) */
/* sign-only binaries (files) */
updateProgress(R.string.progress_signing, 40, 100); updateProgress(R.string.progress_signing, 40, 100);
InputStream in = mData.getInputStream(); InputStream in = mData.getInputStream();
if (mEnableAsciiArmorOutput) {
// TODO: this requires a ascii text, this is currently not checked
// mEnableAsciiArmorOutput does not mean that the input is ascii
final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line; if (enableCompression) {
while ((line = reader.readLine()) != null) { compressGen = new PGPCompressedDataGenerator(mCompressionId);
if (mSignatureForceV3) { bcpgOut = new BCPGOutputStream(compressGen.open(out));
processLineV3(line, null, signatureV3Generator);
signatureV3Generator.update(NEW_LINE);
} else {
processLine(line, null, signatureGenerator);
signatureGenerator.update(NEW_LINE);
}
}
} else { } else {
byte[] buffer = new byte[1 << 16]; bcpgOut = new BCPGOutputStream(out);
int n; }
while ((n = in.read(buffer)) > 0) {
if (mSignatureForceV3) { if (mSignatureForceV3) {
signatureV3Generator.update(buffer, 0, n); signatureV3Generator.generateOnePassVersion(false).encode(bcpgOut);
} else { } else {
signatureGenerator.update(buffer, 0, n); signatureGenerator.generateOnePassVersion(false).encode(bcpgOut);
} }
PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator();
// file name not needed, so empty string
pOut = literalGen.open(bcpgOut, PGPLiteralData.BINARY, "", new Date(),
new byte[1 << 16]);
byte[] buffer = new byte[1 << 16];
int n;
while ((n = in.read(buffer)) > 0) {
pOut.write(buffer, 0, n);
if (mSignatureForceV3) {
signatureV3Generator.update(buffer, 0, n);
} else {
signatureGenerator.update(buffer, 0, n);
} }
} }
pOut = new BCPGOutputStream(out); literalGen.close();
} else { } else {
pOut = null; pOut = null;
Log.e(Constants.TAG, "not supported!"); Log.e(Constants.TAG, "not supported!");

View File

@ -175,6 +175,10 @@ public class OpenPgpService extends RemoteService {
.signatureForceV3(false) .signatureForceV3(false)
.signatureMasterKeyId(accSettings.getKeyId()) .signatureMasterKeyId(accSettings.getKeyId())
.signaturePassphrase(passphrase); .signaturePassphrase(passphrase);
// TODO: currently always assume cleartext input, no sign-only of binary currently!
builder.cleartextInput(true);
builder.build().execute(); builder.build().execute();
} finally { } finally {
is.close(); is.close();

View File

@ -330,6 +330,11 @@ public class KeychainIntentService extends IntentService
.signaturePassphrase( .signaturePassphrase(
PassphraseCacheService.getCachedPassphrase(this, signatureKeyId)); PassphraseCacheService.getCachedPassphrase(this, signatureKeyId));
// this assumes that the bytes are cleartext (valid for current implementation!)
if (target == TARGET_BYTES) {
builder.cleartextInput(true);
}
builder.build().execute(); builder.build().execute();
outStream.close(); outStream.close();