pEpObjCAdapter/PEPSession.m
author Dirk Zimmermann <dz@pep.security>
Thu, 06 Aug 2020 17:29:00 +0200
branchIOSAD-180
changeset 1992 e905069c5952
parent 1586 579a553946bf
child 1686 0675495157e8
permissions -rw-r--r--
IOSAD-180 Let ensure_passphrase use PEPPassphraseUtil
     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)updateIdentity:(PEPIdentity * _Nonnull)identity
   183                  error:(NSError * _Nullable * _Nullable)error
   184 {
   185     PEPInternalSession *session = [PEPSessionProvider session];
   186     RETURN_ON_ERROR(session, error, NO);
   187     return [session updateIdentity:identity error:error];
   188 }
   189 
   190 - (BOOL)trustPersonalKey:(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 trustPersonalKey:identity error:error];
   196 }
   197 
   198 - (BOOL)keyMistrusted:(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 keyMistrusted:identity error:error];
   204 }
   205 
   206 - (BOOL)keyResetTrust:(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 keyResetTrust:identity error:error];
   212 }
   213 
   214 - (BOOL)enableSyncForIdentity:(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 enableSyncForIdentity:identity error:error];
   220 }
   221 
   222 - (BOOL)disableSyncForIdentity:(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 disableSyncForIdentity:identity error:error];
   228 }
   229 
   230 - (NSNumber * _Nullable)queryKeySyncEnabledForIdentity:(PEPIdentity * _Nonnull)identity
   231                                                  error:(NSError * _Nullable * _Nullable)error
   232 {
   233     PEPInternalSession *session = [PEPSessionProvider session];
   234     RETURN_ON_ERROR(session, error, nil);
   235     return [session queryKeySyncEnabledForIdentity:identity error:error];
   236 }
   237 
   238 #pragma mark Internal API (testing etc.)
   239 
   240 - (NSArray * _Nullable)importKey:(NSString * _Nonnull)keydata
   241                            error:(NSError * _Nullable * _Nullable)error
   242 {
   243     PEPInternalSession *session = [PEPSessionProvider session];
   244     RETURN_ON_ERROR(session, error, nil);
   245     return [session importKey:keydata error:error];
   246 }
   247 
   248 - (BOOL)logTitle:(NSString * _Nonnull)title
   249           entity:(NSString * _Nonnull)entity
   250      description:(NSString * _Nullable)description
   251          comment:(NSString * _Nullable)comment
   252            error:(NSError * _Nullable * _Nullable)error
   253 {
   254     PEPInternalSession *session = [PEPSessionProvider session];
   255     RETURN_ON_ERROR(session, error, NO);
   256     return [session
   257             logTitle:title
   258             entity:entity
   259             description:description
   260             comment:comment
   261             error:error];
   262 }
   263 
   264 - (NSString * _Nullable)getLogWithError:(NSError * _Nullable * _Nullable)error
   265 {
   266     PEPInternalSession *session = [PEPSessionProvider session];
   267     RETURN_ON_ERROR(session, error, nil);
   268     return [session getLogWithError:error];
   269 }
   270 
   271 - (NSString * _Nullable)getTrustwordsIdentity1:(PEPIdentity * _Nonnull)identity1
   272                                      identity2:(PEPIdentity * _Nonnull)identity2
   273                                       language:(NSString * _Nullable)language
   274                                           full:(BOOL)full
   275                                          error:(NSError * _Nullable * _Nullable)error
   276 {
   277     PEPInternalSession *session = [PEPSessionProvider session];
   278     RETURN_ON_ERROR(session, error, nil);
   279     return [session getTrustwordsIdentity1:identity1
   280                                  identity2:identity2
   281                                   language:language
   282                                       full:full
   283                                      error:error];
   284 }
   285 
   286 - (NSString * _Nullable)getTrustwordsFpr1:(NSString * _Nonnull)fpr1
   287                                      fpr2:(NSString * _Nonnull)fpr2
   288                                  language:(NSString * _Nullable)language
   289                                      full:(BOOL)full
   290                                     error:(NSError * _Nullable * _Nullable)error
   291 {
   292     PEPInternalSession *session = [PEPSessionProvider session];
   293     RETURN_ON_ERROR(session, error, nil);
   294     return [session getTrustwordsFpr1:fpr1 fpr2:fpr2 language:language full:full error:error];
   295 }
   296 
   297 - (NSArray<PEPLanguage *> * _Nullable)languageListWithError:(NSError * _Nullable * _Nullable)error
   298 {
   299     PEPInternalSession *session = [PEPSessionProvider session];
   300     RETURN_ON_ERROR(session, error, nil);
   301     return [session languageListWithError:error];
   302 }
   303 
   304 - (PEPRating)ratingFromString:(NSString * _Nonnull)string
   305 {
   306     PEPInternalSession *session = [PEPSessionProvider session];
   307     if (session == nil) {
   308         return PEPRatingUndefined;
   309     }
   310     return [session ratingFromString:string];
   311 }
   312 
   313 - (NSString * _Nonnull)stringFromRating:(PEPRating)rating
   314 {
   315     PEPInternalSession *session = [PEPSessionProvider session];
   316     if (session == nil) {
   317         return kUndefined;
   318     }
   319     return [session stringFromRating:rating];
   320 }
   321 
   322 - (NSNumber * _Nullable)isPEPUser:(PEPIdentity * _Nonnull)identity
   323                             error:(NSError * _Nullable * _Nullable)error
   324 {
   325     PEPInternalSession *session = [PEPSessionProvider session];
   326     RETURN_ON_ERROR(session, error, nil);
   327     return [session isPEPUser:identity error:error];
   328 }
   329 
   330 - (BOOL)setOwnKey:(PEPIdentity * _Nonnull)identity fingerprint:(NSString * _Nonnull)fingerprint
   331             error:(NSError * _Nullable * _Nullable)error
   332 {
   333     PEPInternalSession *session = [PEPSessionProvider session];
   334     RETURN_ON_ERROR(session, error, NO);
   335     return [session setOwnKey:identity fingerprint:fingerprint error:error];
   336 }
   337 
   338 - (void)configurePassiveModeEnabled:(BOOL)enabled
   339 {
   340     PEPInternalSession *session = [PEPSessionProvider session];
   341     return [session configurePassiveModeEnabled:enabled];
   342 }
   343 
   344 - (BOOL)setFlags:(PEPIdentityFlags)flags
   345      forIdentity:(PEPIdentity *)identity
   346            error:(NSError * _Nullable * _Nullable)error
   347 {
   348     PEPInternalSession *session = [PEPSessionProvider session];
   349     RETURN_ON_ERROR(session, error, NO);
   350     return [session setFlags:flags forIdentity:identity error:error];
   351 }
   352 
   353 - (BOOL)trustOwnKeyIdentity:(PEPIdentity * _Nonnull)identity
   354                       error:(NSError * _Nullable * _Nullable)error
   355 {
   356     PEPInternalSession *session = [PEPSessionProvider session];
   357     RETURN_ON_ERROR(session, error, NO);
   358     return [session trustOwnKeyIdentity:identity error:error];
   359 }
   360 
   361 - (BOOL)deliverHandshakeResult:(PEPSyncHandshakeResult)result
   362              identitiesSharing:(NSArray<PEPIdentity *> * _Nullable)identitiesSharing
   363                          error:(NSError * _Nullable * _Nullable)error;
   364 {
   365     PEPInternalSession *session = [PEPSessionProvider session];
   366     RETURN_ON_ERROR(session, error, NO);
   367     return [session deliverHandshakeResult:result identitiesSharing:identitiesSharing error:error];
   368 }
   369 
   370 - (PEPColor)colorFromRating:(PEPRating)rating
   371 {
   372     PEPInternalSession *session = [PEPSessionProvider session];
   373     if (session == nil) {
   374         return PEPColorNoColor;
   375     }
   376     return [session colorFromRating:rating];
   377 }
   378 
   379 - (BOOL)keyReset:(PEPIdentity * _Nonnull)identity
   380      fingerprint:(NSString * _Nullable)fingerprint
   381            error:(NSError * _Nullable * _Nullable)error
   382 {
   383     PEPInternalSession *session = [PEPSessionProvider session];
   384     RETURN_ON_ERROR(session, error, NO);
   385     return [session keyReset:identity fingerprint:fingerprint error:error];
   386 }
   387 
   388 - (BOOL)leaveDeviceGroup:(NSError * _Nullable * _Nullable)error
   389 {
   390     PEPInternalSession *session = [PEPSessionProvider session];
   391     RETURN_ON_ERROR(session, error, NO);
   392     return [session leaveDeviceGroup:error];
   393 }
   394 
   395 - (BOOL)keyResetAllOwnKeysError:(NSError * _Nullable * _Nullable)error
   396 {
   397     PEPInternalSession *session = [PEPSessionProvider session];
   398     RETURN_ON_ERROR(session, error, NO);
   399     return [session keyResetAllOwnKeysError:error];
   400 }
   401 
   402 - (BOOL)configurePassphrase:(NSString * _Nonnull)passphrase
   403                       error:(NSError * _Nullable __autoreleasing * _Nullable)error {
   404     PEPInternalSession *session = [PEPSessionProvider session];
   405     RETURN_ON_ERROR(session, error, NO);
   406     return [session configurePassphrase:passphrase error:error];
   407 }
   408 
   409 
   410 - (BOOL)configurePassphraseForNewKeys:(NSString * _Nullable)passphrase
   411                                enable:(BOOL)enable
   412                                 error:(NSError * _Nullable __autoreleasing * _Nullable)error {
   413     PEPInternalSession *session = [PEPSessionProvider session];
   414     RETURN_ON_ERROR(session, error, NO);
   415     return [session configurePassphraseForNewKeys:passphrase enable:enable error:error];
   416 }
   417 
   418 @end