merge IOSAD-148
authorDirk Zimmermann <dz@pep.security>
Mon, 30 Sep 2019 10:16:22 +0200
changeset 1293dd19c6e2fcf8
parent 1280 d8593d9624e5
parent 1292 7ed48cdffca1
child 1294 e4560d5853cd
child 1299 1a8cd3285b52
merge IOSAD-148
     1.1 --- a/pEpObjCAdapter/NSError+PEP+Internal.h	Fri Sep 27 14:43:19 2019 +0200
     1.2 +++ b/pEpObjCAdapter/NSError+PEP+Internal.h	Mon Sep 30 10:16:22 2019 +0200
     1.3 @@ -16,15 +16,9 @@
     1.4   */
     1.5  @interface NSError (Internal)
     1.6  
     1.7 -+ (NSError * _Nonnull)errorWithPEPStatus:(PEPStatus)status
     1.8 -                                userInfo:(NSDictionary<NSErrorUserInfoKey, id> * _Nonnull)dict;
     1.9 ++ (NSError * _Nullable)errorWithPEPStatus:(PEPStatus)status;
    1.10  
    1.11 -+ (NSError * _Nonnull)errorWithPEPStatus:(PEPStatus)status;
    1.12 -
    1.13 -+ (NSError * _Nonnull)errorWithPEPStatusInternal:(PEP_STATUS)status
    1.14 -                                        userInfo:(NSDictionary<NSErrorUserInfoKey, id> * _Nonnull)dict;
    1.15 -
    1.16 -+ (NSError * _Nonnull)errorWithPEPStatusInternal:(PEP_STATUS)status;
    1.17 ++ (NSError * _Nullable)errorWithPEPStatusInternal:(PEP_STATUS)status;
    1.18  
    1.19  /**
    1.20   If the given status indicates an error, tries to set the given error accordingly.
     2.1 --- a/pEpObjCAdapter/NSError+PEP.m	Fri Sep 27 14:43:19 2019 +0200
     2.2 +++ b/pEpObjCAdapter/NSError+PEP.m	Mon Sep 30 10:16:22 2019 +0200
     2.3 @@ -17,8 +17,7 @@
     2.4  
     2.5  @implementation NSError (Extension)
     2.6  
     2.7 -+ (NSError * _Nonnull)errorWithPEPStatusInternal:(PEP_STATUS)status
     2.8 -                                        userInfo:(NSDictionary<NSErrorUserInfoKey, id> * _Nonnull)dict
     2.9 ++ (NSError * _Nullable)errorWithPEPStatusInternal:(PEP_STATUS)status
    2.10  {
    2.11      switch (status) {
    2.12          case PEP_STATUS_OK:
    2.13 @@ -30,36 +29,35 @@
    2.14              return nil;
    2.15              break;
    2.16  
    2.17 -        default:
    2.18 -            if (![dict objectForKey:NSLocalizedDescriptionKey]) {
    2.19 -                NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:dict];
    2.20 -                [dict2 setValue:localizedErrorStringFromPEPStatus(status)
    2.21 -                         forKey:NSLocalizedDescriptionKey];
    2.22 -                dict = dict2;
    2.23 -            }
    2.24 +        default: {
    2.25 +            NSDictionary *dict = [NSDictionary
    2.26 +                                  dictionaryWithObjectsAndKeys:localizedErrorStringFromPEPStatus(status),
    2.27 +                                  NSLocalizedDescriptionKey, nil];
    2.28              return [NSError errorWithDomain:s_pEpAdapterDomain code:status userInfo:dict];
    2.29 +        }
    2.30              break;
    2.31      }
    2.32  }
    2.33  
    2.34 -+ (NSError * _Nonnull)errorWithPEPStatusInternal:(PEP_STATUS)status
    2.35 ++ (NSError * _Nullable)errorWithPEPStatus:(PEPStatus)status
    2.36  {
    2.37 -    NSDictionary *userInfo = [NSDictionary new];
    2.38 -    return [self errorWithPEPStatusInternal:status userInfo:userInfo];
    2.39 +    return [self errorWithPEPStatusInternal:(PEP_STATUS) status];
    2.40  }
    2.41  
    2.42  + (BOOL)setError:(NSError * _Nullable * _Nullable)error fromPEPStatus:(PEP_STATUS)status
    2.43  {
    2.44 -    NSError *theError = [self errorWithPEPStatusInternal:status];
    2.45 -    if (theError) {
    2.46 -        if (error) {
    2.47 -            *error = theError;
    2.48 -        }
    2.49 +    // Determine if the given status is an error.
    2.50 +    NSError *errorFromStatus = [self errorWithPEPStatusInternal:status];
    2.51 +
    2.52 +    // Set caller's error, if given
    2.53 +    if (error) {
    2.54 +        *error = errorFromStatus;
    2.55 +    }
    2.56 +
    2.57 +    // Indicate error status.
    2.58 +    if (errorFromStatus) {
    2.59          return YES;
    2.60      } else {
    2.61 -        if (error) {
    2.62 -            *error = nil;
    2.63 -        }
    2.64          return NO;
    2.65      }
    2.66  }
    2.67 @@ -85,15 +83,4 @@
    2.68      }
    2.69  }
    2.70  
    2.71 -+ (NSError * _Nonnull)errorWithPEPStatus:(PEPStatus)status
    2.72 -                                userInfo:(NSDictionary<NSErrorUserInfoKey, id> * _Nonnull)dict
    2.73 -{
    2.74 -    return [self errorWithPEPStatusInternal:(PEP_STATUS) status userInfo:dict];
    2.75 -}
    2.76 -
    2.77 -+ (NSError * _Nonnull)errorWithPEPStatus:(PEPStatus)status
    2.78 -{
    2.79 -    return [self errorWithPEPStatusInternal:(PEP_STATUS) status userInfo:[NSDictionary new]];
    2.80 -}
    2.81 -
    2.82  @end
     3.1 --- a/pEpObjCAdapter/PEPInternalSession.m	Fri Sep 27 14:43:19 2019 +0200
     3.2 +++ b/pEpObjCAdapter/PEPInternalSession.m	Mon Sep 30 10:16:22 2019 +0200
     3.3 @@ -953,13 +953,10 @@
     3.4                                                  UTF8String]);
     3.5      free_identity(ident);
     3.6  
     3.7 -    if (status == PEPStatusOK) {
     3.8 +    if ([NSError setError:error fromPEPStatus:status]) {
     3.9 +        return NO;
    3.10 +    } else {
    3.11          return YES;
    3.12 -    } else {
    3.13 -        if (error) {
    3.14 -            *error = [NSError errorWithPEPStatus:status];
    3.15 -        }
    3.16 -        return NO;
    3.17      }
    3.18  }
    3.19  
    3.20 @@ -976,13 +973,10 @@
    3.21      PEPStatus status = (PEPStatus) set_identity_flags(self.session, ident, flags);
    3.22      free_identity(ident);
    3.23  
    3.24 -    if (status == PEPStatusOK) {
    3.25 +    if ([NSError setError:error fromPEPStatus:status]) {
    3.26 +        return NO;
    3.27 +    } else {
    3.28          return YES;
    3.29 -    } else {
    3.30 -        if (error) {
    3.31 -            *error = [NSError errorWithPEPStatus:status];
    3.32 -        }
    3.33 -        return NO;
    3.34      }
    3.35  }
    3.36  
    3.37 @@ -1002,13 +996,10 @@
    3.38  
    3.39      free(identitiesSharingData);
    3.40  
    3.41 -    if (status == PEPStatusOK) {
    3.42 +    if ([NSError setError:error fromPEPStatus:status]) {
    3.43 +        return NO;
    3.44 +    } else {
    3.45          return YES;
    3.46 -    } else {
    3.47 -        if (error) {
    3.48 -            *error = [NSError errorWithPEPStatus:status];
    3.49 -        }
    3.50 -        return NO;
    3.51      }
    3.52  }
    3.53  
    3.54 @@ -1019,13 +1010,10 @@
    3.55      PEPStatus status = (PEPStatus) trust_own_key(self.session, ident);
    3.56      free_identity(ident);
    3.57  
    3.58 -    if (status == PEPStatusOK) {
    3.59 +    if ([NSError setError:error fromPEPStatus:status]) {
    3.60 +        return NO;
    3.61 +    } else {
    3.62          return YES;
    3.63 -    } else {
    3.64 -        if (error) {
    3.65 -            *error = [NSError errorWithPEPStatus:status];
    3.66 -        }
    3.67 -        return NO;
    3.68      }
    3.69  }
    3.70  
    3.71 @@ -1046,13 +1034,10 @@
    3.72  
    3.73      free_identity(ident);
    3.74  
    3.75 -    if (status == PEPStatusOK) {
    3.76 +    if ([NSError setError:error fromPEPStatus:status]) {
    3.77 +        return NO;
    3.78 +    } else {
    3.79          return YES;
    3.80 -    } else {
    3.81 -        if (error) {
    3.82 -            *error = [NSError errorWithPEPStatus:status];
    3.83 -        }
    3.84 -        return NO;
    3.85      }
    3.86  }
    3.87  
    3.88 @@ -1060,13 +1045,21 @@
    3.89  {
    3.90      PEPStatus status = (PEPStatus) leave_device_group(self.session);
    3.91  
    3.92 -    if (status == PEPStatusOK) {
    3.93 +    if ([NSError setError:error fromPEPStatus:status]) {
    3.94 +        return NO;
    3.95 +    } else {
    3.96          return YES;
    3.97 +    }
    3.98 +}
    3.99 +
   3.100 +- (BOOL)keyResetAllOwnKeysError:(NSError * _Nullable * _Nullable)error
   3.101 +{
   3.102 +    PEPStatus status = (PEPStatus) key_reset_all_own_keys(self.session);
   3.103 +
   3.104 +    if ([NSError setError:error fromPEPStatus:status]) {
   3.105 +        return NO;
   3.106      } else {
   3.107 -        if (error) {
   3.108 -            *error = [NSError errorWithPEPStatus:status];
   3.109 -        }
   3.110 -        return NO;
   3.111 +        return YES;
   3.112      }
   3.113  }
   3.114  
     4.1 --- a/pEpObjCAdapter/PEPSession.m	Fri Sep 27 14:43:19 2019 +0200
     4.2 +++ b/pEpObjCAdapter/PEPSession.m	Mon Sep 30 10:16:22 2019 +0200
     4.3 @@ -475,4 +475,11 @@
     4.4      return [session leaveDeviceGroupError:error];
     4.5  }
     4.6  
     4.7 +- (BOOL)keyResetAllOwnKeysError:(NSError * _Nullable * _Nullable)error
     4.8 +{
     4.9 +    PEPInternalSession *session = [PEPSessionProvider session];
    4.10 +    RETURN_ON_ERROR(session, error, NO);
    4.11 +    return [session keyResetAllOwnKeysError:error];
    4.12 +}
    4.13 +
    4.14  @end
     5.1 --- a/pEpObjCAdapterFramework/PEPSessionProtocol.h	Fri Sep 27 14:43:19 2019 +0200
     5.2 +++ b/pEpObjCAdapterFramework/PEPSessionProtocol.h	Mon Sep 30 10:16:22 2019 +0200
     5.3 @@ -327,4 +327,12 @@
     5.4  /** Wraps leave_device_group. */
     5.5  - (BOOL)leaveDeviceGroupError:(NSError * _Nullable * _Nullable)error;
     5.6  
     5.7 +/**
     5.8 + Revoke and mistrust all own keys. See key_reset_all_own_keys for details.
     5.9 +
    5.10 + @param error The default cocoa error handling.
    5.11 + @return YES on success, NO if there were errors.
    5.12 + */
    5.13 +- (BOOL)keyResetAllOwnKeysError:(NSError * _Nullable * _Nullable)error;
    5.14 +
    5.15  @end
     6.1 --- a/pEpObjCTests/PEPSessionTest.m	Fri Sep 27 14:43:19 2019 +0200
     6.2 +++ b/pEpObjCTests/PEPSessionTest.m	Mon Sep 30 10:16:22 2019 +0200
     6.3 @@ -1426,6 +1426,82 @@
     6.4      }
     6.5  }
     6.6  
     6.7 +/**
     6.8 + Tests [PEPSessionProtocol keyResetAllOwnKeysError:error].
     6.9 +
    6.10 + Does the following:
    6.11 + * Do a mySelf.
    6.12 + * Catch the sent out sync (beacon?) message.
    6.13 + * Do a key reset on all own identities ([PEPSessionProtocol keyResetAllOwnKeysError:error]).
    6.14 + * Catch the sent out sync message.
    6.15 + * Decrypt the caught sync messages.
    6.16 + */
    6.17 +- (void)testDecryptBeaconsAfterKeyReset
    6.18 +{
    6.19 +    PEPSession *session = [PEPSession new];
    6.20 +
    6.21 +    XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
    6.22 +    XCTAssertNil(self.sendMessageDelegate.lastMessage);
    6.23 +
    6.24 +    PEPIdentity *identMe = [[PEPIdentity alloc]
    6.25 +                            initWithAddress:@"me-myself-and-i@pep-project.org"
    6.26 +                            userID:@"me-myself-and-i"
    6.27 +                            userName:@"pEp Me"
    6.28 +                            isOwn:YES];
    6.29 +    NSError *error = nil;
    6.30 +    XCTAssertTrue([session mySelf:identMe error:&error]);
    6.31 +    XCTAssertNil(error);
    6.32 +    XCTAssertNotNil(identMe.fingerPrint);
    6.33 +
    6.34 +    [self startSync];
    6.35 +
    6.36 +    XCTKVOExpectation *expHaveMessage1 = [[XCTKVOExpectation alloc]
    6.37 +                                          initWithKeyPath:@"lastMessage"
    6.38 +                                          object:self.sendMessageDelegate];
    6.39 +    [self waitForExpectations:@[expHaveMessage1] timeout:PEPTestInternalSyncTimeout];
    6.40 +    XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
    6.41 +    XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
    6.42 +
    6.43 +    PEPMessage *oldBeacon = self.sendMessageDelegate.lastMessage;
    6.44 +
    6.45 +    XCTAssertTrue([session keyResetAllOwnKeysError:&error]);
    6.46 +    XCTAssertNil(error);
    6.47 +
    6.48 +    XCTKVOExpectation *expHaveMessage2 = [[XCTKVOExpectation alloc]
    6.49 +                                          initWithKeyPath:@"lastMessage"
    6.50 +                                          object:self.sendMessageDelegate];
    6.51 +    [self waitForExpectations:@[expHaveMessage2] timeout:PEPTestInternalSyncTimeout];
    6.52 +    XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
    6.53 +    XCTAssertEqual(self.sendMessageDelegate.messages.count, 2);
    6.54 +
    6.55 +    PEPMessage *newBeacon = self.sendMessageDelegate.lastMessage;
    6.56 +
    6.57 +    XCTAssertNotEqual(oldBeacon, newBeacon);
    6.58 +
    6.59 +    PEPRating rating;
    6.60 +    PEPStringList *extraKeys;
    6.61 +    PEPStatus status;
    6.62 +    PEPMessage *decryptedOldBeacon = [session decryptMessage:oldBeacon
    6.63 +                                                       flags:nil
    6.64 +                                                      rating:&rating
    6.65 +                                                   extraKeys:&extraKeys
    6.66 +                                                      status:&status
    6.67 +                                                       error:&error];
    6.68 +    XCTAssertNotNil(decryptedOldBeacon);
    6.69 +    XCTAssertNil(error);
    6.70 +
    6.71 +    PEPMessage *decryptedNewBeacon = [session decryptMessage:newBeacon
    6.72 +                                                       flags:nil
    6.73 +                                                      rating:&rating
    6.74 +                                                   extraKeys:&extraKeys
    6.75 +                                                      status:&status
    6.76 +                                                       error:&error];
    6.77 +    XCTAssertNotNil(decryptedNewBeacon);
    6.78 +    XCTAssertNil(error);
    6.79 +
    6.80 +    [self shutdownSync];
    6.81 +}
    6.82 +
    6.83  #pragma mark - Helpers
    6.84  
    6.85  - (void)testSendMessageOnSession:(PEPSession *)session