Skip to content

Commit c7d9e6e

Browse files
committed
MXCrypto: Create MXOlmSession to add additional information to OLMSession objects from OLMKit
1 parent c8a8575 commit c7d9e6e

File tree

6 files changed

+122
-30
lines changed

6 files changed

+122
-30
lines changed

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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
@end
41+
42+
NS_ASSUME_NONNULL_END
43+
44+
#endif

MatrixSDK/Crypto/Data/MXOlmSession.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
28+
return self;
29+
}
30+
31+
@end

MatrixSDK/Crypto/Data/Store/MXCryptoStore.h

Lines changed: 4 additions & 3 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,7 +189,7 @@
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
/**
194195
Retrieve an end-to-end session between the logged-in user and another
@@ -197,7 +198,7 @@
197198
@param deviceKey the public key of the other device.
198199
@return a array of end-to-end sessions sorted by the last updated first.
199200
*/
200-
- (OLMSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)sessionId;
201+
- (MXOlmSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)sessionId;
201202

202203
/**
203204
Retrieve all end-to-end sessions between the logged-in user and another
@@ -206,7 +207,7 @@
206207
@param deviceKey the public key of the other device.
207208
@return a array of end-to-end sessions.
208209
*/
209-
- (NSArray<OLMSession*>*)sessionsWithDevice:(NSString*)deviceKey;
210+
- (NSArray<MXOlmSession*>*)sessionsWithDevice:(NSString*)deviceKey;
210211

211212

