NCEndToEndMetadata.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // NCEndToEndMetadata.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/17.
  6. // Copyright © 2017 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 UIKit
  24. import NextcloudKit
  25. class NCEndToEndMetadata: NSObject {
  26. struct E2eeV1: Codable {
  27. struct Metadata: Codable {
  28. let metadataKeys: [String: String]
  29. let version: Double
  30. }
  31. struct Encrypted: Codable {
  32. let key: String
  33. let filename: String
  34. let mimetype: String
  35. }
  36. struct Files: Codable {
  37. let initializationVector: String
  38. let authenticationTag: String?
  39. let metadataKey: Int
  40. let encrypted: String
  41. }
  42. let metadata: Metadata
  43. let files: [String: Files]?
  44. }
  45. struct E2eeV12: Codable {
  46. struct Metadata: Codable {
  47. let metadataKey: String
  48. let version: Double
  49. let checksum: String?
  50. }
  51. struct Encrypted: Codable {
  52. let key: String
  53. let filename: String
  54. let mimetype: String
  55. }
  56. struct Files: Codable {
  57. let initializationVector: String
  58. let authenticationTag: String?
  59. let encrypted: String
  60. }
  61. struct Filedrop: Codable {
  62. let initializationVector: String
  63. let authenticationTag: String?
  64. let encrypted: String
  65. let encryptedKey: String?
  66. let encryptedTag: String?
  67. let encryptedInitializationVector: String?
  68. }
  69. let metadata: Metadata
  70. let files: [String: Files]?
  71. let filedrop: [String: Filedrop]?
  72. }
  73. struct E2eeV20: Codable {
  74. struct Metadata: Codable {
  75. let ciphertext: String
  76. let nonce: String
  77. let authenticationTag: String
  78. let counter: Double?
  79. }
  80. struct Users: Codable {
  81. let userId: String
  82. let certificate: String
  83. let encryptedMetadataKey: String?
  84. let encryptedFiledropKey: String?
  85. }
  86. struct Filedrop: Codable {
  87. let ciphertext: String?
  88. let nonce: String?
  89. let authenticationTag: String?
  90. let users: [String: UsersFiledrop]?
  91. struct UsersFiledrop: Codable {
  92. let userId: String?
  93. let encryptedFiledropKey: String?
  94. }
  95. }
  96. let metadata: Metadata
  97. let users: [Users]
  98. let filedrop: [String: Filedrop]?
  99. let version: String
  100. }
  101. // --------------------------------------------------------------------------------------------
  102. // MARK: Encode JSON Metadata Bridge
  103. // --------------------------------------------------------------------------------------------
  104. func encoderMetadata(_ items: [tableE2eEncryption], account: String, serverUrl: String) -> (metadata: String?, signature: String?) {
  105. let e2EEApiVersion = NCGlobal.shared.capabilityE2EEApiVersion
  106. switch e2EEApiVersion {
  107. case "1.2":
  108. return encoderMetadataV12(items, account: account, serverUrl: serverUrl)
  109. case "2.0":
  110. return encoderMetadataV20(items, account: account, serverUrl: serverUrl)
  111. default:
  112. return (nil, nil)
  113. }
  114. }
  115. // --------------------------------------------------------------------------------------------
  116. // MARK: Decode JSON Metadata Bridge
  117. // --------------------------------------------------------------------------------------------
  118. func decoderMetadata(_ json: String, serverUrl: String, account: String, urlBase: String, userId: String, ownerId: String?) -> NKError {
  119. guard let data = json.data(using: .utf8) else {
  120. return (NKError(errorCode: NCGlobal.shared.errorE2EE, errorDescription: "Error decoding JSON"))
  121. }
  122. data.printJson()
  123. let decoder = JSONDecoder()
  124. if (try? decoder.decode(E2eeV1.self, from: data)) != nil {
  125. return decoderMetadataV1(json, serverUrl: serverUrl, account: account, urlBase: urlBase, userId: userId)
  126. } else if (try? decoder.decode(E2eeV12.self, from: data)) != nil {
  127. return decoderMetadataV12(json, serverUrl: serverUrl, account: account, urlBase: urlBase, userId: userId, ownerId: ownerId)
  128. } else if (try? decoder.decode(E2eeV20.self, from: data)) != nil {
  129. return decoderMetadataV20(json, serverUrl: serverUrl, account: account, urlBase: urlBase, userId: userId, ownerId: ownerId)
  130. } else {
  131. return NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "Server E2EE version " + NCGlobal.shared.capabilityE2EEApiVersion + ", not compatible")
  132. }
  133. }
  134. }