Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NCEndToEndMetadata.swift « Security « iOSClient - github.com/nextcloud/ios.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdaa8a4afa95d179795fa8f29c922b38caf48631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
//  NCEndToEndMetadata.swift
//  Nextcloud
//
//  Created by Marino Faggiana on 13/11/17.
//  Copyright © 2017 Marino Faggiana. All rights reserved.
//
//  Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import UIKit
import NextcloudKit

class NCEndToEndMetadata: NSObject {

    struct e2eMetadata: Codable {

        struct metadataKeyCodable: Codable {

            let metadataKeys: [String: String]
            let version: Int
        }

        struct sharingCodable: Codable {

            let recipient: [String: String]
        }

        struct encryptedFileAttributes: Codable {

            let key: String
            let filename: String
            let mimetype: String
            let version: Int
        }

        struct filesCodable: Codable {

            let initializationVector: String
            let authenticationTag: String?
            let metadataKey: Int                // Number of metadataKey
            let encrypted: String               // encryptedFileAttributes
        }

        let files: [String: filesCodable]
        let metadata: metadataKeyCodable
        let sharing: sharingCodable?
    }

    @objc static let shared: NCEndToEndMetadata = {
        let instance = NCEndToEndMetadata()
        return instance
    }()

    // --------------------------------------------------------------------------------------------
    // MARK: Encode / Decode JSON Metadata
    // --------------------------------------------------------------------------------------------

    @objc func encoderMetadata(_ recordsE2eEncryption: [tableE2eEncryption], privateKey: String, serverUrl: String) -> String? {

        let jsonEncoder = JSONEncoder()
        var files: [String: e2eMetadata.filesCodable] = [:]
        var version = 1
        var metadataKeysDictionary: [String: String] = [:]

        for recordE2eEncryption in recordsE2eEncryption {

            // *** metadataKey ***

            // Encode64 for Android compatibility
            let metadatakey = (recordE2eEncryption.metadataKey.data(using: .utf8)?.base64EncodedString())!

            guard let metadataKeyEncryptedData = NCEndToEndEncryption.sharedManager().encryptAsymmetricString(metadatakey, publicKey: nil, privateKey: privateKey) else {
                return nil
            }

            let metadataKeyEncryptedBase64 = metadataKeyEncryptedData.base64EncodedString()

            metadataKeysDictionary["\(recordE2eEncryption.metadataKeyIndex)"] = metadataKeyEncryptedBase64

            // *** File ***

            let encrypted = e2eMetadata.encryptedFileAttributes(key: recordE2eEncryption.key, filename: recordE2eEncryption.fileName, mimetype: recordE2eEncryption.mimeType, version: recordE2eEncryption.version)

            do {

                // Create "encrypted"
                let encryptedJsonData = try jsonEncoder.encode(encrypted)
                let encryptedJsonString = String(data: encryptedJsonData, encoding: .utf8)

                guard let encryptedEncryptedJson = NCEndToEndEncryption.sharedManager().encryptEncryptedJson(encryptedJsonString, key: recordE2eEncryption.metadataKey) else {
                    print("Serious internal error in encoding metadata")
                    return nil
                }

                let e2eMetadataFilesKey = e2eMetadata.filesCodable(initializationVector: recordE2eEncryption.initializationVector, authenticationTag: recordE2eEncryption.authenticationTag, metadataKey: 0, encrypted: encryptedEncryptedJson)

                files.updateValue(e2eMetadataFilesKey, forKey: recordE2eEncryption.fileNameIdentifier)

            } catch let error {
                print("Serious internal error in encoding metadata ("+error.localizedDescription+")")
                return nil
            }

            version = recordE2eEncryption.version
        }

        // Create Json metadataKeys
        // e2eMetadataKey = e2eMetadata.metadataKeyCodable(metadataKeys: ["0":metadataKeyEncryptedBase64], version: version)
        let e2eMetadataKey = e2eMetadata.metadataKeyCodable(metadataKeys: metadataKeysDictionary, version: version)

        // Create final Json e2emetadata
        let e2emetadata = e2eMetadata(files: files, metadata: e2eMetadataKey, sharing: nil)

        do {

            let jsonData = try jsonEncoder.encode(e2emetadata)
            let jsonString = String(data: jsonData, encoding: .utf8)
            print("JSON String : " + jsonString!)

            return jsonString

        } catch let error {
            print("Serious internal error in encoding metadata ("+error.localizedDescription+")")
            return nil
        }
    }

