NCKeychain.swift 16 KB

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