NCManageEndToEndEncryption.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. /*
  117. #ifdef DEBUG
  118. // Section DELETE KEYS -------------------------------------------------
  119. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  120. [form addFormSection:section];
  121. // Delete publicKey
  122. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePublicKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PublicKey", nil)];
  123. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  124. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  125. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  126. row.action.formSelector = @selector(deletePublicKey:);
  127. [section addFormRow:row];
  128. // Delete privateKey
  129. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  130. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  131. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  132. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  133. row.action.formSelector = @selector(deletePrivateKey:);
  134. [section addFormRow:row];
  135. #endif
  136. */
  137. self.form = form;
  138. }
  139. -(void)reloadForm
  140. {
  141. [self initializeForm];
  142. }
  143. #pragma --------------------------------------------------------------------------------------------
  144. #pragma mark === Action ===
  145. #pragma --------------------------------------------------------------------------------------------
  146. - (void)startE2E:(XLFormRowDescriptor *)sender
  147. {
  148. [self deselectFormRow:sender];
  149. [app.endToEndInterface initEndToEndEncryption];
  150. }
  151. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  152. {
  153. [self deselectFormRow:sender];
  154. if ([[CCUtility getBlockCode] length]) {
  155. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  156. viewController.delegate = self;
  157. viewController.fromType = CCBKPasscodeFromCheckPassphrase;
  158. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  159. if ([CCUtility getSimplyBlockCode]) {
  160. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  161. viewController.passcodeInputView.maximumLength = 6;
  162. } else {
  163. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  164. viewController.passcodeInputView.maximumLength = 64;
  165. }
  166. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  167. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  168. viewController.touchIDManager = touchIDManager;
  169. viewController.title = NSLocalizedString(@"_e2e_settings_read_passphrase_", nil);
  170. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  171. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  172. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  173. [self presentViewController:navigationController animated:YES completion:nil];
  174. } else {
  175. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  176. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  177. }];
  178. [alertController addAction:okAction];
  179. [self presentViewController:alertController animated:YES completion:nil];
  180. }
  181. }
  182. - (void)removeLocallyEncryption:(XLFormRowDescriptor *)sender
  183. {
  184. [self deselectFormRow:sender];
  185. if ([[CCUtility getBlockCode] length]) {
  186. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  187. viewController.delegate = self;
  188. viewController.fromType = CCBKPasscodeFromRemoveEncryption;
  189. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  190. if ([CCUtility getSimplyBlockCode]) {
  191. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  192. viewController.passcodeInputView.maximumLength = 6;
  193. } else {
  194. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  195. viewController.passcodeInputView.maximumLength = 64;
  196. }
  197. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  198. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  199. viewController.touchIDManager = touchIDManager;
  200. viewController.title = NSLocalizedString(@"_e2e_settings_remove_", nil);
  201. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  202. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  203. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  204. [self presentViewController:navigationController animated:YES completion:nil];
  205. } else {
  206. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  207. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  208. }];
  209. [alertController addAction:okAction];
  210. [self presentViewController:alertController animated:YES completion:nil];
  211. }
  212. }
  213. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  214. {
  215. [self deselectFormRow:sender];
  216. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  217. metadataNet.action = actionDeleteEndToEndPublicKey;
  218. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  219. }
  220. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  221. {
  222. [self deselectFormRow:sender];
  223. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  224. metadataNet.action = actionDeleteEndToEndPrivateKey;
  225. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  226. }
  227. #pragma --------------------------------------------------------------------------------------------
  228. #pragma mark === BKPasscodeViewController ===
  229. #pragma --------------------------------------------------------------------------------------------
  230. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  231. {
  232. return _failedAttempts;
  233. }
  234. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  235. {
  236. return _lockUntilDate;
  237. }
  238. - (void)passcodeViewCloseButtonPressed:(id)sender
  239. {
  240. [self dismissViewControllerAnimated:YES completion:nil];
  241. }
  242. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  243. {
  244. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  245. _lockUntilDate = nil;
  246. _failedAttempts = 0;
  247. aResultHandler(YES);
  248. } else
  249. aResultHandler(NO);
  250. }
  251. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  252. {
  253. [aViewController dismissViewControllerAnimated:YES completion:nil];
  254. if (aViewController.fromType == CCBKPasscodeFromCheckPassphrase) {
  255. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:app.activeAccount];
  256. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  257. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  258. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  259. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  260. }];
  261. [alertController addAction:okAction];
  262. [self presentViewController:alertController animated:YES completion:nil];
  263. }
  264. if (aViewController.fromType == CCBKPasscodeFromRemoveEncryption) {
  265. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_e2e_settings_remove_", nil) message:NSLocalizedString(@"_e2e_settings_remove_message_", nil) preferredStyle:UIAlertControllerStyleAlert];
  266. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  267. [CCUtility clearAllKeysEndToEnd:app.activeAccount];
  268. [self initializeForm];
  269. }];
  270. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  271. NSLog(@"[LOG] Cancel action");
  272. }];
  273. [alertController addAction:okAction];
  274. [alertController addAction:cancelAction];
  275. [self presentViewController:alertController animated:YES completion:nil];
  276. }
  277. }
  278. @end