Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Fixed not storing OMEMO dhS and dhR
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed Mar 28, 2021
1 parent 4e835aa commit d6cd3c2
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 29 deletions.
37 changes: 20 additions & 17 deletions Component_Tests/Classes/Crypto/Omemo/InMemmoryOmemoStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,8 @@ public class InMemmoryOmemoStorage: IExtendedOmemoStorage
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
public readonly Dictionary<OmemoProtocolAddress, OmemoSessionModel> SESSIONS = new Dictionary<OmemoProtocolAddress, OmemoSessionModel>();

public Tuple<OmemoDeviceListSubscriptionState, DateTime> LoadDeviceListSubscription(string bareJid)
{
throw new NotImplementedException();
}

public List<OmemoProtocolAddress> LoadDevices(string bareJid)
{
throw new NotImplementedException();
}

public OmemoFingerprint LoadFingerprint(OmemoProtocolAddress address)
{
throw new NotImplementedException();
}
public readonly Dictionary<OmemoProtocolAddress, OmemoFingerprint> FINGERPRINTS = new Dictionary<OmemoProtocolAddress, OmemoFingerprint>();
public readonly Dictionary<string, List<OmemoProtocolAddress>> DEVICES = new Dictionary<string, List<OmemoProtocolAddress>>();

#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
Expand Down Expand Up @@ -58,19 +45,35 @@ public void StoreDeviceListSubscription(string bareJid, Tuple<OmemoDeviceListSub

public void StoreDevices(List<OmemoProtocolAddress> devices, string bareJid)
{
throw new NotImplementedException();
DEVICES[bareJid] = devices;
}

public void StoreFingerprint(OmemoFingerprint fingerprint)
{
throw new NotImplementedException();
FINGERPRINTS[fingerprint.ADDRESS] = fingerprint;
}

public void StoreSession(OmemoProtocolAddress address, OmemoSessionModel session)
{
SESSIONS[address] = session;
}

public Tuple<OmemoDeviceListSubscriptionState, DateTime> LoadDeviceListSubscription(string bareJid)
{
throw new NotImplementedException();
}

public List<OmemoProtocolAddress> LoadDevices(string bareJid)
{
return DEVICES.ContainsKey(bareJid) ? DEVICES[bareJid] : new List<OmemoProtocolAddress>();
}

public OmemoFingerprint LoadFingerprint(OmemoProtocolAddress address)
{
return FINGERPRINTS.ContainsKey(address) ? FINGERPRINTS[address] : null;
}


#endregion

#region --Misc Methods (Private)--
Expand Down
218 changes: 215 additions & 3 deletions Component_Tests/Classes/Crypto/Omemo/Test_Omemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,21 @@ public void Test_Double_Rachet_Multiple_Sending()
omemoEncryptedMessage.encrypt(ALICE_ADDRESS.DEVICE_ID, aliceIdentKey, aliceStorage, bobDevices);
Assert.IsTrue(omemoEncryptedMessage.ENCRYPTED);

// Decrypt Message 1:
// Decrypt Message 2:
// Throws an exception in case something goes wrong:
OmemoDecryptionContext decryptCtx1 = new OmemoDecryptionContext(BOB_ADDRESS, bobIdentKey, bobSignedPreKey, bobPreKeys, false, bobStorage);
omemoEncryptedMessage.decrypt(decryptCtx1);
Assert.AreEqual(msg1, omemoEncryptedMessage.MESSAGE);
Assert.IsFalse(omemoEncryptedMessage.IS_PURE_KEY_EXCHANGE_MESSAGE);
Assert.IsFalse(omemoEncryptedMessage.ENCRYPTED);

// Encrypt Message 1:
// Encrypt Message 2:
string msg2 = "Hello OMEMO 2";
OmemoEncryptedMessage omemoEncryptedMessage2 = new OmemoEncryptedMessage(ALICE_ADDRESS.BARE_JID, BOB_ADDRESS.BARE_JID, msg2, MessageMessage.TYPE_CHAT, false);
omemoEncryptedMessage2.encrypt(ALICE_ADDRESS.DEVICE_ID, aliceIdentKey, aliceStorage, bobDevices);
Assert.IsTrue(omemoEncryptedMessage2.ENCRYPTED);

// Decrypt Message 1:
// Decrypt Message 2:
// Throws an exception in case something goes wrong:
OmemoDecryptionContext decryptCtx2 = new OmemoDecryptionContext(BOB_ADDRESS, bobIdentKey, bobSignedPreKey, bobPreKeys, false, bobStorage);
omemoEncryptedMessage2.decrypt(decryptCtx2);
Expand All @@ -178,6 +178,218 @@ public void Test_Double_Rachet_Multiple_Sending()
Assert.IsFalse(omemoEncryptedMessage2.ENCRYPTED);
}

