NCManageEndToEndEncryption.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //
  2. // NCManageEndToEndEncryption.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/10/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 ()
  28. {
  29. AppDelegate *appDelegate;
  30. NSUInteger _failedAttempts;
  31. NSDate *_lockUntilDate;
  32. }
  33. @end
  34. @implementation NCManageEndToEndEncryption
  35. - (id)initWithCoder:(NSCoder *)aDecoder
  36. {
  37. self = [super initWithCoder:aDecoder];
  38. if (self) {
  39. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadForm) name:@"reloadManageEndToEndEncryption" object:nil];
  41. [self initializeForm];
  42. }
  43. return self;
  44. }
  45. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  46. {
  47. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  48. if (self) {
  49. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadForm) name:@"reloadManageEndToEndEncryption" object:nil];
  51. [self initializeForm];
  52. }
  53. return self;
  54. }
  55. - (void)initializeForm
  56. {
  57. XLFormDescriptor *form ;
  58. XLFormSectionDescriptor *section;
  59. XLFormRowDescriptor *row;
  60. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_e2e_settings_", nil)];
  61. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilites];
  62. if (capabilities.endToEndEncryption == NO) {
  63. // Section SERVICE NOT AVAILABLE -------------------------------------------------
  64. section = [XLFormSectionDescriptor formSection];
  65. [form addFormSection:section];
  66. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_not_available_", nil)];
  67. [row.cellConfig setObject:[UIImage imageNamed:@"no_red"] forKey:@"imageView.image"];
  68. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  69. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  70. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  71. [section addFormRow:row];
  72. self.form = form;
  73. return;
  74. }
  75. if ([CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  76. // Section SERVICE ACTIVATED -------------------------------------------------
  77. section = [XLFormSectionDescriptor formSection];
  78. [form addFormSection:section];
  79. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_activated_", nil)];
  80. [row.cellConfig setObject:[UIImage imageNamed:@"ok_green"] forKey:@"imageView.image"];
  81. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  82. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  83. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  84. [section addFormRow:row];
  85. // Section PASSPHRASE -------------------------------------------------
  86. section = [XLFormSectionDescriptor formSection];
  87. [form addFormSection:section];
  88. // Read Passphrase
  89. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"readPassphrase" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  90. [row.cellConfig setObject:[UIImage imageNamed:@"e2eReadPassphrase"] forKey:@"imageView.image"];
  91. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  92. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  93. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  94. row.action.formSelector = @selector(readPassphrase:);
  95. [section addFormRow:row];
  96. // Section DELETE -------------------------------------------------
  97. section = [XLFormSectionDescriptor formSection];
  98. [form addFormSection:section];
  99. // remove locally Encryption
  100. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"removeLocallyEncryption" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_remove_", nil)];
  101. [row.cellConfig setObject:[UIImage imageNamed:@"e2eRemoveLocallyEncryption"] forKey:@"imageView.image"];
  102. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  103. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  104. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  105. row.action.formSelector = @selector(removeLocallyEncryption:);
  106. [section addFormRow:row];
  107. } else {
  108. // Section START E2E -------------------------------------------------
  109. section = [XLFormSectionDescriptor formSection];
  110. [form addFormSection:section];
  111. // Start e2e
  112. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startE2E" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_start_", nil)];
  113. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  114. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  115. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  116. row.action.formSelector = @selector(startE2E:);
  117. [section addFormRow:row];
  118. }
  119. #ifdef DEBUG
  120. // Section DELETE KEYS -------------------------------------------------
  121. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  122. [form addFormSection:section];
  123. // Delete publicKey
  124. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePublicKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PublicKey", nil)];
  125. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  126. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  127. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  128. row.action.formSelector = @selector(deletePublicKey:);
  129. [section addFormRow:row];
  130. // Delete privateKey
  131. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  132. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  133. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  134. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  135. row.action.formSelector = @selector(deletePrivateKey:);
  136. [section addFormRow:row];
  137. #endif
  138. self.form = form;
  139. }
  140. -(void)reloadForm
  141. {
  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. [self presentViewController:navigationController animated:YES completion:nil];
  170. } else {
  171. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  172. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  173. }];
  174. [alertController addAction:okAction];
  175. [self presentViewController:alertController animated:YES completion:nil];
  176. }
  177. }
  178. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  179. {
  180. [self deselectFormRow:sender];
  181. if ([[CCUtility getBlockCode] length]) {
  182. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  183. viewController.delegate = self;
  184. viewController.fromType = CCBKPasscodeFromCheckPassphrase;
  185. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  186. if ([CCUtility getSimplyBlockCode]) {
  187. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  188. viewController.passcodeInputView.maximumLength = 6;
  189. } else {
  190. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  191. viewController.passcodeInputView.maximumLength = 64;
  192. }
  193. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  194. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  195. viewController.touchIDManager = touchIDManager;
  196. viewController.title = NSLocalizedString(@"_e2e_settings_read_passphrase_", nil);
  197. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  198. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  199. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  200. [self presentViewController:navigationController animated:YES completion:nil];
  201. } else {
  202. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  203. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  204. }];
  205. [alertController addAction:okAction];
  206. [self presentViewController:alertController animated:YES completion:nil];
  207. }
  208. }
  209. - (void)removeLocallyEncryption:(XLFormRowDescriptor *)sender
  210. {
  211. [self deselectFormRow:sender];
  212. if ([[CCUtility getBlockCode] length]) {
  213. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  214. viewController.delegate = self;
  215. viewController.fromType = CCBKPasscodeFromRemoveEncryption;
  216. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  217. if ([CCUtility getSimplyBlockCode]) {
  218. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  219. viewController.passcodeInputView.maximumLength = 6;
  220. } else {
  221. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  222. viewController.passcodeInputView.maximumLength = 64;
  223. }
  224. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  225. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  226. viewController.touchIDManager = touchIDManager;
  227. viewController.title = NSLocalizedString(@"_e2e_settings_remove_", nil);
  228. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  229. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  230. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  231. [self presentViewController:navigationController animated:YES completion:nil];
  232. } else {
  233. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  234. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  235. }];
  236. [alertController addAction:okAction];
  237. [self presentViewController:alertController animated:YES completion:nil];
  238. }
  239. }
  240. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  241. {
  242. [self deselectFormRow:sender];
  243. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  244. metadataNet.action = actionDeleteEndToEndPublicKey;
  245. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:appDelegate.endToEndInterface metadataNet:metadataNet];
  246. }
  247. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  248. {
  249. [self deselectFormRow:sender];
  250. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  251. metadataNet.action = actionDeleteEndToEndPrivateKey;
  252. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:appDelegate.endToEndInterface metadataNet:metadataNet];
  253. }
  254. #pragma --------------------------------------------------------------------------------------------
  255. #pragma mark === BKPasscodeViewController ===
  256. #pragma --------------------------------------------------------------------------------------------
  257. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  258. {
  259. return _failedAttempts;
  260. }
  261. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  262. {
  263. return _lockUntilDate;
  264. }
  265. - (void)passcodeViewCloseButtonPressed:(id)sender
  266. {
  267. [self dismissViewControllerAnimated:YES completion:nil];
  268. }
  269. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  270. {
  271. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  272. _lockUntilDate = nil;
  273. _failedAttempts = 0;
  274. aResultHandler(YES);
  275. } else
  276. aResultHandler(NO);
  277. }
  278. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  279. {
  280. [aViewController dismissViewControllerAnimated:YES completion:nil];
  281. if (aViewController.fromType == CCBKPasscodeFromStartEncryption) {
  282. [appDelegate.endToEndInterface initEndToEndEncryption];
  283. }
  284. if (aViewController.fromType == CCBKPasscodeFromCheckPassphrase) {
  285. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:appDelegate.activeAccount];
  286. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  287. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  288. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  289. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  290. }];
  291. [alertController addAction:okAction];
  292. [self presentViewController:alertController animated:YES completion:nil];
  293. }
  294. if (aViewController.fromType == CCBKPasscodeFromRemoveEncryption) {
  295. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_e2e_settings_remove_", nil) message:NSLocalizedString(@"_e2e_settings_remove_message_", nil) preferredStyle:UIAlertControllerStyleAlert];
  296. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  297. [CCUtility clearAllKeysEndToEnd:appDelegate.activeAccount];
  298. [self initializeForm];
  299. }];
  300. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  301. NSLog(@"[LOG] Cancel action");
  302. }];
  303. [alertController addAction:okAction];
  304. [alertController addAction:cancelAction];
  305. [self presentViewController:alertController animated:YES completion:nil];
  306. }
  307. }
  308. @end