marinofaggiana 4 rokov pred
rodič
commit
c1798ed64a

+ 6 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -171,6 +171,7 @@
 		F74AF3A7247FB6AE00AC767B /* NCUtilityFileSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = F74AF3A3247FB6AE00AC767B /* NCUtilityFileSystem.swift */; };
 		F74AFCE922E8B025003DE61F /* FSCalendar.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F74AFCE822E8B024003DE61F /* FSCalendar.framework */; };
 		F74C4FBB2328C3C200A23E25 /* OpenSSL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F74C4FBA2328C3C100A23E25 /* OpenSSL.framework */; };
+		F74EE6522481162C0073B8E0 /* NCNetworkingEndToEnd.m in Sources */ = {isa = PBXBuildFile; fileRef = F74EE6502481162C0073B8E0 /* NCNetworkingEndToEnd.m */; };
 		F7501C322212E57500FB1415 /* NCMedia.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7501C302212E57400FB1415 /* NCMedia.storyboard */; };
 		F7501C332212E57500FB1415 /* NCMedia.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7501C312212E57400FB1415 /* NCMedia.swift */; };
 		F755BD9B20594AC7008C5FBB /* NCService.swift in Sources */ = {isa = PBXBuildFile; fileRef = F755BD9A20594AC7008C5FBB /* NCService.swift */; };
@@ -577,6 +578,8 @@
 		F74C4FBA2328C3C100A23E25 /* OpenSSL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenSSL.framework; path = Carthage/Build/iOS/OpenSSL.framework; sourceTree = "<group>"; };
 		F74D3DBD1BAC1941000BAE4B /* OCNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OCNetworking.h; sourceTree = "<group>"; };
 		F74D3DBE1BAC1941000BAE4B /* OCNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OCNetworking.m; sourceTree = "<group>"; };
+		F74EE6502481162C0073B8E0 /* NCNetworkingEndToEnd.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NCNetworkingEndToEnd.m; sourceTree = "<group>"; };
+		F74EE6512481162C0073B8E0 /* NCNetworkingEndToEnd.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NCNetworkingEndToEnd.h; sourceTree = "<group>"; };
 		F7501C302212E57400FB1415 /* NCMedia.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = NCMedia.storyboard; sourceTree = "<group>"; };
 		F7501C312212E57400FB1415 /* NCMedia.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCMedia.swift; sourceTree = "<group>"; };
 		F7514EDA1C7B1336008F3338 /* CCHud.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCHud.h; sourceTree = "<group>"; };
@@ -1345,6 +1348,8 @@
 				F7ACE4291BAC0268006C0017 /* Acknowledgements.h */,
 				F7ACE42B1BAC0268006C0017 /* Acknowledgements.rtf */,
 				F7ACE42A1BAC0268006C0017 /* Acknowledgements.m */,
+				F74EE6512481162C0073B8E0 /* NCNetworkingEndToEnd.h */,
+				F74EE6502481162C0073B8E0 /* NCNetworkingEndToEnd.m */,
 				F7A321AB1E9E6AD50069AD1B /* CCAdvanced.h */,
 				F7A321AC1E9E6AD50069AD1B /* CCAdvanced.m */,
 				F7ACE42C1BAC0268006C0017 /* CCManageAccount.h */,
@@ -2340,6 +2345,7 @@
 				F7E09CE723E308AD00FB3E9E /* NCMasterNavigationController.swift in Sources */,
 				F79018B6240962C7007C9B6D /* NCViewerImageDismissAnimationController.swift in Sources */,
 				F7020FCE2233D7F700B7297D /* NCCreateFormUploadVoiceNote.swift in Sources */,
+				F74EE6522481162C0073B8E0 /* NCNetworkingEndToEnd.m in Sources */,
 				F7F4B1D823C74B3E00D82A6E /* NCRichWorkspace.swift in Sources */,
 				F726EEEC1FED1C820030B9C8 /* NCEndToEndInitialize.swift in Sources */,
 				F79A65C62191D95E00FF6DCC /* NCSelect.swift in Sources */,

+ 2 - 0
iOSClient/Nextcloud-Bridging-Header.h