[TestCategory("Crypto")]
[TestMethod]
public void Test_Omemo_Send_Receive()
{
// Generate Alices keys:
IdentityKeyPairModel aliceIdentKey = KeyHelper.GenerateIdentityKeyPair();
List<PreKeyModel> alicePreKeys = KeyHelper.GeneratePreKeys(0, 1);
SignedPreKeyModel aliceSignedPreKey = KeyHelper.GenerateSignedPreKey(0, aliceIdentKey.privKey);
Bundle aliceBundle = new Bundle()
{
identityKey = aliceIdentKey.pubKey,
preKeys = alicePreKeys.Select(key => new PreKeyModel(null, key.pubKey, key.keyId)).ToList(),
preKeySignature = aliceSignedPreKey.signature,
signedPreKey = aliceSignedPreKey.preKey.pubKey,
signedPreKeyId = aliceSignedPreKey.preKey.keyId
};
InMemmoryOmemoStorage aliceStorage = new InMemmoryOmemoStorage();
DoubleRachet aliceRachet = new DoubleRachet(aliceIdentKey);

// Generate Bobs keys:
IdentityKeyPairModel bobIdentKey = KeyHelper.GenerateIdentityKeyPair();
List<PreKeyModel> bobPreKeys = KeyHelper.GeneratePreKeys(0, 1);
SignedPreKeyModel bobSignedPreKey = KeyHelper.GenerateSignedPreKey(0, bobIdentKey.privKey);
Bundle bobBundle = new Bundle()
{
identityKey = bobIdentKey.pubKey,
preKeys = bobPreKeys.Select(key => new PreKeyModel(null, key.pubKey, key.keyId)).ToList(),
preKeySignature = bobSignedPreKey.signature,
signedPreKey = bobSignedPreKey.preKey.pubKey,
signedPreKeyId = bobSignedPreKey.preKey.keyId
};
InMemmoryOmemoStorage bobStorage = new InMemmoryOmemoStorage();
DoubleRachet bobRachet = new DoubleRachet(bobIdentKey);

//-----------------OMEOMO Session Building:-----------------
MessageParser2 parser = new MessageParser2();

string deviceListMsg = GetBobsDeviceListMsg();
List<AbstractMessage> messages = parser.parseMessages(ref deviceListMsg);
Assert.IsTrue(messages.Count == 1);
Assert.IsTrue(messages[0] is OmemoDeviceListResultMessage);
OmemoDeviceListResultMessage devList = messages[0] as OmemoDeviceListResultMessage;

uint selectedBobDeviceId = devList.DEVICES.getRandomDeviceId();
Assert.IsTrue(selectedBobDeviceId == BOB_ADDRESS.DEVICE_ID);

// Alice builds a session to Bob:
string bundleInfoMsg = GetBobsBundleInfoMsg(bobBundle);
messages = parser.parseMessages(ref bundleInfoMsg);
Assert.IsTrue(messages.Count == 1);
Assert.IsTrue(messages[0] is OmemoBundleInformationResultMessage);
OmemoBundleInformationResultMessage bundleInfo = messages[0] as OmemoBundleInformationResultMessage;
Assert.IsTrue(bundleInfo.BUNDLE_INFO.deviceId == BOB_ADDRESS.DEVICE_ID);
aliceStorage.StoreFingerprint(new OmemoFingerprint(bobBundle.identityKey, BOB_ADDRESS));

// Encrypt Message 1:
string msg1 = "Hello OMEMO";
OmemoEncryptedMessage omemoEncryptedMessage = new OmemoEncryptedMessage(ALICE_ADDRESS.BARE_JID, BOB_ADDRESS.BARE_JID, msg1, MessageMessage.TYPE_CHAT, false);
List<OmemoDeviceGroup> bobDevices = new List<OmemoDeviceGroup>();
OmemoDeviceGroup bobDeviceGroup = new OmemoDeviceGroup(BOB_ADDRESS.BARE_JID);
bobDeviceGroup.SESSIONS[BOB_ADDRESS.DEVICE_ID] = new OmemoSessionModel(bobBundle, 0, aliceIdentKey);
bobDevices.Add(bobDeviceGroup);
omemoEncryptedMessage.encrypt(ALICE_ADDRESS.DEVICE_ID, aliceIdentKey, aliceStorage, bobDevices);
Assert.IsTrue(omemoEncryptedMessage.ENCRYPTED);
Assert.IsNotNull(aliceStorage.LoadFingerprint(BOB_ADDRESS));
Assert.IsNotNull(aliceStorage.LoadSession(BOB_ADDRESS));

// Decrypt Message 1:
// Throws an exception in case something goes wrong:
OmemoDecryptionContext decryptCtx1 = new OmemoDecryptionContext(BOB_ADDRESS, bobIdentKey, bobSignedPreKey, bobPreKeys, false, bobStorage);
omemoEncryptedMessage.decrypt(decryptCtx1);
Assert.AreEqual(msg1, omemoEncryptedMessage.MESSAGE);
Assert.IsFalse(omemoEncryptedMessage.IS_PURE_KEY_EXCHANGE_MESSAGE);
Assert.IsFalse(omemoEncryptedMessage.ENCRYPTED);
Assert.IsNotNull(bobStorage.LoadFingerprint(ALICE_ADDRESS));
Assert.IsNotNull(bobStorage.LoadSession(ALICE_ADDRESS));

// Encrypt Message 2:
string msg2 = "Hello OMEMO 2";
OmemoEncryptedMessage omemoEncryptedMessage2 = new OmemoEncryptedMessage(BOB_ADDRESS.BARE_JID, ALICE_ADDRESS.BARE_JID, msg2, MessageMessage.TYPE_CHAT, false);
List<OmemoDeviceGroup> aliceDevices = new List<OmemoDeviceGroup>();
OmemoDeviceGroup aliceDeviceGroup = new OmemoDeviceGroup(ALICE_ADDRESS.BARE_JID);
aliceDeviceGroup.SESSIONS[ALICE_ADDRESS.DEVICE_ID] = bobStorage.LoadSession(ALICE_ADDRESS);
aliceDevices.Add(aliceDeviceGroup);
omemoEncryptedMessage2.encrypt(BOB_ADDRESS.DEVICE_ID, bobIdentKey, bobStorage, aliceDevices);
Assert.IsTrue(omemoEncryptedMessage2.ENCRYPTED);

// Decrypt Message 2:
// Throws an exception in case something goes wrong:
OmemoDecryptionContext decryptCtx2 = new OmemoDecryptionContext(ALICE_ADDRESS, aliceIdentKey, aliceSignedPreKey, alicePreKeys, false, aliceStorage);
omemoEncryptedMessage2.decrypt(decryptCtx2);
Assert.AreEqual(msg2, omemoEncryptedMessage2.MESSAGE);
Assert.IsFalse(omemoEncryptedMessage2.IS_PURE_KEY_EXCHANGE_MESSAGE);
Assert.IsFalse(omemoEncryptedMessage2.ENCRYPTED);
}

