NCKeychain.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. var typeFilterScanDocument: NCGlobal.TypeFilterScanDocument {
  28. get {
  29. if let rawValue = try? keychain.get("ScanDocumentTypeFilter"), let value = NCGlobal.TypeFilterScanDocument(rawValue: rawValue) {
  30. return value
  31. } else {
  32. return .original
  33. }
  34. }
  35. set {
  36. keychain["ScanDocumentTypeFilter"] = newValue.rawValue
  37. }
  38. }
  39. @objc var passcode: String? {
  40. get {
  41. migrate(key: "passcodeBlock")
  42. if let value = try? keychain.get("passcodeBlock") {
  43. return value
  44. }
  45. return nil
  46. }
  47. set {
  48. keychain["passcodeBlock"] = newValue
  49. }
  50. }
  51. @objc var resetAppCounterFail: Bool {
  52. get {
  53. if let value = try? keychain.get("resetAppCounterFail"), let result = Bool(value) {
  54. return result
  55. }
  56. return false
  57. }
  58. set {
  59. keychain["resetAppCounterFail"] = String(newValue)
  60. }
  61. }
  62. var passcodeCounterFail: Int {
  63. get {
  64. if let value = try? keychain.get("passcodeCounterFail"), let result = Int(value) {
  65. return result
  66. }
  67. return 0
  68. }
  69. set {
  70. keychain["passcodeCounterFail"] = String(newValue)
  71. }
  72. }
  73. var passcodeCounterFailReset: Int {
  74. get {
  75. if let value = try? keychain.get("passcodeCounterFailReset"), let result = Int(value) {
  76. return result
  77. }
  78. return 0
  79. }
  80. set {
  81. keychain["passcodeCounterFailReset"] = String(newValue)
  82. }
  83. }
  84. @objc var requestPasscodeAtStart: Bool {
  85. get {
  86. let keychainOLD = Keychain(service: "Crypto Cloud")
  87. if let value = keychainOLD["notPasscodeAtStart"], !value.isEmpty {
  88. if value == "true" {
  89. keychain["requestPasscodeAtStart"] = "false"
  90. } else if value == "false" {
  91. keychain["requestPasscodeAtStart"] = "true"
  92. }
  93. keychainOLD["notPasscodeAtStart"] = nil
  94. }
  95. if let value = try? keychain.get("requestPasscodeAtStart"), let result = Bool(value) {
  96. return result
  97. }
  98. return true
  99. }
  100. set {
  101. keychain["requestPasscodeAtStart"] = String(newValue)
  102. }
  103. }
  104. @objc var touchFaceID: Bool {
  105. get {
  106. migrate(key: "enableTouchFaceID")
  107. if let value = try? keychain.get("enableTouchFaceID"), let result = Bool(value) {
  108. return result
  109. }
  110. return false
  111. }
  112. set {
  113. keychain["enableTouchFaceID"] = String(newValue)
  114. }
  115. }
  116. var intro: Bool {
  117. get {
  118. migrate(key: "intro")
  119. if let value = try? keychain.get("intro"), let result = Bool(value) {
  120. return result
  121. }
  122. return false
  123. }
  124. set {
  125. keychain["intro"] = String(newValue)
  126. }
  127. }
  128. @objc var incrementalNumber: String {
  129. migrate(key: "incrementalnumber")
  130. var incrementalString = String(format: "%04ld", 0)
  131. if let value = try? keychain.get("incrementalnumber"), var result = Int(value) {
  132. result += 1
  133. incrementalString = String(format: "%04ld", result)
  134. }
  135. keychain["incrementalnumber"] = incrementalString
  136. return incrementalString
  137. }
  138. @objc var showHiddenFiles: Bool {
  139. get {
  140. migrate(key: "showHiddenFiles")
  141. if let value = try? keychain.get("showHiddenFiles"), let result = Bool(value) {
  142. return result
  143. }
  144. return false
  145. }
  146. set {
  147. keychain["showHiddenFiles"] = String(newValue)
  148. }
  149. }
  150. @objc var formatCompatibility: Bool {
  151. get {
  152. migrate(key: "formatCompatibility")
  153. if let value = try? keychain.get("formatCompatibility"), let result = Bool(value) {
  154. return result
  155. }
  156. return true
  157. }
  158. set {
  159. keychain["formatCompatibility"] = String(newValue)
  160. }
  161. }
  162. @objc var disableFilesApp: Bool {
  163. get {
  164. migrate(key: "disablefilesapp")
  165. if let value = try? keychain.get("disablefilesapp"), let result = Bool(value) {
  166. return result
  167. }
  168. return false
  169. }
  170. set {
  171. keychain["disablefilesapp"] = String(newValue)
  172. }
  173. }
  174. @objc var livePhoto: Bool {
  175. get {
  176. migrate(key: "livePhoto")
  177. if let value = try? keychain.get("livePhoto"), let result = Bool(value) {
  178. return result
  179. }
  180. return true
  181. }
  182. set {
  183. keychain["livePhoto"] = String(newValue)
  184. }
  185. }
  186. @objc var disableCrashservice: Bool {
  187. get {
  188. migrate(key: "crashservice")
  189. if let value = try? keychain.get("crashservice"), let result = Bool(value) {
  190. return result
  191. }
  192. return false
  193. }
  194. set {
  195. keychain["crashservice"] = String(newValue)
  196. }
  197. }
  198. @objc var logLevel: Int {
  199. get {
  200. migrate(key: "logLevel")
  201. if let value = try? keychain.get("logLevel"), let result = Int(value) {
  202. return result
  203. }
  204. return 1
  205. }
  206. set {
  207. keychain["logLevel"] = String(newValue)
  208. }
  209. }
  210. @objc var accountRequest: Bool {
  211. get {
  212. migrate(key: "accountRequest")
  213. if let value = try? keychain.get("accountRequest"), let result = Bool(value) {
  214. return result
  215. }
  216. return false
  217. }
  218. set {
  219. keychain["accountRequest"] = String(newValue)
  220. }
  221. }
  222. @objc var removePhotoCameraRoll: Bool {
  223. get {
  224. migrate(key: "removePhotoCameraRoll")
  225. if let value = try? keychain.get("removePhotoCameraRoll"), let result = Bool(value) {
  226. return result
  227. }
  228. return false
  229. }
  230. set {
  231. keychain["removePhotoCameraRoll"] = String(newValue)
  232. }
  233. }
  234. @objc var privacyScreenEnabled: Bool {
  235. get {
  236. migrate(key: "privacyScreen")
  237. if let value = try? keychain.get("privacyScreen"), let result = Bool(value) {
  238. return result
  239. }
  240. return false
  241. }
  242. set {
  243. keychain["privacyScreen"] = String(newValue)
  244. }
  245. }
  246. @objc var cleanUpDay: Int {
  247. get {
  248. migrate(key: "cleanUpDay")
  249. if let value = try? keychain.get("cleanUpDay"), let result = Int(value) {
  250. return result
  251. }
  252. return NCBrandOptions.shared.cleanUpDay
  253. }
  254. set {
  255. keychain["cleanUpDay"] = String(newValue)
  256. }
  257. }
  258. var mediaWidthImage: Int {
  259. get {
  260. migrate(key: "mediaWidthImage")
  261. if let value = try? keychain.get("mediaWidthImage"), let result = Int(value) {
  262. return result
  263. }
  264. return 80
  265. }
  266. set {
  267. keychain["mediaWidthImage"] = String(newValue)
  268. }
  269. }
  270. var mediaSortDate: String {
  271. get {
  272. migrate(key: "mediaSortDate")
  273. if let value = try? keychain.get("mediaSortDate") {
  274. return value
  275. }
  276. return "date"
  277. }
  278. set {
  279. keychain["mediaSortDate"] = newValue
  280. }
  281. }
  282. var textRecognitionStatus: Bool {
  283. get {
  284. migrate(key: "textRecognitionStatus")
  285. if let value = try? keychain.get("textRecognitionStatus"), let result = Bool(value) {
  286. return result
  287. }
  288. return false
  289. }
  290. set {
  291. keychain["textRecognitionStatus"] = String(newValue)
  292. }
  293. }
  294. var deleteAllScanImages: Bool {
  295. get {
  296. migrate(key: "deleteAllScanImages")
  297. if let value = try? keychain.get("deleteAllScanImages"), let result = Bool(value) {
  298. return result
  299. }
  300. return false
  301. }
  302. set {
  303. keychain["deleteAllScanImages"] = String(newValue)
  304. }
  305. }
  306. var qualityScanDocument: Double {
  307. get {
  308. migrate(key: "qualityScanDocument")
  309. if let value = try? keychain.get("qualityScanDocument"), let result = Double(value) {
  310. return result
  311. }
  312. return 2
  313. }
  314. set {
  315. keychain["qualityScanDocument"] = String(newValue)
  316. }
  317. }
  318. // MARK: -
  319. private func migrate(key: String) {
  320. let keychainOLD = Keychain(service: "Crypto Cloud")
  321. if let value = keychainOLD[key], !value.isEmpty {
  322. keychain[key] = value
  323. keychainOLD[key] = nil
  324. }
  325. }
  326. @objc func removeAll() {
  327. try? keychain.removeAll()
  328. }
  329. // MARK: -
  330. @objc func getPassword(account: String) -> String {
  331. let key = "password" + account
  332. migrate(key: key)
  333. return (try? keychain.get(key)) ?? ""
  334. }
  335. @objc func setPassword(account: String, password: String?) {
  336. let key = "password" + account
  337. keychain[key] = password
  338. }
  339. @objc func getOriginalFileName(key: String) -> Bool {
  340. migrate(key: key)
  341. if let value = try? keychain.get(key), let result = Bool(value) {
  342. return result
  343. }
  344. return false
  345. }
  346. @objc func setOriginalFileName(key: String, value: Bool) {
  347. keychain[key] = String(value)
  348. }
  349. @objc func getFileNameMask(key: String) -> String {
  350. migrate(key: key)
  351. if let value = try? keychain.get(key) {
  352. return value
  353. } else {
  354. return ""
  355. }
  356. }
  357. @objc func setFileNameMask(key: String, mask: String?) {
  358. keychain[key] = mask
  359. }
  360. @objc func getFileNameType(key: String) -> Bool {
  361. migrate(key: key)
  362. if let value = try? keychain.get(key), let result = Bool(value) {
  363. return result
  364. } else {
  365. return false
  366. }
  367. }
  368. @objc func setFileNameType(key: String, prefix: Bool) {
  369. keychain[key] = String(prefix)
  370. }
  371. // MARK: - E2EE
  372. func getEndToEndCertificate(account: String) -> String? {
  373. let key = "EndToEndCertificate_" + account
  374. migrate(key: key)
  375. return try? keychain.get(key)
  376. }
  377. func setEndToEndCertificate(account: String, certificate: String?) {
  378. let key = "EndToEndCertificate_" + account
  379. keychain[key] = certificate
  380. }
  381. func getEndToEndPrivateKey(account: String) -> String? {
  382. let key = "EndToEndPrivateKey_" + account
  383. migrate(key: key)
  384. return try? keychain.get(key)
  385. }
  386. func setEndToEndPrivateKey(account: String, privateKey: String?) {
  387. let key = "EndToEndPrivateKey_" + account
  388. keychain[key] = privateKey
  389. }
  390. func getEndToEndPublicKey(account: String) -> String? {
  391. let key = "EndToEndPublicKeyServer_" + account
  392. migrate(key: key)
  393. return try? keychain.get(key)
  394. }
  395. func setEndToEndPublicKey(account: String, publicKey: String?) {
  396. let key = "EndToEndPublicKeyServer_" + account
  397. keychain[key] = publicKey
  398. }
  399. func getEndToEndPassphrase(account: String) -> String? {
  400. let key = "EndToEndPassphrase_" + account
  401. migrate(key: key)
  402. return try? keychain.get(key)
  403. }
  404. func setEndToEndPassphrase(account: String, passphrase: String?) {
  405. let key = "EndToEndPassphrase_" + account
  406. keychain[key] = passphrase
  407. }
  408. func isEndToEndEnabled(account: String) -> Bool {
  409. guard let certificate = getEndToEndCertificate(account: account), !certificate.isEmpty,
  410. let publicKey = getEndToEndPublicKey(account: account), !publicKey.isEmpty,
  411. let privateKey = getEndToEndPrivateKey(account: account), !privateKey.isEmpty,
  412. let passphrase = getEndToEndPassphrase(account: account), !passphrase.isEmpty,
  413. NCGlobal.shared.e2eeVersions.contains(NCGlobal.shared.capabilityE2EEApiVersion) else { return false }
  414. return true
  415. }
  416. func clearAllKeysEndToEnd(account: String) {
  417. setEndToEndCertificate(account: account, certificate: nil)
  418. setEndToEndPrivateKey(account: account, privateKey: nil)
  419. setEndToEndPublicKey(account: account, publicKey: nil)
  420. setEndToEndPassphrase(account: account, passphrase: nil)
  421. }
  422. // MARK: - PUSHNOTIFICATION
  423. @objc func getPushNotificationPublicKey(account: String) -> Data? {
  424. let key = "PNPublicKey" + account
  425. return try? keychain.getData(key)
  426. }
  427. @objc func setPushNotificationPublicKey(account: String, data: Data?) {
  428. let key = "PNPublicKey" + account
  429. keychain[data: key] = data
  430. }
  431. @objc func getPushNotificationPrivateKey(account: String) -> Data? {
  432. let key = "PNPrivateKey" + account
  433. return try? keychain.getData(key)
  434. }
  435. @objc func setPushNotificationPrivateKey(account: String, data: Data?) {
  436. let key = "PNPrivateKey" + account
  437. keychain[data: key] = data
  438. }
  439. @objc func getPushNotificationSubscribingPublicKey(account: String) -> String? {
  440. let key = "PNSubscribingPublicKey" + account
  441. return try? keychain.get(key)
  442. }
  443. @objc func setPushNotificationSubscribingPublicKey(account: String, publicKey: String?) {
  444. let key = "PNSubscribingPublicKey" + account
  445. keychain[key] = publicKey
  446. }
  447. @objc func getPushNotificationToken(account: String) -> String? {
  448. let key = "PNToken" + account
  449. return try? keychain.get(key)
  450. }
  451. @objc func setPushNotificationToken(account: String, token: String?) {
  452. let key = "PNToken" + account
  453. keychain[key] = token
  454. }
  455. @objc func getPushNotificationDeviceIdentifier(account: String) -> String? {
  456. let key = "PNDeviceIdentifier" + account
  457. return try? keychain.get(key)
  458. }
  459. @objc func setPushNotificationDeviceIdentifier(account: String, deviceIdentifier: String?) {
  460. let key = "PNDeviceIdentifier" + account
  461. keychain[key] = deviceIdentifier
  462. }
  463. @objc func getPushNotificationDeviceIdentifierSignature(account: String) -> String? {
  464. let key = "PNDeviceIdentifierSignature" + account
  465. return try? keychain.get(key)
  466. }
  467. @objc func setPushNotificationDeviceIdentifierSignature(account: String, deviceIdentifierSignature: String?) {
  468. let key = "PNDeviceIdentifierSignature" + account
  469. keychain[key] = deviceIdentifierSignature
  470. }
  471. @objc func clearAllKeysPushNotification(account: String) {
  472. setPushNotificationPublicKey(account: account, data: nil)
  473. setPushNotificationSubscribingPublicKey(account: account, publicKey: nil)
  474. setPushNotificationPrivateKey(account: account, data: nil)
  475. setPushNotificationToken(account: account, token: nil)
  476. setPushNotificationDeviceIdentifier(account: account, deviceIdentifier: nil)
  477. setPushNotificationDeviceIdentifierSignature(account: account, deviceIdentifierSignature: nil)
  478. }
  479. }