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