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

Commit

Permalink
Scanning OMEMO fingerprint QR Codes now actually marks them as trusted
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed Sep 2, 2019
1 parent 7057408 commit 7fd858e
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 24 deletions.
3 changes: 1 addition & 2 deletions Data_Manager2/Classes/Toast/ToastHelper.cs
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Toolkit.Uwp.Notifications;
using Shared.Classes;
using Windows.Data.Xml.Dom;
using Windows.Phone.Devices.Notification;
using Windows.UI.Notifications;

namespace Data_Manager2.Classes.Toast
Expand Down Expand Up @@ -230,7 +229,7 @@ private static void popToastReduced()
// Vibrate:
if (DeviceFamilyHelper.SupportsVibration() && !Settings.getSettingBoolean(SettingsConsts.DISABLE_VIBRATION_FOR_NEW_CHAT_MESSAGES))
{
VibrationDevice.GetDefault().Vibrate(VIBRATE_TS);
SharedUtils.VibratePress(VIBRATE_TS);
}

// Play sound:
Expand Down
16 changes: 15 additions & 1 deletion Shared/Classes/SharedUtils.cs
Expand Up @@ -4,6 +4,7 @@
using Windows.ApplicationModel.Core;
using Windows.Media.Core;
using Windows.Media.Playback;
using Windows.Phone.Devices.Notification;
using Windows.UI.Core;

namespace Shared.Classes
Expand Down Expand Up @@ -89,7 +90,7 @@ public static void RetryOnException(Action action, int retryCount)
/// <returns>The return value of the given function.</returns>
public static T RetryOnException<T>(Func<T> funct)
{
return RetryOnException<T>(funct, 1);
return RetryOnException(funct, 1);
}

/// <summary>
Expand Down Expand Up @@ -137,6 +138,19 @@ public static MediaPlayer PlaySoundFromUri(string uri)
return player;
}

/// <summary>
/// Vibrates the device for the given timespan if the device supports phone vibration.
/// </summary>
/// <param name="duration">How long should the vibration persist. Max 5 seconds.</param>
public static void VibratePress(TimeSpan duration)
{
if (DeviceFamilyHelper.SupportsVibration())
{
VibrationDevice.GetDefault().Vibrate(duration);
}
Logger.Debug("Vibration not supported.");
}

#endregion

