NCKeychain.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 UIKit
  25. import KeychainAccess
  26. @objc class NCKeychain: NSObject {
  27. let keychain = Keychain(service: "com.nextcloud.keychain")
  28. var showDescription: Bool {
  29. get {
  30. if let value = try? keychain.get("showDescription"), let result = Bool(value) {
  31. return result
  32. }
  33. return true
  34. }
  35. set {
  36. keychain["showDescription"] = String(newValue)
  37. }
  38. }
  39. var typeFilterScanDocument: NCGlobal.TypeFilterScanDocument {
  40. get {
  41. if let rawValue = try? keychain.get("ScanDocumentTypeFilter"), let value = NCGlobal.TypeFilterScanDocument(rawValue: rawValue) {
  42. return value
  43. } else {
  44. return .original
  45. }
  46. }
  47. set {
  48. keychain["ScanDocumentTypeFilter"] = newValue.rawValue
  49. }
  50. }
  51. @objc var passcode: String? {
  52. get {
  53. migrate(key: "passcodeBlock")
  54. if let value = try? keychain.get("passcodeBlock"), !value.isEmpty {
  55. return value
  56. }
  57. return nil
  58. }
  59. set {
  60. keychain["passcodeBlock"] = newValue
  61. }
  62. }
  63. var resetAppCounterFail: Bool {
  64. get {
  65. if let value = try? keychain.get("resetAppCounterFail"), let result = Bool(value) {
  66. return result
  67. }
  68. return false
  69. }
  70. set {
  71. keychain["resetAppCounterFail"] = String(newValue)
  72. }
  73. }
  74. var passcodeCounterFail: Int {
  75. get {
  76. if let value = try? keychain.get("passcodeCounterFail"), let result = Int(value) {
  77. return result
  78. }
  79. return 0
  80. }
  81. set {
  82. keychain["passcodeCounterFail"] = String(newValue)
  83. }
  84. }
  85. var passcodeCounterFailReset: Int {
  86. get {
  87. if let value = try? keychain.get("passcodeCounterFailReset"), let result = Int(value) {
  88. return result
  89. }
  90. return 0
  91. }
  92. set {
  93. keychain["passcodeCounterFailReset"] = String(newValue)
  94. }
  95. }
  96. var requestPasscodeAtStart: Bool {
  97. get {
  98. let keychainOLD = Keychain(service: "Crypto Cloud")
  99. if let value = keychainOLD["notPasscodeAtStart"], !value.isEmpty {
  100. if value == "true" {
  101. keychain["requestPasscodeAtStart"] = "false"
  102. } else if value == "false" {
  103. keychain["requestPasscodeAtStart"] = "true"
  104. }
  105. keychainOLD["notPasscodeAtStart"] = nil
  106. }
  107. if NCBrandOptions.shared.doNotAskPasscodeAtStartup {
  108. return false
  109. } else if let value = try? keychain.get("requestPasscodeAtStart"), let result = Bool(value) {
  110. return result
  111. }
  112. return true
  113. }
  114. set {
  115. keychain["requestPasscodeAtStart"] = String(newValue)
  116. }
  117. }
  118. var touchFaceID: Bool {
  119. get {
  120. migrate(key: "enableTouchFaceID")
  121. if let value = try? keychain.get("enableTouchFaceID"), let result = Bool(value) {
  122. return result
  123. }
  124. return false
  125. }
  126. set {
  127. keychain["enableTouchFaceID"] = String(newValue)
  128. }
  129. }
  130. var presentPasscode: Bool {
  131. return passcode != nil && requestPasscodeAtStart
  132. }
  133. var incrementalNumber: String {
  134. migrate(key: "incrementalnumber")
  135. var incrementalString = String(format: "%04ld", 0)
  136. if let value = try? keychain.get("incrementalnumber"), var result = Int(value) {
  137. result += 1
  138. incrementalString = String(format: "%04ld", result)
  139. }
  140. keychain["incrementalnumber"] = incrementalString
  141. return incrementalString
  142. }
  143. var showHiddenFiles: Bool {
  144. get {
  145. migrate(key: "showHiddenFiles")
  146. if let value = try? keychain.get("showHiddenFiles"), let result = Bool(value) {
  147. return result
  148. }
  149. return false
  150. }
  151. set {
  152. keychain["showHiddenFiles"] = String(newValue)
  153. }
  154. }
  155. var formatCompatibility: Bool {
  156. get {
  157. migrate(key: "formatCompatibility")
  158. if let value = try? keychain.get("formatCompatibility"), let result = Bool(value) {
  159. return result
  160. }
  161. return true
  162. }
  163. set {
  164. keychain["formatCompatibility"] = String(newValue)
  165. }
  166. }
  167. var disableFilesApp: Bool {
  168. get {
  169. migrate(key: "disablefilesapp")
  170. if let value = try? keychain.get("disablefilesapp"), let result = Bool(value) {
  171. return result
  172. }
  173. return false
  174. }
  175. set {
  176. keychain["disablefilesapp"] = String(newValue)
  177. }
  178. }
  179. var livePhoto: Bool {
  180. get {
  181. migrate(key: "livePhoto")
  182. if let value = try? keychain.get("livePhoto"), let result = Bool(value) {
  183. return result
  184. }
  185. return true
  186. }
  187. set {
  188. keychain["livePhoto"] = String(newValue)
  189. }
  190. }
  191. var disableCrashservice: Bool {
  192. get {
  193. migrate(key: "crashservice")
  194. if let value = try? keychain.get("crashservice"), let result = Bool(value) {
  195. return result
  196. }
  197. return false
  198. }
  199. set {
  200. keychain["crashservice"] = String(newValue)
  201. }
  202. }
  203. var logLevel: Int {
  204. get {
  205. migrate(key: "logLevel")
  206. if let value = try? keychain.get("logLevel"), let result = Int(value) {
  207. return result
  208. }
  209. return 1
  210. }
  211. set {
  212. keychain["logLevel"] = String(newValue)
  213. }
  214. }
  215. var accountRequest: Bool {
  216. get {
  217. migrate(key: "accountRequest")
  218. if let value = try? keychain.get("accountRequest"), let result = Bool(value) {
  219. return result
  220. }
  221. return false
  222. }
  223. set {
  224. keychain["accountRequest"] = String(newValue)
  225. }
  226. }
  227. var removePhotoCameraRoll: Bool {
  228. get {
  229. migrate(key: "removePhotoCameraRoll")
  230. if let value = try? keychain.get("removePhotoCameraRoll"), let result = Bool(value) {
  231. return result
  232. }
  233. return false
  234. }
  235. set {
  236. keychain["removePhotoCameraRoll"] = String(newValue)
  237. }
  238. }
  239. var privacyScreenEnabled: Bool {
  240. get {
  241. migrate(key: "privacyScreen")
  242. if let value = try? keychain.get("privacyScreen"), let result = Bool(value) {
  243. return result
  244. }
  245. return false
  246. }
  247. set {
  248. keychain["privacyScreen"] = String(newValue)
  249. }
  250. }
  251. var cleanUpDay: Int {
  252. get {
  253. migrate(key: "cleanUpDay")
  254. if let value = try? keychain.get("cleanUpDay"), let result = Int(value) {
  255. return result
  256. }
  257. return NCBrandOptions.shared.cleanUpDay
  258. }
  259. set {
  260. keychain["cleanUpDay"] = String(newValue)
  261. }
  262. }
  263. var mediaColumnCount: Int {
  264. get {
  265. if let value = try? keychain.get("mediaColumnCount"), let result = Int(value) {
  266. return result
  267. }
  268. return 3
  269. }
  270. set {
  271. keychain["mediaColumnCount"] = String(newValue)
  272. }
  273. }
  274. var mediaTypeLayout: String {
  275. get {
  276. if let value = try? keychain.get("mediaTypeLayout") {
  277. return value
  278. }
  279. return NCGlobal.shared.mediaLayoutRatio
  280. }
  281. set {
  282. keychain["mediaTypeLayout"] = String(newValue)
  283. }
  284. }
  285. var textRecognitionStatus: Bool {
  286. get {
  287. migrate(key: "textRecognitionStatus")
  288. if let value = try? keychain.get("textRecognitionStatus"), let result = Bool(value) {
  289. return result
  290. }
  291. return false
  292. }
  293. set {
  294. keychain["textRecognitionStatus"] = String(newValue)
  295. }
  296. }
  297. var deleteAllScanImages: Bool {
  298. get {
  299. migrate(key: "deleteAllScanImages")
  300. if let value = try? keychain.get("deleteAllScanImages"), let result = Bool(value) {
  301. return result
  302. }
  303. return false
  304. }
  305. set {
  306. keychain["deleteAllScanImages"] = String(newValue)
  307. }
  308. }
  309. var qualityScanDocument: Double {
  310. get {
  311. migrate(key: "qualityScanDocument")
  312. if let value = try? keychain.get("qualityScanDocument"), let result = Double(value) {
  313. return result
  314. }
  315. return 2
  316. }
  317. set {
  318. keychain["qualityScanDocument"] = String(newValue)
  319. }
  320. }
  321. var appearanceAutomatic: Bool {
  322. get {
  323. if let value = try? keychain.get("appearanceAutomatic"), let result = Bool(value) {
  324. return result
  325. }
  326. return true
  327. }
  328. set {
  329. keychain["appearanceAutomatic"] = String(newValue)
  330. }
  331. }
  332. var appearanceInterfaceStyle: UIUserInterfaceStyle {
  333. get {
  334. if let value = try? keychain.get("appearanceInterfaceStyle") {
  335. if value == "light" {
  336. return .light
  337. } else {
  338. return .dark
  339. }
  340. }
  341. return .light
  342. }
  343. set {
  344. if newValue == .light {
  345. keychain["appearanceInterfaceStyle"] = "light"
  346. } else {
  347. keychain["appearanceInterfaceStyle"] = "dark"
  348. }
  349. }
  350. }
  351. var fileNameType: Bool {
  352. get {
  353. if let value = try? keychain.get("fileNameType"), let result = Bool(value) {
  354. return result
  355. }
  356. return false
  357. }
  358. set {
  359. keychain["fileNameType"] = String(newValue)
  360. }
  361. }
  362. var fileNameOriginal: Bool {
  363. get {
  364. if let value = try? keychain.get("fileNameOriginal"), let result = Bool(value) {
  365. return result
  366. }
  367. return false
  368. }
  369. set {
  370. keychain["fileNameOriginal"] = String(newValue)
  371. }
  372. }
  373. var fileNameMask: String {
  374. get {
  375. if let value = try? keychain.get("fileNameMask") {
  376. return value
  377. }
  378. return ""
  379. }
  380. set {
  381. keychain["fileNameMask"] = String(newValue)
  382. }
  383. }
  384. // MARK: -
  385. @objc func getPassword(account: String) -> String {
  386. let key = "password" + account
  387. migrate(key: key)
  388. return (try? keychain.get(key)) ?? ""
  389. }
  390. func setPassword(account: String, password: String?) {
  391. let key = "password" + account
  392. keychain[key] = password
  393. }
  394. func setPersonalFilesOnly(account: String, value: Bool) {
  395. let key = "personalFilesOnly" + account
  396. keychain[key] = String(value)
  397. }
  398. func getPersonalFilesOnly(account: String) -> Bool {
  399. let key = "personalFilesOnly" + account
  400. if let value = try? keychain.get(key), let result = Bool(value) {
  401. return result
  402. } else {
  403. return false
  404. }
  405. }
  406. // MARK: - E2EE
  407. func getEndToEndCertificate(account: String) -> String? {
  408. let key = "EndToEndCertificate_" + account
  409. migrate(key: key)
  410. return try? keychain.get(key)
  411. }
  412. func setEndToEndCertificate(account: String, certificate: String?) {
  413. let key = "EndToEndCertificate_" + account
  414. keychain[key] = certificate
  415. }
  416. func getEndToEndPrivateKey(account: String) -> String? {
  417. let key = "EndToEndPrivateKey_" + account
  418. migrate(key: key)
  419. return try? keychain.get(key)
  420. }
  421. func setEndToEndPrivateKey(account: String, privateKey: String?) {
  422. let key = "EndToEndPrivateKey_" + account
  423. keychain[key] = privateKey
  424. }
  425. func getEndToEndPublicKey(account: String) -> String? {
  426. let key = "EndToEndPublicKeyServer_" + account
  427. migrate(key: key)
  428. return try? keychain.get(key)
  429. }
  430. func setEndToEndPublicKey(account: String, publicKey: String?) {
  431. let key = "EndToEndPublicKeyServer_" + account
  432. keychain[key] = publicKey
  433. }
  434. func getEndToEndPassphrase(account: String) -> String? {
  435. let key = "EndToEndPassphrase_" + account
  436. migrate(key: key)
  437. return try? keychain.get(key)
  438. }
  439. func setEndToEndPassphrase(account: String, passphrase: String?) {
  440. let key = "EndToEndPassphrase_" + account
  441. keychain[key] = passphrase
  442. }
  443. func isEndToEndEnabled(account: String) -> Bool {
  444. guard let certificate = getEndToEndCertificate(account: account), !certificate.isEmpty,
  445. let publicKey = getEndToEndPublicKey(account: account), !publicKey.isEmpty,
  446. let privateKey = getEndToEndPrivateKey(account: account), !privateKey.isEmpty,
  447. let passphrase = getEndToEndPassphrase(account: account), !passphrase.isEmpty,
  448. NCGlobal.shared.e2eeVersions.contains(NCGlobal.shared.capabilityE2EEApiVersion) else { return false }
  449. return true
  450. }
  451. func clearAllKeysEndToEnd(account: String) {
  452. setEndToEndCertificate(account: account, certificate: nil)
  453. setEndToEndPrivateKey(account: account, privateKey: nil)
  454. setEndToEndPublicKey(account: account, publicKey: nil)
  455. setEndToEndPassphrase(account: account, passphrase: nil)
  456. }
  457. // MARK: - PUSHNOTIFICATION
  458. func getPushNotificationPublicKey(account: String) -> Data? {
  459. let key = "PNPublicKey" + account
  460. return try? keychain.getData(key)
  461. }
  462. @objc func setPushNotificationPublicKey(account: String, data: Data?) {
  463. let key = "PNPublicKey" + account
  464. keychain[data: key] = data
  465. }
  466. func getPushNotificationPrivateKey(account: String) -> Data? {
  467. let key = "PNPrivateKey" + account
  468. return try? keychain.getData(key)
  469. }
  470. @objc func setPushNotificationPrivateKey(account: String, data: Data?) {
  471. let key = "PNPrivateKey" + account
  472. keychain[data: key] = data
  473. }
  474. func getPushNotificationSubscribingPublicKey(account: String) -> String? {
  475. let key = "PNSubscribingPublicKey" + account
  476. return try? keychain.get(key)
  477. }
  478. func setPushNotificationSubscribingPublicKey(account: String, publicKey: String?) {
  479. let key = "PNSubscribingPublicKey" + account
  480. keychain[key] = publicKey
  481. }
  482. func getPushNotificationToken(account: String) -> String? {
  483. let key = "PNToken" + account
  484. return try? keychain.get(key)
  485. }
  486. func setPushNotificationToken(account: String, token: String?) {
  487. let key = "PNToken" + account
  488. keychain[key] = token
  489. }
  490. func getPushNotificationDeviceIdentifier(account: String) -> String? {
  491. let key = "PNDeviceIdentifier" + account
  492. return try? keychain.get(key)
  493. }
  494. func setPushNotificationDeviceIdentifier(account: String, deviceIdentifier: String?) {
  495. let key = "PNDeviceIdentifier" + account
  496. keychain[key] = deviceIdentifier
  497. }
  498. func getPushNotificationDeviceIdentifierSignature(account: String) -> String? {
  499. let key = "PNDeviceIdentifierSignature" + account
  500. return try? keychain.get(key)
  501. }
  502. func setPushNotificationDeviceIdentifierSignature(account: String, deviceIdentifierSignature: String?) {
  503. let key = "PNDeviceIdentifierSignature" + account
  504. keychain[key] = deviceIdentifierSignature
  505. }
  506. func clearAllKeysPushNotification(account: String) {
  507. setPushNotificationPublicKey(account: account, data: nil)
  508. setPushNotificationSubscribingPublicKey(account: account, publicKey: nil)
  509. setPushNotificationPrivateKey(account: account, data: nil)
  510. setPushNotificationToken(account: account, token: nil)
  511. setPushNotificationDeviceIdentifier(account: account, deviceIdentifier: nil)
  512. setPushNotificationDeviceIdentifierSignature(account: account, deviceIdentifierSignature: nil)
  513. }
  514. // MARK: - Certificates
  515. func setClientCertificate(account: String, p12Data: Data?, p12Password: String?) {
  516. var key = "ClientCertificateData" + account
  517. keychain[data: key] = p12Data
  518. key = "ClientCertificatePassword" + account
  519. keychain[key] = p12Password
  520. }
  521. func getClientCertificate(account: String) -> (p12Data: Data?, p12Password: String?) {
  522. var key = "ClientCertificateData" + account
  523. let data = try? keychain.getData(key)
  524. key = "ClientCertificatePassword" + account
  525. let password = keychain[key]
  526. return (data, password)
  527. }
  528. // MARK: -
  529. private func migrate(key: String) {
  530. let keychainOLD = Keychain(service: "Crypto Cloud")
  531. if let value = keychainOLD[key], !value.isEmpty {
  532. keychain[key] = value
  533. keychainOLD[key] = nil
  534. }
  535. }
  536. func removeAll() {
  537. try? keychain.removeAll()
  538. }
  539. }