@@ -23,3 +23,5 @@
 #import "NCRichDocumentTemplate.h"
 #import "HCFeatures.h"
 #import "NCComments.h"
+
+#import "NCNetworkingEndToEnd.h"

+ 267 - 268
iOSClient/Settings/NCEndToEndInitialize.swift

@@ -28,7 +28,7 @@ import NCCommunication
     
     func endToEndInitializeSuccess()
 }
-
+/*
 class NCEndToEndInitialize : NSObject  {
 
     @objc weak var delegate: NCEndToEndInitializeDelegate?
@@ -287,277 +287,276 @@ class NCEndToEndInitialize : NSObject  {
     }
     
 }
+*/
 
+ class NCEndToEndInitialize : NSObject  {
 
-/*
-class NCEndToEndInitialize : NSObject  {
+     @objc weak var delegate: NCEndToEndInitializeDelegate?
 
-    @objc weak var delegate: NCEndToEndInitializeDelegate?
-
-    let appDelegate = UIApplication.shared.delegate as! AppDelegate
-    
-    override init() {
-    }
-    
-    // --------------------------------------------------------------------------------------------
-    // MARK: Initialize
-    // --------------------------------------------------------------------------------------------
-    
-    @objc func initEndToEndEncryption() {
-        
-        // Clear all keys
-        CCUtility.clearAllKeysEnd(toEnd: appDelegate.activeAccount)
-        
-        self.getPublicKey()
-    }
-    
-    func getPublicKey() {
-    
-        NCNetworkingEndToEnd.sharedManager()?.getPublicKey(withAccount: appDelegate.activeAccount, completion: { (account, publicKey, message, errorCode) in
-            
-            if (errorCode == 0 && account == self.appDelegate.activeAccount) {
-                
-                CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
-                
-                // Request PrivateKey chiper to Server
-                self.getPrivateKeyCipher()
-                
-            } else if errorCode != 0 {
-                
-                switch errorCode {
-                    
-                case 400:
-                    NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                    
-                case 404:
-                    guard let csr = NCEndToEndEncryption.sharedManager().createCSR(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData()) else {
-                        
-                        NCContentPresenter.shared.messageNotification("E2E Csr", description: "Error to create Csr", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                        
-                        return
-                    }
-                    
-                    NCNetworkingEndToEnd.sharedManager()?.signPublicKey(withAccount: account, publicKey: csr, completion: { (account, publicKey, message, errorCode) in
-                        
-                        if (errorCode == 0 && account == self.appDelegate.activeAccount) {
-                            
-                            CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
-                            
-                            // Request PrivateKey chiper to Server
-                            self.getPrivateKeyCipher()
-                            
-                        } else if errorCode != 0 {
-                            
-                            switch errorCode {
-                                
-                            case 400:
-                                NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                
-                            case 409:
-                                NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "conflict: a public key for the user already exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                
-                            default:
-                                NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                            }
-                        }
-                    })
-                    
-                case 409:
-                    NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "forbidden: the user can't access the public keys", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                    
-                default:
-                    NCContentPresenter.shared.messageNotification("E2E get publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                }
-            }
-        })
-    }
-    
-    func getPrivateKeyCipher() {
-        
-        // Request PrivateKey chiper to Server
-        NCNetworkingEndToEnd.sharedManager()?.getPrivateKeyCipher(withAccount: appDelegate.activeAccount, completion: { (account, privateKeyChiper, message, errorCode) in
-            
-            if (errorCode == 0 && account == self.appDelegate.activeAccount) {
-                
-                // request Passphrase
-                
-                var passphraseTextField: UITextField?
-                
-                let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
-                
-                //TEST
-                /*
-                 if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
+     let appDelegate = UIApplication.shared.delegate as! AppDelegate
+     
+     override init() {
+     }
+     
+     // --------------------------------------------------------------------------------------------
+     // MARK: Initialize
+     // --------------------------------------------------------------------------------------------
+     
+     @objc func initEndToEndEncryption() {
+         
+         // Clear all keys
+         CCUtility.clearAllKeysEnd(toEnd: appDelegate.activeAccount)
+         
+         self.getPublicKey()
+     }
+     
+     func getPublicKey() {
+     
+         NCNetworkingEndToEnd.sharedManager()?.getPublicKey(withAccount: appDelegate.activeAccount, completion: { (account, publicKey, message, errorCode) in
+             
+             if (errorCode == 0 && account == self.appDelegate.activeAccount) {
+                 
+                 CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
+                 
+                 // Request PrivateKey chiper to Server
+                 self.getPrivateKeyCipher()
                  
-                 let fileURL = dir.appendingPathComponent("privatekey.txt")
+             } else if errorCode != 0 {
                  
-                 //writing
-                 do {
-                 try metadataNet.key.write(to: fileURL, atomically: false, encoding: .utf8)
+                 switch errorCode {
+                     
+                 case 400:
+                     NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                     
+                 case 404:
+                     guard let csr = NCEndToEndEncryption.sharedManager().createCSR(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData()) else {
+                         
+                         NCContentPresenter.shared.messageNotification("E2E Csr", description: "Error to create Csr", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                         
+                         return
+                     }
+                     
+                     NCNetworkingEndToEnd.sharedManager()?.signPublicKey(withAccount: account, publicKey: csr, completion: { (account, publicKey, message, errorCode) in
+                         
+                         if (errorCode == 0 && account == self.appDelegate.activeAccount) {
+                             
+                             CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
+                             
+                             // Request PrivateKey chiper to Server
+                             self.getPrivateKeyCipher()
+                             
+                         } else if errorCode != 0 {
+                             
+                             switch errorCode {
+                                 
+                             case 400:
+                                 NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                 
+                             case 409:
+                                 NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "conflict: a public key for the user already exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                 
+                             default:
+                                 NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                             }
+                         }
+                     })
+                     
+                 case 409:
+                     NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "forbidden: the user can't access the public keys", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                     
+                 default:
+                     NCContentPresenter.shared.messageNotification("E2E get publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
                  }
-                 catch {/* error handling here */}
+             }
+         })
+     }
+     
+     func getPrivateKeyCipher() {
+         
+         // Request PrivateKey chiper to Server
+         NCNetworkingEndToEnd.sharedManager()?.getPrivateKeyCipher(withAccount: appDelegate.activeAccount, completion: { (account, privateKeyChiper, message, errorCode) in
+             
+             if (errorCode == 0 && account == self.appDelegate.activeAccount) {
+                 
+                 // request Passphrase
+                 
+                 var passphraseTextField: UITextField?
+                 
+                 let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
+                 
+                 //TEST
+                 /*
+                  if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
+                  
+                  let fileURL = dir.appendingPathComponent("privatekey.txt")
+                  
+                  //writing
+                  do {
+                  try metadataNet.key.write(to: fileURL, atomically: false, encoding: .utf8)
+                  }
+                  catch {/* error handling here */}
+                  }
+                  */
+                 //
+                 
+                 let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
+                     
+                     let passphrase = passphraseTextField?.text
+                     
+                     let publicKey = CCUtility.getEndToEndPublicKey(self.appDelegate.activeAccount)
+                     
+                     guard let privateKey = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(privateKeyChiper, passphrase: passphrase, publicKey: publicKey)) else {
+                         
+                         NCContentPresenter.shared.messageNotification("E2E decrypt privateKey", description: "Serious internal error to decrypt Private Key", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
+                         
+                         return
+                     }
+                     
+                     // privateKey
+                     print(privateKey)
+                     
+                     // Save to keychain
+                     CCUtility.setEndToEndPrivateKey(self.appDelegate.activeAccount, privateKey: privateKey)
+                     CCUtility.setEndToEndPassphrase(self.appDelegate.activeAccount, passphrase:passphrase)
+                     
+                     // request publicKey Server()
+                     NCNetworkingEndToEnd.sharedManager()?.getServerPublicKey(withAccount: account, completion: { (account, publicKey, message, errorCode) in
+                         
+                         if (errorCode == 0 && account == self.appDelegate.activeAccount) {
+                             
+                             CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
+                             
+                             // Clear Table
+                             NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
+                             NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
+                             
+                             self.delegate?.endToEndInitializeSuccess()
+                             
+                         } else if errorCode != 0 {
+                             
+                             switch (errorCode) {
+                                 
+                             case 400:
+                                 NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                 
+                             case 404:
+                                 NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                 
+                             case 409:
+                                 NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                 
+                             default:
+                                 NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                             }
+                         }
+                     })
+                 })
+                 
+                 let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
                  }
