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