NCEndToEndMetadata.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // let dataDecoded : NSData = NSData(base64Encoded: encrypted, options: NSData.Base64DecodingOptions(rawValue: 0))!
  54. @objc func decoderMetadata(_ e2eMetaDataJSON: String, privateKey: String, serverUrl: String, account: String) -> String? {
  55. let jsonDecoder = JSONDecoder.init()
  56. let data = e2eMetaDataJSON.data(using: .utf8)
  57. do {
  58. let decode = try jsonDecoder.decode(e2eMetadata.self, from: data!)
  59. let files = decode.files
  60. let metadata = decode.metadata
  61. //let sharing = decode.sharing ---> V 2.0
  62. var decodeMetadataKeys = [String:String]()
  63. for metadataKeys in metadata.metadataKeys {
  64. guard let metadataKeysData : NSData = NSData(base64Encoded: metadataKeys.value, options: NSData.Base64DecodingOptions(rawValue: 0)) else {
  65. return "Serious internal error in decoding metadata"
  66. }
  67. guard let metadataKey = NCEndToEndEncryption.sharedManager().decryptAsymmetricData(metadataKeysData as Data!, privateKey: privateKey) else {
  68. return "Serious internal error in decoding metadata"
  69. }
  70. // Encode to Base64
  71. let metadataKeyData = Data(base64Encoded: metadataKey, options: NSData.Base64DecodingOptions(rawValue: 0))!
  72. let metadataKeyBase64 = String(data: metadataKeyData, encoding: .utf8)
  73. decodeMetadataKeys[metadataKeys.key] = metadataKeyBase64
  74. }
  75. for file in files {
  76. let fileNameIdentifier = file.key
  77. let elementOfFile = file.value as e2eMetadata.filesKey
  78. let encrypted = elementOfFile.encrypted
  79. let key = decodeMetadataKeys["\(elementOfFile.metadataKey)"]
  80. guard let decyptedMetadata = NCEndToEndEncryption.sharedManager().decryptMetadata(encrypted, key: key) else {
  81. return "Serious internal error in decoding metadata"
  82. }
  83. do {
  84. let decode = try jsonDecoder.decode(e2eMetadata.encrypted.self, from: decyptedMetadata.data(using: .utf8)!)
  85. let object = tableE2eEncryption()
  86. object.account = account
  87. object.authenticationTag = elementOfFile.authenticationTag
  88. object.fileName = decode.filename
  89. object.fileNameIdentifier = fileNameIdentifier
  90. object.key = decode.key
  91. object.initializationVector = elementOfFile.initializationVector
  92. object.mimeType = decode.mimetype
  93. object.serverUrl = serverUrl
  94. object.version = decode.version
  95. // Write file parameter for decrypted on DB
  96. if NCManageDatabase.sharedInstance.addE2eEncryption(object) == false {
  97. return "Serious internal write DB"
  98. }
  99. // Write e2eMetaDataJSON on DB
  100. if NCManageDatabase.sharedInstance.setDirectoryE2EMetadataJSON(serverUrl: serverUrl, metadata: e2eMetaDataJSON) == false {
  101. return "Serious internal write DB"
  102. }
  103. } catch let error {
  104. return "Serious internal error in decoding metadata ("+error.localizedDescription+")"
  105. }
  106. }
  107. } catch let error {
  108. return "Serious internal error in decoding metadata ("+error.localizedDescription+")"
  109. }
  110. return nil
  111. }
  112. }