Unverified Commit ed7f2a11 authored by Felix Förtsch's avatar Felix Förtsch
Browse files

add work-in-progress

parent db6427c9
......@@ -4,6 +4,7 @@ import android.content.Context;
import android.util.Pair;
import androidx.annotation.NonNull;
import com.google.gson.JsonObject;
import com.nexenio.rxkeystore.util.ByteLoggingUtil;
import de.culture4life.luca.Manager;
import de.culture4life.luca.checkin.CheckInManager;
import de.culture4life.luca.crypto.AsymmetricCipherProvider;
......@@ -16,20 +17,17 @@ import de.culture4life.luca.registration.RegistrationData;
import de.culture4life.luca.registration.RegistrationManager;
import de.culture4life.luca.ui.qrcode.QrCodeData;
import de.culture4life.luca.util.SerializationUtil;
import de.culture4life.luca.util.TimeUtil;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Single;
import org.jetbrains.annotations.NotNull;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.nio.ByteBuffer;
import java.security.*;
import java.security.interfaces.ECPublicKey;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static de.culture4life.luca.util.SerializationUtil.serializeToBase64;
import static java.lang.String.valueOf;
public class FakeManager extends Manager {
......@@ -59,7 +57,6 @@ public class FakeManager extends Manager {
).andThen(Completable.fromAction(() -> this.context = context));
}
public UUID doFakeRegistration() {
// Create Fake RegistrationData
......@@ -110,11 +107,30 @@ public class FakeManager extends Manager {
return UUID.fromString(jsonObject.get("userId").getAsString());
}
// private void doFakeCheckIn() {
// QrCodeData qrCodeData = generateFakeQrCodeData();
//
// checkInManager.checkIn(UUID.fromString("09eb8d41-1914-4950-9526-36ebc6ad58fe"), qrCodeData);
// }
public void generateCheckin() {
// TODO: Scanner ID aus QR-Code einlesen
UUID scannerId = UUID.fromString("09eb8d41-1914-4950-9526-36ebc6ad58fe");
ECPublicKey publicKey = checkInManager.getLocationPublicKey(scannerId).blockingGet();
DeviceData deviceData = new DeviceData();
VenueData venueData = new VenueData(cryptoManager, publicKey, deviceData);
CheckinData checkinData = new CheckinData(deviceData, venueData);
}
public static String generateRandomData(Integer length) {
SecureRandom random = new SecureRandom();
byte[] random_bytes = new byte[length];
random.nextBytes(random_bytes);
return SerializationUtil.serializeToBase64(random_bytes).blockingGet();
}
// private QrCodeData generateFakeQrCodeData() {
// UUID userId = UUID.fromString("80caf273-2bbd-42fa-b1d7-0e5309d40d82");
......@@ -150,34 +166,7 @@ public class FakeManager extends Manager {
// }
// private CheckInRequestData generateCheckInData(@NonNull QrCodeData qrCodeData, @NonNull PublicKey locationPublicKey) {
// CheckInRequestData checkInRequestData = new CheckInRequestData();
// SecureRandom random = new SecureRandom();
//
// checkInRequestData.setDeviceType(1);
// checkInRequestData.setUnixTimestamp(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
//
// byte[] traceId = new byte[16];
// random.nextBytes(traceId);
// String serialisedTraceId = serializeToBase64(traceId).blockingGet();
// checkInRequestData.setTraceId(serialisedTraceId);
//
// KeyPair scannerEphemeralKeyPair = cryptoManager.generateScannerEphemeralKeyPair().blockingGet();
// cryptoManager.persistScannerEphemeralKeyPair(scannerEphemeralKeyPair).blockingAwait();
//
// String serializedScannerPublicKey = AsymmetricCipherProvider.encode((ECPublicKey) scannerEphemeralKeyPair.getPublic())
// .flatMap(SerializationUtil::serializeToBase64).blockingGet();
// checkInRequestData.setScannerEphemeralPublicKey(serializedScannerPublicKey);
//
// byte[] iv = cryptoManager.generateSecureRandomData(16).blockingGet();
// String encodedIv = serializeToBase64(iv).blockingGet();
// checkInRequestData.setIv(encodedIv);
//
// byte[] diffieHellmanSecret = cryptoManager.getAsymmetricCipherProvider()
// .generateSecret(scannerEphemeralKeyPair.getPrivate(), locationPublicKey).blockingGet();
//
// byte[] encryptedQrCodeData = encryptQrCodeData(qrCodeData, iv, diffieHellmanSecret).blockingGet();
// String serialisedEncryptedQrCodeData = serializeToBase64(encryptedQrCodeData).blockingGet();
// checkInRequestData.setReEncryptedQrCodeData(serialisedEncryptedQrCodeData);
//
// String serialisedMac = createQrCodeDataMac(encryptedQrCodeData, diffieHellmanSecret)
// .flatMap(SerializationUtil::serializeToBase64).blockingGet();
......@@ -199,4 +188,104 @@ public class FakeManager extends Manager {
return registrationData;
}
}
class CheckinData {
// Device
String traceId;
String timestamp;
String deviceType;
// Venue
String scannerId;
String data;
String iv;
String mac;
String publicKey;
public CheckinData(DeviceData deviceData, VenueData venueData) {
this.traceId = deviceData.traceId;
this.timestamp = deviceData.timestamp;
this.deviceType = deviceData.deviceType;
this.scannerId = scannerId;
// check_in_data = version || key_id || ephemeral_keys.public || verification_tag || enc_data
this.data = data;
this.iv = iv;
this.mac = mac;
this.publicKey = publicKey;
}
}
class VenueData {
final String iv = FakeManager.generateRandomData(16);
String publicKey;
String data;
String hmac;
public VenueData(CryptoManager cryptoManager, ECPublicKey publicKey, DeviceData deviceData) {
KeyPair ephScannerKeys = cryptoManager.generateScannerEphemeralKeyPair().blockingGet();
AsymmetricCipherProvider asymmetricCipherProvider = cryptoManager.getAsymmetricCipherProvider();
// dh = Diffie Hellman
byte[] dhKeys = asymmetricCipherProvider
.generateSecret(ephScannerKeys.getPrivate(), publicKey).blockingGet();
String dhKeysString = ByteLoggingUtil.bytesToHex(dhKeys);
MessageDigest sha256 = getSha256();
String encKeyString = dhKeysString + "01";
String authKeyString = dhKeysString + "02";
byte[] encKey = sha256.digest(encKeyString.getBytes());
byte[] authKey = sha256.digest(authKeyString.getBytes());
byte[] encryptedVenueData = cryptoManager.generateDataEncryptionSecret(dhKeys)
.flatMap(CryptoManager::createKeyFromSecret)
.flatMap(encryptionKey -> Single.fromCallable(
() -> ByteBuffer.allocate(75)
.put((byte) 3)
.put(deviceData.keyId.getBytes())
.put(deviceData.publicKey.getBytes())
.put(deviceData.verificationTag.getBytes())
.put(deviceData.data.getBytes())
.array())
.flatMap(encodedQrCodeData -> cryptoManager.getSymmetricCipherProvider()
.encrypt(encodedQrCodeData, iv.getBytes(), encryptionKey)))
.blockingGet();
// String serialisedEncryptedQrCodeData = serializeToBase64(encryptedQrCodeData).blockingGet();
// checkInRequestData.setReEncryptedQrCodeData(serialisedEncryptedQrCodeData);
String serializedScannerPublicKey = AsymmetricCipherProvider
.encode((ECPublicKey) ephScannerKeys.getPublic())
.flatMap(SerializationUtil::serializeToBase64).blockingGet();
String clearTextData = "03" + deviceData.keyId + publicKey + deviceData.verificationTag;
}
MessageDigest getSha256() {
MessageDigest sha256 = null;
try {
sha256 = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return sha256;
}
}
class DeviceData {
final String version = "3";
final String deviceType = "1";
final String timestamp = valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
final String data = FakeManager.generateRandomData(32);
final String keyId = FakeManager.generateRandomData(1);
final String traceId = FakeManager.generateRandomData(16);
final String publicKey = FakeManager.generateRandomData(33);
final String verificationTag = FakeManager.generateRandomData(8);
}
\ No newline at end of file
......@@ -74,9 +74,18 @@ public class FakeCheckinActivity extends BaseActivity {
}
private void fakeRegistrationAndCheckIn(Integer registrationCount) {
FakeManager fakeManager = new FakeManager(
application.getRegistrationManager(),
application.getCryptoManager(),
application.getNetworkManager(),
application.getCheckInManager());
for (int i = 0; i < registrationCount; i++) {
System.out.println("FakeManager: Do fake registration");
System.out.println("FakeManager: Do fake checkin");
fakeManager.doFakeRegistration();
fakeManager.generateCheckin();
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment