Browse Source

enhanced e2e

Marino Faggiana 7 years ago
parent
commit
827e3edb73

+ 5 - 5
iOSClient/Database/NCManageDatabase.swift

@@ -1121,10 +1121,10 @@ class NCManageDatabase: NSObject {
         }
     }
     
-    @objc func renameFileE2eEncryption(serverUrl: String, fileNameIdentifier: String, newFileName: String, newFileNamePath: String) -> Bool {
+    @objc func renameFileE2eEncryption(serverUrl: String, fileNameIdentifier: String, newFileName: String, newFileNamePath: String) {
         
         guard let tableAccount = self.getAccountActive() else {
-            return false
+            return
         }
         
         let realm = try! Realm()
@@ -1133,7 +1133,7 @@ class NCManageDatabase: NSObject {
 
         guard let result = realm.objects(tableE2eEncryption.self).filter("account = %@ AND serverUrl = %@ AND fileNameIdentifier = %@", tableAccount.account, serverUrl, fileNameIdentifier).first else {
             realm.cancelWrite()
-            return false
+            return 
         }
         
         let object = tableE2eEncryption.init(value: result)
@@ -1149,10 +1149,10 @@ class NCManageDatabase: NSObject {
             try realm.commitWrite()
         } catch let error {
             print("[LOG] Could not write to database: ", error)
-            return false
+            return
         }
         
-        return true
+        return
     }
     
     //MARK: -

+ 0 - 2
iOSClient/Main/CCMain.h

@@ -54,8 +54,6 @@
 
 @interface CCMain : UIViewController <UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate, UIGestureRecognizerDelegate, UIDocumentInteractionControllerDelegate, UIViewControllerPreviewingDelegate, CCMoveDelegate, CTAssetsPickerControllerDelegate, BKPasscodeViewControllerDelegate, UISplitViewControllerDelegate, UIPopoverControllerDelegate, CCNetworkingDelegate, CCShareOCDelegate, CCPeekPopDelegate, UIDocumentMenuDelegate, UIDocumentPickerDelegate, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, UIScrollViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
 
-@property BOOL isFolderEncrypted;
-
 @property (nonatomic, weak) IBOutlet UITableView *tableView;
 @property (nonatomic, strong) UIRefreshControl *refreshControl;
 @property (nonatomic, strong) NSString *serverUrl;

+ 56 - 45
iOSClient/Main/CCMain.m

@@ -2090,25 +2090,33 @@
             return;
         }
         
-        if ([[NCManageDatabase sharedInstance] renameFileE2eEncryptionWithServerUrl:self.serverUrl fileNameIdentifier:metadata.fileName newFileName:fileName newFileNamePath:[CCUtility returnFileNamePathFromFileName:fileName serverUrl:self.serverUrl activeUrl:appDelegate.activeUrl]]) {
+        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
             
-            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
+            NSString *token;
+            
+            // Rename on DB E2E (TEMP)
+            [[NCManageDatabase sharedInstance] renameFileE2eEncryptionWithServerUrl:self.serverUrl fileNameIdentifier:metadata.fileName newFileName:fileName newFileNamePath:[CCUtility returnFileNamePathFromFileName:fileName serverUrl:self.serverUrl activeUrl:appDelegate.activeUrl]];
+            
+            NSError *error = [[CCNetworking sharedNetworking] SendEndToEndMetadataOnServerUrl:self.serverUrl token:&token];
+            if (error != nil) {
+                dispatch_async(dispatch_get_main_queue(), ^{
+                    [appDelegate messageNotification:@"_error_" description:[NSString stringWithFormat:@"Error to send metadata %d", (int)error.code] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:0];
+                });
+            }
                 
-                NSError *error = [[CCNetworking sharedNetworking] SendEndToEndMetadataOnServerUrl:self.serverUrl];
-                if (error == nil) {
+            // Unlock
+            if (token) {
+                NSError *error = [[NCNetworkingSync sharedManager] unlockEndToEndFolderEncrypted:appDelegate.activeUser userID:appDelegate.activeUserID password:appDelegate.activePassword url:appDelegate.activeUrl fileID:_metadataFolder.fileID token:token];
+                if (error) {
                     dispatch_async(dispatch_get_main_queue(), ^{
-                        [[NCManageDatabase sharedInstance] setMetadataFileNameViewWithDirectoryID:metadata.directoryID fileName:metadata.fileName newFileNameView:fileName];
-                        [self reloadDatasource];
+                        [appDelegate messageNotification:@"_error_" description:[NSString stringWithFormat:@"%@ %d", error.localizedDescription, (int)error.code] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:0];
                     });
-                } else {
-                    // Restore previuos fileName on DB
-                    (void)[[NCManageDatabase sharedInstance] renameFileE2eEncryptionWithServerUrl:self.serverUrl fileNameIdentifier:metadata.fileName newFileName:metadata.fileNameView newFileNamePath:[CCUtility returnFileNamePathFromFileName:metadata.fileNameView serverUrl:self.serverUrl activeUrl:appDelegate.activeUrl]];
-                    [appDelegate messageNotification:@"_error_" description:[NSString stringWithFormat:@"Error to send metadata %d", (int)error.code] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:0];
                 }
+            }
+            dispatch_async(dispatch_get_main_queue(), ^{
+                [self readFolder:self.serverUrl];
             });
-        } else {
-            [appDelegate messageNotification:@"_error_" description:@"Error file not found" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:0];
-        }
+        });
         
     } else  {
         
@@ -3878,10 +3886,15 @@
     
     if (metadata == nil || metadata.sessionTaskIdentifier != k_taskIdentifierDone)
         return NO;
-    else
-        return YES;
+    
+    // E2E
+    if ([CCUtility isFolderEncrypted:self.serverUrl account:appDelegate.activeAccount] && [CCUtility isEndToEndEnabled:appDelegate.activeAccount] == NO)
+        return NO;
+    
+    return YES;
 }
 
