NCEndToEndInitialize.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 UIKit
  24. import NextcloudKit
  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. var extractedPublicKey: String?
  32. override init() {
  33. }
  34. // --------------------------------------------------------------------------------------------
  35. // MARK: Initialize
  36. // --------------------------------------------------------------------------------------------
  37. @objc func initEndToEndEncryption() {
  38. // Clear all keys
  39. CCUtility.clearAllKeysEnd(toEnd: appDelegate.account)
  40. self.getPublicKey()
  41. }
  42. @objc func statusOfService(completion: @escaping (_ error: NKError?) -> Void) {
  43. NextcloudKit.shared.getE2EECertificate { _, _, _, _, error in
  44. completion(error)
  45. }
  46. }
  47. func getPublicKey() {
  48. NextcloudKit.shared.getE2EECertificate { account, certificate, _, _, error in
  49. if error == .success && account == self.appDelegate.account {
  50. CCUtility.setEndToEndCertificate(account, certificate: certificate)
  51. self.extractedPublicKey = NCEndToEndEncryption.sharedManager().extractPublicKey(fromCertificate: certificate)
  52. // Request PrivateKey chiper to Server
  53. self.getPrivateKeyCipher()
  54. } else if error != .success {
  55. switch error.errorCode {
  56. case NCGlobal.shared.errorBadRequest:
  57. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  58. NCContentPresenter.shared.messageNotification("E2E get publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  59. case NCGlobal.shared.errorResourceNotFound:
  60. guard let csr = NCEndToEndEncryption.sharedManager().createCSR(self.appDelegate.userId, directory: CCUtility.getDirectoryUserData()) else {
  61. let error = NKError(errorCode: error.errorCode, errorDescription: "Error to create Csr")
  62. NCContentPresenter.shared.messageNotification("E2E Csr", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  63. return
  64. }
  65. NextcloudKit.shared.signE2EECertificate(certificate: csr) { account, certificate, data, error in
  66. if error == .success && account == self.appDelegate.account {
  67. // TEST publicKey
  68. let extractedPublicKey = NCEndToEndEncryption.sharedManager().extractPublicKey(fromCertificate: certificate)
  69. if extractedPublicKey != NCEndToEndEncryption.sharedManager().generatedPublicKey {
  70. let error = NKError(errorCode: error.errorCode, errorDescription: "error: the public key is incorrect")
  71. NCContentPresenter.shared.messageNotification("E2E sign publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  72. } else {
  73. CCUtility.setEndToEndCertificate(account, certificate: certificate)
  74. // Request PrivateKey chiper to Server
  75. self.getPrivateKeyCipher()
  76. }
  77. } else if error != .success {
  78. switch error.errorCode {
  79. case NCGlobal.shared.errorBadRequest:
  80. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  81. NCContentPresenter.shared.messageNotification("E2E sign publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  82. case NCGlobal.shared.errorConflict:
  83. let error = NKError(errorCode: error.errorCode, errorDescription: "conflict: a public key for the user already exists")
  84. NCContentPresenter.shared.messageNotification("E2E sign publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  85. default:
  86. NCContentPresenter.shared.messageNotification("E2E sign publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  87. }
  88. }
  89. }
  90. case NCGlobal.shared.errorConflict:
  91. let error = NKError(errorCode: error.errorCode, errorDescription: "forbidden: the user can't access the public keys")
  92. NCContentPresenter.shared.messageNotification("E2E get publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  93. default:
  94. NCContentPresenter.shared.messageNotification("E2E get publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  95. }
  96. }
  97. }
  98. }
  99. func getPrivateKeyCipher() {
  100. // Request PrivateKey chiper to Server
  101. NextcloudKit.shared.getE2EEPrivateKey { account, privateKeyChiper, data, error in
  102. if error == .success && account == self.appDelegate.account {
  103. // request Passphrase
  104. var passphraseTextField: UITextField?
  105. let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
  106. let ok = UIAlertAction(title: "OK", style: .default, handler: { _ -> Void in
  107. let passphrase = passphraseTextField?.text
  108. let publicKey = CCUtility.getEndToEndCertificate(self.appDelegate.account)
  109. if let privateKeyData = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(privateKeyChiper, passphrase: passphrase, publicKey: publicKey)),
  110. let keyData = Data(base64Encoded: privateKeyData) {
  111. let privateKey = String(data: keyData, encoding: .utf8)
  112. CCUtility.setEndToEndPrivateKey(self.appDelegate.account, privateKey: privateKey)
  113. } else {
  114. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "Serious internal error to decrypt Private Key")
  115. NCContentPresenter.shared.messageNotification("E2E decrypt privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  116. return
  117. }
  118. // Save to keychain
  119. CCUtility.setEndToEndPassphrase(self.appDelegate.account, passphrase: passphrase)
  120. // request server publicKey
  121. NextcloudKit.shared.getE2EEPublicKey { account, publicKey, data, error in
  122. if error == .success && account == self.appDelegate.account {
  123. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  124. // Clear Table
  125. NCManageDatabase.shared.clearTable(tableDirectory.self, account: account)
  126. NCManageDatabase.shared.clearTable(tableE2eEncryption.self, account: account)
  127. self.delegate?.endToEndInitializeSuccess()
  128. } else if error != .success {
  129. switch error.errorCode {
  130. case NCGlobal.shared.errorBadRequest:
  131. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  132. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  133. case NCGlobal.shared.errorResourceNotFound:
  134. let error = NKError(errorCode: error.errorCode, errorDescription: "Server publickey doesn't exists")
  135. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  136. case NCGlobal.shared.errorConflict:
  137. let error = NKError(errorCode: error.errorCode, errorDescription: "forbidden: the user can't access the Server publickey")
  138. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  139. default:
  140. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  141. }
  142. }
  143. }
  144. })
  145. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ -> Void in
  146. }
  147. alertController.addAction(ok)
  148. alertController.addAction(cancel)
  149. alertController.addTextField { textField -> Void in
  150. passphraseTextField = textField
  151. passphraseTextField?.placeholder = NSLocalizedString("_enter_passphrase_", comment: "")
  152. }
  153. self.appDelegate.window?.rootViewController?.present(alertController, animated: true)
  154. } else if error != .success {
  155. switch error.errorCode {
  156. case NCGlobal.shared.errorBadRequest:
  157. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  158. NCContentPresenter.shared.messageNotification("E2E get privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  159. case NCGlobal.shared.errorResourceNotFound:
  160. // message
  161. guard let e2ePassphrase = NYMnemonic.generateString(128, language: "english") else { return }
  162. let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase
  163. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  164. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
  165. self.createNewE2EE(e2ePassphrase: e2ePassphrase, error: error, copyPassphrase: false)
  166. }
  167. let copyAction = UIAlertAction(title: NSLocalizedString("_ok_copy_passphrase_", comment: ""), style: .default) { _ in
  168. self.createNewE2EE(e2ePassphrase: e2ePassphrase, error: error, copyPassphrase: true)
  169. }
  170. alertController.addAction(OKAction)
  171. alertController.addAction(copyAction)
  172. self.appDelegate.window?.rootViewController?.present(alertController, animated: true)
  173. case NCGlobal.shared.errorConflict:
  174. let error = NKError(errorCode: error.errorCode, errorDescription: "forbidden: the user can't access the private key")
  175. NCContentPresenter.shared.messageNotification("E2E get privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  176. default:
  177. NCContentPresenter.shared.messageNotification("E2E get privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  178. }
  179. }
  180. }
  181. }
  182. func createNewE2EE(e2ePassphrase: String, error: NKError, copyPassphrase: Bool) {
  183. var privateKeyString: NSString?
  184. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.userId, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKeyString) else {
  185. let error = NKError(errorCode: error.errorCode, errorDescription: "Serious internal error to create PrivateKey chiper")
  186. NCContentPresenter.shared.messageNotification("E2E privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  187. return
  188. }
  189. // privateKeyChiper
  190. print(privateKeyChiper)
  191. NextcloudKit.shared.storeE2EEPrivateKey(privateKey: privateKeyChiper) { account, privateKey, data, error in
  192. if error == .success && account == self.appDelegate.account {
  193. CCUtility.setEndToEndPrivateKey(account, privateKey: privateKeyString! as String)
  194. CCUtility.setEndToEndPassphrase(account, passphrase: e2ePassphrase)
  195. // request server publicKey
  196. NextcloudKit.shared.getE2EEPublicKey { account, publicKey, data, error in
  197. if error == .success && account == self.appDelegate.account {
  198. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  199. // Clear Table
  200. NCManageDatabase.shared.clearTable(tableDirectory.self, account: account)
  201. NCManageDatabase.shared.clearTable(tableE2eEncryption.self, account: account)
  202. if copyPassphrase {
  203. UIPasteboard.general.string = e2ePassphrase
  204. }
  205. self.delegate?.endToEndInitializeSuccess()
  206. } else if error != .success {
  207. switch error.errorCode {
  208. case NCGlobal.shared.errorBadRequest:
  209. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  210. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  211. case NCGlobal.shared.errorResourceNotFound:
  212. let error = NKError(errorCode: error.errorCode, errorDescription: "Server publickey doesn't exists")
  213. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  214. case NCGlobal.shared.errorConflict:
  215. let error = NKError(errorCode: error.errorCode, errorDescription: "forbidden: the user can't access the Server publickey")
  216. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  217. default:
  218. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  219. }
  220. }
  221. }
  222. } else if error != .success {
  223. switch error.errorCode {
  224. case NCGlobal.shared.errorBadRequest:
  225. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  226. NCContentPresenter.shared.messageNotification("E2E store privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  227. case NCGlobal.shared.errorConflict:
  228. let error = NKError(errorCode: error.errorCode, errorDescription: "conflict: a private key for the user already exists")
  229. NCContentPresenter.shared.messageNotification("E2E store privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  230. default:
  231. NCContentPresenter.shared.messageNotification("E2E store privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  232. }
  233. }
  234. }
  235. }
  236. }