Skip to content

MXCrypto: Use the last olm session that got a message #611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
Changes in Matrix iOS SDK in 0.12.1 (2012-12-xx)
===============================================

Improvements:
* MXCrypto: Use the last olm session that got a message (vector-im/riot-ios/issues/2128).
* MXScanManager: Support the encrypted body (the request body is now encrypted by default using the server public key).
* MXMediaManager: Support the encrypted body.

Bug Fix:
* MXCryptoStore: Stop duplicating devices in the store (vector-im/riot-ios/issues/2132).

Improvements:
* MXScanManager: Support the encrypted body (the request body is now encrypted by default using the server public key).
* MXMediaManager: Support the encrypted body.

Changes in Matrix iOS SDK in 0.12.0 (2018-12-06)
===============================================

Expand Down
8 changes: 8 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@
32E226A61D06AC9F00E6CA54 /* MXPeekingRoom.h in Headers */ = {isa = PBXBuildFile; fileRef = 32E226A41D06AC9F00E6CA54 /* MXPeekingRoom.h */; settings = {ATTRIBUTES = (Public, ); }; };
32E226A71D06AC9F00E6CA54 /* MXPeekingRoom.m in Sources */ = {isa = PBXBuildFile; fileRef = 32E226A51D06AC9F00E6CA54 /* MXPeekingRoom.m */; };
32E226A91D081CE200E6CA54 /* MXPeekingRoomTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 32E226A81D081CE200E6CA54 /* MXPeekingRoomTests.m */; };
32E402B921C957D2004E87A6 /* MXOlmSession.h in Headers */ = {isa = PBXBuildFile; fileRef = 32E402B721C957D2004E87A6 /* MXOlmSession.h */; };
32E402BA21C957D2004E87A6 /* MXOlmSession.m in Sources */ = {isa = PBXBuildFile; fileRef = 32E402B821C957D2004E87A6 /* MXOlmSession.m */; };
32F634AB1FC5E3480054EF49 /* MXEventDecryptionResult.h in Headers */ = {isa = PBXBuildFile; fileRef = 32F634A91FC5E3470054EF49 /* MXEventDecryptionResult.h */; settings = {ATTRIBUTES = (Public, ); }; };
32F634AC1FC5E3480054EF49 /* MXEventDecryptionResult.m in Sources */ = {isa = PBXBuildFile; fileRef = 32F634AA1FC5E3470054EF49 /* MXEventDecryptionResult.m */; };
32F945F51FAB83D900622468 /* MXIncomingRoomKeyRequestCancellation.m in Sources */ = {isa = PBXBuildFile; fileRef = 32F945F11FAB83D800622468 /* MXIncomingRoomKeyRequestCancellation.m */; };
Expand Down Expand Up @@ -614,6 +616,8 @@
32E226A41D06AC9F00E6CA54 /* MXPeekingRoom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXPeekingRoom.h; sourceTree = "<group>"; };
32E226A51D06AC9F00E6CA54 /* MXPeekingRoom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXPeekingRoom.m; sourceTree = "<group>"; };
32E226A81D081CE200E6CA54 /* MXPeekingRoomTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXPeekingRoomTests.m; sourceTree = "<group>"; };
32E402B721C957D2004E87A6 /* MXOlmSession.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXOlmSession.h; sourceTree = "<group>"; };
32E402B821C957D2004E87A6 /* MXOlmSession.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXOlmSession.m; sourceTree = "<group>"; };
32F1FE9AF82A426C2EAED587 /* Pods-MatrixSDK.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MatrixSDK.release.xcconfig"; path = "Pods/Target Support Files/Pods-MatrixSDK/Pods-MatrixSDK.release.xcconfig"; sourceTree = "<group>"; };
32F634A91FC5E3470054EF49 /* MXEventDecryptionResult.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXEventDecryptionResult.h; sourceTree = "<group>"; };
32F634AA1FC5E3470054EF49 /* MXEventDecryptionResult.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXEventDecryptionResult.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1131,6 +1135,8 @@
32A151451DAF7C0C00400192 /* MXUsersDevicesMap.m */,
324BE46A1E422766008D99D4 /* MXMegolmSessionData.h */,
324BE46B1E422766008D99D4 /* MXMegolmSessionData.m */,
32E402B721C957D2004E87A6 /* MXOlmSession.h */,
32E402B821C957D2004E87A6 /* MXOlmSession.m */,
);
path = Data;
sourceTree = "<group>";
Expand Down Expand Up @@ -1593,6 +1599,7 @@
323F3F9420D3F0C700D26D6A /* MXRoomEventFilter.h in Headers */,
32A9E8251EF4026E0081358A /* MXUIKitBackgroundModeHandler.h in Headers */,
325D1C261DFECE0D0070B8BF /* MXCrypto_Private.h in Headers */,
32E402B921C957D2004E87A6 /* MXOlmSession.h in Headers */,
B146D4D621A5A44E00D8C2C6 /* MXScanRealmInMemoryProvider.h in Headers */,
B17285792100C8EA0052C51E /* MXSendReplyEventStringsLocalizable.h in Headers */,
32A151521DAF8A7200400192 /* MXQueuedEncryption.h in Headers */,
Expand Down Expand Up @@ -1866,6 +1873,7 @@
32D776821A27877300FC4AA2 /* MXMemoryRoomStore.m in Sources */,
3293C701214BBA4F009B3DDB /* MXPeekingRoomSummary.m in Sources */,
C602B58C1F2268F700B67D87 /* MXRoom.swift in Sources */,
32E402BA21C957D2004E87A6 /* MXOlmSession.m in Sources */,
32A151271DABB0CB00400192 /* MXMegolmDecryption.m in Sources */,
C6D5D60E1E4FBD2900706C0F /* MXSession.swift in Sources */,
320DFDE319DD99B60068622A /* MXError.m in Sources */,
Expand Down
56 changes: 56 additions & 0 deletions MatrixSDK/Crypto/Data/MXOlmSession.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2018 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MXSDKOptions.h"

