NCManageE2EE.swift 15 KB

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