NCNetworkingE2EE.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 generateRandomIdentifier() -> String {
  25. var UUID = NSUUID().uuidString
  26. UUID = "E2EE" + UUID.replacingOccurrences(of: "-", with: "")
  27. return UUID
  28. }
  29. func uploadMetadata(account: String, serverUrl: String, userId: String, addUserId: String?, removeUserId: String?) async -> NKError {
  30. var addCertificate: String?
  31. var method = "POST"
  32. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  33. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: "_e2e_error_")
  34. }
  35. if let addUserId {
  36. let results = await NextcloudKit.shared.getE2EECertificate(user: addUserId)
  37. if results.error == .success, let certificateUser = results.certificateUser {
  38. addCertificate = certificateUser
  39. } else {
  40. return results.error
  41. }
  42. }
  43. // LOCK
  44. //
  45. let resultsLock = await lock(account: account, serverUrl: serverUrl)
  46. guard resultsLock.error == .success, let e2eToken = resultsLock.e2eToken, let fileId = resultsLock.fileId else {
  47. return resultsLock.error
  48. }
  49. // METHOD
  50. //
  51. let resultsGetE2EEMetadata = await NextcloudKit.shared.getE2EEMetadata(fileId: fileId, e2eToken: e2eToken)
  52. if resultsGetE2EEMetadata.error == .success {
  53. method = "PUT"
  54. } else if resultsGetE2EEMetadata.error.errorCode != NCGlobal.shared.errorResourceNotFound {
  55. return resultsGetE2EEMetadata.error
  56. }
  57. // UPLOAD METADATA
  58. //
  59. let uploadMetadataError = await uploadMetadata(account: account,
  60. serverUrl: serverUrl,
  61. ocIdServerUrl: directory.ocId,
  62. fileId: fileId,
  63. userId: userId,
  64. e2eToken: e2eToken,
  65. method: method,
  66. addUserId: addUserId,
  67. addCertificate: addCertificate,
  68. removeUserId: removeUserId)
  69. guard uploadMetadataError == .success else {
  70. await unlock(account: account, serverUrl: serverUrl)
  71. return uploadMetadataError
  72. }
  73. // UNLOCK
  74. //
  75. await unlock(account: account, serverUrl: serverUrl)
  76. return NKError()
  77. }
  78. func downloadMetadata(account: String,
  79. serverUrl: String,
  80. urlBase: String,
  81. userId: String,
  82. fileId: String,
  83. e2eToken: String) async -> NKError {
  84. let resultsGetE2EEMetadata = await NextcloudKit.shared.getE2EEMetadata(fileId: fileId, e2eToken: e2eToken)
  85. guard resultsGetE2EEMetadata.error == .success, let e2eMetadata = resultsGetE2EEMetadata.e2eMetadata else {
  86. return resultsGetE2EEMetadata.error
  87. }
  88. let resultsDecodeMetadataError = NCEndToEndMetadata().decodeMetadata(e2eMetadata, signature: resultsGetE2EEMetadata.signature, serverUrl: serverUrl, account: account, urlBase: urlBase, userId: userId)
  89. guard resultsDecodeMetadataError == .success else {
  90. return resultsDecodeMetadataError
  91. }
  92. return NKError()
  93. }
  94. func uploadMetadata(account: String,
  95. serverUrl: String,
  96. ocIdServerUrl: String,
  97. fileId: String,
  98. userId: String,
  99. e2eToken: String,
  100. method: String,
  101. addUserId: String? = nil,
  102. addCertificate: String? = nil,
  103. removeUserId: String? = nil) async -> NKError {
  104. let resultsEncodeMetadata = NCEndToEndMetadata().encodeMetadata(account: account, serverUrl: serverUrl, userId: userId, addCertificate: addCertificate, removeUserId: removeUserId)
  105. guard resultsEncodeMetadata.error == .success, let e2eMetadata = resultsEncodeMetadata.metadata else {
  106. return resultsEncodeMetadata.error
  107. }
  108. let putE2EEMetadataResults = await NextcloudKit.shared.putE2EEMetadata(fileId: fileId, e2eToken: e2eToken, e2eMetadata: e2eMetadata, signature: resultsEncodeMetadata.signature, method: method)
  109. guard putE2EEMetadataResults.error == .success else {
  110. return putE2EEMetadataResults.error
  111. }
  112. // COUNTER
  113. //
  114. if NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20 {
  115. NCManageDatabase.shared.updateCounterE2eMetadataV2(account: account, ocIdServerUrl: ocIdServerUrl, counter: resultsEncodeMetadata.counter)
  116. }
  117. return NKError()
  118. }
  119. func lock(account: String, serverUrl: String) async -> (fileId: String?, e2eToken: String?, error: NKError) {
  120. var e2eToken: String?
  121. var e2eCounter = "0"
  122. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  123. return (nil, nil, NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: "_e2e_error_"))
  124. }
  125. if let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: account, serverUrl: serverUrl) {
  126. e2eToken = tableLock.e2eToken
  127. }
  128. if NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20, var counter = NCManageDatabase.shared.getCounterE2eMetadataV2(account: account, ocIdServerUrl: directory.ocId) {
  129. counter += 1
  130. e2eCounter = "\(counter)"
  131. }
  132. let resultsLockE2EEFolder = await NextcloudKit.shared.lockE2EEFolder(fileId: directory.fileId, e2eToken: e2eToken, e2eCounter: e2eCounter, method: "POST")
  133. if resultsLockE2EEFolder.error == .success, let e2eToken = resultsLockE2EEFolder.e2eToken {
  134. NCManageDatabase.shared.setE2ETokenLock(account: account, serverUrl: serverUrl, fileId: directory.fileId, e2eToken: e2eToken)
  135. }
  136. return (directory.fileId, resultsLockE2EEFolder.e2eToken, resultsLockE2EEFolder.error)
  137. }
  138. func unlock(account: String, serverUrl: String) async {
  139. guard let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: account, serverUrl: serverUrl) else {
  140. return
  141. }
  142. let resultsLockE2EEFolder = await NextcloudKit.shared.lockE2EEFolder(fileId: tableLock.fileId, e2eToken: tableLock.e2eToken, e2eCounter: nil, method: "DELETE")
  143. if resultsLockE2EEFolder.error == .success {
  144. NCManageDatabase.shared.deleteE2ETokenLock(account: account, serverUrl: serverUrl)
  145. }
  146. return
  147. }
  148. func unlockAll(account: String) {
  149. guard CCUtility.isEnd(toEndEnabled: account) else { return }
  150. Task {
  151. for result in NCManageDatabase.shared.getE2EAllTokenLock(account: account) {
  152. let resultsLockE2EEFolder = await NextcloudKit.shared.lockE2EEFolder(fileId: result.fileId, e2eToken: result.e2eToken, e2eCounter: nil, method: "DELETE")
  153. if resultsLockE2EEFolder.error == .success {
  154. NCManageDatabase.shared.deleteE2ETokenLock(account: account, serverUrl: result.serverUrl)
  155. }
  156. }
  157. }
  158. }
  159. }