make unencrypted backup an explicit parameter

This commit is contained in:
Vincent Breitmoser
2016-05-17 20:59:07 +02:00
parent f833b0f2ef
commit 1da8e4c1ed
4 changed files with 21 additions and 16 deletions

View File

@@ -20,7 +20,6 @@ package org.sufficientlysecure.keychain.operations;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
@@ -113,23 +112,25 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
log.add(LogType.MSG_BACKUP_ALL, 0); log.add(LogType.MSG_BACKUP_ALL, 0);
} }
if (backupInput.mIsEncrypted && cryptoInput == null) {
throw new IllegalStateException("Encrypted backup must supply cryptoInput parameter");
}
try { try {
boolean nonEncryptedOutput = cryptoInput == null;
Uri plainUri = null; Uri plainUri = null;
OutputStream plainOut; OutputStream plainOut;
if (nonEncryptedOutput && backupInput.mOutputUri == null) { if (backupInput.mIsEncrypted) {
plainOut = outputStream;
} else if (nonEncryptedOutput) {
plainOut = mContext.getContentResolver().openOutputStream(backupInput.mOutputUri);
} else {
plainUri = TemporaryFileProvider.createFile(mContext); plainUri = TemporaryFileProvider.createFile(mContext);
plainOut = mContext.getContentResolver().openOutputStream(plainUri); plainOut = mContext.getContentResolver().openOutputStream(plainUri);
} else {
if (backupInput.mOutputUri == null) {
throw new IllegalArgumentException("Unencrypted export to output stream is not supported!");
} else {
plainOut = mContext.getContentResolver().openOutputStream(backupInput.mOutputUri);
}
} }
int exportedDataSize; int exportedDataSize;
{ // export key data, and possibly return if we don't encrypt { // export key data, and possibly return if we don't encrypt
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(plainOut)); DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(plainOut));

View File

@@ -757,7 +757,7 @@ public class OpenPgpService extends Service {
// after user interaction with RemoteBackupActivity, // after user interaction with RemoteBackupActivity,
// the backup code is cached in CryptoInputParcelCacheService, now we can proceed // the backup code is cached in CryptoInputParcelCacheService, now we can proceed
BackupKeyringParcel input = new BackupKeyringParcel(masterKeyIds, backupSecret, null); BackupKeyringParcel input = new BackupKeyringParcel(masterKeyIds, backupSecret, true, null);
BackupOperation op = new BackupOperation(this, mProviderHelper, null); BackupOperation op = new BackupOperation(this, mProviderHelper, null);
ExportResult pgpResult = op.execute(input, inputParcel, outputStream); ExportResult pgpResult = op.execute(input, inputParcel, outputStream);

View File

@@ -29,14 +29,16 @@ import org.sufficientlysecure.keychain.util.Passphrase;
public class BackupKeyringParcel implements Parcelable { public class BackupKeyringParcel implements Parcelable {
public Uri mCanonicalizedPublicKeyringUri; public Uri mCanonicalizedPublicKeyringUri;
public boolean mExportSecret; public final boolean mExportSecret;
public long mMasterKeyIds[]; public final boolean mIsEncrypted;
public Uri mOutputUri; public final long mMasterKeyIds[];
public final Uri mOutputUri;
public BackupKeyringParcel(long[] masterKeyIds, boolean exportSecret, Uri outputUri) { public BackupKeyringParcel(long[] masterKeyIds, boolean exportSecret, boolean isEncrypted, Uri outputUri) {
mMasterKeyIds = masterKeyIds; mMasterKeyIds = masterKeyIds;
mExportSecret = exportSecret; mExportSecret = exportSecret;
mOutputUri = outputUri; mOutputUri = outputUri;
mIsEncrypted = isEncrypted;
} }
protected BackupKeyringParcel(Parcel in) { protected BackupKeyringParcel(Parcel in) {
@@ -44,6 +46,7 @@ public class BackupKeyringParcel implements Parcelable {
mExportSecret = in.readByte() != 0x00; mExportSecret = in.readByte() != 0x00;
mOutputUri = (Uri) in.readValue(Uri.class.getClassLoader()); mOutputUri = (Uri) in.readValue(Uri.class.getClassLoader());
mMasterKeyIds = in.createLongArray(); mMasterKeyIds = in.createLongArray();
mIsEncrypted = in.readInt() != 0;
} }
@Override @Override
@@ -57,6 +60,7 @@ public class BackupKeyringParcel implements Parcelable {
dest.writeByte((byte) (mExportSecret ? 0x01 : 0x00)); dest.writeByte((byte) (mExportSecret ? 0x01 : 0x00));
dest.writeValue(mOutputUri); dest.writeValue(mOutputUri);
dest.writeLongArray(mMasterKeyIds); dest.writeLongArray(mMasterKeyIds);
dest.writeInt(mIsEncrypted ? 1 : 0);
} }
public static final Parcelable.Creator<BackupKeyringParcel> CREATOR = new Parcelable.Creator<BackupKeyringParcel>() { public static final Parcelable.Creator<BackupKeyringParcel> CREATOR = new Parcelable.Creator<BackupKeyringParcel>() {

View File

@@ -606,7 +606,7 @@ public class BackupCodeFragment extends CryptoOperationFragment<BackupKeyringPar
@Nullable @Nullable
@Override @Override
public BackupKeyringParcel createOperationInput() { public BackupKeyringParcel createOperationInput() {
return new BackupKeyringParcel(mMasterKeyIds, mExportSecret, mCachedBackupUri); return new BackupKeyringParcel(mMasterKeyIds, mExportSecret, true, mCachedBackupUri);
} }
@Override @Override