NCKeychain.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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: NCGlobal.shared.serviceShareKeyChain)
  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: NCGlobal.shared.serviceShareKeyChain)
  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. @objc func getPassword(account: String) -> String {
  297. let key = "password" + account
  298. migrate(key: key)
  299. return (try? keychain.get(key)) ?? ""
  300. }
  301. @objc func setPassword(account: String, password: String?) {
  302. let key = "password" + account
  303. keychain[key] = password
  304. }
  305. @objc func getOriginalFileName(key: String) -> Bool {
  306. migrate(key: key)
  307. if let value = try? keychain.get(key), let result = Bool(value) {
  308. return result
  309. }
  310. return false
  311. }
  312. @objc func setOriginalFileName(key: String, value: Bool) {
  313. keychain[key] = String(value)
  314. }
  315. @objc func getFileNameMask(key: String) -> String {
  316. migrate(key: key)
  317. if let value = try? keychain.get(key) {
  318. return value
  319. } else {
  320. return ""
  321. }
  322. }
  323. @objc func setFileNameMask(key: String, mask: String?) {
  324. keychain[key] = mask
  325. }
  326. @objc func getFileNameType(key: String) -> Bool {
  327. migrate(key: key)
  328. if let value = try? keychain.get(key), let result = Bool(value) {
  329. return result
  330. } else {
  331. return false
  332. }
  333. }
  334. @objc func setFileNameType(key: String, prefix: Bool) {
  335. keychain[key] = String(prefix)
  336. }
  337. // MARK: - E2EE
  338. func getEndToEndCertificate(account: String) -> String? {
  339. let key = "EndToEndCertificate_" + account
  340. migrate(key: key)
  341. return try? keychain.get(key)
  342. }
  343. func setEndToEndCertificate(account: String, certificate: String?) {
  344. let key = "EndToEndCertificate_" + account
  345. keychain[key] = certificate
  346. }
  347. func getEndToEndPrivateKey(account: String) -> String? {
  348. let key = "EndToEndPrivateKey_" + account
  349. migrate(key: key)
  350. return try? keychain.get(key)
  351. }
  352. func setEndToEndPrivateKey(account: String, privateKey: String?) {
  353. let key = "EndToEndPrivateKey_" + account
  354. keychain[key] = privateKey
  355. }
  356. func getEndToEndPublicKey(account: String) -> String? {
  357. let key = "EndToEndPublicKeyServer_" + account
  358. migrate(key: key)
  359. return try? keychain.get(key)
  360. }
  361. func setEndToEndPublicKey(account: String, publicKey: String?) {
  362. let key = "EndToEndPublicKeyServer_" + account
  363. keychain[key] = publicKey
  364. }
  365. func getEndToEndPassphrase(account: String) -> String? {
  366. let key = "EndToEndPassphrase_" + account
  367. migrate(key: key)
  368. return try? keychain.get(key)
  369. }
  370. func setEndToEndPassphrase(account: String, passphrase: String?) {
  371. let key = "EndToEndPassphrase_" + account
  372. keychain[key] = passphrase
  373. }
  374. func isEndToEndEnabled(account: String) -> Bool {
  375. guard let certificate = getEndToEndCertificate(account: account), !certificate.isEmpty,
  376. let publicKey = getEndToEndPublicKey(account: account), !publicKey.isEmpty,
  377. let privateKey = getEndToEndPrivateKey(account: account), !privateKey.isEmpty,
  378. let passphrase = getEndToEndPassphrase(account: account), !passphrase.isEmpty,
  379. NCGlobal.shared.e2eeVersions.contains(NCGlobal.shared.capabilityE2EEApiVersion) else { return false }
  380. return true
  381. }
  382. func clearAllKeysEndToEnd(account: String) {
  383. setEndToEndCertificate(account: account, certificate: nil)
  384. setEndToEndPrivateKey(account: account, privateKey: nil)
  385. setEndToEndPublicKey(account: account, publicKey: nil)
  386. setEndToEndPassphrase(account: account, passphrase: nil)
  387. }
  388. // MARK: - PUSHNOTIFICATION
  389. @objc func getPushNotificationPublicKey(account: String) -> Data? {
  390. let key = "PNPublicKey" + account
  391. migrate(key: key)
  392. return try? keychain.getData(key)
  393. }
  394. @objc func setPushNotificationPublicKey(account: String, data: Data?) {
  395. let key = "PNPublicKey" + account
  396. keychain[data: key] = data
  397. }
  398. @objc func getPushNotificationPrivateKey(account: String) -> Data? {
  399. let key = "PNPrivateKey" + account
  400. migrate(key: key)
  401. return try? keychain.getData(key)
  402. }
  403. @objc func setPushNotificationPrivateKey(account: String, data: Data?) {
  404. let key = "PNPrivateKey" + account
  405. keychain[data: key] = data
  406. }
  407. @objc func getPushNotificationSubscribingPublicKey(account: String) -> String? {
  408. let key = "PNSubscribingPublicKey" + account
  409. migrate(key: key)
  410. return try? keychain.get(key)
  411. }
  412. @objc func setPushNotificationSubscribingPublicKey(account: String, publicKey: String?) {
  413. let key = "PNSubscribingPublicKey" + account
  414. keychain[key] = publicKey
  415. }
  416. @objc func getPushNotificationToken(account: String) -> String? {
  417. let key = "PNToken" + account
  418. migrate(key: key)
  419. return try? keychain.get(key)
  420. }
  421. @objc func setPushNotificationToken(account: String, token: String?) {
  422. let key = "PNToken" + account
  423. keychain[key] = token
  424. }
  425. @objc func getPushNotificationDeviceIdentifier(account: String) -> String? {
  426. let key = "PNDeviceIdentifier" + account
  427. migrate(key: key)
  428. return try? keychain.get(key)
  429. }
  430. @objc func setPushNotificationDeviceIdentifier(account: String, deviceIdentifier: String?) {
  431. let key = "PNDeviceIdentifier" + account
  432. keychain[key] = deviceIdentifier
  433. }
  434. @objc func getPushNotificationDeviceIdentifierSignature(account: String) -> String? {
  435. let key = "PNDeviceIdentifierSignature" + account
  436. migrate(key: key)
  437. return try? keychain.get(key)
  438. }
  439. @objc func setPushNotificationDeviceIdentifierSignature(account: String, deviceIdentifierSignature: String?) {
  440. let key = "PNDeviceIdentifierSignature" + account
  441. keychain[key] = deviceIdentifierSignature
  442. }
  443. @objc func clearAllKeysPushNotification(account: String) {
  444. setPushNotificationPublicKey(account: account, data: nil)
  445. setPushNotificationSubscribingPublicKey(account: account, publicKey: nil)
  446. setPushNotificationPrivateKey(account: account, data: nil)
  447. setPushNotificationToken(account: account, token: nil)
  448. setPushNotificationDeviceIdentifier(account: account, deviceIdentifier: nil)
  449. setPushNotificationDeviceIdentifierSignature(account: account, deviceIdentifierSignature: nil)
  450. }
  451. }