Simplify encrypt and sign backend

This commit is contained in:
Dominik Schürmann 2014-04-01 14:25:37 +02:00
parent c859bbb6da
commit 866d2d28cc
3 changed files with 58 additions and 114 deletions

View File

@ -18,7 +18,6 @@
package org.sufficientlysecure.keychain.service; package org.sufficientlysecure.keychain.service;
import android.app.IntentService; import android.app.IntentService;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -36,7 +35,6 @@ import org.sufficientlysecure.keychain.helper.OtherHelper;
import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.pgp.*; import org.sufficientlysecure.keychain.pgp.*;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.KeychainContract.DataStream;
import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry;
import org.sufficientlysecure.keychain.util.*; import org.sufficientlysecure.keychain.util.*;
@ -88,12 +86,10 @@ public class KeychainIntentService extends IntentService
public static final int TARGET_STREAM = 3; public static final int TARGET_STREAM = 3;
// encrypt // encrypt
public static final String ENCRYPT_SECRET_KEY_ID = "secret_key_id"; public static final String ENCRYPT_SIGNATURE_KEY_ID = "secret_key_id";
public static final String ENCRYPT_USE_ASCII_ARMOR = "use_ascii_armor"; public static final String ENCRYPT_USE_ASCII_ARMOR = "use_ascii_armor";
public static final String ENCRYPT_ENCRYPTION_KEYS_IDS = "encryption_keys_ids"; public static final String ENCRYPT_ENCRYPTION_KEYS_IDS = "encryption_keys_ids";
public static final String ENCRYPT_COMPRESSION_ID = "compression_id"; public static final String ENCRYPT_COMPRESSION_ID = "compression_id";
public static final String ENCRYPT_GENERATE_SIGNATURE = "generate_signature";
public static final String ENCRYPT_SIGN_ONLY = "sign_only";
public static final String ENCRYPT_MESSAGE_BYTES = "message_bytes"; public static final String ENCRYPT_MESSAGE_BYTES = "message_bytes";
public static final String ENCRYPT_INPUT_FILE = "input_file"; public static final String ENCRYPT_INPUT_FILE = "input_file";
public static final String ENCRYPT_OUTPUT_FILE = "output_file"; public static final String ENCRYPT_OUTPUT_FILE = "output_file";
@ -152,7 +148,7 @@ public class KeychainIntentService extends IntentService
public static final String RESULT_SIGNATURE_BYTES = "signature_data"; public static final String RESULT_SIGNATURE_BYTES = "signature_data";
public static final String RESULT_SIGNATURE_STRING = "signature_text"; public static final String RESULT_SIGNATURE_STRING = "signature_text";
public static final String RESULT_ENCRYPTED_STRING = "encrypted_message"; public static final String RESULT_ENCRYPTED_STRING = "encrypted_message";
public static final String RESULT_ENCRYPTED_BYTES = "encrypted_data"; public static final String RESULT_BYTES = "encrypted_data";
public static final String RESULT_URI = "result_uri"; public static final String RESULT_URI = "result_uri";
// decrypt/verify // decrypt/verify
@ -220,20 +216,17 @@ public class KeychainIntentService extends IntentService
/* Input */ /* Input */
int target = data.getInt(TARGET); int target = data.getInt(TARGET);
long secretKeyId = data.getLong(ENCRYPT_SECRET_KEY_ID); long signatureKeyId = data.getLong(ENCRYPT_SIGNATURE_KEY_ID);
String symmetricPassphrase = data.getString(ENCRYPT_SYMMETRIC_PASSPHRASE); String symmetricPassphrase = data.getString(ENCRYPT_SYMMETRIC_PASSPHRASE);
boolean useAsciiArmor = data.getBoolean(ENCRYPT_USE_ASCII_ARMOR); boolean useAsciiArmor = data.getBoolean(ENCRYPT_USE_ASCII_ARMOR);
long encryptionKeyIds[] = data.getLongArray(ENCRYPT_ENCRYPTION_KEYS_IDS); long encryptionKeyIds[] = data.getLongArray(ENCRYPT_ENCRYPTION_KEYS_IDS);
int compressionId = data.getInt(ENCRYPT_COMPRESSION_ID); int compressionId = data.getInt(ENCRYPT_COMPRESSION_ID);
boolean generateSignature = data.getBoolean(ENCRYPT_GENERATE_SIGNATURE); InputStream inStream;
boolean signOnly = data.getBoolean(ENCRYPT_SIGN_ONLY); long inLength;
InputData inputData;
InputStream inStream = null; OutputStream outStream;
long inLength = -1; // String streamFilename = null;
InputData inputData = null;
OutputStream outStream = null;
String streamFilename = null;
switch (target) { switch (target) {
case TARGET_BYTES: /* encrypting bytes directly */ case TARGET_BYTES: /* encrypting bytes directly */
byte[] bytes = data.getByteArray(ENCRYPT_MESSAGE_BYTES); byte[] bytes = data.getByteArray(ENCRYPT_MESSAGE_BYTES);
@ -265,29 +258,30 @@ public class KeychainIntentService extends IntentService
break; break;
case TARGET_STREAM: /* Encrypting stream from content uri */ // TODO: not used currently
Uri providerUri = (Uri) data.getParcelable(ENCRYPT_PROVIDER_URI); // case TARGET_STREAM: /* Encrypting stream from content uri */
// Uri providerUri = (Uri) data.getParcelable(ENCRYPT_PROVIDER_URI);
// InputStream //
InputStream in = getContentResolver().openInputStream(providerUri); // // InputStream
inLength = PgpHelper.getLengthOfStream(in); // InputStream in = getContentResolver().openInputStream(providerUri);
inputData = new InputData(in, inLength); // inLength = PgpHelper.getLengthOfStream(in);
// inputData = new InputData(in, inLength);
// OutputStream //
try { // // OutputStream
while (true) { // try {
streamFilename = PgpHelper.generateRandomFilename(32); // while (true) {
if (streamFilename == null) { // streamFilename = PgpHelper.generateRandomFilename(32);
throw new PgpGeneralException("couldn't generate random file name"); // if (streamFilename == null) {
} // throw new PgpGeneralException("couldn't generate random file name");
openFileInput(streamFilename).close(); // }
} // openFileInput(streamFilename).close();
} catch (FileNotFoundException e) { // }
// found a name that isn't used yet // } catch (FileNotFoundException e) {
} // // found a name that isn't used yet
outStream = openFileOutput(streamFilename, Context.MODE_PRIVATE); // }
// outStream = openFileOutput(streamFilename, Context.MODE_PRIVATE);
break; //
// break;
default: default:
throw new PgpGeneralException("No target choosen!"); throw new PgpGeneralException("No target choosen!");
@ -299,45 +293,20 @@ public class KeychainIntentService extends IntentService
new PgpSignEncrypt.Builder(this, inputData, outStream); new PgpSignEncrypt.Builder(this, inputData, outStream);
builder.progress(this); builder.progress(this);
if (generateSignature) { builder.enableAsciiArmorOutput(useAsciiArmor)
Log.d(Constants.TAG, "generating signature..."); .compressionId(compressionId)
builder.enableAsciiArmorOutput(useAsciiArmor) .symmetricEncryptionAlgorithm(
.signatureForceV3(Preferences.getPreferences(this).getForceV3Signatures()) Preferences.getPreferences(this).getDefaultEncryptionAlgorithm())
.signatureKeyId(secretKeyId) .signatureForceV3(Preferences.getPreferences(this).getForceV3Signatures())
.signatureHashAlgorithm( .encryptionKeyIds(encryptionKeyIds)
Preferences.getPreferences(this).getDefaultHashAlgorithm()) .symmetricPassphrase(symmetricPassphrase)
.signaturePassphrase( .signatureKeyId(signatureKeyId)
PassphraseCacheService.getCachedPassphrase(this, secretKeyId)); .signatureHashAlgorithm(
Preferences.getPreferences(this).getDefaultHashAlgorithm())
.signaturePassphrase(
PassphraseCacheService.getCachedPassphrase(this, signatureKeyId));
builder.build().generateSignature(); builder.build().execute();
} else if (signOnly) {
Log.d(Constants.TAG, "sign only...");
builder.enableAsciiArmorOutput(useAsciiArmor)
.signatureForceV3(Preferences.getPreferences(this).getForceV3Signatures())
.signatureKeyId(secretKeyId)
.signatureHashAlgorithm(
Preferences.getPreferences(this).getDefaultHashAlgorithm())
.signaturePassphrase(
PassphraseCacheService.getCachedPassphrase(this, secretKeyId));
builder.build().execute();
} else {
Log.d(Constants.TAG, "encrypt...");
builder.enableAsciiArmorOutput(useAsciiArmor)
.compressionId(compressionId)
.symmetricEncryptionAlgorithm(
Preferences.getPreferences(this).getDefaultEncryptionAlgorithm())
.signatureForceV3(Preferences.getPreferences(this).getForceV3Signatures())
.encryptionKeyIds(encryptionKeyIds)
.symmetricPassphrase(symmetricPassphrase)
.signatureKeyId(secretKeyId)
.signatureHashAlgorithm(
Preferences.getPreferences(this).getDefaultHashAlgorithm())
.signaturePassphrase(
PassphraseCacheService.getCachedPassphrase(this, secretKeyId));
builder.build().execute();
}
outStream.close(); outStream.close();
@ -347,33 +316,20 @@ public class KeychainIntentService extends IntentService
switch (target) { switch (target) {
case TARGET_BYTES: case TARGET_BYTES:
if (useAsciiArmor) { byte output[] = ((ByteArrayOutputStream) outStream).toByteArray();
String output = new String(
((ByteArrayOutputStream) outStream).toByteArray()); resultData.putByteArray(RESULT_BYTES, output);
if (generateSignature) {
resultData.putString(RESULT_SIGNATURE_STRING, output);
} else {
resultData.putString(RESULT_ENCRYPTED_STRING, output);
}
} else {
byte output[] = ((ByteArrayOutputStream) outStream).toByteArray();
if (generateSignature) {
resultData.putByteArray(RESULT_SIGNATURE_BYTES, output);
} else {
resultData.putByteArray(RESULT_ENCRYPTED_BYTES, output);
}
}
break; break;
case TARGET_URI: case TARGET_URI:
// nothing, file was written, just send okay // nothing, file was written, just send okay
break; break;
case TARGET_STREAM: // case TARGET_STREAM:
String uri = DataStream.buildDataStreamUri(streamFilename).toString(); // String uri = DataStream.buildDataStreamUri(streamFilename).toString();
resultData.putString(RESULT_URI, uri); // resultData.putString(RESULT_URI, uri);
//
break; // break;
} }
OtherHelper.logDebugBundle(resultData, "resultData"); OtherHelper.logDebugBundle(resultData, "resultData");

View File

@ -282,12 +282,8 @@ public class EncryptFileFragment extends Fragment {
} }
data.putString(KeychainIntentService.ENCRYPT_SYMMETRIC_PASSPHRASE, passphrase); data.putString(KeychainIntentService.ENCRYPT_SYMMETRIC_PASSPHRASE, passphrase);
} else { } else {
data.putLong(KeychainIntentService.ENCRYPT_SECRET_KEY_ID, mEncryptInterface.getSignatureKey()); data.putLong(KeychainIntentService.ENCRYPT_SIGNATURE_KEY_ID, mEncryptInterface.getSignatureKey());
data.putLongArray(KeychainIntentService.ENCRYPT_ENCRYPTION_KEYS_IDS, mEncryptInterface.getEncryptionKeys()); data.putLongArray(KeychainIntentService.ENCRYPT_ENCRYPTION_KEYS_IDS, mEncryptInterface.getEncryptionKeys());
boolean signOnly = (mEncryptInterface.getEncryptionKeys() == null
|| mEncryptInterface.getEncryptionKeys().length == 0);
data.putBoolean(KeychainIntentService.ENCRYPT_SIGN_ONLY, signOnly);
} }
Log.d(Constants.TAG, "mInputFilename=" + mInputFilename + ", mOutputFilename=" Log.d(Constants.TAG, "mInputFilename=" + mInputFilename + ", mOutputFilename="
@ -313,10 +309,6 @@ public class EncryptFileFragment extends Fragment {
super.handleMessage(message); super.handleMessage(message);
if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) {
// get returned data bundle
Bundle data = message.getData();
String output;
AppMsg.makeText(getActivity(), R.string.encryption_successful, AppMsg.makeText(getActivity(), R.string.encryption_successful,
AppMsg.STYLE_INFO).show(); AppMsg.STYLE_INFO).show();

View File

@ -182,12 +182,11 @@ public class EncryptMessageFragment extends Fragment {
} }
data.putString(KeychainIntentService.ENCRYPT_SYMMETRIC_PASSPHRASE, passphrase); data.putString(KeychainIntentService.ENCRYPT_SYMMETRIC_PASSPHRASE, passphrase);
} else { } else {
data.putLong(KeychainIntentService.ENCRYPT_SECRET_KEY_ID, mEncryptInterface.getSignatureKey()); data.putLong(KeychainIntentService.ENCRYPT_SIGNATURE_KEY_ID, mEncryptInterface.getSignatureKey());
data.putLongArray(KeychainIntentService.ENCRYPT_ENCRYPTION_KEYS_IDS, mEncryptInterface.getEncryptionKeys()); data.putLongArray(KeychainIntentService.ENCRYPT_ENCRYPTION_KEYS_IDS, mEncryptInterface.getEncryptionKeys());
boolean signOnly = (mEncryptInterface.getEncryptionKeys() == null boolean signOnly = (mEncryptInterface.getEncryptionKeys() == null
|| mEncryptInterface.getEncryptionKeys().length == 0); || mEncryptInterface.getEncryptionKeys().length == 0);
data.putBoolean(KeychainIntentService.ENCRYPT_SIGN_ONLY, signOnly);
if (signOnly) { if (signOnly) {
message = fixBadCharactersForGmail(message); message = fixBadCharactersForGmail(message);
} }
@ -214,18 +213,15 @@ public class EncryptMessageFragment extends Fragment {
// get returned data bundle // get returned data bundle
Bundle data = message.getData(); Bundle data = message.getData();
String output; String output = new String(data.getByteArray(KeychainIntentService.RESULT_BYTES));
Log.d(Constants.TAG, "output: " + output);
if (toClipboard) { if (toClipboard) {
output = data.getString(KeychainIntentService.RESULT_ENCRYPTED_STRING);
Log.d(Constants.TAG, "output: " + output);
ClipboardReflection.copyToClipboard(getActivity(), output); ClipboardReflection.copyToClipboard(getActivity(), output);
AppMsg.makeText(getActivity(), AppMsg.makeText(getActivity(),
R.string.encryption_to_clipboard_successful, AppMsg.STYLE_INFO) R.string.encryption_to_clipboard_successful, AppMsg.STYLE_INFO)
.show(); .show();
} else { } else {
output = data.getString(KeychainIntentService.RESULT_ENCRYPTED_STRING);
Log.d(Constants.TAG, "output: " + output);
Intent sendIntent = new Intent(Intent.ACTION_SEND); Intent sendIntent = new Intent(Intent.ACTION_SEND);
// Type is set to text/plain so that encrypted messages can // Type is set to text/plain so that encrypted messages can