pEpObjCAdapter/PEPSession.m
author Dirk Zimmermann <dz@pep.security>
Thu, 25 Jul 2019 10:47:04 +0200
branchIOSAD-121
changeset 1105 d17cf18af014
parent 1020 22b3668ada3d
child 1106 fb23fbc7b4d6
permissions -rw-r--r--
IOSAD-121 Macro for returning nil on session fail.
     1 //
     2 //  PEPSession.m
     3 //  pEpObjCAdapter
     4 //
     5 //  Created by Andreas Buff on 11.10.17.
     6 //  Copyright © 2017 p≡p. All rights reserved.
     7 //
     8 
     9 #import "PEPSession.h"
    10 
    11 #import "PEPInternalSession.h"
    12 #import "PEPSessionProvider.h"
    13 #import "PEPIdentity.h"
    14 
    15 #import "PEPMessageUtil.h"
    16 #import "NSNumber+PEPRating.h"
    17 #import "NSError+PEP+Internal.h"
    18 
    19 @implementation PEPSession
    20 
    21 #define RETURN_NIL_ON_ERROR(session, error)\
    22  if (session == nil) { \
    23    if (error != nil) { \
    24      *error = [NSError errorWithPEPStatusInternal:PEP_UNKNOWN_ERROR]; \
    25      return nil; \
    26    } \
    27  }
    28 
    29 #pragma mark - Public API
    30 
    31 + (void)cleanup
    32 {
    33     [PEPSessionProvider cleanup];
    34 }
    35 
    36 - (PEPDict * _Nullable)decryptMessageDict:(PEPMutableDict * _Nonnull)messageDict
    37                                     flags:(PEPDecryptFlags * _Nullable)flags
    38                                    rating:(PEPRating * _Nullable)rating
    39                                 extraKeys:(PEPStringList * _Nullable * _Nullable)extraKeys
    40                                    status:(PEPStatus * _Nullable)status
    41                                     error:(NSError * _Nullable * _Nullable)error
    42 {
    43     PEPInternalSession *session = [PEPSessionProvider session];
    44     return [session
    45             decryptMessageDict:messageDict
    46             flags:flags
    47             rating:rating
    48             extraKeys:extraKeys
    49             status:status
    50             error:error];
    51 }
    52 
    53 - (PEPMessage * _Nullable)decryptMessage:(PEPMessage * _Nonnull)message
    54                                    flags:(PEPDecryptFlags * _Nullable)flags
    55                                   rating:(PEPRating * _Nullable)rating
    56                                extraKeys:(PEPStringList * _Nullable * _Nullable)extraKeys
    57                                   status:(PEPStatus * _Nullable)status
    58                                    error:(NSError * _Nullable * _Nullable)error
    59 {
    60     PEPInternalSession *session = [PEPSessionProvider session];
    61     return [session
    62             decryptMessage:message
    63             flags:flags
    64             rating:rating
    65             extraKeys:extraKeys
    66             status:status
    67             error:error];
    68 }
    69 
    70 - (BOOL)reEvaluateMessageDict:(PEPDict * _Nonnull)messageDict
    71                      xKeyList:(PEPStringList * _Nullable)xKeyList
    72                        rating:(PEPRating * _Nonnull)rating
    73                        status:(PEPStatus * _Nullable)status
    74                         error:(NSError * _Nullable * _Nullable)error
    75 {
    76     PEPInternalSession *session = [PEPSessionProvider session];
    77     return [session reEvaluateMessageDict:messageDict
    78                                  xKeyList:xKeyList
    79                                    rating:rating
    80                                    status:status
    81                                     error:error];
    82 }
    83 
    84 - (BOOL)reEvaluateMessage:(PEPMessage * _Nonnull)message
    85                  xKeyList:(PEPStringList * _Nullable)xKeyList
    86                    rating:(PEPRating * _Nonnull)rating
    87                    status:(PEPStatus * _Nullable)status
    88                     error:(NSError * _Nullable * _Nullable)error
    89 {
    90     PEPInternalSession *session = [PEPSessionProvider session];
    91     return [session reEvaluateMessage:message
    92                              xKeyList:xKeyList
    93                                rating:rating
    94                                status:status
    95                                 error:error];
    96 }
    97 
    98 - (PEPDict * _Nullable)encryptMessageDict:(PEPDict * _Nonnull)messageDict
    99                                     extraKeys:(PEPStringList * _Nullable)extraKeys
   100                                 encFormat:(PEPEncFormat)encFormat
   101                                    status:(PEPStatus * _Nullable)status
   102                                     error:(NSError * _Nullable * _Nullable)error
   103 {
   104     PEPInternalSession *session = [PEPSessionProvider session];
   105     return [session
   106             encryptMessageDict:messageDict
   107             extraKeys:extraKeys
   108             encFormat:encFormat
   109             status:status
   110             error:error];
   111 }
   112 
   113 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   114                                extraKeys:(PEPStringList * _Nullable)extraKeys
   115                                encFormat:(PEPEncFormat)encFormat
   116                                   status:(PEPStatus * _Nullable)status
   117                                    error:(NSError * _Nullable * _Nullable)error
   118 {
   119     PEPInternalSession *session = [PEPSessionProvider session];
   120     return [session
   121             encryptMessage:message
   122             extraKeys:extraKeys
   123             encFormat:encFormat
   124             status:status
   125             error:error];
   126 }
   127 
   128 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   129                                extraKeys:(PEPStringList * _Nullable)extraKeys
   130                                   status:(PEPStatus * _Nullable)status
   131                                    error:(NSError * _Nullable * _Nullable)error
   132 {
   133     PEPInternalSession *session = [PEPSessionProvider session];
   134     return [session encryptMessage:message extraKeys:extraKeys status:status error:error];
   135 }
   136 
   137 - (PEPDict * _Nullable)encryptMessageDict:(PEPDict * _Nonnull)messageDict
   138                                   forSelf:(PEPIdentity * _Nonnull)ownIdentity
   139                                 extraKeys:(PEPStringList * _Nullable)extraKeys
   140                                    status:(PEPStatus * _Nullable)status
   141                                     error:(NSError * _Nullable * _Nullable)error
   142 {
   143     PEPInternalSession *session = [PEPSessionProvider session];
   144     return [session
   145             encryptMessageDict:messageDict
   146             forSelf:ownIdentity
   147             extraKeys:extraKeys
   148             status:status
   149             error:error];
   150 }
   151 
   152 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   153                                  forSelf:(PEPIdentity * _Nonnull)ownIdentity
   154                                extraKeys:(PEPStringList * _Nullable)extraKeys
   155                                   status:(PEPStatus * _Nullable)status
   156                                    error:(NSError * _Nullable * _Nullable)error
   157 {
   158     PEPInternalSession *session = [PEPSessionProvider session];
   159     return [session
   160             encryptMessage:message
   161             forSelf:ownIdentity
   162             extraKeys:extraKeys
   163             status:status
   164             error:error];
   165 }
   166 
   167 - (PEPDict * _Nullable)encryptMessageDict:(PEPDict * _Nonnull)messageDict
   168                                     toFpr:(NSString * _Nonnull)toFpr
   169                                 encFormat:(PEPEncFormat)encFormat
   170                                     flags:(PEPDecryptFlags)flags
   171                                    status:(PEPStatus * _Nullable)status
   172                                     error:(NSError * _Nullable * _Nullable)error __deprecated
   173 {
   174     PEPInternalSession *session = [PEPSessionProvider session];
   175     return [session
   176             encryptMessageDict:messageDict
   177             toFpr:toFpr
   178             encFormat:encFormat
   179             flags:flags
   180             status:status
   181             error:error];
   182 }
   183 
   184 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   185                                    toFpr:(NSString * _Nonnull)toFpr
   186                                encFormat:(PEPEncFormat)encFormat
   187                                    flags:(PEPDecryptFlags)flags
   188                                   status:(PEPStatus * _Nullable)status
   189                                    error:(NSError * _Nullable * _Nullable)error
   190 {
   191     PEPInternalSession *session = [PEPSessionProvider session];
   192     return [session
   193             encryptMessage:message
   194             toFpr:toFpr
   195             encFormat:encFormat
   196             flags:flags
   197             status:status
   198             error:error];
   199 }
   200 
   201 - (NSNumber * _Nullable)outgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
   202                                            error:(NSError * _Nullable * _Nullable)error
   203 {
   204     PEPInternalSession *session = [PEPSessionProvider session];
   205     return [session outgoingRatingForMessage:theMessage error:error];
   206 }
   207 
   208 - (NSNumber * _Nullable)outgoingRatingPreviewForMessage:(PEPMessage * _Nonnull)theMessage
   209                                                   error:(NSError * _Nullable * _Nullable)error
   210 {
   211     PEPInternalSession *session = [PEPSessionProvider session];
   212     return [session outgoingRatingPreviewForMessage:theMessage error:error];
   213 }
   214 
   215 - (NSNumber * _Nullable)ratingForIdentity:(PEPIdentity * _Nonnull)identity
   216                                     error:(NSError * _Nullable * _Nullable)error
   217 {
   218     PEPInternalSession *session = [PEPSessionProvider session];
   219     return [session ratingForIdentity:identity error:error];
   220 }
   221 
   222 - (NSArray * _Nullable)trustwordsForFingerprint:(NSString * _Nonnull)fingerprint
   223                                      languageID:(NSString * _Nonnull)languageID
   224                                       shortened:(BOOL)shortened
   225                                           error:(NSError * _Nullable * _Nullable)error
   226 {
   227     PEPInternalSession *session = [PEPSessionProvider session];
   228     return [session
   229             trustwordsForFingerprint:fingerprint
   230             languageID:languageID
   231             shortened:shortened
   232             error:error];
   233 }
   234 
   235 - (BOOL)mySelf:(PEPIdentity * _Nonnull)identity error:(NSError * _Nullable * _Nullable)error
   236 {
   237     PEPInternalSession *session = [PEPSessionProvider session];
   238     return [session mySelf:identity error:error];
   239 }
   240 
   241 - (BOOL)updateIdentity:(PEPIdentity * _Nonnull)identity
   242                  error:(NSError * _Nullable * _Nullable)error
   243 {
   244     PEPInternalSession *session = [PEPSessionProvider session];
   245     return [session updateIdentity:identity error:error];
   246 }
   247 
   248 - (BOOL)trustPersonalKey:(PEPIdentity * _Nonnull)identity
   249                    error:(NSError * _Nullable * _Nullable)error
   250 {
   251     PEPInternalSession *session = [PEPSessionProvider session];
   252     return [session trustPersonalKey:identity error:error];
   253 }
   254 
   255 - (BOOL)keyMistrusted:(PEPIdentity * _Nonnull)identity
   256                 error:(NSError * _Nullable * _Nullable)error
   257 {
   258     PEPInternalSession *session = [PEPSessionProvider session];
   259     return [session keyMistrusted:identity error:error];
   260 }
   261 
   262 - (BOOL)keyResetTrust:(PEPIdentity * _Nonnull)identity
   263                 error:(NSError * _Nullable * _Nullable)error
   264 {
   265     PEPInternalSession *session = [PEPSessionProvider session];
   266     return [session keyResetTrust:identity error:error];
   267 }
   268 
   269 #pragma mark Internal API (testing etc.)
   270 
   271 - (NSArray * _Nullable)importKey:(NSString * _Nonnull)keydata
   272                            error:(NSError * _Nullable * _Nullable)error
   273 {
   274     PEPInternalSession *session = [PEPSessionProvider session];
   275     return [session importKey:keydata error:error];
   276 }
   277 
   278 - (BOOL)logTitle:(NSString * _Nonnull)title
   279           entity:(NSString * _Nonnull)entity
   280      description:(NSString * _Nullable)description
   281          comment:(NSString * _Nullable)comment
   282            error:(NSError * _Nullable * _Nullable)error
   283 {
   284     PEPInternalSession *session = [PEPSessionProvider session];
   285     return [session
   286             logTitle:title
   287             entity:entity
   288             description:description
   289             comment:comment
   290             error:error];
   291 }
   292 
   293 - (NSString * _Nullable)getLogWithError:(NSError * _Nullable * _Nullable)error
   294 {
   295     PEPInternalSession *session = [PEPSessionProvider session];
   296     return [session getLogWithError:error];
   297 }
   298 
   299 - (NSString * _Nullable)getTrustwordsIdentity1:(PEPIdentity * _Nonnull)identity1
   300                                      identity2:(PEPIdentity * _Nonnull)identity2
   301                                       language:(NSString * _Nullable)language
   302                                           full:(BOOL)full
   303                                          error:(NSError * _Nullable * _Nullable)error
   304 {
   305     PEPInternalSession *session = [PEPSessionProvider session];
   306     return [session getTrustwordsIdentity1:identity1
   307                                  identity2:identity2
   308                                   language:language
   309                                       full:full
   310                                      error:error];
   311 }
   312 
   313 - (NSString * _Nullable)getTrustwordsFpr1:(NSString * _Nonnull)fpr1
   314                                      fpr2:(NSString * _Nonnull)fpr2
   315                                  language:(NSString * _Nullable)language
   316                                      full:(BOOL)full
   317                                     error:(NSError * _Nullable * _Nullable)error
   318 {
   319     PEPInternalSession *session = [PEPSessionProvider session];
   320     return [session getTrustwordsFpr1:fpr1 fpr2:fpr2 language:language full:full error:error];
   321 }
   322 
   323 - (NSArray<PEPLanguage *> * _Nullable)languageListWithError:(NSError * _Nullable * _Nullable)error
   324 {
   325     PEPInternalSession *session = [PEPSessionProvider session];
   326     return [session languageListWithError:error];
   327 }
   328 
   329 - (PEPRating)ratingFromString:(NSString * _Nonnull)string
   330 {
   331     PEPInternalSession *session = [PEPSessionProvider session];
   332     return [session ratingFromString:string];
   333 }
   334 
   335 - (NSString * _Nonnull)stringFromRating:(PEPRating)rating
   336 {
   337     PEPInternalSession *session = [PEPSessionProvider session];
   338     return [session stringFromRating:rating];
   339 }
   340 
   341 - (NSNumber * _Nullable)isPEPUser:(PEPIdentity * _Nonnull)identity
   342                             error:(NSError * _Nullable * _Nullable)error
   343 {
   344     PEPInternalSession *session = [PEPSessionProvider session];
   345     return [session isPEPUser:identity error:error];
   346 }
   347 
   348 - (BOOL)setOwnKey:(PEPIdentity * _Nonnull)identity fingerprint:(NSString * _Nonnull)fingerprint
   349             error:(NSError * _Nullable * _Nullable)error
   350 {
   351     PEPInternalSession *session = [PEPSessionProvider session];
   352     return [session setOwnKey:identity fingerprint:fingerprint error:error];
   353 }
   354 
   355 - (void)configurePassiveModeEnabled:(BOOL)enabled
   356 {
   357     PEPInternalSession *session = [PEPSessionProvider session];
   358     return [session configurePassiveModeEnabled:enabled];
   359 }
   360 
   361 - (BOOL)setFlags:(PEPIdentityFlags)flags
   362      forIdentity:(PEPIdentity *)identity
   363            error:(NSError * _Nullable * _Nullable)error
   364 {
   365     PEPInternalSession *session = [PEPSessionProvider session];
   366     return [session setFlags:flags forIdentity:identity error:error];
   367 }
   368 
   369 - (BOOL)trustOwnKeyIdentity:(PEPIdentity * _Nonnull)identity
   370                       error:(NSError * _Nullable * _Nullable)error
   371 {
   372     PEPInternalSession *session = [PEPSessionProvider session];
   373     return [session trustOwnKeyIdentity:identity error:error];
   374 }
   375 
   376 - (BOOL)deliverHandshakeResult:(PEPSyncHandshakeResult)result
   377              identitiesSharing:(NSArray<PEPIdentity *> * _Nullable)identitiesSharing
   378                          error:(NSError * _Nullable * _Nullable)error;
   379 {
   380     PEPInternalSession *session = [PEPSessionProvider session];
   381     return [session deliverHandshakeResult:result identitiesSharing:identitiesSharing error:error];
   382 }
   383 
   384 - (PEPColor)colorFromRating:(PEPRating)rating
   385 {
   386     PEPInternalSession *session = [PEPSessionProvider session];
   387     return [session colorFromRating:rating];
   388 }
   389 
   390 - (BOOL)keyReset:(PEPIdentity * _Nonnull)identity
   391      fingerprint:(NSString * _Nullable)fingerprint
   392            error:(NSError * _Nullable * _Nullable)error
   393 {
   394     PEPInternalSession *session = [PEPSessionProvider session];
   395     return [session keyReset:identity fingerprint:fingerprint error:error];
   396 }
   397 
   398 - (BOOL)leaveDeviceGroupError:(NSError * _Nullable * _Nullable)error
   399 {
   400     PEPInternalSession *session = [PEPSessionProvider session];
   401     return [session leaveDeviceGroupError:error];
   402 }
   403 
   404 @end