generalize nfc crypto input structure

This commit is contained in:
Vincent Breitmoser
2015-03-18 18:25:44 +01:00
parent 4499caef1e
commit aca54e31ea
18 changed files with 883 additions and 235 deletions

View File

@@ -26,31 +26,31 @@ import java.util.ArrayList;
import java.util.Date;
import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute;
import org.sufficientlysecure.keychain.service.input.CryptoOperationParcel;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
/**
* This class is a a transferable representation for a number of keyrings to
* be certified.
*/
public class CertifyActionsParcel extends CryptoOperationParcel {
public class CertifyActionsParcel implements Parcelable {
// the master key id to certify with
final public long mMasterKeyId;
public CertifyLevel mLevel;
public ArrayList<CertifyAction> mCertifyActions = new ArrayList<>();
public CryptoInputParcel mCryptoInput;
public CertifyActionsParcel(Date operationTime, long masterKeyId) {
super(operationTime);
mMasterKeyId = masterKeyId;
mCryptoInput = new CryptoInputParcel(operationTime);
mLevel = CertifyLevel.DEFAULT;
}
public CertifyActionsParcel(Parcel source) {
super(source);
mMasterKeyId = source.readLong();
mCryptoInput = source.readParcelable(CertifyActionsParcel.class.getClassLoader());
// just like parcelables, this is meant for ad-hoc IPC only and is NOT portable!
mLevel = CertifyLevel.values()[source.readInt()];
@@ -63,9 +63,8 @@ public class CertifyActionsParcel extends CryptoOperationParcel {
@Override
public void writeToParcel(Parcel destination, int flags) {
super.writeToParcel(destination, flags);
destination.writeLong(mMasterKeyId);
destination.writeParcelable(mCryptoInput, 0);
destination.writeInt(mLevel.ordinal());
destination.writeSerializable(mCertifyActions);

View File

@@ -0,0 +1,81 @@
package org.sufficientlysecure.keychain.service.input;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import android.os.Parcel;
import android.os.Parcelable;
/** This is a base class for the input of crypto operations.
*
*/
public class CryptoInputParcel implements Parcelable {
Date mSignatureTime;
// this map contains both decrypted session keys and signed hashes to be
// used in the crypto operation described by this parcel.
private HashMap<ByteBuffer,byte[]> mCryptoData = new HashMap<>();
public CryptoInputParcel(Date signatureTime) {
mSignatureTime = signatureTime == null ? new Date() : signatureTime;
}
protected CryptoInputParcel(Parcel source) {
mSignatureTime = new Date(source.readLong());
{
int count = source.readInt();
mCryptoData = new HashMap<>(count);
for (int i = 0; i < count; i++) {
byte[] key = source.createByteArray();
byte[] value = source.createByteArray();
mCryptoData.put(ByteBuffer.wrap(key), value);
}
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(mSignatureTime.getTime());
dest.writeInt(mCryptoData.size());
for (HashMap.Entry<ByteBuffer,byte[]> entry : mCryptoData.entrySet()) {
dest.writeByteArray(entry.getKey().array());
dest.writeByteArray(entry.getValue());
}
}
public void addCryptoData(byte[] hash, byte[] signedHash) {
mCryptoData.put(ByteBuffer.wrap(hash), signedHash);
}
public Map<ByteBuffer, byte[]> getCryptoData() {
return Collections.unmodifiableMap(mCryptoData);
}
public Date getSignatureTime() {
return mSignatureTime;
}
public static final Creator<CryptoInputParcel> CREATOR = new Creator<CryptoInputParcel>() {
public CryptoInputParcel createFromParcel(final Parcel source) {
return new CryptoInputParcel(source);
}
public CryptoInputParcel[] newArray(final int size) {
return new CryptoInputParcel[size];
}
};
}

View File

@@ -1,52 +0,0 @@
package org.sufficientlysecure.keychain.service.input;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.HashMap;
import android.os.Parcel;
import android.os.Parcelable;
/** This is a base class for the input of crypto operations.
*
*/
public abstract class CryptoOperationParcel implements Parcelable {
Date mOperationTime;
// this map contains both decrypted session keys and signed hashes to be
// used in the crypto operation described by this parcel.
HashMap<ByteBuffer,byte[]> mCryptoData;
protected CryptoOperationParcel(Date operationTime) {
mOperationTime = operationTime;
}
protected CryptoOperationParcel(Parcel source) {
mOperationTime = new Date(source.readLong());
{
int count = source.readInt();
mCryptoData = new HashMap<>(count);
for (int i = 0; i < count; i++) {
byte[] key = source.createByteArray();
byte[] value = source.createByteArray();
mCryptoData.put(ByteBuffer.wrap(key), value);
}
}
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(mOperationTime.getTime());
dest.writeInt(mCryptoData.size());
for (HashMap.Entry<ByteBuffer,byte[]> entry : mCryptoData.entrySet()) {
dest.writeByteArray(entry.getKey().array());
dest.writeByteArray(entry.getValue());
}
}
}

View File

@@ -0,0 +1,85 @@
package org.sufficientlysecure.keychain.service.input;
import java.util.Date;
import android.os.Parcel;
import android.os.Parcelable;
public class NfcOperationsParcel implements Parcelable {
public enum NfcOperationType {
NFC_SIGN, NFC_DECRYPT
}
public Date mSignatureTime;
public final NfcOperationType mType;
public final byte[][] mInputHash;
public final int[] mSignAlgo;
private NfcOperationsParcel(NfcOperationType type, byte[] inputHash, int signAlgo, Date signatureTime) {
mType = type;
mInputHash = new byte[][] { inputHash };
mSignAlgo = new int[] { signAlgo };
mSignatureTime = signatureTime;
}
public NfcOperationsParcel(Parcel source) {
mType = NfcOperationType.values()[source.readInt()];
{
int count = source.readInt();
mInputHash = new byte[count][];
mSignAlgo = new int[count];
for (int i = 0; i < count; i++) {
mInputHash[i] = source.createByteArray();
mSignAlgo[i] = source.readInt();
}
}
mSignatureTime = source.readInt() != 0 ? new Date(source.readLong()) : null;
}
public static NfcOperationsParcel createNfcSignOperation(
byte[] inputHash, int signAlgo, Date signatureTime) {
return new NfcOperationsParcel(NfcOperationType.NFC_SIGN, inputHash, signAlgo, signatureTime);
}
public static NfcOperationsParcel createNfcDecryptOperation(byte[] inputHash) {
return new NfcOperationsParcel(NfcOperationType.NFC_DECRYPT, inputHash, 0, null);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mType.ordinal());
dest.writeInt(mInputHash.length);
for (int i = 0; i < mInputHash.length; i++) {
dest.writeByteArray(mInputHash[i]);
dest.writeInt(mSignAlgo[i]);
}
if (mSignatureTime != null) {
dest.writeInt(1);
dest.writeLong(mSignatureTime.getTime());
} else {
dest.writeInt(0);
}
}
public static final Creator<NfcOperationsParcel> CREATOR = new Creator<NfcOperationsParcel>() {
public NfcOperationsParcel createFromParcel(final Parcel source) {
return new NfcOperationsParcel(source);
}
public NfcOperationsParcel[] newArray(final int size) {
return new NfcOperationsParcel[size];
}
};
}