Skip to content

Commit

Permalink
reEncryptKeys Method
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarBasem committed May 28, 2021
1 parent ae3fbdd commit 2c7e048
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 33 deletions.
Binary file modified android/.idea/caches/build_file_checksums.ser
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.database.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.stiiick.stickprotocol.util.Base64;
Expand All @@ -23,8 +24,11 @@
import org.whispersystems.libsignal.ecc.ECPrivateKey;
import org.whispersystems.libsignal.ecc.ECPublicKey;
import org.whispersystems.libsignal.state.PreKeyRecord;
import org.whispersystems.libsignal.state.SignedPreKeyRecord;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class OneTimePreKeyDatabase extends Database {

Expand Down Expand Up @@ -68,6 +72,26 @@ public class OneTimePreKeyDatabase extends Database {
return null;
}

public @NonNull
List<PreKeyRecord> getAllPreKeys() {
SQLiteDatabase database = databaseHelper.fetchReadableDatabase();
List<PreKeyRecord> results = new LinkedList<>();

try (Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
try {
int keyId = cursor.getInt(cursor.getColumnIndexOrThrow(KEY_ID));
ECPublicKey publicKey = Curve.decodePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PUBLIC_KEY))), 0);
ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PRIVATE_KEY))));
results.add(new PreKeyRecord(keyId, new ECKeyPair(publicKey, privateKey)));
} catch (InvalidKeyException | IOException e) {
e.printStackTrace();
}
}
}
return results;
}

