NCManageE2EE.swift 14 KB

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