pEpObjCAdapter/PEPInternalSession.m
author Dirk Zimmermann <dz@pep.security>
Thu, 12 Sep 2019 16:41:52 +0200
branchIOSAD-141
changeset 1245 cd15ff3a40b9
parent 1241 3b625296d61e
child 1247 ec96c9c3248e
permissions -rw-r--r--
IOSAD-141 Don't share session between threads.
andreas@269
     1
//
andreas@269
     2
//  PEPSession.m
andreas@269
     3
//  pEpiOSAdapter
andreas@269
     4
//
andreas@269
     5
//  Created by Volker Birk on 08.07.15.
andreas@269
     6
//  Copyright (c) 2015 p≡p. All rights reserved.
andreas@269
     7
//
andreas@269
     8
andreas@269
     9
#import "PEPInternalSession.h"
dz@828
    10
dz@902
    11
#import "PEPConstants.h"
dz@851
    12
andreas@269
    13
#import "PEPObjCAdapter.h"
andreas@269
    14
#import "PEPObjCAdapter+Internal.h"
dirk@367
    15
#import "PEPMessageUtil.h"
andreas@269
    16
#import "PEPLanguage.h"
andreas@269
    17
#import "PEPCSVScanner.h"
andreas@269
    18
#import "NSArray+Extension.h"
dz@865
    19
#import "NSDictionary+CommType.h"
dirk@622
    20
#import "NSDictionary+Debug.h"
dz@902
    21
#import "PEPIdentity.h"
dirk@379
    22
#import "PEPMessage.h"
dz@831
    23
#import "NSError+PEP+Internal.h"
dirk@564
    24
#import "PEPAutoPointer.h"
dirk@573
    25
#import "NSNumber+PEPRating.h"
dirk@598
    26
#import "NSMutableDictionary+PEP.h"
dz@952
    27
#import "PEPSync_Internal.h"
dz@1111
    28
#import "PEPInternalConstants.h"
andreas@269
    29
dz@930
    30
#import "key_reset.h"
dz@930
    31
andreas@269
    32
@implementation PEPInternalSession
andreas@269
    33
dz@1104
    34
- (_Nullable instancetype)init
andreas@269
    35
{
andreas@274
    36
    self = [super init];
andreas@274
    37
    if (self) {
andreas@274
    38
        [PEPInternalSession setupTrustWordsDB];
andreas@269
    39
dirk@725
    40
        NSError *error = nil;
dirk@725
    41
        _session = [PEPSync createSession:&error];
andreas@269
    42
dirk@725
    43
        if (error) {
andreas@274
    44
            return nil;
andreas@274
    45
        }
andreas@269
    46
    }
andreas@269
    47
    return self;
andreas@269
    48
}
andreas@269
    49
andreas@269
    50
- (void)dealloc
andreas@269
    51
{
dz@1103
    52
    if (_session != nil) {
dz@1103
    53
        release(_session);
dz@1103
    54
    }
andreas@269
    55
}
andreas@269
    56
andreas@416
    57
#pragma mark - CONFIG
andreas@416
    58
dirk@601
    59
- (void)configUnEncryptedSubjectEnabled:(BOOL)enabled;
andreas@416
    60
{
andreas@416
    61
    config_unencrypted_subject(self.session, enabled);
andreas@416
    62
}
andreas@416
    63
andreas@416
    64
#pragma mark - INTERNAL
andreas@416
    65
andreas@416
    66
+ (void)setupTrustWordsDB
andreas@416
    67
{
andreas@416
    68
    static dispatch_once_t once;
andreas@416
    69
    dispatch_once(&once, ^{
andreas@416
    70
        [PEPObjCAdapter setupTrustWordsDB:[NSBundle bundleForClass:[self class]]];
andreas@416
    71
    });
andreas@416
    72
}
andreas@416
    73
andreas@416
    74
#pragma mark - DEBUG UTILS
andreas@416
    75
andreas@269
    76
/**
andreas@269
    77
 Saves the given message dict as a plist to the local filesystem
andreas@269
    78
 (directly under NSApplicationSupportDirectory).
andreas@269
    79
 Since the complete output file will be logged by `debugSaveToFilePath`,
andreas@269
    80
 you can get access to the files easily when it's the simulator.
andreas@269
    81
 */
dirk@535
    82
- (void)debugOutPutMessageDict:(PEPDict * _Nonnull)src
andreas@269
    83
{
andreas@269
    84
    NSString *from = src[kPepFrom][kPepAddress];
andreas@269
    85
    NSArray *tos = src[kPepTo];
andreas@269
    86
    NSString *to = tos[0][kPepAddress];
andreas@269
    87
    NSString *msgID = src[kPepID];
andreas@269
    88
    NSString *fileName = [NSString stringWithFormat:@"%@_from(%@)_%@",
andreas@269
    89
                          to, from, msgID];
andreas@269
    90
    [src debugSaveToFilePath:fileName];
andreas@269
    91
}
andreas@269
    92
andreas@416
    93
#pragma mark - PEPSessionProtocol
andreas@416
    94
dirk@588
    95
void decryptMessageDictFree(message *src, message *dst, stringlist_t *extraKeys)
dirk@588
    96
{
dirk@588
    97
    free_message(src);
dirk@588
    98
    free_message(dst);
dirk@588
    99
    free_stringlist(extraKeys);
dirk@588
   100
}
dirk@588
   101
dirk@596
   102
- (PEPDict * _Nullable)decryptMessageDict:(PEPMutableDict * _Nonnull)messageDict
dz@829
   103
                                    flags:(PEPDecryptFlags * _Nullable)flags
dz@830
   104
                                   rating:(PEPRating * _Nullable)rating
dirk@523
   105
                                extraKeys:(PEPStringList * _Nullable * _Nullable)extraKeys
dz@829
   106
                                   status:(PEPStatus * _Nullable)status
dirk@517
   107
                                    error:(NSError * _Nullable * _Nullable)error
