Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negotiate E2E by default for DMs #2946

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 30 additions & 18 deletions Riot/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -3841,33 +3841,45 @@ - (void)createDirectChatWithUserId:(NSString*)userId completion:(void (^)(void))
// Create a new room by inviting the other user only if it is defined and not oneself
NSArray *invite = ((userId && ![mxSession.myUser.userId isEqualToString:userId]) ? @[userId] : nil);

MXRoomCreationParameters *roomCreationParameters = [MXRoomCreationParameters new];
roomCreationParameters.visibility = kMXRoomDirectoryVisibilityPrivate;
roomCreationParameters.inviteArray = invite;
roomCreationParameters.isDirect = (invite.count != 0);
roomCreationParameters.preset = kMXRoomPresetTrustedPrivateChat;

[mxSession createRoomWithParameters:roomCreationParameters success:^(MXRoom *room) {

// Open created room
[self showRoom:room.roomId andEventId:nil withMatrixSession:mxSession];
void (^onFailure)(NSError *) = ^(NSError *error){
NSLog(@"[AppDelegate] Create direct chat failed");
//Alert user
[self showErrorAsAlert:error];

if (completion)
{
completion();
}
};

} failure:^(NSError *error) {

NSLog(@"[AppDelegate] Create direct chat failed");
//Alert user
[self showErrorAsAlert:error];
[mxSession canEnableE2EByDefaultInNewRoomWithUsers:invite success:^(BOOL canEnableE2E) {

MXRoomCreationParameters *roomCreationParameters = [MXRoomCreationParameters new];
roomCreationParameters.visibility = kMXRoomDirectoryVisibilityPrivate;
roomCreationParameters.inviteArray = invite;
roomCreationParameters.isDirect = (invite.count != 0);
roomCreationParameters.preset = kMXRoomPresetTrustedPrivateChat;

if (completion)
if (canEnableE2E)
{
completion();
roomCreationParameters.initialStateEvents = @[
[MXRoomCreationParameters initialStateEventForEncryptionWithAlgorithm:kMXCryptoMegolmAlgorithm
]];
}
}];

[mxSession createRoomWithParameters:roomCreationParameters success:^(MXRoom *room) {

// Open created room
[self showRoom:room.roomId andEventId:nil withMatrixSession:mxSession];

if (completion)
{
completion();
}

} failure:onFailure];

} failure:onFailure];
}
else if (completion)
{
Expand Down
14 changes: 14 additions & 0 deletions Riot/Categories/MXSession+Riot.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@
*/
- (NSUInteger)riot_missedDiscussionsCount;

/**
Decide if E2E must be enabled in a new room with a list users

@param userIds the list of users;

@param success A block object called when the operation succeeds.
@param failure A block object called when the operation fails.

@return a MXHTTPOperation instance.
*/
- (MXHTTPOperation*)canEnableE2EByDefaultInNewRoomWithUsers:(NSArray<NSString*>*)userIds
success:(void (^)(BOOL canEnableE2E))success
failure:(void (^)(NSError *error))failure;

@end
34 changes: 34 additions & 0 deletions Riot/Categories/MXSession+Riot.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "MXSession+Riot.h"

#import "MXRoom+Riot.h"
#import "Riot-Swift.h"

@implementation MXSession (Riot)

Expand Down Expand Up @@ -48,4 +49,37 @@ - (NSUInteger)riot_missedDiscussionsCount
return missedDiscussionsCount;
}

- (MXHTTPOperation*)canEnableE2EByDefaultInNewRoomWithUsers:(NSArray<NSString*>*)userIds
success:(void (^)(BOOL canEnableE2E))success
failure:(void (^)(NSError *error))failure
{
MXHTTPOperation *operation;
if (RiotSettings.shared.enableCrossSigning)
{
// Check whether all users have uploaded device keys before.
// If so, encryption can be enabled in the new room
operation = [self.crypto downloadKeys:userIds forceDownload:NO success:^(MXUsersDevicesMap<MXDeviceInfo *> *usersDevicesInfoMap, NSDictionary<NSString *,MXCrossSigningInfo *> *crossSigningKeysMap) {

BOOL allUsersHaveDeviceKeys = YES;
for (NSString *userId in userIds)
{
if ([usersDevicesInfoMap deviceIdsForUser:userId].count == 0)
{
allUsersHaveDeviceKeys = NO;
break;
}
}

success(allUsersHaveDeviceKeys);

} failure:failure];
}
else
{
success(NO);
}

return operation;
}

@end
56 changes: 38 additions & 18 deletions Riot/Modules/Contacts/Details/ContactDetailsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#import "AppDelegate.h"
#import "Riot-Swift.h"
#import "MXSession+Riot.h"

#import "RoomMemberTitleView.h"

Expand Down Expand Up @@ -1040,33 +1041,52 @@ - (void)onActionButtonPressed:(id)sender
{
inviteArray = @[participantId];
}

