Skip to content

Commit

Permalink
Merge branch 'develop' into andy/cached_megolm
Browse files Browse the repository at this point in the history
  • Loading branch information
Anderas committed Aug 30, 2022
2 parents b3b481e + 9d6ef50 commit cc53e14
Show file tree
Hide file tree
Showing 27 changed files with 947 additions and 355 deletions.
6 changes: 6 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,8 @@
EDD578EA2881C37C006739DD /* MXCryptoUserIdentityWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDD578E02881C37C006739DD /* MXCryptoUserIdentityWrapper.swift */; };
EDD578EC2881C38C006739DD /* MXCrossSigningV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDD578EB2881C38C006739DD /* MXCrossSigningV2.swift */; };
EDD578ED2881C38C006739DD /* MXCrossSigningV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDD578EB2881C38C006739DD /* MXCrossSigningV2.swift */; };
EDE1B13B28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDE1B13A28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift */; };
EDE1B13C28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDE1B13A28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift */; };
EDF1B6902876CD2C00BBBCEE /* MXTaskQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDF1B68F2876CD2C00BBBCEE /* MXTaskQueue.swift */; };
EDF1B6912876CD2C00BBBCEE /* MXTaskQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDF1B68F2876CD2C00BBBCEE /* MXTaskQueue.swift */; };
EDF1B6932876CD8600BBBCEE /* MXTaskQueueUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDF1B6922876CD8600BBBCEE /* MXTaskQueueUnitTests.swift */; };
Expand Down Expand Up @@ -2963,6 +2965,7 @@
EDD578DF2881C37C006739DD /* MXCryptoDeviceWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXCryptoDeviceWrapper.swift; sourceTree = "<group>"; };
EDD578E02881C37C006739DD /* MXCryptoUserIdentityWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXCryptoUserIdentityWrapper.swift; sourceTree = "<group>"; };
EDD578EB2881C38C006739DD /* MXCrossSigningV2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXCrossSigningV2.swift; sourceTree = "<group>"; };
EDE1B13A28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXCrossSigningV2UnitTests.swift; sourceTree = "<group>"; };
EDF1B68F2876CD2C00BBBCEE /* MXTaskQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXTaskQueue.swift; sourceTree = "<group>"; };
EDF1B6922876CD8600BBBCEE /* MXTaskQueueUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXTaskQueueUnitTests.swift; sourceTree = "<group>"; };
EDF4678627E3331D00435913 /* EventsEnumeratorDataSourceStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventsEnumeratorDataSourceStub.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5259,6 +5262,7 @@
children = (
ED8F1D1528857FDA00F897E7 /* Data */,
ED8F1D242885A39800F897E7 /* MXCrossSigningInfoSourceUnitTests.swift */,
EDE1B13A28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift */,
);
path = CrossSigning;
sourceTree = "<group>";
Expand Down Expand Up @@ -6945,6 +6949,7 @@
32792BE12296C64200F4FC9D /* MXAggregatedEditsTests.m in Sources */,
329571931B0240CE00ABB3BA /* MXVoIPTests.m in Sources */,
ED8F1D322885AC5700F897E7 /* Device+Stub.swift in Sources */,
EDE1B13B28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift in Sources */,
EC746C56274E5197002AD24C /* MXThreadingServiceUnitTests.swift in Sources */,
ED8F1D252885A39800F897E7 /* MXCrossSigningInfoSourceUnitTests.swift in Sources */,
32A27D1F19EC335300BAFADE /* MXRoomTests.m in Sources */,
Expand Down Expand Up @@ -7543,6 +7548,7 @@
B1E09A332397FD750057C069 /* MXRoomStateTests.m in Sources */,
18937E7D273A5AE500902626 /* MXPollRelationTests.m in Sources */,
B1E09A352397FD7D0057C069 /* MXEventTests.m in Sources */,
EDE1B13C28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift in Sources */,
A816248525F60D0300A46F05 /* MXDeviceListOperationsPoolUnitTests.swift in Sources */,
EC746C57274E5197002AD24C /* MXThreadingServiceUnitTests.swift in Sources */,
B1E09A312397FD750057C069 /* MXSessionTests.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "MXCrossSigningKey.h"

#import "MXKey.h"
#import "MXCryptoConstants.h"


#pragma mark - Constants
Expand Down
1 change: 1 addition & 0 deletions MatrixSDK/Crypto/CrossSigning/MXCrossSigningTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#import "MXCryptoTools.h"
#import "MXKey.h"
#import "MXCryptoConstants.h"


#pragma mark - Constants
Expand Down
46 changes: 36 additions & 10 deletions MatrixSDK/Crypto/CrossSigning/MXCrossSigningV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ class MXCrossSigningV2: MXCrossSigning {
}

override var state: MXCrossSigningState {
log.debug("Only partial implementation")
return hasAllPrivateKeys ? .canCrossSign : .notBootstrapped
if hasAllPrivateKeys {
return .canCrossSign
} else if let info = info {
if info.trustLevel.isVerified {
return .trustCrossSigning
} else {
return .crossSigningExists
}
} else {
return .notBootstrapped
}
}

override var canTrustCrossSigning: Bool {
Expand All @@ -46,16 +55,20 @@ class MXCrossSigningV2: MXCrossSigning {
}

override var hasAllPrivateKeys: Bool {
let status = machine.crossSigningStatus()
let status = crossSigning.crossSigningStatus()
return status.hasMaster && status.hasSelfSigning && status.hasUserSigning
}

private let machine: MXCryptoMachine
private let crossSigning: MXCryptoCrossSigning
private let infoSource: MXCrossSigningInfoSource
private var info: MXCrossSigningInfo?
private let restClient: MXRestClient

private let log = MXNamedLog(name: "MXCrossSigningV2")

init(machine: MXCryptoMachine, restClient: MXRestClient) {
self.machine = machine
init(crossSigning: MXCryptoCrossSigning, restClient: MXRestClient) {
self.crossSigning = crossSigning
self.infoSource = MXCrossSigningInfoSource(source: crossSigning)
self.restClient = restClient
}

Expand All @@ -67,7 +80,7 @@ class MXCrossSigningV2: MXCrossSigning {
Task {
do {
let authParams = try await authParameters(password: password)
try await machine.bootstrapCrossSigning(authParams: authParams)
try await crossSigning.bootstrapCrossSigning(authParams: authParams)
await MainActor.run {
success()
}
Expand All @@ -87,7 +100,7 @@ class MXCrossSigningV2: MXCrossSigning {
) {
Task {
do {
try await machine.bootstrapCrossSigning(authParams: authParams)
try await crossSigning.bootstrapCrossSigning(authParams: authParams)
await MainActor.run {
success()
}
Expand All @@ -104,8 +117,21 @@ class MXCrossSigningV2: MXCrossSigning {
success: ((Bool) -> Void)?,
failure: ((Swift.Error) -> Void)? = nil
) {
log.debug("Not implemented")
success?(true)
Task {
do {
try await crossSigning.downloadKeys(users: [crossSigning.userId])
info = infoSource.crossSigningInfo(userId: crossSigning.userId)

await MainActor.run {
success?(true)
}
} catch {
log.error("Cannot refresh cross signing state", context: error)
await MainActor.run {
failure?(error)
}
}
}
}

override func crossSignDevice(
Expand Down
146 changes: 100 additions & 46 deletions MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,41 @@ class MXCryptoMachine {
}
}

@available(iOS 13.0.0, *)
extension MXCryptoMachine: MXCryptoIdentity {
var userId: String {
return machine.userId()
}

var deviceId: String {
return machine.deviceId()
}

var deviceCurve25519Key: String? {
guard let key = machine.identityKeys()[kMXKeyCurve25519Type] else {
log.error("Cannot get device curve25519 key")
return nil
}
return key
}

var deviceEd25519Key: String? {
guard let key = machine.identityKeys()[kMXKeyEd25519Type] else {
log.error("Cannot get device ed25519 key")
return nil
}
return key
}
}

@available(iOS 13.0.0, *)
extension MXCryptoMachine: MXCryptoSyncing {
func handleSyncResponse(
toDevice: MXToDeviceSyncResponse?,
deviceLists: MXDeviceListResponse?,
deviceOneTimeKeysCounts: [String: NSNumber],
unusedFallbackKeys: [String]?
) throws {
) throws -> MXToDeviceSyncResponse {
let events = toDevice?.jsonString() ?? "[]"
let deviceChanges = DeviceLists(
changed: deviceLists?.changed ?? [],
Expand All @@ -120,9 +147,13 @@ extension MXCryptoMachine: MXCryptoSyncing {
unusedFallbackKeys: unusedFallbackKeys
)

if let result = MXTools.deserialiseJSONString(result) as? [String: Any], !result.isEmpty {
log(error: "Result processing not implemented \(result)")
guard let json = MXTools.deserialiseJSONString(result) as? [AnyHashable: Any] else {
log.error("Result cannot be serialized", context: [
"result": result
])
return MXToDeviceSyncResponse()
}
return MXToDeviceSyncResponse(fromJSON: json)
}

func completeSync() async throws {
Expand Down Expand Up @@ -215,22 +246,6 @@ extension MXCryptoMachine: MXCryptoSyncing {

@available(iOS 13.0.0, *)
extension MXCryptoMachine: MXCryptoDevicesSource {
var deviceCurve25519Key: String? {
guard let key = machine.identityKeys()["curve25519"] else {
log.error("Cannot get device curve25519 key")
return nil
}
return key
}

var deviceEd25519Key: String? {
guard let key = machine.identityKeys()["ed25519"] else {
log.error("Cannot get device ed25519 key")
return nil
}
return key
}

func devices(userId: String) -> [Device] {
do {
return try machine.getUserDevices(userId: userId, timeout: 0)
Expand Down Expand Up @@ -269,6 +284,12 @@ extension MXCryptoMachine: MXCryptoUserIdentitySource {
return nil
}
}

func downloadKeys(users: [String]) async throws {
try await handleRequest(
.keysQuery(requestId: UUID().uuidString, users: users)
)
}
}

@available(iOS 13.0.0, *)
Expand Down Expand Up @@ -371,7 +392,15 @@ extension MXCryptoMachine: MXCryptoCrossSigning {
}

@available(iOS 13.0.0, *)
extension MXCryptoMachine: MXCryptoVerification {
extension MXCryptoMachine: MXCryptoVerificationRequesting {
func requestSelfVerification(methods: [String]) async throws -> VerificationRequest {
guard let result = try machine.requestSelfVerification(methods: methods) else {
throw Error.missingVerification
}
try await handleOutgoingVerificationRequest(result.request)
return result.verification
}

func requestVerification(userId: String, roomId: String, methods: [String]) async throws -> VerificationRequest {
guard let content = try machine.verificationRequestContent(userId: userId, methods: methods) else {
throw Error.missingVerificationContent
Expand Down Expand Up @@ -403,18 +432,47 @@ extension MXCryptoMachine: MXCryptoVerification {
return machine.getVerificationRequest(userId: userId, flowId: flowId)
}

func verification(userId: String, flowId: String) -> Verification? {
return machine.getVerification(userId: userId, flowId: flowId)
func acceptVerificationRequest(userId: String, flowId: String, methods: [String]) async throws {
guard let request = machine.acceptVerificationRequest(userId: userId, flowId: flowId, methods: methods) else {
throw Error.missingVerificationRequest
}
try await handleOutgoingVerificationRequest(request)
}

func beginSasVerification(userId: String, flowId: String) async throws -> Sas {
guard let result = try machine.startSasVerification(userId: userId, flowId: flowId) else {
throw Error.missingVerification
func cancelVerification(userId: String, flowId: String, cancelCode: String) async throws {
guard let request = machine.cancelVerification(userId: userId, flowId: flowId, cancelCode: cancelCode) else {
throw Error.cannotCancelVerification
}
try await handleOutgoingVerificationRequest(result.request)
return result.sas
try await handleOutgoingVerificationRequest(request)
}

// MARK: - Private

private func handleOutgoingVerificationRequest(_ request: OutgoingVerificationRequest) async throws {
switch request {
case .toDevice(_, let eventType, let body):
try await requests.sendToDevice(
request: .init(
eventType: eventType,
body: body
)
)
case .inRoom(_, let roomId, let eventType, let content):
let _ = try await sendRoomMessage(
roomId: roomId,
eventType: eventType,
content: content
)
}
}
}

@available(iOS 13.0.0, *)
extension MXCryptoMachine: MXCryptoVerifying {
func verification(userId: String, flowId: String) -> Verification? {
return machine.getVerification(userId: userId, flowId: flowId)
}

func confirmVerification(userId: String, flowId: String) async throws {
let result = try machine.confirmVerification(userId: userId, flowId: flowId)
guard let result = result else {
Expand All @@ -435,10 +493,21 @@ extension MXCryptoMachine: MXCryptoVerification {
try await group.waitForAll()
}
}
}

@available(iOS 13.0.0, *)
extension MXCryptoMachine: MXCryptoSASVerifying {
func startSasVerification(userId: String, flowId: String) async throws -> Sas {
guard let result = try machine.startSasVerification(userId: userId, flowId: flowId) else {
throw Error.missingVerification
}
try await handleOutgoingVerificationRequest(result.request)
return result.sas
}

func cancelVerification(userId: String, flowId: String, cancelCode: String) async throws {
guard let request = machine.cancelVerification(userId: userId, flowId: flowId, cancelCode: cancelCode) else {
throw Error.cannotCancelVerification
func acceptSasVerification(userId: String, flowId: String) async throws {
guard let request = machine.acceptSasVerification(userId: userId, flowId: flowId) else {
throw Error.missingVerification
}
try await handleOutgoingVerificationRequest(request)
}
Expand All @@ -449,21 +518,6 @@ extension MXCryptoMachine: MXCryptoVerification {
}
return indexes.map(Int.init)
}

// MARK: - Private

private func handleOutgoingVerificationRequest(_ request: OutgoingVerificationRequest) async throws {
guard case .inRoom(let requestId, let roomId, let eventType, let content) = request else {
assertionFailure("Not yet implemented")
return
}

let _ = try await sendRoomMessage(
roomId: roomId,
eventType: eventType,
content: content
)
}
}

@available(iOS 13.0.0, *)
Expand Down
Loading

0 comments on commit cc53e14

Please sign in to comment.