Refactor encryption trust level

This commit is contained in:
Andy Uhnak
2023-03-13 16:18:11 +00:00
parent 013c62f6e1
commit fc350b546d
14 changed files with 286 additions and 59 deletions
@@ -0,0 +1,31 @@
/*
Copyright 2016 OpenMarket Ltd
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 "MatrixKit.h"
/**
TODO: This view as it is implemented in this class must disappear.
It should be part of the device verification flow (`DeviceVerificationCoordinator`).
*/
@interface EncryptionInfoView : MXKEncryptionInfoView
/**
Open the legacy simple verification screen
*/
- (void)displayLegacyVerificationScreen;
@end
@@ -0,0 +1,91 @@
/*
Copyright 2016 OpenMarket Ltd
Copyright 2017 Vector Creations Ltd
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 "EncryptionInfoView.h"
#import "ThemeService.h"
#import "GeneratedInterface-Swift.h"
@interface EncryptionInfoView() <KeyVerificationCoordinatorBridgePresenterDelegate>
{
KeyVerificationCoordinatorBridgePresenter *keyVerificationCoordinatorBridgePresenter;
}
@end
@implementation EncryptionInfoView
#pragma mark - Override MXKView
-(void)customizeViewRendering
{
[super customizeViewRendering];
self.backgroundColor = ThemeService.shared.theme.headerBackgroundColor;
self.textView.backgroundColor = ThemeService.shared.theme.backgroundColor;
self.defaultTextColor = ThemeService.shared.theme.textPrimaryColor;
self.cancelButton.tintColor = ThemeService.shared.theme.tintColor;
self.verifyButton.tintColor = ThemeService.shared.theme.tintColor;
self.blockButton.tintColor = ThemeService.shared.theme.tintColor;
self.confirmVerifyButton.tintColor = ThemeService.shared.theme.tintColor;
}
- (void)displayLegacyVerificationScreen
{
[super onButtonPressed:self.verifyButton];
}
- (void)onButtonPressed:(id)sender
{
UIViewController *rootViewController = [AppDelegate theDelegate].window.rootViewController;
if (sender == self.verifyButton && self.mxDeviceInfo.trustLevel.localVerificationStatus != MXDeviceVerified
&& self.mxDeviceInfo
&& rootViewController)
{
// Redirect to the interactive device verification flow
keyVerificationCoordinatorBridgePresenter = [[KeyVerificationCoordinatorBridgePresenter alloc] initWithSession:self.mxSession];
keyVerificationCoordinatorBridgePresenter.delegate = self;
// Show it on the root view controller
[keyVerificationCoordinatorBridgePresenter presentFrom:rootViewController otherUserId:self.mxDeviceInfo.userId otherDeviceId:self.mxDeviceInfo.deviceId animated:YES];
}
else
{
[super onButtonPressed:sender];
}
}
- (void)keyVerificationCoordinatorBridgePresenterDelegateDidComplete:(KeyVerificationCoordinatorBridgePresenter * _Nonnull)coordinatorBridgePresenter otherUserId:(NSString * _Nonnull)otherUserId otherDeviceId:(NSString * _Nonnull)otherDeviceId
{
[self dismissKeyVerificationCoordinatorBridgePresenter];
}
- (void)keyVerificationCoordinatorBridgePresenterDelegateDidCancel:(KeyVerificationCoordinatorBridgePresenter * _Nonnull)coordinatorBridgePresenter
{
[self dismissKeyVerificationCoordinatorBridgePresenter];
}
- (void)dismissKeyVerificationCoordinatorBridgePresenter
{
[keyVerificationCoordinatorBridgePresenter dismissWithAnimated:YES completion:nil];
keyVerificationCoordinatorBridgePresenter = nil;
// Eject like MXKEncryptionInfoView does
[self removeFromSuperview];
}
@end
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12121" systemVersion="16F73" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="8VI-1E-fge" customClass="EncryptionInfoView">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<textView autoresizesSubviews="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" bounces="NO" editable="NO" translatesAutoresizingMaskIntoConstraints="NO" id="3Vk-Jx-L6Y">
<rect key="frame" x="10" y="10" width="580" height="540"/>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Ump-1C-SzI">
<rect key="frame" x="60" y="560" width="30" height="30"/>
<state key="normal" title="OK">
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="onButtonPressed:" destination="8VI-1E-fge" eventType="touchUpInside" id="t42-2S-MsY"/>
</connections>
</button>
<button hidden="YES" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="qkC-t0-Qd0">
<rect key="frame" x="355" y="560" width="40" height="30"/>
<state key="normal" title="Verify">
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="onButtonPressed:" destination="8VI-1E-fge" eventType="touchUpInside" id="ljK-ul-fY2"/>
</connections>
</button>
<button hidden="YES" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="IdO-jn-iTV">
<rect key="frame" x="507" y="560" width="38" height="30"/>
<state key="normal" title="Block">
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="onButtonPressed:" destination="8VI-1E-fge" eventType="touchUpInside" id="j21-e2-e3C"/>
</connections>
</button>
<button hidden="YES" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="L4h-aG-8UN">
<rect key="frame" x="506" y="560" width="40" height="30"/>
<state key="normal" title="Verify">
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="onButtonPressed:" destination="8VI-1E-fge" eventType="touchUpInside" id="nKr-w7-ECp"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="L4h-aG-8UN" firstAttribute="top" secondItem="3Vk-Jx-L6Y" secondAttribute="bottom" constant="10" id="0tX-be-JRZ"/>
<constraint firstAttribute="centerX" secondItem="qkC-t0-Qd0" secondAttribute="centerX" multiplier="0.8" id="7iU-9D-miY"/>
<constraint firstAttribute="bottom" secondItem="qkC-t0-Qd0" secondAttribute="bottom" constant="10" id="DyR-rX-nt0"/>
<constraint firstAttribute="bottom" secondItem="Ump-1C-SzI" secondAttribute="bottom" constant="10" id="Nfo-nh-gvW"/>
<constraint firstItem="3Vk-Jx-L6Y" firstAttribute="top" secondItem="8VI-1E-fge" secondAttribute="top" constant="10" id="Oi3-Zq-HKf"/>
<constraint firstAttribute="bottom" secondItem="L4h-aG-8UN" secondAttribute="bottom" constant="10" id="Owt-zM-W2X"/>
<constraint firstItem="3Vk-Jx-L6Y" firstAttribute="leading" secondItem="8VI-1E-fge" secondAttribute="leading" constant="10" id="Tqb-eF-AIh"/>
<constraint firstAttribute="centerX" secondItem="Ump-1C-SzI" secondAttribute="centerX" multiplier="4" id="YMC-ob-tqT"/>
<constraint firstAttribute="bottom" secondItem="IdO-jn-iTV" secondAttribute="bottom" constant="10" id="YcV-iN-9WU"/>
<constraint firstAttribute="trailing" secondItem="3Vk-Jx-L6Y" secondAttribute="trailing" constant="10" id="ZbI-rp-TR9"/>
<constraint firstAttribute="centerX" secondItem="L4h-aG-8UN" secondAttribute="centerX" multiplier="0.57" id="g1v-Mg-fQ6"/>
<constraint firstAttribute="centerX" secondItem="IdO-jn-iTV" secondAttribute="centerX" multiplier="0.57" id="gAm-ph-zPW"/>
</constraints>
<nil key="simulatedStatusBarMetrics"/>
<nil key="simulatedTopBarMetrics"/>
<nil key="simulatedBottomBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<connections>
<outlet property="blockButton" destination="IdO-jn-iTV" id="HI0-ZU-esJ"/>
<outlet property="cancelButton" destination="Ump-1C-SzI" id="U8M-nT-JiK"/>
<outlet property="confirmVerifyButton" destination="L4h-aG-8UN" id="GX6-dB-JG6"/>
<outlet property="textView" destination="3Vk-Jx-L6Y" id="uOw-Bq-neN"/>
<outlet property="verifyButton" destination="qkC-t0-Qd0" id="6LD-04-WgO"/>
</connections>
</view>
</objects>
</document>
@@ -0,0 +1,68 @@
//
// Copyright 2023 New Vector Ltd
//
// 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
/// Object responsible for calculating user and room trust level
///
/// For legacy reasons, the trust of multiple items is represented as `Progress` object,
/// where `completedUnitCount` represents the number of trusted users / devices.
@objc class EncryptionTrustLevel: NSObject {
struct TrustSummary {
let totalCount: Int64
let trustedCount: Int64
let areAllTrusted: Bool
init(progress: Progress) {
totalCount = max(progress.totalUnitCount, progress.completedUnitCount)
trustedCount = progress.completedUnitCount
areAllTrusted = trustedCount == totalCount
}
}
/// Calculate trust level for a single user given their cross-signing info
@objc func userTrustLevel(
crossSigning: MXCrossSigningInfo?,
trustedDevicesProgress: Progress
) -> UserEncryptionTrustLevel {
let devices = TrustSummary(progress: trustedDevicesProgress)
// If we could cross-sign but we haven't, the user is simply not verified
if let crossSigning, !crossSigning.trustLevel.isVerified {
return .notVerified
// If we cannot cross-sign the user (legacy behaviour) and have not signed
// any devices manually, the user is not verified
} else if crossSigning == nil && devices.trustedCount == 0 {
return .notVerified
}
// In all other cases we check devices for trust level
return devices.areAllTrusted ? .trusted : .warning
}
/// Calculate trust level for a room given trust level of users and their devices
@objc func roomTrustLevel(summary: MXUsersTrustLevelSummary) -> RoomEncryptionTrustLevel {
let users = TrustSummary(progress: summary.trustedUsersProgress)
let devices = TrustSummary(progress: summary.trustedDevicesProgress)
guard users.totalCount > 0 && users.areAllTrusted else {
return .normal
}
return devices.areAllTrusted ? .trusted : .warning
}
}
@@ -0,0 +1,59 @@
/*
Copyright 2020 New Vector Ltd
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 UIKit
@objcMembers
final class EncryptionTrustLevelBadgeImageHelper: NSObject {
static func roomBadgeImage(for trustLevel: RoomEncryptionTrustLevel) -> UIImage {
let badgeImage: UIImage
switch trustLevel {
case .warning:
badgeImage = Asset.Images.encryptionWarning.image
case .normal:
badgeImage = Asset.Images.encryptionNormal.image
case .trusted:
badgeImage = Asset.Images.encryptionTrusted.image
case .unknown:
badgeImage = Asset.Images.encryptionNormal.image
@unknown default:
badgeImage = Asset.Images.encryptionNormal.image
}
return badgeImage
}
static func userBadgeImage(for trustLevel: UserEncryptionTrustLevel) -> UIImage? {
let badgeImage: UIImage?
switch trustLevel {
case .warning:
badgeImage = Asset.Images.encryptionWarning.image
case .notVerified, .noCrossSigning:
badgeImage = Asset.Images.encryptionNormal.image
case .trusted:
badgeImage = Asset.Images.encryptionTrusted.image
default:
badgeImage = nil
}
return badgeImage
}
}
@@ -0,0 +1,25 @@
//
// Copyright 2023 New Vector Ltd
//
// 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.
//
/**
RoomEncryptionTrustLevel represents the trust level in an encrypted room.
*/
typedef NS_ENUM(NSUInteger, RoomEncryptionTrustLevel) {
RoomEncryptionTrustLevelTrusted,
RoomEncryptionTrustLevelWarning,
RoomEncryptionTrustLevelNormal,
RoomEncryptionTrustLevelUnknown
};
@@ -0,0 +1,29 @@
/*
Copyright 2020 New Vector Ltd
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;
/**
UserEncryptionTrustLevel represents the user trust level in an encrypted room.
*/
typedef NS_ENUM(NSUInteger, UserEncryptionTrustLevel) {
UserEncryptionTrustLevelTrusted, // The user is verified and they have trusted all their devices
UserEncryptionTrustLevelWarning, // The user is verified but they have not trusted all their devices
UserEncryptionTrustLevelNotVerified, // The user is not verified yet
UserEncryptionTrustLevelNoCrossSigning, // The user has not bootstrapped cross-signing yet
UserEncryptionTrustLevelNone, // Crypto is not enabled. Should not happen
UserEncryptionTrustLevelUnknown // Computation in progress
};