NCEndToEndInitialize.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. // swiftlint:disable force_cast
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. // swiftlint:enable force_cast
  33. var extractedPublicKey: String?
  34. override init() {
  35. }
  36. // --------------------------------------------------------------------------------------------
  37. // MARK: Initialize
  38. // --------------------------------------------------------------------------------------------
  39. @objc func initEndToEndEncryption() {
  40. // Clear all keys
  41. CCUtility.clearAllKeysEnd(toEnd: appDelegate.account)
  42. self.getPublicKey()
  43. }
  44. @objc func statusOfService(completion: @escaping (_ error: NKError?) -> Void) {
  45. NextcloudKit.shared.getE2EECertificate { _, _, _, _, error in
  46. completion(error)
  47. }
  48. }
  49. func getPublicKey() {
  50. NextcloudKit.shared.getE2EECertificate { account, certificate, _, _, error in
  51. if error == .success && account == self.appDelegate.account {
  52. CCUtility.setEndToEndCertificate(account, certificate: certificate)
  53. self.extractedPublicKey = NCEndToEndEncryption.sharedManager().extractPublicKey(fromCertificate: certificate)
  54. // Request PrivateKey chiper to Server
  55. self.getPrivateKeyCipher()
  56. } else if error != .success {
  57. switch error.errorCode {
  58. case NCGlobal.shared.errorBadRequest:
  59. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  60. NCContentPresenter.shared.messageNotification("E2E get publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  61. case NCGlobal.shared.errorResourceNotFound:
  62. guard let csr = NCEndToEndEncryption.sharedManager().createCSR(self.appDelegate.userId, directory: CCUtility.getDirectoryUserData()) else {
  63. let error = NKError(errorCode: error.errorCode, errorDescription: "Error to create Csr")
  64. NCContentPresenter.shared.messageNotification("E2E Csr", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  65. return
  66. }
  67. NextcloudKit.shared.signE2EECertificate(certificate: csr) { account, certificate, _, error in
  68. if error == .success && account == self.appDelegate.account {
  69. // TEST publicKey
  70. let extractedPublicKey = NCEndToEndEncryption.sharedManager().extractPublicKey(fromCertificate: certificate)
  71. if extractedPublicKey != NCEndToEndEncryption.sharedManager().generatedPublicKey {
  72. let error = NKError(errorCode: error.errorCode, errorDescription: "error: the public key is incorrect")
  73. NCContentPresenter.shared.messageNotification("E2E sign publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  74. } else {
  75. CCUtility.setEndToEndCertificate(account, certificate: certificate)
  76. // Request PrivateKey chiper to Server
  77. self.getPrivateKeyCipher()
  78. }
  79. } else if error != .success {
  80. switch error.errorCode {
  81. case NCGlobal.shared.errorBadRequest:
  82. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  83. NCContentPresenter.shared.messageNotification("E2E sign publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  84. case NCGlobal.shared.errorConflict:
  85. let error = NKError(errorCode: error.errorCode, errorDescription: "conflict: a public key for the user already exists")
  86. NCContentPresenter.shared.messageNotification("E2E sign publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  87. default:
  88. NCContentPresenter.shared.messageNotification("E2E sign publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  89. }
  90. }
  91. }
  92. case NCGlobal.shared.errorConflict:
  93. let error = NKError(errorCode: error.errorCode, errorDescription: "forbidden: the user can't access the public keys")
  94. NCContentPresenter.shared.messageNotification("E2E get publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  95. default:
  96. NCContentPresenter.shared.messageNotification("E2E get publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  97. }
  98. }
  99. }
  100. }
  101. func getPrivateKeyCipher() {
  102. // Request PrivateKey chiper to Server
  103. NextcloudKit.shared.getE2EEPrivateKey { account, privateKeyChiper, _, error in
  104. if error == .success && account == self.appDelegate.account {
  105. // request Passphrase
  106. var passphraseTextField: UITextField?
  107. let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
  108. let ok = UIAlertAction(title: "OK", style: .default, handler: { _ -> Void in
  109. let passphrase = passphraseTextField?.text
  110. let publicKey = CCUtility.getEndToEndCertificate(self.appDelegate.account)
  111. if let privateKeyData = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(privateKeyChiper, passphrase: passphrase, publicKey: publicKey, iterationCount: 1024)),
  112. let keyData = Data(base64Encoded: privateKeyData) {
  113. let privateKey = String(data: keyData, encoding: .utf8)
  114. CCUtility.setEndToEndPrivateKey(self.appDelegate.account, privateKey: privateKey)
  115. } else {
  116. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "Serious internal error to decrypt Private Key")
  117. NCContentPresenter.shared.messageNotification("E2E decrypt privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  118. return
  119. }
  120. // Save to keychain
  121. CCUtility.setEndToEndPassphrase(self.appDelegate.account, passphrase: passphrase)
  122. // request server publicKey
  123. NextcloudKit.shared.getE2EEPublicKey { account, publicKey, _, error in
  124. if error == .success && account == self.appDelegate.account {
  125. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  126. NCManageDatabase.shared.clearTablesE2EE(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, iterationCount: 1024) 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, _, _, 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, _, error in
  197. if error == .success && account == self.appDelegate.account {
  198. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  199. NCManageDatabase.shared.clearTablesE2EE(account: account)
  200. if copyPassphrase {
  201. UIPasteboard.general.string = e2ePassphrase
  202. }
  203. self.delegate?.endToEndInitializeSuccess()
  204. } else if error != .success {
  205. switch error.errorCode {
  206. case NCGlobal.shared.errorBadRequest:
  207. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  208. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  209. case NCGlobal.shared.errorResourceNotFound:
  210. let error = NKError(errorCode: error.errorCode, errorDescription: "Server publickey doesn't exists")
  211. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  212. case NCGlobal.shared.errorConflict:
  213. let error = NKError(errorCode: error.errorCode, errorDescription: "forbidden: the user can't access the Server publickey")
  214. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  215. default:
  216. NCContentPresenter.shared.messageNotification("E2E Server publicKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  217. }
  218. }
  219. }
  220. } else if error != .success {
  221. switch error.errorCode {
  222. case NCGlobal.shared.errorBadRequest:
  223. let error = NKError(errorCode: error.errorCode, errorDescription: "bad request: unpredictable internal error")
  224. NCContentPresenter.shared.messageNotification("E2E store privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  225. case NCGlobal.shared.errorConflict:
  226. let error = NKError(errorCode: error.errorCode, errorDescription: "conflict: a private key for the user already exists")
  227. NCContentPresenter.shared.messageNotification("E2E store privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  228. default:
  229. NCContentPresenter.shared.messageNotification("E2E store privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, priority: .max)
  230. }
  231. }
  232. }
  233. }
  234. }