public void insertPreKey(int keyId, PreKeyRecord record) {
SQLiteDatabase database = databaseHelper.fetchWritableDatabase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,79 @@ public JSONObject initialize(String userId, String password, ProgressEvent progr
return null;
}

public JSONObject reEncryptKeys(String password, ProgressEvent progressEvent) {
try {
MyProtocolStore store = new MyProtocolStore(context);
List<PreKeyRecord> preKeys = store.loadPreKeys();
List<SignedPreKeyRecord> signedPreKeys = store.loadSignedPreKeys();
List<IdentityKeyRecord> identityKeys = store.loadIdentityKeys();
int total = identityKeys.size() + signedPreKeys.size() + preKeys.size();
int progress = 0;
JSONArray preKeysArray = new JSONArray();
for (int i=0; i<preKeys.size(); i++) {
JSONObject key = new JSONObject();
key.put("id", preKeys.get(i).getId());
HashMap<String, String> cipherMap = pbEncrypt(preKeys.get(i).getKeyPair().getPrivateKey().serialize(), password);
key.put("cipher", cipherMap.get("cipher"));
key.put("salt", cipherMap.get("salt"));
preKeysArray.put(key);

// PROGRESS
progress += 1;
if (progressEvent != null) {
JSONObject event = new JSONObject();
event.put("progress", progress);
event.put("total", total);
progressEvent.execute(event);
}
}
JSONArray signedPreKeysArray = new JSONArray();
for (int i=0; i<signedPreKeys.size(); i++) {
JSONObject key = new JSONObject();
key.put("id", signedPreKeys.get(i).getId());
HashMap<String, String> cipherMap = pbEncrypt(signedPreKeys.get(i).getKeyPair().getPrivateKey().serialize(), password);
key.put("cipher", cipherMap.get("cipher"));
key.put("salt", cipherMap.get("salt"));
signedPreKeysArray.put(key);

// PROGRESS
progress += 1;
if (progressEvent != null) {
JSONObject event = new JSONObject();
event.put("progress", progress);
event.put("total", total);
progressEvent.execute(event);
}
}
JSONArray identityKeysArray = new JSONArray();
for (int i=0; i<identityKeys.size(); i++) {
JSONObject key = new JSONObject();
key.put("id", identityKeys.get(i).getId());
HashMap<String, String> cipherMap = pbEncrypt(identityKeys.get(i).getKeyPair().getPrivateKey().serialize(), password);
key.put("cipher", cipherMap.get("cipher"));
key.put("salt", cipherMap.get("salt"));
identityKeysArray.put(key);

// PROGRESS
progress += 1;
if (progressEvent != null) {
JSONObject event = new JSONObject();
event.put("progress", progress);
event.put("total", total);
progressEvent.execute(event);
}
}
JSONObject map = new JSONObject();
map.put("preKeys", preKeysArray);
map.put("signedPreKeys", signedPreKeysArray);
map.put("identityKeys", identityKeysArray);
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/***
* The StickProtocol Re-Initialize method to decrypt the user's keys and re-establish the sticky
* sessions. Needs to be called once, at login time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.content.Context;
import android.util.Log;

import com.stiiick.stickprotocol.database.IdentityKeyRecord;
import com.stiiick.stickprotocol.util.IdentityKeyUtil;
import com.stiiick.stickprotocol.util.Preferences;
import com.stiiick.stickprotocol.recipient.Recipient;
Expand All @@ -23,8 +24,10 @@
import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.SignalProtocolAddress;
import org.whispersystems.libsignal.state.IdentityKeyStore;
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
import org.whispersystems.libsignal.util.guava.Optional;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class MyIdentityKeyStore implements IdentityKeyStore {
Expand Down Expand Up @@ -84,32 +87,6 @@ public boolean saveIdentity(SignalProtocolAddress address, IdentityKey identityK
} else {
return true;
}

// if (!identityRecord.get().getIdentityKey().equals(identityKey)) {
// Log.d("REPLACING", "Replacing existing identity...");
// VerifiedStatus verifiedStatus;
//
// if (identityRecord.get().getVerifiedStatus() == VerifiedStatus.VERIFIED ||
// identityRecord.get().getVerifiedStatus() == VerifiedStatus.UNVERIFIED)
// {
// verifiedStatus = VerifiedStatus.UNVERIFIED;
// } else {
// verifiedStatus = VerifiedStatus.DEFAULT;
// }
//
// identityDatabase.saveIdentity(recipient.getId(), identityKey, verifiedStatus, false, System.currentTimeMillis(), nonBlockingApproval);
// Log.d("ARHIVINGGG", "ARHIVIING");
// SessionUtil.archiveSiblingSessions(context, address);
// return true;
// }

// if (isNonBlockingApprovalRequired(identityRecord.get())) {
// Log.i("SETTING", "Setting approval status...");
// identityDatabase.setApproval(recipient.getId(), nonBlockingApproval);
// return false;
// }

// return false;
}
}

Expand All @@ -123,10 +100,10 @@ public boolean isTrustedIdentity(SignalProtocolAddress address, IdentityKey iden
return true;
}

private boolean isNonBlockingApprovalRequired(IdentityDatabase.IdentityRecord identityRecord) {
return !identityRecord.isFirstUse() &&
System.currentTimeMillis() - identityRecord.getTimestamp() < TimeUnit.SECONDS.toMillis(TIMESTAMP_THRESHOLD_SECONDS) &&
!identityRecord.isApprovedNonBlocking();
public List<IdentityKeyRecord> loadIdentityKeys() {
synchronized (LOCK) {
return DatabaseFactory.getIdentityKeyDatabase(context).getAllIdentityKeys();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public List<SignedPreKeyRecord> loadSignedPreKeys() {
}
}

public List<PreKeyRecord> loadPreKeys() {
synchronized (FILE_LOCK) {
return DatabaseFactory.getPreKeyDatabase(context).getAllPreKeys();
}
}

@Override
public void storePreKey(int preKeyId, PreKeyRecord record) {
synchronized (FILE_LOCK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import android.content.Context;

import com.stiiick.stickprotocol.database.IdentityKeyRecord;

import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.InvalidKeyIdException;
Expand All @@ -27,9 +29,9 @@

public class MyProtocolStore implements SignalProtocolStore {

private final PreKeyStore preKeyStore;
private final MyPreKeyStore preKeyStore;
private final SignedPreKeyStore signedPreKeyStore;
private final IdentityKeyStore identityKeyStore;
private final MyIdentityKeyStore identityKeyStore;
private final SessionStore sessionStore;

public MyProtocolStore(Context context) {
Expand Down Expand Up @@ -124,6 +126,14 @@ public List<SignedPreKeyRecord> loadSignedPreKeys() {
return signedPreKeyStore.loadSignedPreKeys();
}

public List<PreKeyRecord> loadPreKeys() {
return preKeyStore.loadPreKeys();
}

public List<IdentityKeyRecord> loadIdentityKeys() {
return identityKeyStore.loadIdentityKeys();
}

@Override
public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) {
signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
Expand Down
2 changes: 1 addition & 1 deletion ios/StickProtocol/StickProtocol.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pod::Spec.new do |spec|


spec.name = "StickProtocol"
spec.version = "1.1.73"
spec.version = "1.1.74"
spec.summary = "End-to-End Encryption protocol for Social Networks based on the Signal Protocol"
spec.swift_version = "5.0"

Expand Down

0 comments on commit 2c7e048

Please sign in to comment.