NCEntoToEndInterface.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. //
  2. // NCEntoToEndInterface.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/04/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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. class NCEntoToEndInterface : NSObject, OCNetworkingDelegate {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var getSignPublicKey = false;
  27. var getStorePrivateKey = false;
  28. override init() {
  29. }
  30. // --------------------------------------------------------------------------------------------
  31. // MARK: End To End Encryption - PublicKey
  32. // --------------------------------------------------------------------------------------------
  33. @objc func initEndToEndEncryption() {
  34. getSignPublicKey = false;
  35. getStorePrivateKey = false;
  36. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  37. metadataNet.action = actionGetEndToEndPublicKeys;
  38. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  39. metadataNet.action = actionGetEndToEndPrivateKeyCipher;
  40. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  41. metadataNet.action = actionGetEndToEndServerPublicKey;
  42. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  43. }
  44. func getEndToEndPublicKeysSuccess(_ metadataNet: CCMetadataNet!) {
  45. CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: metadataNet.key)
  46. getSignPublicKey = true
  47. if (getStorePrivateKey) {
  48. getSignPublicKey = false
  49. getStorePrivateKey = false
  50. alertController("_success_", message: "_e2e_settings_activated_")
  51. }
  52. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPublicKeys, note: "E2E PublicKeys present on Server and stored to keychain", type: k_activityTypeSuccess, verbose: false, activeUrl: "")
  53. }
  54. func getEndToEndPublicKeysFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  55. getSignPublicKey = false
  56. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPublicKeys, note: message as String!, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  57. switch errorCode {
  58. case 400:
  59. appDelegate.messageNotification("E2E public keys", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  60. case 404:
  61. // public keys couldn't be found
  62. // remove keychain
  63. CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: nil)
  64. guard let publicKey = NCEndToEndEncryption.sharedManager().createEnd(toEndPublicKey: appDelegate.activeUserID, directoryUser: appDelegate.directoryUser) else {
  65. appDelegate.messageNotification("E2E public keys", description: "E2E Error to create PublicKey", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  66. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPublicKeys, note: "E2E Error to create PublicKey", type: k_activityTypeFailure, verbose: false, activeUrl: "")
  67. return
  68. }
  69. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  70. metadataNet.action = actionSignEndToEndPublicKey;
  71. metadataNet.key = publicKey;
  72. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  73. case 409:
  74. appDelegate.messageNotification("E2E public keys", description: "forbidden: the user can't access the public keys", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  75. default:
  76. appDelegate.messageNotification("E2E public keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  77. }
  78. }
  79. func signEnd(toEndPublicKeySuccess metadataNet: CCMetadataNet!) {
  80. // Insert CSR To Cheychain end delete
  81. guard let publicKey = NCEndToEndEncryption.sharedManager().getCSRFromDisk(appDelegate.directoryUser, delete: true) else {
  82. appDelegate.messageNotification("E2E public key", description: "Error : publicKey not present", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  83. return
  84. }
  85. // OK signed key locally keychain
  86. CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: publicKey)
  87. getSignPublicKey = true
  88. if (getStorePrivateKey) {
  89. getSignPublicKey = false
  90. getStorePrivateKey = false
  91. alertController("_success_", message: "_e2e_settings_activated_")
  92. }
  93. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionSignEndToEndPublicKey, note: "E2E PublicKey sign on Server and stored locally", type: k_activityTypeFailure, verbose: false, activeUrl: "")
  94. }
  95. func signEnd(toEndPublicKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  96. getSignPublicKey = false
  97. appDelegate.messageNotification("E2E sign public keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  98. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionSignEndToEndPublicKey, note: message, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  99. }
  100. func deleteEnd(toEndPublicKeySuccess metadataNet: CCMetadataNet!) {
  101. appDelegate.messageNotification("E2E delete public key", description: "Success", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.success, errorCode: 0)
  102. }
  103. func deleteEnd(toEndPublicKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  104. appDelegate.messageNotification("E2E delete public key", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  105. }
  106. // --------------------------------------------------------------------------------------------
  107. // MARK: End To End Encryption - PrivateKey
  108. // --------------------------------------------------------------------------------------------
  109. func getEndToEndPrivateKeyCipherSuccess(_ metadataNet: CCMetadataNet!) {
  110. // request Passphrase
  111. var passphraseTextField: UITextField?
  112. let alertController = UIAlertController(title: "UIAlertController", message: "UIAlertController With TextField", preferredStyle: .alert)
  113. let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
  114. let passphrase = passphraseTextField?.text
  115. guard let privateKey = NCEndToEndEncryption.sharedManager().decryptPrivateKeyCipher(metadataNet.key, passphrase: passphrase) else {
  116. self.appDelegate.messageNotification("E2E decrypt private key", description: "E2E Error to decrypt Private Key", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  117. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: "E2E Error to decrypt Private Key", type: k_activityTypeFailure, verbose: false, activeUrl: "")
  118. return
  119. }
  120. // Save to keychain
  121. CCUtility.setEndToEndPrivateKey(self.appDelegate.activeAccount, privateKey: privateKey)
  122. // Save passphrase to keychain
  123. CCUtility.setEndToEndPassphrase(self.appDelegate.activeAccount, passphrase:passphrase)
  124. self.getStorePrivateKey = true
  125. if (self.getSignPublicKey) {
  126. self.getSignPublicKey = false
  127. self.getStorePrivateKey = false
  128. self.alertController("_success_", message: "_e2e_settings_activated_")
  129. }
  130. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: "E2E PrivateKey present on Server and stored to keychain", type: k_activityTypeSuccess, verbose: false, activeUrl: "")
  131. })
  132. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
  133. }
  134. alertController.addAction(ok)
  135. alertController.addAction(cancel)
  136. alertController.addTextField { (textField) -> Void in
  137. passphraseTextField = textField
  138. passphraseTextField?.placeholder = "Enter passphrase (12 words)"
  139. }
  140. appDelegate.activeMain.present(alertController, animated: true)
  141. }
  142. func getEndToEndPrivateKeyCipherFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  143. getStorePrivateKey = false;
  144. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: message as String!, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  145. switch errorCode {
  146. case 400:
  147. appDelegate.messageNotification("E2E public keys", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  148. case 404:
  149. // private keys couldn't be found
  150. // remove keychain
  151. CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: nil)
  152. CCUtility.setEndToEndPassphrase(appDelegate.activeAccount, passphrase: nil)
  153. // message
  154. let e2ePassphrase = NYMnemonic.generateString(128, language: "english")
  155. let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase!
  156. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  157. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  158. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().createEnd(toEndPrivateKey: self.appDelegate.activeUserID, directoryUser: self.appDelegate.directoryUser, passphrase: e2ePassphrase) else {
  159. self.appDelegate.messageNotification("E2E private keys", description: "E2E Error to create PublicKey chiper", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  160. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: "E2E Error to create PublicKey chiper", type: k_activityTypeFailure, verbose: false, activeUrl: "")
  161. return
  162. }
  163. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: self.appDelegate.activeAccount)
  164. metadataNet.action = actionStoreEndToEndPrivateKeyCipher
  165. metadataNet.key = privateKeyChiper
  166. metadataNet.password = e2ePassphrase
  167. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  168. }
  169. alertController.addAction(OKAction)
  170. appDelegate.activeMain.present(alertController, animated: true)
  171. case 409:
  172. appDelegate.messageNotification("E2E private keys", description: "forbidden: the user can't access the private keys", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  173. default:
  174. appDelegate.messageNotification("E2E private keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  175. }
  176. }
  177. func storeEnd(toEndPrivateKeyCipherSuccess metadataNet: CCMetadataNet!) {
  178. // Insert PrivateKey (end delete) and passphrase to Cheychain
  179. guard let privateKey = NCEndToEndEncryption.sharedManager().getPrivateKey(fromDisk: appDelegate.directoryUser, delete: true) else {
  180. appDelegate.messageNotification("E2E private key", description: "Error : privateKey not present", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  181. return
  182. }
  183. CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: privateKey)
  184. CCUtility.setEndToEndPassphrase(appDelegate.activeAccount, passphrase:metadataNet.password)
  185. getStorePrivateKey = true
  186. if (getSignPublicKey) {
  187. getSignPublicKey = false
  188. getStorePrivateKey = false
  189. alertController("_success_", message: "_e2e_settings_activated_")
  190. }
  191. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionStoreEndToEndPrivateKeyCipher, note: "E2E PrivateKey stored on Server and stored locally", type: k_activityTypeSuccess, verbose: false, activeUrl: "")
  192. }
  193. func storeEnd(toEndPrivateKeyCipherFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  194. getStorePrivateKey = false
  195. appDelegate.messageNotification("E2E sign private key", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  196. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionStoreEndToEndPrivateKeyCipher, note: message, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  197. }
  198. func deleteEnd(toEndPrivateKeySuccess metadataNet: CCMetadataNet!) {
  199. appDelegate.messageNotification("E2E delete private key", description: "Success", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.success, errorCode: 0)
  200. }
  201. func deleteEnd(toEndPrivateKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  202. appDelegate.messageNotification("E2E delete private key", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  203. }
  204. // --------------------------------------------------------------------------------------------
  205. // MARK: End To End Encryption - Server PublicKey
  206. // --------------------------------------------------------------------------------------------
  207. func getEndToEndServerPublicKeySuccess(_ metadataNet: CCMetadataNet!) {
  208. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndServerPublicKey, note: "E2E Server PublicKey present on Server and stored to keychain", type: k_activityTypeSuccess, verbose: false, activeUrl: "")
  209. }
  210. func getEndToEndServerPublicKeyFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  211. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndServerPublicKey, note: message as String!, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  212. switch (errorCode) {
  213. case 400:
  214. appDelegate.messageNotification("E2E Server public key", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  215. case 404:
  216. appDelegate.messageNotification("E2E Server public key", description: "Server publickey doesn't exists", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  217. case 409:
  218. appDelegate.messageNotification("E2E Server public key", description: "forbidden: the user can't access the Server publickey", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  219. default:
  220. appDelegate.messageNotification("E2E Server public key", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  221. }
  222. }
  223. // --------------------------------------------------------------------------------------------
  224. // MARK: Mark/Delete Encrypted Folder
  225. // --------------------------------------------------------------------------------------------
  226. func markEnd(toEndFolderEncryptedSuccess metadataNet: CCMetadataNet!) {
  227. print("E2E mark folder success")
  228. }
  229. func markEnd(toEndFolderEncryptedFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  230. // Unauthorized
  231. if (errorCode == kOCErrorServerUnauthorized) {
  232. appDelegate.openLoginView(appDelegate.activeMain, loginType: loginModifyPasswordUser)
  233. }
  234. if (errorCode != kOCErrorServerUnauthorized) {
  235. appDelegate.messageNotification("_error_", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  236. }
  237. }
  238. @objc func markEndToEndFolderEncrypted(_ metadata: tableMetadata) {
  239. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  240. metadataNet.action = actionMarkEndToEndFolderEncrypted;
  241. metadataNet.fileID = metadata.fileID;
  242. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  243. }
  244. func deletemarkEnd(toEndFolderEncryptedSuccess metadataNet: CCMetadataNet!) {
  245. print("E2E delete mark folder success")
  246. }
  247. func deletemarkEnd(toEndFolderEncryptedFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  248. // Unauthorized
  249. if (errorCode == kOCErrorServerUnauthorized) {
  250. appDelegate.openLoginView(appDelegate.activeMain, loginType: loginModifyPasswordUser)
  251. }
  252. if (errorCode != kOCErrorServerUnauthorized) {
  253. appDelegate.messageNotification("_error_", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  254. }
  255. }
  256. @objc func deletemarkEndToEndFolderEncrypted(_ metadata: tableMetadata) {
  257. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  258. metadataNet.action = actionDeletemarkEndToEndFolderEncrypted;
  259. metadataNet.fileID = metadata.fileID;
  260. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  261. }
  262. // --------------------------------------------------------------------------------------------
  263. // MARK: Form
  264. // --------------------------------------------------------------------------------------------
  265. func alertController(_ title: String, message: String) {
  266. let alertController = UIAlertController(title: NSLocalizedString(title, comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  267. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  268. }
  269. alertController.addAction(OKAction)
  270. appDelegate.activeMain.present(alertController, animated: true)
  271. }
  272. }