NCEndToEndInitialize.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. //
  2. // NCEndToEndInitialize.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/04/17.
  6. // Copyright © 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import NCCommunication
  25. @objc protocol NCEndToEndInitializeDelegate {
  26. func endToEndInitializeSuccess()
  27. }
  28. class NCEndToEndInitialize : NSObject {
  29. @objc weak var delegate: NCEndToEndInitializeDelegate?
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. override init() {
  32. }
  33. // --------------------------------------------------------------------------------------------
  34. // MARK: Initialize
  35. // --------------------------------------------------------------------------------------------
  36. @objc func initEndToEndEncryption() {
  37. // Clear all keys
  38. CCUtility.clearAllKeysEnd(toEnd: appDelegate.activeAccount)
  39. self.getPublicKey()
  40. }
  41. func getPublicKey() {
  42. NCCommunication.shared.getE2EEPublicKey { (account, publicKey, errorCode, errorDescription) in
  43. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  44. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  45. // Request PrivateKey chiper to Server
  46. self.getPrivateKeyCipher()
  47. } else if errorCode != 0 {
  48. switch errorCode {
  49. case 400:
  50. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  51. case 404:
  52. guard let csr = NCEndToEndEncryption.sharedManager().createCSR(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData()) else {
  53. NCContentPresenter.shared.messageNotification("E2E Csr", description: "Error to create Csr", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  54. return
  55. }
  56. NCCommunication.shared.signE2EEPublicKey(publicKey: csr) { (account, publicKey, errorCode, errorDescription) in
  57. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  58. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  59. // Request PrivateKey chiper to Server
  60. self.getPrivateKeyCipher()
  61. } else if errorCode != 0 {
  62. switch errorCode {
  63. case 400:
  64. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  65. case 409:
  66. 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)
  67. default:
  68. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  69. }
  70. }
  71. }
  72. case 409:
  73. 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)
  74. default:
  75. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  76. }
  77. }
  78. }
  79. }
  80. func getPrivateKeyCipher() {
  81. // Request PrivateKey chiper to Server
  82. NCCommunication.shared.getE2EEPrivateKey { (account, privateKeyChiper, errorCode, errorDescription) in
  83. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  84. // request Passphrase
  85. var passphraseTextField: UITextField?
  86. let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
  87. let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
  88. let passphrase = passphraseTextField?.text
  89. let publicKey = CCUtility.getEndToEndPublicKey(self.appDelegate.activeAccount)
  90. guard let privateKey = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(privateKeyChiper, passphrase: passphrase, publicKey: publicKey)) else {
  91. 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))
  92. return
  93. }
  94. // privateKey
  95. print(privateKey)
  96. // Save to keychain
  97. CCUtility.setEndToEndPrivateKey(self.appDelegate.activeAccount, privateKey: privateKey)
  98. CCUtility.setEndToEndPassphrase(self.appDelegate.activeAccount, passphrase:passphrase)
  99. // request publicKey Server()
  100. NCCommunication.shared.getE2EEServerPublicKey { (account, publicKey, errorCode, errorDescription) in
  101. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  102. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  103. // Clear Table
  104. NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
  105. NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
  106. self.delegate?.endToEndInitializeSuccess()
  107. } else if errorCode != 0 {
  108. switch (errorCode) {
  109. case 400:
  110. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  111. case 404:
  112. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  113. case 409:
  114. 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)
  115. default:
  116. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  117. }
  118. }
  119. }
  120. })
  121. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
  122. }
  123. alertController.addAction(ok)
  124. alertController.addAction(cancel)
  125. alertController.addTextField { (textField) -> Void in
  126. passphraseTextField = textField
  127. passphraseTextField?.placeholder = "Enter passphrase (12 words)"
  128. }
  129. self.appDelegate.activeMain.present(alertController, animated: true)
  130. } else if errorCode != 0 {
  131. switch errorCode {
  132. case 400:
  133. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  134. case 404:
  135. // message
  136. let e2ePassphrase = NYMnemonic.generateString(128, language: "english")
  137. let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase!
  138. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  139. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  140. var privateKeyString: NSString?
  141. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKeyString) else {
  142. NCContentPresenter.shared.messageNotification("E2E privateKey", description: "Serious internal error to create PrivateKey chiper", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  143. return
  144. }
  145. // privateKeyChiper
  146. print(privateKeyChiper)
  147. NCCommunication.shared.storeE2EEPrivateKey(privateKey: privateKeyChiper) { (account, privateKey, errorCode, errorDescription) in
  148. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  149. CCUtility.setEndToEndPrivateKey(account, privateKey: privateKeyString! as String)
  150. CCUtility.setEndToEndPassphrase(account, passphrase: e2ePassphrase)
  151. // request publicKey Server()
  152. NCCommunication.shared.getE2EEServerPublicKey { (account, publicKey, errorCode, errorDescription) in
  153. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  154. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  155. // Clear Table
  156. NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
  157. NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
  158. self.delegate?.endToEndInitializeSuccess()
  159. } else if errorCode != 0 {
  160. switch (errorCode) {
  161. case 400:
  162. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  163. case 404:
  164. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  165. case 409:
  166. 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)
  167. default:
  168. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  169. }
  170. }
  171. }
  172. } else if errorCode != 0 {
  173. switch errorCode {
  174. case 400:
  175. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  176. case 409:
  177. 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)
  178. default:
  179. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  180. }
  181. }
  182. }
  183. }
  184. alertController.addAction(OKAction)
  185. self.appDelegate.activeMain.present(alertController, animated: true)
  186. case 409:
  187. 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)
  188. default:
  189. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: errorDescription,delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /*
  196. class NCEndToEndInitialize : NSObject {
  197. @objc weak var delegate: NCEndToEndInitializeDelegate?
  198. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  199. override init() {
  200. }
  201. // --------------------------------------------------------------------------------------------
  202. // MARK: Initialize
  203. // --------------------------------------------------------------------------------------------
  204. @objc func initEndToEndEncryption() {
  205. // Clear all keys
  206. CCUtility.clearAllKeysEnd(toEnd: appDelegate.activeAccount)
  207. self.getPublicKey()
  208. }
  209. func getPublicKey() {
  210. NCNetworkingEndToEnd.sharedManager()?.getPublicKey(withAccount: appDelegate.activeAccount, completion: { (account, publicKey, message, errorCode) in
  211. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  212. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  213. // Request PrivateKey chiper to Server
  214. self.getPrivateKeyCipher()
  215. } else if errorCode != 0 {
  216. switch errorCode {
  217. case 400:
  218. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  219. case 404:
  220. guard let csr = NCEndToEndEncryption.sharedManager().createCSR(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData()) else {
  221. NCContentPresenter.shared.messageNotification("E2E Csr", description: "Error to create Csr", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  222. return
  223. }
  224. NCNetworkingEndToEnd.sharedManager()?.signPublicKey(withAccount: account, publicKey: csr, completion: { (account, publicKey, message, errorCode) in
  225. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  226. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  227. // Request PrivateKey chiper to Server
  228. self.getPrivateKeyCipher()
  229. } else if errorCode != 0 {
  230. switch errorCode {
  231. case 400:
  232. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  233. case 409:
  234. 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)
  235. default:
  236. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  237. }
  238. }
  239. })
  240. case 409:
  241. 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)
  242. default:
  243. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  244. }
  245. }
  246. })
  247. }
  248. func getPrivateKeyCipher() {
  249. // Request PrivateKey chiper to Server
  250. NCNetworkingEndToEnd.sharedManager()?.getPrivateKeyCipher(withAccount: appDelegate.activeAccount, completion: { (account, privateKeyChiper, message, errorCode) in
  251. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  252. // request Passphrase
  253. var passphraseTextField: UITextField?
  254. let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
  255. //TEST
  256. /*
  257. if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  258. let fileURL = dir.appendingPathComponent("privatekey.txt")
  259. //writing
  260. do {
  261. try metadataNet.key.write(to: fileURL, atomically: false, encoding: .utf8)
  262. }
  263. catch {/* error handling here */}
  264. }
  265. */
  266. //
  267. let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
  268. let passphrase = passphraseTextField?.text
  269. let publicKey = CCUtility.getEndToEndPublicKey(self.appDelegate.activeAccount)
  270. guard let privateKey = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(privateKeyChiper, passphrase: passphrase, publicKey: publicKey)) else {
  271. 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))
  272. return
  273. }
  274. // privateKey
  275. print(privateKey)
  276. // Save to keychain
  277. CCUtility.setEndToEndPrivateKey(self.appDelegate.activeAccount, privateKey: privateKey)
  278. CCUtility.setEndToEndPassphrase(self.appDelegate.activeAccount, passphrase:passphrase)
  279. // request publicKey Server()
  280. NCNetworkingEndToEnd.sharedManager()?.getServerPublicKey(withAccount: account, completion: { (account, publicKey, message, errorCode) in
  281. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  282. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  283. // Clear Table
  284. NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
  285. NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
  286. self.delegate?.endToEndInitializeSuccess()
  287. } else if errorCode != 0 {
  288. switch (errorCode) {
  289. case 400:
  290. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  291. case 404:
  292. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  293. case 409:
  294. 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)
  295. default:
  296. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  297. }
  298. }
  299. })
  300. })
  301. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
  302. }
  303. alertController.addAction(ok)
  304. alertController.addAction(cancel)
  305. alertController.addTextField { (textField) -> Void in
  306. passphraseTextField = textField
  307. passphraseTextField?.placeholder = "Enter passphrase (12 words)"
  308. }
  309. self.appDelegate.activeMain.present(alertController, animated: true)
  310. } else if errorCode != 0 {
  311. switch errorCode {
  312. case 400:
  313. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  314. case 404:
  315. // message
  316. let e2ePassphrase = NYMnemonic.generateString(128, language: "english")
  317. let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase!
  318. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  319. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  320. var privateKeyString: NSString?
  321. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKeyString) else {
  322. NCContentPresenter.shared.messageNotification("E2E privateKey", description: "Serious internal error to create PrivateKey chiper", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  323. return
  324. }
  325. NCNetworkingEndToEnd.sharedManager()?.storePrivateKeyCipher(withAccount: account, privateKeyString: privateKeyString! as String, privateKeyChiper: privateKeyChiper, completion: { (account, privateKeyString, privateKey, message, errorCode) in
  326. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  327. CCUtility.setEndToEndPrivateKey(account, privateKey: privateKeyString! as String)
  328. CCUtility.setEndToEndPassphrase(account, passphrase: e2ePassphrase)
  329. // request publicKey Server()
  330. NCNetworkingEndToEnd.sharedManager()?.getServerPublicKey(withAccount: account, completion: { (account, publicKey, message, errorCode) in
  331. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  332. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  333. // Clear Table
  334. NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
  335. NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
  336. self.delegate?.endToEndInitializeSuccess()
  337. } else if errorCode != 0 {
  338. switch (errorCode) {
  339. case 400:
  340. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  341. case 404:
  342. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  343. case 409:
  344. 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)
  345. default:
  346. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  347. }
  348. }
  349. })
  350. } else if errorCode != 0 {
  351. switch errorCode {
  352. case 400:
  353. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  354. case 409:
  355. 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)
  356. default:
  357. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  358. }
  359. }
  360. })
  361. }
  362. alertController.addAction(OKAction)
  363. self.appDelegate.activeMain.present(alertController, animated: true)
  364. case 409:
  365. 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)
  366. default:
  367. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: message,delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  368. }
  369. }
  370. })
  371. }
  372. }
  373. */