NCEndToEndInitialize.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // NCEndToEndInitialize.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/04/17.
  6. // Copyright © 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. @objc protocol NCEndToEndInitializeDelegate {
  25. func endToEndInitializeSuccess()
  26. }
  27. class NCEndToEndInitialize : NSObject {
  28. @objc weak var delegate: NCEndToEndInitializeDelegate?
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. override init() {
  31. }
  32. // --------------------------------------------------------------------------------------------
  33. // MARK: Initialize
  34. // --------------------------------------------------------------------------------------------
  35. @objc func initEndToEndEncryption() {
  36. // Clear all keys
  37. CCUtility.clearAllKeysEnd(toEnd: appDelegate.activeAccount)
  38. //metadataNet.action = actionGetEndToEndPublicKeys
  39. }
  40. func getPrivateKeyCipher() {
  41. //metadataNet.action = actionGetEndToEndPrivateKeyCipher
  42. }
  43. func getPublicKeyServer() {
  44. //metadataNet.action = actionGetEndToEndServerPublicKey
  45. }
  46. // --------------------------------------------------------------------------------------------
  47. // MARK: Manage PublicKey
  48. // --------------------------------------------------------------------------------------------
  49. func getEndToEndPublicKeysSuccess(_ metadataNet: CCMetadataNet!) {
  50. CCUtility.setEndToEndPublicKey(appDelegate.activeAccount, publicKey: metadataNet.key)
  51. // Request PrivateKey chiper to Server
  52. getPrivateKeyCipher()
  53. }
  54. func getEndToEndPublicKeysFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  55. switch errorCode {
  56. case 400:
  57. appDelegate.messageNotification("E2E get publicKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  58. case 404:
  59. guard let csr = NCEndToEndEncryption.sharedManager().createCSR(appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData()) else {
  60. appDelegate.messageNotification("E2E Csr", description: "Error to create Csr", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  61. return
  62. }
  63. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  64. metadataNet.action = actionSignEndToEndPublicKey;
  65. metadataNet.key = csr;
  66. //appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  67. case 409:
  68. appDelegate.messageNotification("E2E get publicKey", description: "forbidden: the user can't access the public keys", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  69. default:
  70. appDelegate.messageNotification("E2E get publicKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  71. }
  72. }
  73. func signEnd(toEndPublicKeySuccess metadataNet: CCMetadataNet!) {
  74. CCUtility.setEndToEndPublicKey(appDelegate.activeAccount, publicKey: metadataNet.key)
  75. // Request PrivateKey chiper to Server
  76. getPrivateKeyCipher()
  77. }
  78. func signEnd(toEndPublicKeyFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  79. switch errorCode {
  80. case 400:
  81. appDelegate.messageNotification("E2E sign publicKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  82. case 409:
  83. appDelegate.messageNotification("E2E sign publicKey", description: "conflict: a public key for the user already exists", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  84. default:
  85. appDelegate.messageNotification("E2E sign publicKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  86. }
  87. }
  88. // --------------------------------------------------------------------------------------------
  89. // MARK: Manage PrivateKey
  90. // --------------------------------------------------------------------------------------------
  91. func getEndToEndPrivateKeyCipherSuccess(_ metadataNet: CCMetadataNet!) {
  92. // request Passphrase
  93. var passphraseTextField: UITextField?
  94. let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
  95. //TEST
  96. /*
  97. if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
  98. let fileURL = dir.appendingPathComponent("privatekey.txt")
  99. //writing
  100. do {
  101. try metadataNet.key.write(to: fileURL, atomically: false, encoding: .utf8)
  102. }
  103. catch {/* error handling here */}
  104. }
  105. */
  106. //
  107. let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
  108. let passphrase = passphraseTextField?.text
  109. let publicKey = CCUtility.getEndToEndPublicKey(self.appDelegate.activeAccount)
  110. guard let privateKey = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(metadataNet.key, passphrase: passphrase, publicKey: publicKey)) else {
  111. self.appDelegate.messageNotification("E2E decrypt privateKey", description: "Serious internal error to decrypt Private Key", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  112. return
  113. }
  114. // privateKey
  115. print(privateKey)
  116. // Save to keychain
  117. CCUtility.setEndToEndPrivateKey(self.appDelegate.activeAccount, privateKey: privateKey)
  118. CCUtility.setEndToEndPassphrase(self.appDelegate.activeAccount, passphrase:passphrase)
  119. // request publicKey Server()
  120. self.getPublicKeyServer()
  121. })
  122. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
  123. }
  124. alertController.addAction(ok)
  125. alertController.addAction(cancel)
  126. alertController.addTextField { (textField) -> Void in
  127. passphraseTextField = textField
  128. passphraseTextField?.placeholder = "Enter passphrase (12 words)"
  129. }
  130. appDelegate.activeMain.present(alertController, animated: true)
  131. }
  132. func getEndToEndPrivateKeyCipherFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  133. switch errorCode {
  134. case 400:
  135. appDelegate.messageNotification("E2E get privateKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  136. case 404:
  137. // message
  138. let e2ePassphrase = NYMnemonic.generateString(128, language: "english")
  139. let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase!
  140. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  141. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  142. var privateKey: NSString?
  143. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.activeUserID, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKey) else {
  144. self.appDelegate.messageNotification("E2E privateKey", description: "Serious internal error to create PrivateKey chiper", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  145. return
  146. }
  147. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: self.appDelegate.activeAccount)
  148. metadataNet.action = actionStoreEndToEndPrivateKeyCipher
  149. metadataNet.key = privateKey! as String
  150. metadataNet.keyCipher = privateKeyChiper
  151. metadataNet.password = e2ePassphrase
  152. //self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  153. }
  154. alertController.addAction(OKAction)
  155. appDelegate.activeMain.present(alertController, animated: true)
  156. case 409:
  157. appDelegate.messageNotification("E2E get privateKey", description: "forbidden: the user can't access the private key", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  158. default:
  159. appDelegate.messageNotification("E2E get privateKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  160. }
  161. }
  162. func storeEnd(toEndPrivateKeyCipherSuccess metadataNet: CCMetadataNet!) {
  163. CCUtility.setEndToEndPrivateKey(appDelegate.activeAccount, privateKey: metadataNet.key)
  164. CCUtility.setEndToEndPassphrase(appDelegate.activeAccount, passphrase:metadataNet.password)
  165. // request publicKey Server()
  166. self.getPublicKeyServer()
  167. }
  168. func storeEnd(toEndPrivateKeyCipherFailure metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  169. switch errorCode {
  170. case 400:
  171. appDelegate.messageNotification("E2E store privateKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  172. case 409:
  173. appDelegate.messageNotification("E2E store privateKey", description: "conflict: a private key for the user already exists", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  174. default:
  175. appDelegate.messageNotification("E2E store privateKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  176. }
  177. }
  178. // --------------------------------------------------------------------------------------------
  179. // MARK: Manage Server PublicKey
  180. // --------------------------------------------------------------------------------------------
  181. func getEndToEndServerPublicKeySuccess(_ metadataNet: CCMetadataNet!) {
  182. CCUtility.setEndToEndPublicKeyServer(appDelegate.activeAccount, publicKey: metadataNet.key)
  183. // Clear Table
  184. NCManageDatabase.sharedInstance.clearTable(tableDirectory.self, account: appDelegate.activeAccount)
  185. NCManageDatabase.sharedInstance.clearTable(tableE2eEncryption.self, account: appDelegate.activeAccount)
  186. self.delegate?.endToEndInitializeSuccess()
  187. }
  188. func getEndToEndServerPublicKeyFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  189. switch (errorCode) {
  190. case 400:
  191. appDelegate.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  192. case 404:
  193. appDelegate.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  194. case 409:
  195. appDelegate.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  196. default:
  197. appDelegate.messageNotification("E2E Server publicKey", description: message as String, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  198. }
  199. }
  200. }