pEpObjCAdapter/PEPSession.m
author Dirk Zimmermann <dz@pep.security>
Fri, 20 Sep 2019 10:01:48 +0200
branchIOSAD-141
changeset 1250 b6a4d30abd26
parent 1190 3a53851cc0b9
child 1285 bca0bfd9cc7a
permissions -rw-r--r--
IOSAD-141 Test for "hammering" the engine.
     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 #import "PEPInternalConstants.h"
    19 
    20 @implementation PEPSession
    21 
    22 /**
    23  Macro for causing a return if the given session is nil, optionally setting an error.
    24 
    25  @param session A session object that will be checked for being nil or not.
    26  @param error If non-nil, will receive PEP_UNKNOWN_ERROR when the session is nil.
    27  @param what The value to return in case of an error (session is nil).
    28  */
    29 #define RETURN_ON_ERROR(session, error, what)\
    30   if (session == nil) { \
    31     if (error != nil) { \
    32       *error = [NSError errorWithPEPStatusInternal:PEP_UNKNOWN_ERROR]; \
    33       return what; \
    34     } \
    35   }
    36 
    37 #pragma mark - Public API
    38 
    39 + (void)cleanup
    40 {
    41     [PEPSessionProvider cleanup];
    42 }
    43 
    44 - (PEPDict * _Nullable)decryptMessageDict:(PEPMutableDict * _Nonnull)messageDict
    45                                     flags:(PEPDecryptFlags * _Nullable)flags
    46                                    rating:(PEPRating * _Nullable)rating
    47                                 extraKeys:(PEPStringList * _Nullable * _Nullable)extraKeys
    48                                    status:(PEPStatus * _Nullable)status
    49                                     error:(NSError * _Nullable * _Nullable)error
    50 {
    51     PEPInternalSession *session = [PEPSessionProvider session];
    52     RETURN_ON_ERROR(session, error, nil);
    53     return [session
    54             decryptMessageDict:messageDict
    55             flags:flags
    56             rating:rating
    57             extraKeys:extraKeys
    58             status:status
    59             error:error];
    60 }
    61 
    62 - (PEPMessage * _Nullable)decryptMessage:(PEPMessage * _Nonnull)message
    63                                    flags:(PEPDecryptFlags * _Nullable)flags
    64                                   rating:(PEPRating * _Nullable)rating
    65                                extraKeys:(PEPStringList * _Nullable * _Nullable)extraKeys
    66                                   status:(PEPStatus * _Nullable)status
    67                                    error:(NSError * _Nullable * _Nullable)error
    68 {
    69     PEPInternalSession *session = [PEPSessionProvider session];
    70     RETURN_ON_ERROR(session, error, nil);
    71     return [session
    72             decryptMessage:message
    73             flags:flags
    74             rating:rating
    75             extraKeys:extraKeys
    76             status:status
    77             error:error];
    78 }
    79 
    80 - (BOOL)reEvaluateMessageDict:(PEPDict * _Nonnull)messageDict
    81                      xKeyList:(PEPStringList * _Nullable)xKeyList
    82                        rating:(PEPRating * _Nonnull)rating
    83                        status:(PEPStatus * _Nullable)status
    84                         error:(NSError * _Nullable * _Nullable)error
    85 {
    86     PEPInternalSession *session = [PEPSessionProvider session];
    87     RETURN_ON_ERROR(session, error, NO);
    88     return [session reEvaluateMessageDict:messageDict
    89                                  xKeyList:xKeyList
    90                                    rating:rating
    91                                    status:status
    92                                     error:error];
    93 }
    94 
    95 - (BOOL)reEvaluateMessage:(PEPMessage * _Nonnull)message
    96                  xKeyList:(PEPStringList * _Nullable)xKeyList
    97                    rating:(PEPRating * _Nonnull)rating
    98                    status:(PEPStatus * _Nullable)status
    99                     error:(NSError * _Nullable * _Nullable)error
   100 {
   101     PEPInternalSession *session = [PEPSessionProvider session];
   102     RETURN_ON_ERROR(session, error, NO);
   103     return [session reEvaluateMessage:message
   104                              xKeyList:xKeyList
   105                                rating:rating
   106                                status:status
   107                                 error:error];
   108 }
   109 
   110 - (PEPDict * _Nullable)encryptMessageDict:(PEPDict * _Nonnull)messageDict
   111                                     extraKeys:(PEPStringList * _Nullable)extraKeys
   112                                 encFormat:(PEPEncFormat)encFormat
   113                                    status:(PEPStatus * _Nullable)status
   114                                     error:(NSError * _Nullable * _Nullable)error
   115 {
   116     PEPInternalSession *session = [PEPSessionProvider session];
   117     RETURN_ON_ERROR(session, error, nil);
   118     return [session
   119             encryptMessageDict:messageDict
   120             extraKeys:extraKeys
   121             encFormat:encFormat
   122             status:status
   123             error:error];
   124 }
   125 
   126 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   127                                extraKeys:(PEPStringList * _Nullable)extraKeys
   128                                encFormat:(PEPEncFormat)encFormat
   129                                   status:(PEPStatus * _Nullable)status
   130                                    error:(NSError * _Nullable * _Nullable)error
   131 {
   132     PEPInternalSession *session = [PEPSessionProvider session];
   133     RETURN_ON_ERROR(session, error, nil);
   134     return [session
   135             encryptMessage:message
   136             extraKeys:extraKeys
   137             encFormat:encFormat
   138             status:status
   139             error:error];
   140 }
   141 
   142 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   143                                extraKeys:(PEPStringList * _Nullable)extraKeys
   144                                   status:(PEPStatus * _Nullable)status
   145                                    error:(NSError * _Nullable * _Nullable)error
   146 {
   147     PEPInternalSession *session = [PEPSessionProvider session];
   148     RETURN_ON_ERROR(session, error, nil);
   149     return [session encryptMessage:message extraKeys:extraKeys status:status error:error];
   150 }
   151 
   152 - (PEPDict * _Nullable)encryptMessageDict:(PEPDict * _Nonnull)messageDict
   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_ON_ERROR(session, error, nil);
   160     return [session
   161             encryptMessageDict:messageDict
   162             forSelf:ownIdentity
   163             extraKeys:extraKeys
   164             status:status
   165             error:error];
   166 }
   167 
   168 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   169                                  forSelf:(PEPIdentity * _Nonnull)ownIdentity
   170                                extraKeys:(PEPStringList * _Nullable)extraKeys
   171                                   status:(PEPStatus * _Nullable)status
   172                                    error:(NSError * _Nullable * _Nullable)error
   173 {
   174     PEPInternalSession *session = [PEPSessionProvider session];
   175     RETURN_ON_ERROR(session, error, nil);
   176     return [session
   177             encryptMessage:message
   178             forSelf:ownIdentity
   179             extraKeys:extraKeys
   180             status:status
   181             error:error];
   182 }
   183 
   184 - (PEPDict * _Nullable)encryptMessageDict:(PEPDict * _Nonnull)messageDict
   185                                     toFpr:(NSString * _Nonnull)toFpr
   186                                 encFormat:(PEPEncFormat)encFormat
   187                                     flags:(PEPDecryptFlags)flags
   188                                    status:(PEPStatus * _Nullable)status
   189                                     error:(NSError * _Nullable * _Nullable)error __deprecated
   190 {
   191     PEPInternalSession *session = [PEPSessionProvider session];
   192     RETURN_ON_ERROR(session, error, nil);
   193     return [session
   194             encryptMessageDict:messageDict
   195             toFpr:toFpr
   196             encFormat:encFormat
   197             flags:flags
   198             status:status
   199             error:error];
   200 }
   201 
   202 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   203                                    toFpr:(NSString * _Nonnull)toFpr
   204                                encFormat:(PEPEncFormat)encFormat
   205                                    flags:(PEPDecryptFlags)flags
   206                                   status:(PEPStatus * _Nullable)status
   207                                    error:(NSError * _Nullable * _Nullable)error
   208 {
   209     PEPInternalSession *session = [PEPSessionProvider session];
   210     RETURN_ON_ERROR(session, error, nil);
   211     return [session
   212             encryptMessage:message
   213             toFpr:toFpr
   214             encFormat:encFormat
   215             flags:flags
   216             status:status
   217             error:error];
   218 }
   219 
   220 - (NSNumber * _Nullable)outgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
   221                                            error:(NSError * _Nullable * _Nullable)error
   222 {
   223     PEPInternalSession *session = [PEPSessionProvider session];
   224     RETURN_ON_ERROR(session, error, nil);
   225     return [session outgoingRatingForMessage:theMessage error:error];
   226 }
   227 
   228 - (NSNumber * _Nullable)outgoingRatingPreviewForMessage:(PEPMessage * _Nonnull)theMessage
   229                                                   error:(NSError * _Nullable * _Nullable)error
   230 {
   231     PEPInternalSession *session = [PEPSessionProvider session];
   232     RETURN_ON_ERROR(session, error, nil);
   233     return [session outgoingRatingPreviewForMessage:theMessage error:error];
   234 }
   235 
   236 - (NSNumber * _Nullable)ratingForIdentity:(PEPIdentity * _Nonnull)identity
   237                                     error:(NSError * _Nullable * _Nullable)error
   238 {
   239     PEPInternalSession *session = [PEPSessionProvider session];
   240     RETURN_ON_ERROR(session, error, nil);
   241     return [session ratingForIdentity:identity error:error];
   242 }
   243 
   244 - (NSArray * _Nullable)trustwordsForFingerprint:(NSString * _Nonnull)fingerprint
   245                                      languageID:(NSString * _Nonnull)languageID
   246                                       shortened:(BOOL)shortened
   247                                           error:(NSError * _Nullable * _Nullable)error
   248 {
   249     PEPInternalSession *session = [PEPSessionProvider session];
   250     RETURN_ON_ERROR(session, error, nil);
   251     return [session
   252             trustwordsForFingerprint:fingerprint
   253             languageID:languageID
   254             shortened:shortened
   255             error:error];
   256 }
   257 
   258 - (BOOL)mySelf:(PEPIdentity * _Nonnull)identity error:(NSError * _Nullable * _Nullable)error
   259 {
   260     PEPInternalSession *session = [PEPSessionProvider session];
   261     RETURN_ON_ERROR(session, error, NO);
   262     return [session mySelf:identity error:error];
   263 }
   264 
   265 - (BOOL)updateIdentity:(PEPIdentity * _Nonnull)identity
   266                  error:(NSError * _Nullable * _Nullable)error
   267 {
   268     PEPInternalSession *session = [PEPSessionProvider session];
   269     RETURN_ON_ERROR(session, error, NO);
   270     return [session updateIdentity:identity error:error];
   271 }
   272 
   273 - (BOOL)trustPersonalKey:(PEPIdentity * _Nonnull)identity
   274                    error:(NSError * _Nullable * _Nullable)error
   275 {
   276     PEPInternalSession *session = [PEPSessionProvider session];
   277     RETURN_ON_ERROR(session, error, NO);
   278     return [session trustPersonalKey:identity error:error];
   279 }
   280 
   281 - (BOOL)keyMistrusted:(PEPIdentity * _Nonnull)identity
   282                 error:(NSError * _Nullable * _Nullable)error
   283 {
   284     PEPInternalSession *session = [PEPSessionProvider session];
   285     RETURN_ON_ERROR(session, error, NO);
   286     return [session keyMistrusted:identity error:error];
   287 }
   288 
   289 - (BOOL)keyResetTrust:(PEPIdentity * _Nonnull)identity
   290                 error:(NSError * _Nullable * _Nullable)error
   291 {
   292     PEPInternalSession *session = [PEPSessionProvider session];
   293     RETURN_ON_ERROR(session, error, NO);
   294     return [session keyResetTrust:identity error:error];
   295 }
   296 
   297 - (BOOL)enableSyncForIdentity:(PEPIdentity * _Nonnull)identity
   298                         error:(NSError * _Nullable * _Nullable)error
   299 {
   300     PEPInternalSession *session = [PEPSessionProvider session];
   301     RETURN_ON_ERROR(session, error, NO);
   302     return [session enableSyncForIdentity:identity error:error];
   303 }
   304 
   305 - (BOOL)disableSyncForIdentity:(PEPIdentity * _Nonnull)identity
   306                          error:(NSError * _Nullable * _Nullable)error
   307 {
   308     PEPInternalSession *session = [PEPSessionProvider session];
   309     RETURN_ON_ERROR(session, error, NO);
   310     return [session disableSyncForIdentity:identity error:error];
   311 }
   312 
   313 - (NSNumber * _Nullable)queryKeySyncEnabledForIdentity:(PEPIdentity * _Nonnull)identity
   314                                                  error:(NSError * _Nullable * _Nullable)error
   315 {
   316     PEPInternalSession *session = [PEPSessionProvider session];
   317     RETURN_ON_ERROR(session, error, nil);
   318     return [session queryKeySyncEnabledForIdentity:identity error:error];
   319 }
   320 
   321 #pragma mark Internal API (testing etc.)
   322 
   323 - (NSArray * _Nullable)importKey:(NSString * _Nonnull)keydata
   324                            error:(NSError * _Nullable * _Nullable)error
   325 {
   326     PEPInternalSession *session = [PEPSessionProvider session];
   327     RETURN_ON_ERROR(session, error, nil);
   328     return [session importKey:keydata error:error];
   329 }
   330 
   331 - (BOOL)logTitle:(NSString * _Nonnull)title
   332           entity:(NSString * _Nonnull)entity
   333      description:(NSString * _Nullable)description
   334          comment:(NSString * _Nullable)comment
   335            error:(NSError * _Nullable * _Nullable)error
   336 {
   337     PEPInternalSession *session = [PEPSessionProvider session];
   338     RETURN_ON_ERROR(session, error, NO);
   339     return [session
   340             logTitle:title
   341             entity:entity
   342             description:description
   343             comment:comment
   344             error:error];
   345 }
   346 
   347 - (NSString * _Nullable)getLogWithError:(NSError * _Nullable * _Nullable)error
   348 {
   349     PEPInternalSession *session = [PEPSessionProvider session];
   350     RETURN_ON_ERROR(session, error, nil);
   351     return [session getLogWithError:error];
   352 }
   353 
   354 - (NSString * _Nullable)getTrustwordsIdentity1:(PEPIdentity * _Nonnull)identity1
   355                                      identity2:(PEPIdentity * _Nonnull)identity2
   356                                       language:(NSString * _Nullable)language
   357                                           full:(BOOL)full
   358                                          error:(NSError * _Nullable * _Nullable)error
   359 {
   360     PEPInternalSession *session = [PEPSessionProvider session];
   361     RETURN_ON_ERROR(session, error, nil);
   362     return [session getTrustwordsIdentity1:identity1
   363                                  identity2:identity2
   364                                   language:language
   365                                       full:full
   366                                      error:error];
   367 }
   368 
   369 - (NSString * _Nullable)getTrustwordsFpr1:(NSString * _Nonnull)fpr1
   370                                      fpr2:(NSString * _Nonnull)fpr2
   371                                  language:(NSString * _Nullable)language
   372                                      full:(BOOL)full
   373                                     error:(NSError * _Nullable * _Nullable)error
   374 {
   375     PEPInternalSession *session = [PEPSessionProvider session];
   376     RETURN_ON_ERROR(session, error, nil);
   377     return [session getTrustwordsFpr1:fpr1 fpr2:fpr2 language:language full:full error:error];
   378 }
   379 
   380 - (NSArray<PEPLanguage *> * _Nullable)languageListWithError:(NSError * _Nullable * _Nullable)error
   381 {
   382     PEPInternalSession *session = [PEPSessionProvider session];
   383     RETURN_ON_ERROR(session, error, nil);
   384     return [session languageListWithError:error];
   385 }
   386 
   387 - (PEPRating)ratingFromString:(NSString * _Nonnull)string
   388 {
   389     PEPInternalSession *session = [PEPSessionProvider session];
   390     if (session == nil) {
   391         return PEPRatingUndefined;
   392     }
   393     return [session ratingFromString:string];
   394 }
   395 
   396 - (NSString * _Nonnull)stringFromRating:(PEPRating)rating
   397 {
   398     PEPInternalSession *session = [PEPSessionProvider session];
   399     if (session == nil) {
   400         return kUndefined;
   401     }
   402     return [session stringFromRating:rating];
   403 }
   404 
   405 - (NSNumber * _Nullable)isPEPUser:(PEPIdentity * _Nonnull)identity
   406                             error:(NSError * _Nullable * _Nullable)error
   407 {
   408     PEPInternalSession *session = [PEPSessionProvider session];
   409     RETURN_ON_ERROR(session, error, nil);
   410     return [session isPEPUser:identity error:error];
   411 }
   412 
   413 - (BOOL)setOwnKey:(PEPIdentity * _Nonnull)identity fingerprint:(NSString * _Nonnull)fingerprint
   414             error:(NSError * _Nullable * _Nullable)error
   415 {
   416     PEPInternalSession *session = [PEPSessionProvider session];
   417     RETURN_ON_ERROR(session, error, NO);
   418     return [session setOwnKey:identity fingerprint:fingerprint error:error];
   419 }
   420 
   421 - (void)configurePassiveModeEnabled:(BOOL)enabled
   422 {
   423     PEPInternalSession *session = [PEPSessionProvider session];
   424     return [session configurePassiveModeEnabled:enabled];
   425 }
   426 
   427 - (BOOL)setFlags:(PEPIdentityFlags)flags
   428      forIdentity:(PEPIdentity *)identity
   429            error:(NSError * _Nullable * _Nullable)error
   430 {
   431     PEPInternalSession *session = [PEPSessionProvider session];
   432     RETURN_ON_ERROR(session, error, NO);
   433     return [session setFlags:flags forIdentity:identity error:error];
   434 }
   435 
   436 - (BOOL)trustOwnKeyIdentity:(PEPIdentity * _Nonnull)identity
   437                       error:(NSError * _Nullable * _Nullable)error
   438 {
   439     PEPInternalSession *session = [PEPSessionProvider session];
   440     RETURN_ON_ERROR(session, error, NO);
   441     return [session trustOwnKeyIdentity:identity error:error];
   442 }
   443 
   444 - (BOOL)deliverHandshakeResult:(PEPSyncHandshakeResult)result
   445              identitiesSharing:(NSArray<PEPIdentity *> * _Nullable)identitiesSharing
   446                          error:(NSError * _Nullable * _Nullable)error;
   447 {
   448     PEPInternalSession *session = [PEPSessionProvider session];
   449     RETURN_ON_ERROR(session, error, NO);
   450     return [session deliverHandshakeResult:result identitiesSharing:identitiesSharing error:error];
   451 }
   452 
   453 - (PEPColor)colorFromRating:(PEPRating)rating
   454 {
   455     PEPInternalSession *session = [PEPSessionProvider session];
   456     if (session == nil) {
   457         return PEPColorNoColor;
   458     }
   459     return [session colorFromRating:rating];
   460 }
   461 
   462 - (BOOL)keyReset:(PEPIdentity * _Nonnull)identity
   463      fingerprint:(NSString * _Nullable)fingerprint
   464            error:(NSError * _Nullable * _Nullable)error
   465 {
   466     PEPInternalSession *session = [PEPSessionProvider session];
   467     RETURN_ON_ERROR(session, error, NO);
   468     return [session keyReset:identity fingerprint:fingerprint error:error];
   469 }
   470 
   471 - (BOOL)leaveDeviceGroupError:(NSError * _Nullable * _Nullable)error
   472 {
   473     PEPInternalSession *session = [PEPSessionProvider session];
   474     RETURN_ON_ERROR(session, error, NO);
   475     return [session leaveDeviceGroupError:error];
   476 }
   477 
   478 @end