// Create a new room
MXRoomCreationParameters *roomCreationParameters = [MXRoomCreationParameters new];
roomCreationParameters.visibility = kMXRoomDirectoryVisibilityPrivate;
roomCreationParameters.inviteArray = inviteArray;
roomCreationParameters.invite3PIDArray = invite3PIDArray;
roomCreationParameters.isDirect = YES;
roomCreationParameters.preset = kMXRoomPresetTrustedPrivateChat;
roomCreationRequest = [self.mainSession createRoomWithParameters:roomCreationParameters success:^(MXRoom *room) {

roomCreationRequest = nil;

[self removePendingActionMask];

[[AppDelegate theDelegate] showRoom:room.roomId andEventId:nil withMatrixSession:self.mainSession];

} failure:^(NSError *error) {
MXWeakify(self);
void (^onFailure)(NSError *) = ^(NSError *error){
MXStrongifyAndReturnIfNil(self);

NSLog(@"[ContactDetailsViewController] Create room failed");

roomCreationRequest = nil;
self->roomCreationRequest = nil;

[self removePendingActionMask];

// Notify user
[[AppDelegate theDelegate] showErrorAsAlert:error];
}];
};


// Create a new room
[self.mainSession canEnableE2EByDefaultInNewRoomWithUsers:inviteArray success:^(BOOL canEnableE2E) {
MXStrongifyAndReturnIfNil(self);

MXRoomCreationParameters *roomCreationParameters = [MXRoomCreationParameters new];
roomCreationParameters.visibility = kMXRoomDirectoryVisibilityPrivate;
roomCreationParameters.inviteArray = inviteArray;
roomCreationParameters.invite3PIDArray = invite3PIDArray;
roomCreationParameters.isDirect = YES;
roomCreationParameters.preset = kMXRoomPresetTrustedPrivateChat;

if (canEnableE2E && roomCreationParameters.invite3PIDArray == nil)
{
roomCreationParameters.initialStateEvents = @[
[MXRoomCreationParameters initialStateEventForEncryptionWithAlgorithm:kMXCryptoMegolmAlgorithm
]];
}


self->roomCreationRequest = [self.mainSession createRoomWithParameters:roomCreationParameters success:^(MXRoom *room) {

self->roomCreationRequest = nil;

[self removePendingActionMask];

[[AppDelegate theDelegate] showRoom:room.roomId andEventId:nil withMatrixSession:self.mainSession];

} failure:onFailure];

} failure:onFailure];
}
break;
}
Expand Down
54 changes: 35 additions & 19 deletions Riot/Modules/StartChat/StartChatViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#import "AppDelegate.h"
#import "Riot-Swift.h"
#import "MXSession+Riot.h"

@interface StartChatViewController () <UITableViewDataSource, UISearchBarDelegate, ContactsTableViewControllerDelegate>
{
Expand Down Expand Up @@ -571,36 +572,51 @@ - (IBAction)onButtonPressed:(id)sender
{
// Ensure direct chat are created with equal ops on both sides (the trusted_private_chat preset)
MXRoomPreset preset = (isDirect ? kMXRoomPresetTrustedPrivateChat : nil);

// Create new room
MXRoomCreationParameters *roomCreationParameters = [MXRoomCreationParameters new];
roomCreationParameters.visibility = kMXRoomDirectoryVisibilityPrivate;
roomCreationParameters.inviteArray = inviteArray.count ? inviteArray : nil;
roomCreationParameters.invite3PIDArray = invite3PIDArray.count ? invite3PIDArray : nil;
roomCreationParameters.isDirect = isDirect;
roomCreationParameters.preset = preset;

roomCreationRequest = [self.mainSession createRoomWithParameters:roomCreationParameters success:^(MXRoom *room) {
MXWeakify(self);
void (^onFailure)(NSError *) = ^(NSError *error){
MXStrongifyAndReturnIfNil(self);

roomCreationRequest = nil;
self->createBarButtonItem.enabled = YES;

self->roomCreationRequest = nil;
[self stopActivityIndicator];

[[AppDelegate theDelegate] showRoom:room.roomId andEventId:nil withMatrixSession:self.mainSession];
NSLog(@"[StartChatViewController] Create room failed");

} failure:^(NSError *error) {
// Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
};

createBarButtonItem.enabled = YES;
[self.mainSession canEnableE2EByDefaultInNewRoomWithUsers:inviteArray success:^(BOOL canEnableE2E) {
MXStrongifyAndReturnIfNil(self);

roomCreationRequest = nil;
[self stopActivityIndicator];
// Create new room
MXRoomCreationParameters *roomCreationParameters = [MXRoomCreationParameters new];
roomCreationParameters.visibility = kMXRoomDirectoryVisibilityPrivate;
roomCreationParameters.inviteArray = inviteArray.count ? inviteArray : nil;
roomCreationParameters.invite3PIDArray = invite3PIDArray.count ? invite3PIDArray : nil;
roomCreationParameters.isDirect = isDirect;
roomCreationParameters.preset = preset;

NSLog(@"[StartChatViewController] Create room failed");
if (canEnableE2E && roomCreationParameters.invite3PIDArray == nil)
{
roomCreationParameters.initialStateEvents = @[
[MXRoomCreationParameters initialStateEventForEncryptionWithAlgorithm:kMXCryptoMegolmAlgorithm
]];
}

// Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
self->roomCreationRequest = [self.mainSession createRoomWithParameters:roomCreationParameters success:^(MXRoom *room) {

}];
self->roomCreationRequest = nil;

[self stopActivityIndicator];

[[AppDelegate theDelegate] showRoom:room.roomId andEventId:nil withMatrixSession:self.mainSession];

} failure:onFailure];

} failure:onFailure];
}
}
else if (sender == self.navigationItem.leftBarButtonItem)
Expand Down