NCKeychain.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 false
  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. // MARK: -
  139. private func migrate(key: String) {
  140. let keychainOLD = Keychain(service: NCGlobal.shared.serviceShareKeyChain)
  141. if let value = keychainOLD[key], !value.isEmpty {
  142. keychain[key] = value
  143. keychainOLD[key] = nil
  144. }
  145. }
  146. @objc func removeAll() {
  147. try? keychain.removeAll()
  148. }
  149. @objc func getPassword(account: String) -> String {
  150. let key = "password" + account
  151. migrate(key: key)
  152. return (try? keychain.get(key)) ?? ""
  153. }
  154. @objc func getOriginalFileName(key: String) -> Bool {
  155. migrate(key: key)
  156. if let value = try? keychain.get(key), let result = Bool(value) {
  157. return result
  158. }
  159. return false
  160. }
  161. @objc func setOriginalFileName(key: String, value: Bool) {
  162. keychain[key] = String(value)
  163. }
  164. @objc func getFileNameMask(key: String) -> String {
  165. migrate(key: key)
  166. if let value = try? keychain.get(key) {
  167. return value
  168. } else {
  169. return ""
  170. }
  171. }
  172. @objc func setFileNameMask(key: String, mask: String?) {
  173. keychain[key] = mask
  174. }
  175. @objc func getFileNameType(key: String) -> Bool {
  176. migrate(key: key)
  177. if let value = try? keychain.get(key), let result = Bool(value) {
  178. return result
  179. } else {
  180. return false
  181. }
  182. }
  183. @objc func setFileNameType(key: String, prefix: Bool) {
  184. keychain[key] = String(prefix)
  185. }
  186. // MARK: - E2EE
  187. func getEndToEndCertificate(account: String) -> String? {
  188. let key = E2E_certificate + account
  189. migrate(key: key)
  190. return try? keychain.get(key)
  191. }
  192. func setEndToEndCertificate(account: String, certificate: String?) {
  193. let key = E2E_certificate + account
  194. keychain[key] = certificate
  195. }
  196. func getEndToEndPrivateKey(account: String) -> String? {
  197. let key = E2E_PrivateKey + account
  198. migrate(key: key)
  199. return try? keychain.get(key)
  200. }
  201. func setEndToEndPrivateKey(account: String, privateKey: String?) {
  202. let key = E2E_PrivateKey + account
  203. keychain[key] = privateKey
  204. }
  205. func getEndToEndPublicKey(account: String) -> String? {
  206. let key = E2E_PublicKey + account
  207. migrate(key: key)
  208. return try? keychain.get(key)
  209. }
  210. func setEndToEndPublicKey(account: String, publicKey: String?) {
  211. let key = E2E_PublicKey + account
  212. keychain[key] = publicKey
  213. }
  214. func getEndToEndPassphrase(account: String) -> String? {
  215. let key = E2E_Passphrase + account
  216. migrate(key: key)
  217. return try? keychain.get(key)
  218. }
  219. func setEndToEndPassphrase(account: String, passphrase: String?) {
  220. let key = E2E_Passphrase + account
  221. keychain[key] = passphrase
  222. }
  223. func isEndToEndEnabled(account: String) -> Bool {
  224. guard let certificate = getEndToEndCertificate(account: account), !certificate.isEmpty,
  225. let publicKey = getEndToEndPublicKey(account: account), !publicKey.isEmpty,
  226. let privateKey = getEndToEndPrivateKey(account: account), !privateKey.isEmpty,
  227. let passphrase = getEndToEndPassphrase(account: account), !passphrase.isEmpty,
  228. NCGlobal.shared.e2eeVersions.contains(NCGlobal.shared.capabilityE2EEApiVersion) else { return false }
  229. return true
  230. }
  231. func clearAllKeysEndToEnd(account: String) {
  232. setEndToEndCertificate(account: account, certificate: nil)
  233. setEndToEndPrivateKey(account: account, privateKey: nil)
  234. setEndToEndPublicKey(account: account, publicKey: nil)
  235. setEndToEndPassphrase(account: account, passphrase: nil)
  236. }
  237. }