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