use autovalue for ChangeUnlockParcel

This commit is contained in:
Vincent Breitmoser
2017-05-22 12:43:30 +02:00
parent c444ccb781
commit d3357ccf5c
14 changed files with 51 additions and 101 deletions

View File

@@ -47,7 +47,7 @@ public class ChangeUnlockOperation extends BaseReadWriteOperation<ChangeUnlockPa
OperationResult.OperationLog log = new OperationResult.OperationLog();
log.add(OperationResult.LogType.MSG_ED, 0);
if (unlockParcel == null || unlockParcel.mMasterKeyId == null) {
if (unlockParcel == null || unlockParcel.getMasterKeyId() == null) {
log.add(OperationResult.LogType.MSG_ED_ERROR_NO_PARCEL, 1);
return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null);
}
@@ -60,10 +60,10 @@ public class ChangeUnlockOperation extends BaseReadWriteOperation<ChangeUnlockPa
try {
log.add(OperationResult.LogType.MSG_ED_FETCHING, 1,
KeyFormattingUtils.convertKeyIdToHex(unlockParcel.mMasterKeyId));
KeyFormattingUtils.convertKeyIdToHex(unlockParcel.getMasterKeyId()));
CanonicalizedSecretKeyRing secRing =
mKeyRepository.getCanonicalizedSecretKeyRing(unlockParcel.mMasterKeyId);
mKeyRepository.getCanonicalizedSecretKeyRing(unlockParcel.getMasterKeyId());
modifyResult = keyOperations.modifyKeyRingPassphrase(secRing, cryptoInput, unlockParcel);
if (modifyResult.isPending()) {

View File

@@ -1064,7 +1064,7 @@ public class PgpKeyOperation {
indent += 1;
sKR = applyNewPassphrase(sKR, masterPublicKey, cryptoInput.getPassphrase(),
saveParcel.getChangeUnlockParcel().mNewPassphrase, log, indent);
saveParcel.getChangeUnlockParcel().getNewPassphrase(), log, indent);
if (sKR == null) {
// The error has been logged above, just return a bad state
return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null);
@@ -1204,7 +1204,8 @@ public class PgpKeyOperation {
OperationLog log = new OperationLog();
int indent = 0;
if (changeUnlockParcel.mMasterKeyId == null || changeUnlockParcel.mMasterKeyId != wsKR.getMasterKeyId()) {
Long masterKeyId = changeUnlockParcel.getMasterKeyId();
if (masterKeyId == null || masterKeyId != wsKR.getMasterKeyId()) {
log.add(LogType.MSG_MF_ERROR_KEYID, indent);
return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null);
}
@@ -1219,7 +1220,7 @@ public class PgpKeyOperation {
PGPSecretKey masterSecretKey = sKR.getSecretKey();
PGPPublicKey masterPublicKey = masterSecretKey.getPublicKey();
// Make sure the fingerprint matches
if (changeUnlockParcel.mFingerprint == null || !Arrays.equals(changeUnlockParcel.mFingerprint,
if (changeUnlockParcel.getFingerprint()== null || !Arrays.equals(changeUnlockParcel.getFingerprint(),
masterSecretKey.getPublicKey().getFingerprint())) {
log.add(LogType.MSG_MF_ERROR_FINGERPRINT, indent);
return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null);
@@ -1245,7 +1246,7 @@ public class PgpKeyOperation {
try {
sKR = applyNewPassphrase(sKR, masterPublicKey, cryptoInput.getPassphrase(),
changeUnlockParcel.mNewPassphrase, log, indent);
changeUnlockParcel.getNewPassphrase(), log, indent);
if (sKR == null) {
// The error has been logged above, just return a bad state
return new PgpEditKeyResult(PgpEditKeyResult.RESULT_ERROR, log, null);

View File

@@ -19,70 +19,28 @@
package org.sufficientlysecure.keychain.service;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import com.google.auto.value.AutoValue;
import org.sufficientlysecure.keychain.util.Passphrase;
public class ChangeUnlockParcel implements Parcelable {
@AutoValue
public abstract class ChangeUnlockParcel implements Parcelable {
@Nullable
public abstract Long getMasterKeyId();
@Nullable
public abstract byte[] getFingerprint();
public abstract Passphrase getNewPassphrase();
// the master key id of keyring.
public Long mMasterKeyId;
// the key fingerprint, for safety.
public byte[] mFingerprint;
// The new passphrase to use
public final Passphrase mNewPassphrase;
public ChangeUnlockParcel(Passphrase newPassphrase) {
mNewPassphrase = newPassphrase;
public static ChangeUnlockParcel createChangeUnlockParcel(Long masterKeyId, byte[] fingerprint,
Passphrase newPassphrase) {
return new AutoValue_ChangeUnlockParcel(masterKeyId, fingerprint, newPassphrase);
}
public ChangeUnlockParcel(Long masterKeyId, byte[] fingerprint, Passphrase newPassphrase) {
if (newPassphrase == null) {
throw new AssertionError("newPassphrase must be non-null. THIS IS A BUG!");
}
mMasterKeyId = masterKeyId;
mFingerprint = fingerprint;
mNewPassphrase = newPassphrase;
public static ChangeUnlockParcel createUnLockParcelForNewKey(Passphrase newPassphrase) {
return new AutoValue_ChangeUnlockParcel(null, null, newPassphrase);
}
public ChangeUnlockParcel(Parcel source) {
mMasterKeyId = source.readInt() != 0 ? source.readLong() : null;
mFingerprint = source.createByteArray();
mNewPassphrase = source.readParcelable(Passphrase.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel destination, int flags) {
destination.writeInt(mMasterKeyId == null ? 0 : 1);
if (mMasterKeyId != null) {
destination.writeLong(mMasterKeyId);
}
destination.writeByteArray(mFingerprint);
destination.writeParcelable(mNewPassphrase, flags);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<ChangeUnlockParcel> CREATOR = new Creator<ChangeUnlockParcel>() {
public ChangeUnlockParcel createFromParcel(final Parcel source) {
return new ChangeUnlockParcel(source);
}
public ChangeUnlockParcel[] newArray(final int size) {
return new ChangeUnlockParcel[size];
}
};
public String toString() {
String out = "mMasterKeyId: " + mMasterKeyId + "\n";
out += "passphrase (" + mNewPassphrase + ")";
return out;
}
}

View File

@@ -109,10 +109,6 @@ public class SaveKeyringParcel implements Parcelable {
}
public ChangeUnlockParcel getChangeUnlockParcel() {
if(mNewUnlock != null) {
mNewUnlock.mMasterKeyId = mMasterKeyId;
mNewUnlock.mFingerprint = mFingerprint;
}
return mNewUnlock;
}

View File

@@ -307,12 +307,13 @@ public class CreateKeyFinalFragment extends Fragment {
createKeyActivity.mSecurityTokenAuth.addToSaveKeyringParcel(saveKeyringParcel, KeyFlags.AUTHENTICATION);
// use empty passphrase
saveKeyringParcel.setNewUnlock(new ChangeUnlockParcel(new Passphrase()));
saveKeyringParcel.setNewUnlock(ChangeUnlockParcel.createUnLockParcelForNewKey(new Passphrase()));
} else {
Constants.addDefaultSubkeys(saveKeyringParcel);
if (createKeyActivity.mPassphrase != null) {
saveKeyringParcel.setNewUnlock(new ChangeUnlockParcel(createKeyActivity.mPassphrase));
saveKeyringParcel.setNewUnlock(
ChangeUnlockParcel.createUnLockParcelForNewKey(createKeyActivity.mPassphrase));
} else {
saveKeyringParcel.setNewUnlock(null);
}

View File

@@ -341,7 +341,8 @@ public class EditKeyFragment extends QueueingCryptoOperationFragment<SaveKeyring
Bundle data = message.getData();
// cache new returned passphrase!
mSaveKeyringParcel.setNewUnlock(new ChangeUnlockParcel(
mSaveKeyringParcel.setNewUnlock(ChangeUnlockParcel.createChangeUnlockParcel(
mSaveKeyringParcel.mMasterKeyId, mSaveKeyringParcel.mFingerprint,
(Passphrase) data.getParcelable(SetPassphraseDialogFragment.MESSAGE_NEW_PASSPHRASE)));
}
}

View File

@@ -474,9 +474,8 @@ public class ViewKeyActivity extends BaseSecurityTokenActivity implements
Bundle data = message.getData();
// use new passphrase!
mChangeUnlockParcel = new ChangeUnlockParcel(
mMasterKeyId,
mFingerprint,
mChangeUnlockParcel = ChangeUnlockParcel.createChangeUnlockParcel(
mMasterKeyId, mFingerprint,
(Passphrase) data.getParcelable(SetPassphraseDialogFragment.MESSAGE_NEW_PASSPHRASE)
);