#region --Misc Methods (Private)--
Expand Down
5 changes: 5 additions & 0 deletions Shared/Shared.csproj
Expand Up @@ -166,6 +166,11 @@
<Name>Logging</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<SDKReference Include="WindowsMobile, Version=10.0.17134.0">
<Name>Windows Mobile Extensions for the UWP</Name>
</SDKReference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion UWPX_UI/Controls/OMEMO/OmemoTrustFingerprintControl.xaml
Expand Up @@ -25,7 +25,7 @@
<ToggleSwitch x:Name="trust_tgls"
Grid.Row="1"
Grid.Column="0"
Margin="0,0,-20,0"
Margin="0,0,-30,0"
VerticalAlignment="Center"
IsOn="{x:Bind VIEW_MODEL.MODEL.Trusted, Mode=TwoWay}"
OffContent="Untrusted"
Expand Down
6 changes: 4 additions & 2 deletions UWPX_UI/Controls/QrCodeScannerControl.xaml.cs
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using System.Threading.Tasks;
using Logging;
using Shared.Classes;
using UWPX_UI_Context.Classes.DataContext.Controls;
using UWPX_UI_Context.Classes.Events;
using Windows.ApplicationModel;
Expand Down Expand Up @@ -94,7 +95,7 @@ private void StartQrCodeTask()
{
try
{
Result result = QR_CODE_READER.Decode(qrCodeBitmap);
QR_CODE_READER.Decode(qrCodeBitmap);
}
catch (Exception e)
{
Expand Down Expand Up @@ -149,12 +150,13 @@ private void Current_Resuming(object sender, object e)

private void QR_CODE_READER_ResultFound(Result result)
{
Logger.Debug("Scanned QR Code: " + result.Text);
Logger.Info("Scanned QR Code: " + result.Text);
if (!string.Equals(result.Text, VIEW_MODEL.MODEL.QrCode))
{
VIEW_MODEL.MODEL.QrCode = result.Text;
if (VIEW_MODEL.IsValidQrCode(result.Text))
{
SharedUtils.VibratePress(TimeSpan.FromMilliseconds(150));
NewValidQrCode?.Invoke(this, new NewQrCodeEventArgs(result.Text));
}
else
Expand Down
@@ -1,13 +1,16 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Data_Manager2.Classes.DBManager.Omemo;
using Data_Manager2.Classes.DBTables;
using Logging;
using UWPX_UI_Context.Classes.DataTemplates.Controls.Chat;
using UWPX_UI_Context.Classes.DataTemplates.Dialogs;
using Windows.UI.Xaml;
using XMPP_API.Classes;
using XMPP_API.Classes.Network.XML.Messages.XEP_0384;
using XMPP_API.Classes.XmppUri;

namespace UWPX_UI_Context.Classes.DataContext.Controls.Chat
{
Expand Down Expand Up @@ -67,7 +70,46 @@ public void OnFingerprintTrustedChanged(OmemoFingerprint fingerprint)

public void OnQrCodeScannerShown(QrCodeScannerDialogDataTemplate model)
{
// TODO: Analyze QR Code text and set the right fingerprint to valid.
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);
}
}
}

#endregion
Expand Down
4 changes: 4 additions & 0 deletions XMPP_API/Classes/XmppUri/IUriAction.cs
@@ -0,0 +1,4 @@
namespace XMPP_API.Classes.XmppUri
{
public interface IUriAction { }
}
@@ -1,15 +1,20 @@
namespace XMPP_API.Classes.XmppUri
using XMPP_API.Classes.Network.XML.Messages.XEP_0384;

namespace XMPP_API.Classes.XmppUri
{
public abstract class AbstractUriAction
public class OmemoFingerprintUriAction: IUriAction
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--

public readonly OmemoFingerprint FINGERPRINT;

#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
#region --Constructors--

public OmemoFingerprintUriAction(OmemoFingerprint fingerprint)
{
FINGERPRINT = fingerprint;
}

#endregion
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
Expand Down
2 changes: 1 addition & 1 deletion XMPP_API/Classes/XmppUri/SendMessageUriAction.cs
@@ -1,6 +1,6 @@
namespace XMPP_API.Classes.XmppUri
{
public class SendMessageUriAction: AbstractUriAction
public class SendMessageUriAction: IUriAction
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
Expand Down
51 changes: 41 additions & 10 deletions XMPP_API/Classes/XmppUri/UriUtils.cs
Expand Up @@ -3,8 +3,12 @@
using System.Linq;
using System.Net;
using System.Text;
using libsignal;
using libsignal.ecc;
using Logging;
using Windows.Foundation;
using XMPP_API.Classes.Crypto;
using XMPP_API.Classes.Network.XML.Messages.XEP_0384;

namespace XMPP_API.Classes.XmppUri
{
Expand Down Expand Up @@ -87,19 +91,20 @@ public static WwwFormUrlDecoder parseUriQuery(Uri uri)
}

/// <summary>
/// TODO: Not done yet.
/// [WIP]<para/>
/// Parses XMPP IRIs and URIs based on RFC 5122 and returns the result.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public static AbstractUriAction parse(Uri uri)
/// <param name="uri">The URI or IRI that should get parsed.</param>
/// <returns>The URI or IRI result or null if an error occurred.</returns>
public static IUriAction parse(Uri uri)
{
if (!string.IsNullOrEmpty(uri?.OriginalString))
{
string tmp = uri.OriginalString;

// 1. 'xmpp:'
if (tmp.StartsWith("xmpp:"))
if (string.Equals(uri.Scheme.ToLowerInvariant(), "xmpp"))
{
string tmp = uri.OriginalString;

// 1. remove 'xmpp:'
tmp = tmp.Substring(5);

// 2. Authority
Expand All @@ -124,11 +129,37 @@ public static AbstractUriAction parse(Uri uri)
}
}

// 3.
// Check if is OMEMO fingerprint URI:
WwwFormUrlDecoder query = parseUriQuery(uri);
IWwwFormUrlDecoderEntry entry = query.FirstOrDefault(x => x.Name.StartsWith("omemo-sid-"));
if (!(entry is null))
{
ECPublicKey pubKey = null;
try
{
byte[] fingerprintBytes = CryptoUtils.hexStringToByteArray(entry.Value);
pubKey = Curve.decodePoint(fingerprintBytes, 0);
}
catch (Exception e)
{
Logger.Error("Failed to parse XMPP URI. Parsing fingerprint failed: " + entry.Value, e);
return null;
}

if (uint.TryParse(entry.Name.Replace("omemo-sid-", "").Trim(), out uint deviceId))
{
SignalProtocolAddress address = new SignalProtocolAddress(uri.LocalPath, deviceId);
return new OmemoFingerprintUriAction(new OmemoFingerprint(pubKey, address));
}
else
{
Logger.Warn("Failed to parse XMPP URI. Invalid device ID: " + entry.Name);
}
}
}
else
{
Logger.Warn("Unable to parse XMPP URI - 'xmpp:' missing.");
Logger.Warn("Failed to parse XMPP URI. No 'xmpp' scheme.");
}
}
return null;
Expand Down
3 changes: 2 additions & 1 deletion XMPP_API/XMPP_API.csproj
Expand Up @@ -339,8 +339,9 @@
<Compile Include="Classes\OmemoCommandHelper.cs" />
<Compile Include="Classes\Presence.cs" />
<Compile Include="Classes\PubSubCommandHelper.cs" />
<Compile Include="Classes\XmppUri\OmemoFingerprintUriAction.cs" />
<Compile Include="Classes\XmppUri\UriUtils.cs" />
<Compile Include="Classes\XmppUri\AbstractUriAction.cs" />
<Compile Include="Classes\XmppUri\IUriAction.cs" />
<Compile Include="Classes\XmppUri\SendMessageUriAction.cs" />
<Compile Include="Classes\Utils.cs" />
<Compile Include="Classes\XMPPClient.cs" />
Expand Down

0 comments on commit 7fd858e

Please sign in to comment.