NCManageE2EE.swift 15 KB

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