extract select by signer from KeychainProvider

This commit is contained in:
Vincent Breitmoser
2018-06-22 19:58:17 +02:00
parent 6cd065a3bd
commit d57a409fac
13 changed files with 87 additions and 128 deletions

View File

@@ -33,6 +33,7 @@ import com.squareup.sqldelight.SqlDelightQuery;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.sufficientlysecure.keychain.model.KeyRingPublic;
import org.sufficientlysecure.keychain.model.SubKey;
import org.sufficientlysecure.keychain.model.SubKey.UnifiedKeyInfo;
import org.sufficientlysecure.keychain.model.UserPacket;
import org.sufficientlysecure.keychain.model.UserPacket.UserId;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
@@ -172,7 +173,7 @@ public class KeyRepository extends AbstractDao {
return getGenericData(KeyRings.buildUnifiedKeyRingUri(masterKeyId), proj, types);
}
public long getMasterKeyId(long subKeyId) throws NotFoundException {
public long getMasterKeyIdBySubKeyId(long subKeyId) throws NotFoundException {
return (Long) getGenericData(KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(subKeyId),
KeyRings.MASTER_KEY_ID, FIELD_TYPE_INTEGER);
}
@@ -238,6 +239,33 @@ public class KeyRepository extends AbstractDao {
}
}
public List<Long> getAllMasterKeyIds() {
SqlDelightQuery query = KeyRingPublic.FACTORY.selectAllMasterKeyIds();
return mapAllRows(query, KeyRingPublic.FACTORY.selectAllMasterKeyIdsMapper()::map);
}
public List<Long> getMasterKeyIdsBySigner(List<Long> signerMasterKeyIds) {
long[] signerKeyIds = new long[signerMasterKeyIds.size()];
int i = 0;
for (Long signerKeyId : signerMasterKeyIds) {
signerKeyIds[i++] = signerKeyId;
}
SqlDelightQuery query = SubKey.FACTORY.selectMasterKeyIdsBySigner(signerKeyIds);
return mapAllRows(query, KeyRingPublic.FACTORY.selectAllMasterKeyIdsMapper()::map);
}
public List<UnifiedKeyInfo> getUnifiedKeyInfo() {
SqlDelightQuery query = SubKey.FACTORY.selectAllUnifiedKeyInfo();
List<UnifiedKeyInfo> result = new ArrayList<>();
try (Cursor cursor = getReadableDb().query(query)) {
while (cursor.moveToNext()) {
UnifiedKeyInfo unifiedKeyInfo = SubKey.UNIFIED_KEY_INFO_MAPPER.map(cursor);
result.add(unifiedKeyInfo);
}
}
return result;
}
public List<UserId> getUserIds(long... masterKeyIds) {
SqlDelightQuery query = UserPacket.FACTORY.selectUserIdsByMasterKeyId(masterKeyIds);
return mapAllRows(query, UserPacket.USER_ID_MAPPER::map);

View File

@@ -1013,30 +1013,18 @@ public class KeyWritableRepository extends KeyRepository {
log.add(LogType.MSG_TRUST, 0);
Cursor cursor;
Preferences preferences = Preferences.getPreferences(context);
boolean isTrustDbInitialized = preferences.isKeySignaturesTableInitialized();
List<Long> masterKeyIds;
if (!isTrustDbInitialized) {
log.add(LogType.MSG_TRUST_INITIALIZE, 1);
cursor = contentResolver.query(KeyRings.buildUnifiedKeyRingsUri(),
new String[] { KeyRings.MASTER_KEY_ID }, null, null, null);
masterKeyIds = getAllMasterKeyIds();
} else {
String[] signerMasterKeyIdStrings = new String[signerMasterKeyIds.size()];
int i = 0;
for (Long masterKeyId : signerMasterKeyIds) {
log.add(LogType.MSG_TRUST_KEY, 1, KeyFormattingUtils.beautifyKeyId(masterKeyId));
signerMasterKeyIdStrings[i++] = Long.toString(masterKeyId);
}
cursor = contentResolver.query(KeyRings.buildUnifiedKeyRingsFilterBySigner(),
new String[] { KeyRings.MASTER_KEY_ID }, null, signerMasterKeyIdStrings, null);
masterKeyIds = getMasterKeyIdsBySigner(signerMasterKeyIds);
}
if (cursor == null) {
throw new IllegalStateException();
}
int totalKeys = cursor.getCount();
int totalKeys = masterKeyIds.size();
int processedKeys = 0;
if (totalKeys == 0) {
@@ -1046,34 +1034,30 @@ public class KeyWritableRepository extends KeyRepository {
log.add(LogType.MSG_TRUST_COUNT, 1, totalKeys);
}
try {
while (cursor.moveToNext()) {
try {
long masterKeyId = cursor.getLong(0);
for (long masterKeyId : masterKeyIds) {
try {
log.add(LogType.MSG_TRUST_KEY, 1, KeyFormattingUtils.beautifyKeyId(masterKeyId));
byte[] pubKeyData = loadPublicKeyRingData(masterKeyId);
UncachedKeyRing uncachedKeyRing = UncachedKeyRing.decodeFromData(pubKeyData);
byte[] pubKeyData = loadPublicKeyRingData(masterKeyId);
UncachedKeyRing uncachedKeyRing = UncachedKeyRing.decodeFromData(pubKeyData);
clearLog();
SaveKeyringResult result = savePublicKeyRing(uncachedKeyRing, true);
clearLog();
SaveKeyringResult result = savePublicKeyRing(uncachedKeyRing, true);
log.add(result, 1);
progress.setProgress(processedKeys++, totalKeys);
} catch (NotFoundException | PgpGeneralException | IOException e) {
Timber.e(e, "Error updating trust database");
return new UpdateTrustResult(UpdateTrustResult.RESULT_ERROR, log);
}
log.add(result, 1);
progress.setProgress(processedKeys++, totalKeys);
} catch (NotFoundException | PgpGeneralException | IOException e) {
Timber.e(e, "Error updating trust database");
return new UpdateTrustResult(UpdateTrustResult.RESULT_ERROR, log);
}
if (!isTrustDbInitialized) {
preferences.setKeySignaturesTableInitialized();
}
log.add(LogType.MSG_TRUST_OK, 1);
return new UpdateTrustResult(UpdateTrustResult.RESULT_OK, log);
} finally {
cursor.close();
}
if (!isTrustDbInitialized) {
preferences.setKeySignaturesTableInitialized();
}
log.add(LogType.MSG_TRUST_OK, 1);
return new UpdateTrustResult(UpdateTrustResult.RESULT_OK, log);
}
/**

View File

@@ -104,9 +104,6 @@ public class KeychainContract {
public static final String PATH_BY_SUBKEY = "subkey";
public static final String PATH_BY_USER_ID = "user_id";
public static final String PATH_FILTER = "filter";
public static final String PATH_BY_SIGNER = "signer";
public static final String PATH_PUBLIC = "public";
public static final String PATH_USER_IDS = "user_ids";
public static final String PATH_KEYS = "keys";
@@ -170,10 +167,6 @@ public class KeychainContract {
return CONTENT_URI.buildUpon().appendPath(PATH_FIND)
.appendPath(PATH_BY_SUBKEY).appendPath(Long.toString(subkey)).build();
}
public static Uri buildUnifiedKeyRingsFilterBySigner() {
return CONTENT_URI.buildUpon().appendPath(PATH_FILTER).appendPath(PATH_BY_SIGNER).build();
}
}
public static class KeyRingData implements KeyRingsColumns, BaseColumns {

View File

@@ -40,6 +40,7 @@ import org.sufficientlysecure.keychain.CertsModel;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.KeyMetadataModel;
import org.sufficientlysecure.keychain.KeyRingsPublicModel;
import org.sufficientlysecure.keychain.KeySignaturesModel;
import org.sufficientlysecure.keychain.UserPacketsModel;
import org.sufficientlysecure.keychain.model.ApiApp;
import org.sufficientlysecure.keychain.model.Certification;
@@ -199,7 +200,7 @@ public class KeychainDatabase {
db.execSQL(UserPacketsModel.CREATE_TABLE);
db.execSQL(CertsModel.CREATE_TABLE);
db.execSQL(KeyMetadataModel.CREATE_TABLE);
db.execSQL(CREATE_KEY_SIGNATURES);
db.execSQL(KeySignaturesModel.CREATE_TABLE);
db.execSQL(CREATE_API_APPS_ALLOWED_KEYS);
db.execSQL(CREATE_OVERRIDDEN_WARNINGS);
db.execSQL(AutocryptPeersModel.CREATE_TABLE);

View File

@@ -65,7 +65,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
private static final int KEY_RINGS_FIND_BY_EMAIL = 400;
private static final int KEY_RINGS_FIND_BY_SUBKEY = 401;
private static final int KEY_RINGS_FIND_BY_USER_ID = 402;
private static final int KEY_RINGS_FILTER_BY_SIGNER = 403;
private static final int KEY_SIGNATURES = 700;
@@ -108,9 +107,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
matcher.addURI(authority, KeychainContract.BASE_KEY_RINGS + "/"
+ KeychainContract.PATH_FIND + "/" + KeychainContract.PATH_BY_USER_ID + "/*",
KEY_RINGS_FIND_BY_USER_ID);
matcher.addURI(authority, KeychainContract.BASE_KEY_RINGS + "/"
+ KeychainContract.PATH_FILTER + "/" + KeychainContract.PATH_BY_SIGNER,
KEY_RINGS_FILTER_BY_SIGNER);
/*
* list key_ring specifics
@@ -191,8 +187,7 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
case KEY_RINGS_UNIFIED:
case KEY_RINGS_FIND_BY_EMAIL:
case KEY_RINGS_FIND_BY_SUBKEY:
case KEY_RINGS_FIND_BY_USER_ID:
case KEY_RINGS_FILTER_BY_SIGNER: {
case KEY_RINGS_FIND_BY_USER_ID: {
HashMap<String, String> projectionMap = new HashMap<>();
projectionMap.put(KeyRings._ID, Tables.KEYS + ".oid AS _id");
projectionMap.put(KeyRings.MASTER_KEY_ID, Tables.KEYS + "." + Keys.MASTER_KEY_ID);
@@ -356,23 +351,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
}
break;
}
case KEY_RINGS_FILTER_BY_SIGNER: {
StringBuilder signerKeyIds = new StringBuilder();
signerKeyIds.append(selectionArgs[0]);
for (int i = 1; i < selectionArgs.length; i++) {
signerKeyIds.append(',').append(selectionArgs[i]);
}
qb.appendWhere(" AND EXISTS (SELECT 1 FROM " + Tables.KEY_SIGNATURES + " WHERE " +
Tables.KEY_SIGNATURES + "." + KeySignatures.MASTER_KEY_ID + " = " + Tables.KEYS + "." + Keys.MASTER_KEY_ID +
" AND " +
Tables.KEY_SIGNATURES + "." + KeySignatures.SIGNER_KEY_ID + " IN (" + signerKeyIds + ")" +
")");
selection = null;
selectionArgs = null;
break;
}
case KEY_RINGS_FIND_BY_EMAIL:
case KEY_RINGS_FIND_BY_USER_ID: {
String chunks[] = uri.getLastPathSegment().split(" *, *");