NCManageEndToEndEncryption.m 21 KB

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