andreas@269
   108
{
dirk@514
   109
    if (rating) {
dz@830
   110
        *rating = PEPRatingUndefined;
dirk@514
   111
    }
dirk@514
   112
dirk@522
   113
    message *_src = PEP_messageDictToStruct(messageDict);
dirk@467
   114
    message *_dst = NULL;
dirk@605
   115
    stringlist_t *theKeys = NULL;
dz@829
   116
    PEPDecryptFlags theFlags = 0;
dirk@583
   117
dirk@583
   118
    if (flags) {
dirk@583
   119
        theFlags = *flags;
dirk@583
   120
    }
andreas@269
   121
dirk@605
   122
    if (extraKeys && [*extraKeys count]) {
dirk@605
   123
        theKeys = PEP_arrayToStringlist(*extraKeys);
dirk@605
   124
    }
dirk@605
   125
dz@830
   126
    PEPRating internalRating = PEPRatingUndefined;
dirk@514
   127
dz@830
   128
    PEPStatus theStatus = (PEPStatus) decrypt_message(_session,
dz@830
   129
                                                      _src,
dz@830
   130
                                                      &_dst,
dz@830
   131
                                                      &theKeys,
dz@830
   132
                                                      (PEP_rating *) &internalRating,
dz@987
   133
                                                      (PEP_decrypt_flags *) &theFlags);
andreas@269
   134
dirk@524
   135
    if (status) {
dirk@524
   136
        *status = theStatus;
dirk@524
   137
    }
dirk@524
   138
dirk@524
   139
    if ([NSError setError:error fromPEPStatus:theStatus]) {
dirk@605
   140
        decryptMessageDictFree(_src, _dst, theKeys);
dirk@517
   141
        return nil;
dirk@514
   142
    }
dirk@514
   143
dirk@583
   144
    if (flags) {
dirk@583
   145
        *flags = theFlags;
dirk@583
   146
    }
dirk@583
   147
dirk@467
   148
    NSDictionary *dst_;
andreas@269
   149
andreas@269
   150
    if (_dst) {
andreas@269
   151
        dst_ = PEP_messageDictFromStruct(_dst);
dirk@514
   152
    } else {
andreas@269
   153
        dst_ = PEP_messageDictFromStruct(_src);
andreas@269
   154
    }
andreas@269
   155
dirk@598
   156
    if (theFlags & PEP_decrypt_flag_untrusted_server) {
dirk@598
   157
        [messageDict replaceWithMessage:_src];
dirk@598
   158
    }
dirk@583
   159
dirk@605
   160
    if (extraKeys) {
dirk@605
   161
        *extraKeys = PEP_arrayFromStringlist(theKeys);
dirk@605
   162
    }
andreas@269
   163
dirk@605
   164
    decryptMessageDictFree(_src, _dst, theKeys);
dirk@514
   165
dirk@514
   166
    if (rating) {
dirk@514
   167
        *rating = internalRating;
dirk@514
   168
    }
dirk@514
   169
dirk@517
   170
    return dst_;
andreas@269
   171
}
andreas@269
   172
dirk@535
   173
- (PEPMessage * _Nullable)decryptMessage:(PEPMessage * _Nonnull)message
dz@829
   174
                                   flags:(PEPDecryptFlags * _Nullable)flags
dz@830
   175
                                  rating:(PEPRating * _Nullable)rating
dirk@523
   176
                               extraKeys:(PEPStringList * _Nullable * _Nullable)extraKeys
dz@829
   177
                                  status:(PEPStatus * _Nullable)status
dirk@517
   178
                                   error:(NSError * _Nullable * _Nullable)error
dirk@383
   179
{
dirk@583
   180
    PEPDict *destDict = [self
dirk@609
   181
                         decryptMessageDict:message.mutableDictionary
dirk@583
   182
                         flags:flags
dirk@583
   183
                         rating:rating
dirk@583
   184
                         extraKeys:extraKeys
dirk@583
   185
                         status:status
dirk@583
   186
                         error:error];
dirk@514
   187
dirk@614
   188
    if (destDict) {
dirk@383
   189
        PEPMessage *msg = [PEPMessage new];
dirk@383
   190
        [msg setValuesForKeysWithDictionary:destDict];
dirk@517
   191
        return msg;
dirk@614
   192
    } else {
dirk@614
   193
        return nil;
dirk@383
   194
    }
dirk@383
   195
}
dirk@383
   196
dirk@535
   197
- (BOOL)reEvaluateMessageDict:(PEPDict * _Nonnull)messageDict
dirk@634
   198
                     xKeyList:(PEPStringList * _Nullable)xKeyList
dz@830
   199
                       rating:(PEPRating * _Nonnull)rating
dz@829
   200
                       status:(PEPStatus * _Nullable)status
dirk@516
   201
                        error:(NSError * _Nullable * _Nullable)error
andreas@269
   202
{
dirk@516
   203
    message *_src = PEP_messageDictToStruct(messageDict);
andreas@269
   204
dirk@634
   205
    stringlist_t *theKeys = NULL;
dirk@634
   206
    if ([xKeyList count]) {
dirk@634
   207
        theKeys = PEP_arrayToStringlist(xKeyList);
dirk@634
   208
    }
dirk@634
   209
dz@834
   210
    PEPStatus theStatus = (PEPStatus) re_evaluate_message_rating(_session,
dz@834
   211
                                                                 _src,
dz@834
   212
                                                                 theKeys,
dz@834
   213
                                                                 (PEP_rating) *rating,
dz@834
   214
                                                                 (PEP_rating *) rating);
andreas@269
   215
andreas@269
   216
    free_message(_src);
dirk@634
   217
    free_stringlist(theKeys);
andreas@269
   218
dirk@524
   219
    if (status) {
dirk@524
   220
        *status = theStatus;
dirk@524
   221
    }
dirk@524
   222
dirk@524
   223
    if ([NSError setError:error fromPEPStatus:theStatus]) {
dirk@515
   224
        return NO;
dirk@515
   225
    } else {
dirk@515
   226
        return YES;
dirk@515
   227
    }
andreas@269
   228
}
andreas@269
   229
dirk@535
   230
- (BOOL)reEvaluateMessage:(PEPMessage * _Nonnull)message
dirk@634
   231
                 xKeyList:(PEPStringList * _Nullable)xKeyList
dz@830
   232
                   rating:(PEPRating * _Nonnull)rating
dz@829
   233
                   status:(PEPStatus * _Nullable)status
dirk@516
   234
                    error:(NSError * _Nullable * _Nullable)error
dirk@384
   235
{
dirk@524
   236
    return [self reEvaluateMessageDict:(PEPDict *) message
dirk@634
   237
                              xKeyList:xKeyList
dirk@524
   238
                                rating:rating
dirk@524
   239
                                status:status
dirk@524
   240
                                 error:error];
dirk@384
   241
}
dirk@384
   242
andreas@269
   243
- (void)removeEmptyArrayKey:(NSString *)key inDict:(PEPMutableDict *)dict
andreas@269
   244
{
andreas@269
   245
    if ([[dict objectForKey:key] count] == 0) {
andreas@269
   246
        [dict removeObjectForKey:key];
andreas@269
   247
    }
andreas@269
   248
}
andreas@269
   249
andreas@269
   250
- (NSDictionary *)removeEmptyRecipients:(PEPDict *)src
andreas@269
   251
{
andreas@269
   252
    NSMutableDictionary *dest = src.mutableCopy;
andreas@269
   253
andreas@269
   254
    [self removeEmptyArrayKey:kPepTo inDict:dest];
andreas@269
   255
    [self removeEmptyArrayKey:kPepCC inDict:dest];
andreas@269
   256
    [self removeEmptyArrayKey:kPepBCC inDict:dest];
andreas@269
   257
dirk@370
   258
    return dest;
andreas@269
   259
}
andreas@269
   260
dirk@535
   261
- (PEPDict * _Nullable)encryptMessageDict:(PEPDict * _Nonnull)messageDict
dirk@535
   262
                                extraKeys:(PEPStringList * _Nullable)extraKeys
dz@829
   263
                                encFormat:(PEPEncFormat)encFormat
dz@829
   264
                                   status:(PEPStatus * _Nullable)status
dirk@522
   265
                                    error:(NSError * _Nullable * _Nullable)error
