NCEndToEndMetadata.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 metadataKeyCodable: Codable {
  27. let metadataKeys: [String: String]
  28. let version: Int
  29. }
  30. struct sharingCodable: Codable {
  31. let recipient: [String: String]
  32. }
  33. struct encryptedFileAttributes: Codable {
  34. let key: String
  35. let filename: String
  36. let mimetype: String
  37. let version: Int
  38. }
  39. struct filesCodable: Codable {
  40. let initializationVector: String
  41. let authenticationTag: String
  42. let metadataKey: Int
  43. let encrypted: String // encryptedFileAttributes
  44. }
  45. let files: [String: filesCodable]
  46. let metadata: metadataKeyCodable
  47. let sharing: sharingCodable?
  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, privateKey: String) -> String? {
  57. let jsonEncoder = JSONEncoder.init()
  58. var files = [String: e2eMetadata.filesCodable]()
  59. var version = 1
  60. // Create publicKey encrypted
  61. //NCEndToEndEncryption.sharedManager().encryptAsymmetricString(<#T##plain: String!##String!#>, publicKey: <#T##String!#>)
  62. // Create "files"
  63. for recordE2eEncryption in recordsE2eEncryption {
  64. let encrypted = e2eMetadata.encryptedFileAttributes(key: recordE2eEncryption.key, filename: recordE2eEncryption.fileName, mimetype: recordE2eEncryption.mimeType, version: recordE2eEncryption.version)
  65. do {
  66. // Create "encrypted"
  67. let encryptedJsonData = try jsonEncoder.encode(encrypted)
  68. let encryptedJsonString = String(data: encryptedJsonData, encoding: .utf8)
  69. guard let encryptedEncryptionData = NCEndToEndEncryption.sharedManager().encryptAsymmetricString(encryptedJsonString, publicKey: nil, privateKey: privateKey) else {
  70. print("Serious internal error in encoding metadata")
  71. return nil
  72. }
  73. let encryptedEncryptionBase64 = encryptedEncryptionData.base64EncodedString()
  74. let e2eMetadataFilesKey = e2eMetadata.filesCodable(initializationVector: recordE2eEncryption.initializationVector, authenticationTag: recordE2eEncryption.authenticationTag, metadataKey: 0, encrypted: encryptedEncryptionBase64)
  75. files.updateValue(e2eMetadataFilesKey, forKey: recordE2eEncryption.fileNameIdentifier)
  76. } catch let error {
  77. print("Serious internal error in encoding metadata ("+error.localizedDescription+")")
  78. return nil
  79. }
  80. version = recordE2eEncryption.version
  81. }
  82. // Create "metadataKey" with encrypted publicKey
  83. let e2eMetadataKey = e2eMetadata.metadataKeyCodable(metadataKeys: ["0":"dcccecfvdfvfvsfdvefvefvefvefvefv"], version: version)
  84. // Create final Json e2emetadata
  85. let e2emetadata = e2eMetadata(files: files, metadata: e2eMetadataKey, sharing: nil)
  86. do {
  87. let jsonData = try jsonEncoder.encode(e2emetadata)
  88. let jsonString = String(data: jsonData, encoding: .utf8)
  89. print("JSON String : " + jsonString!)
  90. return jsonString
  91. } catch let error {
  92. print("Serious internal error in encoding metadata ("+error.localizedDescription+")")
  93. return nil
  94. }
  95. }
  96. @objc func decoderMetadata(_ e2eMetaDataJSON: String, privateKey: String, serverUrl: String, account: String, url: String) -> Bool {
  97. let jsonDecoder = JSONDecoder.init()
  98. let data = e2eMetaDataJSON.data(using: .utf8)
  99. do {
  100. let decode = try jsonDecoder.decode(e2eMetadata.self, from: data!)
  101. let files = decode.files
  102. let metadata = decode.metadata
  103. //let sharing = decode.sharing ---> V 2.0
  104. var publicKeys = [String:String]()
  105. for metadataKeys in metadata.metadataKeys {
  106. guard let publicKeyEncryptedData : NSData = NSData(base64Encoded: metadataKeys.value, options: NSData.Base64DecodingOptions(rawValue: 0)) else {
  107. return false
  108. }
  109. guard let publicKeyBase64 = NCEndToEndEncryption.sharedManager().decryptAsymmetricData(publicKeyEncryptedData as Data!, privateKey: privateKey) else {
  110. return false
  111. }
  112. // Initialize a `Data` from a Base-64 encoded String
  113. let publicKeyBase64Data = Data(base64Encoded: publicKeyBase64, options: NSData.Base64DecodingOptions(rawValue: 0))!
  114. let publicKey = String(data: publicKeyBase64Data, encoding: .utf8)
  115. publicKeys[metadataKeys.key] = publicKey
  116. }
  117. for file in files {
  118. let fileNameIdentifier = file.key
  119. let filesCodable = file.value as e2eMetadata.filesCodable
  120. let encrypted = filesCodable.encrypted
  121. let key = publicKeys["\(filesCodable.metadataKey)"]
  122. guard let encryptedFileAttributesJson = NCEndToEndEncryption.sharedManager().decryptMetadata(encrypted, key: key) else {
  123. return false
  124. }
  125. do {
  126. let encryptedFileAttributes = try jsonDecoder.decode(e2eMetadata.encryptedFileAttributes.self, from: encryptedFileAttributesJson.data(using: .utf8)!)
  127. let object = tableE2eEncryption()
  128. object.account = account
  129. object.authenticationTag = filesCodable.authenticationTag
  130. object.fileName = encryptedFileAttributes.filename
  131. object.fileNameIdentifier = fileNameIdentifier
  132. object.fileNamePath = CCUtility.returnFileNamePath(fromFileName: encryptedFileAttributes.filename, serverUrl: serverUrl, activeUrl: url)
  133. object.key = encryptedFileAttributes.key
  134. object.initializationVector = filesCodable.initializationVector
  135. object.mimeType = encryptedFileAttributes.mimetype
  136. object.serverUrl = serverUrl
  137. object.version = encryptedFileAttributes.version
  138. // Write file parameter for decrypted on DB
  139. if NCManageDatabase.sharedInstance.addE2eEncryption(object) == false {
  140. return false
  141. }
  142. // Write e2eMetaDataJSON on DB
  143. if NCManageDatabase.sharedInstance.setDirectoryE2EMetadataJSON(serverUrl: serverUrl, metadata: e2eMetaDataJSON) == false {
  144. return false
  145. }
  146. } catch let error {
  147. print("Serious internal error in decoding metadata ("+error.localizedDescription+")")
  148. return false
  149. }
  150. }
  151. } catch let error {
  152. print("Serious internal error in decoding metadata ("+error.localizedDescription+")")
  153. return false
  154. }
  155. return true
  156. }
  157. }