Skip to content

Commit a389ff9

Browse files
authored
Merge pull request #1449 from matrix-org/andy/4947_sharing_flags
Change `sharedHistory` flag to unstable variant
2 parents 70d0e4e + c56f331 commit a389ff9

18 files changed

+447
-56
lines changed

MatrixSDK.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,10 @@
17871787
EDB4209627DF822B0036AF39 /* MXEventsByTypesEnumeratorOnArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDB4209427DF822B0036AF39 /* MXEventsByTypesEnumeratorOnArrayTests.swift */; };
17881788
EDB4209927DF842F0036AF39 /* MXEventFixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDB4209827DF842F0036AF39 /* MXEventFixtures.swift */; };
17891789
EDB4209A27DF842F0036AF39 /* MXEventFixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDB4209827DF842F0036AF39 /* MXEventFixtures.swift */; };
1790+
EDBCF336281A8ABD00ED5044 /* MXSharedHistoryKeyService.h in Headers */ = {isa = PBXBuildFile; fileRef = EDBCF335281A8AB900ED5044 /* MXSharedHistoryKeyService.h */; settings = {ATTRIBUTES = (Public, ); }; };
1791+
EDBCF337281A8ABE00ED5044 /* MXSharedHistoryKeyService.h in Headers */ = {isa = PBXBuildFile; fileRef = EDBCF335281A8AB900ED5044 /* MXSharedHistoryKeyService.h */; settings = {ATTRIBUTES = (Public, ); }; };
1792+
EDBCF339281A8D3D00ED5044 /* MXSharedHistoryKeyService.m in Sources */ = {isa = PBXBuildFile; fileRef = EDBCF338281A8D3D00ED5044 /* MXSharedHistoryKeyService.m */; };
1793+
EDBCF33A281A8D3D00ED5044 /* MXSharedHistoryKeyService.m in Sources */ = {isa = PBXBuildFile; fileRef = EDBCF338281A8D3D00ED5044 /* MXSharedHistoryKeyService.m */; };
17901794
EDF4678727E3331D00435913 /* EventsEnumeratorDataSourceStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDF4678627E3331D00435913 /* EventsEnumeratorDataSourceStub.swift */; };
17911795
EDF4678827E3331D00435913 /* EventsEnumeratorDataSourceStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDF4678627E3331D00435913 /* EventsEnumeratorDataSourceStub.swift */; };
17921796
F0173EAC1FCF0E8900B5F6A3 /* MXGroup.h in Headers */ = {isa = PBXBuildFile; fileRef = F0173EAA1FCF0E8800B5F6A3 /* MXGroup.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -2785,6 +2789,8 @@
27852789
EDB4209027DF77310036AF39 /* MXEventsEnumeratorOnArrayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXEventsEnumeratorOnArrayTests.swift; sourceTree = "<group>"; };
27862790
EDB4209427DF822B0036AF39 /* MXEventsByTypesEnumeratorOnArrayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXEventsByTypesEnumeratorOnArrayTests.swift; sourceTree = "<group>"; };
27872791
EDB4209827DF842F0036AF39 /* MXEventFixtures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXEventFixtures.swift; sourceTree = "<group>"; };
2792+
EDBCF335281A8AB900ED5044 /* MXSharedHistoryKeyService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXSharedHistoryKeyService.h; sourceTree = "<group>"; };
2793+
EDBCF338281A8D3D00ED5044 /* MXSharedHistoryKeyService.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXSharedHistoryKeyService.m; sourceTree = "<group>"; };
27882794
EDC74874AB2D86EFEE912B04 /* Pods-MatrixSDK-MatrixSDK-macOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MatrixSDK-MatrixSDK-macOS.debug.xcconfig"; path = "Target Support Files/Pods-MatrixSDK-MatrixSDK-macOS/Pods-MatrixSDK-MatrixSDK-macOS.debug.xcconfig"; sourceTree = "<group>"; };
27892795
EDF4678627E3331D00435913 /* EventsEnumeratorDataSourceStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventsEnumeratorDataSourceStub.swift; sourceTree = "<group>"; };
27902796
F0173EAA1FCF0E8800B5F6A3 /* MXGroup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXGroup.h; sourceTree = "<group>"; };
@@ -4123,6 +4129,8 @@
41234129
32A30B161FB4813400C8309E /* MXIncomingRoomKeyRequestManager.h */,
41244130
32A30B171FB4813400C8309E /* MXIncomingRoomKeyRequestManager.m */,
41254131
ED44F01328180EAB00452A5D /* MXSharedHistoryKeyManager.swift */,
4132+
EDBCF335281A8AB900ED5044 /* MXSharedHistoryKeyService.h */,
4133+
EDBCF338281A8D3D00ED5044 /* MXSharedHistoryKeyService.m */,
41264134
);
41274135
path = KeySharing;
41284136
sourceTree = "<group>";
@@ -5171,6 +5179,7 @@
51715179
324DD2A6246AE81300377005 /* MXSecretStorageKeyContent.h in Headers */,
51725180
EC60ED8F265CFD3B00B39A4E /* MXRoomSync.h in Headers */,
51735181
ECD2899E26EB570B00F268CF /* MXRoomSummaryStore.h in Headers */,
5182+
EDBCF336281A8ABD00ED5044 /* MXSharedHistoryKeyService.h in Headers */,
51745183
EC8A53C325B1BC77004E0802 /* MXCallInviteEventContent.h in Headers */,
51755184
3281E8B919E42DFE00976E1A /* MXJSONModels.h in Headers */,
51765185
3A108AA225810FE5005EEBE9 /* MXRawDataKey.h in Headers */,
@@ -5675,6 +5684,7 @@
56755684
B14EF3432397E90400758AF0 /* MXRoomEventTimeline.h in Headers */,
56765685
B14EF3442397E90400758AF0 /* NSArray+MatrixSDK.h in Headers */,
56775686
B165B81225C3307E003CF7F7 /* MXLoginSSOIdentityProviderBrand.h in Headers */,
5687+
EDBCF337281A8ABE00ED5044 /* MXSharedHistoryKeyService.h in Headers */,
56785688
324DD2C6246E638B00377005 /* MXAesHmacSha2.h in Headers */,
56795689
B14EF3452397E90400758AF0 /* MXReplyEventParser.h in Headers */,
56805690
323F878E25553D84009E9E67 /* MXTaskProfile.h in Headers */,
@@ -6083,6 +6093,7 @@
60836093
66836AB727CFA17200515780 /* MXEventStreamService.swift in Sources */,
60846094
B11BD44922CB56790064D8B0 /* MXReplyEventParser.m in Sources */,
60856095
EC0B941127184E8A00B4D440 /* MXRoomSummaryMO.swift in Sources */,
6096+
EDBCF339281A8D3D00ED5044 /* MXSharedHistoryKeyService.m in Sources */,
60866097
EC0B941327184E8A00B4D440 /* MXRoomMembersCountMO.swift in Sources */,
60876098
323360701A403A0D0071A488 /* MXFileStore.m in Sources */,
60886099
B1136967230C1E8600E2B2FA /* MXIdentityService.swift in Sources */,
@@ -6629,6 +6640,7 @@
66296640
66836AB827CFA17200515780 /* MXEventStreamService.swift in Sources */,
66306641
3A59A4A025A7A16F00DDA1FC /* MXOlmOutboundGroupSession.m in Sources */,
66316642
EC0B941227184E8A00B4D440 /* MXRoomSummaryMO.swift in Sources */,
6643+
EDBCF33A281A8D3D00ED5044 /* MXSharedHistoryKeyService.m in Sources */,
66326644
EC0B941427184E8A00B4D440 /* MXRoomMembersCountMO.swift in Sources */,
66336645
B14EF1F92397E90400758AF0 /* MXReactionRelation.m in Sources */,
66346646
B19A30BB2404268600FB6F35 /* MXQRCodeData.m in Sources */,

