Skip to content

Commit c5732c6

Browse files
authored
Merge pull request #611 from matrix-org/riot_2128
MXCrypto: Use the last olm session that got a message
2 parents 266e04b + f2c0f2a commit c5732c6

File tree

7 files changed

+218
-46
lines changed

7 files changed

+218
-46
lines changed

CHANGES.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
Changes in Matrix iOS SDK in 0.12.1 (2012-12-xx)
22
===============================================
33

4+
Improvements:
5+
* MXCrypto: Use the last olm session that got a message (vector-im/riot-ios/issues/2128).
6+
* MXScanManager: Support the encrypted body (the request body is now encrypted by default using the server public key).
7+
* MXMediaManager: Support the encrypted body.
8+
49
Bug Fix:
510
* MXCryptoStore: Stop duplicating devices in the store (vector-im/riot-ios/issues/2132).
611

7-
Improvements:
8-
* MXScanManager: Support the encrypted body (the request body is now encrypted by default using the server public key).
9-
* MXMediaManager: Support the encrypted body.
10-
1112
Changes in Matrix iOS SDK in 0.12.0 (2018-12-06)
1213
===============================================
1314

MatrixSDK.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@
240240
32E226A61D06AC9F00E6CA54 /* MXPeekingRoom.h in Headers */ = {isa = PBXBuildFile; fileRef = 32E226A41D06AC9F00E6CA54 /* MXPeekingRoom.h */; settings = {ATTRIBUTES = (Public, ); }; };
241241
32E226A71D06AC9F00E6CA54 /* MXPeekingRoom.m in Sources */ = {isa = PBXBuildFile; fileRef = 32E226A51D06AC9F00E6CA54 /* MXPeekingRoom.m */; };
242242
32E226A91D081CE200E6CA54 /* MXPeekingRoomTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 32E226A81D081CE200E6CA54 /* MXPeekingRoomTests.m */; };
243+
32E402B921C957D2004E87A6 /* MXOlmSession.h in Headers */ = {isa = PBXBuildFile; fileRef = 32E402B721C957D2004E87A6 /* MXOlmSession.h */; };
244+
32E402BA21C957D2004E87A6 /* MXOlmSession.m in Sources */ = {isa = PBXBuildFile; fileRef = 32E402B821C957D2004E87A6 /* MXOlmSession.m */; };
243245
32F634AB1FC5E3480054EF49 /* MXEventDecryptionResult.h in Headers */ = {isa = PBXBuildFile; fileRef = 32F634A91FC5E3470054EF49 /* MXEventDecryptionResult.h */; settings = {ATTRIBUTES = (Public, ); }; };
244246
32F634AC1FC5E3480054EF49 /* MXEventDecryptionResult.m in Sources */ = {isa = PBXBuildFile; fileRef = 32F634AA1FC5E3470054EF49 /* MXEventDecryptionResult.m */; };
245247
32F945F51FAB83D900622468 /* MXIncomingRoomKeyRequestCancellation.m in Sources */ = {isa = PBXBuildFile; fileRef = 32F945F11FAB83D800622468 /* MXIncomingRoomKeyRequestCancellation.m */; };
@@ -614,6 +616,8 @@
614616
32E226A41D06AC9F00E6CA54 /* MXPeekingRoom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXPeekingRoom.h; sourceTree = "<group>"; };
615617
32E226A51D06AC9F00E6CA54 /* MXPeekingRoom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXPeekingRoom.m; sourceTree = "<group>"; };
616618
32E226A81D081CE200E6CA54 /* MXPeekingRoomTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXPeekingRoomTests.m; sourceTree = "<group>"; };
619+
32E402B721C957D2004E87A6 /* MXOlmSession.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXOlmSession.h; sourceTree = "<group>"; };
620+
32E402B821C957D2004E87A6 /* MXOlmSession.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXOlmSession.m; sourceTree = "<group>"; };
617621
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>"; };
618622
32F634A91FC5E3470054EF49 /* MXEventDecryptionResult.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXEventDecryptionResult.h; sourceTree = "<group>"; };
619623
32F634AA1FC5E3470054EF49 /* MXEventDecryptionResult.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXEventDecryptionResult.m; sourceTree = "<group>"; };
@@ -1131,6 +1135,8 @@
11311135
32A151451DAF7C0C00400192 /* MXUsersDevicesMap.m */,
11321136
324BE46A1E422766008D99D4 /* MXMegolmSessionData.h */,
11331137
324BE46B1E422766008D99D4 /* MXMegolmSessionData.m */,
1138+
32E402B721C957D2004E87A6 /* MXOlmSession.h */,
1139+
32E402B821C957D2004E87A6 /* MXOlmSession.m */,
11341140
);
11351141
path = Data;
11361142
sourceTree = "<group>";
@@ -1593,6 +1599,7 @@
15931599
323F3F9420D3F0C700D26D6A /* MXRoomEventFilter.h in Headers */,
15941600
32A9E8251EF4026E0081358A /* MXUIKitBackgroundModeHandler.h in Headers */,
15951601
325D1C261DFECE0D0070B8BF /* MXCrypto_Private.h in Headers */,
1602+
32E402B921C957D2004E87A6 /* MXOlmSession.h in Headers */,
15961603
B146D4D621A5A44E00D8C2C6 /* MXScanRealmInMemoryProvider.h in Headers */,
15971604
B17285792100C8EA0052C51E /* MXSendReplyEventStringsLocalizable.h in Headers */,
15981605
32A151521DAF8A7200400192 /* MXQueuedEncryption.h in Headers */,
@@ -1866,6 +1873,7 @@
18661873
32D776821A27877300FC4AA2 /* MXMemoryRoomStore.m in Sources */,
18671874
3293C701214BBA4F009B3DDB /* MXPeekingRoomSummary.m in Sources */,
18681875
C602B58C1F2268F700B67D87 /* MXRoom.swift in Sources */,
1876+
32E402BA21C957D2004E87A6 /* MXOlmSession.m in Sources */,
18691877
32A151271DABB0CB00400192 /* MXMegolmDecryption.m in Sources */,
18701878
C6D5D60E1E4FBD2900706C0F /* MXSession.swift in Sources */,
18711879
320DFDE319DD99B60068622A /* MXError.m in Sources */,

