NCNetworkingE2EE.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // NCNetworkingE2EE.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/05/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import Foundation
  22. import NextcloudKit
  23. class NCNetworkingE2EE: NSObject {
  24. func isInUpload(account: String, serverUrl: String) -> Bool {
  25. let counter = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND (status == %d OR status == %d)", account, serverUrl, NCGlobal.shared.metadataStatusWaitUpload, NCGlobal.shared.metadataStatusUploading)).count
  26. return counter > 0 ? true : false
  27. }
  28. func generateRandomIdentifier() -> String {
  29. var UUID = NSUUID().uuidString
  30. UUID = "E2EE" + UUID.replacingOccurrences(of: "-", with: "")
  31. return UUID
  32. }
  33. func getOptions() -> NKRequestOptions {
  34. let version = NCGlobal.shared.capabilityE2EEApiVersion == "2.0" ? "v2" : "v1"
  35. return NKRequestOptions(version: version)
  36. }
  37. func uploadMetadata(account: String, serverUrl: String, userId: String, addUserId: String? = nil, removeUserId: String? = nil) async -> NKError {
  38. var addCertificate: String?
  39. var method = "POST"
  40. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  41. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: "_e2e_error_")
  42. }
  43. if let addUserId {
  44. let results = await NextcloudKit.shared.getE2EECertificate(user: addUserId, options: NCNetworkingE2EE().getOptions())
  45. if results.error == .success, let certificateUser = results.certificateUser {
  46. addCertificate = certificateUser
  47. } else {
  48. return results.error
  49. }
  50. }
  51. // LOCK
  52. //
  53. let resultsLock = await lock(account: account, serverUrl: serverUrl)
  54. guard resultsLock.error == .success, let e2eToken = resultsLock.e2eToken, let fileId = resultsLock.fileId else {
  55. return resultsLock.error
  56. }
  57. // METHOD
  58. //
  59. let resultsGetE2EEMetadata = await NextcloudKit.shared.getE2EEMetadata(fileId: fileId, e2eToken: e2eToken, options: NCNetworkingE2EE().getOptions())
  60. if resultsGetE2EEMetadata.error == .success {
  61. method = "PUT"
  62. } else if resultsGetE2EEMetadata.error.errorCode != NCGlobal.shared.errorResourceNotFound {
  63. return resultsGetE2EEMetadata.error
  64. }
  65. // UPLOAD METADATA
  66. //
  67. let uploadMetadataError = await uploadMetadata(account: account,
  68. serverUrl: serverUrl,
  69. ocIdServerUrl: directory.ocId,
  70. fileId: fileId,
  71. userId: userId,
  72. e2eToken: e2eToken,
  73. method: method,
  74. addUserId: addUserId,
  75. addCertificate: addCertificate,
  76. removeUserId: removeUserId)
  77. guard uploadMetadataError == .success else {
  78. await unlock(account: account, serverUrl: serverUrl)
  79. return uploadMetadataError
  80. }
  81. // UNLOCK
  82. //
  83. await unlock(account: account, serverUrl: serverUrl)
  84. return NKError()
  85. }
  86. func downloadMetadata(account: String,
  87. serverUrl: String,
  88. urlBase: String,
  89. userId: String,
  90. fileId: String,
  91. e2eToken: String) async -> NKError {
  92. let resultsGetE2EEMetadata = await NextcloudKit.shared.getE2EEMetadata(fileId: fileId, e2eToken: e2eToken, options: NCNetworkingE2EE().getOptions())
  93. guard resultsGetE2EEMetadata.error == .success, let e2eMetadata = resultsGetE2EEMetadata.e2eMetadata else {
  94. return resultsGetE2EEMetadata.error
  95. }
  96. let resultsDecodeMetadataError = NCEndToEndMetadata().decodeMetadata(e2eMetadata, signature: resultsGetE2EEMetadata.signature, serverUrl: serverUrl, account: account, urlBase: urlBase, userId: userId)
  97. guard resultsDecodeMetadataError == .success else {
  98. // Client Diagnostic
  99. NCManageDatabase.shared.addDiagnostic(account: account, issue: NCGlobal.shared.diagnosticIssueE2eeErrors)
  100. return resultsDecodeMetadataError
  101. }
  102. return NKError()
  103. }
  104. func uploadMetadata(account: String,
  105. serverUrl: String,
  106. ocIdServerUrl: String,
  107. fileId: String,
  108. userId: String,
  109. e2eToken: String,
  110. method: String,
  111. addUserId: String? = nil,
  112. addCertificate: String? = nil,
  113. removeUserId: String? = nil) async -> NKError {
  114. let resultsEncodeMetadata = NCEndToEndMetadata().encodeMetadata(account: account, serverUrl: serverUrl, userId: userId, addUserId: addUserId, addCertificate: addCertificate, removeUserId: removeUserId)
  115. guard resultsEncodeMetadata.error == .success, let e2eMetadata = resultsEncodeMetadata.metadata else {
  116. // Client Diagnostic
  117. NCManageDatabase.shared.addDiagnostic(account: account, issue: NCGlobal.shared.diagnosticIssueE2eeErrors)
  118. return resultsEncodeMetadata.error
  119. }
  120. let putE2EEMetadataResults = await NextcloudKit.shared.putE2EEMetadata(fileId: fileId, e2eToken: e2eToken, e2eMetadata: e2eMetadata, signature: resultsEncodeMetadata.signature, method: method, options: NCNetworkingE2EE().getOptions())
  121. guard putE2EEMetadataResults.error == .success else {
  122. return putE2EEMetadataResults.error
  123. }
  124. // COUNTER
  125. //
  126. if NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20 {
  127. NCManageDatabase.shared.updateCounterE2eMetadata(account: account, ocIdServerUrl: ocIdServerUrl, counter: resultsEncodeMetadata.counter)
  128. }
  129. return NKError()
  130. }
  131. func lock(account: String, serverUrl: String) async -> (fileId: String?, e2eToken: String?, error: NKError) {
  132. var e2eToken: String?
  133. var e2eCounter = "1"
  134. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  135. return (nil, nil, NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: "_e2e_error_"))
  136. }
  137. if let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: account, serverUrl: serverUrl) {
  138. e2eToken = tableLock.e2eToken
  139. }
  140. if NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20, var counter = NCManageDatabase.shared.getCounterE2eMetadata(account: account, ocIdServerUrl: directory.ocId) {
  141. counter += 1
  142. e2eCounter = "\(counter)"
  143. }
  144. let resultsLockE2EEFolder = await NextcloudKit.shared.lockE2EEFolder(fileId: directory.fileId, e2eToken: e2eToken, e2eCounter: e2eCounter, method: "POST", options: NCNetworkingE2EE().getOptions())
  145. if resultsLockE2EEFolder.error == .success, let e2eToken = resultsLockE2EEFolder.e2eToken {
  146. NCManageDatabase.shared.setE2ETokenLock(account: account, serverUrl: serverUrl, fileId: directory.fileId, e2eToken: e2eToken)
  147. }
  148. return (directory.fileId, resultsLockE2EEFolder.e2eToken, resultsLockE2EEFolder.error)
  149. }
  150. func unlock(account: String, serverUrl: String) async {
  151. guard let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: account, serverUrl: serverUrl) else {
  152. return
  153. }
  154. let resultsLockE2EEFolder = await NextcloudKit.shared.lockE2EEFolder(fileId: tableLock.fileId, e2eToken: tableLock.e2eToken, e2eCounter: nil, method: "DELETE", options: NCNetworkingE2EE().getOptions())
  155. if resultsLockE2EEFolder.error == .success {
  156. NCManageDatabase.shared.deleteE2ETokenLock(account: account, serverUrl: serverUrl)
  157. }
  158. return
  159. }
  160. func unlockAll(account: String) {
  161. guard NCKeychain().isEndToEndEnabled(account: account) else { return }
  162. Task {
  163. for result in NCManageDatabase.shared.getE2EAllTokenLock(account: account) {
  164. let resultsLockE2EEFolder = await NextcloudKit.shared.lockE2EEFolder(fileId: result.fileId, e2eToken: result.e2eToken, e2eCounter: nil, method: "DELETE", options: NCNetworkingE2EE().getOptions())
  165. if resultsLockE2EEFolder.error == .success {
  166. NCManageDatabase.shared.deleteE2ETokenLock(account: account, serverUrl: result.serverUrl)
  167. }
  168. }
  169. }
  170. }
  171. }