From 7ee17a295738f0db8cab74cec2343a577f2ade45 Mon Sep 17 00:00:00 2001
From: manuroe <manu@matrix.org>
Date: Mon, 14 Nov 2016 17:35:24 +0100
Subject: [PATCH] OLMKit: Add missing implementations for matchesInboundSession
 matchesInboundSessionFrom

---
 xcode/OLMKit.xcodeproj/project.pbxproj |  8 +++---
 xcode/OLMKit/OLMSession.m              | 35 ++++++++++++++++++++++++++
 xcode/OLMKitTests/OLMKitTests.m        | 11 ++++++--
 xcode/Podfile.lock                     |  2 +-
 4 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/xcode/OLMKit.xcodeproj/project.pbxproj b/xcode/OLMKit.xcodeproj/project.pbxproj
index 0ec7587..a3b636f 100644
--- a/xcode/OLMKit.xcodeproj/project.pbxproj
+++ b/xcode/OLMKit.xcodeproj/project.pbxproj
@@ -182,7 +182,7 @@
 		3274F5EF1D9A633A005282E4 /* Project object */ = {
 			isa = PBXProject;
 			attributes = {
-				LastUpgradeCheck = 0800;
+				LastUpgradeCheck = 0810;
 				ORGANIZATIONNAME = matrix.org;
 				TargetAttributes = {
 					3274F5F71D9A633A005282E4 = {
@@ -273,7 +273,7 @@
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 			shellPath = /bin/sh;
-			shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n    # print error to STDERR\n    echo \"error: The sandbox is not in sync with the Podfile.lock. Run \'pod install\' or update your CocoaPods installation.\" >&2\n    exit 1\nfi\n";
+			shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n    # print error to STDERR\n    echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n    exit 1\nfi\n";
 			showEnvVarsInLog = 0;
 		};
 		793D0533290528B7C0E17CAD /* [CP] Copy Pods Resources */ = {
@@ -303,7 +303,7 @@
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 			shellPath = /bin/sh;
-			shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n    # print error to STDERR\n    echo \"error: The sandbox is not in sync with the Podfile.lock. Run \'pod install\' or update your CocoaPods installation.\" >&2\n    exit 1\nfi\n";
+			shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n    # print error to STDERR\n    echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n    exit 1\nfi\n";
 			showEnvVarsInLog = 0;
 		};
 /* End PBXShellScriptBuildPhase section */
@@ -354,6 +354,7 @@
 				CLANG_WARN_INFINITE_RECURSION = YES;
 				CLANG_WARN_INT_CONVERSION = YES;
 				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_SUSPICIOUS_MOVE = YES;
 				CLANG_WARN_SUSPICIOUS_MOVES = YES;
 				CLANG_WARN_UNREACHABLE_CODE = YES;
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -405,6 +406,7 @@
 				CLANG_WARN_INFINITE_RECURSION = YES;
 				CLANG_WARN_INT_CONVERSION = YES;
 				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_SUSPICIOUS_MOVE = YES;
 				CLANG_WARN_SUSPICIOUS_MOVES = YES;
 				CLANG_WARN_UNREACHABLE_CODE = YES;
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m
index a47deb1..eee65a9 100644
--- a/xcode/OLMKit/OLMSession.m
+++ b/xcode/OLMKit/OLMSession.m
@@ -156,6 +156,41 @@
     return idString;
 }
 
+- (BOOL)matchesInboundSession:(NSString *)oneTimeKeyMessage {
+    NSData *otk = [oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding];
+
+    size_t result = olm_matches_inbound_session(_session, otk.bytes, otk.length);
+    if (result == 1) {
+        return YES;
+    }
+    else {
+        if (result == olm_error()) {
+            const char *error = olm_session_last_error(_session);
+            NSLog(@"olm_matches_inbound_session error: %s", error);
+        }
+        return NO;
+    }
+}
+
+- (BOOL)matchesInboundSessionFrom:(NSString *)theirIdentityKey oneTimeKeyMessage:(NSString *)oneTimeKeyMessage {
+    NSData *idKey = [theirIdentityKey dataUsingEncoding:NSUTF8StringEncoding];
+    NSData *otk = [oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding];
+
+    size_t result = olm_matches_inbound_session_from(_session,
+                                                     idKey.bytes, idKey.length,
+                                                     otk.bytes, otk.length);
+    if (result == 1) {
+        return YES;
+    }
+    else {
+        if (result == olm_error()) {
+            const char *error = olm_session_last_error(_session);
+            NSLog(@"olm_matches_inbound_session error: %s", error);
+        }
+        return NO;
+    }
+}
+
 - (OLMMessage*) encryptMessage:(NSString*)message error:(NSError**)error {
     size_t messageType = olm_encrypt_message_type(_session);
     size_t randomLength = olm_encrypt_random_length(_session);
diff --git a/xcode/OLMKitTests/OLMKitTests.m b/xcode/OLMKitTests/OLMKitTests.m
index 251c90e..1adbde2 100644
--- a/xcode/OLMKitTests/OLMKitTests.m
+++ b/xcode/OLMKitTests/OLMKitTests.m
@@ -52,6 +52,15 @@
     NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:&error];
     XCTAssertEqualObjects(message, plaintext);
     XCTAssertNil(error);
+
+    XCTAssert([bobSession matchesInboundSession:aliceToBobMsg.ciphertext]);
+    XCTAssertFalse([aliceSession matchesInboundSession:@"ARandomOtkMessage"]);
+
+    NSString *aliceIdKey = alice.identityKeys[@"curve25519"];
+    XCTAssert([bobSession matchesInboundSessionFrom:aliceIdKey oneTimeKeyMessage:aliceToBobMsg.ciphertext]);
+    XCTAssertFalse([bobSession matchesInboundSessionFrom:@"ARandomIdKey" oneTimeKeyMessage:aliceToBobMsg.ciphertext]);
+    XCTAssertFalse([bobSession matchesInboundSessionFrom:aliceIdKey oneTimeKeyMessage:@"ARandomOtkMessage"]);
+
     BOOL success = [bob removeOneTimeKeysForSession:bobSession];
     XCTAssertTrue(success);
 }
@@ -96,8 +105,6 @@
     XCTAssertEqualObjects(msg1, dMsg1);
     XCTAssertEqualObjects(msg2, dMsg2);
     XCTAssertEqualObjects(msg3, dMsg3);
-
-    
 }
 
 - (void) testAccountSerialization {
diff --git a/xcode/Podfile.lock b/xcode/Podfile.lock
index 3ac110a..2d69849 100644
--- a/xcode/Podfile.lock
+++ b/xcode/Podfile.lock
@@ -13,7 +13,7 @@ EXTERNAL SOURCES:
     :path: ../OLMKit.podspec
 
 SPEC CHECKSUMS:
-  OLMKit: ed17cdf7695bc0de1e2bf98243eb65f5b9ddebc1
+  OLMKit: 5f2894a210ca99ad17128af4fd3beeeeb0eca404
 
 PODFILE CHECKSUM: 4e261dae61d833ec5585ced2473023b98909fd35
 
-- 
GitLab