andreas@269
   266
{
dirk@630
   267
    PEP_encrypt_flags_t flags = 0;
dirk@630
   268
dirk@522
   269
    message *_src = PEP_messageDictToStruct([self removeEmptyRecipients:messageDict]);
dirk@467
   270
    message *_dst = NULL;
dirk@522
   271
    stringlist_t *_keys = PEP_arrayToStringlist(extraKeys);
andreas@269
   272
dz@834
   273
    PEPStatus theStatus = (PEPStatus) encrypt_message(
dz@834
   274
                                                      _session,
dz@834
   275
                                                      _src,
dz@834
   276
                                                      _keys,
dz@834
   277
                                                      &_dst,
dz@834
   278
                                                      (PEP_enc_format) encFormat,
dz@834
   279
                                                      flags);
andreas@269
   280
dirk@524
   281
    if (status) {
dirk@524
   282
        *status = theStatus;
dirk@524
   283
    }
dirk@524
   284
dirk@524
   285
    if ([NSError setError:error fromPEPStatus:theStatus]) {
dirk@522
   286
        return nil;
dirk@522
   287
    }
dirk@522
   288
dirk@467
   289
    NSDictionary *dst_;
andreas@269
   290
andreas@269
   291
    if (_dst) {
andreas@269
   292
        dst_ = PEP_messageDictFromStruct(_dst);
andreas@269
   293
    }
andreas@269
   294
    else {
andreas@269
   295
        dst_ = PEP_messageDictFromStruct(_src);
andreas@269
   296
    }
andreas@269
   297
andreas@269
   298
    free_message(_src);
andreas@269
   299
    free_message(_dst);
andreas@269
   300
    free_stringlist(_keys);
andreas@269
   301
dirk@522
   302
    return dst_;
andreas@269
   303
}
andreas@269
   304
dirk@535
   305
