Program: 8418
Project: 8418
Buggy source:
/** 
 * The IntentService calls this method from the default worker thread with the intent that started the service. When this method returns, IntentService stops the service, as appropriate.
 */
@Override protected void onHandleIntent(Intent intent){
  Bundle extras=intent.getExtras();
  if (extras == null) {
    Log.e(Constants.TAG,"String_Node_Str");
    return;
  }
  if (!(extras.containsKey(EXTRA_MESSENGER) || extras.containsKey(EXTRA_DATA) || extras.containsKey(EXTRA_ACTION))) {
    Log.e(Constants.TAG,"String_Node_Str");
    return;
  }
  mMessenger=(Messenger)extras.get(EXTRA_MESSENGER);
  Bundle data=extras.getBundle(EXTRA_DATA);
  OtherHelper.logDebugBundle(data,"String_Node_Str");
  int action=extras.getInt(EXTRA_ACTION);
switch (action) {
case ACTION_ENCRYPT_SIGN:
    try {
      int target=data.getInt(TARGET);
      long secretKeyId=data.getLong(SECRET_KEY_ID);
      String passphrase=data.getString(SYMMETRIC_PASSPHRASE);
      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 inStream=null;
      long inLength=-1;
      InputData inputData=null;
      OutputStream outStream=null;
      String streamFilename=null;
switch (target) {
case TARGET_BYTES:
        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:
String inputFile=data.getString(INPUT_FILE);
String outputFile=data.getString(OUTPUT_FILE);
if (!FileHelper.isStorageMounted(inputFile) || !FileHelper.isStorageMounted(outputFile)) {
sendErrorToHandler(new GeneralException(getString(R.string.error_externalStorageNotReady)));
return;
}
inStream=new FileInputStream(inputFile);
File file=new File(inputFile);
inLength=file.length();
inputData=new InputData(inStream,inLength);
outStream=new FileOutputStream(outputFile);
break;
case TARGET_STREAM:
Uri providerUri=(Uri)data.getParcelable(PROVIDER_URI);
InputStream in=getContentResolver().openInputStream(providerUri);
inLength=PGPMain.getLengthOfStream(in);
inputData=new InputData(in,inLength);
try {
while (true) {
streamFilename=PGPMain.generateRandomString(32);
if (streamFilename == null) {
throw new PGPMain.GeneralException("String_Node_Str");
}
openFileInput(streamFilename).close();
}
}
 catch (FileNotFoundException e) {
}
outStream=openFileOutput(streamFilename,Context.MODE_PRIVATE);
break;
default :
throw new PGPMain.GeneralException("String_Node_Str");
}
if (generateSignature) {
Log.d(Constants.TAG,"String_Node_Str");
PGPMain.generateSignature(this,inputData,outStream,useAsciiArmour,false,secretKeyId,PassphraseCacheService.getCachedPassphrase(this,secretKeyId),Preferences.getPreferences(this).getDefaultHashAlgorithm(),Preferences.getPreferences(this).getForceV3Signatures(),this);
}
 else if (signOnly) {
Log.d(Constants.TAG,"String_Node_Str");
PGPMain.signText(this,inputData,outStream,secretKeyId,PassphraseCacheService.getCachedPassphrase(this,secretKeyId),Preferences.getPreferences(this).getDefaultHashAlgorithm(),Preferences.getPreferences(this).getForceV3Signatures(),this);
}
 else {
Log.d(Constants.TAG,"String_Node_Str");
PGPMain.encrypt(this,inputData,outStream,useAsciiArmour,encryptionKeyIds,signatureKeyId,PassphraseCacheService.getCachedPassphrase(this,signatureKeyId),this,Preferences.getPreferences(this).getDefaultEncryptionAlgorithm(),Preferences.getPreferences(this).getDefaultHashAlgorithm(),compressionId,Preferences.getPreferences(this).getForceV3Signatures(),passphrase);
}
outStream.close();
Bundle resultData=new Bundle();
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 {
byte output[]=((ByteArrayOutputStream)outStream).toByteArray();
if (generateSignature) {
resultData.putByteArray(RESULT_SIGNATURE_DATA,output);
}
 else {
resultData.putByteArray(RESULT_ENCRYPTED_DATA,output);
}
}
break;
case TARGET_FILE:
break;
case TARGET_STREAM:
String uri="String_Node_Str" + DataProvider.AUTHORITY + "String_Node_Str"+ streamFilename;
resultData.putString(RESULT_URI,uri);
break;
}
OtherHelper.logDebugBundle(resultData,"String_Node_Str");
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_DECRYPT_VERIFY:
try {
int target=data.getInt(TARGET);
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);
boolean lookupUnknownKey=data.getBoolean(LOOKUP_UNKNOWN_KEY);
InputStream inStream=null;
long inLength=-1;
InputData inputData=null;
OutputStream outStream=null;
String streamFilename=null;
switch (target) {
case TARGET_BYTES:
inStream=new ByteArrayInputStream(bytes);
inLength=bytes.length;
inputData=new InputData(inStream,inLength);
outStream=new ByteArrayOutputStream();
break;
case TARGET_FILE:
String inputFile=data.getString(INPUT_FILE);
String outputFile=data.getString(OUTPUT_FILE);
if (!FileHelper.isStorageMounted(inputFile) || !FileHelper.isStorageMounted(outputFile)) {
sendErrorToHandler(new GeneralException(getString(R.string.error_externalStorageNotReady)));
return;
}
inLength=-1;
inStream=new FileInputStream(inputFile);
File file=new File(inputFile);
inLength=file.length();
inputData=new InputData(inStream,inLength);
outStream=new FileOutputStream(outputFile);
break;
case TARGET_STREAM:
Uri providerUri=(Uri)data.getParcelable(PROVIDER_URI);
InputStream in=getContentResolver().openInputStream(providerUri);
inLength=PGPMain.getLengthOfStream(in);
inputData=new InputData(in,inLength);
try {
while (true) {
streamFilename=PGPMain.generateRandomString(32);
if (streamFilename == null) {
throw new PGPMain.GeneralException("String_Node_Str");
}
openFileInput(streamFilename).close();
}
}
 catch (FileNotFoundException e) {
}
outStream=openFileOutput(streamFilename,Context.MODE_PRIVATE);
break;
default :
throw new PGPMain.GeneralException("String_Node_Str");
}
Bundle resultData=new Bundle();
if (signedOnly) {
resultData=PGPMain.verifyText(this,inputData,outStream,lookupUnknownKey,this);
}
 else {
resultData=PGPMain.decrypt(this,inputData,outStream,PassphraseCacheService.getCachedPassphrase(this,secretKeyId),this,assumeSymmetricEncryption);
}
outStream.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);
}
break;
case TARGET_FILE:
break;
case TARGET_STREAM:
String uri="String_Node_Str" + DataProvider.AUTHORITY + "String_Node_Str"+ streamFilename;
resultData.putString(RESULT_URI,uri);
break;
}
OtherHelper.logDebugBundle(resultData,"String_Node_Str");
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_SAVE_KEYRING:
try {
String oldPassPhrase=data.getString(CURRENT_PASSPHRASE);
String newPassPhrase=data.getString(NEW_PASSPHRASE);
if (newPassPhrase == null) {
newPassPhrase=oldPassPhrase;
}
@SuppressWarnings("String_Node_Str") ArrayList<String> userIds=(ArrayList<String>)data.getSerializable(USER_IDS);
ArrayList<PGPSecretKey> keys=PGPConversionHelper.BytesToPGPSecretKeyList(data.getByteArray(KEYS));
@SuppressWarnings("String_Node_Str") ArrayList<Integer> keysUsages=(ArrayList<Integer>)data.getSerializable(KEYS_USAGES);
long masterKeyId=data.getLong(MASTER_KEY_ID);
PGPMain.buildSecretKey(this,userIds,keys,keysUsages,masterKeyId,oldPassPhrase,newPassPhrase,this);
PassphraseCacheService.addCachedPassphrase(this,masterKeyId,newPassPhrase);
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_GENERATE_KEY:
try {
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));
}
PGPSecretKeyRing newKeyRing=PGPMain.createKey(this,algorithm,keysize,passphrase,masterKey);
Bundle resultData=new Bundle();
resultData.putByteArray(RESULT_NEW_KEY,PGPConversionHelper.PGPSecretKeyRingToBytes(newKeyRing));
OtherHelper.logDebugBundle(resultData,"String_Node_Str");
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_GENERATE_DEFAULT_RSA_KEYS:
try {
String passphrase=data.getString(SYMMETRIC_PASSPHRASE);
PGPSecretKeyRing masterKeyRing=PGPMain.createKey(this,Id.choice.algorithm.rsa,2048,passphrase,null);
PGPSecretKeyRing subKeyRing=PGPMain.createKey(this,Id.choice.algorithm.rsa,2048,passphrase,masterKeyRing.getSecretKey());
Bundle resultData=new Bundle();
resultData.putByteArray(RESULT_NEW_KEY,PGPConversionHelper.PGPSecretKeyRingToBytes(masterKeyRing));
resultData.putByteArray(RESULT_NEW_KEY2,PGPConversionHelper.PGPSecretKeyRingToBytes(subKeyRing));
OtherHelper.logDebugBundle(resultData,"String_Node_Str");
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_DELETE_FILE_SECURELY:
try {
String deleteFile=data.getString(DELETE_FILE);
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));
}
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_IMPORT_KEY:
try {
int target=data.getInt(TARGET);
int keyType=Id.type.public_key;
if (data.containsKey(IMPORT_KEY_TYPE)) {
keyType=data.getInt(IMPORT_KEY_TYPE);
}
InputStream inStream=null;
long inLength=-1;
InputData inputData=null;
switch (target) {
case TARGET_BYTES:
byte[] bytes=data.getByteArray(IMPORT_BYTES);
inStream=new ByteArrayInputStream(bytes);
inLength=bytes.length;
inputData=new InputData(inStream,inLength);
break;
case TARGET_FILE:
String inputFile=data.getString(IMPORT_FILENAME);
inStream=new FileInputStream(inputFile);
File file=new File(inputFile);
inLength=file.length();
inputData=new InputData(inStream,inLength);
break;
case TARGET_STREAM:
break;
}
Bundle resultData=new Bundle();
resultData=PGPMain.importKeyRings(this,keyType,inputData,this);
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_EXPORT_KEY:
try {
int keyType=Id.type.public_key;
if (data.containsKey(EXPORT_KEY_TYPE)) {
keyType=data.getInt(EXPORT_KEY_TYPE);
}
String outputFile=data.getString(EXPORT_FILENAME);
boolean exportAll=data.getBoolean(EXPORT_ALL);
int keyRingId=-1;
if (!exportAll) {
keyRingId=data.getInt(EXPORT_KEY_RING_ID);
}
if (!FileHelper.isStorageMounted(outputFile)) {
sendErrorToHandler(new GeneralException(getString(R.string.error_externalStorageNotReady)));
return;
}
FileOutputStream outStream=new FileOutputStream(outputFile);
Vector<Integer> keyRingIds=new Vector<Integer>();
if (exportAll) {
keyRingIds=PGPMain.getKeyRingIds(keyType == Id.type.public_key ? Id.database.type_public : Id.database.type_secret);
}
 else {
keyRingIds.add(keyRingId);
}
Bundle resultData=new Bundle();
resultData=PGPMain.exportKeyRings(this,keyRingIds,outStream,this);
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_UPLOAD_KEY:
try {
int keyRingId=data.getInt(UPLOAD_KEY_KEYRING_ID);
String keyServer=data.getString(UPLOAD_KEY_SERVER);
HkpKeyServer server=new HkpKeyServer(keyServer);
PGPKeyRing keyring=PGPMain.getKeyRing(keyRingId);
if (keyring != null && keyring instanceof PGPPublicKeyRing) {
boolean uploaded=PGPMain.uploadKeyRingToServer(server,(PGPPublicKeyRing)keyring);
if (!uploaded) {
sendErrorToHandler(new GeneralException("String_Node_Str"));
return;
}
}
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_QUERY_KEY:
try {
int queryType=data.getInt(QUERY_KEY_TYPE);
String keyServer=data.getString(QUERY_KEY_SERVER);
String queryString=data.getString(QUERY_KEY_STRING);
long keyId=data.getLong(QUERY_KEY_ID);
Bundle resultData=new Bundle();
HkpKeyServer server=new HkpKeyServer(keyServer);
if (queryType == Id.keyserver.search) {
ArrayList<KeyInfo> searchResult=server.search(queryString);
resultData.putParcelableArrayList(RESULT_QUERY_KEY_SEARCH_RESULT,searchResult);
}
 else if (queryType == Id.keyserver.get) {
String keyData=server.get(keyId);
resultData.putString(RESULT_QUERY_KEY_KEY_DATA,keyData);
}
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_SIGN_KEY:
try {
long masterKeyId=data.getLong(SIGN_KEY_MASTER_KEY_ID);
long pubKeyId=data.getLong(SIGN_KEY_PUB_KEY_ID);
String signaturePassPhrase=PassphraseCacheService.getCachedPassphrase(this,masterKeyId);
PGPPublicKeyRing signedPubKeyRing=PGPMain.signKey(this,masterKeyId,pubKeyId,signaturePassPhrase);
int retval=PGPMain.storeKeyRingInCache(signedPubKeyRing);
if (retval != Id.return_value.ok && retval != Id.return_value.updated) {
throw new GeneralException("String_Node_Str");
}
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
default :
break;
}
}

Fixed version (ground truth):
/** 
 * The IntentService calls this method from the default worker thread with the intent that started the service. When this method returns, IntentService stops the service, as appropriate.
 */
@Override protected void onHandleIntent(Intent intent){
  Bundle extras=intent.getExtras();
  if (extras == null) {
    Log.e(Constants.TAG,"String_Node_Str");
    return;
  }
  if (!(extras.containsKey(EXTRA_MESSENGER) || extras.containsKey(EXTRA_DATA) || extras.containsKey(EXTRA_ACTION))) {
    Log.e(Constants.TAG,"String_Node_Str");
    return;
  }
  mMessenger=(Messenger)extras.get(EXTRA_MESSENGER);
  Bundle data=extras.getBundle(EXTRA_DATA);
  OtherHelper.logDebugBundle(data,"String_Node_Str");
  int action=extras.getInt(EXTRA_ACTION);
switch (action) {
case ACTION_ENCRYPT_SIGN:
    try {
      int target=data.getInt(TARGET);
      long secretKeyId=data.getLong(SECRET_KEY_ID);
      String passphrase=data.getString(SYMMETRIC_PASSPHRASE);
      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 inStream=null;
      long inLength=-1;
      InputData inputData=null;
      OutputStream outStream=null;
      String streamFilename=null;
switch (target) {
case TARGET_BYTES:
        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:
String inputFile=data.getString(INPUT_FILE);
String outputFile=data.getString(OUTPUT_FILE);
if (!FileHelper.isStorageMounted(inputFile) || !FileHelper.isStorageMounted(outputFile)) {
sendErrorToHandler(new GeneralException(getString(R.string.error_externalStorageNotReady)));
return;
}
inStream=new FileInputStream(inputFile);
File file=new File(inputFile);
inLength=file.length();
inputData=new InputData(inStream,inLength);
outStream=new FileOutputStream(outputFile);
break;
case TARGET_STREAM:
Uri providerUri=(Uri)data.getParcelable(PROVIDER_URI);
InputStream in=getContentResolver().openInputStream(providerUri);
inLength=PGPMain.getLengthOfStream(in);
inputData=new InputData(in,inLength);
try {
while (true) {
streamFilename=PGPMain.generateRandomString(32);
if (streamFilename == null) {
throw new PGPMain.GeneralException("String_Node_Str");
}
openFileInput(streamFilename).close();
}
}
 catch (FileNotFoundException e) {
}
outStream=openFileOutput(streamFilename,Context.MODE_PRIVATE);
break;
default :
throw new PGPMain.GeneralException("String_Node_Str");
}
if (generateSignature) {
Log.d(Constants.TAG,"String_Node_Str");
PGPMain.generateSignature(this,inputData,outStream,useAsciiArmour,false,secretKeyId,PassphraseCacheService.getCachedPassphrase(this,secretKeyId),Preferences.getPreferences(this).getDefaultHashAlgorithm(),Preferences.getPreferences(this).getForceV3Signatures(),this);
}
 else if (signOnly) {
Log.d(Constants.TAG,"String_Node_Str");
PGPMain.signText(this,inputData,outStream,secretKeyId,PassphraseCacheService.getCachedPassphrase(this,secretKeyId),Preferences.getPreferences(this).getDefaultHashAlgorithm(),Preferences.getPreferences(this).getForceV3Signatures(),this);
}
 else {
Log.d(Constants.TAG,"String_Node_Str");
PGPMain.encrypt(this,inputData,outStream,useAsciiArmour,encryptionKeyIds,signatureKeyId,PassphraseCacheService.getCachedPassphrase(this,signatureKeyId),this,Preferences.getPreferences(this).getDefaultEncryptionAlgorithm(),Preferences.getPreferences(this).getDefaultHashAlgorithm(),compressionId,Preferences.getPreferences(this).getForceV3Signatures(),passphrase);
}
outStream.close();
Bundle resultData=new Bundle();
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 {
byte output[]=((ByteArrayOutputStream)outStream).toByteArray();
if (generateSignature) {
resultData.putByteArray(RESULT_SIGNATURE_DATA,output);
}
 else {
resultData.putByteArray(RESULT_ENCRYPTED_DATA,output);
}
}
break;
case TARGET_FILE:
break;
case TARGET_STREAM:
String uri="String_Node_Str" + DataProvider.AUTHORITY + "String_Node_Str"+ streamFilename;
resultData.putString(RESULT_URI,uri);
break;
}
OtherHelper.logDebugBundle(resultData,"String_Node_Str");
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_DECRYPT_VERIFY:
try {
int target=data.getInt(TARGET);
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);
boolean lookupUnknownKey=data.getBoolean(LOOKUP_UNKNOWN_KEY);
InputStream inStream=null;
long inLength=-1;
InputData inputData=null;
OutputStream outStream=null;
String streamFilename=null;
switch (target) {
case TARGET_BYTES:
inStream=new ByteArrayInputStream(bytes);
inLength=bytes.length;
inputData=new InputData(inStream,inLength);
outStream=new ByteArrayOutputStream();
break;
case TARGET_FILE:
String inputFile=data.getString(INPUT_FILE);
String outputFile=data.getString(OUTPUT_FILE);
if (!FileHelper.isStorageMounted(inputFile) || !FileHelper.isStorageMounted(outputFile)) {
sendErrorToHandler(new GeneralException(getString(R.string.error_externalStorageNotReady)));
return;
}
inLength=-1;
inStream=new FileInputStream(inputFile);
File file=new File(inputFile);
inLength=file.length();
inputData=new InputData(inStream,inLength);
outStream=new FileOutputStream(outputFile);
break;
case TARGET_STREAM:
Uri providerUri=(Uri)data.getParcelable(PROVIDER_URI);
InputStream in=getContentResolver().openInputStream(providerUri);
inLength=PGPMain.getLengthOfStream(in);
inputData=new InputData(in,inLength);
try {
while (true) {
streamFilename=PGPMain.generateRandomString(32);
if (streamFilename == null) {
throw new PGPMain.GeneralException("String_Node_Str");
}
openFileInput(streamFilename).close();
}
}
 catch (FileNotFoundException e) {
}
outStream=openFileOutput(streamFilename,Context.MODE_PRIVATE);
break;
default :
throw new PGPMain.GeneralException("String_Node_Str");
}
Bundle resultData=new Bundle();
if (signedOnly) {
resultData=PGPMain.verifyText(this,inputData,outStream,lookupUnknownKey,this);
}
 else {
resultData=PGPMain.decrypt(this,inputData,outStream,PassphraseCacheService.getCachedPassphrase(this,secretKeyId),this,assumeSymmetricEncryption);
}
outStream.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);
}
break;
case TARGET_FILE:
break;
case TARGET_STREAM:
String uri="String_Node_Str" + DataProvider.AUTHORITY + "String_Node_Str"+ streamFilename;
resultData.putString(RESULT_URI,uri);
break;
}
OtherHelper.logDebugBundle(resultData,"String_Node_Str");
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_SAVE_KEYRING:
try {
String oldPassPhrase=data.getString(CURRENT_PASSPHRASE);
String newPassPhrase=data.getString(NEW_PASSPHRASE);
if (newPassPhrase == null) {
newPassPhrase=oldPassPhrase;
}
ArrayList<String> userIds=data.getStringArrayList(USER_IDS);
ArrayList<PGPSecretKey> keys=PGPConversionHelper.BytesToPGPSecretKeyList(data.getByteArray(KEYS));
ArrayList<Integer> keysUsages=data.getIntegerArrayList(KEYS_USAGES);
long masterKeyId=data.getLong(MASTER_KEY_ID);
PGPMain.buildSecretKey(this,userIds,keys,keysUsages,masterKeyId,oldPassPhrase,newPassPhrase,this);
PassphraseCacheService.addCachedPassphrase(this,masterKeyId,newPassPhrase);
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_GENERATE_KEY:
try {
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));
}
PGPSecretKeyRing newKeyRing=PGPMain.createKey(this,algorithm,keysize,passphrase,masterKey);
Bundle resultData=new Bundle();
resultData.putByteArray(RESULT_NEW_KEY,PGPConversionHelper.PGPSecretKeyRingToBytes(newKeyRing));
OtherHelper.logDebugBundle(resultData,"String_Node_Str");
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_GENERATE_DEFAULT_RSA_KEYS:
try {
String passphrase=data.getString(SYMMETRIC_PASSPHRASE);
PGPSecretKeyRing masterKeyRing=PGPMain.createKey(this,Id.choice.algorithm.rsa,2048,passphrase,null);
PGPSecretKeyRing subKeyRing=PGPMain.createKey(this,Id.choice.algorithm.rsa,2048,passphrase,masterKeyRing.getSecretKey());
Bundle resultData=new Bundle();
resultData.putByteArray(RESULT_NEW_KEY,PGPConversionHelper.PGPSecretKeyRingToBytes(masterKeyRing));
resultData.putByteArray(RESULT_NEW_KEY2,PGPConversionHelper.PGPSecretKeyRingToBytes(subKeyRing));
OtherHelper.logDebugBundle(resultData,"String_Node_Str");
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_DELETE_FILE_SECURELY:
try {
String deleteFile=data.getString(DELETE_FILE);
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));
}
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_IMPORT_KEY:
try {
int target=data.getInt(TARGET);
int keyType=Id.type.public_key;
if (data.containsKey(IMPORT_KEY_TYPE)) {
keyType=data.getInt(IMPORT_KEY_TYPE);
}
InputStream inStream=null;
long inLength=-1;
InputData inputData=null;
switch (target) {
case TARGET_BYTES:
byte[] bytes=data.getByteArray(IMPORT_BYTES);
inStream=new ByteArrayInputStream(bytes);
inLength=bytes.length;
inputData=new InputData(inStream,inLength);
break;
case TARGET_FILE:
String inputFile=data.getString(IMPORT_FILENAME);
inStream=new FileInputStream(inputFile);
File file=new File(inputFile);
inLength=file.length();
inputData=new InputData(inStream,inLength);
break;
case TARGET_STREAM:
break;
}
Bundle resultData=new Bundle();
resultData=PGPMain.importKeyRings(this,keyType,inputData,this);
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_EXPORT_KEY:
try {
int keyType=Id.type.public_key;
if (data.containsKey(EXPORT_KEY_TYPE)) {
keyType=data.getInt(EXPORT_KEY_TYPE);
}
String outputFile=data.getString(EXPORT_FILENAME);
boolean exportAll=data.getBoolean(EXPORT_ALL);
int keyRingId=-1;
if (!exportAll) {
keyRingId=data.getInt(EXPORT_KEY_RING_ID);
}
if (!FileHelper.isStorageMounted(outputFile)) {
sendErrorToHandler(new GeneralException(getString(R.string.error_externalStorageNotReady)));
return;
}
FileOutputStream outStream=new FileOutputStream(outputFile);
Vector<Integer> keyRingIds=new Vector<Integer>();
if (exportAll) {
keyRingIds=PGPMain.getKeyRingIds(keyType == Id.type.public_key ? Id.database.type_public : Id.database.type_secret);
}
 else {
keyRingIds.add(keyRingId);
}
Bundle resultData=new Bundle();
resultData=PGPMain.exportKeyRings(this,keyRingIds,outStream,this);
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_UPLOAD_KEY:
try {
int keyRingId=data.getInt(UPLOAD_KEY_KEYRING_ID);
String keyServer=data.getString(UPLOAD_KEY_SERVER);
HkpKeyServer server=new HkpKeyServer(keyServer);
PGPKeyRing keyring=PGPMain.getKeyRing(keyRingId);
if (keyring != null && keyring instanceof PGPPublicKeyRing) {
boolean uploaded=PGPMain.uploadKeyRingToServer(server,(PGPPublicKeyRing)keyring);
if (!uploaded) {
sendErrorToHandler(new GeneralException("String_Node_Str"));
return;
}
}
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_QUERY_KEY:
try {
int queryType=data.getInt(QUERY_KEY_TYPE);
String keyServer=data.getString(QUERY_KEY_SERVER);
String queryString=data.getString(QUERY_KEY_STRING);
long keyId=data.getLong(QUERY_KEY_ID);
Bundle resultData=new Bundle();
HkpKeyServer server=new HkpKeyServer(keyServer);
if (queryType == Id.keyserver.search) {
ArrayList<KeyInfo> searchResult=server.search(queryString);
resultData.putParcelableArrayList(RESULT_QUERY_KEY_SEARCH_RESULT,searchResult);
}
 else if (queryType == Id.keyserver.get) {
String keyData=server.get(keyId);
resultData.putString(RESULT_QUERY_KEY_KEY_DATA,keyData);
}
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY,resultData);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
case ACTION_SIGN_KEY:
try {
long masterKeyId=data.getLong(SIGN_KEY_MASTER_KEY_ID);
long pubKeyId=data.getLong(SIGN_KEY_PUB_KEY_ID);
String signaturePassPhrase=PassphraseCacheService.getCachedPassphrase(this,masterKeyId);
PGPPublicKeyRing signedPubKeyRing=PGPMain.signKey(this,masterKeyId,pubKeyId,signaturePassPhrase);
int retval=PGPMain.storeKeyRingInCache(signedPubKeyRing);
if (retval != Id.return_value.ok && retval != Id.return_value.updated) {
throw new GeneralException("String_Node_Str");
}
sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY);
}
 catch (Exception e) {
sendErrorToHandler(e);
}
break;
default :
break;
}
}