Skip to content

Commit 7864fdf

Browse files
authored
Merge pull request #1447 from matrix-org/andy/4947_sharing_keys
Share Megolm session keys when inviting a new user
2 parents 817b9b9 + 51503c0 commit 7864fdf

File tree

11 files changed

+522
-38
lines changed

11 files changed

+522
-38
lines changed

MatrixSDK.xcodeproj/project.pbxproj

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,12 @@
17671767
ED35652D281150310002BF6A /* MXOlmInboundGroupSessionUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED35652B281150310002BF6A /* MXOlmInboundGroupSessionUnitTests.swift */; };
17681768
ED35652F281153480002BF6A /* MXMegolmSessionDataUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED35652E281153480002BF6A /* MXMegolmSessionDataUnitTests.swift */; };
17691769
ED356530281153480002BF6A /* MXMegolmSessionDataUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED35652E281153480002BF6A /* MXMegolmSessionDataUnitTests.swift */; };
1770+
ED44F01128180BCC00452A5D /* MXSharedHistoryKeyRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED44F01028180BCC00452A5D /* MXSharedHistoryKeyRequest.swift */; };
1771+
ED44F01228180BCC00452A5D /* MXSharedHistoryKeyRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED44F01028180BCC00452A5D /* MXSharedHistoryKeyRequest.swift */; };
1772+
ED44F01428180EAB00452A5D /* MXSharedHistoryKeyManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED44F01328180EAB00452A5D /* MXSharedHistoryKeyManager.swift */; };
1773+
ED44F01528180EAB00452A5D /* MXSharedHistoryKeyManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED44F01328180EAB00452A5D /* MXSharedHistoryKeyManager.swift */; };
1774+
ED44F01A28180F4000452A5D /* MXSharedHistoryKeyManagerUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED44F01728180F1C00452A5D /* MXSharedHistoryKeyManagerUnitTests.swift */; };
1775+
ED44F01B28180F4000452A5D /* MXSharedHistoryKeyManagerUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED44F01728180F1C00452A5D /* MXSharedHistoryKeyManagerUnitTests.swift */; };
17701776
ED5AE8C52816C8CF00105072 /* MXCoreDataRoomSummaryStore.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = ED5AE8C22816C8CF00105072 /* MXCoreDataRoomSummaryStore.xcdatamodeld */; };
17711777
ED5AE8C62816C8CF00105072 /* MXCoreDataRoomSummaryStore.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = ED5AE8C22816C8CF00105072 /* MXCoreDataRoomSummaryStore.xcdatamodeld */; };
17721778
ED88999127F2065D00718486 /* MXRoomAliasResolution.h in Headers */ = {isa = PBXBuildFile; fileRef = ED88998F27F2065C00718486 /* MXRoomAliasResolution.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -2768,6 +2774,9 @@
27682774
ED2F344856EFFCA383E37B22 /* Pods-SDK-MatrixSDK.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDK-MatrixSDK.release.xcconfig"; path = "Target Support Files/Pods-SDK-MatrixSDK/Pods-SDK-MatrixSDK.release.xcconfig"; sourceTree = "<group>"; };
27692775
ED35652B281150310002BF6A /* MXOlmInboundGroupSessionUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXOlmInboundGroupSessionUnitTests.swift; sourceTree = "<group>"; };
27702776
ED35652E281153480002BF6A /* MXMegolmSessionDataUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXMegolmSessionDataUnitTests.swift; sourceTree = "<group>"; };
2777+
ED44F01028180BCC00452A5D /* MXSharedHistoryKeyRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXSharedHistoryKeyRequest.swift; sourceTree = "<group>"; };
2778+
ED44F01328180EAB00452A5D /* MXSharedHistoryKeyManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXSharedHistoryKeyManager.swift; sourceTree = "<group>"; };
2779+
ED44F01728180F1C00452A5D /* MXSharedHistoryKeyManagerUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXSharedHistoryKeyManagerUnitTests.swift; sourceTree = "<group>"; };
27712780
ED5AE8C32816C8CF00105072 /* MXRoomSummaryCoreDataStore2.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MXRoomSummaryCoreDataStore2.xcdatamodel; sourceTree = "<group>"; };
27722781
ED5AE8C42816C8CF00105072 /* MXRoomSummaryCoreDataStore.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MXRoomSummaryCoreDataStore.xcdatamodel; sourceTree = "<group>"; };
27732782
ED88998F27F2065C00718486 /* MXRoomAliasResolution.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXRoomAliasResolution.h; sourceTree = "<group>"; };
@@ -4113,6 +4122,7 @@
41134122
32FA10C01FA1C9EE00E54233 /* MXOutgoingRoomKeyRequestManager.m */,
41144123
32A30B161FB4813400C8309E /* MXIncomingRoomKeyRequestManager.h */,
41154124
32A30B171FB4813400C8309E /* MXIncomingRoomKeyRequestManager.m */,
4125+
ED44F01328180EAB00452A5D /* MXSharedHistoryKeyManager.swift */,
41164126
);
41174127
path = KeySharing;
41184128
sourceTree = "<group>";
@@ -4126,6 +4136,7 @@
41264136
32F945F11FAB83D800622468 /* MXIncomingRoomKeyRequestCancellation.m */,
41274137
32FA10C81FA1C9F700E54233 /* MXOutgoingRoomKeyRequest.h */,
41284138
32FA10C91FA1C9F700E54233 /* MXOutgoingRoomKeyRequest.m */,
4139+
ED44F01028180BCC00452A5D /* MXSharedHistoryKeyRequest.swift */,
41294140
);
41304141
path = Data;
41314142
sourceTree = "<group>";
@@ -4873,6 +4884,7 @@
48734884
ED21F67A28104B9A002FF83D /* Crypto */ = {
48744885
isa = PBXGroup;
48754886
children = (
4887+
ED44F01628180F1300452A5D /* KeySharing */,
48764888
ED35652A281150230002BF6A /* Data */,
48774889
ED21F67B28104BA1002FF83D /* Algorithms */,
48784890
);
@@ -4904,6 +4916,14 @@
49044916
path = Data;
49054917
sourceTree = "<group>";
49064918
};
4919+
ED44F01628180F1300452A5D /* KeySharing */ = {
4920+
isa = PBXGroup;
4921+
children = (
4922+
ED44F01728180F1C00452A5D /* MXSharedHistoryKeyManagerUnitTests.swift */,
4923+
);
4924+
path = KeySharing;
4925+
sourceTree = "<group>";
4926+
};
49074927
ED8943D127E3474A000FC39C /* Store */ = {
49084928
isa = PBXGroup;
49094929
children = (
@@ -6031,7 +6051,9 @@
60316051
8EC511062568216B00EC4E5B /* MXTaggedEventInfo.m in Sources */,
60326052
3AC135DB2640335100EE1E74 /* MXDehydrationService.m in Sources */,
60336053
32792BDD2296B90A00F4FC9D /* MXAggregatedEditsUpdater.m in Sources */,
6054+
ED44F01428180EAB00452A5D /* MXSharedHistoryKeyManager.swift in Sources */,
60346055
3259CD541DF860C300186944 /* MXRealmCryptoStore.m in Sources */,
6056+
ED44F01128180BCC00452A5D /* MXSharedHistoryKeyRequest.swift in Sources */,
60356057
EC60EDAA265CFE3B00B39A4E /* MXRoomSyncTimeline.m in Sources */,
60366058
EC0B9438271DB0D600B4D440 /* MXMemoryRoomSummaryStore.m in Sources */,
60376059
EC8A53E225B1BCC6004E0802 /* MXThirdPartyUserInstance.m in Sources */,
@@ -6486,6 +6508,7 @@
64866508
32FCAB4D19E578860049C555 /* MXRestClientTests.m in Sources */,
64876509
32C78BA7256D227D008130B1 /* MXCryptoMigrationTests.m in Sources */,
64886510
ED21F68528104DA2002FF83D /* MXMegolmEncryptionTests.swift in Sources */,
6511+
ED44F01A28180F4000452A5D /* MXSharedHistoryKeyManagerUnitTests.swift in Sources */,
64896512
322985CB26FAF898001890BC /* MXSession.swift in Sources */,
64906513
EC131B192779D8D500712964 /* MXThreadEventTimelineUnitTests.swift in Sources */,
64916514
B135067427EB201E00BD3276 /* MXLocationServiceTests.swift in Sources */,
@@ -6574,7 +6597,9 @@
65746597
B14EF1E82397E90400758AF0 /* MXRoomPowerLevels.m in Sources */,
65756598
32EEA85E260401490041425B /* MXSummable.swift in Sources */,
65766599
B14EF1E92397E90400758AF0 /* MXRealmMediaScanMapper.m in Sources */,
6600+
ED44F01528180EAB00452A5D /* MXSharedHistoryKeyManager.swift in Sources */,
65776601
EC8A53E725B1BCC6004E0802 /* MXThirdPartyProtocol.m in Sources */,
6602+
ED44F01228180BCC00452A5D /* MXSharedHistoryKeyRequest.swift in Sources */,
65786603
B19A30D724042F2700FB6F35 /* MXSelfVerifyingMasterKeyNotTrustedQRCodeData.m in Sources */,
65796604
EC0B9439271DB0D600B4D440 /* MXMemoryRoomSummaryStore.m in Sources */,
65806605
B14EF1EA2397E90400758AF0 /* MXRealmMediaScan.m in Sources */,
@@ -7029,6 +7054,7 @@
70297054
EC116598270FCA8B0089FA56 /* MXBackgroundTaskUnitTests.swift in Sources */,
70307055
B1E09A322397FD750057C069 /* MXRoomTests.m in Sources */,
70317056
ED21F68628104DA2002FF83D /* MXMegolmEncryptionTests.swift in Sources */,
7057+
ED44F01B28180F4000452A5D /* MXSharedHistoryKeyManagerUnitTests.swift in Sources */,
70327058
322985CC26FAF898001890BC /* MXSession.swift in Sources */,
70337059
EC131B1A2779D8D500712964 /* MXThreadEventTimelineUnitTests.swift in Sources */,
70347060
B135067527EB201E00BD3276 /* MXLocationServiceTests.swift in Sources */,

MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmDecryption.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
#import "MXDecrypting.h"
2424

25-
@interface MXMegolmDecryption : NSObject <MXDecrypting>
25+
@protocol MXSharedHistoryKeyService;
26+
27+
@interface MXMegolmDecryption : NSObject <MXDecrypting, MXSharedHistoryKeyService>
2628

2729
@end
2830

MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmDecryption.m

Lines changed: 89 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -328,50 +328,80 @@ - (MXHTTPOperation*)shareKeysWithDevice:(MXIncomingRoomKeyRequest*)keyRequest
328328
NSString *deviceId = keyRequest.deviceId;
329329
MXDeviceInfo *deviceInfo = [crypto.deviceList storedDevice:userId deviceId:deviceId];
330330
NSDictionary *body = keyRequest.requestBody;
331+
NSString *roomId, *senderKey, *sessionId;
332+
MXJSONModelSetString(roomId, body[@"room_id"]);
333+
MXJSONModelSetString(senderKey, body[@"sender_key"]);
334+
MXJSONModelSetString(sessionId, body[@"session_id"]);
335+
336+
return [self shareKeysWitUserId:userId
337+
devices:@[deviceInfo]
338+
forceEnsureOlmSessions:NO
339+
roomId:roomId
340+
sessionId:sessionId
341+
senderKey:senderKey
342+
success:success
343+
failure:failure];
344+
}
345+
346+
#pragma mark - Private methods
331347

348+
- (MXHTTPOperation *)shareKeysWitUserId:(NSString *)userId
349+
devices:(NSArray <MXDeviceInfo *> *)devices
350+
forceEnsureOlmSessions:(BOOL)forceEnsureOlmSessions
351+
roomId:(NSString *)roomId
352+
sessionId:(NSString *)sessionId
353+
senderKey:(NSString *)senderKey
354+
success:(void (^)(void))success
355+
failure:(void (^)(NSError *error))failure
356+
{
332357
MXHTTPOperation *operation;
333358
MXWeakify(self);
334359
operation = [crypto ensureOlmSessionsForDevices:@{
335-
userId: @[deviceInfo]
360+
userId: devices
336361
}
337-
force:NO
362+
force:forceEnsureOlmSessions
338363
success:^(MXUsersDevicesMap<MXOlmSessionResult *> *results)
339364
{
340-
MXStrongifyAndReturnIfNil(self);
341-
342-
MXOlmSessionResult *olmSessionResult = [results objectForDevice:deviceId forUser:userId];
343-
if (!olmSessionResult.sessionId)
344-
{
345-
// no session with this device, probably because there
346-
// were no one-time keys.
347-
//
348-
// ensureOlmSessionsForUsers has already done the logging,
349-
// so just skip it.
350-
if (success)
351-
{
352-
success();
353-
}
354-
return;
355-
}
356-
357-
NSString *roomId, *senderKey, *sessionId;
358-
MXJSONModelSetString(roomId, body[@"room_id"]);
359-
MXJSONModelSetString(senderKey, body[@"sender_key"]);
360-
MXJSONModelSetString(sessionId, body[@"session_id"]);
361-
362-
MXLogDebug(@"[MXMegolmDecryption] shareKeysWithDevice: sharing keys for session %@|%@ with device %@:%@", senderKey, sessionId, userId, deviceId);
363-
364-
NSDictionary *payload = [self->crypto buildMegolmKeyForwardingMessage:roomId senderKey:senderKey sessionId:sessionId chainIndex:nil];
365-
366-
MXDeviceInfo *deviceInfo = olmSessionResult.device;
367-
368-
MXUsersDevicesMap<NSDictionary*> *contentMap = [[MXUsersDevicesMap alloc] init];
369-
[contentMap setObject:[self->crypto encryptMessage:payload forDevices:@[deviceInfo]]
370-
forUser:userId andDevice:deviceId];
371-
372-
MXHTTPOperation *operation2 = [self->crypto.matrixRestClient sendToDevice:kMXEventTypeStringRoomEncrypted contentMap:contentMap txnId:nil success:success failure:failure];
373-
[operation mutateTo:operation2];
365+
MXStrongifyAndReturnIfNil(self);
366+
NSDictionary *payload = [self->crypto buildMegolmKeyForwardingMessage:roomId
367+
senderKey:senderKey
368+
sessionId:sessionId
369+
chainIndex:nil];
370+
371+
MXUsersDevicesMap<NSDictionary*> *contentMap = [[MXUsersDevicesMap alloc] init];
372+
for (MXDeviceInfo *deviceInfo in devices)
373+
{
374+
MXOlmSessionResult *olmSessionResult = [results objectForDevice:deviceInfo.deviceId forUser:userId];
375+
if (olmSessionResult.sessionId)
376+
{
377+
NSDictionary *message = [self->crypto encryptMessage:payload forDevices:@[deviceInfo]];
378+
[contentMap setObject:message forUser:userId andDevice:deviceInfo.deviceId];
379+
}
380+
else
381+
{
382+
MXLogDebug(@"[MXMegolmDecryption] No session with device %@, cannot share keys", deviceInfo.deviceId);
383+
}
384+
}
385+
386+
if (contentMap.count == 0)
387+
{
388+
MXLogDebug(@"[MXMegolmDecryption] No devices available for user %@, cannot share keys", userId);
389+
if (success)
390+
{
391+
success();
392+
}
393+
return;
394+
}
395+
396+
MXLogDebug(@"[MXMegolmDecryption] shareKeysWithDevices: sharing keys for session %@|%@ with devices of user %@", senderKey, sessionId, userId);
374397

398+
MXHTTPOperation *operation2 = [self->crypto.matrixRestClient sendToDevice:kMXEventTypeStringRoomEncrypted
399+
contentMap:contentMap
400+
txnId:nil
401+
success:success
402+
failure:failure];
403+
[operation mutateTo:operation2];
404+
375405
} failure:failure];
376406

377407
return operation;
@@ -498,6 +528,29 @@ - (void)requestKeysForEvent:(MXEvent*)event
498528
}
499529
}
500530

