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-11 17:58:13 +00:00
|
|
|
import java.lang.reflect.Method;
|
2011-01-05 14:07:09 +00:00
|
|
|
import java.util.ArrayList;
|
2011-01-09 19:16:45 +00:00
|
|
|
import java.util.HashMap;
|
2011-01-05 14:07:09 +00:00
|
|
|
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 {
|
2011-01-09 19:16:45 +00:00
|
|
|
final static String TAG = "ApgService";
|
2010-12-29 16:31:58 +00:00
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-09 19:16:45 +00:00
|
|
|
/** a map from ApgService parameters to function calls to get the default */
|
|
|
|
|
static final HashMap<String, String> FUNCTIONS_DEFAULTS = new HashMap<String, String>();
|
|
|
|
|
static {
|
|
|
|
|
FUNCTIONS_DEFAULTS.put("ENCRYPTION_ALGO", "getDefaultEncryptionAlgorithm");
|
|
|
|
|
FUNCTIONS_DEFAULTS.put("HASH_ALGO", "getDefaultHashAlgorithm");
|
|
|
|
|
FUNCTIONS_DEFAULTS.put("ARMORED", "getDefaultAsciiArmour");
|
|
|
|
|
FUNCTIONS_DEFAULTS.put("FORCE_V3_SIG", "getForceV3Signatures");
|
|
|
|
|
FUNCTIONS_DEFAULTS.put("COMPRESSION", "getDefaultMessageCompression");
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-11 17:58:13 +00:00
|
|
|
/** a map the default functions to their return types */
|
|
|
|
|
static final HashMap<String, Class> FUNCTIONS_DEFAULTS_TYPES = new HashMap<String, Class>();
|
|
|
|
|
static {
|
|
|
|
|
try {
|
|
|
|
|
FUNCTIONS_DEFAULTS_TYPES.put("getDefaultEncryptionAlgorithm", Preferences.class.getMethod("getDefaultEncryptionAlgorithm").getReturnType());
|
|
|
|
|
FUNCTIONS_DEFAULTS_TYPES.put("getDefaultHashAlgorithm", Preferences.class.getMethod("getDefaultHashAlgorithm").getReturnType());
|
|
|
|
|
FUNCTIONS_DEFAULTS_TYPES.put("getDefaultAsciiArmour", Preferences.class.getMethod("getDefaultAsciiArmour").getReturnType());
|
|
|
|
|
FUNCTIONS_DEFAULTS_TYPES.put("getForceV3Signatures", Preferences.class.getMethod("getForceV3Signatures").getReturnType());
|
|
|
|
|
FUNCTIONS_DEFAULTS_TYPES.put("getDefaultMessageCompression", Preferences.class.getMethod("getDefaultMessageCompression").getReturnType());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Function default exception: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** a map the default function names to their method */
|
|
|
|
|
static final HashMap<String, Method> FUNCTIONS_DEFAULTS_METHODS = new HashMap<String, Method>();
|
|
|
|
|
static {
|
|
|
|
|
try {
|
|
|
|
|
FUNCTIONS_DEFAULTS_METHODS.put("getDefaultEncryptionAlgorithm", Preferences.class.getMethod("getDefaultEncryptionAlgorithm"));
|
|
|
|
|
FUNCTIONS_DEFAULTS_METHODS.put("getDefaultHashAlgorithm", Preferences.class.getMethod("getDefaultHashAlgorithm"));
|
|
|
|
|
FUNCTIONS_DEFAULTS_METHODS.put("getDefaultAsciiArmour", Preferences.class.getMethod("getDefaultAsciiArmour"));
|
|
|
|
|
FUNCTIONS_DEFAULTS_METHODS.put("getForceV3Signatures", Preferences.class.getMethod("getForceV3Signatures"));
|
|
|
|
|
FUNCTIONS_DEFAULTS_METHODS.put("getDefaultMessageCompression", Preferences.class.getMethod("getDefaultMessageCompression"));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Function method exception: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-09 19:16:45 +00:00
|
|
|
/**
|
|
|
|
|
* Add default arguments if missing
|
|
|
|
|
*
|
|
|
|
|
* @param args
|
|
|
|
|
* the bundle to add default parameters to if missing
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
private void add_defaults(Bundle args) {
|
|
|
|
|
Preferences _mPreferences = Preferences.getPreferences(getBaseContext(), true);
|
|
|
|
|
|
|
|
|
|
Iterator<String> _iter = FUNCTIONS_DEFAULTS.keySet().iterator();
|
|
|
|
|
while (_iter.hasNext()) {
|
|
|
|
|
String _current_key = _iter.next();
|
|
|
|
|
if (!args.containsKey(_current_key)) {
|
|
|
|
|
String _current_function_name = FUNCTIONS_DEFAULTS.get(_current_key);
|
|
|
|
|
try {
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2011-01-11 17:58:13 +00:00
|
|
|
Class _ret_type = FUNCTIONS_DEFAULTS_TYPES.get(_current_function_name);
|
2011-01-09 19:16:45 +00:00
|
|
|
if (_ret_type == String.class) {
|
2011-01-11 17:58:13 +00:00
|
|
|
args.putString(_current_key, (String) FUNCTIONS_DEFAULTS_METHODS.get(_current_function_name).invoke(_mPreferences));
|
2011-01-09 19:16:45 +00:00
|
|
|
} else if (_ret_type == boolean.class) {
|
2011-01-11 17:58:13 +00:00
|
|
|
args.putBoolean(_current_key, (Boolean) FUNCTIONS_DEFAULTS_METHODS.get(_current_function_name).invoke(_mPreferences));
|
2011-01-09 19:16:45 +00:00
|
|
|
} else if (_ret_type == int.class) {
|
2011-01-11 17:58:13 +00:00
|
|
|
args.putInt(_current_key, (Integer) FUNCTIONS_DEFAULTS_METHODS.get(_current_function_name).invoke(_mPreferences));
|
2011-01-09 19:16:45 +00:00
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Unknown return type " + _ret_type.toString() + " for default option");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Exception in add_defaults " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2011-01-09 19:16:45 +00:00
|
|
|
|
2011-01-05 14:07:09 +00:00
|
|
|
ArrayList<String> errors = new ArrayList<String>();
|
|
|
|
|
ArrayList<String> warnings = new ArrayList<String>();
|
|
|
|
|
|
|
|
|
|
pReturn.putStringArrayList("ERRORS", errors);
|
|
|
|
|
pReturn.putStringArrayList("WARNINGS", warnings);
|
|
|
|
|
|
2011-01-09 19:16:45 +00:00
|
|
|
Bundle _my_args = new Bundle(pArgs);
|
|
|
|
|
|
|
|
|
|
/* add default values if missing */
|
|
|
|
|
add_defaults(_my_args);
|
|
|
|
|
|
|
|
|
|
/* required args */
|
|
|
|
|
String msg = _my_args.getString("MSG");
|
|
|
|
|
_my_args.remove("MSG");
|
|
|
|
|
|
|
|
|
|
String passphrase = _my_args.getString("SYM_KEY");
|
|
|
|
|
_my_args.remove("SYM_KEY");
|
|
|
|
|
|
|
|
|
|
/* optional args */
|
|
|
|
|
Boolean armored = _my_args.getBoolean("ARMORED");
|
|
|
|
|
_my_args.remove("ARMORED");
|
|
|
|
|
|
|
|
|
|
int encryption_algorithm = _my_args.getInt("ENCRYPTION_ALGO");
|
|
|
|
|
_my_args.remove("ENCRYPTION_ALGO");
|
|
|
|
|
|
|
|
|
|
int hash_algorithm = _my_args.getInt("HASH_ALGO");
|
|
|
|
|
_my_args.remove("HASH_ALGO");
|
|
|
|
|
|
|
|
|
|
int compression = _my_args.getInt("COMPRESSION");
|
|
|
|
|
_my_args.remove("COMPRESSION");
|
2011-01-05 14:07:09 +00:00
|
|
|
|
2011-01-09 19:16:45 +00:00
|
|
|
Boolean force_v3_signatures = _my_args.getBoolean("FORCE_V3_SIG");
|
|
|
|
|
_my_args.remove("FORCE_V3_SIG");
|
2011-01-05 14:07:09 +00:00
|
|
|
|
2011-01-09 19:16:45 +00:00
|
|
|
/* check required args */
|
2011-01-05 14:07:09 +00:00
|
|
|
if (msg == null) {
|
|
|
|
|
errors.add("Message to encrypt (MSG) missing");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (passphrase == null) {
|
|
|
|
|
errors.add("Symmetric key (SYM_KEY) missing");
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-09 19:16:45 +00:00
|
|
|
/* check for unknown args and add to warning */
|
|
|
|
|
if (!_my_args.isEmpty()) {
|
|
|
|
|
Iterator<String> _iter = _my_args.keySet().iterator();
|
2011-01-09 19:16:22 +00:00
|
|
|
while (_iter.hasNext()) {
|
|
|
|
|
warnings.add("Unknown key: " + _iter.next());
|
2011-01-05 14:07:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-09 19:16:45 +00:00
|
|
|
/* return if errors happened */
|
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
|
|
|
InputStream _inStream = new ByteArrayInputStream(msg.getBytes());
|
2011-01-11 17:58:13 +00:00
|
|
|
InputData _in = new InputData(_inStream, 0); // XXX Size second
|
|
|
|
|
// param?
|
2011-01-09 19:16:22 +00:00
|
|
|
OutputStream _out = new ByteArrayOutputStream();
|
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
|
2011-01-09 19:16:45 +00:00
|
|
|
armored, // armored
|
|
|
|
|
new long[0], // encryption keys
|
2010-12-29 16:31:58 +00:00
|
|
|
0, // signature key
|
|
|
|
|
null, // signature passphrase
|
|
|
|
|
null, // progress
|
2011-01-09 19:16:45 +00:00
|
|
|
encryption_algorithm, // encryption
|
|
|
|
|
hash_algorithm, // hash
|
|
|
|
|
compression, // compression
|
|
|
|
|
force_v3_signatures, // 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());
|
2011-01-11 17:58:13 +00:00
|
|
|
InputData in = new InputData(inStream, 0); // 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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|