From aca54e31eae450e7deec54cca6654ee202c7a90f Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 18 Mar 2015 18:25:44 +0100 Subject: generalize nfc crypto input structure --- .../keychain/service/input/CryptoInputParcel.java | 81 +++++++++++++++++++++ .../service/input/CryptoOperationParcel.java | 52 ------------- .../service/input/NfcOperationsParcel.java | 85 ++++++++++++++++++++++ 3 files changed, 166 insertions(+), 52 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/CryptoInputParcel.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/CryptoOperationParcel.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/NfcOperationsParcel.java (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/CryptoInputParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/CryptoInputParcel.java new file mode 100644 index 000000000..e02eda4b3 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/CryptoInputParcel.java @@ -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 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 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 getCryptoData() { + return Collections.unmodifiableMap(mCryptoData); + } + + public Date getSignatureTime() { + return mSignatureTime; + } + + public static final Creator CREATOR = new Creator() { + public CryptoInputParcel createFromParcel(final Parcel source) { + return new CryptoInputParcel(source); + } + + public CryptoInputParcel[] newArray(final int size) { + return new CryptoInputParcel[size]; + } + }; + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/CryptoOperationParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/CryptoOperationParcel.java deleted file mode 100644 index 2101755ad..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/CryptoOperationParcel.java +++ /dev/null @@ -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 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 entry : mCryptoData.entrySet()) { - dest.writeByteArray(entry.getKey().array()); - dest.writeByteArray(entry.getValue()); - } - } - -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/NfcOperationsParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/NfcOperationsParcel.java new file mode 100644 index 000000000..5d35e94e0 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/input/NfcOperationsParcel.java @@ -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 CREATOR = new Creator() { + public NfcOperationsParcel createFromParcel(final Parcel source) { + return new NfcOperationsParcel(source); + } + + public NfcOperationsParcel[] newArray(final int size) { + return new NfcOperationsParcel[size]; + } + }; + +} -- cgit v1.2.3