Skip to content

Commit

Permalink
Fail launch when unavailable crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
Anderas committed Nov 29, 2022
1 parent 084a06a commit 8be47c0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
6 changes: 6 additions & 0 deletions MatrixSDK/Crypto/MXCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@

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
13 changes: 7 additions & 6 deletions MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
*/
#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 @@ -157,9 +159,9 @@ @implementation MXLegacyCrypto
#ifdef MX_CRYPTO

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

Expand All @@ -180,10 +182,9 @@ + (void)checkCryptoWithMatrixSession:(MXSession*)mxSession complete:(void (^)(id
{
#ifdef MX_CRYPTO
#if DEBUG
id<MXCrypto> cryptoV2 = [self createCryptoV2IfAvailableWithSession:mxSession];
if (cryptoV2)
if (MXSDKOptions.sharedInstance.enableCryptoV2)
{
complete(cryptoV2);
complete([self createCryptoV2WithSession:mxSession]);
return;
}
#endif
Expand Down
12 changes: 2 additions & 10 deletions MatrixSDK/Crypto/MXCryptoV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@ 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? {
@objc static func createCryptoV2(session: MXSession!) -> MXCrypto? {
let log = MXNamedLog(name: "MXCryptoV2")

guard MXSDKOptions.sharedInstance().enableCryptoV2 else {
return nil
}


guard let session = session else {
log.failure("Cannot create crypto V2, missing session")
return nil
Expand Down
21 changes: 18 additions & 3 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 @@ -966,6 +966,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 @@ -2271,7 +2272,7 @@ - (void)enableCrypto:(BOOL)enableCrypto success:(void (^)(void))success failure:

if (_state == MXSessionStateRunning)
{
[_crypto start:success failure:failure];
[self startCrypto:success failure:failure];
}
else
{
Expand Down Expand Up @@ -4907,6 +4908,8 @@ - (MXHTTPOperation*)filterWithFilterId:(NSString*)filterId
- (void)startCrypto:(void (^)(void))success
failure:(void (^)(NSError *error))failure
{
#ifdef MX_CRYPTO

MXLogDebug(@"[MXSession] Start crypto");

if (_crypto)
Expand All @@ -4915,9 +4918,21 @@ - (void)startCrypto:(void (^)(void))success
}
else
{
MXLogDebug(@"[MXSession] Start crypto -> No crypto");
NSError *error = [NSError errorWithDomain:MXCryptoErrorDomain code:MXCryptoUnavailableErrorCode userInfo:@{
NSLocalizedDescriptionKey: @"Encryption not available, please restart the app",
}];
if (failure)
{
failure(error);
}
}
#else
MXLogDebug(@"[MXSession] Start crypto -> No crypto");
if (success)
{
success();
}
#endif
}

- (void)decryptEvents:(NSArray<MXEvent*> *)events
Expand Down

0 comments on commit 8be47c0

Please sign in to comment.