Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail launch when unavailable crypto #1646

Merged
merged 3 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions MatrixSDK/Crypto/MXCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,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
29 changes: 16 additions & 13 deletions MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,16 @@ @implementation MXLegacyCrypto
@synthesize recoveryService = _recoveryService;

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

#ifdef MX_CRYPTO

#if DEBUG
id<MXCrypto> cryptoV2 = [self createCryptoV2IfAvailableWithSession:mxSession];
if (cryptoV2) {
return cryptoV2;
if (MXSDKOptions.sharedInstance.enableCryptoV2)
{
return [self createCryptoV2WithSession:mxSession error:error];
}
#endif

Expand All @@ -176,14 +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
id<MXCrypto> cryptoV2 = [self createCryptoV2IfAvailableWithSession:mxSession];
if (cryptoV2)
if (MXSDKOptions.sharedInstance.enableCryptoV2)
{
complete(cryptoV2);
NSError *error;
id<MXCrypto> crypto = [self createCryptoV2WithSession:mxSession error:&error];
complete(crypto, error);
return;
}
#endif
Expand All @@ -194,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 @@ -220,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 @@ -244,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
26 changes: 13 additions & 13 deletions MatrixSDK/Crypto/MXCryptoV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ import Foundation

#if DEBUG
public extension MXLegacyCrypto {
/// Create a Rust-based work-in-progress implementation of `MXCrypto`
///
/// The experimental crypto module is created only if:
/// - using DEBUG build
/// - enabling `enableCryptoV2` feature flag
@objc static func createCryptoV2IfAvailable(session: MXSession!) -> MXCrypto? {
let log = MXNamedLog(name: "MXCryptoV2")
enum CryptoError: Swift.Error, LocalizedError {
case cryptoNotAvailable

guard MXSDKOptions.sharedInstance().enableCryptoV2 else {
return nil
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!) 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
27 changes: 23 additions & 4 deletions MatrixSDK/MXSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ The list of global events listeners (`MXSessionEventListener`).
*/
NSMutableArray *globalEventListeners;

/**
/**
The block to call when MSSession resume is complete.
*/
MXOnResumeDone onResumeDone;
Expand Down Expand Up @@ -398,9 +398,18 @@ -(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 && error)
{
if (failure)
{
failure(error);
}
return;
}

self->_crypto = crypto;

// Sanity check: The session may be closed before the end of this operation.
Expand Down Expand Up @@ -966,6 +975,7 @@ - (void)_startWithSyncFilterId:(NSString *)syncFilterId onServerSyncDone:(void (
MXLogDebug(@"[MXSession] Crypto has been started");
} failure:^(NSError *error) {
MXLogDebug(@"[MXSession] Crypto failed to start. Error: %@", error);
failure(error);
}];
}
else
Expand Down Expand Up @@ -2267,11 +2277,20 @@ - (void)enableCrypto:(BOOL)enableCrypto success:(void (^)(void))success failure:

if (enableCrypto && !_crypto)
{
_crypto = [MXLegacyCrypto createCryptoWithMatrixSession:self];
NSError *error;
_crypto = [MXLegacyCrypto createCryptoWithMatrixSession:self error:&error];
if (!_crypto && error)
{
if (failure)
{
failure(error);
}
return;
}

if (_state == MXSessionStateRunning)
{
[_crypto start:success failure:failure];
[self startCrypto:success failure:failure];
}
else
{
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-1646.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Crypto: Fail launch when unavailable crypto