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

add fake registration

parent dac1c5ea
......@@ -8,6 +8,7 @@ import de.culture4life.luca.Manager;
import de.culture4life.luca.checkin.CheckInManager;
import de.culture4life.luca.crypto.AsymmetricCipherProvider;
import de.culture4life.luca.crypto.CryptoManager;
import de.culture4life.luca.crypto.SignatureProvider;
import de.culture4life.luca.network.NetworkManager;
import de.culture4life.luca.network.pojo.CheckInRequestData;
import de.culture4life.luca.network.pojo.ContactData;
......@@ -15,20 +16,21 @@ import de.culture4life.luca.network.pojo.UserRegistrationRequestData;
import de.culture4life.luca.registration.RegistrationData;
import de.culture4life.luca.registration.RegistrationManager;
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 io.reactivex.rxjava3.disposables.CompositeDisposable;
import okhttp3.*;
import javax.crypto.SecretKey;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.ECPublicKey;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static de.culture4life.luca.crypto.CryptoManager.concatenate;
import static de.culture4life.luca.util.SerializationUtil.serializeToBase64;
import static de.culture4life.luca.util.SerializationUtil.serializeToJson;
......@@ -61,64 +63,65 @@ public class FakeManager extends Manager {
}
public UUID doFakeRegistration() {
// Generate secrets
byte[] dataSecret = generateRandomData(16);
byte[] dataEncryptionSecret = cryptoManager.generateDataEncryptionSecret(dataSecret).blockingGet();
byte[] dataAuthenticationSecret = cryptoManager.generateDataAuthenticationSecret(dataSecret).blockingGet();
// Create fake ContactData and serialize it
ContactData contactData = new ContactData(getFakeRegistrationData());
String jsonContactData = SerializationUtil.serializeToJson(contactData).blockingGet();
// Encrypt the ContactData
SecretKey privateKey = CryptoManager.createKeyFromSecret(dataEncryptionSecret).blockingGet();
byte[] iv = generateRandomData(16);
byte[] encodedContactData = jsonContactData.getBytes(StandardCharsets.UTF_8);
byte[] encryptedContactData = cryptoManager.getSymmetricCipherProvider().encrypt(encodedContactData, iv, privateKey).blockingGet();
// Create HMAC for the ContactData
SecretKey dataAuthenticationKey = CryptoManager.createKeyFromSecret(dataAuthenticationSecret).blockingGet();
byte[] guestDataMac = cryptoManager.getMacProvider().sign(encryptedContactData, dataAuthenticationKey).blockingGet();
// Create guest data signature for the ContactData
byte[] data = concatenate(encryptedContactData, guestDataMac, iv).blockingGet();
KeyPair keyPair = cryptoManager.getAsymmetricCipherProvider().generateKeyPair("user_master_key_pair", context).blockingGet();
SignatureProvider signatureProvider = cryptoManager.getSignatureProvider();
byte[] signature = signatureProvider.sign(data, keyPair.getPrivate()).blockingGet();
// Serialize everything
String serializedEncryptedData = serializeToBase64(encryptedContactData).blockingGet();
String serializedIv = serializeToBase64(iv).blockingGet();
String serializedMac = serializeToBase64(guestDataMac).blockingGet();
String serializedSignature = serializeToBase64(signature).blockingGet();
String serializedPublicKey = AsymmetricCipherProvider.encode((ECPublicKey) keyPair.getPublic())
.flatMap(SerializationUtil::serializeToBase64)
.blockingGet();
// Create Fake RegistrationData
RegistrationData registrationData = getFakeRegistrationData();
// Make it a ContactData
ContactData contactData = new ContactData(registrationData);
Pair<Single<byte[]>, Single<Pair<byte[], byte[]>>> stuffedPair = registrationManager.encryptFakeContactData(contactData);
Single<byte[]> datasecret = stuffedPair.first;
Pair<byte[], byte[]> encryptedContactData = stuffedPair.second.blockingGet();
// Wrap it into a RequestData
// Create UserRegistrationRequestData with the fake data
UserRegistrationRequestData requestData = new UserRegistrationRequestData();
String serializedEncryptedData = serializeToBase64(encryptedContactData.first).blockingGet();
requestData.setEncryptedContactData(serializedEncryptedData);
String serializedIv = serializeToBase64(encryptedContactData.second).blockingGet();
requestData.setIv(serializedIv);
byte[] mac = datasecret
.flatMap(cryptoManager::generateDataAuthenticationSecret)
.flatMap(CryptoManager::createKeyFromSecret)
.flatMap(dataAuthenticationKey ->
cryptoManager.getMacProvider()
.sign(encryptedContactData.first, dataAuthenticationKey))
.blockingGet();
String serializedMac = serializeToBase64(mac).blockingGet();
requestData.setMac(serializedMac);
byte[] signature = registrationManager
.createContactDataSignature(encryptedContactData.first, mac, encryptedContactData.second)
.blockingGet();
String serializedSignature = serializeToBase64(signature).blockingGet();
requestData.setSignature(serializedSignature);
ECPublicKey publicKey = cryptoManager.getGuestKeyPairPublicKey().blockingGet();
String serializedPublicKey = AsymmetricCipherProvider.encode(publicKey)
.flatMap(SerializationUtil::serializeToBase64)
.blockingGet();
requestData.setGuestKeyPairPublicKey(serializedPublicKey);
// Make network request and get UserID
JsonObject jsonObject = networkManager.getLucaEndpointsV3().blockingGet().registerUser(requestData).blockingGet();
System.out.println(jsonObject.get("userId").getAsString());
String userId = jsonObject.get("userId").getAsString();
System.out.println(userId);
return UUID.fromString(jsonObject.get("userId").getAsString());
return UUID.fromString(userId);
}
public void doFakeCheckin() {
public void doFakeCheckin(UUID userId) {
// TODO: Scanner ID aus QR-Code einlesen
UUID scannerId = UUID.fromString("1fe5f46d-2025-4d82-a10c-569f6fe0127e");
DeviceData deviceData = new DeviceData();
DeviceData deviceData = new DeviceData(cryptoManager, userId, generateRandomData(16));
VenueData venueData = new VenueData(cryptoManager, checkInManager, scannerId, deviceData);
CheckinData checkinData = new CheckinData(deviceData, venueData);
CheckInRequestData checkInRequestData = CheckinData.map(checkinData);
String json = serializeToJson(checkInRequestData).blockingGet();
System.out.println(json);
......@@ -230,15 +233,27 @@ class VenueData {
class DeviceData {
final Integer deviceType = 1;
final Long timestamp = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
final Long timestamp;
final byte version = 3;
final byte[] data = FakeManager.generateRandomData(32);
final byte[] keyId = FakeManager.generateRandomData(1);
final byte[] traceId = FakeManager.generateRandomData(16);
final byte[] traceId;
final byte[] publicKey = FakeManager.generateRandomData(33);
final byte[] verificationTag = FakeManager.generateRandomData(8);
public DeviceData(CryptoManager cryptoManager, UUID userId, byte[] tracingSecret) {
long timestamp = TimeUtil.roundUnixTimestampDownToMinute(TimeUtil.getCurrentUnixTimestamp().blockingGet()).blockingGet();
SecretKey privateKey = CryptoManager.createKeyFromSecret(tracingSecret).blockingGet();
byte[] data = Single.zip(CryptoManager.encode(userId), TimeUtil.encodeUnixTimestamp(timestamp), Pair::new)
.flatMap(encodedDataPair -> concatenate(encodedDataPair.first, encodedDataPair.second)).blockingGet();
byte[] traceId = cryptoManager.getMacProvider().sign(data, privateKey).blockingGet();
this.traceId = CryptoManager.trim(traceId, 16).blockingGet();
this.timestamp = timestamp;
}
public byte[] getDeviceData() {
return ByteBuffer.allocate(75)
.put(version)
......
......@@ -235,24 +235,6 @@ public class RegistrationManager extends Manager {
.map(encryptedData -> new Pair<>(encryptedData, dataEncryptionKeyAndIv.second))));
}
public Pair<Single<byte[]>, Single<Pair<byte[], byte[]>>> encryptFakeContactData(@NonNull ContactData contactData) {
Single<byte[]> datasecret = cryptoManager.generateSecureRandomData(16);
Single<Pair<byte[], byte[]>> encryptedContactData = SerializationUtil.serializeToJson(contactData)
.map(contactDataJson -> contactDataJson.getBytes(StandardCharsets.UTF_8))
.flatMap(encodedContactData -> Single.zip(
datasecret
.flatMap(cryptoManager::generateDataEncryptionSecret)
.flatMap(CryptoManager::createKeyFromSecret),
cryptoManager.generateSecureRandomData(16),
Pair::new
).flatMap(dataEncryptionKeyAndIv -> cryptoManager.getSymmetricCipherProvider()
.encrypt(encodedContactData, dataEncryptionKeyAndIv.second, dataEncryptionKeyAndIv.first)
.map(encryptedData -> new Pair<>(encryptedData, dataEncryptionKeyAndIv.second))));
return new Pair<>(datasecret, encryptedContactData);
}
public Single<byte[]> createContactDataMac(byte[] encryptedContactData) {
return cryptoManager.getDataSecret()
.flatMap(cryptoManager::generateDataAuthenticationSecret)
......@@ -267,19 +249,6 @@ public class RegistrationManager extends Manager {
.sign(concatenatedData, userMasterPrivateKey)));
}
// public Single<byte[]> createFakeContactDataSignature(byte[] encryptedContactData, byte[] mac, byte[] iv) {
// Single<KeyPair> keyPairSingle = cryptoManager.generateGuestKeyPair();
//
// Single<byte[]> concatenatedData = CryptoManager.concatenate(encryptedContactData, mac, iv);
//
//
// .flatMap(concatenatedData -> keyPairSingle.getPrivate()
// .flatMap(userMasterPrivateKey -> cryptoManager.getSignatureProvider()
// .sign(concatenatedData, userMasterPrivateKey)));
//
// return
// }
/*
Data transfer request
*/
......
package de.culture4life.luca.ui;
import static de.culture4life.luca.registration.RegistrationManager.REGISTRATION_DATA_KEY;
import android.content.Intent;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import de.culture4life.luca.R;
import de.culture4life.luca.fake.FakeManager;
import java.io.IOException;
import java.util.UUID;
import static de.culture4life.luca.registration.RegistrationManager.REGISTRATION_DATA_KEY;
public class FakeCheckinActivity extends BaseActivity {
......@@ -57,13 +56,13 @@ public class FakeCheckinActivity extends BaseActivity {
for (int i = 0; i < registrationCount; i++) {
System.out.println("FakeManager: Do fake registration");
System.out.println("FakeManager: Do fake checkin");
UUID userId = fakeManager.doFakeRegistration();
fakeManager.doFakeCheckin();
System.out.println("FakeManager: Do fake checkin");
fakeManager.doFakeCheckin(userId);
}
}
private void showMainApp() {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
......@@ -103,7 +102,4 @@ public class FakeCheckinActivity extends BaseActivity {
return progress;
}
}
}
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