From ab27bf67d7b7cd514123d1a0c2ad777fadb8efcc Mon Sep 17 00:00:00 2001 From: Valere Date: Fri, 29 Sep 2023 09:21:27 +0200 Subject: [PATCH] Fix QR login support with cryptoV2 --- .../CryptoMachine/MXCryptoMachine.swift | 17 ++++++++ .../CryptoMachine/MXCryptoProtocols.swift | 2 + MatrixSDK/Crypto/MXCryptoV2.swift | 42 ++++++++++++++----- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift b/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift index da4f1ff2f..8ef5d6fc4 100644 --- a/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift +++ b/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift @@ -613,6 +613,23 @@ extension MXCryptoMachine: MXCryptoCrossSigning { log.error("Failed importing cross signing keys", context: error) } } + + func queryMissingSecretsFromOtherSessions() async throws { + let has_missing = try machine.queryMissingSecretsFromOtherSessions() + + if (has_missing) { + // Out-of-sync check if there are any secret request to sent out as a result of + // the missing secret request + for request in try machine.outgoingRequests() { + if case .toDevice(_, let eventType, _) = request { + if (eventType == kMXEventTypeStringSecretRequest) { + try await handleRequest(request) + } + } + } + } + } + } extension MXCryptoMachine: MXCryptoVerifying { diff --git a/MatrixSDK/Crypto/CryptoMachine/MXCryptoProtocols.swift b/MatrixSDK/Crypto/CryptoMachine/MXCryptoProtocols.swift index 2a53e44f4..b9b4dabc7 100644 --- a/MatrixSDK/Crypto/CryptoMachine/MXCryptoProtocols.swift +++ b/MatrixSDK/Crypto/CryptoMachine/MXCryptoProtocols.swift @@ -90,6 +90,8 @@ protocol MXCryptoCrossSigning: MXCryptoUserIdentitySource, MXCryptoDevicesSource func bootstrapCrossSigning(authParams: [AnyHashable: Any]) async throws func exportCrossSigningKeys() -> CrossSigningKeyExport? func importCrossSigningKeys(export: CrossSigningKeyExport) + + func queryMissingSecretsFromOtherSessions() async throws } /// Verification functionality diff --git a/MatrixSDK/Crypto/MXCryptoV2.swift b/MatrixSDK/Crypto/MXCryptoV2.swift index 60fbda52b..a0134a1bd 100644 --- a/MatrixSDK/Crypto/MXCryptoV2.swift +++ b/MatrixSDK/Crypto/MXCryptoV2.swift @@ -387,18 +387,39 @@ class MXCryptoV2: NSObject, MXCrypto { case .verified: // If we want to set verified status, we will manually verify the device, // including uploading relevant signatures + try? machine.setLocalTrust(userId: machine.userId, deviceId: deviceId, trust: .verified) - Task { - do { - try await machine.verifyDevice(userId: userId, deviceId: deviceId) - log.debug("Successfully marked device as verified") - await MainActor.run { - success?() + if (userId == machine.userId) { + if (machine.crossSigningStatus().hasSelfSigning) { + // if we can cross sign, upload a new signature for that device + Task { + do { + // This method will always fail if the device belongs to someone else. + // XXX Should update API? and remove the userId? + try await machine.verifyDevice(userId: userId, deviceId: deviceId) + log.debug("Successfully marked device as verified") + await MainActor.run { + success?() + } + } catch { + log.error("Failed marking device as verified", context: error) + await MainActor.run { + failure?(error) + } + } } - } catch { - log.error("Failed marking device as verified", context: error) - await MainActor.run { - failure?(error) + } else { + // It's a good time to request secrets + Task { + do { + try await machine.queryMissingSecretsFromOtherSessions() + await MainActor.run { + success?() + } + } catch { + log.error("Failed to query missing secrets", context: error) + failure?(error) + } } } } @@ -409,6 +430,7 @@ class MXCryptoV2: NSObject, MXCrypto { do { try machine.setLocalTrust(userId: userId, deviceId: deviceId, trust: localTrust) log.debug("Successfully set local trust to \(localTrust)") + // XXX: Why no MainActor.run here? success?() } catch { log.error("Failed setting local trust", context: error)