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