NCKeychain.swift 15 KB

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