MatrixSDK/Crypto/Data/MXOlmSession.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2018 New Vector Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import "MXSDKOptions.h"
20+
21+
#ifdef MX_CRYPTO
22+
23+
#import <OLMKit/OLMKit.h>
24+
25+
NS_ASSUME_NONNULL_BEGIN
26+
27+
/**
28+
The 'MXOlmSession' class adds additional information to an OLMSession object from OLMKit.
29+
*/
30+
@interface MXOlmSession : NSObject
31+
32+
33+
- (instancetype)initWithOlmSession:(OLMSession*)session;
34+
35+
/**
36+
The associated olm session.
37+
*/
38+
@property (nonatomic, readonly) OLMSession *session;
39+
40+
/**
41+
Timestamp at which the session last received a message.
42+
*/
43+
@property (nonatomic) NSTimeInterval lastReceivedMessageTs;
44+
45+
46+
/**
47+
Notify this model that a message has been received on this olm session
48+
so that it updates `lastReceivedMessageTs`
49+
*/
50+
- (void)didReceiveMessage;
51+
52+
@end
53+
54+
NS_ASSUME_NONNULL_END
55+
56+
#endif

MatrixSDK/Crypto/Data/MXOlmSession.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2018 New Vector Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#import "MXOlmSession.h"
18+
19+
@implementation MXOlmSession
20+
21+
- (instancetype)initWithOlmSession:(OLMSession *)session
22+
{
23+
self = [super init];
24+
if (self)
25+
{
26+
_session = session;
27+
_lastReceivedMessageTs = 0;
28+
}
29+
return self;
30+
}
31+
32+
- (void)didReceiveMessage
33+
{
34+
_lastReceivedMessageTs = [[NSDate date] timeIntervalSince1970];
35+
}
36+
37+
@end

