Browse Source

add new class NCEndToEndMetadata.swift

Marino Faggiana 7 years ago
parent
commit
d22da1da54

+ 10 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -597,6 +597,10 @@
 		F7F54D0D1E5B14C800E19C62 /* MWZoomingScrollView.m in Sources */ = {isa = PBXBuildFile; fileRef = F7F54CE21E5B14C700E19C62 /* MWZoomingScrollView.m */; };
 		F7F54D0E1E5B14C800E19C62 /* UIImage+MWPhotoBrowser.m in Sources */ = {isa = PBXBuildFile; fileRef = F7F54CE41E5B14C700E19C62 /* UIImage+MWPhotoBrowser.m */; };
 		F7F801031D98205A007537BC /* CCCertificate.m in Sources */ = {isa = PBXBuildFile; fileRef = F7F801011D98205A007537BC /* CCCertificate.m */; };
+		F7F878AE1FB9E3B900599E4F /* NCEndToEndMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7F878AD1FB9E3B900599E4F /* NCEndToEndMetadata.swift */; };
+		F7F878AF1FB9E3B900599E4F /* NCEndToEndMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7F878AD1FB9E3B900599E4F /* NCEndToEndMetadata.swift */; };
+		F7F878B01FB9E3B900599E4F /* NCEndToEndMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7F878AD1FB9E3B900599E4F /* NCEndToEndMetadata.swift */; };
+		F7F878B11FB9E3B900599E4F /* NCEndToEndMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7F878AD1FB9E3B900599E4F /* NCEndToEndMetadata.swift */; };
 		F7F8D71C1ED6183000E711F3 /* CCCellShareExt.m in Sources */ = {isa = PBXBuildFile; fileRef = F7F8D71A1ED6183000E711F3 /* CCCellShareExt.m */; };
 		F7F8D71D1ED6183000E711F3 /* CCCellShareExt.xib in Resources */ = {isa = PBXBuildFile; fileRef = F7F8D71B1ED6183000E711F3 /* CCCellShareExt.xib */; };
 		F7FC7D561DC1F93800BB2C6A /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = F7FC7D551DC1F93800BB2C6A /* libz.tbd */; };
@@ -1500,6 +1504,7 @@
 		F7F67BB81A24D27800EE80DA /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
 		F7F801001D98205A007537BC /* CCCertificate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCCertificate.h; sourceTree = "<group>"; };
 		F7F801011D98205A007537BC /* CCCertificate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCCertificate.m; sourceTree = "<group>"; };
+		F7F878AD1FB9E3B900599E4F /* NCEndToEndMetadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCEndToEndMetadata.swift; sourceTree = "<group>"; };
 		F7F8D7191ED6183000E711F3 /* CCCellShareExt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCCellShareExt.h; sourceTree = "<group>"; };
 		F7F8D71A1ED6183000E711F3 /* CCCellShareExt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCCellShareExt.m; sourceTree = "<group>"; };
 		F7F8D71B1ED6183000E711F3 /* CCCellShareExt.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CCCellShareExt.xib; sourceTree = "<group>"; };
@@ -2997,6 +3002,7 @@
 				F7F801001D98205A007537BC /* CCCertificate.h */,
 				F7F801011D98205A007537BC /* CCCertificate.m */,
 				F717EFFF1F96661400A324A5 /* NCEntoToEndInterface.swift */,
+				F7F878AD1FB9E3B900599E4F /* NCEndToEndMetadata.swift */,
 				F70CAE381F8CF31A008125FD /* NCEndToEndEncryption.h */,
 				F70CAE391F8CF31A008125FD /* NCEndToEndEncryption.m */,
 			);
@@ -3409,6 +3415,7 @@
 				F70022ED1EC4C9100080073F /* OCXMLSharedParser.m in Sources */,
 				F70022D51EC4C9100080073F /* NSDate+ISO8601.m in Sources */,
 				F70022FC1EC4C9100080073F /* NSString+Encode.m in Sources */,
+				F7F878AF1FB9E3B900599E4F /* NCEndToEndMetadata.swift in Sources */,
 				F70022E41EC4C9100080073F /* OCXMLParser.m in Sources */,
 				F71459C91D12E3B700CAFEEC /* OCNetworking.m in Sources */,
 				F71459CD1D12E3B700CAFEEC /* AFViewShaker.m in Sources */,
@@ -3473,6 +3480,7 @@
 				F70022E21EC4C9100080073F /* OCXMLListParser.m in Sources */,
 				F70022A61EC4C9100080073F /* AFNetworkReachabilityManager.m in Sources */,
 				F70022E51EC4C9100080073F /* OCXMLParser.m in Sources */,
