Skip to content

Commit

Permalink
Enable Crypto SDK by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Anderas committed Apr 18, 2023
1 parent d22d277 commit edc99aa
Show file tree
Hide file tree
Showing 36 changed files with 791 additions and 937 deletions.
86 changes: 0 additions & 86 deletions .github/workflows/ci-crypto-tests.yml

This file was deleted.

134 changes: 86 additions & 48 deletions MatrixSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@
<TestPlanReference
reference = "container:MatrixSDKTests/TestPlans/UnitTestsWithSanitizers.xctestplan">
</TestPlanReference>
<TestPlanReference
reference = "container:MatrixSDKTests/TestPlans/CryptoTests.xctestplan">
</TestPlanReference>
<TestPlanReference
reference = "container:MatrixSDKTests/TestPlans/AllWorkingTests.xctestplan">
</TestPlanReference>
Expand Down
2 changes: 1 addition & 1 deletion MatrixSDK/Crypto/CrossSigning/MXCrossSigningV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class MXCrossSigningV2: NSObject, MXCrossSigning {
// If we are considered verified, there is no need for a verification upgrade
// after migrating from legacy crypto
if myUserCrossSigningKeys?.trustLevel.isVerified == true {
MXSDKOptions.sharedInstance().cryptoSDKFeature?.needsVerificationUpgrade = false
MXSDKOptions.sharedInstance().needsVerificationUpgrade = false
}

log.debug("Cross signing state refreshed, new state: \(state)")
Expand Down
29 changes: 18 additions & 11 deletions MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,26 @@ + (void)initializeCryptoWithMatrixSession:(MXSession *)mxSession
complete:(void (^)(id<MXCrypto> crypto, NSError *error))complete
{
#ifdef MX_CRYPTO

// Each time we construct the crypto module (app launch, login etc) we have a chance to try to enable
// the newer SDK crypto module, if it is available for this particular user.
[MXSDKOptions.sharedInstance.cryptoSDKFeature enableIfAvailableForUserId:mxSession.myUserId];

if (MXSDKOptions.sharedInstance.enableCryptoSDK)
{
[MXCryptoV2Factory.shared buildCryptoWithSession:mxSession
migrationProgress:migrationProgress
success:^(id<MXCrypto> crypto) {
complete(crypto, nil); }
failure:^(NSError *error) {
complete(nil, error);
}];
BOOL enableCrypto = [MXSDKOptions sharedInstance].enableCryptoWhenStartingMXSession || [MXCryptoV2Factory.shared hasCryptoDataFor:mxSession];
if (enableCrypto)
{
[MXCryptoV2Factory.shared buildCryptoWithSession:mxSession
migrationProgress:migrationProgress
success:^(id<MXCrypto> crypto) {
complete(crypto, nil); }
failure:^(NSError *error) {
complete(nil, error);
}];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
complete(nil, nil);
});
}
return;
}

Expand Down
17 changes: 16 additions & 1 deletion MatrixSDK/Crypto/MXCryptoV2Factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ import Foundation
.deprecated3
}

@objc public func hasCryptoData(for session: MXSession!) -> Bool {
guard let userId = session?.myUserId else {
log.error("Missing required dependencies")
return false
}

do {
let url = try MXCryptoMachineStore.storeURL(for: userId)
return FileManager.default.fileExists(atPath: url.path)
} catch {
log.error("Failed creating url for user", context: error)
return false
}
}

@objc public func buildCrypto(
session: MXSession!,
migrationProgress: ((Double) -> Void)?,
Expand Down Expand Up @@ -124,7 +139,7 @@ import Foundation
// unless the rust-based crypto already considers the current session to be verified given
// the migration data
log.debug("Needs verification upgrade")
MXSDKOptions.sharedInstance().cryptoSDKFeature?.needsVerificationUpgrade = true
MXSDKOptions.sharedInstance().needsVerificationUpgrade = true
}
}
}
50 changes: 0 additions & 50 deletions MatrixSDK/Crypto/MXCryptoV2Feature.swift

This file was deleted.

15 changes: 9 additions & 6 deletions MatrixSDK/MXSDKOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,21 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) BOOL enableRoomSharedHistoryOnInvite;

/**
An object which controls the availabilty of the rust-based `MatrixCryptoSDK`.
Use the newer rust-based `MatrixCryptoSDK` instead of the legacy `MatrixSDK`'s internal crypto module.
@remark nil by default.
@remark YES by default
*/
@property (nonatomic, nullable) id<MXCryptoV2Feature> cryptoSDKFeature;
@property (nonatomic) BOOL enableCryptoSDK;

/**
Use the rust-based `MatrixCryptoSDK` instead of `MatrixSDK`'s internal crypto module.
Flag indicating whether this account requires a re-verification after migrating to Crypto SDK
This flag is set to true if the legacy account is considered verified but the rust account
does not consider the migrated data secure enough, as it applies stricter security conditions.
@remark this property is a convenience getter for `cryptoSDKFeature.isEnabled`
@remark NO by default.
*/
@property (nonatomic, readonly) BOOL enableCryptoSDK;
@property (nonatomic) BOOL needsVerificationUpgrade;

/**
The text-based identifier for the crypto module being used (e.g. native vs rust)
Expand Down
12 changes: 2 additions & 10 deletions MatrixSDK/MXSDKOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ - (instancetype)init
_authEnableRefreshTokens = NO;
_enableThreads = NO;
_enableRoomSharedHistoryOnInvite = NO;
_enableCryptoSDK = YES;
_needsVerificationUpgrade = NO;
_enableSymmetricBackup = NO;
_enableNewClientInformationFeature = NO;
_enableStartupProgress = YES;
Expand All @@ -62,16 +64,6 @@ - (instancetype)init
return self;
}

- (BOOL)enableCryptoSDK
{
if (!self.cryptoSDKFeature)
{
MXLogError(@"[MXSDKOptions] enableCryptoSDK: Crypto SDK feature is not configured");
return NO;
}
return self.cryptoSDKFeature.isEnabled;
}

- (NSString *)cryptoModuleId
{
return self.enableCryptoSDK ? @"rust" : @"native";
Expand Down
12 changes: 12 additions & 0 deletions MatrixSDKTests/Crypto/CrossSigning/MXCrossSigningV2UnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@ private extension MXCrossSigningV2 {
}
}
}

private extension MXCrossSigning {
func refreshState() async throws {
return try await withCheckedThrowingContinuation { continuation in
refreshState { _ in
continuation.resume()
} failure: { error in
continuation.resume(throwing: error)
}
}
}
}
31 changes: 31 additions & 0 deletions MatrixSDKTests/Crypto/CryptoMachine/MXKeyProviderStub.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Copyright 2023 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

class MXKeyProviderStub: NSObject, MXKeyProviderDelegate {
func isEncryptionAvailableForData(ofType dataType: String) -> Bool {
return true
}

func hasKeyForData(ofType dataType: String) -> Bool {
return true
}

func keyDataForData(ofType dataType: String) -> MXKeyData? {
MXRawDataKey(key: "1234".data(using: .ascii)!)
}
}
Loading

0 comments on commit edc99aa

Please sign in to comment.