Skip to content

Commit 79e3d20

Browse files
committed
Merge branch 'release/0.26.7/master'
2 parents 44cfbde + 012e63c commit 79e3d20

File tree

10 files changed

+221
-51
lines changed

10 files changed

+221
-51
lines changed

CHANGES.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## Changes in 0.26.7 (2023-04-12)
2+
3+
🙌 Improvements
4+
5+
- Crypto: Upgrade Crypto SDK ([#1765](https://github.com/matrix-org/matrix-ios-sdk/pull/1765))
6+
7+
🐛 Bugfixes
8+
9+
- Crypto: Delete data for mismatched accounts ([#1763](https://github.com/matrix-org/matrix-ios-sdk/pull/1763))
10+
11+
112
## Changes in 0.26.6 (2023-04-04)
213

314
🙌 Improvements

MatrixSDK.podspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "MatrixSDK"
4-
s.version = "0.26.6"
4+
s.version = "0.26.7"
55
s.summary = "The iOS SDK to build apps compatible with Matrix (https://www.matrix.org)"
66

77
s.description = <<-DESC
@@ -45,7 +45,7 @@ Pod::Spec.new do |s|
4545
ss.dependency 'OLMKit', '~> 3.2.5'
4646
ss.dependency 'Realm', '10.27.0'
4747
ss.dependency 'libbase58', '~> 0.1.4'
48-
ss.dependency 'MatrixSDKCrypto', '0.3.2', :configurations => ["DEBUG", "RELEASE"], :inhibit_warnings => true
48+
ss.dependency 'MatrixSDKCrypto', '0.3.3', :configurations => ["DEBUG", "RELEASE"], :inhibit_warnings => true
4949
end
5050

5151
s.subspec 'JingleCallStack' do |ss|

MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmDecryption.m

+3-2
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,14 @@ - (MXEventDecryptionDecorationColor)decryptionColorForEvent:(MXEvent *)event
167167
{
168168
if (event.sender && [crypto trustLevelForUser:event.sender].isVerified)
169169
{
170-
MXDeviceInfo *deviceInfo = [crypto eventDeviceInfo:event];
170+
NSString *algorithm = event.wireContent[@"algorithm"];
171+
MXDeviceInfo *deviceInfo = [crypto.deviceList deviceWithIdentityKey:decryptionResult.senderKey andAlgorithm:algorithm];
171172
if (!deviceInfo.trustLevel.isVerified)
172173
{
173174
return MXEventDecryptionDecorationColorRed;
174175
}
175176
}
176-
177+
177178
return decryptionResult.isUntrusted ? MXEventDecryptionDecorationColorGrey : MXEventDecryptionDecorationColorNone;
178179
}
179180

MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift

+43-12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class MXCryptoMachine {
3636
}
3737

3838
private static let kdfRounds: Int32 = 500_000
39+
// Error type will be moved to rust sdk
40+
private static let MismatchedAccountError = "the account in the store doesn't match the account in the constructor"
3941

4042
enum Error: Swift.Error {
4143
case invalidEvent
@@ -72,17 +74,7 @@ class MXCryptoMachine {
7274
) throws {
7375
MXCryptoSDKLogger.shared.log(logLine: "Starting logs")
7476

75-
let url = try MXCryptoMachineStore.createStoreURLIfNecessary(for: userId)
76-
let passphrase = try MXCryptoMachineStore.storePassphrase()
77-
log.debug("Opening crypto store at \(url.path)/matrix-sdk-crypto.sqlite3") // Hardcoding path to db for debugging purpose
78-
79-
machine = try OlmMachine(
80-
userId: userId,
81-
deviceId: deviceId,
82-
path: url.path,
83-
passphrase: passphrase
84-
)
85-
77+
self.machine = try Self.createMachine(userId: userId, deviceId: deviceId, log: log)
8678
self.requests = MXCryptoRequests(restClient: restClient)
8779
self.getRoomAction = getRoomAction
8880

@@ -119,7 +111,46 @@ class MXCryptoMachine {
119111

120112
func deleteAllData() throws {
121113
let url = try MXCryptoMachineStore.storeURL(for: userId)
122-
try FileManager.default.removeItem(at: url)
114+
if FileManager.default.fileExists(atPath: url.path) {
115+
try FileManager.default.removeItem(at: url)
116+
}
117+
}
118+
119+
// MARK: - Private
120+
121+
private static func createMachine(userId: String, deviceId: String, log: MXNamedLog) throws -> OlmMachine {
122+
let url = try MXCryptoMachineStore.createStoreURLIfNecessary(for: userId)
123+
let passphrase = try MXCryptoMachineStore.storePassphrase()
124+
125+
log.debug("Opening crypto store at \(url.path)/matrix-sdk-crypto.sqlite3") // Hardcoding full path to db for debugging purposes
126+
127+
do {
128+
return try OlmMachine(
129+
userId: userId,
130+
deviceId: deviceId,
131+
path: url.path,
132+
passphrase: passphrase
133+
)
134+
} catch {
135+
// If we cannot open machine due to a mismatched account, delete previous data and try again
136+
if case CryptoStoreError.CryptoStore(let message) = error,
137+
message.contains(Self.MismatchedAccountError) {
138+
log.error("Credentials of the account do not match, deleting previous data", context: [
139+
"error": message
140+
])
141+
try FileManager.default.removeItem(at: url)
142+
return try OlmMachine(
143+
userId: userId,
144+
deviceId: deviceId,
145+
path: url.path,
146+
passphrase: passphrase
147+
)
148+
149+
// Otherwise re-throw the error
150+
} else {
151+
throw error
152+
}
153+
}
123154
}
124155
}
125156

MatrixSDK/Crypto/MXCryptoV2.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ class MXCryptoV2: NSObject, MXCrypto {
197197
}
198198

199199
if deleteStore {
200-
if let credentials = session?.credentials {
200+
if let credentials = session?.credentials,
201+
MXRealmCryptoStore.hasData(for: credentials) {
201202
MXRealmCryptoStore.delete(with: credentials)
202203
} else {
203204
log.failure("Missing credentials, cannot delete store")

MatrixSDK/MatrixSDKVersion.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19-
NSString *const MatrixSDKVersion = @"0.26.6";
19+
NSString *const MatrixSDKVersion = @"0.26.7";

MatrixSDKTests/Crypto/Algorithms/Megolm/MXMegolmDecryptionUnitTests.swift

+62-16
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ class MXMegolmDecryptionUnitTests: XCTestCase {
9090
private let device: MXOlmDevice
9191
private let cryptoStore: MXCryptoStore
9292
private let session: MXSession
93+
private let devices: MXDeviceList
9394

94-
init(device: MXOlmDevice, store: MXCryptoStore, session: MXSession) {
95+
init(device: MXOlmDevice, store: MXCryptoStore, session: MXSession, devices: MXDeviceList) {
9596
self.device = device
9697
self.cryptoStore = store
9798
self.session = session
99+
self.devices = devices
98100
}
99101

100102
override var olmDevice: MXOlmDevice! {
@@ -113,15 +115,22 @@ class MXMegolmDecryptionUnitTests: XCTestCase {
113115
return session
114116
}
115117

118+
override var deviceList: MXDeviceList! {
119+
return devices
120+
}
121+
116122
override func trustLevel(forUser userId: String) -> MXUserTrustLevel {
117123
return MXUserTrustLevel(crossSigningVerified: true, locallyVerified: true)
118124
}
119-
120-
var stubbedDeviceInfo: Device?
121-
override func eventDeviceInfo(_ event: MXEvent) -> MXDeviceInfo? {
122-
guard let device = stubbedDeviceInfo else {
125+
}
126+
127+
class DeviceListStub: MXDeviceList {
128+
var stubbedDevices = [String: Device]()
129+
override func device(withIdentityKey senderKey: String!, andAlgorithm algorithm: String!) -> MXDeviceInfo! {
130+
guard algorithm != nil, let senderKey = senderKey, let device = stubbedDevices[senderKey] else {
123131
return nil
124132
}
133+
125134
return .init(device: .init(device: device))
126135
}
127136
}
@@ -136,6 +145,7 @@ class MXMegolmDecryptionUnitTests: XCTestCase {
136145
var store: CryptoStoreStub!
137146
var session: SessionStub!
138147
var crypto: CryptoStub!
148+
var devicesList: DeviceListStub!
139149
var decryption: MXMegolmDecryption!
140150

141151
override func setUp() {
@@ -144,7 +154,8 @@ class MXMegolmDecryptionUnitTests: XCTestCase {
144154
device = DeviceStub()
145155
store = CryptoStoreStub()
146156
session = SessionStub()
147-
crypto = CryptoStub(device: device, store: store, session: session)
157+
devicesList = DeviceListStub()
158+
crypto = CryptoStub(device: device, store: store, session: session, devices: devicesList)
148159
decryption = MXMegolmDecryption(crypto: crypto)
149160
}
150161

@@ -247,10 +258,14 @@ class MXMegolmDecryptionUnitTests: XCTestCase {
247258
func test_decryptEvent_untrustedResult() {
248259
let event = MXEvent.encryptedFixture()
249260
device.stubbedResult = MXDecryptionResult()
250-
crypto.stubbedDeviceInfo = Device.stub(
251-
locallyTrusted: false,
252-
crossSigningTrusted: true
253-
)
261+
device.stubbedResult?.senderKey = "ABCD"
262+
263+
devicesList.stubbedDevices = [
264+
"ABCD": Device.stub(
265+
locallyTrusted: false,
266+
crossSigningTrusted: true
267+
)
268+
]
254269

255270
device.stubbedResult?.isUntrusted = true
256271
let result1 = decryption.decryptEvent(event, inTimeline: nil)
@@ -264,18 +279,49 @@ class MXMegolmDecryptionUnitTests: XCTestCase {
264279
func test_decryptEvent_untrustedDevice() {
265280
let event = MXEvent.encryptedFixture()
266281
device.stubbedResult = MXDecryptionResult()
282+
device.stubbedResult?.senderKey = "XYZ"
267283

268-
crypto.stubbedDeviceInfo = Device.stub(
269-
locallyTrusted: false,
270-
crossSigningTrusted: false
271-
)
284+
devicesList.stubbedDevices = [
285+
"XYZ": Device.stub(
286+
locallyTrusted: false,
287+
crossSigningTrusted: false
288+
)
289+
]
272290
let result1 = decryption.decryptEvent(event, inTimeline: nil)
273291
XCTAssertEqual(result1?.decoration.color, .red)
274292

275-
crypto.stubbedDeviceInfo = Device.stub(
276-
locallyTrusted: false,
293+
devicesList.stubbedDevices = [
294+
"XYZ": Device.stub(
295+
locallyTrusted: false,
296+
crossSigningTrusted: true
297+
)
298+
]
299+
let result2 = decryption.decryptEvent(event, inTimeline: nil)
300+
XCTAssertEqual(result2?.decoration.color, MXEventDecryptionDecorationColor.none)
301+
}
302+
303+
func test_decryptEvent_unknownDevice() {
304+
let invalidKey = "123"
305+
let validKey = "456"
306+
307+
let event = MXEvent.encryptedFixture()
308+
device.stubbedResult = MXDecryptionResult()
309+
device.stubbedResult?.senderKey = validKey
310+
311+
let stub = Device.stub(
312+
locallyTrusted: true,
277313
crossSigningTrusted: true
278314
)
315+
316+
devicesList.stubbedDevices = [
317+
invalidKey: stub
318+
]
319+
let result1 = decryption.decryptEvent(event, inTimeline: nil)
320+
XCTAssertEqual(result1?.decoration.color, .red)
321+
322+
devicesList.stubbedDevices = [
323+
validKey: stub
324+
]
279325
let result2 = decryption.decryptEvent(event, inTimeline: nil)
280326
XCTAssertEqual(result2?.decoration.color, MXEventDecryptionDecorationColor.none)
281327
}

0 commit comments

Comments
 (0)