+				F7F878B01FB9E3B900599E4F /* NCEndToEndMetadata.swift in Sources */,
 				F70022B21EC4C9100080073F /* AFURLSessionManager.m in Sources */,
 				F70022BB1EC4C9100080073F /* OCCommunication.m in Sources */,
 				F70022A31EC4C9100080073F /* AFHTTPSessionManager.m in Sources */,
@@ -3534,6 +3542,7 @@
 				F7BB50CE1F22388000C47094 /* OCNotifications.m in Sources */,
 				F7BB50CC1F22387900C47094 /* OCExternalSites.m in Sources */,
 				F7BB50EA1F2238FB00C47094 /* UIScrollView+EmptyDataSet.m in Sources */,
+				F7F878B11FB9E3B900599E4F /* NCEndToEndMetadata.swift in Sources */,
 				F7BB50F71F2239BF00C47094 /* BKPasscodeLockScreenManager.m in Sources */,
 				F7BB50DB1F2238A900C47094 /* OCXMLShareByLinkParser.m in Sources */,
 				F7BB50E21F2238D000C47094 /* AFURLRequestSerialization.m in Sources */,
@@ -3732,6 +3741,7 @@
 				F7D424581F063B82009C9782 /* PHAssetCollection+CTAssetsPickerController.m in Sources */,
 				F73CC07E1E813DFF006E3047 /* BKTouchIDSwitchView.m in Sources */,
 				F77B0E5F1D118A16002130FE /* CCSettings.m in Sources */,
+				F7F878AE1FB9E3B900599E4F /* NCEndToEndMetadata.swift in Sources */,
 				F7D424611F063B82009C9782 /* CTAssetScrollView.m in Sources */,
 				F762CB141EACB66200B38484 /* XLFormRightImageButton.m in Sources */,
 				F7B3A4EE1E97818A000DACE8 /* CCLoginWeb.swift in Sources */,

+ 1 - 0
Picker/Picker-Bridging-Header.h

@@ -13,6 +13,7 @@
 #import "CCUtility.h"
 #import "BKPasscodeViewController.h"
 #import "CCBKPasscode.h"
+#import "NCEndToEndEncryption.h"
 
 #import "OCActivity.h"
 #import "OCUserProfile.h"

+ 1 - 0
PickerFileProvider/PickerFileProvider-Bridging-Header.h

@@ -13,6 +13,7 @@
 #import "BKPasscodeViewController.h"
 #import "CCBKPasscode.h"
 #import "CCUtility.h"
+#import "NCEndToEndEncryption.h"
 
 #import "OCActivity.h"
 #import "OCUserProfile.h"

+ 1 - 0
Share/Share-Bridging-Header.h

@@ -4,6 +4,7 @@
 
 #import "CCHud.h"
 #import "ShareViewController.h"
+#import "NCEndToEndEncryption.h"
 
 #import "OCActivity.h"
 #import "OCUserProfile.h"

+ 27 - 0
iOSClient/Networking/CCNetworking.m

@@ -929,12 +929,39 @@
         
         // Create/update Metadata
         
+        BOOL updateMetadata;
+        
+        tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@", _activeAccount, directoryID]];
+        
+        if (directory.e2eMetadataJSON.length > 0) {
+            
+            updateMetadata = YES;
+            
+        } else {
+            
+            updateMetadata = NO;
+            
+        }
+        
         // ...
         
         // Upload Metadata
         
         // ...
         
+        
+        /*
+         NSError *error;
+         BOOL encrypted = [CCUtility isFolderEncrypted:serverUrl account:_activeAccount];
+         if (encrypted) {
+         
+         NSString *tokenLock = [[NCManageDatabase sharedInstance] getE2eEncryptionTokenLockWithServerUrl:serverUrl];
+         tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", _activeAccount, serverUrl]];
+         error = [[NCNetworkingSync sharedManager] lockEndToEndFolderEncrypted:_activeUser userID:_activeUserID password:_activePassword url:_activeUrl fileID:directory.fileID token:&tokenLock];
+         }
+         */
+        
+        
         // Now the fileName is fileNameIdentifier
         metadata.fileName = fileNameIdentifier;
         metadata.e2eEncrypted = true;

+ 149 - 0
iOSClient/Security/NCEndToEndMetadata.swift

