NCKeychain.swift 17 KB

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