NCKeychain.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //
  2. // NCKeychain.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/10/23.
  6. // Copyright © 2023 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 Foundation
  24. import KeychainAccess
  25. @objc class NCKeychain: NSObject {
  26. let keychain = Keychain(service: "com.nextcloud.keychain")
  27. // swiftlint:disable identifier_name
  28. let E2E_certificate = "EndToEndCertificate_"
  29. let E2E_PrivateKey = "EndToEndPrivateKey_"
  30. let E2E_Passphrase = "EndToEndPassphrase_"
  31. let E2E_PublicKey = "EndToEndPublicKeyServer_"
  32. // swiftlint:enable identifier_name
  33. override init() {
  34. super.init()
  35. }
  36. var typeFilterScanDocument: NCGlobal.TypeFilterScanDocument {
  37. get {
  38. if let rawValue = try? keychain.get("ScanDocumentTypeFilter"), let value = NCGlobal.TypeFilterScanDocument(rawValue: rawValue) {
  39. return value
  40. } else {
  41. return .original
  42. }
  43. }
  44. set {
  45. keychain["ScanDocumentTypeFilter"] = newValue.rawValue
  46. }
  47. }
  48. @objc var passcode: String? {
  49. get {
  50. migrate(key: "passcodeBlock")
  51. if let value = try? keychain.get("passcodeBlock") {
  52. return value
  53. }
  54. return nil
  55. }
  56. set {
  57. keychain["passcodeBlock"] = newValue
  58. }
  59. }
  60. @objc var requestPasscodeAtStart: Bool {
  61. get {
  62. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  63. if let value = keychainOLD["notPasscodeAtStart"], !value.isEmpty {
  64. if value == "true" {
  65. keychain["requestPasscodeAtStart"] = "false"
  66. } else if value == "false" {
  67. keychain["requestPasscodeAtStart"] = "true"
  68. }
  69. keychainOLD["notPasscodeAtStart"] = nil
  70. }
  71. if let value = try? keychain.get("requestPasscodeAtStart"), let result = Bool(value) {
  72. return result
  73. }
  74. return true
  75. }
  76. set {
  77. keychain["requestPasscodeAtStart"] = String(newValue)
  78. }
  79. }
  80. @objc var touchFaceID: Bool {
  81. get {
  82. migrate(key: "enableTouchFaceID")
  83. if let value = try? keychain.get("enableTouchFaceID"), let result = Bool(value) {
  84. return result
  85. }
  86. return false
  87. }
  88. set {
  89. keychain["enableTouchFaceID"] = String(newValue)
  90. }
  91. }
  92. var intro: Bool {
  93. get {
  94. migrate(key: "intro")
  95. if let value = try? keychain.get("intro"), let result = Bool(value) {
  96. return result
  97. }
  98. return false
  99. }
  100. set {
  101. keychain["intro"] = String(newValue)
  102. }
  103. }
  104. @objc var incrementalNumber: String {
  105. migrate(key: "incrementalnumber")
  106. var incrementalString = String(format: "%04ld", 0)
  107. if let value = try? keychain.get("incrementalnumber"), var result = Int(value) {
  108. result += 1
  109. incrementalString = String(format: "%04ld", result)
  110. }
  111. keychain["incrementalnumber"] = incrementalString
  112. return incrementalString
  113. }
  114. @objc var showHiddenFiles: Bool {
  115. get {
  116. migrate(key: "showHiddenFiles")
  117. if let value = try? keychain.get("showHiddenFiles"), let result = Bool(value) {
  118. return result
  119. }
  120. return false
  121. }
  122. set {
  123. keychain["showHiddenFiles"] = String(newValue)
  124. }
  125. }
  126. @objc var formatCompatibility: Bool {
  127. get {
  128. migrate(key: "formatCompatibility")
  129. if let value = try? keychain.get("formatCompatibility"), let result = Bool(value) {
  130. return result
  131. }
  132. return true
  133. }
  134. set {
  135. keychain["formatCompatibility"] = String(newValue)
  136. }
  137. }
  138. @objc var disableFilesApp: Bool {
  139. get {
  140. migrate(key: "disablefilesapp")
  141. if let value = try? keychain.get("disablefilesapp"), let result = Bool(value) {
  142. return result
  143. }
  144. return false
  145. }
  146. set {
  147. keychain["disablefilesapp"] = String(newValue)
  148. }
  149. }
  150. @objc var livePhoto: Bool {
  151. get {
  152. migrate(key: "livePhoto")
  153. if let value = try? keychain.get("livePhoto"), let result = Bool(value) {
  154. return result
  155. }
  156. return true
  157. }
  158. set {
  159. keychain["livePhoto"] = String(newValue)
  160. }
  161. }
  162. @objc var disableCrashservice: Bool {
  163. get {
  164. migrate(key: "crashservice")
  165. if let value = try? keychain.get("crashservice"), let result = Bool(value) {
  166. return result
  167. }
  168. return false
  169. }
  170. set {
  171. keychain["crashservice"] = String(newValue)
  172. }
  173. }
  174. @objc var logLevel: Int {
  175. get {
  176. migrate(key: "logLevel")
  177. if let value = try? keychain.get("logLevel"), let result = Int(value) {
  178. return result
  179. }
  180. return 1
  181. }
  182. set {
  183. keychain["logLevel"] = String(newValue)
  184. }
  185. }
  186. // MARK: -
  187. private func migrate(key: String) {
  188. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  189. if let value = keychainOLD[key], !value.isEmpty {
  190. keychain[key] = value
  191. keychainOLD[key] = nil
  192. }
  193. }
  194. @objc func removeAll() {
  195. try? keychain.removeAll()
  196. }
  197. @objc func getPassword(account: String) -> String {
  198. let key = "password" + account
  199. migrate(key: key)
  200. return (try? keychain.get(key)) ?? ""
  201. }
  202. @objc func setPassword(account: String, password: String?) {
  203. let key = "password" + account
  204. keychain[key] = password
  205. }
  206. @objc func getOriginalFileName(key: String) -> Bool {
  207. migrate(key: key)
  208. if let value = try? keychain.get(key), let result = Bool(value) {
  209. return result
  210. }
  211. return false
  212. }
  213. @objc func setOriginalFileName(key: String, value: Bool) {
  214. keychain[key] = String(value)
  215. }
  216. @objc func getFileNameMask(key: String) -> String {
  217. migrate(key: key)
  218. if let value = try? keychain.get(key) {
  219. return value
  220. } else {
  221. return ""
  222. }
  223. }
  224. @objc func setFileNameMask(key: String, mask: String?) {
  225. keychain[key] = mask
  226. }
  227. @objc func getFileNameType(key: String) -> Bool {
  228. migrate(key: key)
  229. if let value = try? keychain.get(key), let result = Bool(value) {
  230. return result
  231. } else {
  232. return false
  233. }
  234. }
  235. @objc func setFileNameType(key: String, prefix: Bool) {
  236. keychain[key] = String(prefix)
  237. }
  238. // MARK: - E2EE
  239. func getEndToEndCertificate(account: String) -> String? {
  240. let key = E2E_certificate + account
  241. migrate(key: key)
  242. return try? keychain.get(key)
  243. }
  244. func setEndToEndCertificate(account: String, certificate: String?) {
  245. let key = E2E_certificate + account
  246. keychain[key] = certificate
  247. }
  248. func getEndToEndPrivateKey(account: String) -> String? {
  249. let key = E2E_PrivateKey + account
  250. migrate(key: key)
  251. return try? keychain.get(key)
  252. }
  253. func setEndToEndPrivateKey(account: String, privateKey: String?) {
  254. let key = E2E_PrivateKey + account
  255. keychain[key] = privateKey
  256. }
  257. func getEndToEndPublicKey(account: String) -> String? {
  258. let key = E2E_PublicKey + account
  259. migrate(key: key)
  260. return try? keychain.get(key)
  261. }
  262. func setEndToEndPublicKey(account: String, publicKey: String?) {
  263. let key = E2E_PublicKey + account
  264. keychain[key] = publicKey
  265. }
  266. func getEndToEndPassphrase(account: String) -> String? {
  267. let key = E2E_Passphrase + account
  268. migrate(key: key)
  269. return try? keychain.get(key)
  270. }
  271. func setEndToEndPassphrase(account: String, passphrase: String?) {
  272. let key = E2E_Passphrase + account
  273. keychain[key] = passphrase
  274. }
  275. func isEndToEndEnabled(account: String) -> Bool {
  276. guard let certificate = getEndToEndCertificate(account: account), !certificate.isEmpty,
  277. let publicKey = getEndToEndPublicKey(account: account), !publicKey.isEmpty,
  278. let privateKey = getEndToEndPrivateKey(account: account), !privateKey.isEmpty,
  279. let passphrase = getEndToEndPassphrase(account: account), !passphrase.isEmpty,
  280. NCGlobal.shared.e2eeVersions.contains(NCGlobal.shared.capabilityE2EEApiVersion) else { return false }
  281. return true
  282. }
  283. func clearAllKeysEndToEnd(account: String) {
  284. setEndToEndCertificate(account: account, certificate: nil)
  285. setEndToEndPrivateKey(account: account, privateKey: nil)
  286. setEndToEndPublicKey(account: account, publicKey: nil)
  287. setEndToEndPassphrase(account: account, passphrase: nil)
  288. }
  289. }