From 623964701bd6493e68d5f6a1f070ee17b27be1e3 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 20 Feb 2017 21:32:54 +0100 Subject: [PATCH] get rid of direct query for blob from backup operation --- .../keychain/operations/BackupOperation.java | 42 +++++++++---------- .../keychain/provider/DatabaseInteractor.java | 10 +++++ 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java index 7534ee2bf..dacfc427f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java @@ -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 { 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 { // 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 { 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 { 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) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/DatabaseInteractor.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/DatabaseInteractor.java index 0b5fe964e..be3e9154d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/DatabaseInteractor.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/DatabaseInteractor.java @@ -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() { }