-                 */
-                //
-                
-                let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
-                    
-                    let passphrase = passphraseTextField?.text
-                    
-                    let publicKey = CCUtility.getEndToEndPublicKey(self.appDelegate.activeAccount)
-                    
-                    guard let privateKey = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(privateKeyChiper, passphrase: passphrase, publicKey: publicKey)) else {
-                        
-                        NCContentPresenter.shared.messageNotification("E2E decrypt privateKey", description: "Serious internal error to decrypt Private Key", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
-                        
-                        return
-                    }
-                    
-                    // privateKey
-                    print(privateKey)
-                    
-                    // Save to keychain
-                    CCUtility.setEndToEndPrivateKey(self.appDelegate.activeAccount, privateKey: privateKey)
-                    CCUtility.setEndToEndPassphrase(self.appDelegate.activeAccount, passphrase:passphrase)
-                    
-                    // request publicKey Server()
-                    NCNetworkingEndToEnd.sharedManager()?.getServerPublicKey(withAccount: account, completion: { (account, publicKey, message, errorCode) in
-                        
-                        if (errorCode == 0 && account == self.appDelegate.activeAccount) {
-                            
-                            CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
-                            
-                            // Clear Table
-                            NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
-                            NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
-                            
-                            self.delegate?.endToEndInitializeSuccess()
-                            
-                        } else if errorCode != 0 {
-                            
-                            switch (errorCode) {
-                                
-                            case 400:
-                                NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                
-                            case 404:
-                                NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                
-                            case 409:
-                                NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                
-                            default:
-                                NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                            }
-                        }
-                    })
-                })
-                
-                let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
-                }
-                
-                alertController.addAction(ok)
-                alertController.addAction(cancel)
-                alertController.addTextField { (textField) -> Void in
-                    passphraseTextField = textField
-                    passphraseTextField?.placeholder = "Enter passphrase (12 words)"
-                }
-                
-                self.appDelegate.activeMain.present(alertController, animated: true)
-                
-            } else if errorCode != 0 {
-                
-                switch errorCode {
-                    
-                case 400:
-                    NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                    
-                case 404:
-                    // message
-                    let e2ePassphrase = NYMnemonic.generateString(128, language: "english")
-                    let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase!
-                    
-                    let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
-                    
-                    let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
-                        
-                        var privateKeyString: NSString?
-                        
-                        guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKeyString) else {
-                            
-                            NCContentPresenter.shared.messageNotification("E2E privateKey", description: "Serious internal error to create PrivateKey chiper", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                            
-                            return
-                        }
-                        
-                        NCNetworkingEndToEnd.sharedManager()?.storePrivateKeyCipher(withAccount: account, privateKeyString: privateKeyString! as String, privateKeyChiper: privateKeyChiper, completion: { (account, privateKeyString, privateKey, message, errorCode) in
-                            
-                            if (errorCode == 0 && account == self.appDelegate.activeAccount) {
-                                
-                                CCUtility.setEndToEndPrivateKey(account, privateKey: privateKeyString! as String)
-                                CCUtility.setEndToEndPassphrase(account, passphrase: e2ePassphrase)
-                                
-                                // request publicKey Server()
-                                NCNetworkingEndToEnd.sharedManager()?.getServerPublicKey(withAccount: account, completion: { (account, publicKey, message, errorCode) in
-                                    
-                                    if (errorCode == 0 && account == self.appDelegate.activeAccount) {
-                                        
-                                        CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
-                                        
-                                        // Clear Table
-                                        NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
-                                        NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
-                                        
-                                        self.delegate?.endToEndInitializeSuccess()
-                                        
-                                    } else if errorCode != 0 {
-                                        
-                                        switch (errorCode) {
-                                            
-                                        case 400:
-                                            NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                            
-                                        case 404:
-                                            NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                            
-                                        case 409:
-                                            NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                            
-                                        default:
-                                            NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                        }
-                                    }
-                                })
-                                
-                            } else if errorCode != 0 {
-                                
-                                switch errorCode {
-                                    
-                                case 400:
-                                    NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                    
-                                case 409:
-                                    NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "conflict: a private key for the user already exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                    
-                                default:
-                                    NCContentPresenter.shared.messageNotification("E2E store privateKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                                }
-                            }
-                        })
-                    }
-                    
-                    alertController.addAction(OKAction)
-                    self.appDelegate.activeMain.present(alertController, animated: true)
-                    
-                case 409:
-                    NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "forbidden: the user can't access the private key", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                    
-                default:
-                    NCContentPresenter.shared.messageNotification("E2E get privateKey", description: message,delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
-                }
-            }
-        })
-    }
-    
-}
-*/
+                 
+                 alertController.addAction(ok)
+                 alertController.addAction(cancel)
+                 alertController.addTextField { (textField) -> Void in
+                     passphraseTextField = textField
+                     passphraseTextField?.placeholder = "Enter passphrase (12 words)"
+                 }
+                 
+                 self.appDelegate.activeMain.present(alertController, animated: true)
+                 
+             } else if errorCode != 0 {
+                 
+                 switch errorCode {
+                     
+                 case 400:
+                     NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                     
+                 case 404:
+                     // message
+                     let e2ePassphrase = NYMnemonic.generateString(128, language: "english")
+                     let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase!
+                     
+                     let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
+                     
+                     let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
+                         
+                         var privateKeyString: NSString?
+                         
+                         guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKeyString) else {
+                             
+                             NCContentPresenter.shared.messageNotification("E2E privateKey", description: "Serious internal error to create PrivateKey chiper", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                             
+                             return
+                         }
+                         
+                         NCNetworkingEndToEnd.sharedManager()?.storePrivateKeyCipher(withAccount: account, privateKeyString: privateKeyString! as String, privateKeyChiper: privateKeyChiper, completion: { (account, privateKeyString, privateKey, message, errorCode) in
+                             
+                             if (errorCode == 0 && account == self.appDelegate.activeAccount) {
+                                 
+                                 CCUtility.setEndToEndPrivateKey(account, privateKey: privateKeyString! as String)
+                                 CCUtility.setEndToEndPassphrase(account, passphrase: e2ePassphrase)
+                                 
+                                 // request publicKey Server()
+                                 NCNetworkingEndToEnd.sharedManager()?.getServerPublicKey(withAccount: account, completion: { (account, publicKey, message, errorCode) in
+                                     
+                                     if (errorCode == 0 && account == self.appDelegate.activeAccount) {
+                                         
+                                         CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
+                                         
+                                         // Clear Table
+                                         NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
+                                         NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
+                                         
+                                         self.delegate?.endToEndInitializeSuccess()
+                                         
+                                     } else if errorCode != 0 {
+                                         
+                                         switch (errorCode) {
+                                             
+                                         case 400:
+                                             NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                             
+                                         case 404:
+                                             NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                             
+                                         case 409:
+                                             NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                             
+                                         default:
+                                             NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                         }
+                                     }
+                                 })
+                                 
+                             } else if errorCode != 0 {
+                                 
+                                 switch errorCode {
+                                     
+                                 case 400:
+                                     NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                     
+                                 case 409:
+                                     NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "conflict: a private key for the user already exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                     
+                                 default:
+                                     NCContentPresenter.shared.messageNotification("E2E store privateKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                                 }
+                             }
+                         })
+                     }
+                     
+                     alertController.addAction(OKAction)
+                     self.appDelegate.activeMain.present(alertController, animated: true)
+                     
+                 case 409:
+                     NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "forbidden: the user can't access the private key", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                     
+                 default:
+                     NCContentPresenter.shared.messageNotification("E2E get privateKey", description: message,delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                 }
+             }
+         })
+     }
+     
+ }
+

+ 0 - 1
iOSClient/Settings/NCNetworkingEndToEnd.h

@@ -38,5 +38,4 @@
 - (void)deleteEndToEndPrivateKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion;
 - (void)getEndToEndServerPublicKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *publicKey, NSString *message, NSInteger errorCode))completion;
 
-
 @end