service: add opportunistic mode to encryption

This commit is contained in:
Vincent Breitmoser 2016-03-10 18:34:15 +01:00
parent 48867e89bd
commit 69e6e404bf
2 changed files with 28 additions and 22 deletions

View file

@ -103,20 +103,20 @@ public class OpenPgpService extends Service {
} }
private static class KeyIdResult { private static class KeyIdResult {
final Intent mRequiredUserInteraction; final Intent mResultIntent;
final HashSet<Long> mKeyIds; final HashSet<Long> mKeyIds;
KeyIdResult(Intent requiredUserInteraction) { KeyIdResult(Intent resultIntent) {
mRequiredUserInteraction = requiredUserInteraction; mResultIntent = resultIntent;
mKeyIds = null; mKeyIds = null;
} }
KeyIdResult(HashSet<Long> keyIds) { KeyIdResult(HashSet<Long> keyIds) {
mRequiredUserInteraction = null; mResultIntent = null;
mKeyIds = keyIds; mKeyIds = keyIds;
} }
} }
private KeyIdResult returnKeyIdsFromEmails(Intent data, String[] encryptionUserIds) { private KeyIdResult returnKeyIdsFromEmails(Intent data, String[] encryptionUserIds, boolean isOpportunistic) {
boolean noUserIdsCheck = (encryptionUserIds == null || encryptionUserIds.length == 0); boolean noUserIdsCheck = (encryptionUserIds == null || encryptionUserIds.length == 0);
boolean missingUserIdsCheck = false; boolean missingUserIdsCheck = false;
boolean duplicateUserIdsCheck = false; boolean duplicateUserIdsCheck = false;
@ -159,9 +159,15 @@ public class OpenPgpService extends Service {
} }
} }
if (noUserIdsCheck || missingUserIdsCheck || duplicateUserIdsCheck) { if (isOpportunistic && (noUserIdsCheck || missingUserIdsCheck)) {
// allow the user to verify pub key selection Intent result = new Intent();
result.putExtra(OpenPgpApi.RESULT_ERROR,
new OpenPgpError(OpenPgpError.OPPORTUNISTIC_MISSING_KEYS, "missing keys in opportunistic mode"));
result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
return new KeyIdResult(result);
}
if (noUserIdsCheck || missingUserIdsCheck || duplicateUserIdsCheck) {
// convert ArrayList<Long> to long[] // convert ArrayList<Long> to long[]
long[] keyIdsArray = getUnboxedLongArray(keyIds); long[] keyIdsArray = getUnboxedLongArray(keyIds);
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext()); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext());
@ -173,16 +179,15 @@ public class OpenPgpService extends Service {
result.putExtra(OpenPgpApi.RESULT_INTENT, pi); result.putExtra(OpenPgpApi.RESULT_INTENT, pi);
result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED); result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED);
return new KeyIdResult(result); return new KeyIdResult(result);
} else { }
// everything was easy, we have exactly one key for every email
// everything was easy, we have exactly one key for every email
if (keyIds.isEmpty()) { if (keyIds.isEmpty()) {
Log.e(Constants.TAG, "keyIdsArray.length == 0, should never happen!"); Log.e(Constants.TAG, "keyIdsArray.length == 0, should never happen!");
} }
return new KeyIdResult(keyIds); return new KeyIdResult(keyIds);
} }
}
private Intent signImpl(Intent data, InputStream inputStream, private Intent signImpl(Intent data, InputStream inputStream,
OutputStream outputStream, boolean cleartextSign) { OutputStream outputStream, boolean cleartextSign) {
@ -302,11 +307,12 @@ public class OpenPgpService extends Service {
// get key ids based on given user ids // get key ids based on given user ids
if (data.hasExtra(OpenPgpApi.EXTRA_USER_IDS)) { if (data.hasExtra(OpenPgpApi.EXTRA_USER_IDS)) {
String[] userIds = data.getStringArrayExtra(OpenPgpApi.EXTRA_USER_IDS); String[] userIds = data.getStringArrayExtra(OpenPgpApi.EXTRA_USER_IDS);
boolean isOpportunistic = data.getBooleanExtra(OpenPgpApi.EXTRA_OPPORTUNISTIC_ENCRYPTION, false);
// give params through to activity... // give params through to activity...
KeyIdResult result = returnKeyIdsFromEmails(data, userIds); KeyIdResult result = returnKeyIdsFromEmails(data, userIds, isOpportunistic);
if (result.mRequiredUserInteraction != null) { if (result.mResultIntent != null) {
return result.mRequiredUserInteraction; return result.mResultIntent;
} }
encryptKeyIds.addAll(result.mKeyIds); encryptKeyIds.addAll(result.mKeyIds);
} }
@ -694,9 +700,9 @@ public class OpenPgpService extends Service {
} else { } else {
// get key ids based on given user ids // get key ids based on given user ids
String[] userIds = data.getStringArrayExtra(OpenPgpApi.EXTRA_USER_IDS); String[] userIds = data.getStringArrayExtra(OpenPgpApi.EXTRA_USER_IDS);
KeyIdResult keyResult = returnKeyIdsFromEmails(data, userIds); KeyIdResult keyResult = returnKeyIdsFromEmails(data, userIds, false);
if (keyResult.mRequiredUserInteraction != null) { if (keyResult.mResultIntent != null) {
return keyResult.mRequiredUserInteraction; return keyResult.mResultIntent;
} }
if (keyResult.mKeyIds == null) { if (keyResult.mKeyIds == null) {

@ -1 +1 @@
Subproject commit 8ca0f578bb843db7744dbd5724b32f6664b5c3db Subproject commit 710a0d8fe8d89cb9a1f247007000a7f49a29c527