NCEndToEndInitialize.swift 18 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 UIKit
  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 NCGlobal.shared.errorBadRequest:
  50. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "bad request: unpredictable internal error", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  51. case NCGlobal.shared.errorResourceNotFound:
  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: NCGlobal.shared.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 NCGlobal.shared.errorBadRequest:
  64. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "bad request: unpredictable internal error", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  65. case NCGlobal.shared.errorConflict:
  66. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: "conflict: a public key for the user already exists", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  67. default:
  68. NCContentPresenter.shared.messageNotification("E2E sign publicKey", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  69. }
  70. }
  71. }
  72. case NCGlobal.shared.errorConflict:
  73. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: "forbidden: the user can't access the public keys", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  74. default:
  75. NCContentPresenter.shared.messageNotification("E2E get publicKey", description: errorDescription, delay: NCGlobal.shared.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: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  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. //TODO: verifi if publicKey == NCCommunication.shared.getE2EEPublicKey
  103. // + (NSString *)getEndToEndPublicKey:(NSString *)account;
  104. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  105. // Clear Table
  106. NCManageDatabase.shared.clearTable(tableDirectory.self, account: account)
  107. NCManageDatabase.shared.clearTable(tableE2eEncryption.self, account: account)
  108. self.delegate?.endToEndInitializeSuccess()
  109. } else if errorCode != 0 {
  110. switch (errorCode) {
  111. case NCGlobal.shared.errorBadRequest:
  112. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  113. case NCGlobal.shared.errorResourceNotFound:
  114. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  115. case NCGlobal.shared.errorConflict:
  116. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  117. default:
  118. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  119. }
  120. }
  121. }
  122. })
  123. let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
  124. }
  125. alertController.addAction(ok)
  126. alertController.addAction(cancel)
  127. alertController.addTextField { (textField) -> Void in
  128. passphraseTextField = textField
  129. passphraseTextField?.placeholder = "Enter passphrase (12 words)"
  130. }
  131. self.appDelegate.window?.rootViewController?.present(alertController, animated: true)
  132. } else if errorCode != 0 {
  133. switch errorCode {
  134. case NCGlobal.shared.errorBadRequest:
  135. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "bad request: unpredictable internal error", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  136. case NCGlobal.shared.errorResourceNotFound:
  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 privateKeyString: NSString?
  143. guard let privateKeyChiper = NCEndToEndEncryption.sharedManager().encryptPrivateKey(self.appDelegate.userId, directory: CCUtility.getDirectoryUserData(), passphrase: e2ePassphrase, privateKey: &privateKeyString) else {
  144. NCContentPresenter.shared.messageNotification("E2E privateKey", description: "Serious internal error to create PrivateKey chiper", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  145. return
  146. }
  147. // privateKeyChiper
  148. print(privateKeyChiper)
  149. NCCommunication.shared.storeE2EEPrivateKey(privateKey: privateKeyChiper) { (account, privateKey, errorCode, errorDescription) in
  150. if (errorCode == 0 && account == self.appDelegate.account) {
  151. CCUtility.setEndToEndPrivateKey(account, privateKey: privateKeyString! as String)
  152. CCUtility.setEndToEndPassphrase(account, passphrase: e2ePassphrase)
  153. // request publicKey Server()
  154. NCCommunication.shared.getE2EEServerPublicKey { (account, publicKey, errorCode, errorDescription) in
  155. if (errorCode == 0 && account == self.appDelegate.account) {
  156. //TODO: verifi if publicKey == NCCommunication.shared.getE2EEPublicKey
  157. // + (NSString *)getEndToEndPublicKey:(NSString *)account;
  158. CCUtility.setEndToEndPublicKeyServer(account, publicKey: publicKey)
  159. // Clear Table
  160. NCManageDatabase.shared.clearTable(tableDirectory.self, account: account)
  161. NCManageDatabase.shared.clearTable(tableE2eEncryption.self, account: account)
  162. self.delegate?.endToEndInitializeSuccess()
  163. } else if errorCode != 0 {
  164. switch (errorCode) {
  165. case NCGlobal.shared.errorBadRequest:
  166. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "bad request: unpredictable internal error", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  167. case NCGlobal.shared.errorResourceNotFound:
  168. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "Server publickey doesn't exists", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  169. case NCGlobal.shared.errorConflict:
  170. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: "forbidden: the user can't access the Server publickey", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  171. default:
  172. NCContentPresenter.shared.messageNotification("E2E Server publicKey", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  173. }
  174. }
  175. }
  176. } else if errorCode != 0 {
  177. switch errorCode {
  178. case NCGlobal.shared.errorBadRequest:
  179. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "bad request: unpredictable internal error", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  180. case NCGlobal.shared.errorConflict:
  181. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: "conflict: a private key for the user already exists", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  182. default:
  183. NCContentPresenter.shared.messageNotification("E2E store privateKey", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  184. }
  185. }
  186. }
  187. }
  188. alertController.addAction(OKAction)
  189. self.appDelegate.window?.rootViewController?.present(alertController, animated: true)
  190. case NCGlobal.shared.errorConflict:
  191. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: "forbidden: the user can't access the private key", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  192. default:
  193. NCContentPresenter.shared.messageNotification("E2E get privateKey", description: errorDescription,delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  194. }
  195. }
  196. }
  197. }
  198. }