From 8858dd5c3b10188dddaf1bb003f8c93cae4c0fc5 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 2 Apr 2018 22:32:30 +0200 Subject: [PATCH] Return extra values for select sign key call --- .../provider/CachedPublicKeyRing.java | 10 ++++ .../keychain/remote/OpenPgpService.java | 49 ++++++++++++------- .../ui/dialog/RemoteSelectIdKeyActivity.java | 2 +- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java index b0de16a63..077c72285 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java @@ -92,6 +92,16 @@ public class CachedPublicKeyRing extends KeyRing { } } + public long getCreationTime() throws PgpKeyNotFoundException { + try { + Object data = mKeyRepository.getGenericData(mUri, + KeychainContract.KeyRings.CREATION, KeyRepository.FIELD_TYPE_INTEGER); + return (long) data; + } catch (KeyWritableRepository.NotFoundException e) { + throw new PgpKeyNotFoundException(e); + } + } + @Override public String getPrimaryUserId() throws PgpKeyNotFoundException { try { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index 3a09a54cf..22885c36e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -67,6 +67,7 @@ import org.sufficientlysecure.keychain.pgp.SecurityProblem; import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.ApiDataAccessObject; import org.sufficientlysecure.keychain.provider.AutocryptPeerDataAccessObject; +import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; @@ -683,28 +684,42 @@ public class OpenPgpService extends Service { } private Intent getSignKeyIdImpl(Intent data) { - // if data already contains EXTRA_SIGN_KEY_ID, it has been executed again - // after user interaction. Then, we just need to return the long again! - if (data.hasExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID)) { - long signKeyId = data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, Constants.key.none); + Intent result = new Intent(); + data.setAction(OpenPgpApi.ACTION_GET_SIGN_KEY_ID); - Intent result = new Intent(); - result.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, signKeyId); - result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); - return result; - } else { + { // return PendingIntent to be executed by client String currentPkg = mApiPermissionHelper.getCurrentCallingPackage(); String preferredUserId = data.getStringExtra(OpenPgpApi.EXTRA_USER_ID); - - PendingIntent pi = mApiPendingIntentFactory.createSelectSignKeyIdPendingIntent(data, currentPkg, preferredUserId); - - // return PendingIntent to be executed by client - Intent result = new Intent(); - result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED); + PendingIntent pi = + mApiPendingIntentFactory.createSelectSignKeyIdPendingIntent(data, currentPkg, preferredUserId); result.putExtra(OpenPgpApi.RESULT_INTENT, pi); - - return result; } + + long signKeyId; + if (data.hasExtra(OpenPgpApi.RESULT_SIGN_KEY_ID)) { + signKeyId = data.getLongExtra(OpenPgpApi.RESULT_SIGN_KEY_ID, Constants.key.none); + result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); + } else { + signKeyId = data.getLongExtra(OpenPgpApi.EXTRA_PRESELECT_KEY_ID, Constants.key.none); + result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED); + } + result.putExtra(OpenPgpApi.RESULT_SIGN_KEY_ID, signKeyId); + + if (signKeyId != Constants.key.none) { + try { + CachedPublicKeyRing cachedPublicKeyRing = mKeyRepository.getCachedPublicKeyRing(signKeyId); + String userId = cachedPublicKeyRing.getPrimaryUserId(); + long creationTime = cachedPublicKeyRing.getCreationTime() * 1000; + + result.putExtra(OpenPgpApi.RESULT_PRIMARY_USER_ID, userId); + result.putExtra(OpenPgpApi.RESULT_KEY_CREATION_TIME, creationTime); + } catch (PgpKeyNotFoundException e) { + Timber.e(e, "Error loading key info"); + return createErrorResultIntent(OpenPgpError.GENERIC_ERROR, e.getMessage()); + } + } + + return result; } private Intent getKeyIdsImpl(Intent data) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/dialog/RemoteSelectIdKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/dialog/RemoteSelectIdKeyActivity.java index b71a0d681..0ee5b2936 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/dialog/RemoteSelectIdKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/dialog/RemoteSelectIdKeyActivity.java @@ -210,7 +210,7 @@ public class RemoteSelectIdKeyActivity extends FragmentActivity { } Intent resultData = new Intent(); - resultData.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, masterKeyId); + resultData.putExtra(OpenPgpApi.RESULT_SIGN_KEY_ID, masterKeyId); activity.setResult(RESULT_OK, resultData); activity.finish(); }