NCManageEndToEndEncryption.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. //
  2. // NCManageEndToEndEncryption.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/10/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 "NCManageEndToEndEncryption.h"
  24. #import "AppDelegate.h"
  25. #import "CCNetworking.h"
  26. #import "NCBridgeSwift.h"
  27. @interface NCManageEndToEndEncryption () <NCEndToEndInitializeDelegate>
  28. {
  29. AppDelegate *appDelegate;
  30. }
  31. @end
  32. @implementation NCManageEndToEndEncryption
  33. - (void)initializeForm
  34. {
  35. XLFormDescriptor *form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_e2e_settings_", nil)];
  36. XLFormSectionDescriptor *section;
  37. XLFormRowDescriptor *row;
  38. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:appDelegate.activeAccount];
  39. if (capabilities.endToEndEncryption == NO) {
  40. // Section SERVICE NOT AVAILABLE -------------------------------------------------
  41. section = [XLFormSectionDescriptor formSection];
  42. [form addFormSection:section];
  43. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_not_available_", nil)];
  44. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  45. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"closeCircle"] width:50 height:50 color:[UIColor redColor]] forKey:@"imageView.image"];
  46. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  47. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  48. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  49. [section addFormRow:row];
  50. self.tableView.showsVerticalScrollIndicator = NO;
  51. self.form = form;
  52. return;
  53. }
  54. if ([CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  55. // Section SERVICE ACTIVATED -------------------------------------------------
  56. section = [XLFormSectionDescriptor formSection];
  57. [form addFormSection:section];
  58. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_activated_", nil)];
  59. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  60. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"selectFull"] width:50 height:50 color:[UIColor greenColor]] forKey:@"imageView.image"];
  61. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  62. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  63. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  64. [section addFormRow:row];
  65. // Section PASSPHRASE -------------------------------------------------
  66. section = [XLFormSectionDescriptor formSection];
  67. [form addFormSection:section];
  68. // Read Passphrase
  69. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"readPassphrase" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  70. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  71. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"e2eReadPassphrase"] width:50 height:50 color:NCBrandColor.sharedInstance.icon] forKey:@"imageView.image"];
  72. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  73. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  74. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  75. row.action.formSelector = @selector(readPassphrase:);
  76. [section addFormRow:row];
  77. // Section DELETE -------------------------------------------------
  78. section = [XLFormSectionDescriptor formSection];
  79. [form addFormSection:section];
  80. // remove locally Encryption
  81. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"removeLocallyEncryption" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_remove_", nil)];
  82. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  83. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"lock"] width:50 height:50 color:NCBrandColor.sharedInstance.icon] forKey:@"imageView.image"];
  84. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  85. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  86. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  87. row.action.formSelector = @selector(removeLocallyEncryption:);
  88. [section addFormRow:row];
  89. } else {
  90. // Section START E2E -------------------------------------------------
  91. section = [XLFormSectionDescriptor formSection];
  92. [form addFormSection:section];
  93. // Start e2e
  94. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startE2E" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_start_", nil)];
  95. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  96. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  97. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  98. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  99. row.action.formSelector = @selector(startE2E:);
  100. [section addFormRow:row];
  101. }
  102. #ifdef DEBUG
  103. // Section DELETE KEYS -------------------------------------------------
  104. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  105. [form addFormSection:section];
  106. // Delete publicKey
  107. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePublicKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PublicKey", nil)];
  108. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  109. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  110. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  111. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  112. row.action.formSelector = @selector(deletePublicKey:);
  113. [section addFormRow:row];
  114. // Delete privateKey
  115. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  116. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  117. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  118. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  119. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  120. row.action.formSelector = @selector(deletePrivateKey:);
  121. [section addFormRow:row];
  122. #endif
  123. self.tableView.showsVerticalScrollIndicator = NO;
  124. self.form = form;
  125. }
  126. - (void)viewDidLoad
  127. {
  128. [super viewDidLoad];
  129. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  130. // E2EE
  131. self.endToEndInitialize = [NCEndToEndInitialize new];
  132. self.endToEndInitialize.delegate = self;
  133. // changeTheming
  134. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:k_notificationCenter_changeTheming object:nil];
  135. [self changeTheming];
  136. }
  137. - (void)changeTheming
  138. {
  139. [appDelegate changeTheming:self tableView:self.tableView collectionView:nil form:true];
  140. [self initializeForm];
  141. }
  142. #pragma --------------------------------------------------------------------------------------------
  143. #pragma mark === Action ===
  144. #pragma --------------------------------------------------------------------------------------------
  145. - (void)startE2E:(XLFormRowDescriptor *)sender
  146. {
  147. [self deselectFormRow:sender];
  148. if ([[CCUtility getBlockCode] length]) {
  149. /*
  150. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  151. viewController.delegate = self;
  152. viewController.fromType = CCBKPasscodeFromStartEncryption;
  153. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  154. if ([CCUtility getSimplyBlockCode]) {
  155. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  156. viewController.passcodeInputView.maximumLength = 6;
  157. } else {
  158. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  159. viewController.passcodeInputView.maximumLength = 64;
  160. }
  161. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  162. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  163. viewController.touchIDManager = touchIDManager;
  164. viewController.title = NSLocalizedString(@"_e2e_settings_start_", nil);
  165. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  166. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  167. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  168. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  169. [self presentViewController:navigationController animated:YES completion:nil];
  170. */
  171. } else {
  172. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  173. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  174. }];
  175. [alertController addAction:okAction];
  176. [self presentViewController:alertController animated:YES completion:nil];
  177. }
  178. }
  179. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  180. {
  181. [self deselectFormRow:sender];
  182. /*
  183. if ([[CCUtility getBlockCode] length]) {
  184. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  185. viewController.delegate = self;
  186. viewController.fromType = CCBKPasscodeFromCheckPassphrase;
  187. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  188. if ([CCUtility getSimplyBlockCode]) {
  189. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  190. viewController.passcodeInputView.maximumLength = 6;
  191. } else {
  192. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  193. viewController.passcodeInputView.maximumLength = 64;
  194. }
  195. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  196. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  197. viewController.touchIDManager = touchIDManager;
  198. viewController.title = NSLocalizedString(@"_e2e_settings_read_passphrase_", nil);
  199. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  200. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  201. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  202. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  203. [self presentViewController:navigationController animated:YES completion:nil];
  204. } else {
  205. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  206. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  207. }];
  208. [alertController addAction:okAction];
  209. [self presentViewController:alertController animated:YES completion:nil];
  210. }
  211. */
  212. }
  213. - (void)removeLocallyEncryption:(XLFormRowDescriptor *)sender
  214. {
  215. [self deselectFormRow:sender];
  216. /*
  217. if ([[CCUtility getBlockCode] length]) {
  218. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  219. viewController.delegate = self;
  220. viewController.fromType = CCBKPasscodeFromRemoveEncryption;
  221. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  222. if ([CCUtility getSimplyBlockCode]) {
  223. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  224. viewController.passcodeInputView.maximumLength = 6;
  225. } else {
  226. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  227. viewController.passcodeInputView.maximumLength = 64;
  228. }
  229. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  230. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  231. viewController.touchIDManager = touchIDManager;
  232. viewController.title = NSLocalizedString(@"_e2e_settings_remove_", nil);
  233. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  234. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  235. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  236. navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  237. [self presentViewController:navigationController animated:YES completion:nil];
  238. } else {
  239. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  240. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  241. }];
  242. [alertController addAction:okAction];
  243. [self presentViewController:alertController animated:YES completion:nil];
  244. }
  245. */
  246. }
  247. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  248. {
  249. [self deselectFormRow:sender];
  250. [[NCNetworkingEndToEnd sharedManager] deleteEndToEndPublicKeyWithAccount:appDelegate.activeAccount completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  251. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  252. [[NCContentPresenter shared] messageNotification:@"E2E delete publicKey" description:@"Success" delay:k_dismissAfterSecond type:messageTypeSuccess errorCode:0];
  253. } else {
  254. [[NCContentPresenter shared] messageNotification:@"E2E delete publicKey" description:message delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  255. }
  256. }];
  257. }
  258. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  259. {
  260. [self deselectFormRow:sender];
  261. [[NCNetworkingEndToEnd sharedManager] deleteEndToEndPrivateKeyWithAccount:appDelegate.activeAccount completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  262. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  263. [[NCContentPresenter shared] messageNotification:@"E2E delete privateKey" description:@"Success" delay:k_dismissAfterSecond type:messageTypeSuccess errorCode:0];
  264. } else {
  265. [[NCContentPresenter shared] messageNotification:@"E2E delete privateKey" description:message delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  266. }
  267. }];
  268. }
  269. #pragma --------------------------------------------------------------------------------------------
  270. #pragma mark === Delegate ===
  271. #pragma --------------------------------------------------------------------------------------------
  272. - (void)endToEndInitializeSuccess
  273. {
  274. // Reload All Datasource
  275. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_clearDateReadDataSource object:nil];
  276. [self initializeForm];
  277. }
  278. #pragma --------------------------------------------------------------------------------------------
  279. #pragma mark === BKPasscodeViewController ===
  280. #pragma --------------------------------------------------------------------------------------------
  281. /*
  282. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  283. {
  284. return _failedAttempts;
  285. }
  286. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  287. {
  288. return _lockUntilDate;
  289. }
  290. - (void)passcodeViewCloseButtonPressed:(id)sender
  291. {
  292. [self dismissViewControllerAnimated:YES completion:nil];
  293. }
  294. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  295. {
  296. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  297. _lockUntilDate = nil;
  298. _failedAttempts = 0;
  299. aResultHandler(YES);
  300. } else
  301. aResultHandler(NO);
  302. }
  303. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  304. {
  305. [aViewController dismissViewControllerAnimated:YES completion:nil];
  306. if (aViewController.fromType == CCBKPasscodeFromStartEncryption) {
  307. [self.endToEndInitialize initEndToEndEncryption];
  308. }
  309. if (aViewController.fromType == CCBKPasscodeFromCheckPassphrase) {
  310. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:appDelegate.activeAccount];
  311. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  312. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  313. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  314. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  315. }];
  316. [alertController addAction:okAction];
  317. [self presentViewController:alertController animated:YES completion:nil];
  318. }
  319. if (aViewController.fromType == CCBKPasscodeFromRemoveEncryption) {
  320. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_e2e_settings_remove_", nil) message:NSLocalizedString(@"_e2e_settings_remove_message_", nil) preferredStyle:UIAlertControllerStyleAlert];
  321. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  322. [CCUtility clearAllKeysEndToEnd:appDelegate.activeAccount];
  323. [self initializeForm];
  324. }];
  325. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  326. NSLog(@"[LOG] Cancel action");
  327. }];
  328. [alertController addAction:okAction];
  329. [alertController addAction:cancelAction];
  330. [self presentViewController:alertController animated:YES completion:nil];
  331. }
  332. }
  333. */
  334. @end