diff options
author | Dominik <dominik@dominikschuermann.de> | 2012-06-20 20:37:23 +0300 |
---|---|---|
committer | Dominik <dominik@dominikschuermann.de> | 2012-06-20 20:37:23 +0300 |
commit | 72718d7fbc6cdc7c93d3b2e3bbcddd8b8a1c9006 (patch) | |
tree | 496083e1e0af5e3c6207d7d4caa09be9ef019aa1 /org_apg | |
parent | 1e78dd165a2d32d28e3a77c512b41102dedd5cc8 (diff) | |
download | open-keychain-72718d7fbc6cdc7c93d3b2e3bbcddd8b8a1c9006.tar.gz open-keychain-72718d7fbc6cdc7c93d3b2e3bbcddd8b8a1c9006.tar.bz2 open-keychain-72718d7fbc6cdc7c93d3b2e3bbcddd8b8a1c9006.zip |
merging actions in ApgService
Diffstat (limited to 'org_apg')
-rw-r--r-- | org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java | 2 | ||||
-rw-r--r-- | org_apg/src/org/thialfihar/android/apg/service/ApgService.java | 614 | ||||
-rw-r--r-- | org_apg/src/org/thialfihar/android/apg/ui/BaseActivity.java | 2 | ||||
-rw-r--r-- | org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java | 9 | ||||
-rw-r--r-- | org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java | 10 | ||||
-rw-r--r-- | org_apg/src/org/thialfihar/android/apg/util/ProgressDialogUpdater.java (renamed from org_apg/src/org/thialfihar/android/apg/ProgressDialogUpdater.java) | 2 |
6 files changed, 279 insertions, 360 deletions
diff --git a/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java b/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java index 4c87b2ca8..180716ef5 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java @@ -82,10 +82,10 @@ import org.thialfihar.android.apg.util.HkpKeyServer; import org.thialfihar.android.apg.util.InputData; import org.thialfihar.android.apg.util.PositionAwareInputStream; import org.thialfihar.android.apg.util.Primes; +import org.thialfihar.android.apg.util.ProgressDialogUpdater; import org.thialfihar.android.apg.util.KeyServer.AddKeyException; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; -import org.thialfihar.android.apg.ProgressDialogUpdater; import org.thialfihar.android.apg.R; import android.app.Activity; diff --git a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java index f57d67c44..8a14df60e 100644 --- a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java +++ b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java @@ -24,13 +24,13 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import org.spongycastle.openpgp.PGPSecretKey; import org.spongycastle.openpgp.PGPSecretKeyRing; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; -import org.thialfihar.android.apg.ProgressDialogUpdater; import org.thialfihar.android.apg.R; import org.thialfihar.android.apg.helper.FileHelper; import org.thialfihar.android.apg.helper.PGPMain; @@ -39,6 +39,7 @@ import org.thialfihar.android.apg.helper.PGPMain.GeneralException; import org.thialfihar.android.apg.helper.PGPConversionHelper; import org.thialfihar.android.apg.provider.DataProvider; import org.thialfihar.android.apg.util.InputData; +import org.thialfihar.android.apg.util.ProgressDialogUpdater; import android.app.IntentService; import android.content.Context; @@ -55,24 +56,21 @@ import org.thialfihar.android.apg.util.Log; * data from the activities or other apps, queues these intents, executes them, and stops itself * after doing them. */ - -/** - * TODO: - * - * - ProgressDialogUpdater rework??? - * - * - put recurring things into private functions when possible - * - * - */ public class ApgService extends IntentService implements ProgressDialogUpdater { - // extras that can be given by intent + /* extras that can be given by intent */ public static final String EXTRA_MESSENGER = "messenger"; public static final String EXTRA_ACTION = "action"; public static final String EXTRA_DATA = "data"; - // keys for data bundle + /* keys for data bundle */ + + // encrypt and decrypt + public static final String TARGET = "type"; + // possible targets: + public static final int TARGET_BYTES = 1; + public static final int TARGET_FILE = 2; + public static final int TARGET_STREAM = 3; // encrypt public static final String SECRET_KEY_ID = "secretKeyId"; @@ -110,14 +108,10 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { // delete file securely public static final String DELETE_FILE = "deleteFile"; - // possible EXTRA_ACTIONs - public static final int ACTION_ENCRYPT_SIGN_BYTES = 10; - public static final int ACTION_ENCRYPT_SIGN_FILE = 11; - public static final int ACTION_ENCRYPT_SIGN_STREAM = 12; + /* possible EXTRA_ACTIONs */ + public static final int ACTION_ENCRYPT_SIGN = 10; - public static final int ACTION_DECRYPT_BYTES = 20; - public static final int ACTION_DECRYPT_FILE = 21; - public static final int ACTION_DECRYPT_STREAM = 22; + public static final int ACTION_DECRYPT_VERIFY = 20; public static final int ACTION_SAVE_KEYRING = 30; public static final int ACTION_GENERATE_KEY = 31; @@ -125,7 +119,7 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { public static final int ACTION_DELETE_FILE_SECURELY = 40; - // possible data keys as result + /* possible data keys as result send over messenger */ // keys public static final String RESULT_NEW_KEY = "newKey"; public static final String RESULT_NEW_KEY2 = "newKey2"; @@ -178,116 +172,93 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { // execute action from extra bundle switch (action) { - case ACTION_SAVE_KEYRING: + case ACTION_ENCRYPT_SIGN: try { /* Input */ - String oldPassPhrase = data.getString(CURRENT_PASSPHRASE); - String newPassPhrase = data.getString(NEW_PASSPHRASE); - if (newPassPhrase == null) { - newPassPhrase = oldPassPhrase; - } - @SuppressWarnings("unchecked") - ArrayList<String> userIds = (ArrayList<String>) data.getSerializable(USER_IDS); - ArrayList<PGPSecretKey> keys = PGPConversionHelper.BytesToPGPSecretKeyList(data - .getByteArray(KEYS)); - @SuppressWarnings("unchecked") - ArrayList<Integer> keysUsages = (ArrayList<Integer>) data - .getSerializable(KEYS_USAGES); - long masterKeyId = data.getLong(MASTER_KEY_ID); - - /* Operation */ - PGPMain.buildSecretKey(this, userIds, keys, keysUsages, masterKeyId, oldPassPhrase, - newPassPhrase, this); - PGPMain.setCachedPassPhrase(masterKeyId, newPassPhrase); + int target = data.getInt(TARGET); - /* Output */ - sendMessageToHandler(ApgHandler.MESSAGE_OKAY); - } catch (Exception e) { - sendErrorToHandler(e); - } - - break; - - case ACTION_GENERATE_KEY: - - try { - /* Input */ - int algorithm = data.getInt(ALGORITHM); + long secretKeyId = data.getLong(SECRET_KEY_ID); String passphrase = data.getString(SYMMETRIC_PASSPHRASE); - int keysize = data.getInt(KEY_SIZE); - PGPSecretKey masterKey = null; - if (data.containsKey(MASTER_KEY)) { - masterKey = PGPConversionHelper.BytesToPGPSecretKey(data - .getByteArray(MASTER_KEY)); - } - - /* Operation */ - PGPSecretKeyRing newKeyRing = PGPMain.createKey(this, algorithm, keysize, - passphrase, masterKey); - /* Output */ - Bundle resultData = new Bundle(); - resultData.putByteArray(RESULT_NEW_KEY, - PGPConversionHelper.PGPSecretKeyRingToBytes(newKeyRing)); - sendMessageToHandler(ApgHandler.MESSAGE_OKAY, resultData); - } catch (Exception e) { - sendErrorToHandler(e); - } + boolean useAsciiArmour = data.getBoolean(USE_ASCII_AMOR); + long encryptionKeyIds[] = data.getLongArray(ENCRYPTION_KEYS_IDS); + long signatureKeyId = data.getLong(SIGNATURE_KEY_ID); + int compressionId = data.getInt(COMPRESSION_ID); + boolean generateSignature = data.getBoolean(GENERATE_SIGNATURE); + boolean signOnly = data.getBoolean(SIGN_ONLY); - break; + InputStream inStream = null; + long inLength = -1; + InputData inputData = null; + OutputStream outStream = null; + String streamFilename = null; + switch (target) { + case TARGET_BYTES: /* encrypting bytes directly */ + byte[] bytes = data.getByteArray(MESSAGE_BYTES); + + inStream = new ByteArrayInputStream(bytes); + inLength = bytes.length; + + inputData = new InputData(inStream, inLength); + outStream = new ByteArrayOutputStream(); + + break; + case TARGET_FILE: /* encrypting file */ + String inputFile = data.getString(INPUT_FILE); + String outputFile = data.getString(OUTPUT_FILE); + + // check if storage is ready + if (!FileHelper.isStorageMounted(inputFile) + || !FileHelper.isStorageMounted(outputFile)) { + sendErrorToHandler(new GeneralException( + getString(R.string.error_externalStorageNotReady))); + return; + } - case ACTION_GENERATE_DEFAULT_RSA_KEYS: - // generate one RSA 2048 key for signing and one subkey for encrypting! - try { - /* Input */ - String passphrase = data.getString(SYMMETRIC_PASSPHRASE); + inStream = new FileInputStream(inputFile); + File file = new File(inputFile); + inLength = file.length(); + inputData = new InputData(inStream, inLength); - /* Operation */ - PGPSecretKeyRing masterKeyRing = PGPMain.createKey(this, Id.choice.algorithm.rsa, - 2048, passphrase, null); + outStream = new FileOutputStream(outputFile); - PGPSecretKeyRing subKeyRing = PGPMain.createKey(this, Id.choice.algorithm.rsa, - 2048, passphrase, masterKeyRing.getSecretKey()); + break; - /* Output */ - Bundle resultData = new Bundle(); - resultData.putByteArray(RESULT_NEW_KEY, - PGPConversionHelper.PGPSecretKeyRingToBytes(masterKeyRing)); - resultData.putByteArray(RESULT_NEW_KEY2, - PGPConversionHelper.PGPSecretKeyRingToBytes(subKeyRing)); - sendMessageToHandler(ApgHandler.MESSAGE_OKAY, resultData); - } catch (Exception e) { - sendErrorToHandler(e); - } + case TARGET_STREAM: /* Encrypting stream from content uri */ + Uri providerUri = Uri.parse(data.getString(PROVIDER_URI)); - break; + // InputStream + InputStream in = getContentResolver().openInputStream(providerUri); + inLength = PGPMain.getLengthOfStream(in); + inputData = new InputData(in, inLength); - case ACTION_ENCRYPT_SIGN_BYTES: + // OutputStream + try { + while (true) { + streamFilename = PGPMain.generateRandomString(32); + if (streamFilename == null) { + throw new PGPMain.GeneralException( + "couldn't generate random file name"); + } + openFileInput(streamFilename).close(); + } + } catch (FileNotFoundException e) { + // found a name that isn't used yet + } + outStream = openFileOutput(streamFilename, Context.MODE_PRIVATE); - try { - /* Input */ - long secretKeyId = data.getLong(SECRET_KEY_ID); - String passphrase = data.getString(SYMMETRIC_PASSPHRASE); + break; - byte[] bytes = data.getByteArray(MESSAGE_BYTES); + default: + throw new PGPMain.GeneralException("No target choosen!"); - boolean useAsciiArmour = data.getBoolean(USE_ASCII_AMOR); - long encryptionKeyIds[] = data.getLongArray(ENCRYPTION_KEYS_IDS); - long signatureKeyId = data.getLong(SIGNATURE_KEY_ID); - int compressionId = data.getInt(COMPRESSION_ID); - boolean generateSignature = data.getBoolean(GENERATE_SIGNATURE); - boolean signOnly = data.getBoolean(SIGN_ONLY); + } /* Operation */ - ByteArrayInputStream inStream = new ByteArrayInputStream(bytes); - int inLength = bytes.length; - - InputData inputData = new InputData(inStream, inLength); - ByteArrayOutputStream outStream = new ByteArrayOutputStream(); if (generateSignature) { - Log.d(Constants.TAG, "generate signature..."); + Log.d(Constants.TAG, "generating signature..."); PGPMain.generateSignature(this, inputData, outStream, useAsciiArmour, false, secretKeyId, PGPMain.getCachedPassPhrase(secretKeyId), Preferences .getPreferences(this).getDefaultHashAlgorithm(), Preferences @@ -311,22 +282,38 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { outStream.close(); /* Output */ + Bundle resultData = new Bundle(); - if (useAsciiArmour) { - String output = new String(outStream.toByteArray()); - if (generateSignature) { - resultData.putString(RESULT_SIGNATURE_TEXT, output); - } else { - resultData.putString(RESULT_ENCRYPTED_MESSAGE, output); - } - } else { - byte output[] = outStream.toByteArray(); - if (generateSignature) { - resultData.putByteArray(RESULT_SIGNATURE_DATA, output); + switch (target) { + case TARGET_BYTES: + if (useAsciiArmour) { + String output = new String( + ((ByteArrayOutputStream) outStream).toByteArray()); + if (generateSignature) { + resultData.putString(RESULT_SIGNATURE_TEXT, output); + } else { + resultData.putString(RESULT_ENCRYPTED_MESSAGE, output); + } } else { - resultData.putByteArray(RESULT_ENCRYPTED_DATA, output); + byte output[] = ((ByteArrayOutputStream) outStream).toByteArray(); + if (generateSignature) { + resultData.putByteArray(RESULT_SIGNATURE_DATA, output); + } else { + resultData.putByteArray(RESULT_ENCRYPTED_DATA, output); + } } + + break; + case TARGET_FILE: + // nothing, file was written, just send okay + + break; + case TARGET_STREAM: + String uri = "content://" + DataProvider.AUTHORITY + "/data/" + streamFilename; + resultData.putString(RESULT_URI, uri); + + break; } sendMessageToHandler(ApgHandler.MESSAGE_OKAY, resultData); @@ -336,134 +323,126 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { break; - case ACTION_ENCRYPT_SIGN_FILE: + case ACTION_DECRYPT_VERIFY: try { /* Input */ + int target = data.getInt(TARGET); + long secretKeyId = data.getLong(SECRET_KEY_ID); - String passphrase = data.getString(SYMMETRIC_PASSPHRASE); + byte[] bytes = data.getByteArray(CIPHERTEXT_BYTES); + boolean signedOnly = data.getBoolean(SIGNED_ONLY); + boolean returnBytes = data.getBoolean(RETURN_BYTES); + boolean assumeSymmetricEncryption = data.getBoolean(ASSUME_SYMMETRIC); - String inputFile = data.getString(INPUT_FILE); - String outputFile = data.getString(OUTPUT_FILE); + InputStream inStream = null; + long inLength = -1; + InputData inputData = null; + OutputStream outStream = null; + String streamFilename = null; + switch (target) { + case TARGET_BYTES: /* decrypting bytes directly */ + inStream = new ByteArrayInputStream(bytes); + inLength = bytes.length; + + inputData = new InputData(inStream, inLength); + outStream = new ByteArrayOutputStream(); + + break; + + case TARGET_FILE: /* decrypting file */ + String inputFile = data.getString(INPUT_FILE); + String outputFile = data.getString(OUTPUT_FILE); + + // check if storage is ready + if (!FileHelper.isStorageMounted(inputFile) + || !FileHelper.isStorageMounted(outputFile)) { + sendErrorToHandler(new GeneralException( + getString(R.string.error_externalStorageNotReady))); + return; + } - boolean useAsciiArmour = data.getBoolean(USE_ASCII_AMOR); - long encryptionKeyIds[] = data.getLongArray(ENCRYPTION_KEYS_IDS); - long signatureKeyId = data.getLong(SIGNATURE_KEY_ID); - int compressionId = data.getInt(COMPRESSION_ID); - boolean generateSignature = data.getBoolean(GENERATE_SIGNATURE); - boolean signOnly = data.getBoolean(SIGN_ONLY); + // InputStream + inLength = -1; + inStream = new FileInputStream(inputFile); + File file = new File(inputFile); + inLength = file.length(); + inputData = new InputData(inStream, inLength); + + // OutputStream + outStream = new FileOutputStream(outputFile); + + break; + + case TARGET_STREAM: /* decrypting stream from content uri */ + Uri providerUri = Uri.parse(data.getString(PROVIDER_URI)); + + // InputStream + InputStream in = getContentResolver().openInputStream(providerUri); + inLength = PGPMain.getLengthOfStream(in); + inputData = new InputData(in, inLength); + + // OutputStream + try { + while (true) { + streamFilename = PGPMain.generateRandomString(32); + if (streamFilename == null) { + throw new PGPMain.GeneralException( + "couldn't generate random file name"); + } + openFileInput(streamFilename).close(); + } + } catch (FileNotFoundException e) { + // found a name that isn't used yet + } + outStream = openFileOutput(streamFilename, Context.MODE_PRIVATE); + + break; + + default: + throw new PGPMain.GeneralException("No target choosen!"); - /* Operation */ - // check if storage is ready - if (!FileHelper.isStorageMounted(inputFile) - || !FileHelper.isStorageMounted(outputFile)) { - sendErrorToHandler(new GeneralException( - getString(R.string.error_externalStorageNotReady))); - return; } - // InputStream - long inLength = -1; - FileInputStream inStream = new FileInputStream(inputFile); - File file = new File(inputFile); - inLength = file.length(); - InputData inputData = new InputData(inStream, inLength); + /* Operation */ - // OutputStream - FileOutputStream outStream = new FileOutputStream(outputFile); + Bundle resultData = new Bundle(); - if (generateSignature) { - Log.d(Constants.TAG, "generate signature..."); - PGPMain.generateSignature(this, inputData, outStream, useAsciiArmour, true, - secretKeyId, PGPMain.getCachedPassPhrase(secretKeyId), Preferences - .getPreferences(this).getDefaultHashAlgorithm(), Preferences - .getPreferences(this).getForceV3Signatures(), this); - } else if (signOnly) { - Log.d(Constants.TAG, "sign only..."); - PGPMain.signText(this, inputData, outStream, secretKeyId, PGPMain - .getCachedPassPhrase(secretKeyId), Preferences.getPreferences(this) - .getDefaultHashAlgorithm(), Preferences.getPreferences(this) - .getForceV3Signatures(), this); + // verifyText and decrypt returning additional resultData values for the + // verification of signatures + if (signedOnly) { + resultData = PGPMain.verifyText(this, inputData, outStream, this); } else { - Log.d(Constants.TAG, "encrypt..."); - PGPMain.encrypt(this, inputData, outStream, useAsciiArmour, encryptionKeyIds, - signatureKeyId, PGPMain.getCachedPassPhrase(signatureKeyId), this, - Preferences.getPreferences(this).getDefaultEncryptionAlgorithm(), - Preferences.getPreferences(this).getDefaultHashAlgorithm(), - compressionId, Preferences.getPreferences(this).getForceV3Signatures(), - passphrase); + resultData = PGPMain.decrypt(this, inputData, outStream, + PGPMain.getCachedPassPhrase(secretKeyId), this, + assumeSymmetricEncryption); } outStream.close(); /* Output */ - sendMessageToHandler(ApgHandler.MESSAGE_OKAY); - } catch (Exception e) { - sendErrorToHandler(e); - } - break; - - case ACTION_ENCRYPT_SIGN_STREAM: - try { - /* Input */ - long secretKeyId = data.getLong(SECRET_KEY_ID); - String passphrase = data.getString(SYMMETRIC_PASSPHRASE); - Uri providerUri = Uri.parse(data.getString(PROVIDER_URI)); - - boolean useAsciiArmour = data.getBoolean(USE_ASCII_AMOR); - long encryptionKeyIds[] = data.getLongArray(ENCRYPTION_KEYS_IDS); - long signatureKeyId = data.getLong(SIGNATURE_KEY_ID); - int compressionId = data.getInt(COMPRESSION_ID); - boolean generateSignature = data.getBoolean(GENERATE_SIGNATURE); - boolean signOnly = data.getBoolean(SIGN_ONLY); - - /* Operation */ - // InputStream - InputStream in = getContentResolver().openInputStream(providerUri); - long inLength = PGPMain.getLengthOfStream(in); - InputData inputData = new InputData(in, inLength); - - // OutputStream - String streamFilename = null; - try { - while (true) { - streamFilename = PGPMain.generateRandomString(32); - if (streamFilename == null) { - throw new PGPMain.GeneralException("couldn't generate random file name"); - } - openFileInput(streamFilename).close(); + switch (target) { + case TARGET_BYTES: + if (returnBytes) { + byte output[] = ((ByteArrayOutputStream) outStream).toByteArray(); + resultData.putByteArray(RESULT_DECRYPTED_DATA, output); + } else { + String output = new String( + ((ByteArrayOutputStream) outStream).toByteArray()); + resultData.putString(RESULT_DECRYPTED_MESSAGE, output); } - } catch (FileNotFoundException e) { - // found a name that isn't used yet - } - FileOutputStream outStream = openFileOutput(streamFilename, Context.MODE_PRIVATE); - - if (generateSignature) { - PGPMain.generateSignature(this, inputData, outStream, useAsciiArmour, true, - secretKeyId, PGPMain.getCachedPassPhrase(secretKeyId), Preferences - .getPreferences(this).getDefaultHashAlgorithm(), Preferences - .getPreferences(this).getForceV3Signatures(), this); - } else if (signOnly) { - PGPMain.signText(this, inputData, outStream, secretKeyId, PGPMain - .getCachedPassPhrase(secretKeyId), Preferences.getPreferences(this) - .getDefaultHashAlgorithm(), Preferences.getPreferences(this) - .getForceV3Signatures(), this); - } else { - PGPMain.encrypt(this, inputData, outStream, useAsciiArmour, encryptionKeyIds, - signatureKeyId, PGPMain.getCachedPassPhrase(signatureKeyId), this, - Preferences.getPreferences(this).getDefaultEncryptionAlgorithm(), - Preferences.getPreferences(this).getDefaultHashAlgorithm(), - compressionId, Preferences.getPreferences(this).getForceV3Signatures(), - passphrase); - } - outStream.close(); + break; + case TARGET_FILE: + // nothing, file was written, just send okay and verification bundle - /* Output */ - Bundle resultData = new Bundle(); + break; + case TARGET_STREAM: + String uri = "content://" + DataProvider.AUTHORITY + "/data/" + streamFilename; + resultData.putString(RESULT_URI, uri); - String uri = "content://" + DataProvider.AUTHORITY + "/data/" + streamFilename; - resultData.putString(RESULT_URI, uri); + break; + } sendMessageToHandler(ApgHandler.MESSAGE_OKAY, resultData); } catch (Exception e) { @@ -472,21 +451,28 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { break; - case ACTION_DELETE_FILE_SECURELY: + case ACTION_SAVE_KEYRING: + try { /* Input */ - String deleteFile = data.getString(DELETE_FILE); + String oldPassPhrase = data.getString(CURRENT_PASSPHRASE); + String newPassPhrase = data.getString(NEW_PASSPHRASE); + if (newPassPhrase == null) { + newPassPhrase = oldPassPhrase; + } + @SuppressWarnings("unchecked") + ArrayList<String> userIds = (ArrayList<String>) data.getSerializable(USER_IDS); + ArrayList<PGPSecretKey> keys = PGPConversionHelper.BytesToPGPSecretKeyList(data + .getByteArray(KEYS)); + @SuppressWarnings("unchecked") + ArrayList<Integer> keysUsages = (ArrayList<Integer>) data + .getSerializable(KEYS_USAGES); + long masterKeyId = data.getLong(MASTER_KEY_ID); /* Operation */ - try { - PGPMain.deleteFileSecurely(this, new File(deleteFile), this); - } catch (FileNotFoundException e) { - throw new PGPMain.GeneralException(getString(R.string.error_fileNotFound, - deleteFile)); - } catch (IOException e) { - throw new PGPMain.GeneralException(getString(R.string.error_fileDeleteFailed, - deleteFile)); - } + PGPMain.buildSecretKey(this, userIds, keys, keysUsages, masterKeyId, oldPassPhrase, + newPassPhrase, this); + PGPMain.setCachedPassPhrase(masterKeyId, newPassPhrase); /* Output */ sendMessageToHandler(ApgHandler.MESSAGE_OKAY); @@ -496,45 +482,27 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { break; - case ACTION_DECRYPT_BYTES: + case ACTION_GENERATE_KEY: + try { /* Input */ - long secretKeyId = data.getLong(SECRET_KEY_ID); - byte[] bytes = data.getByteArray(CIPHERTEXT_BYTES); - boolean signedOnly = data.getBoolean(SIGNED_ONLY); - boolean returnBytes = data.getBoolean(RETURN_BYTES); - boolean assumeSymmetricEncryption = data.getBoolean(ASSUME_SYMMETRIC); - - /* Operation */ - ByteArrayInputStream inStream = new ByteArrayInputStream(bytes); - int inLength = bytes.length; - - InputData inputData = new InputData(inStream, inLength); - ByteArrayOutputStream outStream = new ByteArrayOutputStream(); - - Bundle resultData = new Bundle(); - - // verifyText and decrypt returning additional resultData values for the - // verification of signatures - if (signedOnly) { - resultData = PGPMain.verifyText(this, inputData, outStream, this); - } else { - resultData = PGPMain.decrypt(this, inputData, outStream, - PGPMain.getCachedPassPhrase(secretKeyId), this, - assumeSymmetricEncryption); + int algorithm = data.getInt(ALGORITHM); + String passphrase = data.getString(SYMMETRIC_PASSPHRASE); + int keysize = data.getInt(KEY_SIZE); + PGPSecretKey masterKey = null; + if (data.containsKey(MASTER_KEY)) { + masterKey = PGPConversionHelper.BytesToPGPSecretKey(data + .getByteArray(MASTER_KEY)); } - outStream.close(); + /* Operation */ + PGPSecretKeyRing newKeyRing = PGPMain.createKey(this, algorithm, keysize, + passphrase, masterKey); /* Output */ - if (returnBytes) { - byte output[] = outStream.toByteArray(); - resultData.putByteArray(RESULT_DECRYPTED_DATA, output); - } else { - String output = new String(outStream.toByteArray()); - resultData.putString(RESULT_DECRYPTED_MESSAGE, output); - } - + Bundle resultData = new Bundle(); + resultData.putByteArray(RESULT_NEW_KEY, + PGPConversionHelper.PGPSecretKeyRingToBytes(newKeyRing)); sendMessageToHandler(ApgHandler.MESSAGE_OKAY, resultData); } catch (Exception e) { sendErrorToHandler(e); @@ -542,104 +510,50 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { break; - case ACTION_DECRYPT_FILE: + case ACTION_GENERATE_DEFAULT_RSA_KEYS: + // generate one RSA 2048 key for signing and one subkey for encrypting! try { /* Input */ - long secretKeyId = data.getLong(SECRET_KEY_ID); - boolean signedOnly = data.getBoolean(SIGNED_ONLY); - boolean assumeSymmetricEncryption = data.getBoolean(ASSUME_SYMMETRIC); - - String inputFile = data.getString(INPUT_FILE); - String outputFile = data.getString(OUTPUT_FILE); + String passphrase = data.getString(SYMMETRIC_PASSPHRASE); /* Operation */ - // check if storage is ready - if (!FileHelper.isStorageMounted(inputFile) - || !FileHelper.isStorageMounted(outputFile)) { - sendErrorToHandler(new GeneralException( - getString(R.string.error_externalStorageNotReady))); - return; - } - - // InputStream - long inLength = -1; - FileInputStream inStream = new FileInputStream(inputFile); - File file = new File(inputFile); - inLength = file.length(); - InputData inputData = new InputData(inStream, inLength); - - // OutputStream - FileOutputStream outStream = new FileOutputStream(outputFile); - - Bundle resultData = new Bundle(); - - // verifyText and decrypt returning additional output values for the - // verification of signatures - if (signedOnly) { - resultData = PGPMain.verifyText(this, inputData, outStream, this); - } else { - resultData = PGPMain.decrypt(this, inputData, outStream, - PGPMain.getCachedPassPhrase(secretKeyId), this, - assumeSymmetricEncryption); - } + PGPSecretKeyRing masterKeyRing = PGPMain.createKey(this, Id.choice.algorithm.rsa, + 2048, passphrase, null); - outStream.close(); + PGPSecretKeyRing subKeyRing = PGPMain.createKey(this, Id.choice.algorithm.rsa, + 2048, passphrase, masterKeyRing.getSecretKey()); /* Output */ + Bundle resultData = new Bundle(); + resultData.putByteArray(RESULT_NEW_KEY, + PGPConversionHelper.PGPSecretKeyRingToBytes(masterKeyRing)); + resultData.putByteArray(RESULT_NEW_KEY2, + PGPConversionHelper.PGPSecretKeyRingToBytes(subKeyRing)); sendMessageToHandler(ApgHandler.MESSAGE_OKAY, resultData); } catch (Exception e) { sendErrorToHandler(e); } + break; - case ACTION_DECRYPT_STREAM: + case ACTION_DELETE_FILE_SECURELY: try { /* Input */ - long secretKeyId = data.getLong(SECRET_KEY_ID); - boolean signedOnly = data.getBoolean(SIGNED_ONLY); - boolean assumeSymmetricEncryption = data.getBoolean(ASSUME_SYMMETRIC); - Uri providerUri = Uri.parse(data.getString(PROVIDER_URI)); + String deleteFile = data.getString(DELETE_FILE); /* Operation */ - // InputStream - InputStream in = getContentResolver().openInputStream(providerUri); - long inLength = PGPMain.getLengthOfStream(in); - InputData inputData = new InputData(in, inLength); - - // OutputStream - String streamFilename = null; try { - while (true) { - streamFilename = PGPMain.generateRandomString(32); - if (streamFilename == null) { - throw new PGPMain.GeneralException("couldn't generate random file name"); - } - openFileInput(streamFilename).close(); - } + PGPMain.deleteFileSecurely(this, new File(deleteFile), this); } catch (FileNotFoundException e) { - // found a name that isn't used yet - } - FileOutputStream outStream = openFileOutput(streamFilename, Context.MODE_PRIVATE); - - Bundle resultData = new Bundle(); - - // verifyText and decrypt returning additional output values for the - // verification of signatures - if (signedOnly) { - resultData = PGPMain.verifyText(this, inputData, outStream, this); - } else { - resultData = PGPMain.decrypt(this, inputData, outStream, - PGPMain.getCachedPassPhrase(secretKeyId), this, - assumeSymmetricEncryption); + throw new PGPMain.GeneralException(getString(R.string.error_fileNotFound, + deleteFile)); + } catch (IOException e) { + throw new PGPMain.GeneralException(getString(R.string.error_fileDeleteFailed, + deleteFile)); } - outStream.close(); - /* Output */ - String uri = "content://" + DataProvider.AUTHORITY + "/data/" + streamFilename; - resultData.putString(RESULT_URI, uri); - - sendMessageToHandler(ApgHandler.MESSAGE_OKAY, resultData); + sendMessageToHandler(ApgHandler.MESSAGE_OKAY); } catch (Exception e) { sendErrorToHandler(e); } diff --git a/org_apg/src/org/thialfihar/android/apg/ui/BaseActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/BaseActivity.java index f8df4b1f2..888bb8eda 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/BaseActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/BaseActivity.java @@ -21,11 +21,11 @@ import java.io.File; import org.thialfihar.android.apg.R; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; -import org.thialfihar.android.apg.ProgressDialogUpdater; import org.thialfihar.android.apg.deprecated.AskForPassphrase; import org.thialfihar.android.apg.deprecated.PausableThread; import org.thialfihar.android.apg.helper.PGPMain; import org.thialfihar.android.apg.helper.Preferences; +import org.thialfihar.android.apg.util.ProgressDialogUpdater; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockFragmentActivity; diff --git a/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java index b491ca4c2..75bc04dee 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java @@ -677,13 +677,16 @@ public class DecryptActivity extends SherlockFragmentActivity { // fill values for this action Bundle data = new Bundle(); + intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_DECRYPT_VERIFY); + // choose action based on input: decrypt stream, file or bytes if (mContentUri != null) { - intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_DECRYPT_STREAM); + data.putInt(ApgService.TARGET, ApgService.TARGET_STREAM); + data.putString(ApgService.PROVIDER_URI, mContentUri.toString()); } else if (mDecryptTarget == Id.target.file) { - intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_DECRYPT_FILE); + data.putInt(ApgService.TARGET, ApgService.TARGET_FILE); Log.d(Constants.TAG, "mInputFilename=" + mInputFilename + ", mOutputFilename=" + mOutputFilename); @@ -692,7 +695,7 @@ public class DecryptActivity extends SherlockFragmentActivity { data.putString(ApgService.OUTPUT_FILE, mOutputFilename); } else { - intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_DECRYPT_BYTES); + data.putInt(ApgService.TARGET, ApgService.TARGET_BYTES); if (mData != null) { data.putByteArray(ApgService.CIPHERTEXT_BYTES, mData); diff --git a/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java index 80d135c6c..78aee8f2d 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java @@ -769,16 +769,18 @@ public class EncryptActivity extends SherlockFragmentActivity { signOnly = (mEncryptionKeyIds == null || mEncryptionKeyIds.length == 0); } - // choose default settings, action and data bundle by target + intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_ENCRYPT_SIGN); + + // choose default settings, target and data bundle by target if (mContentUri != null) { - intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_ENCRYPT_SIGN_STREAM); + data.putInt(ApgService.TARGET, ApgService.TARGET_STREAM); data.putString(ApgService.PROVIDER_URI, mContentUri.toString()); } else if (mEncryptTarget == Id.target.file) { useAsciiArmour = mAsciiArmour.isChecked(); compressionId = ((Choice) mFileCompression.getSelectedItem()).getId(); - intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_ENCRYPT_SIGN_FILE); + data.putInt(ApgService.TARGET, ApgService.TARGET_FILE); Log.d(Constants.TAG, "mInputFilename=" + mInputFilename + ", mOutputFilename=" + mOutputFilename); @@ -790,7 +792,7 @@ public class EncryptActivity extends SherlockFragmentActivity { useAsciiArmour = true; compressionId = Preferences.getPreferences(this).getDefaultMessageCompression(); - intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_ENCRYPT_SIGN_BYTES); + data.putInt(ApgService.TARGET, ApgService.TARGET_BYTES); if (mData != null) { data.putByteArray(ApgService.MESSAGE_BYTES, mData); diff --git a/org_apg/src/org/thialfihar/android/apg/ProgressDialogUpdater.java b/org_apg/src/org/thialfihar/android/apg/util/ProgressDialogUpdater.java index 55e177eef..1f76cb071 100644 --- a/org_apg/src/org/thialfihar/android/apg/ProgressDialogUpdater.java +++ b/org_apg/src/org/thialfihar/android/apg/util/ProgressDialogUpdater.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.thialfihar.android.apg; +package org.thialfihar.android.apg.util; public interface ProgressDialogUpdater { void setProgress(String message, int current, int total); |