NCManageEndToEndEncryption.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. NSUInteger _failedAttempts;
  30. NSDate *_lockUntilDate;
  31. }
  32. @end
  33. @implementation NCManageEndToEndEncryption
  34. - (id)initWithCoder:(NSCoder *)aDecoder
  35. {
  36. self = [super initWithCoder:aDecoder];
  37. if (self) {
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadForm) name:@"reloadManageEndToEndEncryption" object:nil];
  39. [self initializeForm];
  40. }
  41. return self;
  42. }
  43. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  44. {
  45. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  46. if (self) {
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadForm) name:@"reloadManageEndToEndEncryption" object:nil];
  48. [self initializeForm];
  49. }
  50. return self;
  51. }
  52. - (void)initializeForm
  53. {
  54. XLFormDescriptor *form ;
  55. XLFormSectionDescriptor *section;
  56. XLFormRowDescriptor *row;
  57. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_e2e_settings_", nil)];
  58. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilites];
  59. if (capabilities.endToEndEncryption == NO) {
  60. // Section SERVICE NOT AVAILABLE -------------------------------------------------
  61. section = [XLFormSectionDescriptor formSection];
  62. [form addFormSection:section];
  63. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_not_available_", nil)];
  64. [row.cellConfig setObject:[UIImage imageNamed:@"no_red"] forKey:@"imageView.image"];
  65. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  66. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  67. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  68. [section addFormRow:row];
  69. self.form = form;
  70. return;
  71. }
  72. if ([CCUtility isEndToEndEnabled:app.activeAccount]) {
  73. // Section SERVICE ACTIVATED -------------------------------------------------
  74. section = [XLFormSectionDescriptor formSection];
  75. [form addFormSection:section];
  76. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_activated_", nil)];
  77. [row.cellConfig setObject:[UIImage imageNamed:@"ok_green"] forKey:@"imageView.image"];
  78. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  79. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  80. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  81. [section addFormRow:row];
  82. // Section PASSPHRASE -------------------------------------------------
  83. section = [XLFormSectionDescriptor formSection];
  84. [form addFormSection:section];
  85. // Read Passphrase
  86. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"readPassphrase" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  87. [row.cellConfig setObject:[UIImage imageNamed:@"e2eReadPassphrase"] forKey:@"imageView.image"];
  88. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  89. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  90. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  91. row.action.formSelector = @selector(readPassphrase:);
  92. [section addFormRow:row];
  93. // Section DELETE -------------------------------------------------
  94. section = [XLFormSectionDescriptor formSection];
  95. [form addFormSection:section];
  96. // remove locally Encryption
  97. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"removeLocallyEncryption" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_remove_", nil)];
  98. [row.cellConfig setObject:[UIImage imageNamed:@"e2eRemoveLocallyEncryption"] forKey:@"imageView.image"];
  99. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  100. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  101. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  102. row.action.formSelector = @selector(removeLocallyEncryption:);
  103. [section addFormRow:row];
  104. } else {
  105. // Section START E2E -------------------------------------------------
  106. section = [XLFormSectionDescriptor formSection];
  107. [form addFormSection:section];
  108. // Start e2e
  109. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startE2E" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_start_", nil)];
  110. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  111. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  112. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  113. row.action.formSelector = @selector(startE2E:);
  114. [section addFormRow:row];
  115. }
  116. #ifdef DEBUG
  117. // Section DELETE KEYS -------------------------------------------------
  118. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  119. [form addFormSection:section];
  120. // Delete publicKey
  121. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePublicKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PublicKey", nil)];
  122. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  123. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  124. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  125. row.action.formSelector = @selector(deletePublicKey:);
  126. [section addFormRow:row];
  127. // Delete privateKey
  128. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  129. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  130. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  131. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  132. row.action.formSelector = @selector(deletePrivateKey:);
  133. [section addFormRow:row];
  134. #endif
  135. self.form = form;
  136. }
  137. -(void)reloadForm
  138. {
  139. [self initializeForm];
  140. }
  141. #pragma --------------------------------------------------------------------------------------------
  142. #pragma mark === Action ===
  143. #pragma --------------------------------------------------------------------------------------------
  144. - (void)startE2E:(XLFormRowDescriptor *)sender
  145. {
  146. [self deselectFormRow:sender];
  147. [app.endToEndInterface initEndToEndEncryption];
  148. }
  149. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  150. {
  151. [self deselectFormRow:sender];
  152. if ([[CCUtility getBlockCode] length]) {
  153. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  154. viewController.delegate = self;
  155. viewController.fromType = CCBKPasscodeFromCheckPassphrase;
  156. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  157. if ([CCUtility getSimplyBlockCode]) {
  158. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  159. viewController.passcodeInputView.maximumLength = 6;
  160. } else {
  161. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  162. viewController.passcodeInputView.maximumLength = 64;
  163. }
  164. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  165. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  166. viewController.touchIDManager = touchIDManager;
  167. viewController.title = NSLocalizedString(@"_e2e_settings_read_passphrase_", nil);
  168. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  169. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  170. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  171. [self presentViewController:navigationController animated:YES completion:nil];
  172. } else {
  173. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  174. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  175. }];
  176. [alertController addAction:okAction];
  177. [self presentViewController:alertController animated:YES completion:nil];
  178. }
  179. }
  180. - (void)removeLocallyEncryption:(XLFormRowDescriptor *)sender
  181. {
  182. [self deselectFormRow:sender];
  183. if ([[CCUtility getBlockCode] length]) {
  184. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  185. viewController.delegate = self;
  186. viewController.fromType = CCBKPasscodeFromRemoveEncryption;
  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_remove_", 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. [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)deletePublicKey:(XLFormRowDescriptor *)sender
  212. {
  213. [self deselectFormRow:sender];
  214. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  215. metadataNet.action = actionDeleteEndToEndPublicKey;
  216. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  217. }
  218. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  219. {
  220. [self deselectFormRow:sender];
  221. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  222. metadataNet.action = actionDeleteEndToEndPrivateKey;
  223. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  224. }
  225. #pragma --------------------------------------------------------------------------------------------
  226. #pragma mark === BKPasscodeViewController ===
  227. #pragma --------------------------------------------------------------------------------------------
  228. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  229. {
  230. return _failedAttempts;
  231. }
  232. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  233. {
  234. return _lockUntilDate;
  235. }
  236. - (void)passcodeViewCloseButtonPressed:(id)sender
  237. {
  238. [self dismissViewControllerAnimated:YES completion:nil];
  239. }
  240. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  241. {
  242. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  243. _lockUntilDate = nil;
  244. _failedAttempts = 0;
  245. aResultHandler(YES);
  246. } else
  247. aResultHandler(NO);
  248. }
  249. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  250. {
  251. [aViewController dismissViewControllerAnimated:YES completion:nil];
  252. if (aViewController.fromType == CCBKPasscodeFromCheckPassphrase) {
  253. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:app.activeAccount];
  254. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  255. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  256. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  257. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  258. }];
  259. [alertController addAction:okAction];
  260. [self presentViewController:alertController animated:YES completion:nil];
  261. }
  262. if (aViewController.fromType == CCBKPasscodeFromRemoveEncryption) {
  263. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_e2e_settings_remove_", nil) message:NSLocalizedString(@"_e2e_settings_remove_message_", nil) preferredStyle:UIAlertControllerStyleAlert];
  264. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  265. [CCUtility clearAllKeysEndToEnd:app.activeAccount];
  266. [self initializeForm];
  267. }];
  268. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  269. NSLog(@"[LOG] Cancel action");
  270. }];
  271. [alertController addAction:okAction];
  272. [alertController addAction:cancelAction];
  273. [self presentViewController:alertController animated:YES completion:nil];
  274. }
  275. }
  276. @end