MatrixSDK/Crypto/Data/Store/MXCryptoStore.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#import "MXJSONModels.h"
2525

2626
#import <OLMKit/OLMKit.h>
27+
#import "MXOlmSession.h"
2728
#import "MXOlmInboundGroupSession.h"
2829
#import "MXDeviceInfo.h"
2930
#import "MXOutgoingRoomKeyRequest.h"
@@ -188,16 +189,26 @@
188189
@param deviceKey the public key of the other device.
189190
@param session the end-to-end session.
190191
*/
191-
- (void)storeSession:(OLMSession*)session forDevice:(NSString*)deviceKey;
192+
- (void)storeSession:(MXOlmSession*)session forDevice:(NSString*)deviceKey;
192193

193194
/**
194-
Retrieve the end-to-end sessions between the logged-in user and another
195+
Retrieve an end-to-end session between the logged-in user and another
195196
device.
196197
197198
@param deviceKey the public key of the other device.
198-
@return {object} A map from sessionId to Base64 end-to-end session.
199+
@return a array of end-to-end sessions sorted by the last updated first.
199200
*/
200-
- (NSDictionary<NSString*, OLMSession*>*)sessionsWithDevice:(NSString*)deviceKey;
201+
- (MXOlmSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)sessionId;
202+
203+
/**
204+
Retrieve all end-to-end sessions between the logged-in user and another
205+
device sorted by `lastReceivedMessageTs`, the most recent(higest value) first.
206+
207+
@param deviceKey the public key of the other device.
208+
@return a array of end-to-end sessions.
209+
*/
210+
- (NSArray<MXOlmSession*>*)sessionsWithDevice:(NSString*)deviceKey;
211+
201212

