Skip to content

Commit

Permalink
Merge pull request #1724 from matrix-org/andy/crypto_logs
Browse files Browse the repository at this point in the history
Add more crypto logs
  • Loading branch information
Anderas authored Feb 24, 2023
2 parents 395bb44 + a328347 commit 29e3713
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,17 @@ struct MXRoomEventEncryption: MXRoomEventEncrypting {
private func ensureRoomEncryption(roomId: String, algorithm: String?) throws {
let existingAlgorithm = legacyStore.algorithm(forRoom: roomId)
if existingAlgorithm != nil && existingAlgorithm == algorithm {
log.debug("Encryption in room is already set to the correct algorithm")
// Encryption in room is already set to the correct algorithm
return
}

guard let algorithm = algorithm else {
log.error("Resetting encryption is not allowed")
throw Error.invalidEncryptionAlgorithm
if existingAlgorithm != nil {
log.error("Resetting encryption is not allowed")
return
} else {
throw Error.invalidEncryptionAlgorithm
}
}

let supportedAlgorithms = Set([kMXCryptoMegolmAlgorithm])
Expand Down
15 changes: 10 additions & 5 deletions MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ extension MXCryptoMachine: MXCryptoSyncing {
// MARK: - Private

private func handleRequest(_ request: Request) async throws {
log.debug("Handling request `\(request.type)`")
log.debug("Handling `\(request.type)` request")

switch request {
case .toDevice(let requestId, let eventType, let body):
Expand All @@ -240,9 +240,14 @@ extension MXCryptoMachine: MXCryptoSyncing {
try markRequestAsSent(requestId: requestId, requestType: .keysQuery, response: response.jsonString())

case .keysClaim(let requestId, let oneTimeKeys):
log.debug("Claiming keys \(oneTimeKeys)")

let response = try await requests.claimKeys(
request: .init(oneTimeKeys: oneTimeKeys)
)

let dictionary = response.jsonDictionary() as? [String: Any] ?? [:]
log.debug("Keys claimed\n\(dictionary)")
try markRequestAsSent(requestId: requestId, requestType: .keysClaim, response: response.jsonString())

case .keysBackup(let requestId, let version, let rooms):
Expand Down Expand Up @@ -387,11 +392,9 @@ extension MXCryptoMachine: MXCryptoRoomEventEncrypting {
try await self?.getMissingSessions(users: users)
}

log.debug("Acquiring lock for room \(roomId)")
let roomQueue = await roomQueues.getQueue(for: roomId)

try await roomQueue.sync { [weak self] in
self?.log.debug("Sharing room keys")
try await self?.shareRoomKey(roomId: roomId, users: users, settings: settings)
}
}
Expand Down Expand Up @@ -420,13 +423,13 @@ extension MXCryptoMachine: MXCryptoRoomEventEncrypting {
// MARK: - Private

private func getMissingSessions(users: [String]) async throws {
log.debug("Getting missing sessions for \(users.count) user(s)")
log.debug("Checking missing olm sessions for \(users.count) user(s): \(users)")

guard
let request = try machine.getMissingSessions(users: users),
case .keysClaim = request
else {
log.debug("No sessions are missing")
log.debug("No olm sessions are missing")
return
}

Expand All @@ -435,6 +438,8 @@ extension MXCryptoMachine: MXCryptoRoomEventEncrypting {
}

private func shareRoomKey(roomId: String, users: [String], settings: EncryptionSettings) async throws {
log.debug("Checking unshared room keys")

let requests = try machine.shareRoomKey(roomId: roomId, users: users, settings: settings)
guard !requests.isEmpty else {
log.debug("There are no new keys to share")
Expand Down
3 changes: 0 additions & 3 deletions MatrixSDK/Crypto/MXCryptoV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,9 @@ class MXCryptoV2: NSObject, MXCrypto {
success: (() -> Void)?,
failure: ((Swift.Error) -> Void)?
) -> MXHTTPOperation? {
log.debug("->")

Task {
do {
try await encryptor.ensureRoomKeysShared(roomId: roomId)
log.debug("Room keys shared when necessary")
await MainActor.run {
success?()
}
Expand Down

0 comments on commit 29e3713

Please sign in to comment.