NCManageE2EE.swift 16 KB

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