NCEndToEndInitialize.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. class NCEndToEndInitialize : NSObject {
  29. @objc weak var delegate: NCEndToEndInitializeDelegate?
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. override init() {
  32. }
  33. // --------------------------------------------------------------------------------------------
  34. // MARK: Initialize
  35. // --------------------------------------------------------------------------------------------
  36. @objc func initEndToEndEncryption() {
  37. // Clear all keys
  38. CCUtility.clearAllKeysEnd(toEnd: appDelegate.activeAccount)
  39. self.getPublicKey()
  40. }
  41. func getPublicKey() {
  42. NCCommunication.shared.getE2EEPublicKey { (account, publicKey, errorCode, errorDescription) in
  43. //NCNetworkingEndToEnd.sharedManager()?.getPublicKey(withAccount: appDelegate.activeAccount, completion: { (account, publicKey, message, errorCode) 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. //NCNetworkingEndToEnd.sharedManager()?.signPublicKey(withAccount: account, publicKey: csr, completion: { (account, publicKey, message, errorCode) in
  59. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  60. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  61. // Request PrivateKey chiper to Server
  62. self.getPrivateKeyCipher()
  63. } else if errorCode != 0 {
  64. switch errorCode {
  65. case 400:
  66. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  67. case 409:
  68. 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)
  69. default:
  70. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  71. }
  72. }
  73. }
  74. case 409:
  75. 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)
  76. default:
  77. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  78. }
  79. }
  80. }
  81. }
  82. func getPrivateKeyCipher() {
  83. // Request PrivateKey chiper to Server
  84. NCCommunication.shared.getE2EEPrivateKey { (account, privateKeyChiper, errorCode, errorDescription) in
  85. //NCNetworkingEndToEnd.sharedManager()?.getPrivateKeyCipher(withAccount: appDelegate.activeAccount, completion: { (account, privateKeyChiper, message, errorCode) in
  86. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  87. // request Passphrase
  88. var passphraseTextField: UITextField?
  89. let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
  90. //TEST
  91. /*
  92. if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  93. let fileURL = dir.appendingPathComponent("privatekey.txt")
  94. //writing
  95. do {
  96. try metadataNet.key.write(to: fileURL, atomically: false, encoding: .utf8)
  97. }
  98. catch {/* error handling here */}
  99. }
  100. */
  101. //
  102. let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
  103. let passphrase = passphraseTextField?.text
  104. let publicKey = CCUtility.getEndToEndPublicKey(self.appDelegate.activeAccount)
  105. guard let privateKey = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(privateKeyChiper, passphrase: passphrase, publicKey: publicKey)) else {
  106. 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))
  107. return
  108. }
  109. // privateKey
  110. print(privateKey)
  111. // Save to keychain
  112. CCUtility.setEndToEndPrivateKey(self.appDelegate.activeAccount, privateKey: privateKey)
  113. CCUtility.setEndToEndPassphrase(self.appDelegate.activeAccount, passphrase:passphrase)
  114. // request publicKey Server()
  115. NCCommunication.shared.getE2EEServerPublicKey { (account, publicKey, errorCode, errorDescription) in
  116. //NCNetworkingEndToEnd.sharedManager()?.getServerPublicKey(withAccount: account, completion: { (account, publicKey, message, errorCode) in
  117. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  118. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  119. // Clear Table
  120. NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
  121. NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
  122. self.delegate?.endToEndInitializeSuccess()
  123. } else if errorCode != 0 {
  124. switch (errorCode) {
  125. case 400:
  126. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  127. case 404:
  128. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  129. case 409:
  130. 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)
  131. default:
  132. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  133. }
  134. }
  135. }
  136. })
  137. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
  138. }
  139. alertController.addAction(ok)
  140. alertController.addAction(cancel)
  141. alertController.addTextField { (textField) -> Void in
  142. passphraseTextField = textField
  143. passphraseTextField?.placeholder = "Enter passphrase (12 words)"
  144. }
  145. self.appDelegate.activeMain.present(alertController, animated: true)
  146. } else if errorCode != 0 {
  147. switch errorCode {
  148. case 400:
  149. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  150. case 404:
  151. // message
  152. let e2ePassphrase = NYMnemonic.generateString(128, language: "english")
  153. let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase!
  154. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  155. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  156. var privateKeyString: NSString?
  157. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKeyString) else {
  158. NCContentPresenter.shared.messageNotification("E2E privateKey", description: "Serious internal error to create PrivateKey chiper", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  159. return
  160. }
  161. // privateKeyChiper
  162. print(privateKeyChiper)
  163. NCCommunication.shared.storeE2EEPrivateKey(privateKey: privateKeyString! as String) { (account, privateKeyString, errorCode, errorDescription) in
  164. //NCNetworkingEndToEnd.sharedManager()?.storePrivateKeyCipher(withAccount: account, privateKeyString: privateKeyString! as String, privateKeyChiper: privateKeyChiper, completion: { (account, privateKeyString, privateKey, message, errorCode) in
  165. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  166. CCUtility.setEndToEndPrivateKey(account, privateKey: privateKeyString! as String)
  167. CCUtility.setEndToEndPassphrase(account, passphrase: e2ePassphrase)
  168. // request publicKey Server()
  169. NCCommunication.shared.getE2EEServerPublicKey { (account, publicKey, errorCode, errorDescription) in
  170. //NCNetworkingEndToEnd.sharedManager()?.getServerPublicKey(withAccount: account, completion: { (account, publicKey, message, errorCode) in
  171. if (errorCode == 0 && account == self.appDelegate.activeAccount) {
  172. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  173. // Clear Table
  174. NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: account)
  175. NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: account)
  176. self.delegate?.endToEndInitializeSuccess()
  177. } else if errorCode != 0 {
  178. switch (errorCode) {
  179. case 400:
  180. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  181. case 404:
  182. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  183. case 409:
  184. 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)
  185. default:
  186. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  187. }
  188. }
  189. }
  190. } else if errorCode != 0 {
  191. switch errorCode {
  192. case 400:
  193. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  194. case 409:
  195. 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)
  196. default:
  197. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  198. }
  199. }
  200. }
  201. }
  202. alertController.addAction(OKAction)
  203. self.appDelegate.activeMain.present(alertController, animated: true)
  204. case 409:
  205. 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)
  206. default:
  207. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: errorDescription,delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  208. }
  209. }
  210. }
  211. }
  212. }