Ejemplo n.º 1
0
        public void StoreSession(OmemoProtocolAddress address, OmemoSessionModel session)
        {
            OmemoDeviceModel device;

            if (string.Equals(address.BARE_JID, dbAccount.bareJid))
            {
                device = dbAccount.omemoInfo.devices.Where(d => address.DEVICE_ID == d.deviceId).FirstOrDefault();
            }
            else
            {
                ChatModel chat;
                using (SemaLock semaLock = DataCache.INSTANCE.NewChatSemaLock())
                {
                    chat = DataCache.INSTANCE.GetChat(dbAccount.bareJid, address.BARE_JID, semaLock);
                }
                if (chat is null)
                {
                    throw new InvalidOperationException("Failed to store session. Chat '" + address.BARE_JID + "' does not exist.");
                }
                device = chat.omemoInfo.devices.Where(d => d.deviceId == address.DEVICE_ID).FirstOrDefault();
            }

            if (device is null)
            {
                throw new InvalidOperationException("Failed to store session. Device '" + address.ToString() + "' does not exist.");
            }
            if (device.session is null)
            {
                device.session = session;
            }

            bool newSession = device.session.id != session.id;
            OmemoSessionModel oldSession = null;

            if (newSession)
            {
                oldSession     = device.session;
                device.session = session;
            }

            device.Update();

            // Remove the old session:
            if (newSession)
            {
                using (MainDbContext ctx = new MainDbContext())
                {
                    ctx.Remove(oldSession);
                }
            }
        }
Ejemplo n.º 2
0
        private async Task buildSessionForDevicesAsync(OmemoDeviceGroup deviceGroup, IList <OmemoProtocolAddress> devices)
        {
            if (devices.Count <= 0)
            {
                return;
            }
            OmemoProtocolAddress device = devices[0];

            devices.RemoveAt(0);

            OmemoFingerprint fingerprint = OMEMO_HELPER.OMEMO_STORAGE.LoadFingerprint(device);
            // Check if there exists already a session for this device:
            OmemoSessionModel session = OMEMO_HELPER.OMEMO_STORAGE.LoadSession(device);

            if (session is null)
            {
                // Try to build a new session by requesting the devices bundle information:
                OmemoBundleInformationResultMessage bundleMsg = await requestBundleInformationAsync(device);

                if (!(bundleMsg is null) && !(bundleMsg.BUNDLE_INFO.bundle is null))
                {
                    int preKeyIndex = bundleMsg.BUNDLE_INFO.bundle.GetRandomPreKeyIndex();
                    session = new OmemoSessionModel(bundleMsg.BUNDLE_INFO.bundle, preKeyIndex, CONNECTION.account.omemoIdentityKey);

                    // Validate fingerprints:
                    if (fingerprint is null)
                    {
                        fingerprint = new OmemoFingerprint(bundleMsg.BUNDLE_INFO.bundle.identityKey, device);
                        OMEMO_HELPER.OMEMO_STORAGE.StoreFingerprint(fingerprint);
                    }
                    else
                    {
                        OmemoFingerprint receivedFingerprint = new OmemoFingerprint(bundleMsg.BUNDLE_INFO.bundle.identityKey, device);
                        // Make sure the fingerprint did not change or somebody is performing an attack:
                        if (!fingerprint.checkIdentityKey(receivedFingerprint.IDENTITY_KEY))
                        {
                            Logger.Warn("[OmemoSessionBuildHelper] Unable to establish session with " + device.ToString() + " - other fingerprint received than stored locally.");
                            await buildSessionForDevicesAsync(deviceGroup, devices);

                            return;
                        }
                    }
                }
                else
                {
                    Logger.Warn("[OmemoSessionBuildHelper] Unable to establish session with: " + device.ToString());
                }
            }