private request codes, pass params through methods and pending intents, getKeyIds method

This commit is contained in:
Dominik Schürmann 2014-02-15 01:06:03 +01:00
parent 21ba41edae
commit 494a5fa414
8 changed files with 220 additions and 135 deletions

View File

@ -159,7 +159,7 @@ public class OpenPgpProviderActivity extends Activity {
mCiphertext.setText(os.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
break;
}
@ -170,7 +170,7 @@ public class OpenPgpProviderActivity extends Activity {
REQUEST_CODE_SIGN, null,
0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.e(Constants.TAG, "SendIntentException", e);
}
break;
}
@ -198,7 +198,7 @@ public class OpenPgpProviderActivity extends Activity {
mCiphertext.setText(os.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
break;
}
@ -209,7 +209,7 @@ public class OpenPgpProviderActivity extends Activity {
REQUEST_CODE_ENCRYPT, null,
0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.e(Constants.TAG, "SendIntentException", e);
}
break;
}
@ -237,7 +237,7 @@ public class OpenPgpProviderActivity extends Activity {
mCiphertext.setText(os.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
break;
}
@ -248,7 +248,7 @@ public class OpenPgpProviderActivity extends Activity {
REQUEST_CODE_SIGN_AND_ENCRYPT, null,
0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.e(Constants.TAG, "SendIntentException", e);
}
break;
}
@ -275,7 +275,7 @@ public class OpenPgpProviderActivity extends Activity {
mMessage.setText(os.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
break;
}
@ -286,7 +286,7 @@ public class OpenPgpProviderActivity extends Activity {
REQUEST_CODE_DECRYPT_AND_VERIFY, null,
0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.e(Constants.TAG, "SendIntentException", e);
}
break;
}
@ -297,7 +297,8 @@ public class OpenPgpProviderActivity extends Activity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
Log.d(Constants.TAG, "onActivityResult");
switch (requestCode) {
case REQUEST_CODE_SIGN: {
@ -305,7 +306,7 @@ public class OpenPgpProviderActivity extends Activity {
// try to sign again after password caching
if (resultCode == RESULT_OK) {
sign(new Bundle());
sign(data.getExtras());
}
break;
}

View File

@ -64,4 +64,15 @@ interface IOpenPgpService {
*/
Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output);
/**
* Retrieves key ids based on given user ids (=emails)
*
* params:
* String[] user_ids
*
* result:
* long[] key_ids
*/
Bundle getKeyIds(in Bundle params);
}

View File

@ -35,6 +35,7 @@ public class OpenPgpApi {
private static final int OPERATION_ENCRYPT = 1;
private static final int OPERATION_SIGN_ENCRYPT = 2;
private static final int OPERATION_DECRYPT_VERIFY = 3;
private static final int OPERATION_GET_KEY_IDS = 4;
public OpenPgpApi(IOpenPgpService service) {
this.mService = service;
@ -88,6 +89,10 @@ public class OpenPgpApi {
executeApiAsync(OPERATION_DECRYPT_VERIFY, params, is, os, callback);
}
public Bundle getKeyIds(Bundle params) {
return executeApi(OPERATION_GET_KEY_IDS, params, null, null);
}
public interface IOpenPgpCallback {
void onReturn(final Bundle result);
}
@ -124,24 +129,6 @@ public class OpenPgpApi {
private Bundle executeApi(int operationId, Bundle params, InputStream is, OutputStream os) {
try {
// send the input and output pfds
ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is,
new ParcelFileDescriptorUtil.IThreadListener() {
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Copy to service finished");
}
});
ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os,
new ParcelFileDescriptorUtil.IThreadListener() {
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Service finished writing!");
}
});
params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION);
// default result is error
@ -150,25 +137,49 @@ public class OpenPgpApi {
result.putParcelable(OpenPgpConstants.RESULT_ERRORS,
new OpenPgpError(OpenPgpError.GENERIC_ERROR, "This should never happen!"));
// blocks until result is ready
switch (operationId) {
case OPERATION_SIGN:
result = mService.sign(params, input, output);
break;
case OPERATION_ENCRYPT:
result = mService.encrypt(params, input, output);
break;
case OPERATION_SIGN_ENCRYPT:
result = mService.signAndEncrypt(params, input, output);
break;
case OPERATION_DECRYPT_VERIFY:
result = mService.decryptAndVerify(params, input, output);
break;
}
// close() is required to halt the TransferThread
output.close();
if (operationId == OPERATION_GET_KEY_IDS) {
result = mService.getKeyIds(params);
return result;
} else {
// send the input and output pfds
ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is,
new ParcelFileDescriptorUtil.IThreadListener() {
return result;
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Copy to service finished");
}
});
ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os,
new ParcelFileDescriptorUtil.IThreadListener() {
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Service finished writing!");
}
});
// blocks until result is ready
switch (operationId) {
case OPERATION_SIGN:
result = mService.sign(params, input, output);
break;
case OPERATION_ENCRYPT:
result = mService.encrypt(params, input, output);
break;
case OPERATION_SIGN_ENCRYPT:
result = mService.signAndEncrypt(params, input, output);
break;
case OPERATION_DECRYPT_VERIFY:
result = mService.decryptAndVerify(params, input, output);
break;
}
// close() is required to halt the TransferThread
output.close();
return result;
}
} catch (Exception e) {
Log.e(OpenPgpConstants.TAG, "Exception", e);
Bundle result = new Bundle();

View File

@ -87,12 +87,12 @@ public class ParcelFileDescriptorUtil {
try {
mIn.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e);
}
try {
mOut.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e);
}
}
if (mListener != null) {

View File

@ -46,6 +46,9 @@ import java.util.ArrayList;
public class OpenPgpService extends RemoteService {
private static final int PRIVATE_REQUEST_CODE_PASSPHRASE = 551;
private static final int PRIVATE_REQUEST_CODE_USER_IDS = 552;
/**
* Search database for key ids based on emails.
@ -95,7 +98,7 @@ public class OpenPgpService extends RemoteService {
intent.putExtra(RemoteServiceActivity.EXTRA_MISSING_USER_IDS, missingUserIds);
intent.putExtra(RemoteServiceActivity.EXTRA_DUBLICATE_USER_IDS, dublicateUserIds);
PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 42, intent, 0);
PendingIntent pi = PendingIntent.getActivity(getBaseContext(), PRIVATE_REQUEST_CODE_USER_IDS, intent, 0);
// return PendingIntent to be executed by client
Bundle result = new Bundle();
@ -120,7 +123,7 @@ public class OpenPgpService extends RemoteService {
Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class);
intent.setAction(RemoteServiceActivity.ACTION_CACHE_PASSPHRASE);
intent.putExtra(RemoteServiceActivity.EXTRA_SECRET_KEY_ID, keyId);
PendingIntent pi = PendingIntent.getActivity(getBaseContext(), 42, intent, 0);
PendingIntent pi = PendingIntent.getActivity(getBaseContext(), PRIVATE_REQUEST_CODE_PASSPHRASE, intent, 0);
// return PendingIntent to be executed by client
Bundle result = new Bundle();
@ -130,6 +133,46 @@ public class OpenPgpService extends RemoteService {
return result;
}
// TODO: asciiArmor?!
private Bundle signImpl(Bundle params, ParcelFileDescriptor input, ParcelFileDescriptor output, AppSettings appSettings) {
try {
// get passphrase from cache, if key has "no" passphrase, this returns an empty String
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId());
if (passphrase == null) {
// get PendingIntent for passphrase input, add it to given params and return to client
Bundle passphraseBundle = getPassphraseBundleIntent(appSettings.getKeyId());
params.putAll(passphraseBundle);
return params;
}
// Get Input- and OutputStream from ParcelFileDescriptor
InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
OutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(output);
try {
long inputLength = is.available();
InputData inputData = new InputData(is, inputLength);
PgpOperation operation = new PgpOperation(getContext(), null, inputData, os);
operation.signText(appSettings.getKeyId(), passphrase, appSettings.getHashAlgorithm(),
Preferences.getPreferences(this).getForceV3Signatures());
} finally {
is.close();
os.close();
}
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_SUCCESS);
return result;
} catch (Exception e) {
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR);
result.putParcelable(OpenPgpConstants.RESULT_ERRORS,
new OpenPgpError(OpenPgpError.GENERIC_ERROR, e.getMessage()));
return result;
}
}
private Bundle encryptAndSignImpl(Bundle params, ParcelFileDescriptor input,
ParcelFileDescriptor output, AppSettings appSettings,
boolean sign) {
@ -143,13 +186,14 @@ public class OpenPgpService extends RemoteService {
// get key ids based on given user ids
String[] userIds = params.getStringArray(OpenPgpConstants.PARAMS_USER_IDS);
Bundle result = getKeyIdsFromEmails(userIds);
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED);
if (result.getInt(OpenPgpConstants.RESULT_CODE, 0) == OpenPgpConstants.RESULT_CODE_SUCCESS) {
keyIds = result.getLongArray(OpenPgpConstants.PARAMS_KEY_IDS);
} else {
// non-unique result, we need user interaction!
return result;
// if not success -> result contains a PendingIntent for user interaction
// return all old params with the new PendingIntent to client!
params.putAll(result);
return params;
}
}
@ -170,7 +214,10 @@ public class OpenPgpService extends RemoteService {
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(),
appSettings.getKeyId());
if (passphrase == null) {
return getPassphraseBundleIntent(appSettings.getKeyId());
// get PendingIntent for passphrase input, add it to given params and return to client
Bundle passphraseBundle = getPassphraseBundleIntent(appSettings.getKeyId());
params.putAll(passphraseBundle);
return params;
}
operation.signAndEncrypt(asciiArmor, appSettings.getCompression(), keyIds, null,
@ -198,42 +245,6 @@ public class OpenPgpService extends RemoteService {
}
}
// TODO: asciiArmor?!
private Bundle signImpl(ParcelFileDescriptor input, ParcelFileDescriptor output, AppSettings appSettings) {
try {
// get passphrase from cache, if key has "no" passphrase, this returns an empty String
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId());
if (passphrase == null) {
return getPassphraseBundleIntent(appSettings.getKeyId());
}
// Get Input- and OutputStream from ParcelFileDescriptor
InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(input);
OutputStream os = new ParcelFileDescriptor.AutoCloseOutputStream(output);
try {
long inputLength = is.available();
InputData inputData = new InputData(is, inputLength);
PgpOperation operation = new PgpOperation(getContext(), null, inputData, os);
operation.signText(appSettings.getKeyId(), passphrase, appSettings.getHashAlgorithm(),
Preferences.getPreferences(this).getForceV3Signatures());
} finally {
is.close();
os.close();
}
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_SUCCESS);
return result;
} catch (Exception e) {
Bundle result = new Bundle();
result.putInt(OpenPgpConstants.RESULT_CODE, OpenPgpConstants.RESULT_CODE_ERROR);
result.putParcelable(OpenPgpConstants.RESULT_ERRORS,
new OpenPgpError(OpenPgpError.GENERIC_ERROR, e.getMessage()));
return result;
}
}
private Bundle decryptAndVerifyImpl(Bundle params, ParcelFileDescriptor input,
ParcelFileDescriptor output, AppSettings appSettings) {
try {
@ -323,11 +334,14 @@ public class OpenPgpService extends RemoteService {
//
// Log.d(Constants.TAG, "secretKeyId " + secretKeyId);
// NOTE: currently this only gets the passphrase for the saved key
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId());
if (passphrase == null) {
return getPassphraseBundleIntent(appSettings.getKeyId());
}
// NOTE: currently this only gets the passphrase for the saved key
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), appSettings.getKeyId());
if (passphrase == null) {
// get PendingIntent for passphrase input, add it to given params and return to client
Bundle passphraseBundle = getPassphraseBundleIntent(appSettings.getKeyId());
params.putAll(passphraseBundle);
return params;
}
// }
// build InputData and write into OutputStream
@ -390,6 +404,15 @@ public class OpenPgpService extends RemoteService {
}
}
private Bundle getKeyIdsImpl(Bundle params) {
// get key ids based on given user ids
String[] userIds = params.getStringArray(OpenPgpConstants.PARAMS_USER_IDS);
Bundle result = getKeyIdsFromEmails(userIds);
params.putAll(result);
return params;
}
/**
* Checks that params != null and API version fits
*
@ -430,7 +453,7 @@ public class OpenPgpService extends RemoteService {
return errorResult;
}
return signImpl(input, output, appSettings);
return signImpl(params, input, output, appSettings);
}
@Override
@ -459,10 +482,27 @@ public class OpenPgpService extends RemoteService {
@Override
public Bundle decryptAndVerify(Bundle params, ParcelFileDescriptor input, ParcelFileDescriptor output) {
final AppSettings appSettings = getAppSettings();
return null;
Bundle errorResult = validateParamsAndVersion(params);
if (errorResult != null) {
return errorResult;
}
return decryptAndVerifyImpl(params, input, output, appSettings);
}
@Override
public Bundle getKeyIds(Bundle params) {
Bundle errorResult = validateParamsAndVersion(params);
if (errorResult != null) {
return errorResult;
}
return getKeyIdsImpl(params);
}
// TODO: old example for checkAndEnqueue!
// @Override
// public void getKeyIds(final String[] userIds, final boolean allowUserInteraction,
// final IOpenPgpKeyIdsCallback callback) throws RemoteException {

View File

@ -64,4 +64,15 @@ interface IOpenPgpService {
*/
Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output);
/**
* Retrieves key ids based on given user ids (=emails)
*
* params:
* String[] user_ids
*
* result:
* long[] key_ids
*/
Bundle getKeyIds(in Bundle params);
}

