public OmemoFingerprintTable(OmemoFingerprint fingerprint, string chatId) { this.chatId = chatId; bareJid = fingerprint.ADDRESS.getName(); deviceId = fingerprint.ADDRESS.getDeviceId(); id = generateId(chatId, bareJid, deviceId); identityPubKey = fingerprint.IDENTITY_PUB_KEY.serialize(); lastSeen = fingerprint.lastSeen; trusted = fingerprint.trusted; }
public void OnFingerprintTrustedChanged(OmemoFingerprint fingerprint) { if (fingerprint.trusted) { MODEL.TrustedOnly = true; } if (!(MODEL.Client is null)) { OmemoSignalKeyDBManager.INSTANCE.setFingerprint(fingerprint, MODEL.Client.getXMPPAccount().getBareJid()); } }
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()); } }
/// <summary> /// Checks if the fingerprint is trusted by the following mechanism: /// 1. Does a chat exist for the fingerprint? Yes -> 2. No. -> false /// 2. Is "trusted keys only" activated for chats? Yes -> 3. No -> true /// 3. Is the key trusted? Yes -> true No -> false /// </summary> /// <param name="fingerprint">The fingerprint we want to check if it's valid.</param> public bool IsFingerprintTrusted(OmemoFingerprint fingerprint) { // 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 OnQrCodeScannerShown(QrCodeScannerDialogDataTemplate model) { if (model.Success) { Uri uri = null; try { uri = new Uri(model.QrCode); } catch (Exception e) { Logger.Error("Failed to parse OMEMO fingerprint XMPP URI. Malformed URI: " + model.QrCode, e); return; } if (string.Equals(uri.LocalPath.ToLowerInvariant(), MODEL.Chat.chatJabberId.ToLowerInvariant())) { IUriAction action = UriUtils.parse(uri); if (action is OmemoFingerprintUriAction fingerprintUriAction) { OmemoFingerprint fingerprint = OmemoSignalKeyDBManager.INSTANCE.getFingerprint(fingerprintUriAction.FINGERPRINT.ADDRESS, MODEL.Client.getXMPPAccount().getBareJid()); if (fingerprint is null) { fingerprint = fingerprintUriAction.FINGERPRINT; } fingerprint.trusted = true; OmemoSignalKeyDBManager.INSTANCE.setFingerprint(fingerprint, MODEL.Client.getXMPPAccount().getBareJid()); Logger.Info("Scanned OMEMO fingerprint successful."); Logger.Debug("Fingerprint: " + fingerprint.ADDRESS.ToString()); LoadFingerprints(); } else { Logger.Warn("Failed to parse OMEMO fingerprint XMPP URI. Not an " + nameof(OmemoFingerprintUriAction) + "."); } } else { Logger.Warn("Failed to parse OMEMO fingerprint XMPP URI. Wrong chat: " + uri.LocalPath); } } }
public void StoreFingerprint(OmemoFingerprint fingerprint) { OmemoDeviceModel device; if (string.Equals(fingerprint.ADDRESS.BARE_JID, dbAccount.bareJid)) { device = dbAccount.omemoInfo.devices.Where(d => fingerprint.ADDRESS.DEVICE_ID == d.deviceId).FirstOrDefault(); } else { ChatModel chat; using (SemaLock semaLock = DataCache.INSTANCE.NewChatSemaLock()) { chat = DataCache.INSTANCE.GetChat(dbAccount.bareJid, fingerprint.ADDRESS.BARE_JID, semaLock); } if (chat is null) { throw new InvalidOperationException("Failed to store fingerprint. Chat '" + fingerprint.ADDRESS.BARE_JID + "' does not exist."); } device = chat.omemoInfo.devices.Where(d => d.deviceId == fingerprint.ADDRESS.DEVICE_ID).FirstOrDefault(); } if (device is null) { throw new InvalidOperationException("Failed to store fingerprint. Device '" + fingerprint.ADDRESS.ToString() + "' does not exist."); } if (device.fingerprint is null) { device.fingerprint = new OmemoFingerprintModel(fingerprint); using (MainDbContext ctx = new MainDbContext()) { ctx.Add(device.fingerprint); ctx.Update(device); } } else { device.fingerprint.lastSeen = fingerprint.lastSeen; device.fingerprint.trusted = fingerprint.trusted; device.fingerprint.Update(); } }
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- public OmemoFingerprintTrustChangedEventArgs(OmemoFingerprint fingerprint) { FINGERPRINT = fingerprint; }
private async Task buildSessionForDevicesAsync(Dictionary <uint, SessionCipher> sessions, IList <SignalProtocolAddress> devices) { if (devices.Count <= 0) { return; } 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)) { // If yes, the load it: SessionCipher cipher = OMEMO_HELPER.loadCipher(device); sessions.Add(device.getDeviceId(), cipher); Logger.Info("[OmemoSessionBuildHelper] Session for " + device.ToString() + " loaded from cache."); } else { // 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(device.getName(), bundleMsg); // Validate fingerprints: if (fingerprint is null) { fingerprint = new OmemoFingerprint(bundleMsg.BUNDLE_INFO.PUBLIC_IDENTITY_KEY, device); OMEMO_HELPER.OMEMO_STORE.StoreFingerprint(fingerprint); } else { OmemoFingerprint receivedFingerprint = new OmemoFingerprint(bundleMsg.BUNDLE_INFO.PUBLIC_IDENTITY_KEY, device); // Make sure the fingerprint did not change or somebody is doing an attack: 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; } } // Check if the fingerprint is trusted: if (OMEMO_HELPER.OMEMO_STORE.IsFingerprintTrusted(fingerprint)) { SessionCipher cipher = OMEMO_HELPER.loadCipher(device); sessions.Add(device.getDeviceId(), cipher); Logger.Info("[OmemoSessionBuildHelper] Session with " + device.ToString() + " established."); } else { Logger.Warn("[OmemoSessionBuildHelper] Unable to establish session with " + device.ToString() + " - key not trusted."); } }
public void setFingerprint(OmemoFingerprint fingerprint, string accountId) { string chatId = ChatTable.generateId(fingerprint.ADDRESS.getName(), accountId); dB.InsertOrReplace(new OmemoFingerprintTable(fingerprint, chatId)); }
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- public OmemoFingerprintUriAction(OmemoFingerprint fingerprint) { FINGERPRINT = fingerprint; }
public void StoreFingerprint(OmemoFingerprint fingerprint) { FINGERPRINTS[fingerprint.ADDRESS] = fingerprint; }
public void StoreFingerprint(OmemoFingerprint fingerprint) { OmemoSignalKeyDBManager.INSTANCE.setFingerprint(fingerprint, ACCOUNT.getBareJid()); }