NCEntoToEndInterface.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 deletePublicKey = false;
  27. var deletePrivateKey = false;
  28. var signPublicKey = false;
  29. var storePrivateKey = false;
  30. override init() {
  31. }
  32. // --------------------------------------------------------------------------------------------
  33. // MARK: End To End Encryption - PublicKey
  34. // --------------------------------------------------------------------------------------------
  35. @objc func initEndToEndEncryption() {
  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. 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: "")
  47. }
  48. func getEndToEndPublicKeysFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  49. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPublicKeys, note: message as String!, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  50. switch errorCode {
  51. case 400:
  52. appDelegate.messageNotification("E2E public keys", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  53. case 404:
  54. // public keys couldn't be found
  55. // remove keychain
  56. CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: nil)
  57. guard let publicKey = NCEndToEndEncryption.sharedManager().createEnd(toEndPublicKey: appDelegate.activeUserID, directoryUser: appDelegate.directoryUser) else {
  58. appDelegate.messageNotification("E2E public keys", description: "E2E Error to create PublicKey", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  59. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPublicKeys, note: "E2E Error to create PublicKey", type: k_activityTypeFailure, verbose: false, activeUrl: "")
  60. return
  61. }
  62. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  63. metadataNet.action = actionSignEndToEndPublicKey;
  64. metadataNet.key = publicKey;
  65. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  66. case 409:
  67. 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)
  68. default:
  69. appDelegate.messageNotification("E2E public keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  70. }
  71. }
  72. func signEnd(toEndPublicKeySuccess metadataNet: CCMetadataNet!) {
  73. // Insert CSR To Cheychain end delete
  74. guard let publicKey = NCEndToEndEncryption.sharedManager().getCSRFromDisk(appDelegate.directoryUser, delete: true) else {
  75. appDelegate.messageNotification("E2E public key", description: "Error : publicKey not present", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  76. return
  77. }
  78. // OK signed key locally keychain
  79. CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: publicKey)
  80. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionSignEndToEndPublicKey, note: "E2E PublicKey sign on Server and stored locally", type: k_activityTypeFailure, verbose: false, activeUrl: "")
  81. signPublicKey = true
  82. if (storePrivateKey) {
  83. signPublicKey = false
  84. storePrivateKey = false
  85. alertController("_e2e_settings_encryption_initialize_", message: "_e2e_settings_encryption_activated_")
  86. }
  87. }
  88. func signEnd(toEndPublicKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  89. signPublicKey = false
  90. appDelegate.messageNotification("E2E sign public keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  91. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionSignEndToEndPublicKey, note: message, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  92. }
  93. func deleteEnd(toEndPublicKeySuccess metadataNet: CCMetadataNet!) {
  94. deletePublicKey = true
  95. if (deletePrivateKey) {
  96. deletePublicKey = false
  97. deletePrivateKey = false
  98. initEndToEndEncryption()
  99. }
  100. }
  101. func deleteEnd(toEndPublicKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  102. deletePublicKey = false
  103. appDelegate.messageNotification("E2E delete public key", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  104. }
  105. // --------------------------------------------------------------------------------------------
  106. // MARK: End To End Encryption - PrivateKey
  107. // --------------------------------------------------------------------------------------------
  108. func getEndToEndPrivateKeyCipherSuccess(_ metadataNet: CCMetadataNet!) {
  109. guard let privateKey = NCEndToEndEncryption.sharedManager().decryptPrivateKeyCipher(metadataNet.key, mnemonic: k_Mnemonic_test) else {
  110. appDelegate.messageNotification("E2E decrypt private key", description: "E2E Error to decrypt Private Key", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  111. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: "E2E Error to decrypt Private Key", type: k_activityTypeFailure, verbose: false, activeUrl: "")
  112. return
  113. }
  114. // Save to keychain
  115. CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: privateKey)
  116. // Save mnemonic to keychain
  117. CCUtility.setEndToEndMnemonic(appDelegate.activeAccount, mnemonic:k_Mnemonic_test)
  118. 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: "")
  119. }
  120. func getEndToEndPrivateKeyCipherFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  121. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: message as String!, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  122. switch errorCode {
  123. case 400:
  124. appDelegate.messageNotification("E2E public keys", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  125. case 404:
  126. // private keys couldn't be found
  127. // remove keychain
  128. CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: nil)
  129. CCUtility.setEndToEndMnemonic(appDelegate.activeAccount, mnemonic: nil)
  130. let mnemonic = k_Mnemonic_test;
  131. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().createEnd(toEndPrivateKey: appDelegate.activeUserID, directoryUser: appDelegate.directoryUser, mnemonic: mnemonic) else {
  132. appDelegate.messageNotification("E2E private keys", description: "E2E Error to create PublicKey chiper", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  133. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: "E2E Error to create PublicKey chiper", type: k_activityTypeFailure, verbose: false, activeUrl: "")
  134. return
  135. }
  136. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  137. metadataNet.action = actionStoreEndToEndPrivateKeyCipher
  138. metadataNet.key = privateKeyChiper
  139. metadataNet.password = mnemonic
  140. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  141. case 409:
  142. 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)
  143. default:
  144. appDelegate.messageNotification("E2E private keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  145. }
  146. }
  147. func storeEnd(toEndPrivateKeyCipherSuccess metadataNet: CCMetadataNet!) {
  148. // Insert PrivateKey (end delete) and mnemonic to Cheychain
  149. guard let privateKey = NCEndToEndEncryption.sharedManager().getPrivateKey(fromDisk: appDelegate.directoryUser, delete: true) else {
  150. appDelegate.messageNotification("E2E private key", description: "Error : privateKey not present", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  151. return
  152. }
  153. CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: privateKey)
  154. CCUtility.setEndToEndMnemonic(appDelegate.activeAccount, mnemonic:metadataNet.password)
  155. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionStoreEndToEndPrivateKeyCipher, note: "E2E PrivateKey stored on Server and stored locally", type: k_activityTypeSuccess, verbose: false, activeUrl: "")
  156. storePrivateKey = true
  157. if (signPublicKey) {
  158. signPublicKey = false
  159. storePrivateKey = false
  160. alertController("_e2e_settings_encryption_initialize_", message: "_e2e_settings_encryption_activated_")
  161. }
  162. }
  163. func storeEnd(toEndPrivateKeyCipherFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  164. storePrivateKey = false
  165. appDelegate.messageNotification("E2E sign private key", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  166. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionStoreEndToEndPrivateKeyCipher, note: message, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  167. }
  168. func deleteEnd(toEndPrivateKeySuccess metadataNet: CCMetadataNet!) {
  169. deletePrivateKey = true
  170. if (deletePublicKey) {
  171. deletePublicKey = false
  172. deletePrivateKey = false
  173. initEndToEndEncryption()
  174. }
  175. }
  176. func deleteEnd(toEndPrivateKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  177. deletePrivateKey = false
  178. appDelegate.messageNotification("E2E delete private key", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  179. }
  180. // --------------------------------------------------------------------------------------------
  181. // MARK: End To End Encryption - Server PublicKey
  182. // --------------------------------------------------------------------------------------------
  183. func getEndToEndServerPublicKeySuccess(_ metadataNet: CCMetadataNet!) {
  184. 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: "")
  185. }
  186. func getEndToEndServerPublicKeyFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  187. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndServerPublicKey, note: message as String!, type: k_activityTypeFailure, verbose: false, activeUrl: "")
  188. switch (errorCode) {
  189. case 400:
  190. appDelegate.messageNotification("E2E Server public key", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  191. case 404:
  192. appDelegate.messageNotification("E2E Server public key", description: "Server publickey doesn't exists", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  193. case 409:
  194. 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)
  195. default:
  196. appDelegate.messageNotification("E2E Server public key", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  197. }
  198. }
  199. // --------------------------------------------------------------------------------------------
  200. // MARK: Mark/Delete Encrypted Folder
  201. // --------------------------------------------------------------------------------------------
  202. func markEnd(toEndFolderEncryptedSuccess metadataNet: CCMetadataNet!) {
  203. print("E2E mark folder success")
  204. }
  205. func markEnd(toEndFolderEncryptedFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  206. // Unauthorized
  207. if (errorCode == kOCErrorServerUnauthorized) {
  208. appDelegate.openLoginView(appDelegate.activeMain, loginType: loginModifyPasswordUser)
  209. }
  210. if (errorCode != kOCErrorServerUnauthorized) {
  211. appDelegate.messageNotification("_error_", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  212. }
  213. }
  214. @objc func markEndToEndFolderEncrypted(_ metadata: tableMetadata) {
  215. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  216. metadataNet.action = actionMarkEndToEndFolderEncrypted;
  217. metadataNet.fileID = metadata.fileID;
  218. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  219. }
  220. func deleteEnd(toEndFolderEncryptedSuccess metadataNet: CCMetadataNet!) {
  221. print("E2E delete folder success")
  222. }
  223. func deleteEnd(toEndFolderEncryptedFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  224. // Unauthorized
  225. if (errorCode == kOCErrorServerUnauthorized) {
  226. appDelegate.openLoginView(appDelegate.activeMain, loginType: loginModifyPasswordUser)
  227. }
  228. if (errorCode != kOCErrorServerUnauthorized) {
  229. appDelegate.messageNotification("_error_", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  230. }
  231. }
  232. @objc func deleteEndToEndFolderEncrypted(_ metadata: tableMetadata) {
  233. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  234. metadataNet.action = actionDeleteEndToEndFolderEncrypted;
  235. metadataNet.fileID = metadata.fileID;
  236. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  237. }
  238. // --------------------------------------------------------------------------------------------
  239. // MARK: Form
  240. // --------------------------------------------------------------------------------------------
  241. func alertController(_ title: String, message: String) {
  242. let alertController = UIAlertController(title: NSLocalizedString(title, comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  243. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  244. }
  245. alertController.addAction(OKAction)
  246. appDelegate.activeMain.present(alertController, animated: true)
  247. }
  248. }