+
 -(void)swipeTableCell:(nonnull MGSwipeTableCell *)cell didChangeSwipeState:(MGSwipeState)state gestureIsActive:(BOOL)gestureIsActive
 {
 }
@@ -4123,7 +4136,8 @@
                                     }];
         }
         
-        if ([CCUtility isEndToEndEnabled:appDelegate.activeAccount] && !_metadata.e2eEncrypted && !([_metadata.fileName isEqualToString:_autoUploadFileName] == YES && [serverUrl isEqualToString:_autoUploadDirectory] == YES)) {
+        if (!_metadata.e2eEncrypted && !([_metadata.fileName isEqualToString:_autoUploadFileName] == YES && [serverUrl isEqualToString:_autoUploadDirectory] == YES)) {
+
             
             [actionSheet addButtonWithTitle:NSLocalizedString(@"_e2e_set_folder_encrypted_", nil)
                                       image:[UIImage imageNamed:@"encrypted_empty"]
@@ -4144,7 +4158,7 @@
                                     }];
         }
         
-        if ([CCUtility isEndToEndEnabled:appDelegate.activeAccount] && _metadata.e2eEncrypted) {
+        if (_metadata.e2eEncrypted) {
             
             [actionSheet addButtonWithTitle:NSLocalizedString(@"_e2e_remove_folder_encrypted_", nil)
                                       image:[UIImage imageNamed:@"encrypted_empty"]
@@ -4165,7 +4179,7 @@
                                     }];
         }
         
-        if ([CCUtility isEndToEndEnabled:appDelegate.activeAccount] && directory.e2eTokenLock.length > 0) {
+        if (directory.e2eTokenLock.length > 0) {
             
             [actionSheet addButtonWithTitle:NSLocalizedString(@"_e2e_remove_folder_lock_", nil)
                                       image:[UIImage imageNamed:@"encrypted_empty"]
@@ -4233,40 +4247,37 @@
                                     [self performSelector:@selector(openIn:) withObject:_metadata];
                                 }];
         
-        if (isFolderEncrypted == NO || (isFolderEncrypted && [CCUtility isEndToEndEnabled:appDelegate.activeAccount])) {
-            
-            [actionSheet addButtonWithTitle:NSLocalizedString(@"_rename_", nil)
-                                      image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetRename"] color:[NCBrandColor sharedInstance].brandElement]
-                            backgroundColor:[NCBrandColor sharedInstance].backgroundView
-                                     height: 50.0
-                                       type:AHKActionSheetButtonTypeDefault
-                                    handler:^(AHKActionSheet *as) {
+        
+        [actionSheet addButtonWithTitle:NSLocalizedString(@"_rename_", nil)
+                                  image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"actionSheetRename"] color:[NCBrandColor sharedInstance].brandElement]
+                        backgroundColor:[NCBrandColor sharedInstance].backgroundView
+                                 height: 50.0
+                                   type:AHKActionSheetButtonTypeDefault
+                                handler:^(AHKActionSheet *as) {
                                     
-                                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_rename_",nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
+                                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_rename_",nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
                                     
-                                        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
-                                            textField.text = _metadata.fileNameView;
-                                            [textField addTarget:self action:@selector(minCharTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
-                                        }];
+                                    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
+                                        textField.text = _metadata.fileNameView;
+                                        [textField addTarget:self action:@selector(minCharTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
+                                    }];
                                     
-                                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
-                                            NSLog(@"[LOG] Cancel action");
-                                        }];
+                                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
+                                        NSLog(@"[LOG] Cancel action");
+                                    }];
                                     
