extract database access from CachedPublicKeyRing

This commit is contained in:
Vincent Breitmoser
2018-06-26 10:24:19 +02:00
parent aa640f3227
commit 31830a8c86
40 changed files with 328 additions and 730 deletions

View File

@@ -36,9 +36,9 @@ import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.CertifyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
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.service.CertifyActionsParcel;
import org.sufficientlysecure.keychain.service.CertifyActionsParcel.CertifyAction;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
@@ -75,7 +75,7 @@ public class CertifyKeyFragment
if (key.canCertify()) {
mCertifyKeySpinner.setPreSelectedKeyId(certifyKeyId);
}
} catch (PgpKeyNotFoundException e) {
} catch (NotFoundException e) {
Timber.e(e, "certify certify check failed");
}
}

View File

@@ -48,9 +48,9 @@ import org.sufficientlysecure.keychain.operations.results.EditKeyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.operations.results.UploadResult;
import org.sufficientlysecure.keychain.pgp.KeyRing;
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.service.ChangeUnlockParcel;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel;
import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyChange;
@@ -413,10 +413,10 @@ public class CreateKeyFinalFragment extends Fragment {
KeyRepository keyRepository = KeyRepository.create(getContext());
SaveKeyringParcel.Builder builder;
CachedPublicKeyRing key = keyRepository.getCachedPublicKeyRing(saveKeyResult.mMasterKeyId);
try {
CachedPublicKeyRing key = keyRepository.getCachedPublicKeyRing(saveKeyResult.mMasterKeyId);
builder = SaveKeyringParcel.buildChangeKeyringParcel(saveKeyResult.mMasterKeyId, key.getFingerprint());
} catch (PgpKeyNotFoundException e) {
} catch (NotFoundException e) {
Timber.e("Key that should be moved to Security Token not found in database!");
return;
}

View File

@@ -38,6 +38,7 @@ import android.widget.Spinner;
import android.widget.TextView;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
import org.sufficientlysecure.keychain.operations.results.DeleteResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.operations.results.RevokeResult;
@@ -86,35 +87,23 @@ public class DeleteKeyDialogActivity extends FragmentActivity {
if (mMasterKeyIds.length == 1 && mHasSecret) {
// if mMasterKeyIds.length == 0 we let the DeleteOperation respond
try {
KeyRepository keyRepository = KeyRepository.create(this);
HashMap<String, Object> data = keyRepository.getUnifiedData(
mMasterKeyIds[0], new String[]{
KeychainContract.KeyRings.NAME,
KeychainContract.KeyRings.IS_REVOKED
}, new int[]{
KeyRepository.FIELD_TYPE_STRING,
KeyRepository.FIELD_TYPE_INTEGER
}
);
String name;
name = (String) data.get(KeychainContract.KeyRings.NAME);
if (name == null) {
name = getString(R.string.user_id_no_name);
}
if ((long) data.get(KeychainContract.KeyRings.IS_REVOKED) > 0) {
showNormalDeleteDialog();
} else {
showRevokeDeleteDialog(name);
}
} catch (KeyRepository.NotFoundException e) {
Timber.e(e, "Secret key to delete not found at DeleteKeyDialogActivity for "
+ mMasterKeyIds[0]);
KeyRepository keyRepository = KeyRepository.create(this);
UnifiedKeyInfo keyInfo = keyRepository.getUnifiedKeyInfo(mMasterKeyIds[0]);
if (keyInfo == null) {
Timber.e("Secret key to delete not found at DeleteKeyDialogActivity for " + mMasterKeyIds[0]);
finish();
return;
}
String name = keyInfo.name();
if (name == null) {
name = getString(R.string.user_id_no_name);
}
if (keyInfo.is_revoked()) {
showNormalDeleteDialog();
} else {
showRevokeDeleteDialog(name);
}
} else {
showNormalDeleteDialog();
@@ -269,36 +258,26 @@ public class DeleteKeyDialogActivity extends FragmentActivity {
if (masterKeyIds.length == 1) {
long masterKeyId = masterKeyIds[0];
try {
HashMap<String, Object> data = KeyRepository.create(getContext())
.getUnifiedData(
masterKeyId, new String[]{
KeychainContract.KeyRings.NAME,
KeychainContract.KeyRings.HAS_ANY_SECRET
}, new int[]{
KeyRepository.FIELD_TYPE_STRING,
KeyRepository.FIELD_TYPE_INTEGER
}
);
String name;
name = (String) data.get(KeychainContract.KeyRings.NAME);
if (name == null) {
name = getString(R.string.user_id_no_name);
}
if (hasSecret) {
// show title only for secret key deletions,
// see http://www.google.com/design/spec/components/dialogs.html#dialogs-behavior
builder.setTitle(getString(R.string.title_delete_secret_key, name));
mMainMessage.setText(getString(R.string.secret_key_deletion_confirmation, name));
} else {
mMainMessage.setText(getString(R.string.public_key_deletetion_confirmation, name));
}
} catch (KeyRepository.NotFoundException e) {
KeyRepository keyRepository = KeyRepository.create(getContext());
UnifiedKeyInfo keyInfo = keyRepository.getUnifiedKeyInfo(masterKeyId);
if (keyInfo == null) {
dismiss();
return null;
}
String name = keyInfo.name();
if (name == null) {
name = getString(R.string.user_id_no_name);
}
if (keyInfo.has_any_secret()) {
// show title only for secret key deletions,
// see http://www.google.com/design/spec/components/dialogs.html#dialogs-behavior
builder.setTitle(getString(R.string.title_delete_secret_key, name));
mMainMessage.setText(getString(R.string.secret_key_deletion_confirmation, name));
} else {
mMainMessage.setText(getString(R.string.public_key_deletetion_confirmation, name));
}
} else {
mMainMessage.setText(R.string.key_deletion_confirmation_multi);
}

View File

@@ -32,7 +32,6 @@ import com.tokenautocomplete.TokenCompleteTextView.TokenListener;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing;
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;
@@ -142,7 +141,7 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
if (keyring.hasAnySecret()) {
mSignKeySpinner.setPreSelectedKeyId(signatureKeyId);
}
} catch (PgpKeyNotFoundException e) {
} catch (NotFoundException e) {
Timber.e(e, "key not found for signing!");
Notify.create(getActivity(), getString(R.string.error_preselect_sign_key,
KeyFormattingUtils.beautifyKeyId(signatureKeyId)),

View File

@@ -61,7 +61,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;
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
@@ -115,10 +114,9 @@ public class PassphraseDialogActivity extends FragmentActivity {
// handle empty passphrases by directly returning an empty crypto input parcel
try {
CachedPublicKeyRing pubRing =
KeyRepository.create(this).getCachedPublicKeyRing(requiredInput.getMasterKeyId());
KeyRepository keyRepository = KeyRepository.create(this);
// use empty passphrase for empty passphrase
if (pubRing.getSecretKeyType(requiredInput.getSubKeyId()) == SecretKeyType.PASSPHRASE_EMPTY) {
if (keyRepository.getSecretKeyType(requiredInput.getSubKeyId()) == SecretKeyType.PASSPHRASE_EMPTY) {
// also return passphrase back to activity
Intent returnIntent = new Intent();
cryptoInputParcel = cryptoInputParcel.withPassphrase(new Passphrase(""), requiredInput.getSubKeyId());
@@ -299,7 +297,7 @@ public class PassphraseDialogActivity extends FragmentActivity {
userId = getString(R.string.user_id_no_name);
}
keyType = cachedPublicKeyRing.getSecretKeyType(subKeyId);
keyType = keyRepository.getSecretKeyType(subKeyId);
switch (keyType) {
case PASSPHRASE:
message = getString(R.string.passphrase_for, userId);
@@ -316,7 +314,7 @@ public class PassphraseDialogActivity extends FragmentActivity {
throw new AssertionError("Unhandled SecretKeyType (should not happen)");
}
}
} catch (PgpKeyNotFoundException | KeyRepository.NotFoundException e) {
} catch (KeyRepository.NotFoundException e) {
alert.setTitle(R.string.title_key_not_found);
alert.setMessage(getString(R.string.key_not_found, mRequiredInput.getSubKeyId()));
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

View File

@@ -54,8 +54,8 @@ import org.sufficientlysecure.keychain.livedata.GenericLiveData;
import org.sufficientlysecure.keychain.model.Certification.CertDetails;
import org.sufficientlysecure.keychain.operations.results.LinkedVerifyResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.KeyRepository;
import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException;
import org.sufficientlysecure.keychain.service.CertifyActionsParcel;
import org.sufficientlysecure.keychain.service.CertifyActionsParcel.CertifyAction;
import org.sufficientlysecure.keychain.ui.adapter.IdentityAdapter;
@@ -382,9 +382,8 @@ public class LinkedIdViewFragment extends CryptoOperationFragment implements OnB
byte[] fingerprint;
try {
fingerprint = KeyRepository.create(activity).getCachedPublicKeyRing(
masterKeyId).getFingerprint();
} catch (PgpKeyNotFoundException e) {
fingerprint = KeyRepository.create(activity).getCachedPublicKeyRing(masterKeyId).getFingerprint();
} catch (NotFoundException e) {
throw new IllegalStateException("Key to verify linked id for must exist in db!");
}

View File

@@ -483,8 +483,7 @@ public class ViewKeyActivity extends BaseSecurityTokenActivity implements
private boolean keyHasPassphrase() {
try {
long masterKeyId = unifiedKeyInfo.master_key_id();
SecretKeyType secretKeyType =
keyRepository.getCachedPublicKeyRing(masterKeyId).getSecretKeyType(masterKeyId);
SecretKeyType secretKeyType = keyRepository.getSecretKeyType(masterKeyId);
switch (secretKeyType) {
// all of these make no sense to ask
case PASSPHRASE_EMPTY:

View File

@@ -29,14 +29,12 @@ import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.content.AsyncTaskLoader;
import android.text.TextUtils;
import android.util.Log;
import com.google.auto.value.AutoValue;
import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.Request.Builder;
import okhttp3.Response;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.keyimport.HkpKeyserverAddress;
import org.sufficientlysecure.keychain.keyimport.HkpKeyserverClient;
import org.sufficientlysecure.keychain.keyimport.KeyserverClient.QueryFailedException;
@@ -48,11 +46,8 @@ import org.sufficientlysecure.keychain.operations.results.OperationResult.Operat
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing.IteratorWithIOThrow;
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.KeyRepository.NotFoundException;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.ui.token.PublicKeyRetrievalLoader.KeyRetrievalResult;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.ParcelableProxy;
@@ -120,7 +115,6 @@ public abstract class PublicKeyRetrievalLoader extends AsyncTaskLoader<KeyRetrie
log.add(LogType.MSG_RET_LOCAL_NOT_FOUND, 2);
continue;
}
CachedPublicKeyRing cachedPublicKeyRing = keyRepository.getCachedPublicKeyRing(masterKeyId);
// TODO check fingerprint
// if (!Arrays.equals(fingerprints, cachedPublicKeyRing.getFingerprint())) {
@@ -130,7 +124,7 @@ public abstract class PublicKeyRetrievalLoader extends AsyncTaskLoader<KeyRetrie
// log.add(LogType.MSG_RET_LOCAL_FP_MATCH, 1);
// }
switch (cachedPublicKeyRing.getSecretKeyType(keyId)) {
switch (keyRepository.getSecretKeyType(keyId)) {
case PASSPHRASE:
case PASSPHRASE_EMPTY: {
log.add(LogType.MSG_RET_LOCAL_SECRET, 1);

View File

@@ -414,7 +414,7 @@ public class TransferPresenter implements KeyTransferCallback, LoaderCallbacks<L
byte[] armoredSecretKey = databaseInteractor.getSecretKeyRingAsArmoredData(masterKeyId);
secretKeyAdapter.focusItem(masterKeyId);
connectionSend(armoredSecretKey, Long.toString(masterKeyId));
} catch (IOException | NotFoundException | PgpGeneralException e) {
} catch (IOException | NotFoundException e) {
// TODO
e.printStackTrace();
}