private async Task sendAllOutstandingMessagesAsync(OmemoSession omemoSession) { Tuple <List <OmemoMessageMessage>, OmemoSessionBuildHelper> cache = MESSAGE_CACHE[omemoSession.CHAT_JID]; foreach (OmemoMessageMessage msg in cache.Item1) { msg.encrypt(omemoSession, CONNECTION.account.omemoDeviceId); 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 " + omemoSession.DEVICE_SESSIONS_OWN.Count + " own and " + omemoSession.DEVICE_SESSIONS_REMOTE.Count + " remote recipient(s)."); }
private async Task sendAllOutstandingMessagesAsync(OmemoSessions omemoSessions) { Tuple <List <OmemoEncryptedMessage>, OmemoSessionBuildHelper> cache = MESSAGE_CACHE[omemoSessions.DST_DEVICE_GROUP.BARE_JID]; foreach (OmemoEncryptedMessage msg in cache.Item1) { try { msg.encrypt(CONNECTION.account.omemoDeviceId, CONNECTION.account.omemoIdentityKey, OMEMO_STORAGE, omemoSessions.toList()); await CONNECTION.SendAsync(msg, false); } catch (Exception e) { Logger.Error("[OMEMO HELPER] Failed to encrypt and send OMEMO message with: ", e); } } MESSAGE_CACHE.Remove(omemoSessions.DST_DEVICE_GROUP.BARE_JID); Logger.Info("[OMEMO HELPER] Send all outstanding OMEMO messages for: " + omemoSessions.DST_DEVICE_GROUP.BARE_JID + " to " + omemoSessions.SRC_DEVICE_GROUP.SESSIONS.Count + " own and " + omemoSessions.DST_DEVICE_GROUP.SESSIONS.Count + " remote recipient(s)."); }
public async Task decryptOmemoEncryptedMessageAsync(OmemoEncryptedMessage msg, bool trustedKeysOnly) { XMPPAccount account = CONNECTION.account; OmemoProtocolAddress receiverAddress = new OmemoProtocolAddress(account.getBareJid(), account.omemoDeviceId); // Try to decrypt the message, in case no exception occurred, everything went fine: OmemoDecryptionContext decryptCtx = new OmemoDecryptionContext(receiverAddress, account.omemoIdentityKey, account.omemoSignedPreKey, account.OMEMO_PRE_KEYS, trustedKeysOnly, OMEMO_STORAGE); msg.decrypt(decryptCtx); Debug.Assert(!msg.ENCRYPTED); Logger.Debug("Successfully decrypted an " + nameof(OmemoEncryptedMessage) + " for '" + receiverAddress.BARE_JID + "'."); // Republish bundle information in case the message is a key exchange message and used a PreKey: if (decryptCtx.keyExchange) { Logger.Info("Received a OMEMO key exchange message. Republishing bundle for '" + receiverAddress.BARE_JID + "'..."); PreKeyModel newPreKey = decryptCtx.STORAGE.ReplaceOmemoPreKey(decryptCtx.usedPreKey); account.OMEMO_PRE_KEYS.Remove(decryptCtx.usedPreKey); account.OMEMO_PRE_KEYS.Add(newPreKey); await announceBundleInfoAsync(); Logger.Info("Bundle for '" + receiverAddress.BARE_JID + "' republished."); // Reply with an empty message to confirm the successful key exchange: // TODO: This is no good way, since there it would be possible to stalk people without them knowing. OmemoEncryptedMessage reply = new OmemoEncryptedMessage(msg.getTo(), msg.getFrom(), null, msg.TYPE, false); OmemoDeviceGroup deviceGroup = new OmemoDeviceGroup(decryptCtx.senderAddress.BARE_JID); deviceGroup.SESSIONS.Add(decryptCtx.senderAddress.DEVICE_ID, decryptCtx.session); try { reply.encrypt(CONNECTION.account.omemoDeviceId, CONNECTION.account.omemoIdentityKey, OMEMO_STORAGE, new List <OmemoDeviceGroup> { deviceGroup }); await CONNECTION.SendAsync(reply); } catch (Exception e) { Logger.Error("[OMEMO HELPER] Failed to encrypt and the empty OMEMO message reply with: ", e); } Logger.Info($"Send an empty OMEMO message to confirm the successful key exchange with '{msg.getFrom()}'."); } }