-                                            UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
-                                            UITextField *fileName = alertController.textFields.firstObject;
-                                        
-                                            [self performSelectorOnMainThread:@selector(renameFile:) withObject:[NSMutableArray arrayWithObjects:_metadata,fileName.text, nil] waitUntilDone:NO];
-                                        }];
+                                    UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
+                                        UITextField *fileName = alertController.textFields.firstObject;
+                                        [self performSelectorOnMainThread:@selector(renameFile:) withObject:[NSMutableArray arrayWithObjects:_metadata,fileName.text, nil] waitUntilDone:NO];
+                                    }];
                                     
-                                        okAction.enabled = NO;
+                                    okAction.enabled = NO;
                                     
-                                        [alertController addAction:cancelAction];
-                                        [alertController addAction:okAction];
+                                    [alertController addAction:cancelAction];
+                                    [alertController addAction:okAction];
                                     
-                                        [self presentViewController:alertController animated:YES completion:nil];
-                                    }];
-        }
+                                    [self presentViewController:alertController animated:YES completion:nil];
+                                }];
         
         if (!isFolderEncrypted) {
 

+ 1 - 1
iOSClient/Networking/CCNetworking.h

@@ -71,7 +71,7 @@
 - (void)verifyUploadInErrorOrWait;
 
 // E2E Encryption
-- (NSError *)SendEndToEndMetadataOnServerUrl:(NSString *)serverUrl;
+- (NSError *)SendEndToEndMetadataOnServerUrl:(NSString *)serverUrl token:(NSString **)token;
 - (NSError *)rebuildAndSendEndToEndMetadataOnServerUrl:(NSString *)serverUrl;
 
 @end

+ 7 - 5
iOSClient/Networking/CCNetworking.m

@@ -836,7 +836,7 @@
     metadata.sessionSelectorPost = selectorPost;
     
     // E2E *** IS ENCRYPTED ---> ENCRYPTED FILE ***
-    if ([CCUtility isFolderEncrypted:serverUrl account:_activeAccount]) {
+    if ([CCUtility isFolderEncrypted:serverUrl account:_activeAccount] && [CCUtility isEndToEndEnabled:_activeAccount]) {
         
         NSString *fileNameIdentifier;
         NSError *error;
@@ -974,12 +974,13 @@
     } else {
         
         // E2E *** IS ENCRYPTED ---> CREATE SEND METADATA ***
-        if ([CCUtility isFolderEncrypted:serverUrl account:_activeAccount]) {
+        if ([CCUtility isFolderEncrypted:serverUrl account:_activeAccount] && [CCUtility isEndToEndEnabled:_activeAccount]) {
             
             dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
                 
                 // Send Metadata
-                NSError *error = [self SendEndToEndMetadataOnServerUrl:serverUrl];
+                NSString *token;
+                NSError *error = [self SendEndToEndMetadataOnServerUrl:serverUrl token:&token];
                 if (error == nil) {
                         
                     [[NCManageDatabase sharedInstance] setMetadataSession:session sessionError:@"" sessionSelector:nil sessionSelectorPost:nil sessionTaskIdentifier:uploadTask.taskIdentifier predicate:[NSPredicate predicateWithFormat:@"sessionID = %@ AND account = %@", sessionID, _activeAccount]];
@@ -1178,7 +1179,7 @@
     }
     
     // E2E *** IS ENCRYPTED ---> UNLOCK ***
-    if ([CCUtility isFolderEncrypted:serverUrl account:_activeAccount]) {
+    if ([CCUtility isFolderEncrypted:serverUrl account:_activeAccount] && [CCUtility isEndToEndEnabled:_activeAccount]) {
         
         // OK remove record on tableQueueUpload [NEXT UPLOAD]
         [[NCManageDatabase sharedInstance] deleteQueueUploadWithAssetLocalIdentifier:metadata.assetLocalIdentifier selector:metadata.sessionSelector];
@@ -1522,7 +1523,7 @@
     return result;
 }
 
-- (NSError *)SendEndToEndMetadataOnServerUrl:(NSString *)serverUrl
+- (NSError *)SendEndToEndMetadataOnServerUrl:(NSString *)serverUrl token:(NSString **)token
 {
     tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", _activeAccount, serverUrl]];
     
@@ -1564,6 +1565,7 @@
         e2eError = [[NCNetworkingSync sharedManager] storeEndToEndMetadata:_activeUser userID:_activeUserID password:_activePassword url:_activeUrl fileID:directory.fileID metadata:e2eMetadataJSON token:&e2eTokenLock];
     }
     
+    *token = e2eTokenLock;
     return e2eError;
 }
 

+ 1 - 0
iOSClient/Security/NCEndToEndMetadata.swift

@@ -209,6 +209,7 @@ class NCEndToEndMetadata : NSObject  {
                     
                         // If exists remove records
                         NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account = %@ AND fileNamePath = %@", object.account, object.fileNamePath))
+                        NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account = %@ AND fileNameIdentifier = %@", object.account, object.fileNameIdentifier))
                         
                         // Write file parameter for decrypted on DB
                         if NCManageDatabase.sharedInstance.addE2eEncryption(object) == false {