Skip to content

Commit 7399902

Browse files
authored
Merge pull request matrix-org#1854 from matrix-org/valere/NSE_partial_fix
Invalidate crypto store cache when entering foreground
2 parents 0c3ab70 + 758e1af commit 7399902

File tree

9 files changed

+40
-13
lines changed

9 files changed

+40
-13
lines changed

MatrixSDK.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.4.1', :configurations => ["DEBUG", "RELEASE"], :inhibit_warnings => true
48+
ss.dependency 'MatrixSDKCrypto', '0.4.2', :configurations => ["DEBUG", "RELEASE"], :inhibit_warnings => true
4949
end
5050

5151
s.subspec 'JingleCallStack' do |ss|

MatrixSDK/Crypto/Algorithms/RoomEvent/MXRoomEventDecryption.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,13 @@ actor MXRoomEventDecryption: MXRoomEventDecrypting {
227227
])
228228
return trackedDecryptionResult(for: event, error: error)
229229

230-
case .MissingRoomKey(let message):
230+
case .MissingRoomKey(let message, let withheldCode):
231231
if undecryptedEvents[sessionId] == nil {
232232
log.error("Failed to decrypt event(s) due to missing room keys", context: [
233233
"session_id": sessionId,
234234
"message": message,
235235
"error": error,
236+
"withheldCode": withheldCode ?? "N/A",
236237
"details": "further errors for the same key will be supressed",
237238
])
238239
}

MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ class MXCryptoMachine {
116116
}
117117
}
118118

119+
func invalidateCache() async {
120+
await machine.clearCryptoCache()
121+
}
122+
119123
// MARK: - Private
120124

121125
private static func createMachine(userId: String, deviceId: String, log: MXNamedLog) throws -> OlmMachine {

MatrixSDK/Crypto/Dehydration/DehydrationService.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ public class DehydrationService: NSObject {
8989
try await dehydrateDevice(pickleKeyData: pickleKeyData)
9090
} else { // Otherwise, generate a new dehydration pickle key, store it and dehydrate a device
9191
// Generate a new dehydration pickle key
92-
var pickleKeyData = Data(count: 32)
93-
_ = SecRandomCopyBytes(kSecRandomDefault, 32, &pickleKeyData)
92+
var pickleKeyRaw = [UInt8](repeating: 0, count: 32)
93+
_ = SecRandomCopyBytes(kSecRandomDefault, 32, &pickleKeyRaw)
94+
let pickleKeyData = Data(bytes: pickleKeyRaw, count: 32)
9495

9596
// Convert it to unpadded base 64
9697
let base64PickleKey = MXBase64Tools.unpaddedBase64(from: pickleKeyData)

MatrixSDK/Crypto/MXCrypto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ extern NSString *const MXDeviceListDidUpdateUsersDevicesNotification;
378378
*/
379379
- (void)setBlacklistUnverifiedDevicesInRoom:(NSString *)roomId blacklist:(BOOL)blacklist;
380380

381+
- (void) invalidateCache:(void (^)(void))done;
382+
381383
@end
382384

383385
NS_ASSUME_NONNULL_END

MatrixSDK/Crypto/MXCryptoV2.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import MatrixSDKCrypto
2020
/// An implementation of `MXCrypto` which uses [matrix-rust-sdk](https://github.com/matrix-org/matrix-rust-sdk/tree/main/crates/matrix-sdk-crypto)
2121
/// under the hood.
2222
class MXCryptoV2: NSObject, MXCrypto {
23+
2324
enum Error: Swift.Error {
2425
case cannotUnsetTrust
2526
case backupNotEnabled
@@ -720,4 +721,13 @@ class MXCryptoV2: NSObject, MXCrypto {
720721
return dict[info.userId] = info
721722
}
722723
}
723-
}
724+
725+
func invalidateCache(_ done: @escaping () -> Void) {
726+
Task {
727+
log.debug("Invalidating Olm Machine crypto store cache.")
728+
await machine.invalidateCache()
729+
await MainActor.run {
730+
done()
731+
}
732+
}
733+
}}

MatrixSDK/MXSession.m

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,9 +1150,18 @@ - (void)pause
11501150

11511151
- (void)resume:(void (^)(void))resumeDone
11521152
{
1153-
[self handleBackgroundSyncCacheIfRequiredWithCompletion:^{
1154-
[self _resume:resumeDone];
1155-
}];
1153+
// The app has resumed there might have been a NSE run that have invalidated the cache
1154+
if (self.crypto) {
1155+
[self.crypto invalidateCache:^{
1156+
[self handleBackgroundSyncCacheIfRequiredWithCompletion:^{
1157+
[self _resume:resumeDone];
1158+
}];
1159+
}];
1160+
} else {
1161+
[self handleBackgroundSyncCacheIfRequiredWithCompletion:^{
1162+
[self _resume:resumeDone];
1163+
}];
1164+
}
11561165
}
11571166

11581167
- (void)_resume:(void (^)(void))resumeDone

Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract_target 'MatrixSDK' do
1616

1717
pod 'Realm', '10.27.0'
1818
pod 'libbase58', '~> 0.1.4'
19-
pod 'MatrixSDKCrypto', '0.4.1', :inhibit_warnings => true
19+
pod 'MatrixSDKCrypto', '0.4.2', :inhibit_warnings => true
2020

2121
target 'MatrixSDK-iOS' do
2222
platform :ios, '13.0'

Podfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ PODS:
1616
- AFNetworking/NSURLSession
1717
- GZIP (1.3.2)
1818
- libbase58 (0.1.4)
19-
- MatrixSDKCrypto (0.4.1)
19+
- MatrixSDKCrypto (0.4.2)
2020
- OHHTTPStubs (9.1.0):
2121
- OHHTTPStubs/Default (= 9.1.0)
2222
- OHHTTPStubs/Core (9.1.0)
@@ -44,7 +44,7 @@ DEPENDENCIES:
4444
- AFNetworking (~> 4.0.0)
4545
- GZIP (~> 1.3.0)
4646
- libbase58 (~> 0.1.4)
47-
- MatrixSDKCrypto (= 0.4.1)
47+
- MatrixSDKCrypto (= 0.4.2)
4848
- OHHTTPStubs (~> 9.1.0)
4949
- OLMKit (~> 3.2.5)
5050
- Realm (= 10.27.0)
@@ -65,12 +65,12 @@ SPEC CHECKSUMS:
6565
AFNetworking: 3bd23d814e976cd148d7d44c3ab78017b744cd58
6666
GZIP: 3c0abf794bfce8c7cb34ea05a1837752416c8868
6767
libbase58: 7c040313537b8c44b6e2d15586af8e21f7354efd
68-
MatrixSDKCrypto: da2b8a81f7e1989fc61ff85ed6aad92332beeb40
68+
MatrixSDKCrypto: 736069ee0a5ec12852ab3498bf2242acecc443fc
6969
OHHTTPStubs: 90eac6d8f2c18317baeca36698523dc67c513831
7070
OLMKit: da115f16582e47626616874e20f7bb92222c7a51
7171
Realm: 9ca328bd7e700cc19703799785e37f77d1a130f2
7272
SwiftyBeaver: 84069991dd5dca07d7069100985badaca7f0ce82
7373

74-
PODFILE CHECKSUM: bce6f6e7af7aa0ac9a50d4f6594d923fc00ed168
74+
PODFILE CHECKSUM: 37ab0de0200808bcd3335a637e31736df60fc62e
7575

7676
COCOAPODS: 1.15.2

0 commit comments

Comments
 (0)