ditch CachedPublicKeyRing, and some cleanup
This commit is contained in:
@@ -8,6 +8,8 @@ import android.arch.persistence.db.SupportSQLiteDatabase;
|
||||
import android.arch.persistence.db.SupportSQLiteQuery;
|
||||
import android.database.Cursor;
|
||||
|
||||
import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException;
|
||||
|
||||
|
||||
class AbstractDao {
|
||||
private final KeychainDatabase db;
|
||||
@@ -41,6 +43,14 @@ class AbstractDao {
|
||||
return result;
|
||||
}
|
||||
|
||||
<T> T mapSingleRowOrThrow(SupportSQLiteQuery query, Mapper<T> mapper) throws NotFoundException {
|
||||
T result = mapSingleRow(query, mapper);
|
||||
if (result == null) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
<T> T mapSingleRow(SupportSQLiteQuery query, Mapper<T> mapper) {
|
||||
try (Cursor cursor = getReadableDb().query(query)) {
|
||||
if (cursor.moveToNext()) {
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Schürmann & Breitmoser GbR
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.sufficientlysecure.keychain.provider;
|
||||
|
||||
|
||||
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
|
||||
import org.sufficientlysecure.keychain.pgp.CanonicalizedKeyRing.VerificationStatus;
|
||||
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
|
||||
import org.sufficientlysecure.keychain.pgp.KeyRing;
|
||||
import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException;
|
||||
|
||||
|
||||
/** This implementation of KeyRing provides a cached view of PublicKeyRing
|
||||
* objects based on database queries exclusively.
|
||||
*
|
||||
* This class should be used where only few points of data but no actual
|
||||
* cryptographic operations are required about a PublicKeyRing which is already
|
||||
* in the database. This happens commonly in UI code, where parsing of a PGP
|
||||
* key for examination would be a very expensive operation.
|
||||
*
|
||||
* Each getter method is implemented using a more or less expensive database
|
||||
* query, while object construction is (almost) free. A common pattern is
|
||||
* mProviderHelper.getCachedKeyRing(uri).getterMethod()
|
||||
*
|
||||
* TODO Ensure that the values returned here always match the ones returned by
|
||||
* the parsed KeyRing!
|
||||
*
|
||||
*/
|
||||
public class CachedPublicKeyRing extends KeyRing {
|
||||
private UnifiedKeyInfo unifiedKeyInfo;
|
||||
|
||||
public CachedPublicKeyRing(UnifiedKeyInfo unifiedKeyInfo) {
|
||||
this.unifiedKeyInfo = unifiedKeyInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMasterKeyId() {
|
||||
return unifiedKeyInfo.master_key_id();
|
||||
}
|
||||
|
||||
public byte[] getFingerprint() {
|
||||
return unifiedKeyInfo.fingerprint();
|
||||
}
|
||||
|
||||
public long getCreationTime() {
|
||||
return unifiedKeyInfo.creation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrimaryUserId() {
|
||||
return unifiedKeyInfo.user_id();
|
||||
}
|
||||
|
||||
public String getPrimaryUserIdWithFallback() {
|
||||
return getPrimaryUserId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRevoked() {
|
||||
return unifiedKeyInfo.is_revoked();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCertify() {
|
||||
return unifiedKeyInfo.can_certify();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEncryptId() {
|
||||
return unifiedKeyInfo.has_encrypt_key_int();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasEncrypt() {
|
||||
return unifiedKeyInfo.has_encrypt_key();
|
||||
}
|
||||
|
||||
public long getAuthenticationId() {
|
||||
return unifiedKeyInfo.has_auth_key_int();
|
||||
}
|
||||
|
||||
@Override
|
||||
public VerificationStatus getVerified() {
|
||||
return unifiedKeyInfo.verified();
|
||||
}
|
||||
|
||||
public boolean hasAnySecret() {
|
||||
return unifiedKeyInfo.has_any_secret();
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ package org.sufficientlysecure.keychain.provider;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
@@ -102,15 +101,6 @@ public class KeyRepository extends AbstractDao {
|
||||
mLog = new OperationLog();
|
||||
}
|
||||
|
||||
// replace with getUnifiedKeyInfo
|
||||
public CachedPublicKeyRing getCachedPublicKeyRing(long masterKeyId) throws NotFoundException {
|
||||
UnifiedKeyInfo unifiedKeyInfo = getUnifiedKeyInfo(masterKeyId);
|
||||
if (unifiedKeyInfo == null) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return new CachedPublicKeyRing(unifiedKeyInfo);
|
||||
}
|
||||
|
||||
public CanonicalizedPublicKeyRing getCanonicalizedPublicKeyRing(long masterKeyId) throws NotFoundException {
|
||||
UnifiedKeyInfo unifiedKeyInfo = getUnifiedKeyInfo(masterKeyId);
|
||||
if (unifiedKeyInfo == null) {
|
||||
@@ -146,22 +136,12 @@ public class KeyRepository extends AbstractDao {
|
||||
|
||||
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;
|
||||
}
|
||||
return mapSingleRow(query, SubKey.FACTORY.selectMasterKeyIdBySubkeyMapper()::map);
|
||||
}
|
||||
|
||||
public UnifiedKeyInfo getUnifiedKeyInfo(long masterKeyId) {
|
||||
SqlDelightQuery query = SubKey.FACTORY.selectUnifiedKeyInfoByMasterKeyId(masterKeyId);
|
||||
try (Cursor cursor = getReadableDb().query(query)) {
|
||||
if (cursor.moveToNext()) {
|
||||
return SubKey.UNIFIED_KEY_INFO_MAPPER.map(cursor);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return mapSingleRow(query, SubKey.UNIFIED_KEY_INFO_MAPPER::map);
|
||||
}
|
||||
|
||||
public List<UnifiedKeyInfo> getUnifiedKeyInfo(long... masterKeyIds) {
|
||||
@@ -190,13 +170,9 @@ public class KeyRepository extends AbstractDao {
|
||||
}
|
||||
|
||||
public List<String> getConfirmedUserIds(long masterKeyId) {
|
||||
ArrayList<String> userIds = new ArrayList<>();
|
||||
SqlDelightQuery query = UserPacket.FACTORY.selectUserIdsByMasterKeyIdAndVerification(
|
||||
Certification.FACTORY, masterKeyId, VerificationStatus.VERIFIED_SECRET);
|
||||
for (UserId userId : mapAllRows(query, UserPacket.USER_ID_MAPPER::map)) {
|
||||
userIds.add(userId.user_id());
|
||||
}
|
||||
return userIds;
|
||||
return mapAllRows(query, (cursor) -> UserPacket.USER_ID_MAPPER.map(cursor).user_id());
|
||||
}
|
||||
|
||||
public List<SubKey> getSubKeysByMasterKeyId(long masterKeyId) {
|
||||
@@ -206,12 +182,12 @@ public class KeyRepository extends AbstractDao {
|
||||
|
||||
public SecretKeyType getSecretKeyType(long keyId) throws NotFoundException {
|
||||
SqlDelightQuery query = SubKey.FACTORY.selectSecretKeyType(keyId);
|
||||
try (Cursor cursor = getReadableDb().query(query)) {
|
||||
if (cursor.moveToFirst()) {
|
||||
return SubKey.SKT_MAPPER.map(cursor);
|
||||
}
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return mapSingleRowOrThrow(query, SubKey.SKT_MAPPER::map);
|
||||
}
|
||||
|
||||
public byte[] getFingerprintByKeyId(long keyId) throws NotFoundException {
|
||||
SqlDelightQuery query = SubKey.FACTORY.selectFingerprintByKeyId(keyId);
|
||||
return mapSingleRowOrThrow(query, SubKey.FACTORY.selectFingerprintByKeyIdMapper()::map);
|
||||
}
|
||||
|
||||
private byte[] getKeyRingAsArmoredData(byte[] data) throws IOException {
|
||||
@@ -267,20 +243,12 @@ public class KeyRepository extends AbstractDao {
|
||||
|
||||
public long getSecretSignId(long masterKeyId) throws NotFoundException {
|
||||
SqlDelightQuery query = SubKey.FACTORY.selectEffectiveSignKeyIdByMasterKeyId(masterKeyId);
|
||||
Long signKeyId = mapSingleRow(query, SubKey.FACTORY.selectEffectiveSignKeyIdByMasterKeyIdMapper()::map);
|
||||
if (signKeyId == null) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return signKeyId;
|
||||
return mapSingleRowOrThrow(query, SubKey.FACTORY.selectEffectiveSignKeyIdByMasterKeyIdMapper()::map);
|
||||
}
|
||||
|
||||
public Long getSecretAuthenticationId(long masterKeyId) throws NotFoundException {
|
||||
public long getSecretAuthenticationId(long masterKeyId) throws NotFoundException {
|
||||
SqlDelightQuery query = SubKey.FACTORY.selectEffectiveAuthKeyIdByMasterKeyId(masterKeyId);
|
||||
Long authKeyId = mapSingleRow(query, SubKey.FACTORY.selectEffectiveAuthKeyIdByMasterKeyIdMapper()::map);
|
||||
if (authKeyId == null) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return authKeyId;
|
||||
return mapSingleRowOrThrow(query, SubKey.FACTORY.selectEffectiveAuthKeyIdByMasterKeyIdMapper()::map);
|
||||
}
|
||||
|
||||
public static class NotFoundException extends Exception {
|
||||
|
||||
Reference in New Issue
Block a user