From 4416ddf11c5cb8aaad533de1318a0ade1e4e9ef1 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 25 Jun 2018 17:38:26 +0200 Subject: [PATCH] Get rid of more places where URIs are used for loading --- .../keychain/ui/ViewKeyAdvShareTest.java | 120 ------------------ .../keychain/operations/RevokeOperation.java | 5 +- .../pgp/PgpDecryptVerifyOperation.java | 14 +- .../keychain/pgp/PgpSignEncryptOperation.java | 3 +- .../keychain/pgp/PgpSignatureChecker.java | 17 ++- .../keychain/provider/KeyRepository.java | 81 ++++-------- .../keychain/remote/OpenPgpService.java | 5 +- .../ui/RequestKeyPermissionPresenter.java | 11 +- .../keychain/ui/DecryptFragment.java | 19 +-- .../ui/EncryptModeAsymmetricFragment.java | 4 +- .../keychain/ui/PassphraseDialogActivity.java | 19 +-- .../ui/SecurityTokenOperationActivity.java | 12 +- .../ui/token/PublicKeyRetrievalLoader.java | 12 +- .../org/sufficientlysecure/keychain/Keys.sq | 5 + .../keychain/provider/InteropTest.java | 2 - 15 files changed, 86 insertions(+), 243 deletions(-) delete mode 100644 OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareTest.java diff --git a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareTest.java b/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareTest.java deleted file mode 100644 index 83463d179..000000000 --- a/OpenKeychain/src/androidTest/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareTest.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (C) 2015 Vincent Breitmoser - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.sufficientlysecure.keychain.ui; - - -import android.app.Activity; -import android.app.Instrumentation.ActivityResult; -import android.content.Intent; -import android.support.test.espresso.intent.rule.IntentsTestRule; - -import org.junit.Before; -import org.junit.FixMethodOrder; -import org.junit.Rule; -import org.junit.runners.MethodSorters; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; -import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; -import org.sufficientlysecure.keychain.provider.TemporaryFileProvider; -import org.sufficientlysecure.keychain.ui.util.Notify.Style; - -import static android.support.test.espresso.Espresso.onView; -import static android.support.test.espresso.action.ViewActions.click; -import static android.support.test.espresso.intent.Intents.intending; -import static android.support.test.espresso.intent.matcher.IntentMatchers.hasAction; -import static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtra; -import static android.support.test.espresso.intent.matcher.IntentMatchers.hasType; -import static android.support.test.espresso.intent.matcher.UriMatchers.hasHost; -import static android.support.test.espresso.intent.matcher.UriMatchers.hasScheme; -import static android.support.test.espresso.matcher.ViewMatchers.assertThat; -import static android.support.test.espresso.matcher.ViewMatchers.withId; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.startsWith; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.sufficientlysecure.keychain.AndroidTestHelpers.checkAndDismissSnackbar; -import static org.sufficientlysecure.keychain.AndroidTestHelpers.cleanupForTests; - -//TODO This test is disabled because it needs to be fixed to work with updated code -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -//@RunWith(AndroidJUnit4.class) -//@LargeTest -public class ViewKeyAdvShareTest { - - @Rule - public final IntentsTestRule mActivityRule - = new IntentsTestRule(ViewKeyAdvActivity.class) { - @Override - protected Intent getActivityIntent() { - Intent intent = super.getActivityIntent(); - intent.setData(KeyRings.buildGenericKeyRingUri(0x9D604D2F310716A3L)); - intent.putExtra(ViewKeyAdvActivity.EXTRA_SELECTED_TAB, ViewKeyAdvActivity.TAB_SHARE); - return intent; - } - }; - private Activity mActivity; - - @Before - public void setUp() throws Exception { - mActivity = mActivityRule.getActivity(); - - cleanupForTests(mActivity); - } - - //@Test - public void testShareOperations() throws Exception { - - // no-op should yield snackbar - onView(withId(R.id.view_key_action_fingerprint_clipboard)).perform(click()); - checkAndDismissSnackbar(Style.OK, R.string.fingerprint_copied_to_clipboard); - assertThat("clipboard data is fingerprint", ClipboardReflection.getClipboardText(mActivity), - is("c619d53f7a5f96f391a84ca79d604d2f310716a3")); - - intending(allOf( - hasAction("android.intent.action.CHOOSER"), - hasExtra(equalTo(Intent.EXTRA_INTENT), allOf( - hasAction(Intent.ACTION_SEND), - hasType("text/plain"), - hasExtra(is(Intent.EXTRA_TEXT), is("openpgp4fpr:c619d53f7a5f96f391a84ca79d604d2f310716a3")), - hasExtra(is(Intent.EXTRA_STREAM), - allOf(hasScheme("content"), hasHost(TemporaryFileProvider.AUTHORITY))) - )) - )).respondWith(new ActivityResult(Activity.RESULT_OK, null)); - onView(withId(R.id.view_key_action_fingerprint_share)).perform(click()); - - onView(withId(R.id.view_key_action_key_clipboard)).perform(click()); - checkAndDismissSnackbar(Style.OK, R.string.key_copied_to_clipboard); - assertThat("clipboard data is key", - ClipboardReflection.getClipboardText(mActivity), startsWith("----")); - - intending(allOf( - hasAction("android.intent.action.CHOOSER"), - hasExtra(equalTo(Intent.EXTRA_INTENT), allOf( - hasAction(Intent.ACTION_SEND), - hasType("text/plain"), - hasExtra(is(Intent.EXTRA_TEXT), startsWith("----")), - hasExtra(is(Intent.EXTRA_STREAM), - allOf(hasScheme("content"), hasHost(TemporaryFileProvider.AUTHORITY))) - )) - )).respondWith(new ActivityResult(Activity.RESULT_OK, null)); - onView(withId(R.id.view_key_action_key_share)).perform(click()); - - } - - -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/RevokeOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/RevokeOperation.java index 23b3380a2..60e75bafe 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/RevokeOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/RevokeOperation.java @@ -19,7 +19,6 @@ package org.sufficientlysecure.keychain.operations; import android.content.Context; -import android.net.Uri; import android.support.annotation.NonNull; import org.sufficientlysecure.keychain.operations.results.EditKeyResult; @@ -29,7 +28,6 @@ import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeyWritableRepository; -import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.service.RevokeKeyringParcel; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; @@ -59,8 +57,7 @@ public class RevokeOperation extends BaseReadWriteOperation try { - Uri secretUri = KeychainContract.KeyRings.buildUnifiedKeyRingUri(masterKeyId); - CachedPublicKeyRing keyRing = mKeyRepository.getCachedPublicKeyRing(secretUri); + CachedPublicKeyRing keyRing = mKeyRepository.getCachedPublicKeyRing(masterKeyId); // check if this is a master secret key we can work with switch (keyRing.getSecretKeyType(masterKeyId)) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerifyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerifyOperation.java index 6a5881e79..3fc54ef14 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerifyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerifyOperation.java @@ -71,11 +71,9 @@ import org.sufficientlysecure.keychain.pgp.SecurityProblem.EncryptionAlgorithmPr import org.sufficientlysecure.keychain.pgp.SecurityProblem.KeySecurityProblem; import org.sufficientlysecure.keychain.pgp.SecurityProblem.MissingMdc; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; -import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.provider.KeyWritableRepository; -import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.RequireAnyDecryptPassphraseBuilder; @@ -638,10 +636,12 @@ public class PgpDecryptVerifyOperation extends BaseOperation encryptSubKeyIds = keyRing.getEncryptIds(); for (Long subKeyId : encryptSubKeyIds) { CanonicalizedPublicKey key = keyRing.getPublicKey(subKeyId); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignatureChecker.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignatureChecker.java index 6d1313451..43dabc258 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignatureChecker.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignatureChecker.java @@ -40,7 +40,6 @@ import org.sufficientlysecure.keychain.pgp.SecurityProblem.InsecureSigningAlgori import org.sufficientlysecure.keychain.pgp.SecurityProblem.KeySecurityProblem; import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.provider.KeyWritableRepository; -import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import timber.log.Timber; @@ -160,9 +159,11 @@ class PgpSignatureChecker { for (int i = 0; i < sigList.size(); ++i) { try { long sigKeyId = sigList.get(i).getKeyID(); - CanonicalizedPublicKeyRing signingRing = mKeyRepository.getCanonicalizedPublicKeyRing( - KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(sigKeyId) - ); + Long masterKeyId = mKeyRepository.getMasterKeyIdBySubkeyId(sigKeyId); + if (masterKeyId == null) { + continue; + } + CanonicalizedPublicKeyRing signingRing = mKeyRepository.getCanonicalizedPublicKeyRing(masterKeyId); CanonicalizedPublicKey keyCandidate = signingRing.getPublicKey(sigKeyId); if ( ! keyCandidate.canSign()) { continue; @@ -183,9 +184,11 @@ class PgpSignatureChecker { for (int i = 0; i < sigList.size(); ++i) { try { long sigKeyId = sigList.get(i).getKeyID(); - CanonicalizedPublicKeyRing signingRing = mKeyRepository.getCanonicalizedPublicKeyRing( - KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(sigKeyId) - ); + Long masterKeyId = mKeyRepository.getMasterKeyIdBySubkeyId(sigKeyId); + if (masterKeyId == null) { + continue; + } + CanonicalizedPublicKeyRing signingRing = mKeyRepository.getCanonicalizedPublicKeyRing(masterKeyId); CanonicalizedPublicKey keyCandidate = signingRing.getPublicKey(sigKeyId); if ( ! keyCandidate.canSign()) { continue; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java index 9877a6d86..e6d997c92 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeyRepository.java @@ -32,7 +32,6 @@ import android.net.Uri; import com.squareup.sqldelight.SqlDelightQuery; import org.bouncycastle.bcpg.ArmoredOutputStream; import org.sufficientlysecure.keychain.model.Certification; -import org.sufficientlysecure.keychain.model.CustomColumnAdapters; import org.sufficientlysecure.keychain.model.KeyRingPublic; import org.sufficientlysecure.keychain.model.SubKey; import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo; @@ -175,11 +174,6 @@ public class KeyRepository extends AbstractDao { return getGenericData(KeyRings.buildUnifiedKeyRingUri(masterKeyId), proj, types); } - public long getMasterKeyIdBySubKeyId(long subKeyId) throws NotFoundException { - return (Long) getGenericData(KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(subKeyId), - KeyRings.MASTER_KEY_ID, FIELD_TYPE_INTEGER); - } - public CachedPublicKeyRing getCachedPublicKeyRing(Uri queryUri) throws PgpKeyNotFoundException { long masterKeyId = new CachedPublicKeyRing(this, queryUri).extractOrGetMasterKeyId(); return getCachedPublicKeyRing(masterKeyId); @@ -189,59 +183,26 @@ public class KeyRepository extends AbstractDao { return new CachedPublicKeyRing(this, KeyRings.buildUnifiedKeyRingUri(id)); } - public CanonicalizedPublicKeyRing getCanonicalizedPublicKeyRing(long id) throws NotFoundException { - return getCanonicalizedPublicKeyRing(KeyRings.buildUnifiedKeyRingUri(id)); - } - - public CanonicalizedPublicKeyRing getCanonicalizedPublicKeyRing(Uri queryUri) throws NotFoundException { - Cursor cursor = contentResolver.query(queryUri, - new String[] { KeyRings.MASTER_KEY_ID, KeyRings.VERIFIED }, null, null, null); - try { - if (cursor != null && cursor.moveToFirst()) { - long masterKeyId = cursor.getLong(0); - long verified = cursor.getLong(1); - - byte[] publicKeyData = loadPublicKeyRingData(masterKeyId); - VerificationStatus verificationStatus = CustomColumnAdapters.VERIFICATON_STATUS_ADAPTER.decode(verified); - return new CanonicalizedPublicKeyRing(publicKeyData, verificationStatus); - } else { - throw new NotFoundException("Key not found!"); - } - } finally { - if (cursor != null) { - cursor.close(); - } + public CanonicalizedPublicKeyRing getCanonicalizedPublicKeyRing(long masterKeyId) throws NotFoundException { + UnifiedKeyInfo unifiedKeyInfo = getUnifiedKeyInfo(masterKeyId); + if (unifiedKeyInfo == null) { + throw new NotFoundException(); } + + byte[] publicKeyData = loadPublicKeyRingData(masterKeyId); + return new CanonicalizedPublicKeyRing(publicKeyData, unifiedKeyInfo.verified()); } - public CanonicalizedSecretKeyRing getCanonicalizedSecretKeyRing(long id) throws NotFoundException { - return getCanonicalizedSecretKeyRing(KeyRings.buildUnifiedKeyRingUri(id)); - } - - public CanonicalizedSecretKeyRing getCanonicalizedSecretKeyRing(Uri queryUri) throws NotFoundException { - Cursor cursor = contentResolver.query(queryUri, - new String[] { KeyRings.MASTER_KEY_ID, KeyRings.VERIFIED, KeyRings.HAS_ANY_SECRET }, null, null, null); - try { - if (cursor != null && cursor.moveToFirst()) { - long masterKeyId = cursor.getLong(0); - long verified = cursor.getLong(1); - int hasAnySecret = cursor.getInt(2); - if (hasAnySecret == 0) { - throw new NotFoundException("No secret key available or unknown public key!"); - } - - VerificationStatus verificationStatus = CustomColumnAdapters.VERIFICATON_STATUS_ADAPTER.decode(verified); - - byte[] secretKeyData = loadSecretKeyRingData(masterKeyId); - return new CanonicalizedSecretKeyRing(secretKeyData, verificationStatus); - } else { - throw new NotFoundException("Key not found!"); - } - } finally { - if (cursor != null) { - cursor.close(); - } + public CanonicalizedSecretKeyRing getCanonicalizedSecretKeyRing(long masterKeyId) throws NotFoundException { + UnifiedKeyInfo unifiedKeyInfo = getUnifiedKeyInfo(masterKeyId); + if (unifiedKeyInfo == null || !unifiedKeyInfo.has_any_secret()) { + throw new NotFoundException(); } + byte[] secretKeyData = loadSecretKeyRingData(masterKeyId); + if (secretKeyData == null) { + throw new IllegalStateException("Missing expected secret key data!"); + } + return new CanonicalizedSecretKeyRing(secretKeyData, unifiedKeyInfo.verified()); } public List getAllMasterKeyIds() { @@ -259,6 +220,16 @@ public class KeyRepository extends AbstractDao { return mapAllRows(query, KeyRingPublic.FACTORY.selectAllMasterKeyIdsMapper()::map); } + public Long getMasterKeyIdBySubkeyId(long subKeyId) { + SqlDelightQuery query = SubKey.FACTORY.selectMasterKeyIdBySubkey(subKeyId); + try (Cursor cursor = getReadableDb().query(query)) { + if (cursor.moveToFirst()) { + return SubKey.FACTORY.selectMasterKeyIdBySubkeyMapper().map(cursor); + } + return null; + } + } + public UnifiedKeyInfo getUnifiedKeyInfo(long masterKeyId) { SqlDelightQuery query = SubKey.FACTORY.selectUnifiedKeyInfoByMasterKeyId(masterKeyId); try (Cursor cursor = getReadableDb().query(query)) { 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 3591df50f..2f29fc281 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -627,10 +627,7 @@ public class OpenPgpService extends Service { } try { - // try to find key, throws NotFoundException if not in db! - CanonicalizedPublicKeyRing keyRing = - mKeyRepository.getCanonicalizedPublicKeyRing( - KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(masterKeyId)); + CanonicalizedPublicKeyRing keyRing = mKeyRepository.getCanonicalizedPublicKeyRing(masterKeyId); Intent result = new Intent(); result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RequestKeyPermissionPresenter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RequestKeyPermissionPresenter.java index 1a8e6e2c9..0644f6e95 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RequestKeyPermissionPresenter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/RequestKeyPermissionPresenter.java @@ -33,7 +33,6 @@ import org.sufficientlysecure.keychain.provider.ApiAppDao; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException; -import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.remote.ApiPermissionHelper; import org.sufficientlysecure.keychain.remote.ApiPermissionHelper.WrongPackageCertificateException; import timber.log.Timber; @@ -118,9 +117,11 @@ class RequestKeyPermissionPresenter { CachedPublicKeyRing publicFallbackRing = null; for (long candidateSubKeyId : subKeyIds) { try { - CachedPublicKeyRing cachedPublicKeyRing = keyRepository.getCachedPublicKeyRing( - KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(candidateSubKeyId) - ); + Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(candidateSubKeyId); + if (masterKeyId == null) { + continue; + } + CachedPublicKeyRing cachedPublicKeyRing = keyRepository.getCachedPublicKeyRing(masterKeyId); SecretKeyType secretKeyType = cachedPublicKeyRing.getSecretKeyType(candidateSubKeyId); if (secretKeyType.isUsable()) { @@ -129,7 +130,7 @@ class RequestKeyPermissionPresenter { if (publicFallbackRing == null) { publicFallbackRing = cachedPublicKeyRing; } - } catch (PgpKeyNotFoundException | NotFoundException e) { + } catch (NotFoundException e) { // no matter } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java index 8387ff951..bcba0af9b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java @@ -29,7 +29,6 @@ import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; -import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; @@ -41,14 +40,12 @@ import android.widget.ViewAnimator; import org.openintents.openpgp.OpenPgpDecryptionResult; import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.util.OpenPgpUtils; -import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.keyimport.HkpKeyserverAddress; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult; import org.sufficientlysecure.keychain.operations.results.ImportKeyResult; import org.sufficientlysecure.keychain.pgp.KeyRing; -import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; @@ -193,18 +190,14 @@ public abstract class DecryptFragment extends Fragment implements LoaderManager. } private void showKey(long keyId) { - try { - - KeyRepository keyRepository = KeyRepository.create(requireContext()); - long masterKeyId = keyRepository.getCachedPublicKeyRing( - KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(keyId) - ).getMasterKeyId(); - Intent viewKeyIntent = ViewKeyActivity.getViewKeyActivityIntent(requireActivity(), masterKeyId); - startActivity(viewKeyIntent); - - } catch (PgpKeyNotFoundException e) { + KeyRepository keyRepository = KeyRepository.create(requireContext()); + Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(keyId); + if (masterKeyId == null) { Notify.create(getActivity(), R.string.error_key_not_found, Style.ERROR).show(); + return; } + Intent viewKeyIntent = ViewKeyActivity.getViewKeyActivityIntent(requireActivity(), masterKeyId); + startActivity(viewKeyIntent); } protected void loadVerifyResult(DecryptVerifyResult decryptVerifyResult) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java index 3620063e4..dd851052e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptModeAsymmetricFragment.java @@ -36,7 +36,6 @@ import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException; -import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter.KeyItem; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.ui.util.Notify; @@ -139,8 +138,7 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment { private void preselectKeys(Long signatureKeyId, long[] encryptionKeyIds) { if (signatureKeyId != null) { try { - CachedPublicKeyRing keyring = mKeyRepository.getCachedPublicKeyRing( - KeyRings.buildUnifiedKeyRingUri(signatureKeyId)); + CachedPublicKeyRing keyring = mKeyRepository.getCachedPublicKeyRing(signatureKeyId); if (keyring.hasAnySecret()) { mSignKeySpinner.setPreSelectedKeyId(signatureKeyId); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java index bd0f84764..4e48100ed 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java @@ -285,10 +285,9 @@ public class PassphraseDialogActivity extends FragmentActivity { } else { long subKeyId = subKeyIds[0]; - KeyRepository helper = - KeyRepository.create(getContext()); - CachedPublicKeyRing cachedPublicKeyRing = helper.getCachedPublicKeyRing( - KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(subKeyId)); + KeyRepository keyRepository = KeyRepository.create(getContext()); + Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(subKeyId); + CachedPublicKeyRing cachedPublicKeyRing = keyRepository.getCachedPublicKeyRing(masterKeyId); // yes the inner try/catch block is necessary, otherwise the final variable // above can't be statically verified to have been set in all cases because // the catch clause doesn't return. @@ -534,11 +533,13 @@ public class PassphraseDialogActivity extends FragmentActivity { CanonicalizedSecretKey canonicalizedSecretKey = null; for (long subKeyId : mRequiredInput.getSubKeyIds()) { - CanonicalizedSecretKeyRing secretKeyRing = - KeyRepository.create(getContext()).getCanonicalizedSecretKeyRing( - KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(subKeyId)); - CanonicalizedSecretKey secretKeyToUnlock = - secretKeyRing.getSecretKey(subKeyId); + KeyRepository keyRepository = KeyRepository.create(getContext()); + Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(subKeyId); + if (masterKeyId == null) { + continue; + } + CanonicalizedSecretKeyRing secretKeyRing = keyRepository.getCanonicalizedSecretKeyRing(masterKeyId); + CanonicalizedSecretKey secretKeyToUnlock = secretKeyRing.getSecretKey(subKeyId); // this is the operation may take a very long time (100ms to several seconds!) boolean unlockSucceeded = secretKeyToUnlock.unlock(passphrase); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SecurityTokenOperationActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SecurityTokenOperationActivity.java index 496b4adfd..74c17ea5b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SecurityTokenOperationActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SecurityTokenOperationActivity.java @@ -199,12 +199,11 @@ public class SecurityTokenOperationActivity extends BaseSecurityTokenActivity { throw new IOException(getString(R.string.error_wrong_security_token)); } - KeyRepository keyRepository = - KeyRepository.create(this); + KeyRepository keyRepository = KeyRepository.create(this); CanonicalizedPublicKeyRing publicKeyRing; try { - publicKeyRing = keyRepository.getCanonicalizedPublicKeyRing( - KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(mRequiredInput.getMasterKeyId())); + Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(mRequiredInput.getMasterKeyId()); + publicKeyRing = keyRepository.getCanonicalizedPublicKeyRing(masterKeyId); } catch (KeyRepository.NotFoundException e) { throw new IOException("Couldn't find subkey for key to token operation."); } @@ -263,9 +262,8 @@ public class SecurityTokenOperationActivity extends BaseSecurityTokenActivity { KeyRepository.create(this); CanonicalizedSecretKeyRing secretKeyRing; try { - secretKeyRing = keyRepository.getCanonicalizedSecretKeyRing( - KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(mRequiredInput.getMasterKeyId()) - ); + Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(mRequiredInput.getMasterKeyId()); + secretKeyRing = keyRepository.getCanonicalizedSecretKeyRing(masterKeyId); } catch (KeyRepository.NotFoundException e) { throw new IOException("Couldn't find subkey for key to token operation."); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/token/PublicKeyRetrievalLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/token/PublicKeyRetrievalLoader.java index 3d9170064..a0be35c87 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/token/PublicKeyRetrievalLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/token/PublicKeyRetrievalLoader.java @@ -115,11 +115,13 @@ public abstract class PublicKeyRetrievalLoader extends AsyncTaskLoader