Skip to content

Commit

Permalink
Propagate error from crypto init
Browse files Browse the repository at this point in the history
  • Loading branch information
Anderas committed Dec 5, 2022
1 parent 779cf69 commit c9d7bfa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
13 changes: 5 additions & 8 deletions MatrixSDK/Crypto/MXCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -402,17 +396,20 @@ 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<MXCrypto>)createCryptoWithMatrixSession:(MXSession*)mxSession;
+ (id<MXCrypto>)createCryptoWithMatrixSession:(MXSession*)mxSession
error:(NSError **)error;

/**
Check if the user has previously enabled crypto.
If yes, init the crypto module.
@param complete a block called in any case when the operation completes.
*/
+ (void)checkCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id<MXCrypto> crypto))complete;
+ (void)checkCryptoWithMatrixSession:(MXSession*)mxSession
complete:(void (^)(id<MXCrypto> crypto, NSError *error))complete;

/**
Stores the exportedOlmDevice related to the credentials into the store.
Expand Down
24 changes: 13 additions & 11 deletions MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -153,6 +151,7 @@ @implementation MXLegacyCrypto
@synthesize recoveryService = _recoveryService;

+ (id<MXCrypto>)createCryptoWithMatrixSession:(MXSession *)mxSession
error:(NSError **)error
{
__block id<MXCrypto> crypto;

Expand All @@ -161,7 +160,7 @@ @implementation MXLegacyCrypto
#if DEBUG
if (MXSDKOptions.sharedInstance.enableCryptoV2)
{
return [self createCryptoV2WithSession:mxSession];
return [self createCryptoV2WithSession:mxSession error:error];
}
#endif

Expand All @@ -178,13 +177,16 @@ @implementation MXLegacyCrypto
return crypto;
}

+ (void)checkCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id<MXCrypto> crypto))complete
+ (void)checkCryptoWithMatrixSession:(MXSession *)mxSession
complete:(void (^)(id<MXCrypto> crypto, NSError *error))complete
{
#ifdef MX_CRYPTO
#if DEBUG
if (MXSDKOptions.sharedInstance.enableCryptoV2)
{
complete([self createCryptoV2WithSession:mxSession]);
NSError *error;
id<MXCrypto> crypto = [self createCryptoV2WithSession:mxSession error:&error];
complete(crypto, error);
return;
}
#endif
Expand All @@ -195,7 +197,7 @@ + (void)checkCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id
#endif
}

+ (void)checkLegacyCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id<MXCrypto> crypto))complete
+ (void)checkLegacyCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id<MXCrypto> crypto, NSError *error))complete
{
#ifdef MX_CRYPTO

Expand All @@ -221,15 +223,15 @@ + (void)checkLegacyCryptoWithMatrixSession:(MXSession*)mxSession complete:(void
id<MXCrypto> crypto = [[MXLegacyCrypto alloc] initWithMatrixSession:mxSession cryptoQueue:cryptoQueue andStore:cryptoStore];

dispatch_async(dispatch_get_main_queue(), ^{
complete(crypto);
complete(crypto, nil);
});

} failure:^(NSError *error) {

MXLogDebug(@"[MXCrypto] checkCryptoWithMatrixSession: Crypto store failed to open. Error: %@", error);

dispatch_async(dispatch_get_main_queue(), ^{
complete(nil);
complete(nil, error);
});
}];
}
Expand All @@ -245,21 +247,21 @@ + (void)checkLegacyCryptoWithMatrixSession:(MXSession*)mxSession complete:(void
id<MXCrypto> 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
}

Expand Down
18 changes: 13 additions & 5 deletions MatrixSDK/Crypto/MXCryptoV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,28 @@ import Foundation

#if DEBUG
public extension MXLegacyCrypto {
enum CryptoError: Swift.Error, LocalizedError {
case cryptoNotAvailable

public var errorDescription: String? {
return "Encryption not available, please restart the app"
}
}

/// 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 CryptoError.cryptoNotAvailable
}

do {
return try MXCryptoV2(session: session)
} catch {
log.failure("Error creating crypto V2", context: error)
return nil
throw CryptoError.cryptoNotAvailable
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,13 @@ -(void)setStore:(id<MXStore>)store success:(void (^)(void))onStoreDataReady fail

// Check if the user has enabled crypto
MXWeakify(self);
[MXLegacyCrypto checkCryptoWithMatrixSession:self complete:^(id<MXCrypto> crypto) {
[MXLegacyCrypto checkCryptoWithMatrixSession:self complete:^(id<MXCrypto> 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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c9d7bfa

Please sign in to comment.