NCKeychain.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // swiftlint:disable identifier_name
  28. let E2E_certificate = "EndToEndCertificate_"
  29. let E2E_PrivateKey = "EndToEndPrivateKey_"
  30. let E2E_Passphrase = "EndToEndPassphrase_"
  31. let E2E_PublicKey = "EndToEndPublicKeyServer_"
  32. // swiftlint:enable identifier_name
  33. override init() {
  34. super.init()
  35. }
  36. var typeFilterScanDocument: NCGlobal.TypeFilterScanDocument {
  37. get {
  38. if let rawValue = try? keychain.get("ScanDocumentTypeFilter"), let value = NCGlobal.TypeFilterScanDocument(rawValue: rawValue) {
  39. return value
  40. } else {
  41. return .original
  42. }
  43. }
  44. set {
  45. keychain["ScanDocumentTypeFilter"] = newValue.rawValue
  46. }
  47. }
  48. @objc var passcode: String? {
  49. get {
  50. migrate(key: "passcodeBlock")
  51. if let value = try? keychain.get("passcodeBlock") {
  52. return value
  53. }
  54. return nil
  55. }
  56. set {
  57. keychain["passcodeBlock"] = newValue
  58. }
  59. }
  60. @objc var requestPasscodeAtStart: Bool {
  61. get {
  62. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  63. if let value = keychainOLD["notPasscodeAtStart"], !value.isEmpty {
  64. if value == "true" {
  65. keychain["requestPasscodeAtStart"] = "false"
  66. } else if value == "false" {
  67. keychain["requestPasscodeAtStart"] = "true"
  68. }
  69. keychainOLD["notPasscodeAtStart"] = nil
  70. }
  71. if let value = try? keychain.get("requestPasscodeAtStart"), let result = Bool(value) {
  72. return result
  73. }
  74. return true
  75. }
  76. set {
  77. keychain["requestPasscodeAtStart"] = String(newValue)
  78. }
  79. }
  80. @objc var touchFaceID: Bool {
  81. get {
  82. migrate(key: "enableTouchFaceID")
  83. if let value = try? keychain.get("enableTouchFaceID"), let result = Bool(value) {
  84. return result
  85. }
  86. return false
  87. }
  88. set {
  89. keychain["enableTouchFaceID"] = String(newValue)
  90. }
  91. }
  92. var intro: Bool {
  93. get {
  94. migrate(key: "intro")
  95. if let value = try? keychain.get("intro"), let result = Bool(value) {
  96. return result
  97. }
  98. return false
  99. }
  100. set {
  101. keychain["intro"] = String(newValue)
  102. }
  103. }
  104. @objc var incrementalNumber: String {
  105. migrate(key: "incrementalnumber")
  106. var incrementalString = String(format: "%04ld", 0)
  107. if let value = try? keychain.get("incrementalnumber"), var result = Int(value) {
  108. result += 1
  109. incrementalString = String(format: "%04ld", result)
  110. }
  111. keychain["incrementalnumber"] = incrementalString
  112. return incrementalString
  113. }
  114. @objc var showHiddenFiles: Bool {
  115. get {
  116. migrate(key: "showHiddenFiles")
  117. if let value = try? keychain.get("showHiddenFiles"), let result = Bool(value) {
  118. return result
  119. }
  120. return false
  121. }
  122. set {
  123. keychain["showHiddenFiles"] = String(newValue)
  124. }
  125. }
  126. @objc var formatCompatibility: Bool {
  127. get {
  128. migrate(key: "formatCompatibility")
  129. if let value = try? keychain.get("formatCompatibility"), let result = Bool(value) {
  130. return result
  131. }
  132. return true
  133. }
  134. set {
  135. keychain["formatCompatibility"] = String(newValue)
  136. }
  137. }
  138. @objc var disableFilesApp: Bool {
  139. get {
  140. migrate(key: "disablefilesapp")
  141. if let value = try? keychain.get("disablefilesapp"), let result = Bool(value) {
  142. return result
  143. }
  144. return false
  145. }
  146. set {
  147. keychain["disablefilesapp"] = String(newValue)
  148. }
  149. }
  150. // MARK: -
  151. private func migrate(key: String) {
  152. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  153. if let value = keychainOLD[key], !value.isEmpty {
  154. keychain[key] = value
  155. keychainOLD[key] = nil
  156. }
  157. }
  158. @objc func removeAll() {
  159. try? keychain.removeAll()
  160. }
  161. @objc func getPassword(account: String) -> String {
  162. let key = "password" + account
  163. migrate(key: key)
  164. return (try? keychain.get(key)) ?? ""
  165. }
  166. @objc func setPassword(account: String, password: String?) {
  167. let key = "password" + account
  168. keychain[key] = password
  169. }
  170. @objc func getOriginalFileName(key: String) -> Bool {
  171. migrate(key: key)
  172. if let value = try? keychain.get(key), let result = Bool(value) {
  173. return result
  174. }
  175. return false
  176. }
  177. @objc func setOriginalFileName(key: String, value: Bool) {
  178. keychain[key] = String(value)
  179. }
  180. @objc func getFileNameMask(key: String) -> String {
  181. migrate(key: key)
  182. if let value = try? keychain.get(key) {
  183. return value
  184. } else {
  185. return ""
  186. }
  187. }
  188. @objc func setFileNameMask(key: String, mask: String?) {
  189. keychain[key] = mask
  190. }
  191. @objc func getFileNameType(key: String) -> Bool {
  192. migrate(key: key)
  193. if let value = try? keychain.get(key), let result = Bool(value) {
  194. return result
  195. } else {
  196. return false
  197. }
  198. }
  199. @objc func setFileNameType(key: String, prefix: Bool) {
  200. keychain[key] = String(prefix)
  201. }
  202. // MARK: - E2EE
  203. func getEndToEndCertificate(account: String) -> String? {
  204. let key = E2E_certificate + account
  205. migrate(key: key)
  206. return try? keychain.get(key)
  207. }
  208. func setEndToEndCertificate(account: String, certificate: String?) {
  209. let key = E2E_certificate + account
  210. keychain[key] = certificate
  211. }
  212. func getEndToEndPrivateKey(account: String) -> String? {
  213. let key = E2E_PrivateKey + account
  214. migrate(key: key)
  215. return try? keychain.get(key)
  216. }
  217. func setEndToEndPrivateKey(account: String, privateKey: String?) {
  218. let key = E2E_PrivateKey + account
  219. keychain[key] = privateKey
  220. }
  221. func getEndToEndPublicKey(account: String) -> String? {
  222. let key = E2E_PublicKey + account
  223. migrate(key: key)
  224. return try? keychain.get(key)
  225. }
  226. func setEndToEndPublicKey(account: String, publicKey: String?) {
  227. let key = E2E_PublicKey + account
  228. keychain[key] = publicKey
  229. }
  230. func getEndToEndPassphrase(account: String) -> String? {
  231. let key = E2E_Passphrase + account
  232. migrate(key: key)
  233. return try? keychain.get(key)
  234. }
  235. func setEndToEndPassphrase(account: String, passphrase: String?) {
  236. let key = E2E_Passphrase + account
  237. keychain[key] = passphrase
  238. }
  239. func isEndToEndEnabled(account: String) -> Bool {
  240. guard let certificate = getEndToEndCertificate(account: account), !certificate.isEmpty,
  241. let publicKey = getEndToEndPublicKey(account: account), !publicKey.isEmpty,
  242. let privateKey = getEndToEndPrivateKey(account: account), !privateKey.isEmpty,
  243. let passphrase = getEndToEndPassphrase(account: account), !passphrase.isEmpty,
  244. NCGlobal.shared.e2eeVersions.contains(NCGlobal.shared.capabilityE2EEApiVersion) else { return false }
  245. return true
  246. }
  247. func clearAllKeysEndToEnd(account: String) {
  248. setEndToEndCertificate(account: account, certificate: nil)
  249. setEndToEndPrivateKey(account: account, privateKey: nil)
  250. setEndToEndPublicKey(account: account, publicKey: nil)
  251. setEndToEndPassphrase(account: account, passphrase: nil)
  252. }
  253. }