NCEndToEndInitialize.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. import NCCommunication
  25. @objc protocol NCEndToEndInitializeDelegate {
  26. func endToEndInitializeSuccess()
  27. }
  28. class NCEndToEndInitialize : NSObject {
  29. @objc weak var delegate: NCEndToEndInitializeDelegate?
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. override init() {
  32. }
  33. // --------------------------------------------------------------------------------------------
  34. // MARK: Initialize
  35. // --------------------------------------------------------------------------------------------
  36. @objc func initEndToEndEncryption() {
  37. // Clear all keys
  38. CCUtility.clearAllKeysEnd(toEnd: appDelegate.account)
  39. self.getPublicKey()
  40. }
  41. func getPublicKey() {
  42. NCCommunication.shared.getE2EEPublicKey { (account, publicKey, errorCode, errorDescription) in
  43. if (errorCode == 0 && account == self.appDelegate.account) {
  44. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  45. // Request PrivateKey chiper to Server
  46. self.getPrivateKeyCipher()
  47. } else if errorCode != 0 {
  48. switch errorCode {
  49. case Int(k_CCErrorBadRequest):
  50. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  51. case Int(k_CCErrorResourceNotFound):
  52. guard let csr = NCEndToEndEncryption.sharedManager().createCSR(self.appDelegate.userID, directory: CCUtility.getDirectoryUserData()) else {
  53. NCContentPresenter.shared.messageNotification("E2E Csr", description: "Error to create Csr", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  54. return
  55. }
  56. NCCommunication.shared.signE2EEPublicKey(publicKey: csr) { (account, publicKey, errorCode, errorDescription) in
  57. if (errorCode == 0 && account == self.appDelegate.account) {
  58. CCUtility.setEndToEndPublicKey(account, publicKey: publicKey)
  59. // Request PrivateKey chiper to Server
  60. self.getPrivateKeyCipher()
  61. } else if errorCode != 0 {
  62. switch errorCode {
  63. case Int(k_CCErrorBadRequest):
  64. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  65. case Int(k_CCErrorConflict):
  66. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "conflict: a public key for the user already exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  67. default:
  68. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  69. }
  70. }
  71. }
  72. case Int(k_CCErrorConflict):
  73. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "forbidden: the user can't access the public keys", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  74. default:
  75. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  76. }
  77. }
  78. }
  79. }
  80. func getPrivateKeyCipher() {
  81. // Request PrivateKey chiper to Server
  82. NCCommunication.shared.getE2EEPrivateKey { (account, privateKeyChiper, errorCode, errorDescription) in
  83. if (errorCode == 0 && account == self.appDelegate.account) {
  84. // request Passphrase
  85. var passphraseTextField: UITextField?
  86. let alertController = UIAlertController(title: NSLocalizedString("_e2e_passphrase_request_title_", comment: ""), message: NSLocalizedString("_e2e_passphrase_request_message_", comment: ""), preferredStyle: .alert)
  87. let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
  88. let passphrase = passphraseTextField?.text
  89. let publicKey = CCUtility.getEndToEndPublicKey(self.appDelegate.account)
  90. guard let privateKey = (NCEndToEndEncryption.sharedManager().decryptPrivateKey(privateKeyChiper, passphrase: passphrase, publicKey: publicKey)) else {
  91. NCContentPresenter.shared.messageNotification("E2E decrypt privateKey", description: "Serious internal error to decrypt Private Key", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  92. return
  93. }
  94. // privateKey
  95. print(privateKey)
  96. // Save to keychain
  97. CCUtility.setEndToEndPrivateKey(self.appDelegate.account, privateKey: privateKey)
  98. CCUtility.setEndToEndPassphrase(self.appDelegate.account, passphrase:passphrase)
  99. // request publicKey Server()
  100. NCCommunication.shared.getE2EEServerPublicKey { (account, publicKey, errorCode, errorDescription) in
  101. if (errorCode == 0 && account == self.appDelegate.account) {
  102. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  103. // Clear Table
  104. NCManageDatabase.shared.clearTable(tableDirectory.self, account: account)
  105. NCManageDatabase.shared.clearTable(tableE2eEncryption.self, account: account)
  106. self.delegate?.endToEndInitializeSuccess()
  107. } else if errorCode != 0 {
  108. switch (errorCode) {
  109. case Int(k_CCErrorBadRequest):
  110. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  111. case Int(k_CCErrorResourceNotFound):
  112. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  113. case Int(k_CCErrorConflict):
  114. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  115. default:
  116. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  117. }
  118. }
  119. }
  120. })
  121. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
  122. }
  123. alertController.addAction(ok)
  124. alertController.addAction(cancel)
  125. alertController.addTextField { (textField) -> Void in
  126. passphraseTextField = textField
  127. passphraseTextField?.placeholder = "Enter passphrase (12 words)"
  128. }
  129. self.appDelegate.window.rootViewController?.present(alertController, animated: true)
  130. } else if errorCode != 0 {
  131. switch errorCode {
  132. case Int(k_CCErrorBadRequest):
  133. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  134. case Int(k_CCErrorResourceNotFound):
  135. // message
  136. let e2ePassphrase = NYMnemonic.generateString(128, language: "english")
  137. let message = "\n" + NSLocalizedString("_e2e_settings_view_passphrase_", comment: "") + "\n\n" + e2ePassphrase!
  138. let alertController = UIAlertController(title: NSLocalizedString("_e2e_settings_title_", comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
  139. let OKAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  140. var privateKeyString: NSString?
  141. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.userID, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKeyString) else {
  142. NCContentPresenter.shared.messageNotification("E2E privateKey", description: "Serious internal error to create PrivateKey chiper", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  143. return
  144. }
  145. // privateKeyChiper
  146. print(privateKeyChiper)
  147. NCCommunication.shared.storeE2EEPrivateKey(privateKey: privateKeyChiper) { (account, privateKey, errorCode, errorDescription) in
  148. if (errorCode == 0 && account == self.appDelegate.account) {
  149. CCUtility.setEndToEndPrivateKey(account, privateKey: privateKeyString! as String)
  150. CCUtility.setEndToEndPassphrase(account, passphrase: e2ePassphrase)
  151. // request publicKey Server()
  152. NCCommunication.shared.getE2EEServerPublicKey { (account, publicKey, errorCode, errorDescription) in
  153. if (errorCode == 0 && account == self.appDelegate.account) {
  154. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  155. // Clear Table
  156. NCManageDatabase.shared.clearTable(tableDirectory.self, account: account)
  157. NCManageDatabase.shared.clearTable(tableE2eEncryption.self, account: account)
  158. self.delegate?.endToEndInitializeSuccess()
  159. } else if errorCode != 0 {
  160. switch (errorCode) {
  161. case Int(k_CCErrorBadRequest):
  162. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  163. case Int(k_CCErrorResourceNotFound):
  164. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  165. case Int(k_CCErrorConflict):
  166. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  167. default:
  168. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  169. }
  170. }
  171. }
  172. } else if errorCode != 0 {
  173. switch errorCode {
  174. case Int(k_CCErrorBadRequest):
  175. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "bad request: unpredictable internal error", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  176. case Int(k_CCErrorConflict):
  177. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "conflict: a private key for the user already exists", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  178. default:
  179. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  180. }
  181. }
  182. }
  183. }
  184. alertController.addAction(OKAction)
  185. self.appDelegate.window.rootViewController?.present(alertController, animated: true)
  186. case Int(k_CCErrorConflict):
  187. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "forbidden: the user can't access the private key", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  188. default:
  189. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: errorDescription,delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  190. }
  191. }
  192. }
  193. }
  194. }