NCEntoToEndInterface.swift 18 KB

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