NCKeychain.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 incrementalNumber: String {
  130. migrate(key: "incrementalnumber")
  131. var incrementalString = String(format: "%04ld", 0)
  132. if let value = try? keychain.get("incrementalnumber"), var result = Int(value) {
  133. result += 1
  134. incrementalString = String(format: "%04ld", result)
  135. }
  136. keychain["incrementalnumber"] = incrementalString
  137. return incrementalString
  138. }
  139. @objc var showHiddenFiles: Bool {
  140. get {
  141. migrate(key: "showHiddenFiles")
  142. if let value = try? keychain.get("showHiddenFiles"), let result = Bool(value) {
  143. return result
  144. }
  145. return false
  146. }
  147. set {
  148. keychain["showHiddenFiles"] = String(newValue)
  149. }
  150. }
  151. @objc var formatCompatibility: Bool {
  152. get {
  153. migrate(key: "formatCompatibility")
  154. if let value = try? keychain.get("formatCompatibility"), let result = Bool(value) {
  155. return result
  156. }
  157. return true
  158. }
  159. set {
  160. keychain["formatCompatibility"] = String(newValue)
  161. }
  162. }
  163. @objc var disableFilesApp: Bool {
  164. get {
  165. migrate(key: "disablefilesapp")
  166. if let value = try? keychain.get("disablefilesapp"), let result = Bool(value) {
  167. return result
  168. }
  169. return false
  170. }
  171. set {
  172. keychain["disablefilesapp"] = String(newValue)
  173. }
  174. }
  175. @objc var livePhoto: Bool {
  176. get {
  177. migrate(key: "livePhoto")
  178. if let value = try? keychain.get("livePhoto"), let result = Bool(value) {
  179. return result
  180. }
  181. return true
  182. }
  183. set {
  184. keychain["livePhoto"] = String(newValue)
  185. }
  186. }
  187. @objc var disableCrashservice: Bool {
  188. get {
  189. migrate(key: "crashservice")
  190. if let value = try? keychain.get("crashservice"), let result = Bool(value) {
  191. return result
  192. }
  193. return false
  194. }
  195. set {
  196. keychain["crashservice"] = String(newValue)
  197. }
  198. }
  199. @objc var logLevel: Int {
  200. get {
  201. migrate(key: "logLevel")
  202. if let value = try? keychain.get("logLevel"), let result = Int(value) {
  203. return result
  204. }
  205. return 1
  206. }
  207. set {
  208. keychain["logLevel"] = String(newValue)
  209. }
  210. }
  211. @objc var accountRequest: Bool {
  212. get {
  213. migrate(key: "accountRequest")
  214. if let value = try? keychain.get("accountRequest"), let result = Bool(value) {
  215. return result
  216. }
  217. return false
  218. }
  219. set {
  220. keychain["accountRequest"] = String(newValue)
  221. }
  222. }
  223. @objc var removePhotoCameraRoll: Bool {
  224. get {
  225. migrate(key: "removePhotoCameraRoll")
  226. if let value = try? keychain.get("removePhotoCameraRoll"), let result = Bool(value) {
  227. return result
  228. }
  229. return false
  230. }
  231. set {
  232. keychain["removePhotoCameraRoll"] = String(newValue)
  233. }
  234. }
  235. @objc var privacyScreenEnabled: Bool {
  236. get {
  237. migrate(key: "privacyScreen")
  238. if let value = try? keychain.get("privacyScreen"), let result = Bool(value) {
  239. return result
  240. }
  241. return false
  242. }
  243. set {
  244. keychain["privacyScreen"] = String(newValue)
  245. }
  246. }
  247. @objc var cleanUpDay: Int {
  248. get {
  249. migrate(key: "cleanUpDay")
  250. if let value = try? keychain.get("cleanUpDay"), let result = Int(value) {
  251. return result
  252. }
  253. return NCBrandOptions.shared.cleanUpDay
  254. }
  255. set {
  256. keychain["cleanUpDay"] = String(newValue)
  257. }
  258. }
  259. var mediaColumnCount: Int {
  260. get {
  261. if let value = try? keychain.get("mediaColumnCount"), let result = Int(value) {
  262. return result
  263. }
  264. return 3
  265. }
  266. set {
  267. keychain["mediaColumnCount"] = String(newValue)
  268. }
  269. }
  270. var mediaTypeLayout: String {
  271. get {
  272. if let value = try? keychain.get("mediaTypeLayout") {
  273. return value
  274. }
  275. return NCGlobal.shared.mediaLayoutRatio
  276. }
  277. set {
  278. keychain["mediaTypeLayout"] = String(newValue)
  279. }
  280. }
  281. var textRecognitionStatus: Bool {
  282. get {
  283. migrate(key: "textRecognitionStatus")
  284. if let value = try? keychain.get("textRecognitionStatus"), let result = Bool(value) {
  285. return result
  286. }
  287. return false
  288. }
  289. set {
  290. keychain["textRecognitionStatus"] = String(newValue)
  291. }
  292. }
  293. var deleteAllScanImages: Bool {
  294. get {
  295. migrate(key: "deleteAllScanImages")
  296. if let value = try? keychain.get("deleteAllScanImages"), let result = Bool(value) {
  297. return result
  298. }
  299. return false
  300. }
  301. set {
  302. keychain["deleteAllScanImages"] = String(newValue)
  303. }
  304. }
  305. var qualityScanDocument: Double {
  306. get {
  307. migrate(key: "qualityScanDocument")
  308. if let value = try? keychain.get("qualityScanDocument"), let result = Double(value) {
  309. return result
  310. }
  311. return 2
  312. }
  313. set {
  314. keychain["qualityScanDocument"] = String(newValue)
  315. }
  316. }
  317. // MARK: -
  318. @objc func getPassword(account: String) -> String {
  319. let key = "password" + account
  320. migrate(key: key)
  321. return (try? keychain.get(key)) ?? ""
  322. }
  323. @objc func setPassword(account: String, password: String?) {
  324. let key = "password" + account
  325. keychain[key] = password
  326. }
  327. @objc func getOriginalFileName(key: String) -> Bool {
  328. migrate(key: key)
  329. if let value = try? keychain.get(key), let result = Bool(value) {
  330. return result
  331. }
  332. return false
  333. }
  334. @objc func setOriginalFileName(key: String, value: Bool) {
  335. keychain[key] = String(value)
  336. }
  337. @objc func getFileNameMask(key: String) -> String {
  338. migrate(key: key)
  339. if let value = try? keychain.get(key) {
  340. return value
  341. } else {
  342. return ""
  343. }
  344. }
  345. @objc func setFileNameMask(key: String, mask: String?) {
  346. keychain[key] = mask
  347. }
  348. @objc func getFileNameType(key: String) -> Bool {
  349. migrate(key: key)
  350. if let value = try? keychain.get(key), let result = Bool(value) {
  351. return result
  352. } else {
  353. return false
  354. }
  355. }
  356. @objc func setFileNameType(key: String, prefix: Bool) {
  357. keychain[key] = String(prefix)
  358. }
  359. // MARK: - E2EE
  360. func getEndToEndCertificate(account: String) -> String? {
  361. let key = "EndToEndCertificate_" + account
  362. migrate(key: key)
  363. return try? keychain.get(key)
  364. }
  365. func setEndToEndCertificate(account: String, certificate: String?) {
  366. let key = "EndToEndCertificate_" + account
  367. keychain[key] = certificate
  368. }
  369. func getEndToEndPrivateKey(account: String) -> String? {
  370. let key = "EndToEndPrivateKey_" + account
  371. migrate(key: key)
  372. return try? keychain.get(key)
  373. }
  374. func setEndToEndPrivateKey(account: String, privateKey: String?) {
  375. let key = "EndToEndPrivateKey_" + account
  376. keychain[key] = privateKey
  377. }
  378. func getEndToEndPublicKey(account: String) -> String? {
  379. let key = "EndToEndPublicKeyServer_" + account
  380. migrate(key: key)
  381. return try? keychain.get(key)
  382. }
  383. func setEndToEndPublicKey(account: String, publicKey: String?) {
  384. let key = "EndToEndPublicKeyServer_" + account
  385. keychain[key] = publicKey
  386. }
  387. func getEndToEndPassphrase(account: String) -> String? {
  388. let key = "EndToEndPassphrase_" + account
  389. migrate(key: key)
  390. return try? keychain.get(key)
  391. }
  392. func setEndToEndPassphrase(account: String, passphrase: String?) {
  393. let key = "EndToEndPassphrase_" + account
  394. keychain[key] = passphrase
  395. }
  396. func isEndToEndEnabled(account: String) -> Bool {
  397. guard let certificate = getEndToEndCertificate(account: account), !certificate.isEmpty,
  398. let publicKey = getEndToEndPublicKey(account: account), !publicKey.isEmpty,
  399. let privateKey = getEndToEndPrivateKey(account: account), !privateKey.isEmpty,
  400. let passphrase = getEndToEndPassphrase(account: account), !passphrase.isEmpty,
  401. NCGlobal.shared.e2eeVersions.contains(NCGlobal.shared.capabilityE2EEApiVersion) else { return false }
  402. return true
  403. }
  404. func clearAllKeysEndToEnd(account: String) {
  405. setEndToEndCertificate(account: account, certificate: nil)
  406. setEndToEndPrivateKey(account: account, privateKey: nil)
  407. setEndToEndPublicKey(account: account, publicKey: nil)
  408. setEndToEndPassphrase(account: account, passphrase: nil)
  409. }
  410. // MARK: - PUSHNOTIFICATION
  411. @objc func getPushNotificationPublicKey(account: String) -> Data? {
  412. let key = "PNPublicKey" + account
  413. return try? keychain.getData(key)
  414. }
  415. @objc func setPushNotificationPublicKey(account: String, data: Data?) {
  416. let key = "PNPublicKey" + account
  417. keychain[data: key] = data
  418. }
  419. @objc func getPushNotificationPrivateKey(account: String) -> Data? {
  420. let key = "PNPrivateKey" + account
  421. return try? keychain.getData(key)
  422. }
  423. @objc func setPushNotificationPrivateKey(account: String, data: Data?) {
  424. let key = "PNPrivateKey" + account
  425. keychain[data: key] = data
  426. }
  427. @objc func getPushNotificationSubscribingPublicKey(account: String) -> String? {
  428. let key = "PNSubscribingPublicKey" + account
  429. return try? keychain.get(key)
  430. }
  431. @objc func setPushNotificationSubscribingPublicKey(account: String, publicKey: String?) {
  432. let key = "PNSubscribingPublicKey" + account
  433. keychain[key] = publicKey
  434. }
  435. @objc func getPushNotificationToken(account: String) -> String? {
  436. let key = "PNToken" + account
  437. return try? keychain.get(key)
  438. }
  439. @objc func setPushNotificationToken(account: String, token: String?) {
  440. let key = "PNToken" + account
  441. keychain[key] = token
  442. }
  443. @objc func getPushNotificationDeviceIdentifier(account: String) -> String? {
  444. let key = "PNDeviceIdentifier" + account
  445. return try? keychain.get(key)
  446. }
  447. @objc func setPushNotificationDeviceIdentifier(account: String, deviceIdentifier: String?) {
  448. let key = "PNDeviceIdentifier" + account
  449. keychain[key] = deviceIdentifier
  450. }
  451. @objc func getPushNotificationDeviceIdentifierSignature(account: String) -> String? {
  452. let key = "PNDeviceIdentifierSignature" + account
  453. return try? keychain.get(key)
  454. }
  455. @objc func setPushNotificationDeviceIdentifierSignature(account: String, deviceIdentifierSignature: String?) {
  456. let key = "PNDeviceIdentifierSignature" + account
  457. keychain[key] = deviceIdentifierSignature
  458. }
  459. @objc func clearAllKeysPushNotification(account: String) {
  460. setPushNotificationPublicKey(account: account, data: nil)
  461. setPushNotificationSubscribingPublicKey(account: account, publicKey: nil)
  462. setPushNotificationPrivateKey(account: account, data: nil)
  463. setPushNotificationToken(account: account, token: nil)
  464. setPushNotificationDeviceIdentifier(account: account, deviceIdentifier: nil)
  465. setPushNotificationDeviceIdentifierSignature(account: account, deviceIdentifierSignature: nil)
  466. }
  467. // MARK: -
  468. private func migrate(key: String) {
  469. let keychainOLD = Keychain(service: "Crypto Cloud")
  470. if let value = keychainOLD[key], !value.isEmpty {
  471. keychain[key] = value
  472. keychainOLD[key] = nil
  473. }
  474. }
  475. @objc func removeAll() {
  476. try? keychain.removeAll()
  477. }
  478. }