NCKeychain.swift 15 KB

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