NCEntoToEndInterface.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // NCEntoToEndInterface.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/04/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 NCEntoToEndInterface : NSObject, OCNetworkingDelegate {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. override init() {
  27. }
  28. // --------------------------------------------------------------------------------------------
  29. // MARK: End To End Encryption - PublicKey
  30. // --------------------------------------------------------------------------------------------
  31. @objc func initEndToEndEncryption() {
  32. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  33. metadataNet.action = actionGetEndToEndPublicKeys;
  34. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  35. metadataNet.action = actionGetEndToEndPrivateKeyCipher;
  36. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  37. metadataNet.action = actionGetEndToEndServerPublicKey;
  38. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  39. }
  40. @objc func getEndToEndPublicKeysSuccess(_ metadataNet: CCMetadataNet) {
  41. CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: metadataNet.key)
  42. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: metadataNet.selector, note: "E2E PublicKeys present on Server and stored to keychain", type: k_activityTypeSuccess, verbose: true, activeUrl: appDelegate.activeUrl)
  43. }
  44. @objc func getEndToEndPublicKeysFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  45. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionSignEndToEndPublicKey, note: message as String!, type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  46. switch errorCode {
  47. case 400:
  48. appDelegate.messageNotification("E2E public keys", description: "bad request: unpredictable internal error" as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  49. case 404:
  50. // public keys couldn't be found
  51. // remove keychain
  52. CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: nil)
  53. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  54. let publicKey = NCEndToEndEncryption.sharedManager().createEnd(toEndPublicKey: appDelegate.activeUserID, directoryUser: appDelegate.directoryUser)
  55. if (publicKey != nil) {
  56. metadataNet.action = actionSignEndToEndPublicKey;
  57. metadataNet.key = publicKey;
  58. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  59. } else {
  60. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: actionSignEndToEndPublicKey, note: "E2E Error to create PublicKeyEncoded", type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  61. }
  62. case 409:
  63. 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)
  64. default:
  65. appDelegate.messageNotification("E2E public keys", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  66. }
  67. }
  68. @objc func signEndToEndPublicKeySuccess(_ metadataNet: CCMetadataNet) {
  69. // Insert CSR To Cheychain end delete
  70. let publicKey = NCEndToEndEncryption.sharedManager().getCSRFromDisk(appDelegate.directoryUser, delete: true)
  71. // OK signed key locally keychain
  72. CCUtility.setEndToEndPublicKeySign(appDelegate.activeAccount, publicKey: publicKey)
  73. NCManageDatabase.sharedInstance.addActivityClient("", fileID: "", action: k_activityDebugActionEndToEndEncryption, selector: metadataNet.selector, note: "E2E PublicKey sign on Server and stored locally", type: k_activityTypeFailure, verbose: true, activeUrl: appDelegate.activeUrl)
  74. }
  75. func signEnd(toEndPublicKeySuccess metadataNet: CCMetadataNet!) {
  76. <#code#>
  77. }
  78. @objc func signEndToEndPublicKeySuccess(_ metadataNet: CCMetadataNet, message: NSString) {
  79. - (void)signEndToEndPublicKeyFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  80. {
  81. [app messageNotification:@"E2E sign public key" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  82. // Activity
  83. [[NCManageDatabase sharedInstance] addActivityClient:@"" fileID:@"" action:k_activityDebugActionEndToEndEncryption selector:metadataNet.selector note:message type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:app.activeUrl];
  84. }
  85. // --------------------------------------------------------------------------------------------
  86. // MARK: Mark/Delete Encrypted Folder
  87. // --------------------------------------------------------------------------------------------
  88. @objc func markEndToEndFolderEncryptedSuccess(_ metadataNet: CCMetadataNet) {
  89. print("E2E mark folder success")
  90. }
  91. @objc func markEndToEndFolderEncryptedFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  92. {
  93. // Unauthorized
  94. if (errorCode == kOCErrorServerUnauthorized) {
  95. appDelegate.openLoginView(appDelegate.activeMain, loginType: loginModifyPasswordUser)
  96. }
  97. if (errorCode != kOCErrorServerUnauthorized) {
  98. appDelegate.messageNotification("_error_", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  99. }
  100. }
  101. @objc func markEndToEndFolderEncrypted(_ metadata: tableMetadata) {
  102. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  103. metadataNet.action = actionMarkEndToEndFolderEncrypted;
  104. metadataNet.fileID = metadata.fileID;
  105. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  106. }
  107. @objc func deleteEndToEndFolderEncryptedSuccess(_ metadataNet: CCMetadataNet) {
  108. print("E2E delete folder success")
  109. }
  110. @objc func deleteEndToEndFolderEncryptedFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  111. {
  112. // Unauthorized
  113. if (errorCode == kOCErrorServerUnauthorized) {
  114. appDelegate.openLoginView(appDelegate.activeMain, loginType: loginModifyPasswordUser)
  115. }
  116. if (errorCode != kOCErrorServerUnauthorized) {
  117. appDelegate.messageNotification("_error_", description: message as String!, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  118. }
  119. }
  120. @objc func deleteEndToEndFolderEncrypted(_ metadata: tableMetadata) {
  121. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  122. metadataNet.action = actionDeleteEndToEndFolderEncrypted;
  123. metadataNet.fileID = metadata.fileID;
  124. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  125. }
  126. }