@@ -0,0 +1,149 @@
+//
+//  NCEndToEndMetadata.swift
+//  Nextcloud
+//
+//  Created by Marino Faggiana on 13/11/17.
+//  Copyright © 2017 TWS. All rights reserved.
+//
+//  Author Marino Faggiana <m.faggiana@twsweb.it>
+//
+//  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 Foundation
+
+class NCEndToEndMetadata : NSObject  {
+
+    struct e2eMetadata: Codable {
+        
+        struct metadataKey: Codable {
+            
+            let metadataKeys: [String: String]
+            let version: Int
+        }
+        
+        struct sharingKey: Codable {
+            
+            let recipient: [String: String]
+        }
+        
+        struct encrypted: Codable {
+            
+            let key: String
+            let filename: String
+            let mimetype: String
+            let version: Int
+        }
+        
+        struct filesKey: Codable {
+            
+            let initializationVector: String
+            let authenticationTag: String
+            let metadataKey: Int
+            let encrypted: String
+        }
+        
+        let files: [String: filesKey]
+        let metadata: metadataKey
+        let sharing: sharingKey?
+    }
+
+    @objc static let sharedInstance: NCEndToEndMetadata = {
+        let instance = NCEndToEndMetadata()
+        return instance
+    }()
+    
+    // let dataDecoded : NSData = NSData(base64Encoded: encrypted, options: NSData.Base64DecodingOptions(rawValue: 0))!
+    @objc func decoderMetadata(_ e2eMetaDataJSON: String, privateKey: String, serverUrl: String, account: String) -> String? {
+        
+        let jsonDecoder = JSONDecoder.init()
+        let data = e2eMetaDataJSON.data(using: .utf8)
+        
+        do {
+            
+            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 decodeMetadataKeys = [String:String]()
+            
+            for metadataKeys in metadata.metadataKeys {
+                
+                guard let metadataKeysData : NSData = NSData(base64Encoded: metadataKeys.value, options: NSData.Base64DecodingOptions(rawValue: 0)) else {
+                    return "Serious internal error in decoding metadata"
+                }
+                
+                guard let metadataKey = NCEndToEndEncryption.sharedManager().decryptAsymmetricData(metadataKeysData as Data!, privateKey: privateKey) else {
+                    return "Serious internal error in decoding metadata"
+                }
+                
+                // Encode to Base64
+                let metadataKeyData = Data(base64Encoded: metadataKey, options: NSData.Base64DecodingOptions(rawValue: 0))!
+                let metadataKeyBase64 = String(data: metadataKeyData, encoding: .utf8)
+                
+                decodeMetadataKeys[metadataKeys.key] = metadataKeyBase64
+            }
+            
+            for file in files {
+                
+                let fileNameIdentifier = file.key
+                let elementOfFile = file.value as e2eMetadata.filesKey
+                
+                let encrypted = elementOfFile.encrypted
+                let key = decodeMetadataKeys["\(elementOfFile.metadataKey)"]
+                
+                guard let decyptedMetadata = NCEndToEndEncryption.sharedManager().decryptMetadata(encrypted, key: key) else {
+                    return "Serious internal error in decoding metadata"
+                }
+                
+                do {
+                    
+                    let decode = try jsonDecoder.decode(e2eMetadata.encrypted.self, from: decyptedMetadata.data(using: .utf8)!)
+                    
+                    let object = tableE2eEncryption()
+                    
+                    object.account = account
+                    object.authenticationTag = elementOfFile.authenticationTag
+                    object.fileName = decode.filename
+                    object.fileNameIdentifier = fileNameIdentifier
+                    object.key = decode.key
+                    object.initializationVector = elementOfFile.initializationVector
+                    object.mimeType = decode.mimetype
+                    object.serverUrl = serverUrl
+                    object.version = decode.version
+                    
+                    // Write file parameter for decrypted on DB
+                    if NCManageDatabase.sharedInstance.addE2eEncryption(object) == false {
+                        return "Serious internal write DB"
+                    }
+                    
+                    // Write e2eMetaDataJSON on DB
+                    if NCManageDatabase.sharedInstance.setDirectoryE2EMetadataJSON(serverUrl: serverUrl, metadata: e2eMetaDataJSON) == false {
+                        return "Serious internal write DB"
+                    }
+                    
+                } catch let error {
+                    return "Serious internal error in decoding metadata ("+error.localizedDescription+")"
+                }
+            }
+            
+        } catch let error {
+            return "Serious internal error in decoding metadata ("+error.localizedDescription+")"
+        }
+        
+        return nil
+    }
+}