- (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
andreas@1076
   306
                               extraKeys:(PEPStringList * _Nullable)extraKeys
dz@829
   307
                               encFormat:(PEPEncFormat)encFormat
dz@829
   308
                                  status:(PEPStatus * _Nullable)status
dirk@522
   309
                                   error:(NSError * _Nullable * _Nullable)error
dirk@386
   310
{
dirk@522
   311
    PEPDict *encryptedDict = [self encryptMessageDict:(NSDictionary *) message
dirk@524
   312
                                            extraKeys:extraKeys
dirk@522
   313
                                            encFormat:encFormat
dirk@524
   314
                                               status:status
dirk@522
   315
                                                error:error];
dirk@614
   316
    if (encryptedDict) {
dirk@614
   317
        PEPMessage *encrypted = [PEPMessage new];
dirk@614
   318
        [encrypted setValuesForKeysWithDictionary:encryptedDict];
dirk@614
   319
        return encrypted;
dirk@614
   320
    } else {
dirk@614
   321
        return nil;
dirk@614
   322
    }
dirk@379
   323
}
dirk@379
   324
dirk@535
   325
- (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
andreas@1076
   326
                               extraKeys:(PEPStringList * _Nullable)extraKeys
dz@829
   327
                                  status:(PEPStatus * _Nullable)status
dirk@522
   328
                                   error:(NSError * _Nullable * _Nullable)error
dirk@518
   329
{
dirk@524
   330
    return [self
dirk@524
   331
            encryptMessage:message
dirk@524
   332
            extraKeys:extraKeys
dz@864
   333
            encFormat:PEPEncFormatPEP
dirk@524
   334
            status:status
dirk@524
   335
            error:error];
dirk@518
   336
}
dirk@518
   337
dirk@535
   338
- (PEPDict * _Nullable)encryptMessageDict:(PEPDict * _Nonnull)messageDict
dirk@557
   339
                                  forSelf:(PEPIdentity * _Nonnull)ownIdentity
dirk@556
   340
                                extraKeys:(PEPStringList * _Nullable)extraKeys
dz@829
   341
                                   status:(PEPStatus * _Nullable)status
dirk@526
   342
                                    error:(NSError * _Nullable * _Nullable)error
andreas@269
   343
{
dirk@630
   344
    PEP_encrypt_flags_t flags = 0;
dirk@630
   345
dirk@526
   346
    message *_src = PEP_messageDictToStruct([self removeEmptyRecipients:messageDict]);
dirk@557
   347
    pEp_identity *ident = PEP_identityToStruct(ownIdentity);
dirk@467
   348
    message *_dst = NULL;
andreas@269
   349
dirk@556
   350
    stringlist_t *keysStringList = PEP_arrayToStringlist(extraKeys);
dirk@556
   351
dz@834
   352
    PEPStatus theStatus = (PEPStatus) encrypt_message_for_self(_session,
dz@834
   353
                                                               ident,
dz@834
   354
                                                               _src,
dz@834
   355
                                                               keysStringList,
dz@834
   356
                                                               &_dst,
dz@834
   357
                                                               PEP_enc_PGP_MIME,
dz@834
   358
                                                               flags);
andreas@269
   359
dirk@556
   360
    free_stringlist(keysStringList);
dirk@556
   361
dirk@526
   362
    if (status) {
dirk@526
   363
        *status = theStatus;
dirk@526
   364
    }
dirk@526
   365
dirk@526
   366
    if ([NSError setError:error fromPEPStatus:theStatus]) {
dirk@526
   367
        return nil;
dirk@526
   368
    }
dirk@526
   369
dirk@467
   370
    NSDictionary *dst_;
andreas@269
   371
andreas@269
   372
    if (_dst) {
andreas@269
   373
        dst_ = PEP_messageDictFromStruct(_dst);
andreas@269
   374
    }
andreas@269
   375
    else {
andreas@269
   376
        dst_ = PEP_messageDictFromStruct(_src);
andreas@269
   377
    }
andreas@269
   378
andreas@269
   379
    free_message(_src);
andreas@269
   380
    free_message(_dst);
andreas@269
   381
    free_identity(ident);
andreas@269
   382
dirk@526
   383
    return dst_;
andreas@269
   384
}
andreas@269
   385
dirk@535
   386
- (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
dirk@557
   387
                                 forSelf:(PEPIdentity * _Nonnull)ownIdentity
dirk@556
   388
                               extraKeys:(PEPStringList * _Nullable)extraKeys
dz@829
   389
                                  status:(PEPStatus * _Nullable)status
dirk@526
   390
                                   error:(NSError * _Nullable * _Nullable)error
dirk@381
   391
{
dirk@526
   392
    PEPDict *target = [self
dirk@526
   393
                       encryptMessageDict:message.dictionary
dirk@557
   394
                       forSelf:ownIdentity
dirk@556
   395
                       extraKeys:extraKeys
dirk@526
   396
                       status:status
dirk@526
   397
                       error:error];
dirk@526
   398
dirk@614
   399
    if (target) {
dirk@614
   400
        PEPMessage *encrypted = [PEPMessage new];
dirk@614
   401
        [encrypted setValuesForKeysWithDictionary:target];
dirk@614
   402
        return encrypted;
dirk@614
   403
    } else {
dirk@614
   404
        return nil;
dirk@614
   405
    }
dirk@381
   406
}
dirk@381
   407
dirk@608
   408
dirk@608
   409
- (PEPDict * _Nullable)encryptMessageDict:(PEPDict * _Nonnull)messageDict
dirk@608
   410
                                    toFpr:(NSString * _Nonnull)toFpr
dz@829
   411
                                encFormat:(PEPEncFormat)encFormat
dz@829
   412
                                    flags:(PEPDecryptFlags)flags
dz@829
   413
                                   status:(PEPStatus * _Nullable)status
dirk@608
   414
                                    error:(NSError * _Nullable * _Nullable)error __deprecated
dirk@608
   415
{
dirk@608
   416
    message *src = PEP_messageDictToStruct([self removeEmptyRecipients:messageDict]);
dirk@608
   417
    message *dst = NULL;
dirk@608
   418
dz@834
   419
    PEPStatus theStatus = (PEPStatus)
dirk@610
   420
    encrypt_message_and_add_priv_key(_session, src, &dst,
dirk@610
   421
                                     [[toFpr precomposedStringWithCanonicalMapping] UTF8String],
dz@834
   422
                                     (PEP_enc_format) encFormat, flags);
dirk@608
   423
dirk@608
   424
    if (status) {
dirk@608
   425
        *status = theStatus;
dirk@608
   426
    }
dirk@608
   427
dirk@608
   428
    if ([NSError setError:error fromPEPStatus:theStatus]) {
dirk@608
   429
        return nil;
dirk@608
   430
    }
dirk@608
   431
dirk@608
   432
    if (dst) {
dirk@608
   433
        return PEP_messageDictFromStruct(dst);
dirk@608
   434
    }
dirk@608
   435
dirk@608
   436
    return nil;
dirk@608
   437
}
dirk@608
   438
dirk@608
   439
- (PEPMessage * _Nullable)encryptMessage:(PEPMessage * _Nonnull)message
dirk@608
   440
                                   toFpr:(NSString * _Nonnull)toFpr
dz@829
   441
                               encFormat:(PEPEncFormat)encFormat
dz@829
   442
                                   flags:(PEPDecryptFlags)flags
dz@829
   443
                                  status:(PEPStatus * _Nullable)status
dirk@608
   444
                                   error:(NSError * _Nullable * _Nullable)error
dirk@608
   445
{
dirk@608
   446
    PEPDict *target = [self
dirk@608
   447
                       encryptMessageDict:message.dictionary
dirk@608
   448
                       toFpr:toFpr
dirk@608
   449
                       encFormat:encFormat
dirk@608
   450
                       flags:flags
dirk@608
   451
                       status:status
dirk@608
   452
                       error:error];
dirk@608
   453
dirk@614
   454
    if (target) {
dirk@614
   455
        PEPMessage *encrypted = [PEPMessage new];
dirk@614
   456
        [encrypted setValuesForKeysWithDictionary:target];
dirk@614
   457
        return encrypted;
dirk@614
   458
    } else {
dirk@614
   459
        return nil;
dirk@614
   460
    }
dirk@608
   461
}
dirk@608
   462
dz@834
   463
typedef PEP_STATUS (* rating_function_type)(PEP_SESSION session, message *msg, PEP_rating *rating);
dirk@651
   464
dirk@651
   465
- (NSNumber * _Nullable)helperOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
dirk@651
   466
                                        ratingFunction:(rating_function_type)ratingFunction
dirk@651
   467
                                                 error:(NSError * _Nullable * _Nullable)error
andreas@269
   468
{
dirk@650
   469
    message *_msg = PEP_messageToStruct(theMessage);
dz@830
   470
    PEPRating rating = PEPRatingUndefined;
andreas@269
   471
dz@834
   472
    PEPStatus status = (PEPStatus) outgoing_message_rating(_session,
dz@834
   473
                                                           _msg,
dz@834
   474
                                                           (PEP_rating *) &rating);
andreas@269
   475
andreas@269
   476
    free_message(_msg);
andreas@269
   477
dirk@528
   478
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@573
   479
        return nil;
dirk@528
   480
    }
dirk@528
   481
dirk@573
   482
    return [NSNumber numberWithPEPRating:rating];
andreas@269
   483
}
andreas@269
   484
dirk@651
   485
- (NSNumber * _Nullable)outgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
dirk@651
   486
                                           error:(NSError * _Nullable * _Nullable)error
dirk@651
   487
{
dirk@651
   488
    return [self
dirk@651
   489
            helperOutgoingRatingForMessage:theMessage
dirk@651
   490
            ratingFunction:&outgoing_message_rating
dirk@651
   491
            error:error];
dirk@651
   492
}
dirk@651
   493
dirk@652
   494
- (NSNumber * _Nullable)outgoingRatingPreviewForMessage:(PEPMessage * _Nonnull)theMessage
dirk@652
   495
                                                  error:(NSError * _Nullable * _Nullable)error
dirk@652
   496
{
dirk@652
   497
    return [self
dirk@652
   498
            helperOutgoingRatingForMessage:theMessage
dirk@652
   499
            ratingFunction:&outgoing_message_rating_preview
dirk@652
   500
            error:error];
dirk@652
   501
}
dirk@652
   502
dirk@574
   503
- (NSNumber * _Nullable)ratingForIdentity:(PEPIdentity * _Nonnull)identity
dirk@574
   504
                                    error:(NSError * _Nullable * _Nullable)error
andreas@269
   505
{
dirk@364
   506
    pEp_identity *ident = PEP_identityToStruct(identity);
dz@830
   507
    PEPRating rating = PEPRatingUndefined;
andreas@269
   508
dz@834
   509
    PEPStatus status = (PEPStatus) identity_rating(_session, ident, (PEP_rating *) &rating);
andreas@269
   510
andreas@269
   511
    free_identity(ident);
andreas@269
   512
dirk@529
   513
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@574
   514
        return nil;
dirk@529
   515
    }
dirk@529
   516
dirk@574
   517
    return [NSNumber numberWithPEPRating:rating];
andreas@269
   518
}
andreas@269
   519
dirk@531
   520
- (NSArray * _Nullable)trustwordsForFingerprint:(NSString * _Nonnull)fingerprint
dirk@531
   521
                                     languageID:(NSString * _Nonnull)languageID
dirk@531
   522
                                      shortened:(BOOL)shortened
dirk@531
   523
                                          error:(NSError * _Nullable * _Nullable)error
andreas@269
   524
{
andreas@269
   525
    NSMutableArray *array = [NSMutableArray array];
andreas@269
   526
dirk@531
   527
    for (int i = 0; i < [fingerprint length]; i += 4) {
andreas@269
   528
        if (shortened && i >= 20)
andreas@269
   529
            break;
andreas@269
   530
dirk@531
   531
        NSString *str = [fingerprint substringWithRange:NSMakeRange(i, 4)];
andreas@269
   532
andreas@269
   533
        unsigned int value;
andreas@269
   534
        [[NSScanner scannerWithString:str] scanHexInt:&value];
andreas@269
   535
dirk@564
   536
        PEPAutoPointer *word = [PEPAutoPointer new];
andreas@269
   537
        size_t size;
andreas@269
   538
dz@834
   539
        PEPStatus status = (PEPStatus) trustword(_session,
dz@834
   540
                                                 value,
dz@834
   541
                                                 [[languageID precomposedStringWithCanonicalMapping]
dz@834
   542
                                                  UTF8String],
dz@834
   543
                                                 word.charPointerPointer,
dz@834
   544
                                                 &size);
dirk@531
   545
dirk@531
   546
        if ([NSError setError:error fromPEPStatus:status]) {
dirk@531
   547
            return nil;
andreas@269
   548
        }
andreas@269
   549
dirk@563
   550
        [array addObject:[NSString stringWithUTF8String:word.charPointer]];
andreas@269
   551
    }
andreas@269
   552
andreas@269
   553
    return array;
andreas@269
   554
}
andreas@269
   555
dirk@535
   556
- (BOOL)mySelf:(PEPIdentity * _Nonnull)identity error:(NSError * _Nullable * _Nullable)error
andreas@269
   557
{
dirk@312
   558
    pEp_identity *ident = PEP_identityToStruct(identity);
andreas@269
   559
dz@834
   560
    PEPStatus status = (PEPStatus) myself(_session, ident);
andreas@269
   561
dirk@532
   562
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@532
   563
        free_identity(ident);
dirk@532
   564
        return NO;
dirk@532
   565
    }
dirk@532
   566
dirk@477
   567
    [identity reset];
andreas@269
   568
    [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
andreas@269
   569
    free_identity(ident);
dirk@312
   570
dirk@532
   571
    return YES;
andreas@269
   572
}
andreas@269
   573
dirk@535
   574
- (BOOL)updateIdentity:(PEPIdentity * _Nonnull)identity error:(NSError * _Nullable * _Nullable)error
andreas@269
   575
{
dirk@520
   576
    if (identity.isOwn) {
dirk@532
   577
        return [self mySelf:identity error:error];
dirk@520
   578
    } else {
dirk@520
   579
        pEp_identity *ident = PEP_identityToStruct(identity);
andreas@269
   580
dz@834
   581
        PEPStatus status = (PEPStatus) update_identity(_session, ident);
andreas@1073
   582
        
dirk@532
   583
        if ([NSError setError:error fromPEPStatus:status]) {
dirk@532
   584
            free_identity(ident);
dirk@532
   585
            return NO;
dirk@532
   586
        }
dirk@532
   587
dirk@520
   588
        [identity reset];
dirk@520
   589
        [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
dirk@520
   590
        free_identity(ident);
dirk@532
   591
dirk@532
   592
        return YES;
dirk@520
   593
    }
andreas@269
   594
}
andreas@269
   595
dirk@535
   596
- (BOOL)trustPersonalKey:(PEPIdentity * _Nonnull)identity
dirk@534
   597
                   error:(NSError * _Nullable * _Nullable)error
andreas@269
   598
{
dirk@360
   599
    pEp_identity *ident = PEP_identityToStruct(identity);
andreas@269
   600
dz@834
   601
    PEPStatus status = (PEPStatus) trust_personal_key(_session, ident);
andreas@269
   602
dirk@534
   603
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@534
   604
        free_identity(ident);
dirk@534
   605
        return NO;
dirk@534
   606
    }
dirk@534
   607
dirk@477
   608
    [identity reset];
andreas@269
   609
    [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
andreas@269
   610
    free_identity(ident);
dirk@534
   611
    return YES;
andreas@269
   612
}
andreas@269
   613
dirk@536
   614
- (BOOL)keyMistrusted:(PEPIdentity *)identity error:(NSError * _Nullable * _Nullable)error
dirk@536
   615
{
dirk@536
   616
    pEp_identity *ident = PEP_identityToStruct(identity);
dirk@536
   617
dz@834
   618
    PEPStatus status = (PEPStatus) key_mistrusted(_session, ident);
dirk@536
   619
dirk@536
   620
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@536
   621
        free_identity(ident);
dirk@536
   622
        return NO;
dirk@536
   623
    }
dirk@536
   624
dirk@536
   625
    [identity reset];
dirk@536
   626
    [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
dirk@536
   627
    free_identity(ident);
dirk@536
   628
dirk@536
   629
    return YES;
dirk@536
   630
}
dirk@536
   631
dirk@545
   632
- (BOOL)keyResetTrust:(PEPIdentity * _Nonnull)identity
dirk@545
   633
                error:(NSError * _Nullable * _Nullable)error
andreas@269
   634
{
dirk@361
   635
    pEp_identity *ident = PEP_identityToStruct(identity);
andreas@269
   636
dz@834
   637
    PEPStatus status = (PEPStatus) key_reset_trust(_session, ident);
andreas@269
   638
dirk@545
   639
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@545
   640
        free_identity(ident);
dirk@545
   641
        return NO;
dirk@545
   642
    }
dirk@545
   643
dirk@477
   644
    [identity reset];
andreas@269
   645
    [identity setValuesForKeysWithDictionary:PEP_identityDictFromStruct(ident)];
andreas@269
   646
    free_identity(ident);
dirk@545
   647
dirk@545
   648
    return YES;
andreas@269
   649
}
andreas@269
   650
dz@1181
   651
- (BOOL)enableSyncForIdentity:(PEPIdentity * _Nonnull)identity
dz@1181
   652
                        error:(NSError * _Nullable * _Nullable)error
dz@1181
   653
{
dz@1201
   654
    if (!identity.isOwn) {
dz@1201
   655
        [NSError setError:error fromPEPStatus:PEPStatusIllegalValue];
dz@1201
   656
        return NO;
dz@1201
   657
    }
dz@1201
   658
dz@1181
   659
    pEp_identity *ident = PEP_identityToStruct(identity);
dz@1181
   660
dz@1181
   661
    PEPStatus status = (PEPStatus) enable_identity_for_sync(_session, ident);
dz@1181
   662
dz@1181
   663
    if ([NSError setError:error fromPEPStatus:status]) {
dz@1181
   664
        free_identity(ident);
dz@1181
   665
        return NO;
dz@1181
   666
    }
dz@1181
   667
dz@1181
   668
    free_identity(ident);
dz@1181
   669
dz@1181
   670
    return YES;
dz@1181
   671
}
dz@1181
   672
dz@1186
   673
- (BOOL)disableSyncForIdentity:(PEPIdentity * _Nonnull)identity
dz@1186
   674
                         error:(NSError * _Nullable * _Nullable)error
dz@1186
   675
{
dz@1201
   676
    if (!identity.isOwn) {
dz@1201
   677
        [NSError setError:error fromPEPStatus:PEPStatusIllegalValue];
dz@1201
   678
        return NO;
dz@1201
   679
    }
dz@1201
   680
dz@1186
   681
    pEp_identity *ident = PEP_identityToStruct(identity);
dz@1186
   682
dz@1186
   683
    PEPStatus status = (PEPStatus) disable_identity_for_sync(_session, ident);
dz@1186
   684
dz@1186
   685
    if ([NSError setError:error fromPEPStatus:status]) {
dz@1186
   686
        free_identity(ident);
dz@1186
   687
        return NO;
dz@1186
   688
    }
dz@1186
   689
dz@1186
   690
    free_identity(ident);
dz@1186
   691
dz@1186
   692
    return YES;
dz@1186
   693
}
dz@1186
   694
dz@1190
   695
- (NSNumber * _Nullable)queryKeySyncEnabledForIdentity:(PEPIdentity * _Nonnull)identity
dz@1190
   696
                                                 error:(NSError * _Nullable * _Nullable)error
dz@1190
   697
{
dz@1190
   698
    pEp_identity *ident = PEP_identityToStruct(identity);
dz@1190
   699
dz@1201
   700
    if (!identity.isOwn) {
dz@1201
   701
        [NSError setError:error fromPEPStatus:PEPStatusIllegalValue];
dz@1201
   702
        return nil;
dz@1201
   703
    }
dz@1201
   704
dz@1190
   705
    PEPStatus status = (PEPStatus) myself(_session, ident);
dz@1190
   706
dz@1190
   707
    if ([NSError setError:error fromPEPStatus:status]) {
dz@1190
   708
        free_identity(ident);
dz@1200
   709
        return nil;
dz@1190
   710
    }
dz@1190
   711
dz@1190
   712
    identity_flags_t flags = ident->flags;
dz@1190
   713
dz@1190
   714
    free_identity(ident);
dz@1190
   715
dz@1190
   716
    if (flags & PEP_idf_not_for_sync) {
dz@1190
   717
        return [NSNumber numberWithBool:NO];
dz@1190
   718
    } else {
dz@1190
   719
        return [NSNumber numberWithBool:YES];
dz@1190
   720
    }
dz@1190
   721
}
dz@1190
   722
dirk@672
   723
- (NSArray<PEPIdentity *> * _Nullable)importKey:(NSString * _Nonnull)keydata
dirk@672
   724
                                          error:(NSError * _Nullable * _Nullable)error
andreas@269
   725
{
dirk@672
   726
    identity_list *identList = NULL;
dirk@672
   727
dz@834
   728
    PEPStatus status = (PEPStatus) import_key(_session,
andreas@1073
   729
                                              [[keydata precomposedStringWithCanonicalMapping] UTF8String],
andreas@1073
   730
                                              [keydata length], &identList);
andreas@269
   731
dirk@551
   732
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@672
   733
        free(identList);
dirk@665
   734
        return nil;
dirk@551
   735
    }
dirk@551
   736
dirk@672
   737
    NSArray *idents = PEP_arrayFromIdentityList(identList);
dirk@672
   738
    free(identList);
dirk@672
   739
dirk@672
   740
    return idents;
andreas@269
   741
}
andreas@269
   742
