NCKeychain.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. @objc var accountRequest: Bool {
  187. get {
  188. migrate(key: "accountRequest")
  189. if let value = try? keychain.get("accountRequest"), let result = Bool(value) {
  190. return result
  191. }
  192. return false
  193. }
  194. set {
  195. keychain["accountRequest"] = String(newValue)
  196. }
  197. }
  198. @objc var removePhotoCameraRoll: Bool {
  199. get {
  200. migrate(key: "removePhotoCameraRoll")
  201. if let value = try? keychain.get("removePhotoCameraRoll"), let result = Bool(value) {
  202. return result
  203. }
  204. return false
  205. }
  206. set {
  207. keychain["removePhotoCameraRoll"] = String(newValue)
  208. }
  209. }
  210. @objc var privacyScreenEnabled: Bool {
  211. get {
  212. migrate(key: "privacyScreen")
  213. if let value = try? keychain.get("privacyScreen"), let result = Bool(value) {
  214. return result
  215. }
  216. return false
  217. }
  218. set {
  219. keychain["privacyScreen"] = String(newValue)
  220. }
  221. }
  222. @objc var cleanUpDay: Int {
  223. get {
  224. migrate(key: "cleanUpDay")
  225. if let value = try? keychain.get("cleanUpDay"), let result = Int(value) {
  226. return result
  227. }
  228. return NCBrandOptions.shared.cleanUpDay
  229. }
  230. set {
  231. keychain["cleanUpDay"] = String(newValue)
  232. }
  233. }
  234. var mediaWidthImage: Int {
  235. get {
  236. migrate(key: "mediaWidthImage")
  237. if let value = try? keychain.get("mediaWidthImage"), let result = Int(value) {
  238. return result
  239. }
  240. return 80
  241. }
  242. set {
  243. keychain["mediaWidthImage"] = String(newValue)
  244. }
  245. }
  246. var mediaSortDate: String {
  247. get {
  248. migrate(key: "mediaSortDate")
  249. if let value = try? keychain.get("mediaSortDate") {
  250. return value
  251. }
  252. return "date"
  253. }
  254. set {
  255. keychain["mediaSortDate"] = newValue
  256. }
  257. }
  258. var textRecognitionStatus: Bool {
  259. get {
  260. migrate(key: "textRecognitionStatus")
  261. if let value = try? keychain.get("textRecognitionStatus"), let result = Bool(value) {
  262. return result
  263. }
  264. return false
  265. }
  266. set {
  267. keychain["textRecognitionStatus"] = String(newValue)
  268. }
  269. }
  270. var deleteAllScanImages: Bool {
  271. get {
  272. migrate(key: "deleteAllScanImages")
  273. if let value = try? keychain.get("deleteAllScanImages"), let result = Bool(value) {
  274. return result
  275. }
  276. return false
  277. }
  278. set {
  279. keychain["deleteAllScanImages"] = String(newValue)
  280. }
  281. }
  282. var qualityScanDocument: Double {
  283. get {
  284. migrate(key: "qualityScanDocument")
  285. if let value = try? keychain.get("qualityScanDocument"), let result = Double(value) {
  286. return result
  287. }
  288. return 2
  289. }
  290. set {
  291. keychain["qualityScanDocument"] = String(newValue)
  292. }
  293. }
  294. // MARK: -
  295. private func migrate(key: String) {
  296. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  297. if let value = keychainOLD[key], !value.isEmpty {
  298. keychain[key] = value
  299. keychainOLD[key] = nil
  300. }
  301. }
  302. @objc func removeAll() {
  303. try? keychain.removeAll()
  304. }
  305. @objc func getPassword(account: String) -> String {
  306. let key = "password" + account
  307. migrate(key: key)
  308. return (try? keychain.get(key)) ?? ""
  309. }
  310. @objc func setPassword(account: String, password: String?) {
  311. let key = "password" + account
  312. keychain[key] = password
  313. }
  314. @objc func getOriginalFileName(key: String) -> Bool {
  315. migrate(key: key)
  316. if let value = try? keychain.get(key), let result = Bool(value) {
  317. return result
  318. }
  319. return false
  320. }
  321. @objc func setOriginalFileName(key: String, value: Bool) {
  322. keychain[key] = String(value)
  323. }
  324. @objc func getFileNameMask(key: String) -> String {
  325. migrate(key: key)
  326. if let value = try? keychain.get(key) {
  327. return value
  328. } else {
  329. return ""
  330. }
  331. }
  332. @objc func setFileNameMask(key: String, mask: String?) {
  333. keychain[key] = mask
  334. }
  335. @objc func getFileNameType(key: String) -> Bool {
  336. migrate(key: key)
  337. if let value = try? keychain.get(key), let result = Bool(value) {
  338. return result
  339. } else {
  340. return false
  341. }
  342. }
  343. @objc func setFileNameType(key: String, prefix: Bool) {
  344. keychain[key] = String(prefix)
  345. }
  346. // MARK: - E2EE
  347. func getEndToEndCertificate(account: String) -> String? {
  348. let key = E2E_certificate + account
  349. migrate(key: key)
  350. return try? keychain.get(key)
  351. }
  352. func setEndToEndCertificate(account: String, certificate: String?) {
  353. let key = E2E_certificate + account
  354. keychain[key] = certificate
  355. }
  356. func getEndToEndPrivateKey(account: String) -> String? {
  357. let key = E2E_PrivateKey + account
  358. migrate(key: key)
  359. return try? keychain.get(key)
  360. }
  361. func setEndToEndPrivateKey(account: String, privateKey: String?) {
  362. let key = E2E_PrivateKey + account
  363. keychain[key] = privateKey
  364. }
  365. func getEndToEndPublicKey(account: String) -> String? {
  366. let key = E2E_PublicKey + account
  367. migrate(key: key)
  368. return try? keychain.get(key)
  369. }
  370. func setEndToEndPublicKey(account: String, publicKey: String?) {
  371. let key = E2E_PublicKey + account
  372. keychain[key] = publicKey
  373. }
  374. func getEndToEndPassphrase(account: String) -> String? {
  375. let key = E2E_Passphrase + account
  376. migrate(key: key)
  377. return try? keychain.get(key)
  378. }
  379. func setEndToEndPassphrase(account: String, passphrase: String?) {
  380. let key = E2E_Passphrase + account
  381. keychain[key] = passphrase
  382. }
  383. func isEndToEndEnabled(account: String) -> Bool {
  384. guard let certificate = getEndToEndCertificate(account: account), !certificate.isEmpty,
  385. let publicKey = getEndToEndPublicKey(account: account), !publicKey.isEmpty,
  386. let privateKey = getEndToEndPrivateKey(account: account), !privateKey.isEmpty,
  387. let passphrase = getEndToEndPassphrase(account: account), !passphrase.isEmpty,
  388. NCGlobal.shared.e2eeVersions.contains(NCGlobal.shared.capabilityE2EEApiVersion) else { return false }
  389. return true
  390. }
  391. func clearAllKeysEndToEnd(account: String) {
  392. setEndToEndCertificate(account: account, certificate: nil)
  393. setEndToEndPrivateKey(account: account, privateKey: nil)
  394. setEndToEndPublicKey(account: account, publicKey: nil)
  395. setEndToEndPassphrase(account: account, passphrase: nil)
  396. }
  397. }