123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- //
- // NCEntoToEndInterface.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 03/04/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 NCEntoToEndInterface : NSObject, OCNetworkingDelegate {
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
-
- var deletePublicKey = false;
- var deletePrivateKey = false;
-
- var signPublicKey = false;
- var storePrivateKey = false;
-
- override init() {
- }
-
- // --------------------------------------------------------------------------------------------
- // MARK: End To End Encryption - PublicKey
- // --------------------------------------------------------------------------------------------
-
- @objc func initEndToEndEncryption() {
-
- let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
-
- metadataNet.action = actionGetEndToEndPublicKeys;
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
- metadataNet.action = actionGetEndToEndPrivateKeyCipher;
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
-
- metadataNet.action = actionGetEndToEndServerPublicKey;
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
- }
-
- func getEndToEndPublicKeysSuccess(_ metadataNet: CCMetadataNet!) {
-
- CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: metadataNet.key)
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPublicKeys, note: "E2E PublicKeys present on Server and stored to keychain", type: k_activityTypeSuccess, verbose: false, activeUrl: "")
- }
-
- func getEndToEndPublicKeysFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPublicKeys, note: message as String!, type: k_activityTypeFailure, verbose: false, activeUrl: "")
-
- switch errorCode {
-
- case 400:
-
- appDelegate.messageNotification("E2E public keys", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- case 404:
-
- // public keys couldn't be found
- // remove keychain
- CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: nil)
-
- guard let publicKey = NCEndToEndEncryption.sharedManager().createEnd(toEndPublicKey: appDelegate.activeUserID, directoryUser: appDelegate.directoryUser) else {
-
- appDelegate.messageNotification("E2E public keys", description: "E2E Error to create PublicKey", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPublicKeys, note: "E2E Error to create PublicKey", type: k_activityTypeFailure, verbose: false, activeUrl: "")
-
- return
- }
-
- let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
-
- metadataNet.action = actionSignEndToEndPublicKey;
- metadataNet.key = publicKey;
-
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
-
- case 409:
-
- appDelegate.messageNotification("E2E public keys", description: "forbidden: the user can't access the public keys", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- default:
-
- appDelegate.messageNotification("E2E public keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- }
- }
- func signEnd(toEndPublicKeySuccess metadataNet: CCMetadataNet!) {
- // Insert CSR To Cheychain end delete
- guard let publicKey = NCEndToEndEncryption.sharedManager().getCSRFromDisk(appDelegate.directoryUser, delete: true) else {
-
- appDelegate.messageNotification("E2E public key", description: "Error : publicKey not present", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
-
- return
- }
-
- // OK signed key locally keychain
- CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: publicKey)
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionSignEndToEndPublicKey, note: "E2E PublicKey sign on Server and stored locally", type: k_activityTypeFailure, verbose: false, activeUrl: "")
-
- signPublicKey = true
- if (storePrivateKey) {
- signPublicKey = false
- storePrivateKey = false
- alertController("_success_", message: "_e2e_settings_activated_")
- }
- }
- func signEnd(toEndPublicKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-
- signPublicKey = false
-
- appDelegate.messageNotification("E2E sign public keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionSignEndToEndPublicKey, note: message, type: k_activityTypeFailure, verbose: false, activeUrl: "")
- }
-
- func deleteEnd(toEndPublicKeySuccess metadataNet: CCMetadataNet!) {
-
- deletePublicKey = true
- if (deletePrivateKey) {
- deletePublicKey = false
- deletePrivateKey = false
- initEndToEndEncryption()
- }
- }
-
- func deleteEnd(toEndPublicKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-
- deletePublicKey = false
-
- appDelegate.messageNotification("E2E delete public key", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
- }
-
- // --------------------------------------------------------------------------------------------
- // MARK: End To End Encryption - PrivateKey
- // --------------------------------------------------------------------------------------------
-
- func getEndToEndPrivateKeyCipherSuccess(_ metadataNet: CCMetadataNet!) {
-
- guard let privateKey = NCEndToEndEncryption.sharedManager().decryptPrivateKeyCipher(metadataNet.key, passphrase: appDelegate.e2ePassphrase) else {
-
- appDelegate.messageNotification("E2E decrypt private key", description: "E2E Error to decrypt Private Key", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: "E2E Error to decrypt Private Key", type: k_activityTypeFailure, verbose: false, activeUrl: "")
-
- return
- }
-
- // Save to keychain
- CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: privateKey)
-
- // Save passphrase to keychain
- CCUtility.setEndToEndPassphrase(appDelegate.activeAccount, passphrase:appDelegate.e2ePassphrase)
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: "E2E PrivateKey present on Server and stored to keychain", type: k_activityTypeSuccess, verbose: false, activeUrl: "")
- }
-
- func getEndToEndPrivateKeyCipherFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: message as String!, type: k_activityTypeFailure, verbose: false, activeUrl: "")
-
- switch errorCode {
-
- case 400:
-
- appDelegate.messageNotification("E2E public keys", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- case 404:
-
- // private keys couldn't be found
- // remove keychain
- CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: nil)
- CCUtility.setEndToEndPassphrase(appDelegate.activeAccount, passphrase: nil)
-
- guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().createEnd(toEndPrivateKey: appDelegate.activeUserID, directoryUser: appDelegate.directoryUser, passphrase: appDelegate.e2ePassphrase) else {
-
- appDelegate.messageNotification("E2E private keys", description: "E2E Error to create PublicKey chiper", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndPrivateKeyCipher, note: "E2E Error to create PublicKey chiper", type: k_activityTypeFailure, verbose: false, activeUrl: "")
-
- return
- }
-
- let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
-
- metadataNet.action = actionStoreEndToEndPrivateKeyCipher
- metadataNet.key = privateKeyChiper
- metadataNet.password = appDelegate.e2ePassphrase
-
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
-
- case 409:
-
- appDelegate.messageNotification("E2E private keys", description: "forbidden: the user can't access the private keys", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- default:
-
- appDelegate.messageNotification("E2E private keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- }
- }
-
- func storeEnd(toEndPrivateKeyCipherSuccess metadataNet: CCMetadataNet!) {
-
- // Insert PrivateKey (end delete) and passphrase to Cheychain
- guard let privateKey = NCEndToEndEncryption.sharedManager().getPrivateKey(fromDisk: appDelegate.directoryUser, delete: true) else {
-
- appDelegate.messageNotification("E2E private key", description: "Error : privateKey not present", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
-
- return
- }
-
- CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: privateKey)
- CCUtility.setEndToEndPassphrase(appDelegate.activeAccount, passphrase:metadataNet.password)
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionStoreEndToEndPrivateKeyCipher, note: "E2E PrivateKey stored on Server and stored locally", type: k_activityTypeSuccess, verbose: false, activeUrl: "")
-
- storePrivateKey = true
- if (signPublicKey) {
- signPublicKey = false
- storePrivateKey = false
- alertController("_success_", message: "_e2e_settings_activated_")
- }
- }
-
- func storeEnd(toEndPrivateKeyCipherFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-
- storePrivateKey = false
-
- appDelegate.messageNotification("E2E sign private key", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionStoreEndToEndPrivateKeyCipher, note: message, type: k_activityTypeFailure, verbose: false, activeUrl: "")
- }
-
- func deleteEnd(toEndPrivateKeySuccess metadataNet: CCMetadataNet!) {
-
- deletePrivateKey = true
- if (deletePublicKey) {
- deletePublicKey = false
- deletePrivateKey = false
- initEndToEndEncryption()
- }
- }
-
- func deleteEnd(toEndPrivateKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-
- deletePrivateKey = false
-
- appDelegate.messageNotification("E2E delete private key", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
- }
-
- // --------------------------------------------------------------------------------------------
- // MARK: End To End Encryption - Server PublicKey
- // --------------------------------------------------------------------------------------------
-
- func getEndToEndServerPublicKeySuccess(_ metadataNet: CCMetadataNet!) {
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndServerPublicKey, note: "E2E Server PublicKey present on Server and stored to keychain", type: k_activityTypeSuccess, verbose: false, activeUrl: "")
- }
-
- func getEndToEndServerPublicKeyFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-
- NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionGetEndToEndServerPublicKey, note: message as String!, type: k_activityTypeFailure, verbose: false, activeUrl: "")
-
- switch (errorCode) {
-
- case 400:
-
- appDelegate.messageNotification("E2E Server public key", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- case 404:
-
- appDelegate.messageNotification("E2E Server public key", description: "Server publickey doesn't exists", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- case 409:
- appDelegate.messageNotification("E2E Server public key", description: "forbidden: the user can't access the Server publickey", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
-
- default:
- appDelegate.messageNotification("E2E Server public key", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- }
- }
-
- // --------------------------------------------------------------------------------------------
- // MARK: Mark/Delete Encrypted Folder
- // --------------------------------------------------------------------------------------------
-
- func markEnd(toEndFolderEncryptedSuccess metadataNet: CCMetadataNet!) {
- print("E2E mark folder success")
- }
-
- func markEnd(toEndFolderEncryptedFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-
- // Unauthorized
- if (errorCode == kOCErrorServerUnauthorized) {
- appDelegate.openLoginView(appDelegate.activeMain, loginType: loginModifyPasswordUser)
- }
-
- if (errorCode != kOCErrorServerUnauthorized) {
-
- appDelegate.messageNotification("_error_", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- }
- }
-
- @objc func markEndToEndFolderEncrypted(_ metadata: tableMetadata) {
-
- let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
- metadataNet.action = actionMarkEndToEndFolderEncrypted;
- metadataNet.fileID = metadata.fileID;
-
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
- }
-
- func deletemarkEnd(toEndFolderEncryptedSuccess metadataNet: CCMetadataNet!) {
- print("E2E delete folder success")
- }
-
- func deletemarkEnd(toEndFolderEncryptedFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
-
- // Unauthorized
- if (errorCode == kOCErrorServerUnauthorized) {
- appDelegate.openLoginView(appDelegate.activeMain, loginType: loginModifyPasswordUser)
- }
-
- if (errorCode != kOCErrorServerUnauthorized) {
-
- appDelegate.messageNotification("_error_", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- }
- }
-
- @objc func deletemarkEndToEndFolderEncrypted(_ metadata: tableMetadata) {
-
- let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
-
- metadataNet.action = actionDeletemarkEndToEndFolderEncrypted;
- metadataNet.fileID = metadata.fileID;
-
- appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
- }
-
- // --------------------------------------------------------------------------------------------
- // MARK: Form
- // --------------------------------------------------------------------------------------------
-
- func alertController(_ title: String, message: String) {
-
- let alertController = UIAlertController(title: NSLocalizedString(title, comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
-
- let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
- }
- alertController.addAction(OKAction)
- appDelegate.activeMain.present(alertController, animated: true)
- }
- }
|