dirk@553
   743
- (BOOL)logTitle:(NSString * _Nonnull)title
dirk@553
   744
          entity:(NSString * _Nonnull)entity
dirk@553
   745
     description:(NSString * _Nullable)description
dirk@553
   746
         comment:(NSString * _Nullable)comment
dirk@553
   747
           error:(NSError * _Nullable * _Nullable)error
andreas@269
   748
{
dz@834
   749
    PEPStatus status = (PEPStatus) log_event(_session,
dz@834
   750
                                             [[title precomposedStringWithCanonicalMapping]
dz@834
   751
                                              UTF8String],
dz@834
   752
                                             [[entity precomposedStringWithCanonicalMapping]
dz@834
   753
                                              UTF8String],
dz@834
   754
                                             [[description precomposedStringWithCanonicalMapping]
dz@834
   755
                                              UTF8String],
dz@834
   756
                                             [[comment precomposedStringWithCanonicalMapping]
dz@834
   757
                                              UTF8String]);
dirk@553
   758
dirk@553
   759
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@553
   760
        return NO;
dirk@553
   761
    } else {
dirk@553
   762
        return YES;
dirk@553
   763
    }
andreas@269
   764
}
andreas@269
   765
dirk@552
   766
- (NSString * _Nullable)getLogWithError:(NSError * _Nullable * _Nullable)error
andreas@269
   767
{
dirk@475
   768
    char *theChars = NULL;
dz@834
   769
    PEPStatus status = (PEPStatus) get_crashdump_log(_session, 0, &theChars);
dirk@552
   770
dirk@552
   771
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@552
   772
        return nil;
andreas@269
   773
    }