202213
/**
203214
Store an inbound group session.

MatrixSDK/Crypto/Data/Store/MXRealmCryptoStore/MXRealmCryptoStore.m

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#import "MXSession.h"
2424
#import "MXTools.h"
2525

26-
NSUInteger const kMXRealmCryptoStoreVersion = 8;
26+
NSUInteger const kMXRealmCryptoStoreVersion = 9;
2727

2828
static NSString *const kMXRealmCryptoStoreFolder = @"MXRealmCryptoStore";
2929

@@ -84,6 +84,7 @@ + (NSString *)primaryKey
8484
@interface MXRealmOlmSession : RLMObject
8585
@property NSString *sessionId;
8686
@property NSString *deviceKey;
87+
@property NSTimeInterval lastReceivedMessageTs;
8788
@property NSData *olmSessionData;
8889
@end
8990

@@ -599,48 +600,77 @@ - (MXRealmRoomAlgorithm *)realmRoomAlgorithmForRoom:(NSString*)roomId inRealm:(R
599600
}
600601

601602

602-
- (void)storeSession:(OLMSession*)session forDevice:(NSString*)deviceKey
603+
- (void)storeSession:(MXOlmSession*)session forDevice:(NSString*)deviceKey
603604
{
604605
BOOL isNew = NO;
605606
NSDate *startDate = [NSDate date];
606607

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

610-
MXRealmOlmSession *realmOlmSession = [MXRealmOlmSession objectsInRealm:realm where:@"sessionId = %@ AND deviceKey = %@", session.sessionIdentifier, deviceKey].firstObject;
611+
MXRealmOlmSession *realmOlmSession = [MXRealmOlmSession objectsInRealm:realm where:@"sessionId = %@ AND deviceKey = %@", session.session.sessionIdentifier, deviceKey].firstObject;
611612
if (realmOlmSession)
612613
{
613614
// Update the existing one
614-
realmOlmSession.olmSessionData = [NSKeyedArchiver archivedDataWithRootObject:session];
615+
realmOlmSession.olmSessionData = [NSKeyedArchiver archivedDataWithRootObject:session.session];
615616
}
616617
else
617618
{
618619
// Create it
619620
realmOlmSession = [[MXRealmOlmSession alloc] initWithValue:@{
620-
@"sessionId": session.sessionIdentifier,
621+
@"sessionId": session.session.sessionIdentifier,
621622
@"deviceKey": deviceKey,
622-
@"olmSessionData": [NSKeyedArchiver archivedDataWithRootObject:session]
623+
@"olmSessionData": [NSKeyedArchiver archivedDataWithRootObject:session.session]
623624
}];
625+
realmOlmSession.lastReceivedMessageTs = session.lastReceivedMessageTs;
626+
624627
[realm addObject:realmOlmSession];
625628
}
626629
}];
627630

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

631-
- (NSDictionary<NSString*, OLMSession*>*)sessionsWithDevice:(NSString*)deviceKey
634+
- (MXOlmSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)sessionId
632635
{
633-
NSMutableDictionary<NSString*, OLMSession*> *sessionsWithDevice;
636+
MXRealmOlmSession *realmOlmSession = [MXRealmOlmSession objectsInRealm:self.realm
637+
where:@"sessionId = %@ AND deviceKey = %@", sessionId, deviceKey].firstObject;
638+
639+
MXOlmSession *mxOlmSession;
640+
if (realmOlmSession.olmSessionData)
641+
{
642+
OLMSession *olmSession = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];
634643

635-
RLMResults<MXRealmOlmSession *> *realmOlmSessions = [MXRealmOlmSession objectsInRealm:self.realm where:@"deviceKey = %@", deviceKey];
644+
mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
645+
mxOlmSession.lastReceivedMessageTs = realmOlmSession.lastReceivedMessageTs;
646+
}
647+
648+
return mxOlmSession;
649+
}
650+
651+
- (NSArray<MXOlmSession*>*)sessionsWithDevice:(NSString*)deviceKey;
652+
{
653+
NSMutableArray<MXOlmSession*> *sessionsWithDevice;
654+
655+
RLMResults<MXRealmOlmSession *> *realmOlmSessions = [[MXRealmOlmSession objectsInRealm:self.realm
656+
where:@"deviceKey = %@", deviceKey]
657+
sortedResultsUsingKeyPath:@"lastReceivedMessageTs" ascending:NO];
636658
for (MXRealmOlmSession *realmOlmSession in realmOlmSessions)
637659
{
638660
if (!sessionsWithDevice)
639661
{
640-
sessionsWithDevice = [NSMutableDictionary dictionary];
662+
sessionsWithDevice = [NSMutableArray array];
641663
}
642664

643-
sessionsWithDevice[realmOlmSession.sessionId] = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];
665+
if (realmOlmSession.olmSessionData)
666+
{
667+
OLMSession *olmSession = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];
668+
669+
MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
670+
mxOlmSession.lastReceivedMessageTs = realmOlmSession.lastReceivedMessageTs;
671+
672+
[sessionsWithDevice addObject:mxOlmSession];
673+
}
644674
}
645675

646676
return sessionsWithDevice;
@@ -1189,6 +1219,23 @@ + (RLMRealm*)realmForUser:(NSString*)userId
11891219
// make queries. So, the cleaning will be done afterwards.
11901220
cleanDuplicatedDevices = YES;
11911221
}
1222+
1223+
case 8:
1224+
{
1225+
// MXRealmOlmSession.lastReceivedMessageTs has been added to implement:
1226+
// Use the last olm session that got a message
1227+
// https://github.com/vector-im/riot-ios/issues/2128
1228+
1229+
NSLog(@"[MXRealmCryptoStore] Migration from schema #8 -> #9");
1230+
1231+
NSLog(@" Add lastReceivedMessageTs = 0 to all MXRealmOlmSession objects");
1232+
[migration enumerateObjects:MXRealmOlmSession.className block:^(RLMObject *oldObject, RLMObject *newObject) {
1233+
1234+
newObject[@"lastReceivedMessageTs"] = 0;
1235+
}];
1236+
1237+
NSLog(@"[MXRealmCryptoStore] Migration from schema #8 -> #9 completed");
1238+
}
11921239
}
11931240
}
11941241
};

0 commit comments

Comments
 (0)