Skip to content

Commit

Permalink
Perform crypto migration if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Anderas committed Jan 16, 2023
1 parent 7d62ce2 commit 7990397
Show file tree
Hide file tree
Showing 11 changed files with 571 additions and 78 deletions.
274 changes: 274 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion MatrixSDK/Background/MXBackgroundSyncService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public enum MXBackgroundSyncServiceError: Error {

crypto = {
#if DEBUG
if MXSDKOptions.sharedInstance().enableCryptoV2 {
if MXSDKOptions.sharedInstance().isCryptoSDKAvailable && MXSDKOptions.sharedInstance().enableCryptoSDK {
// Crypto V2 is currently unable to decrypt notifications due to single-process store,
// so it uses dummy background crypto that does not do anything.
return MXDummyBackgroundCrypto()
Expand Down
11 changes: 6 additions & 5 deletions MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,18 @@ class MXCryptoMachine {
try await requests.queryKeys(users: users)
}
self.getRoomAction = getRoomAction
}

func start() async throws {

let details = """
Starting the crypto machine for \(userId)
Initialized the crypto machine for \(userId)
- device id : \(deviceId)
- ed25519 : \(deviceEd25519Key ?? "")
- curve25519 : \(deviceCurve25519Key ?? "")
"""
log.debug(details)
}

func uploadKeysIfNecessary() async throws {
log.debug("Checking for keys to upload")

var keysUploadRequest: Request?
for request in try machine.outgoingRequests() {
Expand All @@ -120,7 +122,6 @@ class MXCryptoMachine {
}

try await handleRequest(request)

log.debug("Keys successfully uploaded")
}

Expand Down
4 changes: 2 additions & 2 deletions MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ @implementation MXLegacyCrypto
#ifdef MX_CRYPTO

#if DEBUG
if (MXSDKOptions.sharedInstance.enableCryptoV2)
if (MXSDKOptions.sharedInstance.isCryptoSDKAvailable && MXSDKOptions.sharedInstance.enableCryptoSDK)
{
return [self createCryptoV2WithSession:mxSession error:error];
}
Expand All @@ -182,7 +182,7 @@ + (void)checkCryptoWithMatrixSession:(MXSession *)mxSession
{
#ifdef MX_CRYPTO
#if DEBUG
if (MXSDKOptions.sharedInstance.enableCryptoV2)
if (MXSDKOptions.sharedInstance.isCryptoSDKAvailable && MXSDKOptions.sharedInstance.enableCryptoSDK)
{
NSError *error;
id<MXCrypto> crypto = [self createCryptoV2WithSession:mxSession error:&error];
Expand Down
18 changes: 17 additions & 1 deletion MatrixSDK/Crypto/MXCryptoV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ private class MXCryptoV2: NSObject, MXCrypto {
log.debug("->")
startTask = Task {
do {
try await machine.start()
try migrateIfNecessary()

try await machine.uploadKeysIfNecessary()
crossSigning.refreshState(success: nil)
backup?.checkAndStart()

Expand Down Expand Up @@ -247,6 +249,20 @@ private class MXCryptoV2: NSObject, MXCrypto {
}
}

private func migrateIfNecessary() throws {
guard legacyStore.cryptoVersion.rawValue < MXCryptoVersion.versionLegacyDeprecated.rawValue else {
log.debug("Legacy crypto has already been deprecated, no need to migrate")
return
}

log.debug("Requires migration from legacy crypto")
let migration = MXCryptoMigrationV2(legacyStore: legacyStore)
try migration.migrateCrypto()

log.debug("Marking legacy crypto as deprecated")
legacyStore.cryptoVersion = MXCryptoVersion.versionLegacyDeprecated
}

// MARK: - Event Encryption

public func isRoomEncrypted(_ roomId: String) -> Bool {
Expand Down
7 changes: 6 additions & 1 deletion MatrixSDK/Crypto/Migration/MXCryptoVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ typedef NS_ENUM(NSInteger, MXCryptoVersion)
// https://github.com/vector-im/element-ios/issues/3818
MXCryptoVersion2,

// Keep it at the last position. It is used to compute MXCryptoVersionLast.
// Keep it at the last position of valid versions, except for the deprecated variant.
// It is used to compute MXCryptoVersionLast.
MXCryptoVersionCount,

// The internal crypto module has been deprecated in favour of `MatrixCryptoSDK`
// The value is set manually to leave room for intermediate version 3, 4 ...
MXCryptoVersionLegacyDeprecated = 1000,
};

// The current version of MXCrypto
Expand Down
18 changes: 14 additions & 4 deletions MatrixSDK/MXSDKOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ typedef NS_ENUM(NSUInteger, MXCallTransferType)
MXCallTransferTypeLocal
};


#pragma mark - Build time options

/**
Expand Down Expand Up @@ -207,12 +206,23 @@ NS_ASSUME_NONNULL_BEGIN
#if DEBUG

/**
Enable Crypto module V2, a work-in-progress and NOT production-ready implementation
of [matrix-rust-sdk](https://github.com/matrix-org/matrix-rust-sdk/tree/main/crates/matrix-sdk-crypto).
The state of the rust-based `MatrixCryptoSDK` which replaces `MatrixSDK`'s internal crypto module,
and whether it is available to a user as an option.
To control which crypto module is actually used, set `enableCryptoSDK`.
@remark NO by default.
*/
@property (nonatomic) BOOL isCryptoSDKAvailable;

/**
Use the rust-based `MatrixCryptoSDK` instead of `MatrixSDK`'s internal crypto module.
This option should only be enabled if `isCryptoSDKAvailable` is set to YES.
@remark NO by default.
*/
@property (nonatomic) BOOL enableCryptoV2;
@property (nonatomic) BOOL enableCryptoSDK;

#endif

Expand Down
3 changes: 2 additions & 1 deletion MatrixSDK/MXSDKOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ - (instancetype)init
_enableRoomSharedHistoryOnInvite = NO;

#if DEBUG
_enableCryptoV2 = NO;
_isCryptoSDKAvailable = NO;
_enableCryptoSDK = NO;
#endif

_enableSymmetricBackup = NO;
Expand Down
Loading

0 comments on commit 7990397

Please sign in to comment.