dirk@475
   774
dirk@475
   775
    if (theChars) {
dirk@475
   776
        return [NSString stringWithUTF8String:theChars];
dirk@475
   777
    } else {
dz@834
   778
        [NSError setError:error fromPEPStatusInternal:PEP_UNKNOWN_ERROR];
dirk@475
   779
        return nil;
dirk@475
   780
    }
andreas@269
   781
}
andreas@269
   782
dirk@535
   783
- (NSString * _Nullable)getTrustwordsIdentity1:(PEPIdentity * _Nonnull)identity1
dirk@535
   784
                                     identity2:(PEPIdentity * _Nonnull)identity2
dirk@535
   785
                                      language:(NSString * _Nullable)language
dirk@535
   786
                                          full:(BOOL)full
dirk@558
   787
                                         error:(NSError * _Nullable * _Nullable)error
andreas@269
   788
{
dirk@365
   789
    pEp_identity *ident1 = PEP_identityToStruct(identity1);
dirk@365
   790
    pEp_identity *ident2 = PEP_identityToStruct(identity2);
dz@829
   791
    PEPStatus status;
andreas@269
   792
dirk@564
   793
    PEPAutoPointer *trustwords = [PEPAutoPointer new];
dirk@558
   794
    size_t sizeWritten = 0;
dirk@558
   795
dz@834
   796
    status = (PEPStatus) get_trustwords(_session, ident1, ident2,
dz@834
   797
                                        [[language precomposedStringWithCanonicalMapping]
dz@834
   798
                                         UTF8String],
dz@834
   799
                                        trustwords.charPointerPointer, &sizeWritten, full);
dirk@469
   800
dirk@769
   801
    free_identity(ident1);
dirk@769
   802
    free_identity(ident2);
dirk@769
   803
dirk@558
   804
    NSString *result = nil;
dirk@558
   805
dirk@558
   806
    if (![NSError setError:error fromPEPStatus:status]) {
dirk@616
   807
        result = [NSString stringWithUTF8String:trustwords.charPointer];
andreas@269
   808
    }
dirk@558
   809
andreas@269
   810
    return result;
andreas@269
   811
}
andreas@269
   812
vb@986
   813
- (NSString * _Nullable)getTrustwordsFpr1:(NSString * _Nonnull)fpr1
vb@986
   814
                                     fpr2:(NSString * _Nonnull)fpr2
andreas@1076
   815
                                 language:(NSString * _Nullable)language
andreas@1076
   816
                                     full:(BOOL)full
andreas@1076
   817
                                    error:(NSError * _Nullable * _Nullable)error
