Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarBasem committed Mar 29, 2021
1 parent 4a07707 commit caa5a9e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,7 @@ public String decryptTextPairwise(String senderId, boolean isStickyKey, String c
* @param stickId - String, the stickId of the sticky session
* @return JSONObject - contains the following:
* * id - int, the sender key id
* * chainKey - String
* * public - String, signature key public key
* * cipher - String, signature key encrypted private key
* * key - encrypted sender key (chainKey || private signature key || public signature key)
*/
public JSONObject getEncryptingSenderKey(String userId, String stickId) {
try {
Expand All @@ -528,15 +526,11 @@ public JSONObject getEncryptingSenderKey(String userId, String stickId) {
SenderKeyDistributionMessage senderKeyDistributionMessage = groupSessionBuilder.create(senderKeyName);
DatabaseFactory.getStickyKeyDatabase(context).insertStickyKey(stickId, Base64.encodeBytes(senderKeyDistributionMessage.serialize()));
SenderKeyState senderKeyState = senderKeyStore.loadSenderKey(senderKeyName).getSenderKeyState();
Log.d("PRIVATE KEY LENGTHXXX", Integer.toString(Base64.encodeBytes(senderKeyState.getSigningKeyPrivate().serialize()).length()));
Log.d("PUBLIC KEY LENGTHXXX", Integer.toString(Base64.encodeBytes(senderKeyState.getSigningKeyPublic().serialize()).length()));
Log.d("CHAIN KEY LENGTHXXX", Integer.toString(Base64.encodeBytes(senderKeyState.getSenderChainKey().getSeed()).length()));
String cipher = encryptTextPairwise(userId, Base64.encodeBytes(senderKeyState.getSigningKeyPrivate().serialize()));
String key = Base64.encodeBytes(senderKeyState.getSenderChainKey().getSeed()) + Base64.encodeBytes(senderKeyState.getSigningKeyPrivate().serialize()) + Base64.encodeBytes(senderKeyState.getSigningKeyPublic().serialize());
String encryptKey = encryptTextPairwise(userId, key);
JSONObject map = new JSONObject();
map.put("id", senderKeyState.getKeyId());
map.put("chainKey", Base64.encodeBytes(senderKeyState.getSenderChainKey().getSeed()));
map.put("public", Base64.encodeBytes(senderKeyState.getSigningKeyPublic().serialize()));
map.put("cipher", cipher);
map.put("key", encryptKey);
return map;
} catch (InvalidKeyException | InvalidKeyIdException | JSONException e) {
e.printStackTrace();
Expand Down Expand Up @@ -786,15 +780,21 @@ public void reinitMyStickySession(String userId, JSONObject senderKey) throws IO
SenderKeyName senderKeyName = new SenderKeyName(senderKey.getString("stickId"), signalProtocolAddress);
SenderKeyStore senderKeyStore = new MySenderKeyStore(context);
SenderKeyRecord senderKeyRecord = senderKeyStore.loadSenderKey(senderKeyName);
ECPublicKey senderPubKey = Curve.decodePoint(Base64.decode(senderKey.getString("public")), 0);
String privateKey = decryptStickyKey(userId, senderKey.getString("cipher"), senderKey.getInt("identityKeyId"));
ECPrivateKey senderPrivKey = Curve.decodePrivatePoint(Base64.decode(privateKey));
ECKeyPair signedSenderKey = new ECKeyPair(senderPubKey, senderPrivKey);

String key = decryptStickyKey(userId, senderKey.getString("key"), senderKey.getInt("identityKeyId"));
String chainKey = key.substring(0, 44);
String signaturePrivateKey = key.substring(44, 88);
String signaturePublicKey = key.substring(88, 132);

ECPublicKey senderPubKey = Curve.decodePoint(Base64.decode(signaturePublicKey), 0);
ECPrivateKey senderPrivKey = Curve.decodePrivatePoint(Base64.decode(signaturePrivateKey));
ECKeyPair signatureKey = new ECKeyPair(senderPubKey, senderPrivKey);

senderKeyRecord.setSenderKeyState(
senderKey.getInt("id"),
0,
Base64.decode(senderKey.getString("chainKey")),
signedSenderKey
Base64.decode(chainKey),
signatureKey
);
senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);

Expand Down
2 changes: 1 addition & 1 deletion server/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = stick-protocol-server
version = 1.1.55
version = 1.1.56
description = Stick protocol server for Django.
long_description = file: README.rst
long_description_content_type = text/x-rst
Expand Down
4 changes: 1 addition & 3 deletions server/stick_protocol/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ class EncryptingSenderKey(models.Model):
chainId = models.CharField(max_length=10)
step = models.IntegerField(default=0)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='encryptingSenderKeys')
chainKey = models.CharField(max_length=44)
public = models.CharField(max_length=44)
cipher = models.CharField(max_length=500)
key = models.CharField(max_length=500)

class Meta:
constraints = [models.UniqueConstraint(fields=['partyId', 'chainId', 'user'], name='unique_esk')]
Expand Down
10 changes: 3 additions & 7 deletions server/stick_protocol/stick_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ def get_sender_key(self, data, user):
if memberId != user.id:
key = {'key': senderKey.key, 'identityKeyId': senderKey.identityKey.keyId}
else:
key = {'id': senderKey.keyId, 'chainKey': senderKey.chainKey, 'public': senderKey.public,
'cipher': senderKey.cipher, 'step': senderKey.step, 'identityKeyId': senderKey.identityKey.keyId}
key = {'id': senderKey.keyId, 'key': senderKey.key, 'step': senderKey.step, 'identityKeyId': senderKey.identityKey.keyId}
# SenderKey does not exist, send a `PendingKey` request to the target user to upload their key,
# through a realtime database.
else:
Expand Down Expand Up @@ -404,9 +403,7 @@ def process_sender_keys(self, data, user):
chainId = senderKey['stickId'][36:]
EncryptingSenderKey.objects.create(keyId=senderKey['id'], preKey=preKey, identityKey=identityKey,
partyId=partyId, chainId=chainId,
user=user, chainKey=senderKey['chainKey'],
public=senderKey['public'],
cipher=senderKey['cipher'])
user=user, key=['key'])
if "group_id" in data: # TODO: REMOVE FROM HERE MAYBE?
user.just_added_groups.remove(data["group_id"])

Expand Down Expand Up @@ -482,8 +479,7 @@ def verify_password_and_get_keys(self, data, user):
senderKeys = []
for senderKey in senderKeysList:
stickId = senderKey.partyId + senderKey.chainId
key = {'id': senderKey.keyId, 'chainKey': senderKey.chainKey, 'public': senderKey.public,
'cipher': senderKey.cipher, 'stickId': stickId, 'step': senderKey.step,
key = {'id': senderKey.keyId, 'key': senderKey.key, 'stickId': stickId, 'step': senderKey.step,
'identityKeyId': senderKey.identityKey.keyId}
senderKeys.append(key)
bundle['senderKeys'] = senderKeys
Expand Down

0 comments on commit caa5a9e

Please sign in to comment.