MatrixSDK/Background/MXBackgroundSyncService.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,14 +579,15 @@ public enum MXBackgroundSyncServiceError: Error {
579579
return
580580
}
581581

582+
let sharedHistory = (content[kMXSharedHistoryKeyName] as? Bool) ?? isRoomSharingHistory(roomId: roomId)
582583
olmDevice.addInboundGroupSession(sessionId,
583584
sessionKey: sessionKey,
584585
roomId: roomId,
585586
senderKey: senderKey,
586587
forwardingCurve25519KeyChain: forwardingKeyChain,
587588
keysClaimed: keysClaimed,
588589
exportFormat: exportFormat,
589-
sharedHistory: isRoomSharingHistory(roomId: roomId))
590+
sharedHistory: sharedHistory)
590591
}
591592

592593
private func isRoomSharingHistory(roomId: String) -> Bool {

MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmDecryption.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#ifdef MX_CRYPTO
2222

2323
#import "MXDecrypting.h"
24-
25-
@protocol MXSharedHistoryKeyService;
24+
#import "MXSharedHistoryKeyService.h"
2625

2726
@interface MXMegolmDecryption : NSObject <MXDecrypting, MXSharedHistoryKeyService>
2827

MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmDecryption.m

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "MXCrypto_Private.h"
2626
#import "MXTools.h"
2727
#import "MatrixSDKSwiftHeader.h"
28+
#import "MXSharedHistoryKeyService.h"
2829

2930
@interface MXMegolmDecryption ()
3031
{
@@ -212,11 +213,14 @@ - (void)onRoomKeyEvent:(MXEvent *)event
212213
NSArray<NSString*> *forwardingKeyChain;
213214
BOOL exportFormat = NO;
214215
NSDictionary *keysClaimed;
216+
BOOL sharedHistory = [crypto isRoomSharingHistory:roomId];
217+
if (content[kMXSharedHistoryKeyName] != nil) {
218+
MXJSONModelSetBoolean(sharedHistory, content[kMXSharedHistoryKeyName]);
219+
}
215220

216221
if (event.eventType == MXEventTypeRoomForwardedKey)
217222
{
218223
exportFormat = YES;
219-
220224
MXJSONModelSetArray(forwardingKeyChain, content[@"forwarding_curve25519_key_chain"]);
221225
if (!forwardingKeyChain)
222226
{
@@ -254,7 +258,6 @@ - (void)onRoomKeyEvent:(MXEvent *)event
254258

255259
MXLogDebug(@"[MXMegolmDecryption] onRoomKeyEvent: Adding key for megolm session %@|%@ from %@ event", senderKey, sessionId, event.type);
256260

257-
BOOL sharedHistory = [crypto isRoomSharingHistory:roomId];
258261
[olmDevice addInboundGroupSession:sessionId
259262
sessionKey:sessionKey
260263
roomId:roomId
@@ -530,16 +533,18 @@ - (void)requestKeysForEvent:(MXEvent*)event
530533

531534
#pragma mark - MXSharedHistoryKeyStore
532535

533-
- (BOOL)hasSharedHistoryWithSessionId:(NSString *)sessionId senderKey:(NSString *)senderKey
536+
- (BOOL)hasSharedHistoryForRoomId:(NSString *)roomId
537+
sessionId:(NSString *)sessionId
538+
senderKey:(NSString *)senderKey
534539
{
535540
MXOlmInboundGroupSession *session = [crypto.store inboundGroupSessionWithId:sessionId
536541
andSenderKey:senderKey];
537-
return session.sharedHistory;
542+
return session.sharedHistory && [session.roomId isEqualToString:roomId];
538543
}
539544

540-
- (void)shareKeysWithRequest:(MXSharedHistoryKeyRequest *)request
541-
success:(void (^)(void))success
542-
failure:(void (^)(NSError *error))failure
545+
- (void)shareKeysForRequest:(MXSharedHistoryKeyRequest *)request
546+
success:(void (^)(void))success
547+
failure:(void (^)(NSError *))failure
543548
{
544549
[self shareKeysWitUserId:request.userId
545550
devices:request.devices

MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmEncryption.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import "MXTools.h"
2929
#import "MXOutboundSessionInfo.h"
3030
#import <OLMKit/OLMKit.h>
31+
#import "MXSharedHistoryKeyService.h"
3132

3233

3334
@interface MXMegolmEncryption ()
@@ -354,6 +355,7 @@ - (MXHTTPOperation*)shareKey:(MXOutboundSessionInfo*)session
354355
{
355356
NSString *sessionKey = session.session.sessionKey;
356357
NSUInteger chainIndex = session.session.messageIndex;
358+
BOOL sharedHistory = [self isSessionSharingHistory:session];
357359

358360
NSDictionary *payload = @{
359361
@"type": kMXEventTypeStringRoomKey,
@@ -362,7 +364,8 @@ - (MXHTTPOperation*)shareKey:(MXOutboundSessionInfo*)session
362364
@"room_id": roomId,
363365
@"session_id": session.sessionId,
364366
@"session_key": sessionKey,
365-
@"chain_index": @(chainIndex)
367+
@"chain_index": @(chainIndex),
368+
kMXSharedHistoryKeyName: @(sharedHistory)
366369
}
367370
};
368371

MatrixSDK/Crypto/Data/MXMegolmSessionData.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#import "MXMegolmSessionData.h"
18+
#import "MXSharedHistoryKeyService.h"
1819

1920
@implementation MXMegolmSessionData
2021

@@ -28,7 +29,7 @@ + (id)modelFromJSON:(NSDictionary *)JSONDictionary
2829
MXJSONModelSetString(sessionData.roomId, JSONDictionary[@"room_id"]);
2930
MXJSONModelSetString(sessionData.sessionId, JSONDictionary[@"session_id"]);
3031
MXJSONModelSetString(sessionData.sessionKey, JSONDictionary[@"session_key"]);
31-
MXJSONModelSetBoolean(sessionData.sharedHistory, JSONDictionary[@"shared_history"]);
32+
MXJSONModelSetBoolean(sessionData.sharedHistory, JSONDictionary[kMXSharedHistoryKeyName]);
3233
MXJSONModelSetString(sessionData.algorithm, JSONDictionary[@"algorithm"]);
3334
MXJSONModelSetArray(sessionData.forwardingCurve25519KeyChain, JSONDictionary[@"forwarding_curve25519_key_chain"])
3435
}
@@ -44,7 +45,7 @@ - (NSDictionary *)JSONDictionary
4445
@"room_id": _roomId,
4546
@"session_id": _sessionId,
4647
@"session_key":_sessionKey,
47-
@"shared_history": @(_sharedHistory),
48+
kMXSharedHistoryKeyName: @(_sharedHistory),
4849
@"algorithm": _algorithm,
4950
@"forwarding_curve25519_key_chain": _forwardingCurve25519KeyChain ? _forwardingCurve25519KeyChain : @[]
5051
};

MatrixSDK/Crypto/KeyBackup/MXKeyBackup.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#import "MXKeyProvider.h"
3030
#import "MXRawDataKey.h"
3131
#import "MXCrossSigning_Private.h"
32+
#import "MXSharedHistoryKeyService.h"
3233

3334
#pragma mark - Constants definitions
3435

@@ -1612,7 +1613,7 @@ - (MXKeyBackupData*)encryptGroupSession:(MXOlmInboundGroupSession*)session
16121613
@"sender_claimed_keys": sessionData.senderClaimedKeys,
16131614
@"forwarding_curve25519_key_chain": sessionData.forwardingCurve25519KeyChain ? sessionData.forwardingCurve25519KeyChain : @[],
16141615
@"session_key": sessionData.sessionKey,
1615-
@"shared_history": @(sessionData.sharedHistory)
1616+
kMXSharedHistoryKeyName: @(sessionData.sharedHistory)
16161617
};
16171618
OLMPkMessage *encryptedSessionBackupData = [_backupKey encryptMessage:[MXTools serialiseJSONObject:sessionBackupData] error:nil];
16181619
if (![self checkOLMPkMessage:encryptedSessionBackupData])

MatrixSDK/Crypto/KeySharing/MXSharedHistoryKeyManager.swift

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616

1717
import Foundation
1818

19-
/// Object managing the session keys and responsible for executing key share requests
20-
@objc
21-
public protocol MXSharedHistoryKeyService {
22-
func hasSharedHistory(sessionId: String, senderKey: String) -> Bool
23-
func shareKeys(request: MXSharedHistoryKeyRequest, success: (() -> Void)?, failure: ((NSError?) -> Void)?)
24-
}
25-
2619
/// Manager responsible for sharing keys of messages in a room with an invited user
2720
///
2821
/// The intent of sharing keys with different users on invite is to allow them to see any immediate
@@ -34,15 +27,16 @@ public protocol MXSharedHistoryKeyService {
3427
@objc
3528
public class MXSharedHistoryKeyManager: NSObject {
3629
struct SessionInfo: Hashable {
37-
let roomId: String
3830
let sessionId: String
3931
let senderKey: String
4032
}
4133

34+
private let roomId: String
4235
private let crypto: MXCrypto
4336
private let service: MXSharedHistoryKeyService
4437

45-
@objc public init(crypto: MXCrypto, service: MXSharedHistoryKeyService) {
38+
@objc public init(roomId: String, crypto: MXCrypto, service: MXSharedHistoryKeyService) {
39+
self.roomId = roomId
4640
self.crypto = crypto
4741
self.service = service
4842
}
@@ -74,12 +68,12 @@ public class MXSharedHistoryKeyManager: NSObject {
7468
let request = MXSharedHistoryKeyRequest(
7569
userId: userId,
7670
devices: devices,
77-
roomId: session.roomId,
71+
roomId: roomId,
7872
sessionId: session.sessionId,
7973
senderKey: session.senderKey
8074
)
8175

82-
service.shareKeys(request: request) {
76+
service.shareKeys(for: request) {
8377
// Success does not trigger any further action / user notification, so we only log the outcome
8478
MXLog.debug("[MXSharedHistoryRoomKeyRequestManager] Shared key successfully")
8579
} failure: {
@@ -101,21 +95,19 @@ public class MXSharedHistoryKeyManager: NSObject {
10195
private func sessionInfo(for message: MXEvent) -> SessionInfo? {
10296
let content = message.wireContent
10397
guard
104-
let roomId = message.roomId,
10598
let sessionId = content?["session_id"] as? String,
10699
let senderKey = content?["sender_key"] as? String
107100
else {
108101
MXLog.debug("[MXSharedHistoryRoomKeyRequestManager] Cannot create key request")
109102
return nil
110103
}
111-
112-
guard service.hasSharedHistory(sessionId: sessionId, senderKey: senderKey) else {
113-
MXLog.debug("[MXSharedHistoryRoomKeyRequestManager] Skipping keys for message without shared history")
104+
105+
guard service.hasSharedHistory(forRoomId: roomId, sessionId: sessionId, senderKey: senderKey) else {
106+
MXLog.debug("[MXSharedHistoryRoomKeyRequestManager] Skipping keys for message without shared history or mismatched room identifier")
114107
return nil
115108
}
116109

117110
return .init(
118-
roomId: roomId,
119111
sessionId: sessionId,
120112
senderKey: senderKey
121113
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
#ifndef MXSharedHistoryKeyService_h
18+
#define MXSharedHistoryKeyService_h
19+
20+
/**
21+
Name of the field for `sharedHistory` flag when sharing, exporting or backing up keys
22+
*/
23+
FOUNDATION_EXPORT NSString *const kMXSharedHistoryKeyName;
24+
25+
@class MXSharedHistoryKeyRequest;
26+
27+
/**
28+
Object managing the session keys and responsible for executing key share requests
29+
*/
30+
@protocol MXSharedHistoryKeyService <NSObject>
31+
32+
/**
33+
Check whether key for a given session (sessionId + senderKey) exists
34+
*/
35+
- (BOOL)hasSharedHistoryForRoomId:(NSString *)roomId
36+
sessionId:(NSString *)sessionId
37+
senderKey:(NSString *)senderKey;
38+
39+
/**
40+
Share keys for a given request, containing userId, list of devices and session to share
41+
*/
42+
- (void)shareKeysForRequest:(MXSharedHistoryKeyRequest *)request
43+
success:(void(^)(void))success
44+
failure:(void(^)(NSError *))failure;
45+
46+
@end
47+
48+
#endif /* MXSharedHistoryKeyService_h */
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
#import <Foundation/Foundation.h>
18+
19+
NSString *const kMXSharedHistoryKeyName = @"org.matrix.msc3061.shared_history";

0 commit comments

Comments
 (0)