vb@986
   818
{
vb@986
   819
    const char *_fpr1 = [fpr1 UTF8String]; // fprs are NFC normalized anyway
vb@986
   820
    const char *_fpr2 = [fpr2 UTF8String];
vb@986
   821
    
vb@986
   822
    PEPStatus status;
vb@986
   823
    
vb@986
   824
    PEPAutoPointer *trustwords = [PEPAutoPointer new];
vb@986
   825
    size_t sizeWritten = 0;
andreas@1073
   826
vb@986
   827
    status = (PEPStatus) get_trustwords_for_fprs(_session, _fpr1, _fpr2,
andreas@1076
   828
                                                 [[language precomposedStringWithCanonicalMapping]
andreas@1076
   829
                                                  UTF8String],
andreas@1073
   830
                                                 trustwords.charPointerPointer, &sizeWritten, full);
vb@986
   831
    
vb@986
   832
    NSString *result = nil;
vb@986
   833
    
vb@986
   834
    if (![NSError setError:error fromPEPStatus:status]) {
vb@986
   835
        result = [NSString stringWithUTF8String:trustwords.charPointer];
vb@986
   836
    }
vb@986
   837
    
vb@986
   838
    return result;
vb@986
   839
}
vb@986
   840
dirk@560
   841
- (NSArray<PEPLanguage *> * _Nullable)languageListWithError:(NSError * _Nullable * _Nullable)error
andreas@269
   842
{
dirk@564
   843
    PEPAutoPointer *chLangs = [PEPAutoPointer new];
dz@834
   844
    PEPStatus status = (PEPStatus) get_languagelist(_session, chLangs.charPointerPointer);
dirk@560
   845
dirk@560
   846
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@560
   847
        return nil;
dirk@560
   848
    }
dirk@560
   849
dirk@563
   850
    NSString *parserInput = [NSString stringWithUTF8String:chLangs.charPointer];
andreas@269
   851
andreas@269
   852
    NSMutableArray<NSString *> *tokens = [NSMutableArray array];
andreas@269
   853
    PEPCSVScanner *scanner = [[PEPCSVScanner alloc] initWithString:parserInput];
andreas@269
   854
    while (YES) {
andreas@269
   855
        NSString *token = [scanner nextString];
andreas@269
   856
        if (!token) {
andreas@269
   857
            break;
andreas@269
   858
        }
andreas@269
   859
        [tokens addObject:token];
andreas@269
   860
    }
andreas@269
   861
    
andreas@269
   862
    NSArray *theTokens = [NSArray arrayWithArray:tokens];
andreas@269
   863
    NSMutableArray<PEPLanguage *> *langs = [NSMutableArray new];
andreas@269
   864
    while (YES) {
andreas@269
   865
        ArrayTake *take = [theTokens takeOrNil:3];
andreas@269
   866
        if (!take) {
andreas@269
   867
            break;
andreas@269
   868
        }
andreas@269
   869
        NSArray *elements = take.elements;
andreas@269
   870
        PEPLanguage *lang = [[PEPLanguage alloc]
andreas@269
   871
                             initWithCode:[elements objectAtIndex:0]
andreas@269
   872
                             name:[elements objectAtIndex:1]
andreas@269
   873
                             sentence:[elements objectAtIndex:2]];
andreas@269
   874
        [langs addObject:lang];
andreas@269
   875
        theTokens = take.rest;
andreas@269
   876
    }
andreas@269
   877
    
andreas@269
   878
    return [NSArray arrayWithArray:langs];
andreas@269
   879
}
andreas@269
   880
dirk@417
   881
static NSDictionary *ratingToString;
dirk@417
   882
static NSDictionary *stringToRating;
dirk@417
   883
dirk@417
   884
+ (void)initialize
dirk@417
   885
{
dirk@417
   886
    NSDictionary *ratingToStringIntern =
dirk@417
   887
    @{
dz@834
   888
      [NSNumber numberWithInteger:PEPRatingCannotDecrypt]: @"cannot_decrypt",
dz@834
   889
      [NSNumber numberWithInteger:PEPRatingHaveNoKey]: @"have_no_key",
dz@834
   890
      [NSNumber numberWithInteger:PEPRatingUnencrypted]: @"unencrypted",
dz@834
   891
      [NSNumber numberWithInteger:PEPRatingUnencryptedForSome]: @"unencrypted_for_some",
dz@834
   892
      [NSNumber numberWithInteger:PEPRatingUnreliable]: @"unreliable",
dz@834
   893
      [NSNumber numberWithInteger:PEPRatingReliable]: @"reliable",
dz@834
   894
      [NSNumber numberWithInteger:PEPRatingTrusted]: @"trusted",
dz@834
   895
      [NSNumber numberWithInteger:PEPRatingTrustedAndAnonymized]: @"trusted_and_anonymized",
dz@834
   896
      [NSNumber numberWithInteger:PEPRatingFullyAnonymous]: @"fully_anonymous",
dz@834
   897
      [NSNumber numberWithInteger:PEPRatingMistrust]: @"mistrust",
dz@834
   898
      [NSNumber numberWithInteger:PEPRatingB0rken]: @"b0rken",
dz@834
   899
      [NSNumber numberWithInteger:PEPRatingUnderAttack]: @"under_attack",
dz@1111
   900
      [NSNumber numberWithInteger:PEPRatingUndefined]: kUndefined,
dirk@417
   901
      };
dirk@417
   902
    NSMutableDictionary *stringToRatingMutable = [NSMutableDictionary
dirk@417
   903
                                                  dictionaryWithCapacity:
dirk@417
   904
                                                  ratingToStringIntern.count];
dirk@417
   905
    for (NSNumber *ratingNumber in ratingToStringIntern.allKeys) {
dirk@417
   906
        NSString *ratingName = [ratingToStringIntern objectForKey:ratingNumber];
dirk@417
   907
        [stringToRatingMutable setObject:ratingNumber forKey:ratingName];
dirk@417
   908
    }
dirk@417
   909
    ratingToString = ratingToStringIntern;
dirk@417
   910
    stringToRating = [NSDictionary dictionaryWithDictionary:stringToRatingMutable];
dirk@417
   911
}
dirk@417
   912
dz@830
   913
- (PEPRating)ratingFromString:(NSString * _Nonnull)string
dirk@417
   914
{
dirk@417
   915
    NSNumber *num = [stringToRating objectForKey:string];
dirk@417
   916
    if (num) {
dz@830
   917
        return (PEPRating) [num integerValue];
dirk@417
   918
    } else {
dz@830
   919
        return PEPRatingUndefined;
dirk@417
   920
    }
dirk@417
   921
}
dirk@417
   922
dz@830
   923
- (NSString * _Nonnull)stringFromRating:(PEPRating)rating
dirk@417
   924
{
dirk@417
   925
    NSString *stringRating = [ratingToString objectForKey:[NSNumber numberWithInteger:rating]];
dirk@417
   926
    if (stringRating) {
dirk@417
   927
        return stringRating;
dirk@417
   928
    } else {
dz@1111
   929
        return kUndefined;
dirk@417
   930
    }
dirk@417
   931
}
dirk@417
   932
dirk@567
   933
- (NSNumber * _Nullable)isPEPUser:(PEPIdentity * _Nonnull)identity
dirk@567
   934
                            error:(NSError * _Nullable * _Nullable)error
