Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,10 @@
EDB4209627DF822B0036AF39 /* MXEventsByTypesEnumeratorOnArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDB4209427DF822B0036AF39 /* MXEventsByTypesEnumeratorOnArrayTests.swift */; };
EDB4209927DF842F0036AF39 /* MXEventFixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDB4209827DF842F0036AF39 /* MXEventFixtures.swift */; };
EDB4209A27DF842F0036AF39 /* MXEventFixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDB4209827DF842F0036AF39 /* MXEventFixtures.swift */; };
EDBCF336281A8ABD00ED5044 /* MXSharedHistoryKeyService.h in Headers */ = {isa = PBXBuildFile; fileRef = EDBCF335281A8AB900ED5044 /* MXSharedHistoryKeyService.h */; settings = {ATTRIBUTES = (Public, ); }; };
EDBCF337281A8ABE00ED5044 /* MXSharedHistoryKeyService.h in Headers */ = {isa = PBXBuildFile; fileRef = EDBCF335281A8AB900ED5044 /* MXSharedHistoryKeyService.h */; settings = {ATTRIBUTES = (Public, ); }; };
EDBCF339281A8D3D00ED5044 /* MXSharedHistoryKeyService.m in Sources */ = {isa = PBXBuildFile; fileRef = EDBCF338281A8D3D00ED5044 /* MXSharedHistoryKeyService.m */; };
EDBCF33A281A8D3D00ED5044 /* MXSharedHistoryKeyService.m in Sources */ = {isa = PBXBuildFile; fileRef = EDBCF338281A8D3D00ED5044 /* MXSharedHistoryKeyService.m */; };
EDF4678727E3331D00435913 /* EventsEnumeratorDataSourceStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDF4678627E3331D00435913 /* EventsEnumeratorDataSourceStub.swift */; };
EDF4678827E3331D00435913 /* EventsEnumeratorDataSourceStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDF4678627E3331D00435913 /* EventsEnumeratorDataSourceStub.swift */; };
F0173EAC1FCF0E8900B5F6A3 /* MXGroup.h in Headers */ = {isa = PBXBuildFile; fileRef = F0173EAA1FCF0E8800B5F6A3 /* MXGroup.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -2785,6 +2789,8 @@
EDB4209027DF77310036AF39 /* MXEventsEnumeratorOnArrayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXEventsEnumeratorOnArrayTests.swift; sourceTree = "<group>"; };
EDB4209427DF822B0036AF39 /* MXEventsByTypesEnumeratorOnArrayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXEventsByTypesEnumeratorOnArrayTests.swift; sourceTree = "<group>"; };
EDB4209827DF842F0036AF39 /* MXEventFixtures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXEventFixtures.swift; sourceTree = "<group>"; };
EDBCF335281A8AB900ED5044 /* MXSharedHistoryKeyService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXSharedHistoryKeyService.h; sourceTree = "<group>"; };
EDBCF338281A8D3D00ED5044 /* MXSharedHistoryKeyService.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXSharedHistoryKeyService.m; sourceTree = "<group>"; };
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>"; };
EDF4678627E3331D00435913 /* EventsEnumeratorDataSourceStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventsEnumeratorDataSourceStub.swift; sourceTree = "<group>"; };
F0173EAA1FCF0E8800B5F6A3 /* MXGroup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXGroup.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4123,6 +4129,8 @@
32A30B161FB4813400C8309E /* MXIncomingRoomKeyRequestManager.h */,
32A30B171FB4813400C8309E /* MXIncomingRoomKeyRequestManager.m */,
ED44F01328180EAB00452A5D /* MXSharedHistoryKeyManager.swift */,
EDBCF335281A8AB900ED5044 /* MXSharedHistoryKeyService.h */,
EDBCF338281A8D3D00ED5044 /* MXSharedHistoryKeyService.m */,
);
path = KeySharing;
sourceTree = "<group>";
Expand Down Expand Up @@ -5171,6 +5179,7 @@
324DD2A6246AE81300377005 /* MXSecretStorageKeyContent.h in Headers */,
EC60ED8F265CFD3B00B39A4E /* MXRoomSync.h in Headers */,
ECD2899E26EB570B00F268CF /* MXRoomSummaryStore.h in Headers */,
EDBCF336281A8ABD00ED5044 /* MXSharedHistoryKeyService.h in Headers */,
EC8A53C325B1BC77004E0802 /* MXCallInviteEventContent.h in Headers */,
3281E8B919E42DFE00976E1A /* MXJSONModels.h in Headers */,
3A108AA225810FE5005EEBE9 /* MXRawDataKey.h in Headers */,
Expand Down Expand Up @@ -5675,6 +5684,7 @@
B14EF3432397E90400758AF0 /* MXRoomEventTimeline.h in Headers */,
B14EF3442397E90400758AF0 /* NSArray+MatrixSDK.h in Headers */,
B165B81225C3307E003CF7F7 /* MXLoginSSOIdentityProviderBrand.h in Headers */,
EDBCF337281A8ABE00ED5044 /* MXSharedHistoryKeyService.h in Headers */,
324DD2C6246E638B00377005 /* MXAesHmacSha2.h in Headers */,
B14EF3452397E90400758AF0 /* MXReplyEventParser.h in Headers */,
323F878E25553D84009E9E67 /* MXTaskProfile.h in Headers */,
Expand Down Expand Up @@ -6083,6 +6093,7 @@
66836AB727CFA17200515780 /* MXEventStreamService.swift in Sources */,
B11BD44922CB56790064D8B0 /* MXReplyEventParser.m in Sources */,
EC0B941127184E8A00B4D440 /* MXRoomSummaryMO.swift in Sources */,
EDBCF339281A8D3D00ED5044 /* MXSharedHistoryKeyService.m in Sources */,
EC0B941327184E8A00B4D440 /* MXRoomMembersCountMO.swift in Sources */,
323360701A403A0D0071A488 /* MXFileStore.m in Sources */,
B1136967230C1E8600E2B2FA /* MXIdentityService.swift in Sources */,
Expand Down Expand Up @@ -6629,6 +6640,7 @@
66836AB827CFA17200515780 /* MXEventStreamService.swift in Sources */,
3A59A4A025A7A16F00DDA1FC /* MXOlmOutboundGroupSession.m in Sources */,
EC0B941227184E8A00B4D440 /* MXRoomSummaryMO.swift in Sources */,
EDBCF33A281A8D3D00ED5044 /* MXSharedHistoryKeyService.m in Sources */,
EC0B941427184E8A00B4D440 /* MXRoomMembersCountMO.swift in Sources */,
B14EF1F92397E90400758AF0 /* MXReactionRelation.m in Sources */,
B19A30BB2404268600FB6F35 /* MXQRCodeData.m in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion MatrixSDK/Background/MXBackgroundSyncService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,15 @@ public enum MXBackgroundSyncServiceError: Error {
return
}

let sharedHistory = (content[kMXSharedHistoryKeyName] as? Bool) ?? isRoomSharingHistory(roomId: roomId)
olmDevice.addInboundGroupSession(sessionId,
sessionKey: sessionKey,
roomId: roomId,
senderKey: senderKey,
forwardingCurve25519KeyChain: forwardingKeyChain,
keysClaimed: keysClaimed,
exportFormat: exportFormat,
sharedHistory: isRoomSharingHistory(roomId: roomId))
sharedHistory: sharedHistory)
}

private func isRoomSharingHistory(roomId: String) -> Bool {
Expand Down
3 changes: 1 addition & 2 deletions MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmDecryption.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#ifdef MX_CRYPTO

#import "MXDecrypting.h"

@protocol MXSharedHistoryKeyService;
#import "MXSharedHistoryKeyService.h"

@interface MXMegolmDecryption : NSObject <MXDecrypting, MXSharedHistoryKeyService>

Expand Down
19 changes: 12 additions & 7 deletions MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmDecryption.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import "MXCrypto_Private.h"
#import "MXTools.h"
#import "MatrixSDKSwiftHeader.h"
#import "MXSharedHistoryKeyService.h"

@interface MXMegolmDecryption ()
{
Expand Down Expand Up @@ -212,11 +213,14 @@ - (void)onRoomKeyEvent:(MXEvent *)event
NSArray<NSString*> *forwardingKeyChain;
BOOL exportFormat = NO;
NSDictionary *keysClaimed;
BOOL sharedHistory = [crypto isRoomSharingHistory:roomId];
if (content[kMXSharedHistoryKeyName] != nil) {
MXJSONModelSetBoolean(sharedHistory, content[kMXSharedHistoryKeyName]);
}

if (event.eventType == MXEventTypeRoomForwardedKey)
{
exportFormat = YES;

MXJSONModelSetArray(forwardingKeyChain, content[@"forwarding_curve25519_key_chain"]);
if (!forwardingKeyChain)
{
Expand Down Expand Up @@ -254,7 +258,6 @@ - (void)onRoomKeyEvent:(MXEvent *)event

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

BOOL sharedHistory = [crypto isRoomSharingHistory:roomId];
[olmDevice addInboundGroupSession:sessionId
sessionKey:sessionKey
roomId:roomId
Expand Down Expand Up @@ -530,16 +533,18 @@ - (void)requestKeysForEvent:(MXEvent*)event

#pragma mark - MXSharedHistoryKeyStore

- (BOOL)hasSharedHistoryWithSessionId:(NSString *)sessionId senderKey:(NSString *)senderKey
- (BOOL)hasSharedHistoryForRoomId:(NSString *)roomId
sessionId:(NSString *)sessionId
senderKey:(NSString *)senderKey
{
MXOlmInboundGroupSession *session = [crypto.store inboundGroupSessionWithId:sessionId
andSenderKey:senderKey];
return session.sharedHistory;
return session.sharedHistory && [session.roomId isEqualToString:roomId];
}

- (void)shareKeysWithRequest:(MXSharedHistoryKeyRequest *)request
success:(void (^)(void))success
failure:(void (^)(NSError *error))failure
- (void)shareKeysForRequest:(MXSharedHistoryKeyRequest *)request
success:(void (^)(void))success
failure:(void (^)(NSError *))failure
{
[self shareKeysWitUserId:request.userId
devices:request.devices
Expand Down
5 changes: 4 additions & 1 deletion MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmEncryption.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#import "MXTools.h"
#import "MXOutboundSessionInfo.h"
#import <OLMKit/OLMKit.h>
#import "MXSharedHistoryKeyService.h"


@interface MXMegolmEncryption ()
Expand Down Expand Up @@ -354,6 +355,7 @@ - (MXHTTPOperation*)shareKey:(MXOutboundSessionInfo*)session
{
NSString *sessionKey = session.session.sessionKey;
NSUInteger chainIndex = session.session.messageIndex;
BOOL sharedHistory = [self isSessionSharingHistory:session];

NSDictionary *payload = @{
@"type": kMXEventTypeStringRoomKey,
Expand All @@ -362,7 +364,8 @@ - (MXHTTPOperation*)shareKey:(MXOutboundSessionInfo*)session
@"room_id": roomId,
@"session_id": session.sessionId,
@"session_key": sessionKey,
@"chain_index": @(chainIndex)
@"chain_index": @(chainIndex),
kMXSharedHistoryKeyName: @(sharedHistory)
}
};

Expand Down
5 changes: 3 additions & 2 deletions MatrixSDK/Crypto/Data/MXMegolmSessionData.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#import "MXMegolmSessionData.h"
#import "MXSharedHistoryKeyService.h"

@implementation MXMegolmSessionData

Expand All @@ -28,7 +29,7 @@ + (id)modelFromJSON:(NSDictionary *)JSONDictionary
MXJSONModelSetString(sessionData.roomId, JSONDictionary[@"room_id"]);
MXJSONModelSetString(sessionData.sessionId, JSONDictionary[@"session_id"]);
MXJSONModelSetString(sessionData.sessionKey, JSONDictionary[@"session_key"]);
MXJSONModelSetBoolean(sessionData.sharedHistory, JSONDictionary[@"shared_history"]);
MXJSONModelSetBoolean(sessionData.sharedHistory, JSONDictionary[kMXSharedHistoryKeyName]);
MXJSONModelSetString(sessionData.algorithm, JSONDictionary[@"algorithm"]);
MXJSONModelSetArray(sessionData.forwardingCurve25519KeyChain, JSONDictionary[@"forwarding_curve25519_key_chain"])
}
Expand All @@ -44,7 +45,7 @@ - (NSDictionary *)JSONDictionary
@"room_id": _roomId,
@"session_id": _sessionId,
@"session_key":_sessionKey,
@"shared_history": @(_sharedHistory),
kMXSharedHistoryKeyName: @(_sharedHistory),
@"algorithm": _algorithm,
@"forwarding_curve25519_key_chain": _forwardingCurve25519KeyChain ? _forwardingCurve25519KeyChain : @[]
};
Expand Down
3 changes: 2 additions & 1 deletion MatrixSDK/Crypto/KeyBackup/MXKeyBackup.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "MXKeyProvider.h"
#import "MXRawDataKey.h"
#import "MXCrossSigning_Private.h"
#import "MXSharedHistoryKeyService.h"

#pragma mark - Constants definitions

Expand Down Expand Up @@ -1612,7 +1613,7 @@ - (MXKeyBackupData*)encryptGroupSession:(MXOlmInboundGroupSession*)session
@"sender_claimed_keys": sessionData.senderClaimedKeys,
@"forwarding_curve25519_key_chain": sessionData.forwardingCurve25519KeyChain ? sessionData.forwardingCurve25519KeyChain : @[],
@"session_key": sessionData.sessionKey,
@"shared_history": @(sessionData.sharedHistory)
kMXSharedHistoryKeyName: @(sessionData.sharedHistory)
};
OLMPkMessage *encryptedSessionBackupData = [_backupKey encryptMessage:[MXTools serialiseJSONObject:sessionBackupData] error:nil];
if (![self checkOLMPkMessage:encryptedSessionBackupData])
Expand Down
24 changes: 8 additions & 16 deletions MatrixSDK/Crypto/KeySharing/MXSharedHistoryKeyManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@

import Foundation

/// Object managing the session keys and responsible for executing key share requests
@objc
public protocol MXSharedHistoryKeyService {
func hasSharedHistory(sessionId: String, senderKey: String) -> Bool
func shareKeys(request: MXSharedHistoryKeyRequest, success: (() -> Void)?, failure: ((NSError?) -> Void)?)
}

/// Manager responsible for sharing keys of messages in a room with an invited user
///
/// The intent of sharing keys with different users on invite is to allow them to see any immediate
Expand All @@ -34,15 +27,16 @@ public protocol MXSharedHistoryKeyService {
@objc
public class MXSharedHistoryKeyManager: NSObject {
struct SessionInfo: Hashable {
let roomId: String
let sessionId: String
let senderKey: String
}

private let roomId: String
private let crypto: MXCrypto
private let service: MXSharedHistoryKeyService

@objc public init(crypto: MXCrypto, service: MXSharedHistoryKeyService) {
@objc public init(roomId: String, crypto: MXCrypto, service: MXSharedHistoryKeyService) {
self.roomId = roomId
self.crypto = crypto
self.service = service
}
Expand Down Expand Up @@ -74,12 +68,12 @@ public class MXSharedHistoryKeyManager: NSObject {
let request = MXSharedHistoryKeyRequest(
userId: userId,
devices: devices,
roomId: session.roomId,
roomId: roomId,
sessionId: session.sessionId,
senderKey: session.senderKey
)

service.shareKeys(request: request) {
service.shareKeys(for: request) {
// Success does not trigger any further action / user notification, so we only log the outcome
MXLog.debug("[MXSharedHistoryRoomKeyRequestManager] Shared key successfully")
} failure: {
Expand All @@ -101,21 +95,19 @@ public class MXSharedHistoryKeyManager: NSObject {
private func sessionInfo(for message: MXEvent) -> SessionInfo? {
let content = message.wireContent
guard
let roomId = message.roomId,
let sessionId = content?["session_id"] as? String,
let senderKey = content?["sender_key"] as? String
else {
MXLog.debug("[MXSharedHistoryRoomKeyRequestManager] Cannot create key request")
return nil
}
guard service.hasSharedHistory(sessionId: sessionId, senderKey: senderKey) else {
MXLog.debug("[MXSharedHistoryRoomKeyRequestManager] Skipping keys for message without shared history")

guard service.hasSharedHistory(forRoomId: roomId, sessionId: sessionId, senderKey: senderKey) else {
MXLog.debug("[MXSharedHistoryRoomKeyRequestManager] Skipping keys for message without shared history or mismatched room identifier")
return nil
}

return .init(
roomId: roomId,
sessionId: sessionId,
senderKey: senderKey
)
Expand Down
48 changes: 48 additions & 0 deletions MatrixSDK/Crypto/KeySharing/MXSharedHistoryKeyService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Copyright 2022 The Matrix.org Foundation C.I.C
//
// 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.
//

#ifndef MXSharedHistoryKeyService_h
#define MXSharedHistoryKeyService_h

/**
Name of the field for `sharedHistory` flag when sharing, exporting or backing up keys
*/
FOUNDATION_EXPORT NSString *const kMXSharedHistoryKeyName;

