Add option to BackupOperation to export only secret keys

This commit is contained in:
Vincent Breitmoser
2018-04-16 17:37:29 +02:00
parent 347ab8fa15
commit 8ad2d56f1c
5 changed files with 26 additions and 13 deletions

View File

@@ -129,8 +129,8 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
}
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<BackupKeyringParcel> {
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<BackupKeyringParcel> {
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));

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -610,7 +610,7 @@ public class BackupCodeFragment extends CryptoOperationFragment<BackupKeyringPar
@Override
public BackupKeyringParcel createOperationInput() {
return BackupKeyringParcel
.createBackupKeyringParcel(mMasterKeyIds, mExportSecret, true, true, mCachedBackupUri);
.create(mMasterKeyIds, mExportSecret, true, true, mCachedBackupUri);
}
@Override

View File

@@ -157,7 +157,7 @@ public class BackupOperationTest {
assertTrue("second keyring has local certification", checkForLocal(mStaticRing2));
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean result = op.exportKeysToStream(new OperationLog(), null, false, out);
boolean result = op.exportKeysToStream(new OperationLog(), null, false, true, out);
assertTrue("export must be a success", result);
@@ -194,7 +194,7 @@ public class BackupOperationTest {
}
out = new ByteArrayOutputStream();
result = op.exportKeysToStream(new OperationLog(), null, true, out);
result = op.exportKeysToStream(new OperationLog(), null, true, true, out);
assertTrue("export must be a success", result);
@@ -252,7 +252,7 @@ public class BackupOperationTest {
BackupOperation op = new BackupOperation(spyApplication,
KeyWritableRepository.create(RuntimeEnvironment.application), null);
BackupKeyringParcel parcel = BackupKeyringParcel.createBackupKeyringParcel(
BackupKeyringParcel parcel = BackupKeyringParcel.create(
new long[] { mStaticRing1.getMasterKeyId() }, false, false, true, fakeOutputUri);
ExportResult result = op.execute(parcel, null);
@@ -309,7 +309,7 @@ public class BackupOperationTest {
BackupOperation op = new BackupOperation(spyApplication,
KeyWritableRepository.create(RuntimeEnvironment.application), null);
BackupKeyringParcel parcel = BackupKeyringParcel.createBackupKeyringParcel(
BackupKeyringParcel parcel = BackupKeyringParcel.create(
new long[] { mStaticRing1.getMasterKeyId() }, false, true, true, fakeOutputUri);
CryptoInputParcel inputParcel = CryptoInputParcel.createCryptoInputParcel(passphrase);
ExportResult result = op.execute(parcel, inputParcel);