dirk@427
   935
{
dirk@428
   936
    pEp_identity *ident = PEP_identityToStruct(identity);
dirk@428
   937
    bool isPEP;
dz@834
   938
    PEPStatus status = (PEPStatus) is_pEp_user(self.session, ident, &isPEP);
dirk@567
   939
dirk@769
   940
    free_identity(ident);
dirk@769
   941
dirk@567
   942
    if ([NSError setError:error fromPEPStatus:status]) {
dirk@567
   943
        return nil;
dirk@428
   944
    } else {
dirk@567
   945
        return [NSNumber numberWithBool:isPEP];
dirk@428
   946
    }
dirk@427
   947
}
dirk@427
   948
dirk@501
   949
- (BOOL)setOwnKey:(PEPIdentity * _Nonnull)identity fingerprint:(NSString * _Nonnull)fingerprint
dirk@501
   950
            error:(NSError * _Nullable * _Nullable)error
dirk@501
   951
{
dirk@501
   952
    pEp_identity *ident = PEP_identityToStruct(identity);
dz@834
   953
    PEPStatus status = (PEPStatus) set_own_key(self.session, ident,
dz@834
   954
                                               [[fingerprint precomposedStringWithCanonicalMapping]
dz@834
   955
                                                UTF8String]);
dirk@769
   956
    free_identity(ident);
dirk@769
   957
dz@834
   958
    if (status == PEPStatusOK) {
dirk@501
   959
        return YES;
dirk@501
   960
    } else {
dirk@501
   961
        if (error) {
dirk@501
   962
            *error = [NSError errorWithPEPStatus:status];
dirk@501
   963
        }
dirk@501
   964
        return NO;
dirk@501
   965
    }
dirk@501
   966
}
dirk@501
   967
dirk@624
   968
- (void)configurePassiveModeEnabled:(BOOL)enabled
dirk@624
   969
{
dirk@624
   970
    config_passive_mode(_session, enabled);
dirk@624
   971
}
dirk@624
   972
dz@834
   973
- (BOOL)setFlags:(PEPIdentityFlags)flags
dirk@768
   974
     forIdentity:(PEPIdentity *)identity
dirk@768
   975
           error:(NSError * _Nullable * _Nullable)error
dirk@768
   976
{
dirk@768
   977
    pEp_identity *ident = PEP_identityToStruct(identity);
dz@834
   978
    PEPStatus status = (PEPStatus) set_identity_flags(self.session, ident, flags);
dirk@768
   979
    free_identity(ident);
dirk@768
   980
dz@834
   981
    if (status == PEPStatusOK) {
dirk@768
   982
        return YES;
dirk@768
   983
    } else {
dirk@768
   984
        if (error) {
dirk@768
   985
            *error = [NSError errorWithPEPStatus:status];
dirk@768
   986
        }
dirk@768
   987
        return NO;
dirk@768
   988
    }
dirk@768
   989
}
dirk@768
   990
dz@829
   991
- (BOOL)deliverHandshakeResult:(PEPSyncHandshakeResult)result
dz@913
   992
             identitiesSharing:(NSArray<PEPIdentity *> * _Nullable)identitiesSharing
dz@913
   993
                         error:(NSError * _Nullable * _Nullable)error;
dirk@773
   994
{
dz@913
   995
    identity_list *identitiesSharingData = NULL;
dz@913
   996
dz@913
   997
    if (identitiesSharing) {
dz@913
   998
        identitiesSharingData = PEP_identityArrayToList(identitiesSharing);
dz@913
   999
    }
dz@913
  1000
dz@834
  1001
    PEPStatus status = (PEPStatus) deliverHandshakeResult(self.session,
dz@913
  1002
                                                          (sync_handshake_result) result,
dz@913
  1003
                                                          identitiesSharingData);
dz@913
  1004
dz@913
  1005
    free(identitiesSharingData);
dirk@773
  1006
dz@834
  1007
    if (status == PEPStatusOK) {
dirk@773
  1008
        return YES;
dirk@773
  1009
    } else {
dirk@773
  1010
        if (error) {
dirk@773
  1011
            *error = [NSError errorWithPEPStatus:status];
dirk@773
  1012
        }
dirk@773
  1013
        return NO;
dirk@773
  1014
    }
dirk@773
  1015
}
dirk@773
  1016
dz@928
  1017
- (BOOL)trustOwnKeyIdentity:(PEPIdentity * _Nonnull)identity
dz@775
  1018
                      error:(NSError * _Nullable * _Nullable)error
dz@775
  1019
{
dz@775
  1020
    pEp_identity *ident = PEP_identityToStruct(identity);
dz@834
  1021
    PEPStatus status = (PEPStatus) trust_own_key(self.session, ident);
dz@775
  1022
    free_identity(ident);
dz@775
  1023
dz@834
  1024
    if (status == PEPStatusOK) {
dz@775
  1025
        return YES;
dz@775
  1026
    } else {
dz@775
  1027
        if (error) {
dz@775
  1028
            *error = [NSError errorWithPEPStatus:status];
dz@775
  1029
        }
dz@775
  1030
        return NO;
dz@775
  1031
    }
dz@775
  1032
}
dz@775
  1033
dz@856
  1034
- (PEPColor)colorFromRating:(PEPRating)rating
dz@856
  1035
{
dz@856
  1036
    return (PEPColor) color_from_rating((PEP_rating) rating);
dz@856
  1037
}
dz@856
  1038
dz@930
  1039
dz@930
  1040
- (BOOL)keyReset:(PEPIdentity * _Nonnull)identity
dz@930
  1041
     fingerprint:(NSString * _Nullable)fingerprint
dz@930
  1042
           error:(NSError * _Nullable * _Nullable)error
dz@930
  1043
{
dz@930
  1044
    pEp_identity *ident = PEP_identityToStruct(identity);
dz@930
  1045
    const char *fpr = [[fingerprint precomposedStringWithCanonicalMapping] UTF8String];
dz@930
  1046
dz@930
  1047
    PEPStatus status = (PEPStatus) key_reset_identity(self.session, ident, fpr);
dz@930
  1048
dz@930
  1049
    free_identity(ident);
dz@930
  1050
dz@930
  1051
    if (status == PEPStatusOK) {
dz@930
  1052
        return YES;
dz@930
  1053
    } else {
dz@930
  1054
        if (error) {
dz@930
  1055
            *error = [NSError errorWithPEPStatus:status];
dz@930
  1056
        }
dz@930
  1057
        return NO;
dz@930
  1058
    }
dz@930
  1059
}
dz@930
  1060
dz@1010
  1061
- (BOOL)leaveDeviceGroupError:(NSError * _Nullable * _Nullable)error
dz@1010
  1062
{
dz@1010
  1063
    PEPStatus status = (PEPStatus) leave_device_group(self.session);
dz@1010
  1064
dz@1010
  1065
    if (status == PEPStatusOK) {
dz@1010
  1066
        return YES;
dz@1010
  1067
    } else {
dz@1010
  1068
        if (error) {
dz@1010
  1069
            *error = [NSError errorWithPEPStatus:status];
dz@1010
  1070
        }
dz@1010
  1071
        return NO;
dz@1010
  1072
    }
dz@1010
  1073
}
dz@1010
  1074
andreas@269
  1075
@end