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