#ifdef MX_CRYPTO

#import <OLMKit/OLMKit.h>

NS_ASSUME_NONNULL_BEGIN

/**
The 'MXOlmSession' class adds additional information to an OLMSession object from OLMKit.
*/
@interface MXOlmSession : NSObject


- (instancetype)initWithOlmSession:(OLMSession*)session;

/**
The associated olm session.
*/
@property (nonatomic, readonly) OLMSession *session;

/**
Timestamp at which the session last received a message.
*/
@property (nonatomic) NSTimeInterval lastReceivedMessageTs;


/**
Notify this model that a message has been received on this olm session
so that it updates `lastReceivedMessageTs`
*/
- (void)didReceiveMessage;

@end

NS_ASSUME_NONNULL_END

#endif
37 changes: 37 additions & 0 deletions MatrixSDK/Crypto/Data/MXOlmSession.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2018 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "MXOlmSession.h"

@implementation MXOlmSession

- (instancetype)initWithOlmSession:(OLMSession *)session
{
self = [super init];
if (self)
{
_session = session;
_lastReceivedMessageTs = 0;
}
return self;
}

- (void)didReceiveMessage
{
_lastReceivedMessageTs = [[NSDate date] timeIntervalSince1970];
}

@end
19 changes: 15 additions & 4 deletions MatrixSDK/Crypto/Data/Store/MXCryptoStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "MXJSONModels.h"

#import <OLMKit/OLMKit.h>
#import "MXOlmSession.h"
#import "MXOlmInboundGroupSession.h"
#import "MXDeviceInfo.h"
#import "MXOutgoingRoomKeyRequest.h"
Expand Down Expand Up @@ -188,16 +189,26 @@
@param deviceKey the public key of the other device.
@param session the end-to-end session.
*/
- (void)storeSession:(OLMSession*)session forDevice:(NSString*)deviceKey;
- (void)storeSession:(MXOlmSession*)session forDevice:(NSString*)deviceKey;

/**
Retrieve the end-to-end sessions between the logged-in user and another
Retrieve an end-to-end session between the logged-in user and another
device.

@param deviceKey the public key of the other device.
@return {object} A map from sessionId to Base64 end-to-end session.
@return a array of end-to-end sessions sorted by the last updated first.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong comment?

*/
- (NSDictionary<NSString*, OLMSession*>*)sessionsWithDevice:(NSString*)deviceKey;
- (MXOlmSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)sessionId;

/**
Retrieve all end-to-end sessions between the logged-in user and another
device sorted by `lastReceivedMessageTs`, the most recent(higest value) first.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: highest


@param deviceKey the public key of the other device.
@return a array of end-to-end sessions.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result is sorted (comments mixed up)

*/
- (NSArray<MXOlmSession*>*)sessionsWithDevice:(NSString*)deviceKey;


