NCManageEndToEndEncryption.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. [appDelegate.endToEndInterface initEndToEndEncryption];
  152. } else {
  153. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  154. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  155. }];
  156. [alertController addAction:okAction];
  157. [self presentViewController:alertController animated:YES completion:nil];
  158. }
  159. }
  160. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  161. {
  162. [self deselectFormRow:sender];
  163. if ([[CCUtility getBlockCode] length]) {
  164. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  165. viewController.delegate = self;
  166. viewController.fromType = CCBKPasscodeFromCheckPassphrase;
  167. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  168. if ([CCUtility getSimplyBlockCode]) {
  169. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  170. viewController.passcodeInputView.maximumLength = 6;
  171. } else {
  172. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  173. viewController.passcodeInputView.maximumLength = 64;
  174. }
  175. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  176. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  177. viewController.touchIDManager = touchIDManager;
  178. viewController.title = NSLocalizedString(@"_e2e_settings_read_passphrase_", nil);
  179. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  180. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  181. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  182. [self presentViewController:navigationController animated:YES completion:nil];
  183. } else {
  184. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  185. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  186. }];
  187. [alertController addAction:okAction];
  188. [self presentViewController:alertController animated:YES completion:nil];
  189. }
  190. }
  191. - (void)removeLocallyEncryption:(XLFormRowDescriptor *)sender
  192. {
  193. [self deselectFormRow:sender];
  194. if ([[CCUtility getBlockCode] length]) {
  195. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  196. viewController.delegate = self;
  197. viewController.fromType = CCBKPasscodeFromRemoveEncryption;
  198. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  199. if ([CCUtility getSimplyBlockCode]) {
  200. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  201. viewController.passcodeInputView.maximumLength = 6;
  202. } else {
  203. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  204. viewController.passcodeInputView.maximumLength = 64;
  205. }
  206. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  207. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  208. viewController.touchIDManager = touchIDManager;
  209. viewController.title = NSLocalizedString(@"_e2e_settings_remove_", nil);
  210. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  211. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  212. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  213. [self presentViewController:navigationController animated:YES completion:nil];
  214. } else {
  215. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  216. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  217. }];
  218. [alertController addAction:okAction];
  219. [self presentViewController:alertController animated:YES completion:nil];
  220. }
  221. }
  222. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  223. {
  224. [self deselectFormRow:sender];
  225. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  226. metadataNet.action = actionDeleteEndToEndPublicKey;
  227. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:appDelegate.endToEndInterface metadataNet:metadataNet];
  228. }
  229. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  230. {
  231. [self deselectFormRow:sender];
  232. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  233. metadataNet.action = actionDeleteEndToEndPrivateKey;
  234. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:appDelegate.endToEndInterface metadataNet:metadataNet];
  235. }
  236. #pragma --------------------------------------------------------------------------------------------
  237. #pragma mark === BKPasscodeViewController ===
  238. #pragma --------------------------------------------------------------------------------------------
  239. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  240. {
  241. return _failedAttempts;
  242. }
  243. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  244. {
  245. return _lockUntilDate;
  246. }
  247. - (void)passcodeViewCloseButtonPressed:(id)sender
  248. {
  249. [self dismissViewControllerAnimated:YES completion:nil];
  250. }
  251. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  252. {
  253. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  254. _lockUntilDate = nil;
  255. _failedAttempts = 0;
  256. aResultHandler(YES);
  257. } else
  258. aResultHandler(NO);
  259. }
  260. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  261. {
  262. [aViewController dismissViewControllerAnimated:YES completion:nil];
  263. if (aViewController.fromType == CCBKPasscodeFromCheckPassphrase) {
  264. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:appDelegate.activeAccount];
  265. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  266. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  267. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  268. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  269. }];
  270. [alertController addAction:okAction];
  271. [self presentViewController:alertController animated:YES completion:nil];
  272. }
  273. if (aViewController.fromType == CCBKPasscodeFromRemoveEncryption) {
  274. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_e2e_settings_remove_", nil) message:NSLocalizedString(@"_e2e_settings_remove_message_", nil) preferredStyle:UIAlertControllerStyleAlert];
  275. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  276. [CCUtility clearAllKeysEndToEnd:appDelegate.activeAccount];
  277. [self initializeForm];
  278. }];
  279. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  280. NSLog(@"[LOG] Cancel action");
  281. }];
  282. [alertController addAction:okAction];
  283. [alertController addAction:cancelAction];
  284. [self presentViewController:alertController animated:YES completion:nil];
  285. }
  286. }
  287. @end