get rid of direct query for blob from backup operation

This commit is contained in:
Vincent Breitmoser
2017-02-20 21:32:54 +01:00
parent c2b9af077a
commit 623964701b
2 changed files with 31 additions and 21 deletions

View File

@@ -45,13 +45,18 @@ import org.sufficientlysecure.keychain.operations.results.OperationResult.LogTyp
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.operations.results.PgpSignEncryptResult;
import org.sufficientlysecure.keychain.pgp.CanonicalizedKeyRing;
import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing;
import org.sufficientlysecure.keychain.pgp.PgpSignEncryptData;
import org.sufficientlysecure.keychain.pgp.PgpSignEncryptInputParcel;
import org.sufficientlysecure.keychain.pgp.PgpSignEncryptOperation;
import org.sufficientlysecure.keychain.pgp.Progressable;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.DatabaseInteractor;
import org.sufficientlysecure.keychain.provider.DatabaseInteractor.NotFoundException;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
import org.sufficientlysecure.keychain.provider.TemporaryFileProvider;
@@ -77,14 +82,10 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
private static final String[] PROJECTION = new String[] {
KeyRings.MASTER_KEY_ID,
KeyRings.PUBKEY_DATA,
KeyRings.PRIVKEY_DATA,
KeyRings.HAS_ANY_SECRET
};
private static final int INDEX_MASTER_KEY_ID = 0;
private static final int INDEX_PUBKEY_DATA = 1;
private static final int INDEX_SECKEY_DATA = 2;
private static final int INDEX_HAS_ANY_SECRET = 3;
private static final int INDEX_HAS_ANY_SECRET = 1;
public BackupOperation(Context context, DatabaseInteractor databaseInteractor, Progressable
progressable) {
@@ -230,16 +231,16 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
// For each public masterKey id
while (!cursor.isAfterLast()) {
long keyId = cursor.getLong(INDEX_MASTER_KEY_ID);
log.add(LogType.MSG_BACKUP_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(keyId));
long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID);
log.add(LogType.MSG_BACKUP_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(masterKeyId));
if (writePublicKeyToStream(log, outStream, cursor)) {
if (writePublicKeyToStream(masterKeyId, log, outStream)) {
okPublic += 1;
boolean hasSecret = cursor.getInt(INDEX_HAS_ANY_SECRET) > 0;
if (exportSecret && hasSecret) {
log.add(LogType.MSG_BACKUP_SECRET, 2, KeyFormattingUtils.beautifyKeyId(keyId));
if (writeSecretKeyToStream(log, outStream, cursor)) {
log.add(LogType.MSG_BACKUP_SECRET, 2, KeyFormattingUtils.beautifyKeyId(masterKeyId));
if (writeSecretKeyToStream(masterKeyId, log, outStream)) {
okSecret += 1;
}
}
@@ -267,17 +268,16 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
return true;
}
private boolean writePublicKeyToStream(OperationLog log, OutputStream outStream, Cursor cursor)
throws IOException {
private boolean writePublicKeyToStream(long masterKeyId, OperationLog log, OutputStream outStream) throws IOException {
ArmoredOutputStream arOutStream = null;
try {
arOutStream = new ArmoredOutputStream(outStream);
byte[] data = cursor.getBlob(INDEX_PUBKEY_DATA);
CanonicalizedKeyRing ring = UncachedKeyRing.decodeFromData(data).canonicalize(log, 2, true);
byte[] data = mDatabaseInteractor.getPublicKeyRingData(masterKeyId);
UncachedKeyRing uncachedKeyRing = UncachedKeyRing.decodeFromData(data);
CanonicalizedPublicKeyRing ring = (CanonicalizedPublicKeyRing) uncachedKeyRing.canonicalize(log, 2, true);
ring.encode(arOutStream);
} catch (PgpGeneralException e) {
} catch (PgpGeneralException | NotFoundException e) {
log.add(LogType.MSG_UPLOAD_ERROR_IO, 2);
} finally {
if (arOutStream != null) {
@@ -287,17 +287,17 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
return true;
}
private boolean writeSecretKeyToStream(OperationLog log, OutputStream outStream, Cursor cursor)
private boolean writeSecretKeyToStream(long masterKeyId, OperationLog log, OutputStream outStream)
throws IOException {
ArmoredOutputStream arOutStream = null;
try {
arOutStream = new ArmoredOutputStream(outStream);
byte[] data = cursor.getBlob(INDEX_SECKEY_DATA);
CanonicalizedKeyRing ring = UncachedKeyRing.decodeFromData(data).canonicalize(log, 2, true);
byte[] data = mDatabaseInteractor.getSecretKeyRingData(masterKeyId);
UncachedKeyRing uncachedKeyRing = UncachedKeyRing.decodeFromData(data);
CanonicalizedSecretKeyRing ring = (CanonicalizedSecretKeyRing) uncachedKeyRing.canonicalize(log, 2, true);
ring.encode(arOutStream);
} catch (PgpGeneralException e) {
} catch (PgpGeneralException | NotFoundException e) {
log.add(LogType.MSG_UPLOAD_ERROR_IO, 2);
} finally {
if (arOutStream != null) {

View File

@@ -238,6 +238,16 @@ public class DatabaseInteractor {
return mContentResolver;
}
public byte[] getPublicKeyRingData(long masterKeyId) throws NotFoundException {
return (byte[]) getGenericData(KeychainContract.KeyRingData.buildPublicKeyRingUri(masterKeyId),
KeyRingData.KEY_RING_DATA, FIELD_TYPE_BLOB);
}
public byte[] getSecretKeyRingData(long masterKeyId) throws NotFoundException {
return (byte[]) getGenericData(KeychainContract.KeyRingData.buildSecretKeyRingUri(masterKeyId),
KeyRingData.KEY_RING_DATA, FIELD_TYPE_BLOB);
}
public static class NotFoundException extends Exception {
public NotFoundException() {
}