pEpObjCAdapter/PEPSession.m
author Dirk Zimmermann <dz@pep.security>
Tue, 03 Dec 2019 14:07:27 +0100
branchIOSAD-160
changeset 1372 ac119be204d2
parent 1368 1074d7bd40ba
child 1387 b0e0c5afc224
permissions -rw-r--r--
IOSAD-160 Add 2nd myself instead of changing the original one
     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 - (PEPMessage * _Nullable)decryptMessage:(PEPMessage * _Nonnull)message
    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             decryptMessage:message
    55             flags:flags
    56             rating:rating
    57             extraKeys:extraKeys
    58             status:status
    59             error:error];
    60 }
    61 
    62 - (BOOL)reEvaluateMessage:(PEPMessage * _Nonnull)message
    63                  xKeyList:(PEPStringList * _Nullable)xKeyList
    64                    rating:(PEPRating * _Nonnull)rating
    65                    status:(PEPStatus * _Nullable)status
    66                     error:(NSError * _Nullable * _Nullable)error
    67 {
    68     PEPInternalSession *session = [PEPSessionProvider session];
    69     RETURN_ON_ERROR(session, error, NO);
    70     return [session reEvaluateMessage:message
    71                              xKeyList:xKeyList
    72                                rating:rating
    73                                status:status
    74                                 error:error];
    75 }
    76 
    77 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
    78                                extraKeys:(PEPStringList * _Nullable)extraKeys
    79                                encFormat:(PEPEncFormat)encFormat
    80                                   status:(PEPStatus * _Nullable)status
    81                                    error:(NSError * _Nullable * _Nullable)error
    82 {
    83     PEPInternalSession *session = [PEPSessionProvider session];
    84     RETURN_ON_ERROR(session, error, nil);
    85     return [session
    86             encryptMessage:message
    87             extraKeys:extraKeys
    88             encFormat:encFormat
    89             status:status
    90             error:error];
    91 }
    92 
    93 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
    94                                extraKeys:(PEPStringList * _Nullable)extraKeys
    95                                   status:(PEPStatus * _Nullable)status
    96                                    error:(NSError * _Nullable * _Nullable)error
    97 {
    98     PEPInternalSession *session = [PEPSessionProvider session];
    99     RETURN_ON_ERROR(session, error, nil);
   100     return [session encryptMessage:message extraKeys:extraKeys status:status error:error];
   101 }
   102 
   103 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   104                                  forSelf:(PEPIdentity * _Nonnull)ownIdentity
   105                                extraKeys:(PEPStringList * _Nullable)extraKeys
   106                                   status:(PEPStatus * _Nullable)status
   107                                    error:(NSError * _Nullable * _Nullable)error
   108 {
   109     PEPInternalSession *session = [PEPSessionProvider session];
   110     RETURN_ON_ERROR(session, error, nil);
   111     return [session
   112             encryptMessage:message
   113             forSelf:ownIdentity
   114             extraKeys:extraKeys
   115             status:status
   116             error:error];
   117 }
   118 
   119 - (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
   120                                    toFpr:(NSString * _Nonnull)toFpr
   121                                encFormat:(PEPEncFormat)encFormat
   122                                    flags:(PEPDecryptFlags)flags
   123                                   status:(PEPStatus * _Nullable)status
   124                                    error:(NSError * _Nullable * _Nullable)error
   125 {
   126     PEPInternalSession *session = [PEPSessionProvider session];
   127     RETURN_ON_ERROR(session, error, nil);
   128     return [session
   129             encryptMessage:message
   130             toFpr:toFpr
   131             encFormat:encFormat
   132             flags:flags
   133             status:status
   134             error:error];
   135 }
   136 
   137 - (NSNumber * _Nullable)outgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
   138                                            error:(NSError * _Nullable * _Nullable)error
   139 {
   140     PEPInternalSession *session = [PEPSessionProvider session];
   141     RETURN_ON_ERROR(session, error, nil);
   142     return [session outgoingRatingForMessage:theMessage error:error];
   143 }
   144 
   145 - (NSNumber * _Nullable)outgoingRatingPreviewForMessage:(PEPMessage * _Nonnull)theMessage
   146                                                   error:(NSError * _Nullable * _Nullable)error
   147 {
   148     PEPInternalSession *session = [PEPSessionProvider session];
   149     RETURN_ON_ERROR(session, error, nil);
   150     return [session outgoingRatingPreviewForMessage:theMessage error:error];
   151 }
   152 
   153 - (NSNumber * _Nullable)ratingForIdentity:(PEPIdentity * _Nonnull)identity
   154                                     error:(NSError * _Nullable * _Nullable)error
   155 {
   156     PEPInternalSession *session = [PEPSessionProvider session];
   157     RETURN_ON_ERROR(session, error, nil);
   158     return [session ratingForIdentity:identity error:error];
   159 }
   160 
   161 - (NSArray * _Nullable)trustwordsForFingerprint:(NSString * _Nonnull)fingerprint
   162                                      languageID:(NSString * _Nonnull)languageID
   163                                       shortened:(BOOL)shortened
   164                                           error:(NSError * _Nullable * _Nullable)error
   165 {
   166     PEPInternalSession *session = [PEPSessionProvider session];
   167     RETURN_ON_ERROR(session, error, nil);
   168     return [session
   169             trustwordsForFingerprint:fingerprint
   170             languageID:languageID
   171             shortened:shortened
   172             error:error];
   173 }
   174 
   175 - (BOOL)mySelf:(PEPIdentity * _Nonnull)identity
   176          error:(NSError * _Nullable * _Nullable)error {
   177     PEPInternalSession *session = [PEPSessionProvider session];
   178     RETURN_ON_ERROR(session, error, NO);
   179     return [session mySelf:identity error:error];
   180 }
   181 
   182 - (BOOL)mySelf:(PEPIdentity * _Nonnull)identity
   183 pEpSyncEnabled:(BOOL)pEpSyncEnabled
   184          error:(NSError * _Nullable * _Nullable)error {
   185     PEPInternalSession *session = [PEPSessionProvider session];
   186     RETURN_ON_ERROR(session, error, NO);
   187     return [session mySelf:identity pEpSyncEnabled:pEpSyncEnabled error:error];
   188 }
   189 
   190 - (BOOL)updateIdentity:(PEPIdentity * _Nonnull)identity
   191                  error:(NSError * _Nullable * _Nullable)error
   192 {
   193     PEPInternalSession *session = [PEPSessionProvider session];
   194     RETURN_ON_ERROR(session, error, NO);
   195     return [session updateIdentity:identity error:error];
   196 }
   197 
   198 - (BOOL)trustPersonalKey:(PEPIdentity * _Nonnull)identity
   199                    error:(NSError * _Nullable * _Nullable)error
   200 {
   201     PEPInternalSession *session = [PEPSessionProvider session];
   202     RETURN_ON_ERROR(session, error, NO);
   203     return [session trustPersonalKey:identity error:error];
   204 }
   205 
   206 - (BOOL)keyMistrusted:(PEPIdentity * _Nonnull)identity
   207                 error:(NSError * _Nullable * _Nullable)error
   208 {
   209     PEPInternalSession *session = [PEPSessionProvider session];
   210     RETURN_ON_ERROR(session, error, NO);
   211     return [session keyMistrusted:identity error:error];
   212 }
   213 
   214 - (BOOL)keyResetTrust:(PEPIdentity * _Nonnull)identity
   215                 error:(NSError * _Nullable * _Nullable)error
   216 {
   217     PEPInternalSession *session = [PEPSessionProvider session];
   218     RETURN_ON_ERROR(session, error, NO);
   219     return [session keyResetTrust:identity error:error];
   220 }
   221 
   222 - (BOOL)enableSyncForIdentity:(PEPIdentity * _Nonnull)identity
   223                         error:(NSError * _Nullable * _Nullable)error
   224 {
   225     PEPInternalSession *session = [PEPSessionProvider session];
   226     RETURN_ON_ERROR(session, error, NO);
   227     return [session enableSyncForIdentity:identity error:error];
   228 }
   229 
   230 - (BOOL)disableSyncForIdentity:(PEPIdentity * _Nonnull)identity
   231                          error:(NSError * _Nullable * _Nullable)error
   232 {
   233     PEPInternalSession *session = [PEPSessionProvider session];
   234     RETURN_ON_ERROR(session, error, NO);
   235     return [session disableSyncForIdentity:identity error:error];
   236 }
   237 
   238 - (NSNumber * _Nullable)queryKeySyncEnabledForIdentity:(PEPIdentity * _Nonnull)identity
   239                                                  error:(NSError * _Nullable * _Nullable)error
   240 {
   241     PEPInternalSession *session = [PEPSessionProvider session];
   242     RETURN_ON_ERROR(session, error, nil);
   243     return [session queryKeySyncEnabledForIdentity:identity error:error];
   244 }
   245 
   246 #pragma mark Internal API (testing etc.)
   247 
   248 - (NSArray * _Nullable)importKey:(NSString * _Nonnull)keydata
   249                            error:(NSError * _Nullable * _Nullable)error
   250 {
   251     PEPInternalSession *session = [PEPSessionProvider session];
   252     RETURN_ON_ERROR(session, error, nil);
   253     return [session importKey:keydata error:error];
   254 }
   255 
   256 - (BOOL)logTitle:(NSString * _Nonnull)title
   257           entity:(NSString * _Nonnull)entity
   258      description:(NSString * _Nullable)description
   259          comment:(NSString * _Nullable)comment
   260            error:(NSError * _Nullable * _Nullable)error
   261 {
   262     PEPInternalSession *session = [PEPSessionProvider session];
   263     RETURN_ON_ERROR(session, error, NO);
   264     return [session
   265             logTitle:title
   266             entity:entity
   267             description:description
   268             comment:comment
   269             error:error];
   270 }
   271 
   272 - (NSString * _Nullable)getLogWithError:(NSError * _Nullable * _Nullable)error
   273 {
   274     PEPInternalSession *session = [PEPSessionProvider session];
   275     RETURN_ON_ERROR(session, error, nil);
   276     return [session getLogWithError:error];
   277 }
   278 
   279 - (NSString * _Nullable)getTrustwordsIdentity1:(PEPIdentity * _Nonnull)identity1
   280                                      identity2:(PEPIdentity * _Nonnull)identity2
   281                                       language:(NSString * _Nullable)language
   282                                           full:(BOOL)full
   283                                          error:(NSError * _Nullable * _Nullable)error
   284 {
   285     PEPInternalSession *session = [PEPSessionProvider session];
   286     RETURN_ON_ERROR(session, error, nil);
   287     return [session getTrustwordsIdentity1:identity1
   288                                  identity2:identity2
   289                                   language:language
   290                                       full:full
   291                                      error:error];
   292 }
   293 
   294 - (NSString * _Nullable)getTrustwordsFpr1:(NSString * _Nonnull)fpr1
   295                                      fpr2:(NSString * _Nonnull)fpr2
   296                                  language:(NSString * _Nullable)language
   297                                      full:(BOOL)full
   298                                     error:(NSError * _Nullable * _Nullable)error
   299 {
   300     PEPInternalSession *session = [PEPSessionProvider session];
   301     RETURN_ON_ERROR(session, error, nil);
   302     return [session getTrustwordsFpr1:fpr1 fpr2:fpr2 language:language full:full error:error];
   303 }
   304 
   305 - (NSArray<PEPLanguage *> * _Nullable)languageListWithError:(NSError * _Nullable * _Nullable)error
   306 {
   307     PEPInternalSession *session = [PEPSessionProvider session];
   308     RETURN_ON_ERROR(session, error, nil);
   309     return [session languageListWithError:error];
   310 }
   311 
   312 - (PEPRating)ratingFromString:(NSString * _Nonnull)string
   313 {
   314     PEPInternalSession *session = [PEPSessionProvider session];
   315     if (session == nil) {
   316         return PEPRatingUndefined;
   317     }
   318     return [session ratingFromString:string];
   319 }
   320 
   321 - (NSString * _Nonnull)stringFromRating:(PEPRating)rating
   322 {
   323     PEPInternalSession *session = [PEPSessionProvider session];
   324     if (session == nil) {
   325         return kUndefined;
   326     }
   327     return [session stringFromRating:rating];
   328 }
   329 
   330 - (NSNumber * _Nullable)isPEPUser:(PEPIdentity * _Nonnull)identity
   331                             error:(NSError * _Nullable * _Nullable)error
   332 {
   333     PEPInternalSession *session = [PEPSessionProvider session];
   334     RETURN_ON_ERROR(session, error, nil);
   335     return [session isPEPUser:identity error:error];
   336 }
   337 
   338 - (BOOL)setOwnKey:(PEPIdentity * _Nonnull)identity fingerprint:(NSString * _Nonnull)fingerprint
   339             error:(NSError * _Nullable * _Nullable)error
   340 {
   341     PEPInternalSession *session = [PEPSessionProvider session];
   342     RETURN_ON_ERROR(session, error, NO);
   343     return [session setOwnKey:identity fingerprint:fingerprint error:error];
   344 }
   345 
   346 - (void)configurePassiveModeEnabled:(BOOL)enabled
   347 {
   348     PEPInternalSession *session = [PEPSessionProvider session];
   349     return [session configurePassiveModeEnabled:enabled];
   350 }
   351 
   352 - (BOOL)setFlags:(PEPIdentityFlags)flags
   353      forIdentity:(PEPIdentity *)identity
   354            error:(NSError * _Nullable * _Nullable)error
   355 {
   356     PEPInternalSession *session = [PEPSessionProvider session];
   357     RETURN_ON_ERROR(session, error, NO);
   358     return [session setFlags:flags forIdentity:identity error:error];
   359 }
   360 
   361 - (BOOL)trustOwnKeyIdentity:(PEPIdentity * _Nonnull)identity
   362                       error:(NSError * _Nullable * _Nullable)error
   363 {
   364     PEPInternalSession *session = [PEPSessionProvider session];
   365     RETURN_ON_ERROR(session, error, NO);
   366     return [session trustOwnKeyIdentity:identity error:error];
   367 }
   368 
   369 - (BOOL)deliverHandshakeResult:(PEPSyncHandshakeResult)result
   370              identitiesSharing:(NSArray<PEPIdentity *> * _Nullable)identitiesSharing
   371                          error:(NSError * _Nullable * _Nullable)error;
   372 {
   373     PEPInternalSession *session = [PEPSessionProvider session];
   374     RETURN_ON_ERROR(session, error, NO);
   375     return [session deliverHandshakeResult:result identitiesSharing:identitiesSharing error:error];
   376 }
   377 
   378 - (PEPColor)colorFromRating:(PEPRating)rating
   379 {
   380     PEPInternalSession *session = [PEPSessionProvider session];
   381     if (session == nil) {
   382         return PEPColorNoColor;
   383     }
   384     return [session colorFromRating:rating];
   385 }
   386 
   387 - (BOOL)keyReset:(PEPIdentity * _Nonnull)identity
   388      fingerprint:(NSString * _Nullable)fingerprint
   389            error:(NSError * _Nullable * _Nullable)error
   390 {
   391     PEPInternalSession *session = [PEPSessionProvider session];
   392     RETURN_ON_ERROR(session, error, NO);
   393     return [session keyReset:identity fingerprint:fingerprint error:error];
   394 }
   395 
   396 - (BOOL)leaveDeviceGroup:(NSError * _Nullable * _Nullable)error
   397 {
   398     PEPInternalSession *session = [PEPSessionProvider session];
   399     RETURN_ON_ERROR(session, error, NO);
   400     return [session leaveDeviceGroup:error];
   401 }
   402 
   403 - (BOOL)keyResetAllOwnKeysError:(NSError * _Nullable * _Nullable)error
   404 {
   405     PEPInternalSession *session = [PEPSessionProvider session];
   406     RETURN_ON_ERROR(session, error, NO);
   407     return [session keyResetAllOwnKeysError:error];
   408 }
   409 
   410 @end