@class MXSharedHistoryKeyRequest;

/**
Object managing the session keys and responsible for executing key share requests
*/
@protocol MXSharedHistoryKeyService <NSObject>

/**
Check whether key for a given session (sessionId + senderKey) exists
*/
- (BOOL)hasSharedHistoryForRoomId:(NSString *)roomId
sessionId:(NSString *)sessionId
senderKey:(NSString *)senderKey;

/**
Share keys for a given request, containing userId, list of devices and session to share
*/
- (void)shareKeysForRequest:(MXSharedHistoryKeyRequest *)request
success:(void(^)(void))success
failure:(void(^)(NSError *))failure;

@end

#endif /* MXSharedHistoryKeyService_h */
19 changes: 19 additions & 0 deletions MatrixSDK/Crypto/KeySharing/MXSharedHistoryKeyService.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright 2022 The Matrix.org Foundation C.I.C
//
// 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>

NSString *const kMXSharedHistoryKeyName = @"org.matrix.msc3061.shared_history";
4 changes: 3 additions & 1 deletion MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#import "MXDeviceListResponse.h"

#import "MatrixSDKSwiftHeader.h"
#import "MXSharedHistoryKeyService.h"
/**
The store to use for crypto.
*/
Expand Down Expand Up @@ -2506,7 +2507,8 @@ - (NSDictionary*)buildMegolmKeyForwardingMessage:(NSString*)roomId senderKey:(NS
@"session_id": sessionId,
@"session_key": key[@"key"],
@"chain_index": key[@"chain_index"],
@"forwarding_curve25519_key_chain": key[@"forwarding_curve25519_key_chain"]
@"forwarding_curve25519_key_chain": key[@"forwarding_curve25519_key_chain"],
kMXSharedHistoryKeyName: key[@"shared_history"]
}
};
}
Expand Down
4 changes: 3 additions & 1 deletion MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ - (id)initWithRoomId:(NSString *)roomId matrixSession:(MXSession *)mxSession2 an
if (mxSession.crypto)
{
MXMegolmDecryption *decryption = [[MXMegolmDecryption alloc] initWithCrypto:mxSession.crypto];
sharedHistoryKeyManager = [[MXSharedHistoryKeyManager alloc] initWithCrypto:mxSession.crypto service:decryption];
sharedHistoryKeyManager = [[MXSharedHistoryKeyManager alloc] initWithRoomId:roomId
crypto:mxSession.crypto
service:decryption];
}

if (store)
Expand Down
1 change: 1 addition & 0 deletions MatrixSDK/MatrixSDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ FOUNDATION_EXPORT NSString *MatrixSDKVersion;
#import "MXOlmDecryption.h"
#import "MXCachedSyncResponse.h"
#import "MXBackgroundCryptoStore.h"
#import "MXSharedHistoryKeyService.h"

// Sync response models
#import "MXSyncResponse.h"
Expand Down
Loading