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

Commit

Permalink
Fixed OMEMO fingerprint validation and session building for own devices
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed Aug 23, 2019
1 parent 3c9bba6 commit 6939a22
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
16 changes: 13 additions & 3 deletions Data_Manager2/Classes/Omemo/OmemoStore.cs
Expand Up @@ -207,9 +207,19 @@ public void StoreDeviceListSubscription(string name, Tuple<OmemoDeviceListSubscr
/// <param name="fingerprint">The fingerprint we want to check if it's valid.</param>
public bool IsFingerprintTrusted(OmemoFingerprint fingerprint)
{
string chatId = ChatTable.generateId(fingerprint.ADDRESS.getName(), ACCOUNT.getBareJid());
ChatTable chat = ChatDBManager.INSTANCE.getChat(chatId);
return !(chat is null) && (!chat.omemoTrustedKeysOnly || fingerprint.trusted);
// Check for own devices:
if (string.Equals(fingerprint.ADDRESS.getName(), ACCOUNT.getBareJid()))
{
// No trust management for own devices right now.
return true;
}
// Check for contact devices:
else
{
string chatId = ChatTable.generateId(fingerprint.ADDRESS.getName(), ACCOUNT.getBareJid());
ChatTable chat = ChatDBManager.INSTANCE.getChat(chatId);
return !(chat is null) && (!chat.omemoTrustedKeysOnly || fingerprint.trusted);
}
}

public void StoreFingerprint(OmemoFingerprint fingerprint)
Expand Down
2 changes: 1 addition & 1 deletion XMPP_API/Classes/Network/OmemoHelper.cs
Expand Up @@ -167,7 +167,7 @@ private async Task sendAllOutstandingMessagesAsync(OmemoSession omemoSession)
await CONNECTION.sendAsync(msg, false);
}
MESSAGE_CACHE.Remove(omemoSession.CHAT_JID);
Logger.Info("[OMEMO HELPER] Send all outstanding OMEMO messages for: " + omemoSession.CHAT_JID + " to " + cache.Item1.Count + " recipient(s).");
Logger.Info("[OMEMO HELPER] Send all outstanding OMEMO messages for: " + omemoSession.CHAT_JID + " to " + omemoSession.DEVICE_SESSIONS_OWN.Count + " own and " + omemoSession.DEVICE_SESSIONS_REMOTE.Count + " remote recipient(s).");
}

private async Task requestDeviceListAsync()
Expand Down
Expand Up @@ -130,6 +130,15 @@ private async Task buildSessionForDevicesAsync(Dictionary<uint, SessionCipher> s
SignalProtocolAddress device = devices[0];
devices.RemoveAt(0);

// Validate the device fingerprint:
OmemoFingerprint fingerprint = OMEMO_HELPER.OMEMO_STORE.LoadFingerprint(device);
if (!(fingerprint is null) && !OMEMO_HELPER.OMEMO_STORE.IsFingerprintTrusted(fingerprint))
{
Logger.Warn("[OmemoSessionBuildHelper] Not building a session with " + device.ToString() + " - key not trusted.");
await buildSessionForDevicesAsync(sessions, devices);
return;
}

// Check if there exists already a session for this device:
if (OMEMO_HELPER.OMEMO_STORE.ContainsSession(device))
{
Expand All @@ -141,18 +150,12 @@ private async Task buildSessionForDevicesAsync(Dictionary<uint, SessionCipher> s
}
else
{
OmemoFingerprint fingerprint = OMEMO_HELPER.OMEMO_STORE.LoadFingerprint(device);
if (!(fingerprint is null) && !OMEMO_HELPER.OMEMO_STORE.IsFingerprintTrusted(fingerprint))
{
Logger.Warn("[OmemoSessionBuildHelper] Not building a session with " + device.ToString() + " - key not trusted.");
}

// Else try to build a new one by requesting the devices bundle information:
OmemoBundleInformationResultMessage bundleMsg = await requestBundleInformationAsync(device);

if (!(bundleMsg is null))
{
OMEMO_HELPER.newSession(CHAT_JID, bundleMsg);
OMEMO_HELPER.newSession(device.getName(), bundleMsg);

// Validate fingerprints:
if (fingerprint is null)
Expand All @@ -167,6 +170,7 @@ private async Task buildSessionForDevicesAsync(Dictionary<uint, SessionCipher> s
if (!fingerprint.checkIdentityKey(receivedFingerprint.IDENTITY_PUB_KEY))
{
Logger.Warn("[OmemoSessionBuildHelper] Unable to establish session with " + device.ToString() + " - other fingerprint received than stored locally.");
await buildSessionForDevicesAsync(sessions, devices);
return;
}
}
Expand Down

0 comments on commit 6939a22

Please sign in to comment.