/**
Store an inbound group session.
Expand Down
69 changes: 58 additions & 11 deletions MatrixSDK/Crypto/Data/Store/MXRealmCryptoStore/MXRealmCryptoStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#import "MXSession.h"
#import "MXTools.h"

NSUInteger const kMXRealmCryptoStoreVersion = 8;
NSUInteger const kMXRealmCryptoStoreVersion = 9;

static NSString *const kMXRealmCryptoStoreFolder = @"MXRealmCryptoStore";

Expand Down Expand Up @@ -84,6 +84,7 @@ + (NSString *)primaryKey
@interface MXRealmOlmSession : RLMObject
@property NSString *sessionId;
@property NSString *deviceKey;
@property NSTimeInterval lastReceivedMessageTs;
@property NSData *olmSessionData;
@end

Expand Down Expand Up @@ -599,48 +600,77 @@ - (MXRealmRoomAlgorithm *)realmRoomAlgorithmForRoom:(NSString*)roomId inRealm:(R
}


- (void)storeSession:(OLMSession*)session forDevice:(NSString*)deviceKey
- (void)storeSession:(MXOlmSession*)session forDevice:(NSString*)deviceKey
{
BOOL isNew = NO;
NSDate *startDate = [NSDate date];

RLMRealm *realm = self.realm;
[realm transactionWithBlock:^{

MXRealmOlmSession *realmOlmSession = [MXRealmOlmSession objectsInRealm:realm where:@"sessionId = %@ AND deviceKey = %@", session.sessionIdentifier, deviceKey].firstObject;
MXRealmOlmSession *realmOlmSession = [MXRealmOlmSession objectsInRealm:realm where:@"sessionId = %@ AND deviceKey = %@", session.session.sessionIdentifier, deviceKey].firstObject;
if (realmOlmSession)
{
// Update the existing one
realmOlmSession.olmSessionData = [NSKeyedArchiver archivedDataWithRootObject:session];
realmOlmSession.olmSessionData = [NSKeyedArchiver archivedDataWithRootObject:session.session];
}
else
{
// Create it
realmOlmSession = [[MXRealmOlmSession alloc] initWithValue:@{
@"sessionId": session.sessionIdentifier,
@"sessionId": session.session.sessionIdentifier,
@"deviceKey": deviceKey,
@"olmSessionData": [NSKeyedArchiver archivedDataWithRootObject:session]
@"olmSessionData": [NSKeyedArchiver archivedDataWithRootObject:session.session]
}];
realmOlmSession.lastReceivedMessageTs = session.lastReceivedMessageTs;

[realm addObject:realmOlmSession];
}
}];

NSLog(@"[MXRealmCryptoStore] storeSession (%@) in %.0fms", (isNew?@"NEW":@"UPDATE"), [[NSDate date] timeIntervalSinceDate:startDate] * 1000);
}

- (NSDictionary<NSString*, OLMSession*>*)sessionsWithDevice:(NSString*)deviceKey
- (MXOlmSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)sessionId
{
NSMutableDictionary<NSString*, OLMSession*> *sessionsWithDevice;
MXRealmOlmSession *realmOlmSession = [MXRealmOlmSession objectsInRealm:self.realm
where:@"sessionId = %@ AND deviceKey = %@", sessionId, deviceKey].firstObject;

MXOlmSession *mxOlmSession;
if (realmOlmSession.olmSessionData)
{
OLMSession *olmSession = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];

RLMResults<MXRealmOlmSession *> *realmOlmSessions = [MXRealmOlmSession objectsInRealm:self.realm where:@"deviceKey = %@", deviceKey];
mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
mxOlmSession.lastReceivedMessageTs = realmOlmSession.lastReceivedMessageTs;
}

return mxOlmSession;
}

- (NSArray<MXOlmSession*>*)sessionsWithDevice:(NSString*)deviceKey;
{
NSMutableArray<MXOlmSession*> *sessionsWithDevice;

RLMResults<MXRealmOlmSession *> *realmOlmSessions = [[MXRealmOlmSession objectsInRealm:self.realm
where:@"deviceKey = %@", deviceKey]
sortedResultsUsingKeyPath:@"lastReceivedMessageTs" ascending:NO];
for (MXRealmOlmSession *realmOlmSession in realmOlmSessions)
{
if (!sessionsWithDevice)
{
sessionsWithDevice = [NSMutableDictionary dictionary];
sessionsWithDevice = [NSMutableArray array];
}

sessionsWithDevice[realmOlmSession.sessionId] = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];
if (realmOlmSession.olmSessionData)
{
OLMSession *olmSession = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];

MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
mxOlmSession.lastReceivedMessageTs = realmOlmSession.lastReceivedMessageTs;

[sessionsWithDevice addObject:mxOlmSession];
}
}

return sessionsWithDevice;
Expand Down Expand Up @@ -1189,6 +1219,23 @@ + (RLMRealm*)realmForUser:(NSString*)userId
// make queries. So, the cleaning will be done afterwards.
cleanDuplicatedDevices = YES;
}

case 8:
{
// MXRealmOlmSession.lastReceivedMessageTs has been added to implement:
// Use the last olm session that got a message
// https://github.com/vector-im/riot-ios/issues/2128

NSLog(@"[MXRealmCryptoStore] Migration from schema #8 -> #9");

NSLog(@" Add lastReceivedMessageTs = 0 to all MXRealmOlmSession objects");
[migration enumerateObjects:MXRealmOlmSession.className block:^(RLMObject *oldObject, RLMObject *newObject) {

newObject[@"lastReceivedMessageTs"] = 0;
}];

NSLog(@"[MXRealmCryptoStore] Migration from schema #8 -> #9 completed");
}
}
}
};
Expand Down
Loading