531+
#pragma mark - MXSharedHistoryKeyStore
532+
533+
- (BOOL)hasSharedHistoryWithSessionId:(NSString *)sessionId senderKey:(NSString *)senderKey
534+
{
535+
MXOlmInboundGroupSession *session = [crypto.store inboundGroupSessionWithId:sessionId
536+
andSenderKey:senderKey];
537+
return session.sharedHistory;
538+
}
539+
540+
- (void)shareKeysWithRequest:(MXSharedHistoryKeyRequest *)request
541+
success:(void (^)(void))success
542+
failure:(void (^)(NSError *error))failure
543+
{
544+
[self shareKeysWitUserId:request.userId
545+
devices:request.devices
546+
forceEnsureOlmSessions:YES
547+
roomId:request.roomId
548+
sessionId:request.sessionId
549+
senderKey:request.senderKey
550+
success:success
551+
failure:failure];
552+
}
553+
501554
@end
502555

503556
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright 2022 The Matrix.org Foundation C.I.C
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+
/// Request to share the key for a given `sessionId` and `senderKey`
18+
/// with selected user and all of their devices
19+
@objcMembers
20+
public class MXSharedHistoryKeyRequest: NSObject {
21+
public let userId: String
22+
public let devices: [MXDeviceInfo]
23+
public let roomId: String
24+
public let sessionId: String
25+
public let senderKey: String
26+
27+
public init(
28+
userId: String,
29+
devices: [MXDeviceInfo],
30+
roomId: String,
31+
sessionId: String,
32+
senderKey: String
33+
) {
34+
self.userId = userId
35+
self.devices = devices
36+
self.roomId = roomId
37+
self.sessionId = sessionId
38+
self.senderKey = senderKey
39+
}
40+
}

0 commit comments

Comments
 (0)