From 28c7143778ddda5fc982fa13bc87002444242e6c Mon Sep 17 00:00:00 2001 From: buff Date: Mon, 6 Jul 2020 20:53:18 +0200 Subject: [PATCH] IOSAD-177 blocks messageToSend until passphrase is configured. --- pEpObjCAdapter/PEPInternalSession.m | 1 + pEpObjCAdapter/PEPSync.m | 40 +++++++++++++++++++++++++++-- pEpObjCAdapter/PEPSync_Internal.h | 3 +++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/pEpObjCAdapter/PEPInternalSession.m b/pEpObjCAdapter/PEPInternalSession.m index d029ce4..9027558 100644 --- a/pEpObjCAdapter/PEPInternalSession.m +++ b/pEpObjCAdapter/PEPInternalSession.m @@ -1093,6 +1093,7 @@ static NSDictionary *stringToRating; if ([NSError setError:error fromPEPStatus:(PEPStatus) status]) { return NO; } + [PEPSync.sharedInstance handleNewPassphraseConfigured]; return YES; } diff --git a/pEpObjCAdapter/PEPSync.m b/pEpObjCAdapter/PEPSync.m index 948e9b1..50711b4 100644 --- a/pEpObjCAdapter/PEPSync.m +++ b/pEpObjCAdapter/PEPSync.m @@ -36,6 +36,8 @@ typedef int (* t_injectSyncCallback)(SYNC_EVENT ev, void *management); @property (nonatomic, nonnull) PEPQueue *queue; @property (nonatomic, nullable) NSThread *syncThread; @property (nonatomic, nullable) NSConditionLock *conditionLockForJoiningSyncThread; +/// Used to block messageToSend() until the client configured a passphrase. +@property (atomic, nullable) dispatch_group_t blockmessageToSendGroup; /// The session created and used by the sync loop @property (nonatomic, nullable) PEPInternalSession *syncLoopSession; @@ -162,6 +164,8 @@ static __weak PEPSync *s_pEpSync; - (void)startup { + [self stopWaiting]; + if (self.syncThread != nil) { // already started return; @@ -176,7 +180,7 @@ static __weak PEPSync *s_pEpSync; // Make sure queue is empty when we start. [self.queue removeAllObjects]; - [self assureMainSessionExists]; + [self assureMainSessionExists]; //???: Why do we need that? Afaics syncThreadLoop gets the session from PEPSessionProvider, which should have taken care of main session existance. self.conditionLockForJoiningSyncThread = [[NSConditionLock alloc] initWithCondition:NO]; [theSyncThread start]; @@ -184,6 +188,8 @@ static __weak PEPSync *s_pEpSync; - (void)shutdown { + [self stopWaiting]; + if (self.syncThread) { [self injectSyncEvent:nil isFromShutdown:YES]; } @@ -197,6 +203,10 @@ static __weak PEPSync *s_pEpSync; } } +- (void)handleNewPassphraseConfigured { + [self stopWaiting]; +} + // MARK: - Private + (void)initialize @@ -204,7 +214,7 @@ static __weak PEPSync *s_pEpSync; s_logger = os_log_create("security.pEp.adapter", "PEPSync"); } -+ (PEPSync * _Nullable)sharedInstance ++ (PEPSync * _Nullable)sharedInstance //!!!: is not private but internal { return s_pEpSync; } @@ -252,6 +262,8 @@ static __weak PEPSync *s_pEpSync; - (PEP_STATUS)messageToSend:(struct _message * _Nullable)msg { + [self blockUntilPassphraseIsEnteredIfRequired]; + if (msg == NULL && [NSThread currentThread] == self.syncThread) { static NSMutableArray *passphrasesCopy = nil; static BOOL makeNewCopy = YES; @@ -268,6 +280,7 @@ static __weak PEPSync *s_pEpSync; if ([passphrasesCopy count] == 0) { makeNewCopy = YES; + [self nextCallMustWait]; return PEP_PASSPHRASE_REQUIRED; } else { makeNewCopy = NO; @@ -276,6 +289,7 @@ static __weak PEPSync *s_pEpSync; if ([passphrasesCopy count] == 0) { makeNewCopy = YES; + [self nextCallMustWait]; return PEP_WRONG_PASSPHRASE; } else { NSString *password = [passphrasesCopy firstObject]; @@ -354,4 +368,26 @@ static __weak PEPSync *s_pEpSync; } } +// MARK: Blocking (messageToSend) + +- (void)blockUntilPassphraseIsEnteredIfRequired { + if (self.blockmessageToSendGroup) { + dispatch_group_wait(self.blockmessageToSendGroup, DISPATCH_TIME_FOREVER); + } +} + +- (void)nextCallMustWait { + if (!self.blockmessageToSendGroup) { + self.blockmessageToSendGroup = dispatch_group_create(); + } + dispatch_group_enter(self.blockmessageToSendGroup); +} + +- (void)stopWaiting { + if (self.blockmessageToSendGroup) { + dispatch_group_leave(self.blockmessageToSendGroup); + self.blockmessageToSendGroup = nil; + } +} + @end diff --git a/pEpObjCAdapter/PEPSync_Internal.h b/pEpObjCAdapter/PEPSync_Internal.h index bd317c7..12db984 100644 --- a/pEpObjCAdapter/PEPSync_Internal.h +++ b/pEpObjCAdapter/PEPSync_Internal.h @@ -30,6 +30,9 @@ /// The one and only sync instance, or nil, if none exists. + (PEPSync * _Nullable)sharedInstance; +/// MUST be called whenever a passphrase is configured +- (void)handleNewPassphraseConfigured; + @end #endif /* PEPSync_Internal_h */