212213
/**

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -599,27 +599,27 @@ - (MXRealmRoomAlgorithm *)realmRoomAlgorithmForRoom:(NSString*)roomId inRealm:(R
599599
}
600600

601601

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

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

610-
MXRealmOlmSession *realmOlmSession = [MXRealmOlmSession objectsInRealm:realm where:@"sessionId = %@ AND deviceKey = %@", session.sessionIdentifier, deviceKey].firstObject;
610+
MXRealmOlmSession *realmOlmSession = [MXRealmOlmSession objectsInRealm:realm where:@"sessionId = %@ AND deviceKey = %@", session.session.sessionIdentifier, deviceKey].firstObject;
611611
if (realmOlmSession)
612612
{
613613
// Update the existing one
614-
realmOlmSession.olmSessionData = [NSKeyedArchiver archivedDataWithRootObject:session];
614+
realmOlmSession.olmSessionData = [NSKeyedArchiver archivedDataWithRootObject:session.session];
615615
}
616616
else
617617
{
618618
// Create it
619619
realmOlmSession = [[MXRealmOlmSession alloc] initWithValue:@{
620-
@"sessionId": session.sessionIdentifier,
620+
@"sessionId": session.session.sessionIdentifier,
621621
@"deviceKey": deviceKey,
622-
@"olmSessionData": [NSKeyedArchiver archivedDataWithRootObject:session]
622+
@"olmSessionData": [NSKeyedArchiver archivedDataWithRootObject:session.session]
623623
}];
624624
[realm addObject:realmOlmSession];
625625
}
@@ -628,17 +628,20 @@ - (void)storeSession:(OLMSession*)session forDevice:(NSString*)deviceKey
628628
NSLog(@"[MXRealmCryptoStore] storeSession (%@) in %.0fms", (isNew?@"NEW":@"UPDATE"), [[NSDate date] timeIntervalSinceDate:startDate] * 1000);
629629
}
630630

631-
- (OLMSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)sessionId
631+
- (MXOlmSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)sessionId
632632
{
633633
MXRealmOlmSession *realmOlmSession = [MXRealmOlmSession objectsInRealm:self.realm
634634
where:@"sessionId = %@ AND deviceKey = %@", sessionId, deviceKey].firstObject;
635-
636-
return [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];
635+
636+
OLMSession *olmSession = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];
637+
MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
638+
639+
return mxOlmSession;
637640
}
638641

639-
- (NSArray<OLMSession*>*)sessionsWithDevice:(NSString*)deviceKey;
642+
- (NSArray<MXOlmSession*>*)sessionsWithDevice:(NSString*)deviceKey;
640643
{
641-
NSMutableArray<OLMSession*> *sessionsWithDevice;
644+
NSMutableArray<MXOlmSession*> *sessionsWithDevice;
642645

643646
RLMResults<MXRealmOlmSession *> *realmOlmSessions = [MXRealmOlmSession objectsInRealm:self.realm where:@"deviceKey = %@", deviceKey];
644647
for (MXRealmOlmSession *realmOlmSession in realmOlmSessions)
@@ -648,7 +651,10 @@ - (OLMSession*)sessionWithDevice:(NSString*)deviceKey andSessionId:(NSString*)se
648651
sessionsWithDevice = [NSMutableArray array];
649652
}
650653

651-
[sessionsWithDevice addObject:[NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData]];
654+
OLMSession *olmSession = [NSKeyedUnarchiver unarchiveObjectWithData:realmOlmSession.olmSessionData];
655+
MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
656+
657+
[sessionsWithDevice addObject:mxOlmSession];
652658
}
653659

654660
return sessionsWithDevice;

MatrixSDK/Crypto/MXOlmDevice.m

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ - (NSString *)createOutboundSession:(NSString *)theirIdentityKey theirOneTimeKey
148148

149149
if (olmSession)
150150
{
151-
[store storeSession:olmSession forDevice:theirIdentityKey];
151+
MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
152+
[store storeSession:mxOlmSession forDevice:theirIdentityKey];
152153
return olmSession.sessionIdentifier;
153154
}
154155
else if (error)
@@ -184,7 +185,8 @@ - (NSString*)createInboundSession:(NSString*)theirDeviceIdentityKey messageType:
184185
NSLog(@"[MXOlmDevice] createInboundSession. decryptMessage error: %@", error);
185186
}
186187

187-
[store storeSession:olmSession forDevice:theirDeviceIdentityKey];
188+
MXOlmSession *mxOlmSession = [[MXOlmSession alloc] initWithOlmSession:olmSession];
189+
[store storeSession:mxOlmSession forDevice:theirDeviceIdentityKey];
188190

189191
return olmSession.sessionIdentifier;
190192
}
@@ -198,12 +200,12 @@ - (NSString*)createInboundSession:(NSString*)theirDeviceIdentityKey messageType:
198200

199201
- (NSArray<NSString *> *)sessionIdsForDevice:(NSString *)theirDeviceIdentityKey
200202
{
201-
NSArray<OLMSession*> *sessions = [store sessionsWithDevice:theirDeviceIdentityKey];
203+
NSArray<MXOlmSession*> *sessions = [store sessionsWithDevice:theirDeviceIdentityKey];
202204

203205
NSMutableArray *sessionIds = [NSMutableArray arrayWithCapacity:sessions.count];
204-
for (OLMSession *session in sessions)
206+
for (MXOlmSession *session in sessions)
205207
{
206-
[sessionIds addObject:session.sessionIdentifier];
208+
[sessionIds addObject:session.session.sessionIdentifier];
207209
}
208210

209211
return sessionIds;
@@ -229,21 +231,21 @@ - (NSDictionary *)encryptMessage:(NSString *)theirDeviceIdentityKey sessionId:(N
229231
NSError *error;
230232
OLMMessage *olmMessage;
231233

232-
OLMSession *olmSession = [self sessionForDevice:theirDeviceIdentityKey andSessionId:sessionId];
234+
MXOlmSession *mxOlmSession = [self sessionForDevice:theirDeviceIdentityKey andSessionId:sessionId];
233235

234236
// NSLog(@">>>> encryptMessage: olmSession.sessionIdentifier: %@", olmSession.sessionIdentifier);
235237
// NSLog(@">>>> payloadString: %@", payloadString);
236238

237-
if (olmSession)
239+
if (mxOlmSession.session)
238240
{
239-
olmMessage = [olmSession encryptMessage:payloadString error:&error];
241+
olmMessage = [mxOlmSession.session encryptMessage:payloadString error:&error];
240242

241243
if (error)
242244
{
243245
NSLog(@"[MXOlmDevice] encryptMessage failed: %@", error);
244246
}
245247

246-
[store storeSession:olmSession forDevice:theirDeviceIdentityKey];
248+
[store storeSession:mxOlmSession forDevice:theirDeviceIdentityKey];
247249
}
248250

249251
//NSLog(@">>>> ciphertext: %@", olmMessage.ciphertext);
@@ -260,17 +262,17 @@ - (NSString*)decryptMessage:(NSString*)ciphertext withType:(NSUInteger)messageTy
260262
NSError *error;
261263
NSString *payloadString;
262264

263-
OLMSession *olmSession = [self sessionForDevice:theirDeviceIdentityKey andSessionId:sessionId];
264-
if (olmSession)
265+
MXOlmSession *mxOlmSession = [self sessionForDevice:theirDeviceIdentityKey andSessionId:sessionId];
266+
if (mxOlmSession)
265267
{
266-
payloadString = [olmSession decryptMessage:[[OLMMessage alloc] initWithCiphertext:ciphertext type:messageType] error:&error];
268+
payloadString = [mxOlmSession.session decryptMessage:[[OLMMessage alloc] initWithCiphertext:ciphertext type:messageType] error:&error];
267269

268270
if (error)
269271
{
270272
NSLog(@"[MXOlmDevice] decryptMessage failed: %@", error);
271273
}
272274

273-
[store storeSession:olmSession forDevice:theirDeviceIdentityKey];
275+
[store storeSession:mxOlmSession forDevice:theirDeviceIdentityKey];
274276
}
275277

276278
return payloadString;
@@ -283,8 +285,8 @@ - (BOOL)matchesSession:(NSString *)theirDeviceIdentityKey sessionId:(NSString *)
283285
return NO;
284286
}
285287

286-
OLMSession *olmSession = [self sessionForDevice:theirDeviceIdentityKey andSessionId:sessionId];
287-
return [olmSession matchesInboundSession:ciphertext];
288+
MXOlmSession *mxOlmSession = [self sessionForDevice:theirDeviceIdentityKey andSessionId:sessionId];
289+
return [mxOlmSession.session matchesInboundSession:ciphertext];
288290
}
289291

290292

@@ -565,7 +567,7 @@ - (NSString *)sha256:(NSString *)message
565567

566568

567569
#pragma mark - Private methods
568-
- (OLMSession*)sessionForDevice:(NSString *)theirDeviceIdentityKey andSessionId:(NSString*)sessionId
570+
- (MXOlmSession*)sessionForDevice:(NSString *)theirDeviceIdentityKey andSessionId:(NSString*)sessionId
569571
{
570572
return [store sessionWithDevice:theirDeviceIdentityKey andSessionId:sessionId];
571573
}

0 commit comments

Comments
 (0)