[TestCategory("Crypto")]
[TestMethod]
public void Test_Omemo_Send_Receive_Send()
{
// Generate Alices keys:
IdentityKeyPairModel aliceIdentKey = KeyHelper.GenerateIdentityKeyPair();
List<PreKeyModel> alicePreKeys = KeyHelper.GeneratePreKeys(0, 1);
SignedPreKeyModel aliceSignedPreKey = KeyHelper.GenerateSignedPreKey(0, aliceIdentKey.privKey);
Bundle aliceBundle = new Bundle()
{
identityKey = aliceIdentKey.pubKey,
preKeys = alicePreKeys.Select(key => new PreKeyModel(null, key.pubKey, key.keyId)).ToList(),
preKeySignature = aliceSignedPreKey.signature,
signedPreKey = aliceSignedPreKey.preKey.pubKey,
signedPreKeyId = aliceSignedPreKey.preKey.keyId
};
InMemmoryOmemoStorage aliceStorage = new InMemmoryOmemoStorage();
DoubleRachet aliceRachet = new DoubleRachet(aliceIdentKey);

// Generate Bobs keys:
IdentityKeyPairModel bobIdentKey = KeyHelper.GenerateIdentityKeyPair();
List<PreKeyModel> bobPreKeys = KeyHelper.GeneratePreKeys(0, 1);
SignedPreKeyModel bobSignedPreKey = KeyHelper.GenerateSignedPreKey(0, bobIdentKey.privKey);
Bundle bobBundle = new Bundle()
{
identityKey = bobIdentKey.pubKey,
preKeys = bobPreKeys.Select(key => new PreKeyModel(null, key.pubKey, key.keyId)).ToList(),
preKeySignature = bobSignedPreKey.signature,
signedPreKey = bobSignedPreKey.preKey.pubKey,
signedPreKeyId = bobSignedPreKey.preKey.keyId
};
InMemmoryOmemoStorage bobStorage = new InMemmoryOmemoStorage();
DoubleRachet bobRachet = new DoubleRachet(bobIdentKey);

//-----------------OMEOMO Session Building:-----------------
MessageParser2 parser = new MessageParser2();

string deviceListMsg = GetBobsDeviceListMsg();
List<AbstractMessage> messages = parser.parseMessages(ref deviceListMsg);
Assert.IsTrue(messages.Count == 1);
Assert.IsTrue(messages[0] is OmemoDeviceListResultMessage);
OmemoDeviceListResultMessage devList = messages[0] as OmemoDeviceListResultMessage;

uint selectedBobDeviceId = devList.DEVICES.getRandomDeviceId();
Assert.IsTrue(selectedBobDeviceId == BOB_ADDRESS.DEVICE_ID);

// Alice builds a session to Bob:
string bundleInfoMsg = GetBobsBundleInfoMsg(bobBundle);
messages = parser.parseMessages(ref bundleInfoMsg);
Assert.IsTrue(messages.Count == 1);
Assert.IsTrue(messages[0] is OmemoBundleInformationResultMessage);
OmemoBundleInformationResultMessage bundleInfo = messages[0] as OmemoBundleInformationResultMessage;
Assert.IsTrue(bundleInfo.BUNDLE_INFO.deviceId == BOB_ADDRESS.DEVICE_ID);
aliceStorage.StoreFingerprint(new OmemoFingerprint(bobBundle.identityKey, BOB_ADDRESS));

// Encrypt Message 1:
string msg1 = "Hello OMEMO";
OmemoEncryptedMessage omemoEncryptedMessage = new OmemoEncryptedMessage(ALICE_ADDRESS.BARE_JID, BOB_ADDRESS.BARE_JID, msg1, MessageMessage.TYPE_CHAT, false);
List<OmemoDeviceGroup> bobDevices = new List<OmemoDeviceGroup>();
OmemoDeviceGroup bobDeviceGroup = new OmemoDeviceGroup(BOB_ADDRESS.BARE_JID);
bobDeviceGroup.SESSIONS[BOB_ADDRESS.DEVICE_ID] = new OmemoSessionModel(bobBundle, 0, aliceIdentKey);
bobDevices.Add(bobDeviceGroup);
omemoEncryptedMessage.encrypt(ALICE_ADDRESS.DEVICE_ID, aliceIdentKey, aliceStorage, bobDevices);
Assert.IsTrue(omemoEncryptedMessage.ENCRYPTED);
Assert.IsNotNull(aliceStorage.LoadFingerprint(BOB_ADDRESS));
Assert.IsNotNull(aliceStorage.LoadSession(BOB_ADDRESS));

// Decrypt Message 1:
// Throws an exception in case something goes wrong:
OmemoDecryptionContext decryptCtx1 = new OmemoDecryptionContext(BOB_ADDRESS, bobIdentKey, bobSignedPreKey, bobPreKeys, false, bobStorage);
omemoEncryptedMessage.decrypt(decryptCtx1);
Assert.AreEqual(msg1, omemoEncryptedMessage.MESSAGE);
Assert.IsFalse(omemoEncryptedMessage.IS_PURE_KEY_EXCHANGE_MESSAGE);
Assert.IsFalse(omemoEncryptedMessage.ENCRYPTED);
Assert.IsNotNull(bobStorage.LoadFingerprint(ALICE_ADDRESS));
Assert.IsNotNull(bobStorage.LoadSession(ALICE_ADDRESS));

// Encrypt Message 2:
string msg2 = "Hello OMEMO 2";
OmemoEncryptedMessage omemoEncryptedMessage2 = new OmemoEncryptedMessage(BOB_ADDRESS.BARE_JID, ALICE_ADDRESS.BARE_JID, msg2, MessageMessage.TYPE_CHAT, false);
List<OmemoDeviceGroup> aliceDevices = new List<OmemoDeviceGroup>();
OmemoDeviceGroup aliceDeviceGroup = new OmemoDeviceGroup(ALICE_ADDRESS.BARE_JID);
aliceDeviceGroup.SESSIONS[ALICE_ADDRESS.DEVICE_ID] = bobStorage.LoadSession(ALICE_ADDRESS);
aliceDevices.Add(aliceDeviceGroup);
omemoEncryptedMessage2.encrypt(BOB_ADDRESS.DEVICE_ID, bobIdentKey, bobStorage, aliceDevices);
Assert.IsTrue(omemoEncryptedMessage2.ENCRYPTED);

// Decrypt Message 2:
// Throws an exception in case something goes wrong:
OmemoDecryptionContext decryptCtx2 = new OmemoDecryptionContext(ALICE_ADDRESS, aliceIdentKey, aliceSignedPreKey, alicePreKeys, false, aliceStorage);
omemoEncryptedMessage2.decrypt(decryptCtx2);
Assert.AreEqual(msg2, omemoEncryptedMessage2.MESSAGE);
Assert.IsFalse(omemoEncryptedMessage2.IS_PURE_KEY_EXCHANGE_MESSAGE);
Assert.IsFalse(omemoEncryptedMessage2.ENCRYPTED);

// Encrypt Message 3:
string msg3 = "Hello OMEMO 3";
OmemoEncryptedMessage omemoEncryptedMessage3 = new OmemoEncryptedMessage(ALICE_ADDRESS.BARE_JID, BOB_ADDRESS.BARE_JID, msg3, MessageMessage.TYPE_CHAT, false);
bobDeviceGroup.SESSIONS[BOB_ADDRESS.DEVICE_ID] = aliceStorage.LoadSession(BOB_ADDRESS);
omemoEncryptedMessage3.encrypt(ALICE_ADDRESS.DEVICE_ID, aliceIdentKey, aliceStorage, bobDevices);
Assert.IsFalse(omemoEncryptedMessage3.IS_PURE_KEY_EXCHANGE_MESSAGE);
Assert.IsTrue(omemoEncryptedMessage3.ENCRYPTED);
Assert.IsNotNull(aliceStorage.LoadFingerprint(BOB_ADDRESS));
Assert.IsNotNull(aliceStorage.LoadSession(BOB_ADDRESS));

// Decrypt Message 3:
// Throws an exception in case something goes wrong:
OmemoDecryptionContext decryptCtx3 = new OmemoDecryptionContext(BOB_ADDRESS, bobIdentKey, bobSignedPreKey, bobPreKeys, false, bobStorage);
omemoEncryptedMessage3.decrypt(decryptCtx3);
Assert.AreEqual(msg3, omemoEncryptedMessage3.MESSAGE);
Assert.IsFalse(omemoEncryptedMessage3.IS_PURE_KEY_EXCHANGE_MESSAGE);
Assert.IsFalse(omemoEncryptedMessage3.ENCRYPTED);
Assert.IsNotNull(bobStorage.LoadFingerprint(ALICE_ADDRESS));
Assert.IsNotNull(bobStorage.LoadSession(ALICE_ADDRESS));
}

[TestCategory("Crypto")]
[TestMethod]
public void Test_OmemoBundleInformation()
Expand Down
Loading

0 comments on commit d6cd3c2

Please sign in to comment.