NCEndToEndInitialize.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // NCEndToEndInitialize.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. @objc protocol NCEndToEndInitializeDelegate {
  25. func endToEndInitializeSuccess()
  26. }
  27. class NCEndToEndInitialize : NSObject, OCNetworkingDelegate {
  28. @objc weak var delegate: NCEndToEndInitializeDelegate?
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. override init() {
  31. }
  32. // --------------------------------------------------------------------------------------------
  33. // MARK: Initialize
  34. // --------------------------------------------------------------------------------------------
  35. @objc func initEndToEndEncryption() {
  36. // Clear all keys
  37. CCUtility.clearAllKeysEnd(toEnd: appDelegate.activeAccount)
  38. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  39. metadataNet.action = actionGetEndToEndPublicKeys
  40. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  41. }
  42. func getPrivateKeyCipher() {
  43. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  44. metadataNet.action = actionGetEndToEndPrivateKeyCipher
  45. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  46. }
  47. func getPublicKeyServer() {
  48. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  49. metadataNet.action = actionGetEndToEndServerPublicKey
  50. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  51. }
  52. // --------------------------------------------------------------------------------------------
  53. // MARK: Manage PublicKey
  54. // --------------------------------------------------------------------------------------------
  55. func getEndToEndPublicKeysSuccess(_ metadataNet: CCMetadataNet!) {
  56. CCUtility.setEndToEndPublicKey(appDelegate.activeAccount, publicKey: metadataNet.key)
  57. // Request PrivateKey chiper to Server
  58. getPrivateKeyCipher()
  59. }
  60. func getEndToEndPublicKeysFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  61. switch errorCode {
  62. case 400:
  63. appDelegate.messageNotification("E2E get publicKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  64. case 404:
  65. guard let csr = NCEndToEndEncryption.sharedManager().createCSR(appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData()) else {
  66. appDelegate.messageNotification("E2E Csr", description: "Error to create Csr", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  67. return
  68. }
  69. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  70. metadataNet.action = actionSignEndToEndPublicKey;
  71. metadataNet.key = csr;
  72. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  73. case 409:
  74. appDelegate.messageNotification("E2E get publicKey", 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 get publicKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  77. }
  78. }
  79. func signEnd(toEndPublicKeySuccess metadataNet: CCMetadataNet!) {
  80. CCUtility.setEndToEndPublicKey(appDelegate.activeAccount, publicKey: metadataNet.key)
  81. // Request PrivateKey chiper to Server
  82. getPrivateKeyCipher()
  83. }
  84. func signEnd(toEndPublicKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  85. switch errorCode {
  86. case 400:
  87. appDelegate.messageNotification("E2E sign publicKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  88. case 409:
  89. appDelegate.messageNotification("E2E sign publicKey", description: "conflict: a public key for the user already exists", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  90. default:
  91. appDelegate.messageNotification("E2E sign publicKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  92. }
  93. }
  94. // --------------------------------------------------------------------------------------------
  95. // MARK: Manage PrivateKey
  96. // --------------------------------------------------------------------------------------------
  97. func getEndToEndPrivateKeyCipherSuccess(_ metadataNet: CCMetadataNet!) {
  98. // request Passphrase
  99. var passphraseTextField: UITextField?
  100. let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
  101. //TEST
  102. /*
  103. if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  104. let fileURL = dir.appendingPathComponent("privatekey.txt")
  105. //writing
  106. do {
  107. try metadataNet.key.write(to: fileURL, atomically: false, encoding: .utf8)
  108. }
  109. catch {/* error handling here */}
  110. }
  111. */
  112. //
  113. let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
  114. let passphrase = passphraseTextField?.text
  115. let publicKey = CCUtility.getEndToEndPublicKey(self.appDelegate.activeAccount)
  116. guard let privateKey = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(metadataNet.key, passphrase: passphrase, publicKey: publicKey)) else {
  117. self.appDelegate.messageNotification("E2E decrypt privateKey", description: "Serious internal error to decrypt Private Key", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  118. return
  119. }
  120. // privateKey
  121. print(privateKey)
  122. // Save to keychain
  123. CCUtility.setEndToEndPrivateKey(self.appDelegate.activeAccount, privateKey: privateKey)
  124. CCUtility.setEndToEndPassphrase(self.appDelegate.activeAccount, passphrase:passphrase)
  125. // request publicKey Server()
  126. self.getPublicKeyServer()
  127. })
  128. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
  129. }
  130. alertController.addAction(ok)
  131. alertController.addAction(cancel)
  132. alertController.addTextField { (textField) -> Void in
  133. passphraseTextField = textField
  134. passphraseTextField?.placeholder = "Enter passphrase (12 words)"
  135. }
  136. appDelegate.activeMain.present(alertController, animated: true)
  137. }
  138. func getEndToEndPrivateKeyCipherFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  139. switch errorCode {
  140. case 400:
  141. appDelegate.messageNotification("E2E get privateKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  142. case 404:
  143. // message
  144. let e2ePassphrase = NYMnemonic.generateString(128, language: "english")
  145. let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase!
  146. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  147. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  148. var privateKey: NSString?
  149. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKey) else {
  150. self.appDelegate.messageNotification("E2E privateKey", description: "Serious internal error to create PrivateKey chiper", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  151. return
  152. }
  153. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: self.appDelegate.activeAccount)
  154. metadataNet.action = actionStoreEndToEndPrivateKeyCipher
  155. metadataNet.key = privateKey! as String
  156. metadataNet.keyCipher = privateKeyChiper
  157. metadataNet.password = e2ePassphrase
  158. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  159. }
  160. alertController.addAction(OKAction)
  161. appDelegate.activeMain.present(alertController, animated: true)
  162. case 409:
  163. appDelegate.messageNotification("E2E get privateKey", description: "forbidden: the user can't access the private key", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  164. default:
  165. appDelegate.messageNotification("E2E get privateKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  166. }
  167. }
  168. func storeEnd(toEndPrivateKeyCipherSuccess metadataNet: CCMetadataNet!) {
  169. CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: metadataNet.key)
  170. CCUtility.setEndToEndPassphrase(appDelegate.activeAccount, passphrase:metadataNet.password)
  171. // request publicKey Server()
  172. self.getPublicKeyServer()
  173. }
  174. func storeEnd(toEndPrivateKeyCipherFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  175. switch errorCode {
  176. case 400:
  177. appDelegate.messageNotification("E2E store privateKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  178. case 409:
  179. appDelegate.messageNotification("E2E store privateKey", description: "conflict: a private key for the user already exists", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  180. default:
  181. appDelegate.messageNotification("E2E store privateKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  182. }
  183. }
  184. // --------------------------------------------------------------------------------------------
  185. // MARK: Manage Server PublicKey
  186. // --------------------------------------------------------------------------------------------
  187. func getEndToEndServerPublicKeySuccess(_ metadataNet: CCMetadataNet!) {
  188. CCUtility.setEndToEndPublicKeyServer(appDelegate.activeAccount, publicKey: metadataNet.key)
  189. // Clear Table
  190. NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: appDelegate.activeAccount)
  191. NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: appDelegate.activeAccount)
  192. self.delegate?.endToEndInitializeSuccess()
  193. }
  194. func getEndToEndServerPublicKeyFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  195. switch (errorCode) {
  196. case 400:
  197. appDelegate.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  198. case 404:
  199. appDelegate.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  200. case 409:
  201. appDelegate.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  202. default:
  203. appDelegate.messageNotification("E2E Server publicKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  204. }
  205. }
  206. }