NCEndToEndMetadata.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // NCEndToEndMetadata.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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. class NCEndToEndMetadata : NSObject {
  25. struct e2eMetadata: Codable {
  26. struct metadataKey: Codable {
  27. let metadataKeys: [String: String]
  28. let version: Int
  29. }
  30. struct sharingKey: Codable {
  31. let recipient: [String: String]
  32. }
  33. struct encrypted: Codable {
  34. let key: String
  35. let filename: String
  36. let mimetype: String
  37. let version: Int
  38. }
  39. struct filesKey: Codable {
  40. let initializationVector: String
  41. let authenticationTag: String
  42. let metadataKey: Int
  43. let encrypted: String
  44. }
  45. let files: [String: filesKey]
  46. let metadata: metadataKey
  47. let sharing: sharingKey?
  48. }
  49. @objc static let sharedInstance: NCEndToEndMetadata = {
  50. let instance = NCEndToEndMetadata()
  51. return instance
  52. }()
  53. // --------------------------------------------------------------------------------------------
  54. // MARK: Encode / Decode JSON Metadata
  55. // --------------------------------------------------------------------------------------------
  56. @objc func encoderMetadata(_ recordsE2eEncryption: [tableE2eEncryption], publicKey: String, version: Int) -> String? {
  57. let jsonEncoder = JSONEncoder.init()
  58. var files = [String: e2eMetadata.filesKey]()
  59. // Create "files"
  60. for recordE2eEncryption in recordsE2eEncryption {
  61. let plainEncrypted = recordE2eEncryption.key+"|"+recordE2eEncryption.fileName+"|"+recordE2eEncryption.mimeType+"|"+",\(recordE2eEncryption.version)"
  62. guard let encryptedData = NCEndToEndEncryption.sharedManager().encryptAsymmetricString(plainEncrypted, publicKey: publicKey) else {
  63. //appDelegate.messageNotification("E2E encore metadata", description: "Serious internal error in creation \"encrypted\" key", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  64. return nil
  65. }
  66. let e2eMetadataFilesKey = e2eMetadata.filesKey(initializationVector: recordE2eEncryption.initializationVector, authenticationTag: recordE2eEncryption.authenticationTag, metadataKey: 0, encrypted: String(data: encryptedData, encoding: .utf8)!)
  67. files.updateValue(e2eMetadataFilesKey, forKey: recordE2eEncryption.fileNameIdentifier)
  68. }
  69. // Create "metadata"
  70. let e2eMetadataKey = e2eMetadata.metadataKey(metadataKeys: ["0":"dcccecfvdfvfvsfdvefvefvefvefvefv"], version: version)
  71. // Create final Json e2emetadata
  72. let e2emetadata = e2eMetadata(files: files, metadata: e2eMetadataKey, sharing: nil)
  73. do {
  74. let jsonData = try jsonEncoder.encode(e2emetadata)
  75. let jsonString = String(data: jsonData, encoding: .utf8)
  76. print("JSON String : " + jsonString!)
  77. return jsonString
  78. } catch let error {
  79. //appDelegate.messageNotification("E2E encore metadata", description: "Serious internal error in encoding metadata ("+error.localizedDescription+")", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  80. }
  81. return nil
  82. }
  83. // let dataDecoded : NSData = NSData(base64Encoded: encrypted, options: NSData.Base64DecodingOptions(rawValue: 0))!
  84. @objc func decoderMetadata(_ e2eMetaDataJSON: String, privateKey: String, serverUrl: String, account: String) -> String? {
  85. let jsonDecoder = JSONDecoder.init()
  86. let data = e2eMetaDataJSON.data(using: .utf8)
  87. do {
  88. let decode = try jsonDecoder.decode(e2eMetadata.self, from: data!)
  89. let files = decode.files
  90. let metadata = decode.metadata
  91. //let sharing = decode.sharing ---> V 2.0
  92. var decodeMetadataKeys = [String:String]()
  93. for metadataKeys in metadata.metadataKeys {
  94. guard let metadataKeysData : NSData = NSData(base64Encoded: metadataKeys.value, options: NSData.Base64DecodingOptions(rawValue: 0)) else {
  95. return "Serious internal error in decoding metadata"
  96. }
  97. guard let metadataKey = NCEndToEndEncryption.sharedManager().decryptAsymmetricData(metadataKeysData as Data!, privateKey: privateKey) else {
  98. return "Serious internal error in decoding metadata"
  99. }
  100. // Encode to Base64
  101. let metadataKeyData = Data(base64Encoded: metadataKey, options: NSData.Base64DecodingOptions(rawValue: 0))!
  102. let metadataKeyBase64 = String(data: metadataKeyData, encoding: .utf8)
  103. decodeMetadataKeys[metadataKeys.key] = metadataKeyBase64
  104. }
  105. for file in files {
  106. let fileNameIdentifier = file.key
  107. let elementOfFile = file.value as e2eMetadata.filesKey
  108. let encrypted = elementOfFile.encrypted
  109. let key = decodeMetadataKeys["\(elementOfFile.metadataKey)"]
  110. guard let decyptedMetadata = NCEndToEndEncryption.sharedManager().decryptMetadata(encrypted, key: key) else {
  111. return "Serious internal error in decoding metadata"
  112. }
  113. do {
  114. let decode = try jsonDecoder.decode(e2eMetadata.encrypted.self, from: decyptedMetadata.data(using: .utf8)!)
  115. let object = tableE2eEncryption()
  116. object.account = account
  117. object.authenticationTag = elementOfFile.authenticationTag
  118. object.fileName = decode.filename
  119. object.fileNameIdentifier = fileNameIdentifier
  120. object.key = decode.key
  121. object.initializationVector = elementOfFile.initializationVector
  122. object.mimeType = decode.mimetype
  123. object.serverUrl = serverUrl
  124. object.version = decode.version
  125. // Write file parameter for decrypted on DB
  126. if NCManageDatabase.sharedInstance.addE2eEncryption(object) == false {
  127. return "Serious internal write DB"
  128. }
  129. // Write e2eMetaDataJSON on DB
  130. if NCManageDatabase.sharedInstance.setDirectoryE2EMetadataJSON(serverUrl: serverUrl, metadata: e2eMetaDataJSON) == false {
  131. return "Serious internal write DB"
  132. }
  133. } catch let error {
  134. return "Serious internal error in decoding metadata ("+error.localizedDescription+")"
  135. }
  136. }
  137. } catch let error {
  138. return "Serious internal error in decoding metadata ("+error.localizedDescription+")"
  139. }
  140. return nil
  141. }
  142. }