NCEndToEndInitialize.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 { account, certificate, data, error in
  44. completion(error)
  45. }
  46. }
  47. func getPublicKey() {
  48. NextcloudKit.shared.getE2EECertificate { account, certificate, data, 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. guard let privateKey = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(privateKeyChiper, passphrase: passphrase, publicKey: publicKey)) else {
  110. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "Serious internal error to decrypt Private Key")
  111. NCContentPresenter.shared.messageNotification("E2E decrypt privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  112. return
  113. }
  114. // privateKey
  115. print(privateKey)
  116. // Save to keychain
  117. CCUtility.setEndToEndPrivateKey(self.appDelegate.account, privateKey: privateKey)
  118. CCUtility.setEndToEndPassphrase(self.appDelegate.account, passphrase: passphrase)
  119. // request server publicKey
  120. NextcloudKit.shared.getE2EEPublicKey { account, publicKey, data, error in
  121. if error == .success && account == self.appDelegate.account {
  122. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  123. // Clear Table
  124. NCManageDatabase.shared.clearTable(tableDirectory.self, account: account)
  125. NCManageDatabase.shared.clearTable(tableE2eEncryption.self, account: account)
  126. self.delegate?.endToEndInitializeSuccess()
  127. } else if error != .success {
  128. switch error.errorCode {
  129. case NCGlobal.shared.errorBadRequest:
  130. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  131. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  132. case NCGlobal.shared.errorResourceNotFound:
  133. let error = NKError(errorCode: error.errorCode, errorDescription: "Server publickey doesn't exists")
  134. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  135. case NCGlobal.shared.errorConflict:
  136. let error = NKError(errorCode: error.errorCode, errorDescription: "forbidden: the user can't access the Server publickey")
  137. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  138. default:
  139. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  140. }
  141. }
  142. }
  143. })
  144. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ -> Void in
  145. }
  146. alertController.addAction(ok)
  147. alertController.addAction(cancel)
  148. alertController.addTextField { textField -> Void in
  149. passphraseTextField = textField
  150. passphraseTextField?.placeholder = NSLocalizedString("_enter_passphrase_", comment: "")
  151. }
  152. self.appDelegate.window?.rootViewController?.present(alertController, animated: true)
  153. } else if error != .success {
  154. switch error.errorCode {
  155. case NCGlobal.shared.errorBadRequest:
  156. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  157. NCContentPresenter.shared.messageNotification("E2E get privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  158. case NCGlobal.shared.errorResourceNotFound:
  159. // message
  160. guard let e2ePassphrase = NYMnemonic.generateString(128, language: "english") else { return }
  161. let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase
  162. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  163. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { _ in
  164. self.createNewE2EE(e2ePassphrase: e2ePassphrase, error: error, copyPassphrase: false)
  165. }
  166. let copyAction = UIAlertAction(title: NSLocalizedString("_ok_copy_passphrase_", comment: ""), style: .default) { _ in
  167. self.createNewE2EE(e2ePassphrase: e2ePassphrase, error: error, copyPassphrase: true)
  168. }
  169. alertController.addAction(OKAction)
  170. alertController.addAction(copyAction)
  171. self.appDelegate.window?.rootViewController?.present(alertController, animated: true)
  172. case NCGlobal.shared.errorConflict:
  173. let error = NKError(errorCode: error.errorCode, errorDescription: "forbidden: the user can't access the private key")
  174. NCContentPresenter.shared.messageNotification("E2E get privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  175. default:
  176. NCContentPresenter.shared.messageNotification("E2E get privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  177. }
  178. }
  179. }
  180. }
  181. func createNewE2EE(e2ePassphrase: String, error: NKError, copyPassphrase: Bool) {
  182. var privateKeyString: NSString?
  183. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.userId, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKeyString) else {
  184. let error = NKError(errorCode: error.errorCode, errorDescription: "Serious internal error to create PrivateKey chiper")
  185. NCContentPresenter.shared.messageNotification("E2E privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  186. return
  187. }
  188. // privateKeyChiper
  189. print(privateKeyChiper)
  190. NextcloudKit.shared.storeE2EEPrivateKey(privateKey: privateKeyChiper) { account, privateKey, data, error in
  191. if error == .success && account == self.appDelegate.account {
  192. CCUtility.setEndToEndPrivateKey(account, privateKey: privateKeyString! as String)
  193. CCUtility.setEndToEndPassphrase(account, passphrase: e2ePassphrase)
  194. // request server publicKey
  195. NextcloudKit.shared.getE2EEPublicKey { account, publicKey, data, error in
  196. if error == .success && account == self.appDelegate.account {
  197. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  198. // Clear Table
  199. NCManageDatabase.shared.clearTable(tableDirectory.self, account: account)
  200. NCManageDatabase.shared.clearTable(tableE2eEncryption.self, account: account)
  201. if copyPassphrase {
  202. UIPasteboard.general.string = e2ePassphrase
  203. }
  204. self.delegate?.endToEndInitializeSuccess()
  205. } else if error != .success {
  206. switch error.errorCode {
  207. case NCGlobal.shared.errorBadRequest:
  208. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  209. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  210. case NCGlobal.shared.errorResourceNotFound:
  211. let error = NKError(errorCode: error.errorCode, errorDescription: "Server publickey doesn't exists")
  212. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  213. case NCGlobal.shared.errorConflict:
  214. let error = NKError(errorCode: error.errorCode, errorDescription: "forbidden: the user can't access the Server publickey")
  215. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  216. default:
  217. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  218. }
  219. }
  220. }
  221. } else if error != .success {
  222. switch error.errorCode {
  223. case NCGlobal.shared.errorBadRequest:
  224. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  225. NCContentPresenter.shared.messageNotification("E2E store privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  226. case NCGlobal.shared.errorConflict:
  227. let error = NKError(errorCode: error.errorCode, errorDescription: "conflict: a private key for the user already exists")
  228. NCContentPresenter.shared.messageNotification("E2E store privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  229. default:
  230. NCContentPresenter.shared.messageNotification("E2E store privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  231. }
  232. }
  233. }
  234. }
  235. }