2010-12-29 16:31:58 +00:00
|
|
|
package org.thialfihar.android.apg;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
2011-01-05 14:07:09 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Iterator;
|
2010-12-29 16:31:58 +00:00
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2011-01-05 14:07:09 +00:00
|
|
|
import android.os.Bundle;
|
2010-12-29 16:31:58 +00:00
|
|
|
import android.os.IBinder;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
public class ApgService extends Service {
|
|
|
|
|
static String TAG = "ApgService";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
|
Log.d(TAG, "bound");
|
|
|
|
|
return mBinder;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-05 14:07:09 +00:00
|
|
|
private enum error {
|
|
|
|
|
ARGUMENTS_MISSING,
|
|
|
|
|
APG_FAILURE
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-29 16:31:58 +00:00
|
|
|
private final IApgService.Stub mBinder = new IApgService.Stub() {
|
|
|
|
|
|
2011-01-05 14:07:09 +00:00
|
|
|
public boolean encrypt_with_passphrase(Bundle pArgs, Bundle pReturn) {
|
|
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
ArrayList<String> warnings = new ArrayList<String>();
|
|
|
|
|
|
|
|
|
|
pReturn.putStringArrayList("ERRORS", errors);
|
|
|
|
|
pReturn.putStringArrayList("WARNINGS", warnings);
|
|
|
|
|
|
|
|
|
|
String msg = pArgs.getString("MSG");
|
|
|
|
|
pArgs.remove("MSG");
|
|
|
|
|
|
|
|
|
|
String passphrase = pArgs.getString("SYM_KEY");
|
|
|
|
|
pArgs.remove("SYM_KEY");
|
|
|
|
|
|
|
|
|
|
if (msg == null) {
|
|
|
|
|
errors.add("Message to encrypt (MSG) missing");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (passphrase == null) {
|
|
|
|
|
errors.add("Symmetric key (SYM_KEY) missing");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!pArgs.isEmpty()) {
|
2011-01-09 19:16:22 +00:00
|
|
|
Iterator<String> _iter = pArgs.keySet().iterator();
|
|
|
|
|
while (_iter.hasNext()) {
|
|
|
|
|
warnings.add("Unknown key: " + _iter.next());
|
2011-01-05 14:07:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errors.size() != 0) {
|
|
|
|
|
pReturn.putInt("ERROR", error.ARGUMENTS_MISSING.ordinal());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-01-04 23:08:08 +00:00
|
|
|
|
2011-01-09 19:16:22 +00:00
|
|
|
Preferences _mPreferences = Preferences.getPreferences(getBaseContext(), true);
|
|
|
|
|
InputStream _inStream = new ByteArrayInputStream(msg.getBytes());
|
|
|
|
|
InputData _in = new InputData(_inStream, 9999);
|
|
|
|
|
OutputStream _out = new ByteArrayOutputStream();
|
|
|
|
|
long _enc_keys[] = {};
|
2010-12-29 16:31:58 +00:00
|
|
|
|
|
|
|
|
Apg.initialize(getApplicationContext());
|
|
|
|
|
try {
|
2011-01-04 23:08:08 +00:00
|
|
|
Apg.encrypt(getApplicationContext(), // context
|
2011-01-09 19:16:22 +00:00
|
|
|
_in, // input stream
|
|
|
|
|
_out, // output stream
|
2010-12-29 16:31:58 +00:00
|
|
|
true, // armored
|
2011-01-09 19:16:22 +00:00
|
|
|
_enc_keys, // encryption keys
|
2010-12-29 16:31:58 +00:00
|
|
|
0, // signature key
|
|
|
|
|
null, // signature passphrase
|
|
|
|
|
null, // progress
|
2011-01-09 19:16:22 +00:00
|
|
|
_mPreferences.getDefaultEncryptionAlgorithm(), // encryption
|
|
|
|
|
_mPreferences.getDefaultHashAlgorithm(), // hash
|
2010-12-30 13:48:24 +00:00
|
|
|
Id.choice.compression.none, // compression
|
|
|
|
|
false, // mPreferences.getForceV3Signatures(),
|
2010-12-29 16:31:58 +00:00
|
|
|
passphrase // passPhrase
|
|
|
|
|
);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.d(TAG, "Exception in encrypt");
|
2011-01-05 14:07:09 +00:00
|
|
|
errors.add("Internal failure in APG when encrypting: " + e.getMessage());
|
|
|
|
|
|
|
|
|
|
pReturn.putInt("ERROR", error.APG_FAILURE.ordinal());
|
|
|
|
|
return false;
|
2010-12-29 16:31:58 +00:00
|
|
|
}
|
2011-01-05 14:07:09 +00:00
|
|
|
|
2010-12-29 16:31:58 +00:00
|
|
|
Log.d(TAG, "Encrypted");
|
2011-01-09 19:16:22 +00:00
|
|
|
pReturn.putString("RESULT", _out.toString());
|
2011-01-05 14:07:09 +00:00
|
|
|
return true;
|
2010-12-29 16:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-05 14:07:09 +00:00
|
|
|
public boolean decrypt_with_passphrase(Bundle pArgs, Bundle pReturn) {
|
|
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
ArrayList<String> warnings = new ArrayList<String>();
|
|
|
|
|
|
|
|
|
|
pReturn.putStringArrayList("ERRORS", errors);
|
|
|
|
|
pReturn.putStringArrayList("WARNINGS", warnings);
|
|
|
|
|
|
|
|
|
|
String encrypted_msg = pArgs.getString("MSG");
|
|
|
|
|
pArgs.remove("MSG");
|
2011-01-09 19:16:22 +00:00
|
|
|
|
2011-01-05 14:07:09 +00:00
|
|
|
String passphrase = pArgs.getString("SYM_KEY");
|
|
|
|
|
pArgs.remove("SYM_KEY");
|
|
|
|
|
|
|
|
|
|
if (encrypted_msg == null) {
|
|
|
|
|
errors.add("Message to decrypt (MSG) missing");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (passphrase == null) {
|
|
|
|
|
errors.add("Symmetric key (SYM_KEY) missing");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!pArgs.isEmpty()) {
|
|
|
|
|
Iterator<String> iter = pArgs.keySet().iterator();
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
warnings.add("Unknown key: " + iter.next());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errors.size() != 0) {
|
|
|
|
|
pReturn.putStringArrayList("ERROR", errors);
|
|
|
|
|
pReturn.putInt("ERROR", error.ARGUMENTS_MISSING.ordinal());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-01-04 23:08:08 +00:00
|
|
|
|
|
|
|
|
InputStream inStream = new ByteArrayInputStream(encrypted_msg.getBytes());
|
2010-12-29 16:31:58 +00:00
|
|
|
InputData in = new InputData(inStream, 9999); // XXX what size in
|
2011-01-04 23:08:08 +00:00
|
|
|
// second parameter?
|
2010-12-29 16:31:58 +00:00
|
|
|
OutputStream out = new ByteArrayOutputStream();
|
|
|
|
|
try {
|
|
|
|
|
Apg.decrypt(getApplicationContext(), in, out, passphrase, null, // progress
|
|
|
|
|
true // symmetric
|
|
|
|
|
);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.d(TAG, "Exception in decrypt");
|
2011-01-05 14:07:09 +00:00
|
|
|
errors.add("Internal failure in APG when decrypting: " + e.getMessage());
|
|
|
|
|
|
|
|
|
|
pReturn.putInt("ERROR", error.APG_FAILURE.ordinal());
|
|
|
|
|
pReturn.putStringArrayList("ERROR", errors);
|
|
|
|
|
return false;
|
2010-12-29 16:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-05 14:07:09 +00:00
|
|
|
pReturn.putString("RESULT", out.toString());
|
|
|
|
|
return true;
|
2010-12-29 16:31:58 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|