IOS-873 adds global setting for config_unencrypted_subject
authorbuff <andreas@pep-project.org>
Thu, 01 Feb 2018 18:33:09 +0100
changeset 4165a37145e3eca
parent 415 bcc3827f5980
child 417 595cf6bba6c5
IOS-873 adds global setting for config_unencrypted_subject
pEpObjCAdapter/PEPInternalSession.h
pEpObjCAdapter/PEPInternalSession.m
pEpObjCAdapter/PEPObjCAdapter+Internal.h
pEpObjCAdapter/PEPObjCAdapter.h
pEpObjCAdapter/PEPObjCAdapter.m
pEpObjCAdapter/PEPSessionProvider.m
pEpObjCTests/PEPInternalSessionTest.m
pEpObjCTests/PEPSessionTest.m
     1.1 --- a/pEpObjCAdapter/PEPInternalSession.h	Tue Jan 30 08:49:06 2018 +0100
     1.2 +++ b/pEpObjCAdapter/PEPInternalSession.h	Thu Feb 01 18:33:09 2018 +0100
     1.3 @@ -23,4 +23,11 @@
     1.4  
     1.5  @property (nonatomic) PEP_SESSION _Nullable session;
     1.6  
     1.7 +/**
     1.8 + Configures the session's unecryptedSubjectEnabled value.
     1.9 +
    1.10 + @param enabled Whether or not mail subjects should be encrypted when using this session
    1.11 + */
    1.12 +- (void)configUnencryptedSubjectEnabled:(BOOL)enabled;
    1.13 +
    1.14  @end
     2.1 --- a/pEpObjCAdapter/PEPInternalSession.m	Tue Jan 30 08:49:06 2018 +0100
     2.2 +++ b/pEpObjCAdapter/PEPInternalSession.m	Thu Feb 01 18:33:09 2018 +0100
     2.3 @@ -19,14 +19,6 @@
     2.4  
     2.5  @implementation PEPInternalSession
     2.6  
     2.7 -+ (void)setupTrustWordsDB
     2.8 -{
     2.9 -    static dispatch_once_t once;
    2.10 -    dispatch_once(&once, ^{
    2.11 -        [PEPObjCAdapter setupTrustWordsDB:[NSBundle bundleForClass:[self class]]];
    2.12 -    });
    2.13 -}
    2.14 -
    2.15  - (instancetype)init
    2.16  {
    2.17      self = [super init];
    2.18 @@ -56,6 +48,25 @@
    2.19  
    2.20  }
    2.21  
    2.22 +#pragma mark - CONFIG
    2.23 +
    2.24 +- (void)configUnencryptedSubjectEnabled:(BOOL)enabled;
    2.25 +{
    2.26 +    config_unencrypted_subject(self.session, enabled);
    2.27 +}
    2.28 +
    2.29 +#pragma mark - INTERNAL
    2.30 +
    2.31 ++ (void)setupTrustWordsDB
    2.32 +{
    2.33 +    static dispatch_once_t once;
    2.34 +    dispatch_once(&once, ^{
    2.35 +        [PEPObjCAdapter setupTrustWordsDB:[NSBundle bundleForClass:[self class]]];
    2.36 +    });
    2.37 +}
    2.38 +
    2.39 +#pragma mark - DEBUG UTILS
    2.40 +
    2.41  /**
    2.42   Saves the given message dict as a plist to the local filesystem
    2.43   (directly under NSApplicationSupportDirectory).
    2.44 @@ -73,6 +84,8 @@
    2.45      [src debugSaveToFilePath:fileName];
    2.46  }
    2.47  
    2.48 +#pragma mark - PEPSessionProtocol
    2.49 +
    2.50  - (PEP_rating)decryptMessageDict:(nonnull PEPDict *)src
    2.51                              dest:(PEPDict * _Nullable * _Nullable)dst
    2.52                              keys:(PEPStringList * _Nullable * _Nullable)keys
     3.1 --- a/pEpObjCAdapter/PEPObjCAdapter+Internal.h	Tue Jan 30 08:49:06 2018 +0100
     3.2 +++ b/pEpObjCAdapter/PEPObjCAdapter+Internal.h	Thu Feb 01 18:33:09 2018 +0100
     3.3 @@ -17,6 +17,13 @@
     3.4  @interface PEPObjCAdapter ()
     3.5  
     3.6  /**
     3.7 + unecryptedSubjectEnabled value to use for all sessions created.
     3.8 +
     3.9 + @return Whether or not mail subjects should be encrypted
    3.10 + */
    3.11 ++ (BOOL)unecryptedSubjectEnabled;
    3.12 +
    3.13 +/**
    3.14   The lock that should be used for locking all session init() and release().
    3.15   */
    3.16  + (NSLock *)initLock;
     4.1 --- a/pEpObjCAdapter/PEPObjCAdapter.h	Tue Jan 30 08:49:06 2018 +0100
     4.2 +++ b/pEpObjCAdapter/PEPObjCAdapter.h	Thu Feb 01 18:33:09 2018 +0100
     4.3 @@ -26,6 +26,14 @@
     4.4  @interface PEPObjCAdapter : NSObject
     4.5  
     4.6  /**
     4.7 + Sets Engine config for unecryptedSubjectEnabled to the given value on all Sessions created by
     4.8 + this adapter.
     4.9 +
    4.10 + @param enabled Whether or not mail subjects should be encrypted
    4.11 + */
    4.12 ++ (void)setUnecryptedSubjectEnabled:(BOOL)enabled;
    4.13 +
    4.14 +/**
    4.15   The HOME URL, where all pEp related files will be stored.
    4.16   */
    4.17  + (NSURL * _Nonnull)homeURL;
     5.1 --- a/pEpObjCAdapter/PEPObjCAdapter.m	Tue Jan 30 08:49:06 2018 +0100
     5.2 +++ b/pEpObjCAdapter/PEPObjCAdapter.m	Thu Feb 01 18:33:09 2018 +0100
     5.3 @@ -98,15 +98,27 @@
     5.4      
     5.5  }
     5.6  
     5.7 -///////////////////////////////////////////////////////////////////////////////
     5.8 -// DB and paths
     5.9 -
    5.10  const char* _Nullable SystemDB = NULL;
    5.11  NSURL *s_homeURL;
    5.12  static NSLock *s_initLock;
    5.13 +static BOOL s_unecryptedSubjectEnabled = NO;
    5.14  
    5.15  @implementation PEPObjCAdapter
    5.16  
    5.17 +#pragma mark - SUBJECT PROTECTION
    5.18 +
    5.19 ++ (BOOL)unecryptedSubjectEnabled;
    5.20 +{
    5.21 +    return s_unecryptedSubjectEnabled;
    5.22 +}
    5.23 +
    5.24 ++ (void)setUnecryptedSubjectEnabled:(BOOL)enabled;
    5.25 +{
    5.26 +    s_unecryptedSubjectEnabled = enabled;
    5.27 +}
    5.28 +
    5.29 +#pragma mark - DB PATHS
    5.30 +
    5.31  + (void)initialize
    5.32  {
    5.33      s_homeURL = [self createApplicationDirectory];
     6.1 --- a/pEpObjCAdapter/PEPSessionProvider.m	Tue Jan 30 08:49:06 2018 +0100
     6.2 +++ b/pEpObjCAdapter/PEPSessionProvider.m	Thu Feb 01 18:33:09 2018 +0100
     6.3 @@ -8,6 +8,7 @@
     6.4  
     6.5  #import "PEPSessionProvider.h"
     6.6  
     6.7 +#import "PEPObjCAdapter+Internal.h"
     6.8  #import "PEPInternalSession.h"
     6.9  #import "PEPCopyableThread.h"
    6.10  
    6.11 @@ -43,6 +44,7 @@
    6.12          session = [PEPInternalSession new];
    6.13          dict[currentThread] = session;
    6.14      }
    6.15 +    [self setConfigUnencryptedSubjectOnSession:session];
    6.16      [self nullifySessionsOfFinishedThreads];
    6.17  
    6.18      [[self sessionForThreadLock] unlock];
    6.19 @@ -80,6 +82,13 @@
    6.20  }
    6.21  
    6.22  #pragma mark -
    6.23 +
    6.24 ++ (void)setConfigUnencryptedSubjectOnSession:(PEPInternalSession *)session
    6.25 +{
    6.26 +    BOOL unencryptedSubjectEnabled = [PEPObjCAdapter unecryptedSubjectEnabled];
    6.27 +    [session configUnencryptedSubjectEnabled:unencryptedSubjectEnabled];
    6.28 +}
    6.29 +
    6.30  /**
    6.31   Assures a session for the main thread is set.
    6.32   */
    6.33 @@ -96,6 +105,7 @@
    6.34              s_sessionForMainThread = [PEPInternalSession new];
    6.35          });
    6.36      }
    6.37 +    [self setConfigUnencryptedSubjectOnSession:s_sessionForMainThread];
    6.38  }
    6.39  
    6.40  + (void)cleanupInternal
     7.1 --- a/pEpObjCTests/PEPInternalSessionTest.m	Tue Jan 30 08:49:06 2018 +0100
     7.2 +++ b/pEpObjCTests/PEPInternalSessionTest.m	Thu Feb 01 18:33:09 2018 +0100
     7.3 @@ -527,6 +527,8 @@
     7.4  
     7.5      // This will revoke key
     7.6      [self.session keyMistrusted:identAlice2];
     7.7 +    identAlice2.fingerPrint = nil;
     7.8 +    [self.session mySelf:identAlice];
     7.9  
    7.10      // Check fingerprint is different
    7.11      XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
     8.1 --- a/pEpObjCTests/PEPSessionTest.m	Tue Jan 30 08:49:06 2018 +0100
     8.2 +++ b/pEpObjCTests/PEPSessionTest.m	Thu Feb 01 18:33:09 2018 +0100
     8.3 @@ -24,6 +24,8 @@
     8.4  - (void)setUp
     8.5  {
     8.6      [super setUp];
     8.7 +    [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
     8.8 +
     8.9      [self pEpCleanUp];
    8.10  }
    8.11  
    8.12 @@ -471,6 +473,9 @@
    8.13  
    8.14      // This will revoke key
    8.15      [session keyMistrusted:identAlice2];
    8.16 +    identAlice2.fingerPrint = nil;
    8.17 +    [session mySelf:identAlice];
    8.18 +
    8.19  
    8.20      // Check fingerprint is different
    8.21      XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
    8.22 @@ -622,8 +627,56 @@
    8.23      XCTAssertNil(trustwordsUndefined);
    8.24  }
    8.25  
    8.26 +#pragma mark - configUnencryptedSubject
    8.27 +
    8.28 +- (void)testConfigUnencryptedSubject
    8.29 +{
    8.30 +    // Setup Config to encrypt subject
    8.31 +    [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
    8.32 +
    8.33 +    // Write mail to yourself ...
    8.34 +    PEPMessage *encMessage = [self mailWrittenToMySelf];
    8.35 +
    8.36 +    // ... and assert subject is encrypted
    8.37 +    XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
    8.38 +}
    8.39 +
    8.40 +- (void)testConfigUnencryptedSubject_encryptedSubjectDisabled
    8.41 +{
    8.42 +    // Setup Config to not encrypt subject
    8.43 +    [PEPObjCAdapter setUnecryptedSubjectEnabled:YES];
    8.44 +
    8.45 +    // Write mail to yourself ...
    8.46 +    PEPMessage *encMessage = [self mailWrittenToMySelf];
    8.47 +
    8.48 +    // ... and assert the subject is not encrypted
    8.49 +    XCTAssertNotEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should not be encrypted");
    8.50 +}
    8.51 +
    8.52  #pragma mark - Helpers
    8.53  
    8.54 +- (PEPMessage *)mailWrittenToMySelf
    8.55 +{
    8.56 +    PEPSession *session = [PEPSession new];
    8.57 +
    8.58 +    // Write a e-mail to yourself ...
    8.59 +    PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
    8.60 +                                                     userName:@"userName"];
    8.61 +    [session mySelf:me];
    8.62 +
    8.63 +    NSString *shortMessage = @"Subject";
    8.64 +    NSString *longMessage = @"Oh, this is a long body text!";
    8.65 +    PEPMessage *mail = [PEPTestUtils mailFrom:me
    8.66 +                                      toIdent:me
    8.67 +                                 shortMessage:shortMessage
    8.68 +                                  longMessage:longMessage
    8.69 +                                     outgoing:YES];
    8.70 +    PEPMessage *encMessage;
    8.71 +    [session encryptMessage:mail identity:me dest:&encMessage];
    8.72 +
    8.73 +    return encMessage;
    8.74 +}
    8.75 +
    8.76  - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
    8.77  {
    8.78      PEPSession *session = [PEPSession new];