View File

@ -35,6 +35,7 @@ public class OpenPgpApi {
private static final int OPERATION_ENCRYPT = 1;
private static final int OPERATION_SIGN_ENCRYPT = 2;
private static final int OPERATION_DECRYPT_VERIFY = 3;
private static final int OPERATION_GET_KEY_IDS = 4;
public OpenPgpApi(IOpenPgpService service) {
this.mService = service;
@ -88,6 +89,10 @@ public class OpenPgpApi {
executeApiAsync(OPERATION_DECRYPT_VERIFY, params, is, os, callback);
}
public Bundle getKeyIds(Bundle params) {
return executeApi(OPERATION_GET_KEY_IDS, params, null, null);
}
public interface IOpenPgpCallback {
void onReturn(final Bundle result);
}
@ -124,24 +129,6 @@ public class OpenPgpApi {
private Bundle executeApi(int operationId, Bundle params, InputStream is, OutputStream os) {
try {
// send the input and output pfds
ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is,
new ParcelFileDescriptorUtil.IThreadListener() {
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Copy to service finished");
}
});
ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os,
new ParcelFileDescriptorUtil.IThreadListener() {
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Service finished writing!");
}
});
params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION);
// default result is error
@ -150,25 +137,49 @@ public class OpenPgpApi {
result.putParcelable(OpenPgpConstants.RESULT_ERRORS,
new OpenPgpError(OpenPgpError.GENERIC_ERROR, "This should never happen!"));
// blocks until result is ready
switch (operationId) {
case OPERATION_SIGN:
result = mService.sign(params, input, output);
break;
case OPERATION_ENCRYPT:
result = mService.encrypt(params, input, output);
break;
case OPERATION_SIGN_ENCRYPT:
result = mService.signAndEncrypt(params, input, output);
break;
case OPERATION_DECRYPT_VERIFY:
result = mService.decryptAndVerify(params, input, output);
break;
}
// close() is required to halt the TransferThread
output.close();
if (operationId == OPERATION_GET_KEY_IDS) {
result = mService.getKeyIds(params);
return result;
} else {
// send the input and output pfds
ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is,
new ParcelFileDescriptorUtil.IThreadListener() {
return result;
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Copy to service finished");
}
});
ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os,
new ParcelFileDescriptorUtil.IThreadListener() {
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Service finished writing!");
}
});
// blocks until result is ready
switch (operationId) {
case OPERATION_SIGN:
result = mService.sign(params, input, output);
break;
case OPERATION_ENCRYPT:
result = mService.encrypt(params, input, output);
break;
case OPERATION_SIGN_ENCRYPT:
result = mService.signAndEncrypt(params, input, output);
break;
case OPERATION_DECRYPT_VERIFY:
result = mService.decryptAndVerify(params, input, output);
break;
}
// close() is required to halt the TransferThread
output.close();
return result;
}
} catch (Exception e) {
Log.e(OpenPgpConstants.TAG, "Exception", e);
Bundle result = new Bundle();

View File

@ -87,12 +87,12 @@ public class ParcelFileDescriptorUtil {
try {
mIn.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e);
}
try {
mOut.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e);
}
}
if (mListener != null) {