NCEndToEndInitialize.swift 19 KB

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