From 8ad2d56f1c8aef9f112853dcde5f865308582849 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 16 Apr 2018 17:37:29 +0200 Subject: [PATCH] Add option to BackupOperation to export only secret keys --- .../keychain/operations/BackupOperation.java | 17 ++++++++++++----- .../keychain/remote/OpenPgpService.java | 2 +- .../keychain/service/BackupKeyringParcel.java | 10 ++++++++-- .../keychain/ui/BackupCodeFragment.java | 2 +- .../operations/BackupOperationTest.java | 8 ++++---- 5 files changed, 26 insertions(+), 13 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 8e40ee114..7f51a9e9b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/BackupOperation.java @@ -129,8 +129,8 @@ public class BackupOperation extends BaseOperation { } CountingOutputStream outStream = new CountingOutputStream(new BufferedOutputStream(plainOut)); - boolean backupSuccess = exportKeysToStream( - log, backupInput.getMasterKeyIds(), backupInput.getExportSecret(), outStream); + boolean backupSuccess = exportKeysToStream(log, backupInput.getMasterKeyIds(), + backupInput.getExportSecret(), backupInput.getExportPublic(), outStream); if (!backupSuccess) { // if there was an error, it will be in the log so we just have to return @@ -214,7 +214,8 @@ public class BackupOperation extends BaseOperation { pgpSignEncryptData, CryptoInputParcel.createCryptoInputParcel(), inputData, outStream); } - boolean exportKeysToStream(OperationLog log, long[] masterKeyIds, boolean exportSecret, OutputStream outStream) { + boolean exportKeysToStream(OperationLog log, long[] masterKeyIds, boolean exportSecret, boolean exportPublic, + OutputStream outStream) { // noinspection unused TODO use these in a log entry int okSecret = 0, okPublic = 0; @@ -240,9 +241,15 @@ public class BackupOperation extends BaseOperation { long masterKeyId = cursor.getLong(INDEX_MASTER_KEY_ID); log.add(LogType.MSG_BACKUP_PUBLIC, 1, KeyFormattingUtils.beautifyKeyId(masterKeyId)); - if (writePublicKeyToStream(masterKeyId, log, outStream)) { - okPublic += 1; + boolean publicKeyWriteOk = false; + if (exportPublic) { + publicKeyWriteOk = writePublicKeyToStream(masterKeyId, log, outStream); + if (publicKeyWriteOk) { + okPublic += 1; + } + } + if (publicKeyWriteOk || !exportPublic) { boolean hasSecret = cursor.getInt(INDEX_HAS_ANY_SECRET) > 0; if (exportSecret && hasSecret) { log.add(LogType.MSG_BACKUP_SECRET, 2, KeyFormattingUtils.beautifyKeyId(masterKeyId)); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index 1fcba550f..b4cda1d77 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -731,7 +731,7 @@ public class OpenPgpService extends Service { // the backup code is cached in CryptoInputParcelCacheService, now we can proceed BackupKeyringParcel input = BackupKeyringParcel - .createBackupKeyringParcel(masterKeyIds, backupSecret, true, enableAsciiArmorOutput, null); + .create(masterKeyIds, backupSecret, true, enableAsciiArmorOutput, null); BackupOperation op = new BackupOperation(this, mKeyRepository, null); ExportResult pgpResult = op.execute(input, inputParcel, outputStream); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/BackupKeyringParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/BackupKeyringParcel.java index ad9f3793d..78b67c2d4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/BackupKeyringParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/BackupKeyringParcel.java @@ -31,14 +31,20 @@ public abstract class BackupKeyringParcel implements Parcelable { @SuppressWarnings("mutable") public abstract long[] getMasterKeyIds(); public abstract boolean getExportSecret(); + public abstract boolean getExportPublic(); public abstract boolean getIsEncrypted(); public abstract boolean getEnableAsciiArmorOutput(); @Nullable public abstract Uri getOutputUri(); - public static BackupKeyringParcel createBackupKeyringParcel(long[] masterKeyIds, boolean exportSecret, + public static BackupKeyringParcel create(long[] masterKeyIds, boolean exportSecret, boolean isEncrypted, boolean enableAsciiArmorOutput, Uri outputUri) { return new AutoValue_BackupKeyringParcel( - masterKeyIds, exportSecret, isEncrypted, enableAsciiArmorOutput, outputUri); + masterKeyIds, exportSecret, true, isEncrypted, enableAsciiArmorOutput, outputUri); + } + + public static BackupKeyringParcel createExportAutocryptSetupMessage(long[] masterKeyIds) { + return new AutoValue_BackupKeyringParcel( + masterKeyIds, true, false, true, true, null); } } \ No newline at end of file diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/BackupCodeFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/BackupCodeFragment.java index 1a15adafc..1f7b12820 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/BackupCodeFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/BackupCodeFragment.java @@ -610,7 +610,7 @@ public class BackupCodeFragment extends CryptoOperationFragment