    @discardableResult
    @objc func decoderMetadata(_ e2eMetaDataJSON: String, privateKey: String, serverUrl: String, account: String, urlBase: String) -> Bool {

        let jsonDecoder = JSONDecoder()
        let data = e2eMetaDataJSON.data(using: .utf8)
        // let dataQuickLook = (data as! NSData)

        do {

            // *** metadataKey ***

            let decode = try jsonDecoder.decode(e2eMetadata.self, from: data!)

            let files = decode.files
            let metadata = decode.metadata
            // let sharing = decode.sharing ---> V 2.0
            var metadataKeysDictionary: [String: String] = [:]

            for metadataKeyDictionaryEncrypted in metadata.metadataKeys {

                guard let metadataKeyEncryptedData: NSData = NSData(base64Encoded: metadataKeyDictionaryEncrypted.value, options: NSData.Base64DecodingOptions(rawValue: 0)) else {
                    return false
                }

                guard let metadataKeyBase64 = NCEndToEndEncryption.sharedManager().decryptAsymmetricData(metadataKeyEncryptedData as Data?, privateKey: privateKey) else {
                    return false
                }

                // Initialize a `Data` from a Base-64 encoded String
                let metadataKeyBase64Data = Data(base64Encoded: metadataKeyBase64, options: NSData.Base64DecodingOptions(rawValue: 0))!
                let metadataKey = String(data: metadataKeyBase64Data, encoding: .utf8)

                metadataKeysDictionary[metadataKeyDictionaryEncrypted.key] = metadataKey
            }

            // *** File ***

            for file in files {

                let fileNameIdentifier = file.key
                let filesCodable = file.value as e2eMetadata.filesCodable

                let encrypted = filesCodable.encrypted
                let metadataKey = metadataKeysDictionary["\(filesCodable.metadataKey)"]

                guard let encryptedFileAttributesJson = NCEndToEndEncryption.sharedManager().decryptEncryptedJson(encrypted, key: metadataKey) else {
                    return false
                }

                do {
                    let encryptedFileAttributes = try jsonDecoder.decode(e2eMetadata.encryptedFileAttributes.self, from: encryptedFileAttributesJson.data(using: .utf8)!)
                    if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND fileName == %@", account, fileNameIdentifier)) {
                        let metadata = tableMetadata.init(value: metadata)

                        let object = tableE2eEncryption()

                        object.account = account
                        object.authenticationTag = filesCodable.authenticationTag ?? ""
                        object.fileName = encryptedFileAttributes.filename
                        object.fileNameIdentifier = fileNameIdentifier
                        object.fileNamePath = CCUtility.returnFileNamePath(fromFileName: encryptedFileAttributes.filename, serverUrl: serverUrl, urlBase: urlBase, account: account)
                        object.key = encryptedFileAttributes.key
                        object.initializationVector = filesCodable.initializationVector
                        object.metadataKey = metadataKey!
                        object.metadataKeyIndex = filesCodable.metadataKey
                        object.mimeType = encryptedFileAttributes.mimetype
                        object.serverUrl = serverUrl
                        object.version = encryptedFileAttributes.version

                        // If exists remove records
                        NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND fileNamePath == %@", object.account, object.fileNamePath))
                        NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND fileNameIdentifier == %@", object.account, object.fileNameIdentifier))

                        // Write file parameter for decrypted on DB
                        NCManageDatabase.shared.addE2eEncryption(object)

                        // Update metadata on tableMetadata
                        metadata.fileNameView = encryptedFileAttributes.filename
                        metadata.fileNameWithoutExt = (encryptedFileAttributes.filename as NSString).deletingPathExtension

                        let results = NKCommon.shared.getInternalType(fileName: encryptedFileAttributes.filename, mimeType: metadata.contentType, directory: metadata.directory)

                        metadata.contentType = results.mimeType
                        metadata.iconName = results.iconName
                        metadata.classFile = results.classFile

                        NCManageDatabase.shared.addMetadata(metadata)
                    }

                } catch let error {
                    print("Serious internal error in decoding metadata ("+error.localizedDescription+")")
                }
            }

        } catch let error {
            print("Serious internal error in decoding metadata ("+error.localizedDescription+")")
            return false
        }

        return true
    }
}