NCManageE2EE.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // NCManageE2EE.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/11/22.
  6. // Copyright © 2022 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 SwiftUI
  24. import NextcloudKit
  25. import TOPasscodeViewController
  26. import LocalAuthentication
  27. @objc class NCManageE2EEInterface: NSObject {
  28. @objc func makeShipDetailsUI(account: String) -> UIViewController {
  29. let account = (UIApplication.shared.delegate as! AppDelegate).account
  30. let details = NCViewE2EE(account: account)
  31. return UIHostingController(rootView: details)
  32. }
  33. }
  34. class NCManageE2EE: NSObject, ObservableObject, NCEndToEndInitializeDelegate, TOPasscodeViewControllerDelegate {
  35. let endToEndInitialize = NCEndToEndInitialize()
  36. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  37. var passcodeType = ""
  38. @Published var isEndToEndEnabled: Bool = false
  39. @Published var statusOfService: NKError?
  40. override init() {
  41. super.init()
  42. endToEndInitialize.delegate = self
  43. isEndToEndEnabled = CCUtility.isEnd(toEndEnabled: appDelegate.account)
  44. endToEndInitialize.statusOfService { error in
  45. self.statusOfService = error
  46. }
  47. }
  48. func endToEndInitializeSuccess() {
  49. isEndToEndEnabled = true
  50. }
  51. // MARK: - Passcode
  52. @objc func requestPasscodeType(_ passcodeType: String) {
  53. let laContext = LAContext()
  54. var error: NSError?
  55. let passcodeViewController = TOPasscodeViewController(passcodeType: .sixDigits, allowCancel: true)
  56. passcodeViewController.delegate = self
  57. passcodeViewController.keypadButtonShowLettering = false
  58. if CCUtility.getEnableTouchFaceID() && laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
  59. if error == nil {
  60. if laContext.biometryType == .faceID {
  61. passcodeViewController.biometryType = .faceID
  62. passcodeViewController.allowBiometricValidation = true
  63. } else if laContext.biometryType == .touchID {
  64. passcodeViewController.biometryType = .touchID
  65. }
  66. passcodeViewController.allowBiometricValidation = true
  67. passcodeViewController.automaticallyPromptForBiometricValidation = true
  68. }
  69. }
  70. self.passcodeType = passcodeType
  71. appDelegate.window?.rootViewController?.present(passcodeViewController, animated: true)
  72. }
  73. @objc func correctPasscode() {
  74. switch self.passcodeType {
  75. case "startE2E":
  76. endToEndInitialize.initEndToEndEncryption()
  77. case "readPassphrase":
  78. if let e2ePassphrase = CCUtility.getEndToEndPassphrase(appDelegate.account) {
  79. print("[LOG]Passphrase: " + e2ePassphrase)
  80. let message = "\n" + NSLocalizedString("_e2e_settings_the_passphrase_is_", comment: "") + "\n\n\n" + e2ePassphrase
  81. let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: message, preferredStyle: .alert)
  82. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in }))
  83. alertController.addAction(UIAlertAction(title: NSLocalizedString("_copy_passphrase_", comment: ""), style: .default, handler: { action in
  84. UIPasteboard.general.string = e2ePassphrase
  85. }))
  86. appDelegate.window?.rootViewController?.present(alertController, animated: true)
  87. }
  88. case "removeLocallyEncryption":
  89. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_remove_", comment: ""), message: NSLocalizedString("_e2e_settings_remove_message_", comment: ""), preferredStyle: .alert)
  90. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_", comment: ""), style: .default, handler: { action in
  91. CCUtility.clearAllKeysEnd(toEnd: self.appDelegate.account)
  92. self.isEndToEndEnabled = CCUtility.isEnd(toEndEnabled: self.appDelegate.account)
  93. }))
  94. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default, handler: { action in }))
  95. appDelegate.window?.rootViewController?.present(alertController, animated: true)
  96. default:
  97. break
  98. }
  99. }
  100. func passcodeViewController(_ passcodeViewController: TOPasscodeViewController, isCorrectCode code: String) -> Bool {
  101. if code == CCUtility.getPasscode() {
  102. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  103. self.correctPasscode()
  104. }
  105. return true
  106. } else {
  107. return false
  108. }
  109. }
  110. func didPerformBiometricValidationRequest(in passcodeViewController: TOPasscodeViewController) {
  111. LAContext().evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: NCBrandOptions.shared.brand) { (success, error) in
  112. if success {
  113. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  114. passcodeViewController.dismiss(animated: true)
  115. self.correctPasscode()
  116. }
  117. }
  118. }
  119. }
  120. func didTapCancel(in passcodeViewController: TOPasscodeViewController) {
  121. passcodeViewController.dismiss(animated: true)
  122. }
  123. }
  124. struct NCViewE2EE: View {
  125. @ObservedObject var manageE2EE = NCManageE2EE()
  126. @State var account: String = ""
  127. var body: some View {
  128. let versionE2EE = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesE2EEApiVersion) ?? ""
  129. VStack {
  130. VStack {
  131. if manageE2EE.statusOfService == nil {
  132. Text(NSLocalizedString("_status_in_progress_", comment: ""))
  133. } else if manageE2EE.statusOfService == .success {
  134. Text(NSLocalizedString("_status_e2ee_on_server_", comment: ""))
  135. } else {
  136. Text(NSLocalizedString("_status_e2ee_not_setup_", comment: ""))
  137. }
  138. if manageE2EE.isEndToEndEnabled {
  139. List {
  140. Section(footer:Text("End-to-End Encription " + versionE2EE)) {
  141. Label {
  142. Text(NSLocalizedString("_e2e_settings_activated_", comment: ""))
  143. } icon: {
  144. Image(systemName: "checkmark.circle.fill")
  145. .resizable()
  146. .scaledToFit()
  147. .frame(width: 25, height: 25)
  148. .foregroundColor(.green)
  149. }
  150. }
  151. Label {
  152. Text(NSLocalizedString("_e2e_settings_read_passphrase_", comment: "")).onTapGesture {
  153. if CCUtility.getPasscode().isEmpty {
  154. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  155. } else {
  156. manageE2EE.requestPasscodeType("readPassphrase")
  157. }
  158. }
  159. } icon: {
  160. Image(systemName: "text.word.spacing")
  161. .resizable()
  162. .scaledToFit()
  163. .frame(width: 25, height: 25)
  164. }
  165. Label {
  166. Text(NSLocalizedString("_e2e_settings_remove_", comment: "")).onTapGesture {
  167. if CCUtility.getPasscode().isEmpty {
  168. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  169. } else {
  170. manageE2EE.requestPasscodeType("removeLocallyEncryption")
  171. }
  172. }
  173. } icon: {
  174. Image(systemName: "xmark.circle")
  175. .resizable()
  176. .scaledToFit()
  177. .frame(width: 25, height: 25)
  178. }
  179. #if DEBUG
  180. DeleteCerificateSection()
  181. #endif
  182. }
  183. } else {
  184. }
  185. /*
  186. if manageE2EE.isEndToEndEnabled {
  187. Text(NSLocalizedString("_e2e_settings_activated_", comment: ""))
  188. } else {
  189. Button(action: {
  190. if CCUtility.getPasscode().isEmpty {
  191. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  192. } else {
  193. manageE2EE.requestPasscodeType("startE2E")
  194. }
  195. }, label: {
  196. Text(NSLocalizedString("_e2e_settings_start_", comment: ""))
  197. })
  198. }
  199. if manageE2EE.isEndToEndEnabled {
  200. Button(action: {
  201. if CCUtility.getPasscode().isEmpty {
  202. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  203. } else {
  204. manageE2EE.requestPasscodeType("readPassphrase")
  205. }
  206. }, label: {
  207. Text(NSLocalizedString("_e2e_settings_read_passphrase_", comment: ""))
  208. })
  209. }
  210. if manageE2EE.isEndToEndEnabled {
  211. Button(action: {
  212. if CCUtility.getPasscode().isEmpty {
  213. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  214. } else {
  215. manageE2EE.requestPasscodeType("removeLocallyEncryption")
  216. }
  217. }, label: {
  218. Text(NSLocalizedString("_e2e_settings_remove_", comment: ""))
  219. })
  220. }
  221. */
  222. }
  223. }
  224. .navigationTitle("Cifratura End-To-End")
  225. }
  226. }
  227. struct DeleteCerificateSection: View {
  228. var body: some View {
  229. Section(header:Text("Delete Server keys")) {
  230. Label {
  231. Text("Delete certificate").onTapGesture {
  232. NextcloudKit.shared.deleteE2EECertificate { account, error in
  233. if error == .success {
  234. NCContentPresenter.shared.messageNotification("E2E delete certificate", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .success)
  235. } else {
  236. NCContentPresenter.shared.messageNotification("E2E delete certificate", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
  237. }
  238. }
  239. }
  240. } icon: {
  241. Image(systemName: "delete.left")
  242. .resizable()
  243. .scaledToFit()
  244. .frame(width: 25, height: 25)
  245. .foregroundColor(.gray)
  246. }
  247. Label {
  248. Text("Delete PrivateKey").onTapGesture {
  249. NextcloudKit.shared.deleteE2EEPrivateKey { account, error in
  250. if error == .success {
  251. NCContentPresenter.shared.messageNotification("E2E delete privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .success)
  252. } else {
  253. NCContentPresenter.shared.messageNotification("E2E delete privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
  254. }
  255. }
  256. }
  257. } icon: {
  258. Image(systemName: "delete.left")
  259. .resizable()
  260. .scaledToFit()
  261. .frame(width: 25, height: 25)
  262. .foregroundColor(.gray)
  263. }
  264. }
  265. }
  266. }