NCManageE2EE.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. let vc = UIHostingController(rootView: details)
  32. vc.title = NSLocalizedString("_e2e_settings_", comment: "")
  33. return vc
  34. }
  35. }
  36. class NCManageE2EE: NSObject, ObservableObject, NCEndToEndInitializeDelegate, TOPasscodeViewControllerDelegate {
  37. let endToEndInitialize = NCEndToEndInitialize()
  38. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  39. var passcodeType = ""
  40. @Published var isEndToEndEnabled: Bool = false
  41. @Published var statusOfService: String = NSLocalizedString("_status_in_progress_", comment: "")
  42. override init() {
  43. super.init()
  44. endToEndInitialize.delegate = self
  45. isEndToEndEnabled = CCUtility.isEnd(toEndEnabled: appDelegate.account)
  46. if isEndToEndEnabled {
  47. statusOfService = NSLocalizedString("_status_e2ee_configured_", comment: "")
  48. } else {
  49. endToEndInitialize.statusOfService { error in
  50. if error == .success {
  51. self.statusOfService = NSLocalizedString("_status_e2ee_on_server_", comment: "")
  52. } else {
  53. self.statusOfService = NSLocalizedString("_status_e2ee_not_setup_", comment: "")
  54. }
  55. }
  56. }
  57. }
  58. // MARK: - Delegate
  59. func endToEndInitializeSuccess() {
  60. isEndToEndEnabled = true
  61. }
  62. // MARK: - Passcode
  63. @objc func requestPasscodeType(_ passcodeType: String) {
  64. let laContext = LAContext()
  65. var error: NSError?
  66. let passcodeViewController = TOPasscodeViewController(passcodeType: .sixDigits, allowCancel: true)
  67. passcodeViewController.delegate = self
  68. passcodeViewController.keypadButtonShowLettering = false
  69. if CCUtility.getEnableTouchFaceID() && laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
  70. if error == nil {
  71. if laContext.biometryType == .faceID {
  72. passcodeViewController.biometryType = .faceID
  73. passcodeViewController.allowBiometricValidation = true
  74. } else if laContext.biometryType == .touchID {
  75. passcodeViewController.biometryType = .touchID
  76. }
  77. passcodeViewController.allowBiometricValidation = true
  78. passcodeViewController.automaticallyPromptForBiometricValidation = true
  79. }
  80. }
  81. self.passcodeType = passcodeType
  82. appDelegate.window?.rootViewController?.present(passcodeViewController, animated: true)
  83. }
  84. @objc func correctPasscode() {
  85. switch self.passcodeType {
  86. case "startE2E":
  87. endToEndInitialize.initEndToEndEncryption()
  88. case "readPassphrase":
  89. if let e2ePassphrase = CCUtility.getEndToEndPassphrase(appDelegate.account) {
  90. print("[LOG]Passphrase: " + e2ePassphrase)
  91. let message = "\n" + NSLocalizedString("_e2e_settings_the_passphrase_is_", comment: "") + "\n\n\n" + e2ePassphrase
  92. let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: message, preferredStyle: .alert)
  93. alertController.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in }))
  94. alertController.addAction(UIAlertAction(title: NSLocalizedString("_copy_passphrase_", comment: ""), style: .default, handler: { action in
  95. UIPasteboard.general.string = e2ePassphrase
  96. }))
  97. appDelegate.window?.rootViewController?.present(alertController, animated: true)
  98. }
  99. case "removeLocallyEncryption":
  100. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_remove_", comment: ""), message: NSLocalizedString("_e2e_settings_remove_message_", comment: ""), preferredStyle: .alert)
  101. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_", comment: ""), style: .default, handler: { action in
  102. CCUtility.clearAllKeysEnd(toEnd: self.appDelegate.account)
  103. self.isEndToEndEnabled = CCUtility.isEnd(toEndEnabled: self.appDelegate.account)
  104. }))
  105. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default, handler: { action in }))
  106. appDelegate.window?.rootViewController?.present(alertController, animated: true)
  107. default:
  108. break
  109. }
  110. }
  111. func passcodeViewController(_ passcodeViewController: TOPasscodeViewController, isCorrectCode code: String) -> Bool {
  112. if code == CCUtility.getPasscode() {
  113. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  114. self.correctPasscode()
  115. }
  116. return true
  117. } else {
  118. return false
  119. }
  120. }
  121. func didPerformBiometricValidationRequest(in passcodeViewController: TOPasscodeViewController) {
  122. LAContext().evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: NCBrandOptions.shared.brand) { (success, error) in
  123. if success {
  124. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  125. passcodeViewController.dismiss(animated: true)
  126. self.correctPasscode()
  127. }
  128. }
  129. }
  130. }
  131. func didTapCancel(in passcodeViewController: TOPasscodeViewController) {
  132. passcodeViewController.dismiss(animated: true)
  133. }
  134. }
  135. // MARK: Views
  136. struct NCViewE2EE: View {
  137. @ObservedObject var manageE2EE = NCManageE2EE()
  138. @State var account: String = ""
  139. var body: some View {
  140. let versionE2EE = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesE2EEApiVersion) ?? ""
  141. VStack {
  142. if manageE2EE.isEndToEndEnabled {
  143. List {
  144. Section(header: Text(""), footer: Text(manageE2EE.statusOfService + "\n\n" + "End-to-End Encryption " + versionE2EE)) {
  145. Label {
  146. Text(NSLocalizedString("_e2e_settings_activated_", comment: ""))
  147. } icon: {
  148. Image(systemName: "checkmark.circle.fill")
  149. .resizable()
  150. .scaledToFit()
  151. .foregroundColor(.green)
  152. }
  153. }
  154. HStack {
  155. Label {
  156. Text(NSLocalizedString("_e2e_settings_read_passphrase_", comment: ""))
  157. } icon: {
  158. Image(systemName: "eye")
  159. .resizable()
  160. .scaledToFit()
  161. .foregroundColor(Color(UIColor.systemGray))
  162. }
  163. Spacer()
  164. }
  165. .contentShape(Rectangle())
  166. .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("readPassphrase")
  171. }
  172. }
  173. HStack {
  174. Label {
  175. Text(NSLocalizedString("_e2e_settings_remove_", comment: ""))
  176. } icon: {
  177. Image(systemName: "trash.circle")
  178. .resizable()
  179. .scaledToFit()
  180. .foregroundColor(Color.red)
  181. }
  182. Spacer()
  183. }
  184. .contentShape(Rectangle())
  185. .onTapGesture {
  186. if CCUtility.getPasscode().isEmpty {
  187. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  188. } else {
  189. manageE2EE.requestPasscodeType("removeLocallyEncryption")
  190. }
  191. }
  192. #if DEBUG
  193. DeleteCerificateSection()
  194. #endif
  195. }
  196. } else {
  197. List {
  198. Section(header: Text(""), footer:Text(manageE2EE.statusOfService + "\n\n" + "End-to-End Encryption " + versionE2EE)) {
  199. HStack {
  200. Label {
  201. Text(NSLocalizedString("_e2e_settings_start_", comment: ""))
  202. } icon: {
  203. Image(systemName: "play.circle")
  204. .resizable()
  205. .scaledToFit()
  206. .foregroundColor(.green)
  207. }
  208. Spacer()
  209. }
  210. .contentShape(Rectangle())
  211. .onTapGesture {
  212. if let passcode = CCUtility.getPasscode(), !passcode.isEmpty {
  213. manageE2EE.requestPasscodeType("startE2E")
  214. } else {
  215. NCContentPresenter.shared.showInfo(error: NKError(errorCode: 0, errorDescription: "_e2e_settings_lock_not_active_"))
  216. }
  217. }
  218. }
  219. #if DEBUG
  220. DeleteCerificateSection()
  221. #endif
  222. }
  223. }
  224. }
  225. .background(Color(UIColor.systemGroupedBackground))
  226. }
  227. }
  228. struct DeleteCerificateSection: View {
  229. var body: some View {
  230. Section(header: Text("Delete Server keys"), footer: Text("Available only in debug mode")) {
  231. HStack {
  232. Label {
  233. Text("Delete Certificate")
  234. } icon: {
  235. Image(systemName: "exclamationmark.triangle")
  236. .resizable()
  237. .scaledToFit()
  238. .foregroundColor(Color(UIColor.systemGray))
  239. }
  240. Spacer()
  241. }
  242. .contentShape(Rectangle())
  243. .onTapGesture {
  244. NextcloudKit.shared.deleteE2EECertificate { account, data, error in
  245. if error == .success {
  246. NCContentPresenter.shared.messageNotification("E2E delete certificate", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .success)
  247. } else {
  248. NCContentPresenter.shared.messageNotification("E2E delete certificate", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
  249. }
  250. }
  251. }
  252. HStack {
  253. Label {
  254. Text("Delete PrivateKey")
  255. } icon: {
  256. Image(systemName: "exclamationmark.triangle")
  257. .resizable()
  258. .scaledToFit()
  259. .foregroundColor(Color(UIColor.systemGray))
  260. }
  261. Spacer()
  262. }
  263. .contentShape(Rectangle())
  264. .onTapGesture {
  265. NextcloudKit.shared.deleteE2EEPrivateKey { account, data, error in
  266. if error == .success {
  267. NCContentPresenter.shared.messageNotification("E2E delete privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .success)
  268. } else {
  269. NCContentPresenter.shared.messageNotification("E2E delete privateKey", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. // MARK: - Preview / Test
  277. struct SectionView: View {
  278. @State var height: CGFloat = 0
  279. @State var text: String = ""
  280. var body: some View {
  281. HStack {
  282. Text(text)
  283. }
  284. .frame(maxWidth: .infinity, minHeight: height, alignment: .bottomLeading)
  285. }
  286. }
  287. struct NCViewE2EETest: View {
  288. var body: some View {
  289. VStack {
  290. List {
  291. Section(header:SectionView(height: 50, text: "Section Header View")) {
  292. Label {
  293. Text(NSLocalizedString("_e2e_settings_activated_", comment: ""))
  294. } icon: {
  295. Image(systemName: "checkmark.circle.fill")
  296. .resizable()
  297. .scaledToFit()
  298. .frame(width: 25, height: 25)
  299. .foregroundColor(.green)
  300. }
  301. }
  302. Section(header:SectionView(text: "Section Header View 42")) {
  303. Label {
  304. Text(NSLocalizedString("_e2e_settings_activated_", comment: ""))
  305. } icon: {
  306. Image(systemName: "checkmark.circle.fill")
  307. .resizable()
  308. .scaledToFit()
  309. .frame(width: 25, height: 25)
  310. .foregroundColor(.red)
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. struct NCViewE2EE_Previews: PreviewProvider {
  318. static var previews: some View {
  319. let account = (UIApplication.shared.delegate as! AppDelegate).account
  320. NCViewE2EE(account: account)
  321. }
  322. }