From 79903970f2b927d7218069b1db3d85536530da3a Mon Sep 17 00:00:00 2001 From: Andy Uhnak Date: Thu, 12 Jan 2023 19:30:54 +0000 Subject: [PATCH] Perform crypto migration if necessary --- MatrixSDK.xcodeproj/project.pbxproj | 274 ++++++++++++++++++ .../Background/MXBackgroundSyncService.swift | 2 +- .../CryptoMachine/MXCryptoMachine.swift | 11 +- MatrixSDK/Crypto/MXCrypto.m | 4 +- MatrixSDK/Crypto/MXCryptoV2.swift | 18 +- MatrixSDK/Crypto/Migration/MXCryptoVersion.h | 7 +- MatrixSDK/MXSDKOptions.h | 18 +- MatrixSDK/MXSDKOptions.m | 3 +- .../Migration/MXCryptoMigrationV2Tests.swift | 225 ++++++++++---- MatrixSDKTests/MatrixSDKTestsData.swift | 35 +++ MatrixSDKTests/MatrixSDKTestsE2EData.swift | 52 ++++ 11 files changed, 571 insertions(+), 78 deletions(-) create mode 100644 MatrixSDKTests/MatrixSDKTestsData.swift create mode 100644 MatrixSDKTests/MatrixSDKTestsE2EData.swift diff --git a/MatrixSDK.xcodeproj/project.pbxproj b/MatrixSDK.xcodeproj/project.pbxproj index 10052ebebd..2235838e91 100644 --- a/MatrixSDK.xcodeproj/project.pbxproj +++ b/MatrixSDK.xcodeproj/project.pbxproj @@ -1882,6 +1882,10 @@ ED558071296F1BEE003443E3 /* MXCryptoMigrationV2Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED55806F296F1BEE003443E3 /* MXCryptoMigrationV2Tests.swift */; }; ED5580732970265A003443E3 /* MXCryptoMachineLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED5580722970265A003443E3 /* MXCryptoMachineLogger.swift */; }; ED5580742970265A003443E3 /* MXCryptoMachineLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED5580722970265A003443E3 /* MXCryptoMachineLogger.swift */; }; + ED55807629709943003443E3 /* MatrixSDKTestsE2EData.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED55807529709943003443E3 /* MatrixSDKTestsE2EData.swift */; }; + ED55807729709943003443E3 /* MatrixSDKTestsE2EData.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED55807529709943003443E3 /* MatrixSDKTestsE2EData.swift */; }; + ED5580792970A879003443E3 /* MatrixSDKTestsData.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED5580782970A879003443E3 /* MatrixSDKTestsData.swift */; }; + ED55807A2970A879003443E3 /* MatrixSDKTestsData.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED5580782970A879003443E3 /* MatrixSDKTestsData.swift */; }; ED5AE8C52816C8CF00105072 /* MXCoreDataRoomSummaryStore.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = ED5AE8C22816C8CF00105072 /* MXCoreDataRoomSummaryStore.xcdatamodeld */; }; ED5AE8C62816C8CF00105072 /* MXCoreDataRoomSummaryStore.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = ED5AE8C22816C8CF00105072 /* MXCoreDataRoomSummaryStore.xcdatamodeld */; }; ED5C753C28B3E80300D24E85 /* MXLogger.h in Headers */ = {isa = PBXBuildFile; fileRef = ED5C753528B3E80300D24E85 /* MXLogger.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -3079,6 +3083,8 @@ ED55806C296F0E3A003443E3 /* MXCryptoMigrationStoreUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXCryptoMigrationStoreUnitTests.swift; sourceTree = ""; }; ED55806F296F1BEE003443E3 /* MXCryptoMigrationV2Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXCryptoMigrationV2Tests.swift; sourceTree = ""; }; ED5580722970265A003443E3 /* MXCryptoMachineLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXCryptoMachineLogger.swift; sourceTree = ""; }; + ED55807529709943003443E3 /* MatrixSDKTestsE2EData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatrixSDKTestsE2EData.swift; sourceTree = ""; }; + ED5580782970A879003443E3 /* MatrixSDKTestsData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatrixSDKTestsData.swift; sourceTree = ""; }; ED5AE8C32816C8CF00105072 /* MXRoomSummaryCoreDataStore2.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MXRoomSummaryCoreDataStore2.xcdatamodel; sourceTree = ""; }; ED5AE8C42816C8CF00105072 /* MXRoomSummaryCoreDataStore.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MXRoomSummaryCoreDataStore.xcdatamodel; sourceTree = ""; }; ED5C753528B3E80300D24E85 /* MXLogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXLogger.h; sourceTree = ""; }; @@ -4297,7 +4303,9 @@ 32832B5B1BCC048300241108 /* MXStoreTests.h */, 32C03CB52123076F00D92712 /* DirectRoomTests.m */, 3281E8A719E41A2000976E1A /* MatrixSDKTestsData.m */, + ED5580782970A879003443E3 /* MatrixSDKTestsData.swift */, 32BD34BD1E84134A006EDC0D /* MatrixSDKTestsE2EData.m */, + ED55807529709943003443E3 /* MatrixSDKTestsE2EData.swift */, 3264DB931CECA72900B99881 /* MXAccountDataTests.m */, EC40385C28A16EDA0067D5B8 /* MXAes256KeyBackupTests.m */, 32792BE02296C64200F4FC9D /* MXAggregatedEditsTests.m */, @@ -6548,6 +6556,7 @@ 32C6F92919DD814400EA4E9C /* Frameworks */, 32C6F92B19DD814400EA4E9C /* Resources */, 323B2ADA1BCD47F000B11F34 /* CopyFiles */, + B4AA8AD45FF27E7D5BE4F654 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -6587,6 +6596,7 @@ B14EF2942397E90400758AF0 /* Frameworks */, B14EF3662397E90400758AF0 /* Resources */, B14EF3672397E90400758AF0 /* CopyFiles */, + 96432586D5FE972A9EE39035 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -6770,6 +6780,266 @@ shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; + 96432586D5FE972A9EE39035 /* [CP] Copy Pods Resources */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-MatrixSDK-MatrixSDK-macOS/Pods-MatrixSDK-MatrixSDK-macOS-resources.sh", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/device.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/error.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/lib.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/logger.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/machine.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/responses.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/uniffi_api.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/users.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/verification.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/backups/keys/backup.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/backups/keys/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/backups/keys/recovery.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/backups/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/error.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/file_encryption/attachments.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/file_encryption/key_export.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/file_encryption/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/gossiping/machine.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/gossiping/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/identities/device.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/identities/manager.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/identities/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/identities/user.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/lib.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/machine.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/account.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/group_sessions/outbound.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/session.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/signing/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/signing/pk_signing.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/utility.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/requests.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/session_manager/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/session_manager/sessions.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/store/caches.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/store/integration_tests.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/store/memorystore.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/store/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/backup.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/cross_signing_key.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/device_keys.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/forwarded_room_key.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/olm_v1.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/room/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/room_key.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/room_key_request.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/secret_send.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/to_device.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/one_time_keys.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/utilities.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/cache.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/event_enums.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/machine.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/qrcode.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/requests.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/sas/helpers.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/sas/inner_sas.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/sas/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/sas/sas_state.rs", + ); + name = "[CP] Copy Pods Resources"; + outputPaths = ( + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/backup_recovery_key.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/device.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/error.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/lib.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/logger.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/machine.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/responses.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/uniffi_api.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/users.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/verification.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/backup.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/mod.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/recovery.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/attachments.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/key_export.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/manager.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/user.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/account.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/inbound.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/outbound.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/session.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/pk_signing.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/utility.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/requests.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/group_sessions.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/sessions.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/caches.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/integration_tests.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/memorystore.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/cross_signing_key.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/device_keys.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/forwarded_room_key.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/olm_v1.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/encrypted.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/room_key.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/room_key_request.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/secret_send.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/to_device.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/one_time_keys.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/utilities.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/cache.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/event_enums.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/qrcode.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/helpers.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/inner_sas.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/sas_state.rs", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-MatrixSDK-MatrixSDK-macOS/Pods-MatrixSDK-MatrixSDK-macOS-resources.sh\"\n"; + showEnvVarsInLog = 0; + }; + B4AA8AD45FF27E7D5BE4F654 /* [CP] Copy Pods Resources */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-MatrixSDK-MatrixSDK-iOS/Pods-MatrixSDK-MatrixSDK-iOS-resources.sh", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/backup_recovery_key.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/device.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/error.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/lib.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/logger.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/machine.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/responses.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/uniffi_api.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/users.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/bindings/matrix-sdk-crypto-ffi/src/verification.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/backups/keys/backup.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/backups/keys/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/backups/keys/recovery.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/backups/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/error.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/file_encryption/attachments.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/file_encryption/key_export.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/file_encryption/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/gossiping/machine.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/gossiping/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/identities/device.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/identities/manager.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/identities/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/identities/user.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/lib.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/machine.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/account.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/group_sessions/outbound.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/session.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/signing/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/signing/pk_signing.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/olm/utility.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/requests.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/session_manager/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/session_manager/sessions.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/store/caches.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/store/integration_tests.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/store/memorystore.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/store/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/backup.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/cross_signing_key.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/device_keys.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/forwarded_room_key.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/olm_v1.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/room/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/room_key.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/room_key_request.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/secret_send.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/events/to_device.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/types/one_time_keys.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/utilities.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/cache.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/event_enums.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/machine.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/qrcode.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/requests.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/sas/helpers.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/sas/inner_sas.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/sas/mod.rs", + "${PODS_ROOT}/../../matrix-rust-sdk/crates/matrix-sdk-crypto/src/verification/sas/sas_state.rs", + ); + name = "[CP] Copy Pods Resources"; + outputPaths = ( + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/backup_recovery_key.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/device.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/error.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/lib.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/logger.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/machine.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/responses.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/uniffi_api.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/users.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/verification.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/backup.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/mod.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/recovery.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/attachments.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/key_export.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/manager.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/user.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/account.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/inbound.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/outbound.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/session.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/pk_signing.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/utility.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/requests.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/group_sessions.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/sessions.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/caches.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/integration_tests.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/memorystore.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/cross_signing_key.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/device_keys.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/forwarded_room_key.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/olm_v1.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/encrypted.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/room_key.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/room_key_request.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/secret_send.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/to_device.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/one_time_keys.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/utilities.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/cache.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/event_enums.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/qrcode.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/helpers.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/inner_sas.rs", + "${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/sas_state.rs", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-MatrixSDK-MatrixSDK-iOS/Pods-MatrixSDK-MatrixSDK-iOS-resources.sh\"\n"; + showEnvVarsInLog = 0; + }; BBA28E8D0F027A434CEA072E /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; @@ -7317,6 +7587,7 @@ 32684CB821085F770046D2F9 /* MXLazyLoadingTests.m in Sources */, 18121F75273E6D2400B68ADF /* MXPollBuilderTests.swift in Sources */, ED7019F72886CA6C00FC31B9 /* VerificationRequestStub.swift in Sources */, + ED55807629709943003443E3 /* MatrixSDKTestsE2EData.swift in Sources */, B14EECEE2578FE3F00448735 /* MXAuthenticationSessionUnitTests.swift in Sources */, ED558070296F1BEE003443E3 /* MXCryptoMigrationV2Tests.swift in Sources */, ED2DD11D286C4F4400F06731 /* MXCryptoRequestsUnitTests.swift in Sources */, @@ -7356,6 +7627,7 @@ 32322A481E57264E005DD155 /* MXSelfSignedHomeserverTests.m in Sources */, 325653831A2E14ED00CC0423 /* MXStoreTests.m in Sources */, ED997856292E2877006B5248 /* MXSessionSyncProgressUnitTests.swift in Sources */, + ED5580792970A879003443E3 /* MatrixSDKTestsData.swift in Sources */, 18C26C4A273C0B3A00805154 /* PollAggregator.swift in Sources */, 3295719A1B024D2B00ABB3BA /* MXMockCallStackCall.m in Sources */, 324DD2BB246C3ADE00377005 /* MXCryptoSecretStorageTests.m in Sources */, @@ -7973,6 +8245,7 @@ EDF1B6942876CD8600BBBCEE /* MXTaskQueueUnitTests.swift in Sources */, B1E09A442397FD940057C069 /* Dummy.swift in Sources */, ED7019F82886CA6C00FC31B9 /* VerificationRequestStub.swift in Sources */, + ED55807729709943003443E3 /* MatrixSDKTestsE2EData.swift in Sources */, 18121F76273E6D2400B68ADF /* MXPollBuilderTests.swift in Sources */, ED558071296F1BEE003443E3 /* MXCryptoMigrationV2Tests.swift in Sources */, B1E09A1A2397FCE90057C069 /* MXAggregatedEditsTests.m in Sources */, @@ -8012,6 +8285,7 @@ B1E09A422397FD820057C069 /* MXCryptoTests.m in Sources */, B1E09A382397FD7D0057C069 /* MXUserTests.m in Sources */, ED997857292E2877006B5248 /* MXSessionSyncProgressUnitTests.swift in Sources */, + ED55807A2970A879003443E3 /* MatrixSDKTestsData.swift in Sources */, 18C26C4B273C0B3A00805154 /* PollAggregator.swift in Sources */, 324DD2BC246C3ADE00377005 /* MXCryptoSecretStorageTests.m in Sources */, B19A30D92404335D00FB6F35 /* MXQRCodeDataUnitTests.m in Sources */, diff --git a/MatrixSDK/Background/MXBackgroundSyncService.swift b/MatrixSDK/Background/MXBackgroundSyncService.swift index 8695914aa3..a83d4ef272 100644 --- a/MatrixSDK/Background/MXBackgroundSyncService.swift +++ b/MatrixSDK/Background/MXBackgroundSyncService.swift @@ -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() diff --git a/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift b/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift index 328213415e..4aa4976318 100644 --- a/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift +++ b/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift @@ -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() { @@ -120,7 +122,6 @@ class MXCryptoMachine { } try await handleRequest(request) - log.debug("Keys successfully uploaded") } diff --git a/MatrixSDK/Crypto/MXCrypto.m b/MatrixSDK/Crypto/MXCrypto.m index d014cc9039..2eff514c60 100644 --- a/MatrixSDK/Crypto/MXCrypto.m +++ b/MatrixSDK/Crypto/MXCrypto.m @@ -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]; } @@ -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 crypto = [self createCryptoV2WithSession:mxSession error:&error]; diff --git a/MatrixSDK/Crypto/MXCryptoV2.swift b/MatrixSDK/Crypto/MXCryptoV2.swift index 68d6fb225f..be7adaec41 100644 --- a/MatrixSDK/Crypto/MXCryptoV2.swift +++ b/MatrixSDK/Crypto/MXCryptoV2.swift @@ -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() @@ -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 { diff --git a/MatrixSDK/Crypto/Migration/MXCryptoVersion.h b/MatrixSDK/Crypto/Migration/MXCryptoVersion.h index 332a58873d..6fd9a648a8 100644 --- a/MatrixSDK/Crypto/Migration/MXCryptoVersion.h +++ b/MatrixSDK/Crypto/Migration/MXCryptoVersion.h @@ -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 diff --git a/MatrixSDK/MXSDKOptions.h b/MatrixSDK/MXSDKOptions.h index d6d8a9dba3..367e2ab00c 100644 --- a/MatrixSDK/MXSDKOptions.h +++ b/MatrixSDK/MXSDKOptions.h @@ -34,7 +34,6 @@ typedef NS_ENUM(NSUInteger, MXCallTransferType) MXCallTransferTypeLocal }; - #pragma mark - Build time options /** @@ -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 diff --git a/MatrixSDK/MXSDKOptions.m b/MatrixSDK/MXSDKOptions.m index e0b80e81ce..0c79b4958e 100644 --- a/MatrixSDK/MXSDKOptions.m +++ b/MatrixSDK/MXSDKOptions.m @@ -56,7 +56,8 @@ - (instancetype)init _enableRoomSharedHistoryOnInvite = NO; #if DEBUG - _enableCryptoV2 = NO; + _isCryptoSDKAvailable = NO; + _enableCryptoSDK = NO; #endif _enableSymmetricBackup = NO; diff --git a/MatrixSDKTests/Crypto/Migration/MXCryptoMigrationV2Tests.swift b/MatrixSDKTests/Crypto/Migration/MXCryptoMigrationV2Tests.swift index df449a6b35..3c1e401b55 100644 --- a/MatrixSDKTests/Crypto/Migration/MXCryptoMigrationV2Tests.swift +++ b/MatrixSDKTests/Crypto/Migration/MXCryptoMigrationV2Tests.swift @@ -22,6 +22,10 @@ import MatrixSDKCrypto @testable import MatrixSDK class MXCryptoMigrationV2Tests: XCTestCase { + enum Error: Swift.Error { + case missingDependencies + } + class KeyProvider: NSObject, MXKeyProviderDelegate { func isEncryptionAvailableForData(ofType dataType: String) -> Bool { return true @@ -38,82 +42,177 @@ class MXCryptoMigrationV2Tests: XCTestCase { var data: MatrixSDKTestsData! var e2eData: MatrixSDKTestsE2EData! - var keyProvider: KeyProvider! override func setUp() { data = MatrixSDKTestsData() e2eData = MatrixSDKTestsE2EData(matrixSDKTestsData: data) - - keyProvider = KeyProvider() - MXKeyProvider.sharedInstance().delegate = keyProvider + setLogger(logger: self) } - override func tearDown() { + // MARK: - Helpers + + private func migratedOlmMachine(session: MXSession) throws -> MXCryptoMachine { + guard + let store = session.legacyCrypto?.store, + let restClient = session.matrixRestClient + else { + throw Error.missingDependencies + } + + MXKeyProvider.sharedInstance().delegate = KeyProvider() + let migration = MXCryptoMigrationV2(legacyStore: store) + try migration.migrateCrypto() MXKeyProvider.sharedInstance().delegate = nil + + return try MXCryptoMachine( + userId: store.userId(), + deviceId: store.deviceId(), + restClient: restClient, + getRoomAction: { _ in + return nil + }) } - func test_canDecryptMessageAfterMigratingLegacyCrypto() throws { - e2eData.doE2ETestWithAliceAndBob(inARoom: self, cryptedBob: true, warnOnUnknowDevices: false) { aliceSession, bobSession, roomId, exp in - guard - let session = aliceSession, - let userId = session.myUserId, - let deviceId = session.myDeviceId, - let store = session.legacyCrypto?.store, - let room = session.room(withRoomId: roomId) - else { - XCTFail("Missing dependencies") - return + // MARK: - Tests + + func test_migratesAccountDetails() async throws { + let env = try await e2eData.startE2ETest() + let legacySession = env.session + + let machine = try self.migratedOlmMachine(session: env.session) + + XCTAssertEqual(machine.userId, legacySession.myUserId) + XCTAssertEqual(machine.deviceId, legacySession.myDeviceId) + XCTAssertEqual(machine.deviceCurve25519Key, legacySession.crypto.deviceCurve25519Key) + XCTAssertEqual(machine.deviceEd25519Key, legacySession.crypto.deviceEd25519Key) + } + + func test_canDecryptMegolmMessageAfterMigration() async throws { + let env = try await e2eData.startE2ETest() + + guard let room = env.session.room(withRoomId: env.roomId) else { + throw Error.missingDependencies + } + + // Send a new message in encrypted room + let event = try await room.sendTextMessage("Hi bob") + + // Erase cleartext and make sure the event was indeed encrypted + event.setClearData(nil) + XCTAssertTrue(event.isEncrypted) + XCTAssertEqual(event.content["algorithm"] as? String, kMXCryptoMegolmAlgorithm) + XCTAssertNotNil(event.content["ciphertext"]) + + // Migrate the session to crypto v2 + let machine = try self.migratedOlmMachine(session: env.session) + + // Decrypt the event using crypto v2 + let decrypted = try machine.decryptRoomEvent(event) + let result = try MXEventDecryptionResult(event: decrypted) + let content = result.clearEvent["content"] as? [String: Any] + + // At this point we should be able to read back the original message after + // having decrypted the event with room keys migrated earlier + XCTAssertEqual(content?["body"] as? String, "Hi bob") + } + + func test_migratesCrossSigningStatus() async throws { + let env = try await e2eData.startE2ETest() + + // We start with user who cannot cross-sign (did not setup cross signing keys) + let legacyCrossSigning = env.session.crypto.crossSigning + XCTAssertFalse(legacyCrossSigning.canCrossSign) + XCTAssertFalse(legacyCrossSigning.hasAllPrivateKeys) + + // We then migrate the user into crypto v2 + var machine = try migratedOlmMachine(session: env.session) + var crossSigningV2 = MXCrossSigningV2(crossSigning: machine, restClient: env.session.matrixRestClient) + try await crossSigningV2.refreshState() + + // As expected we cannot cross sign in v2 either + XCTAssertFalse(crossSigningV2.canCrossSign) + XCTAssertFalse(crossSigningV2.hasAllPrivateKeys) + + // Now we setup cross-signing with password on the original / legacy session + try await legacyCrossSigning.setup(withPassword: MXTESTS_ALICE_PWD) + XCTAssertTrue(legacyCrossSigning.canCrossSign) + XCTAssertTrue(legacyCrossSigning.hasAllPrivateKeys) + + // We have to migrate the data once again into crypto v2 + machine = try migratedOlmMachine(session: env.session) + crossSigningV2 = MXCrossSigningV2(crossSigning: machine, restClient: env.session.matrixRestClient) + try await crossSigningV2.refreshState() + + // And confirm that cross signing is ready + XCTAssertTrue(crossSigningV2.canCrossSign) + XCTAssertTrue(crossSigningV2.hasAllPrivateKeys) + } +} + +private extension MXCrypto { + func downloadKeys(userIds: [String]) async throws { + return try await withCheckedThrowingContinuation { continuation in + downloadKeys(userIds, forceDownload: false) { _, _ in + continuation.resume() } - - var event: MXEvent! - let clearTextMessage = "Hi bob" - - // Send clear text message to an E2E room - room.sendTextMessage(clearTextMessage, localEcho: &event) { _ in - - // Erase cleartext and make sure the event was indeed encrypted - event.setClearData(nil) - XCTAssertTrue(event.isEncrypted) - XCTAssertEqual(event.content["algorithm"] as? String, kMXCryptoMegolmAlgorithm) - XCTAssertNotNil(event.content["ciphertext"]) - - // Migrate data using crypto v2 migration and legacy store - do { - let migration = MXCryptoMigrationV2(legacyStore: store) - try migration.migrateCrypto() - } catch { - XCTFail("Cannot migrate - \(error)") - } - - // Now instantiate crypto machine (crypto v2) that should be able to find - // the migrated data and use it to decrypt the event - do { - let url = try MXCryptoMachine.storeURL(for: userId) - let machine = try OlmMachine( - userId: userId, - deviceId: deviceId, - path: url.path, - passphrase: nil - ) - - let decrypted = try machine.decryptRoomEvent(event: event.jsonString() ?? "", roomId: roomId!) - let result = try MXEventDecryptionResult(event: decrypted) - let content = result.clearEvent["content"] as? [String: Any] - - // At this point we should be able to read back the original message after - // having decrypted the event with room keys migrated earlier - XCTAssertEqual(content?["body"] as? String, clearTextMessage) - - } catch { - XCTFail("Cannot decrypt - \(error)") - } - - session.close() - bobSession?.close() - exp?.fulfill() + } + } +} + +private extension MXCrossSigning { + func refreshState() async throws { + return try await withCheckedThrowingContinuation { continuation in + refreshState { _ in + continuation.resume() + } failure: { error in + continuation.resume(throwing: error) + } + } + } + + func signUser(userId: String) async throws { + return try await withCheckedThrowingContinuation { continuation in + signUser(withUserId: userId) { + continuation.resume() + } failure: { error in + continuation.resume(throwing: error) + } + } + } + + func setup(withPassword password: String) async throws { + return try await withCheckedThrowingContinuation { continuation in + setup(withPassword: password) { + continuation.resume() + } failure: { error in + continuation.resume(throwing: error) } } } } +private extension MXRoom { + enum Error: Swift.Error { + case cannotSendMessage + } + + func sendTextMessage(_ text: String) async throws -> MXEvent { + var event: MXEvent? + _ = try await performCallbackRequest { + sendTextMessage(text, localEcho: &event, completion: $0) + } + + guard let event else { + throw Error.cannotSendMessage + } + return event + } +} + +extension MXCryptoMigrationV2Tests: Logger { + func log(logLine: String) { + MXLog.debug("[MXCryptoMigrationV2Tests]: \(logLine)") + } +} + #endif diff --git a/MatrixSDKTests/MatrixSDKTestsData.swift b/MatrixSDKTests/MatrixSDKTestsData.swift new file mode 100644 index 0000000000..10e506d374 --- /dev/null +++ b/MatrixSDKTests/MatrixSDKTestsData.swift @@ -0,0 +1,35 @@ +// +// 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 + +extension MatrixSDKTestsData { + enum Error: Swift.Error { + case missingDependencies + } + + func startMXSession() async throws -> MXSession { + return try await withCheckedThrowingContinuation { continuation in + doMXSessionTest(withAUser: nil) { session, _ in + guard let session else { + continuation.resume(throwing: Error.missingDependencies) + return + } + continuation.resume(returning: session) + } + } + } +} diff --git a/MatrixSDKTests/MatrixSDKTestsE2EData.swift b/MatrixSDKTests/MatrixSDKTestsE2EData.swift new file mode 100644 index 0000000000..4225087b1b --- /dev/null +++ b/MatrixSDKTests/MatrixSDKTestsE2EData.swift @@ -0,0 +1,52 @@ +// +// 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 + +extension MatrixSDKTestsE2EData { + enum Error: Swift.Error { + case missingDependencies + } + + class Environment { + let session: MXSession + let roomId: String + + init(session: MXSession, roomId: String) { + self.session = session + self.roomId = roomId + } + + deinit { + let session = session + DispatchQueue.main.async { + session.close() + } + } + } + + func startE2ETest() async throws -> Environment { + return try await withCheckedThrowingContinuation { continuation in + doE2ETestWithAlice(inARoom: nil) { session, roomId, _ in + guard let session, let roomId else { + continuation.resume(throwing: Error.missingDependencies) + return + } + continuation.resume(returning: .init(session: session, roomId: roomId)) + } + } + } +}