diff --git a/MatrixSDK/Crypto/MXCrypto.h b/MatrixSDK/Crypto/MXCrypto.h index 56c49a99c7..6334d6b712 100644 --- a/MatrixSDK/Crypto/MXCrypto.h +++ b/MatrixSDK/Crypto/MXCrypto.h @@ -43,12 +43,6 @@ NS_ASSUME_NONNULL_BEGIN -FOUNDATION_EXPORT NSString *const MXCryptoErrorDomain; -typedef NS_ENUM(NSInteger, MXCryptoErrorCode) -{ - MXCryptoUnavailableErrorCode, -}; - /** Fires when we receive a room key request. @@ -402,9 +396,11 @@ MX_ASSUME_MISSING_NULLABILITY_BEGIN Create a new crypto instance and data for the given user. @param mxSession the session on which to enable crypto. + @param error pointer to error that is non-nil if crypto failed to be created @return the fresh crypto instance. */ -+ (id)createCryptoWithMatrixSession:(MXSession*)mxSession; ++ (id)createCryptoWithMatrixSession:(MXSession*)mxSession + error:(NSError **)error; /** Check if the user has previously enabled crypto. @@ -412,7 +408,8 @@ MX_ASSUME_MISSING_NULLABILITY_BEGIN @param complete a block called in any case when the operation completes. */ -+ (void)checkCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id crypto))complete; ++ (void)checkCryptoWithMatrixSession:(MXSession*)mxSession + complete:(void (^)(id crypto, NSError *error))complete; /** Stores the exportedOlmDevice related to the credentials into the store. diff --git a/MatrixSDK/Crypto/MXCrypto.m b/MatrixSDK/Crypto/MXCrypto.m index f669850b32..a14a6a0133 100644 --- a/MatrixSDK/Crypto/MXCrypto.m +++ b/MatrixSDK/Crypto/MXCrypto.m @@ -62,8 +62,6 @@ */ #define MXCryptoStoreClass MXRealmCryptoStore -NSString *const MXCryptoErrorDomain = @"org.matrix.sdk.crypto"; - NSString *const kMXCryptoRoomKeyRequestNotification = @"kMXCryptoRoomKeyRequestNotification"; NSString *const kMXCryptoRoomKeyRequestNotificationRequestKey = @"kMXCryptoRoomKeyRequestNotificationRequestKey"; NSString *const kMXCryptoRoomKeyRequestCancellationNotification = @"kMXCryptoRoomKeyRequestCancellationNotification"; @@ -153,6 +151,7 @@ @implementation MXLegacyCrypto @synthesize recoveryService = _recoveryService; + (id)createCryptoWithMatrixSession:(MXSession *)mxSession + error:(NSError **)error { __block id crypto; @@ -161,7 +160,7 @@ @implementation MXLegacyCrypto #if DEBUG if (MXSDKOptions.sharedInstance.enableCryptoV2) { - return [self createCryptoV2WithSession:mxSession]; + return [self createCryptoV2WithSession:mxSession error:error]; } #endif @@ -178,13 +177,16 @@ @implementation MXLegacyCrypto return crypto; } -+ (void)checkCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id crypto))complete ++ (void)checkCryptoWithMatrixSession:(MXSession *)mxSession + complete:(void (^)(id crypto, NSError *error))complete { #ifdef MX_CRYPTO #if DEBUG if (MXSDKOptions.sharedInstance.enableCryptoV2) { - complete([self createCryptoV2WithSession:mxSession]); + NSError *error; + id crypto = [self createCryptoV2WithSession:mxSession error:&error]; + complete(crypto, error); return; } #endif @@ -195,7 +197,7 @@ + (void)checkCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id #endif } -+ (void)checkLegacyCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id crypto))complete ++ (void)checkLegacyCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id crypto, NSError *error))complete { #ifdef MX_CRYPTO @@ -221,7 +223,7 @@ + (void)checkLegacyCryptoWithMatrixSession:(MXSession*)mxSession complete:(void id crypto = [[MXLegacyCrypto alloc] initWithMatrixSession:mxSession cryptoQueue:cryptoQueue andStore:cryptoStore]; dispatch_async(dispatch_get_main_queue(), ^{ - complete(crypto); + complete(crypto, nil); }); } failure:^(NSError *error) { @@ -229,7 +231,7 @@ + (void)checkLegacyCryptoWithMatrixSession:(MXSession*)mxSession complete:(void MXLogDebug(@"[MXCrypto] checkCryptoWithMatrixSession: Crypto store failed to open. Error: %@", error); dispatch_async(dispatch_get_main_queue(), ^{ - complete(nil); + complete(nil, error); }); }]; } @@ -245,21 +247,21 @@ + (void)checkLegacyCryptoWithMatrixSession:(MXSession*)mxSession complete:(void id crypto = [[MXLegacyCrypto alloc] initWithMatrixSession:mxSession cryptoQueue:cryptoQueue andStore:cryptoStore]; dispatch_async(dispatch_get_main_queue(), ^{ - complete(crypto); + complete(crypto, nil); }); } else { // Else do not enable crypto dispatch_async(dispatch_get_main_queue(), ^{ - complete(nil); + complete(nil, nil); }); } }); #else - complete(nil); + complete(nil, nil); #endif } diff --git a/MatrixSDK/Crypto/MXCryptoV2.swift b/MatrixSDK/Crypto/MXCryptoV2.swift index 0960ada1cb..03224b8873 100644 --- a/MatrixSDK/Crypto/MXCryptoV2.swift +++ b/MatrixSDK/Crypto/MXCryptoV2.swift @@ -19,19 +19,19 @@ import Foundation #if DEBUG public extension MXLegacyCrypto { /// Create a Rust-based work-in-progress implementation of `MXCrypto` - @objc static func createCryptoV2(session: MXSession!) -> MXCrypto? { + @objc static func createCryptoV2(session: MXSession!) throws -> MXCrypto { let log = MXNamedLog(name: "MXCryptoV2") - + guard let session = session else { log.failure("Cannot create crypto V2, missing session") - return nil + throw MXCryptoV2.Error.sessionNotAvailable } - + do { return try MXCryptoV2(session: session) } catch { log.failure("Error creating crypto V2", context: error) - return nil + throw error } } } @@ -45,6 +45,7 @@ import MatrixSDKCrypto /// under the hood. private class MXCryptoV2: NSObject, MXCrypto { enum Error: Swift.Error { + case sessionNotAvailable case missingCredentials case missingRoom case roomNotEncrypted diff --git a/MatrixSDK/MXSession.m b/MatrixSDK/MXSession.m index fd7d20da3f..66fd720ca5 100644 --- a/MatrixSDK/MXSession.m +++ b/MatrixSDK/MXSession.m @@ -398,16 +398,13 @@ -(void)setStore:(id)store success:(void (^)(void))onStoreDataReady fail // Check if the user has enabled crypto MXWeakify(self); - [MXLegacyCrypto checkCryptoWithMatrixSession:self complete:^(id crypto) { + [MXLegacyCrypto checkCryptoWithMatrixSession:self complete:^(id crypto, NSError *error) { MXStrongifyAndReturnIfNil(self); - if (!crypto) + if (!crypto && error) { if (failure) { - NSError *error = [NSError errorWithDomain:MXCryptoErrorDomain code:MXCryptoUnavailableErrorCode userInfo:@{ - NSLocalizedDescriptionKey: @"Encryption not available, please restart the app", - }]; failure(error); } return; @@ -2280,14 +2277,12 @@ - (void)enableCrypto:(BOOL)enableCrypto success:(void (^)(void))success failure: if (enableCrypto && !_crypto) { - _crypto = [MXLegacyCrypto createCryptoWithMatrixSession:self]; - if (!_crypto) + NSError *error; + _crypto = [MXLegacyCrypto createCryptoWithMatrixSession:self error:&error]; + if (!_crypto && error && failure) { if (failure) { - NSError *error = [NSError errorWithDomain:MXCryptoErrorDomain code:MXCryptoUnavailableErrorCode userInfo:@{ - NSLocalizedDescriptionKey: @"Encryption not available, please restart the app", - }]; failure(error); } return;